summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/bindings.rs15
-rw-r--r--core/encode_decode_test.js6
2 files changed, 20 insertions, 1 deletions
diff --git a/core/bindings.rs b/core/bindings.rs
index 3745abf69..88bdf7f30 100644
--- a/core/bindings.rs
+++ b/core/bindings.rs
@@ -632,7 +632,20 @@ fn encode(
};
let text_str = text.to_rust_string_lossy(scope);
let text_bytes = text_str.as_bytes().to_vec().into_boxed_slice();
- let buf = boxed_slice_to_uint8array(scope, text_bytes);
+
+ let buf = if text_bytes.is_empty() {
+ let ab = v8::ArrayBuffer::new(scope, 0);
+ v8::Uint8Array::new(ab, 0, 0).expect("Failed to create UintArray8")
+ } else {
+ let buf_len = text_bytes.len();
+ let backing_store =
+ v8::ArrayBuffer::new_backing_store_from_boxed_slice(text_bytes);
+ let mut backing_store_shared = backing_store.make_shared();
+ let ab =
+ v8::ArrayBuffer::with_backing_store(scope, &mut backing_store_shared);
+ v8::Uint8Array::new(ab, 0, buf_len).expect("Failed to create UintArray8")
+ };
+
rv.set(buf.into())
}
diff --git a/core/encode_decode_test.js b/core/encode_decode_test.js
index 8a366dd66..294144593 100644
--- a/core/encode_decode_test.js
+++ b/core/encode_decode_test.js
@@ -27,12 +27,18 @@ function main() {
108, 100
];
+ const empty = Deno.core.encode("");
+ if (empty.length !== 0) throw new Error("assert");
+
assertArrayEquals(Array.from(Deno.core.encode("𝓽𝓮𝔁𝓽")), fixture1);
assertArrayEquals(
Array.from(Deno.core.encode("Hello \udc12\ud834 World")),
fixture2
);
+ const emptyBuf = Deno.core.decode(new Uint8Array(0));
+ if (emptyBuf !== "") throw new Error("assert");
+
assert(Deno.core.decode(new Uint8Array(fixture1)) === "𝓽𝓮𝔁𝓽");
assert(Deno.core.decode(new Uint8Array(fixture2)) === "Hello �� World");
}