1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-22 06:09:25 -05:00

cleanup(core): use ZeroCopyBuf for serialize & encode bindings (#10435)

This commit is contained in:
Aaron O'Mullan 2021-05-01 03:09:58 +02:00 committed by GitHub
parent 578f2ba45e
commit c5be20aa67
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -495,20 +495,8 @@ 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 = if text_bytes.is_empty() {
let ab = v8::ArrayBuffer::new(scope, 0);
v8::Uint8Array::new(scope, 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 backing_store_shared = backing_store.make_shared();
let ab = v8::ArrayBuffer::with_backing_store(scope, &backing_store_shared);
v8::Uint8Array::new(scope, ab, 0, buf_len)
.expect("Failed to create UintArray8")
};
rv.set(buf.into())
let zbuf: ZeroCopyBuf = text_bytes.into();
rv.set(to_v8(scope, zbuf).unwrap())
}
fn decode(
@ -516,15 +504,13 @@ fn decode(
args: v8::FunctionCallbackArguments,
mut rv: v8::ReturnValue,
) {
let view = match v8::Local::<v8::ArrayBufferView>::try_from(args.get(0)) {
Ok(view) => view,
let zero_copy: ZeroCopyBuf = match serde_v8::from_v8(scope, args.get(0)) {
Ok(zbuf) => zbuf,
Err(_) => {
throw_type_error(scope, "Invalid argument");
return;
}
};
let zero_copy = ZeroCopyBuf::new(scope, view);
let buf = &zero_copy;
// Strip BOM
@ -580,19 +566,8 @@ fn serialize(
match value_serializer.write_value(scope.get_current_context(), args.get(0)) {
Some(true) => {
let vector = value_serializer.release();
let buf = {
let buf_len = vector.len();
let backing_store = v8::ArrayBuffer::new_backing_store_from_boxed_slice(
vector.into_boxed_slice(),
);
let backing_store_shared = backing_store.make_shared();
let ab =
v8::ArrayBuffer::with_backing_store(scope, &backing_store_shared);
v8::Uint8Array::new(scope, ab, 0, buf_len)
.expect("Failed to create UintArray8")
};
rv.set(buf.into());
let zbuf: ZeroCopyBuf = vector.into();
rv.set(to_v8(scope, zbuf).unwrap());
}
_ => {
throw_type_error(scope, "Invalid argument");
@ -605,15 +580,13 @@ fn deserialize(
args: v8::FunctionCallbackArguments,
mut rv: v8::ReturnValue,
) {
let view = match v8::Local::<v8::ArrayBufferView>::try_from(args.get(0)) {
Ok(view) => view,
let zero_copy: ZeroCopyBuf = match serde_v8::from_v8(scope, args.get(0)) {
Ok(zbuf) => zbuf,
Err(_) => {
throw_type_error(scope, "Invalid argument");
return;
}
};
let zero_copy = ZeroCopyBuf::new(scope, view);
let buf = &zero_copy;
let serialize_deserialize = Box::new(SerializeDeserialize {});