Skip to content

Commit

Permalink
perf(serde_v8): serde_v8::StringOrBuffer return JS ArrayBuffer inst…
Browse files Browse the repository at this point in the history
…ead of Uint8Array (#16360)

Towards #16315
  • Loading branch information
littledivy authored Oct 20, 2022
1 parent d3736f1 commit bfc1fb8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ext/websocket/01_websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@
if (this.binaryType === "blob") {
data = new Blob([value]);
} else {
data = value.buffer;
data = value;
}

const event = new MessageEvent("message", {
Expand Down
19 changes: 18 additions & 1 deletion serde_v8/magic/string_or_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,24 @@ impl ToV8 for StringOrBuffer {
scope: &mut v8::HandleScope<'a>,
) -> Result<v8::Local<'a, v8::Value>, crate::Error> {
match self {
Self::Buffer(buf) => crate::to_v8(scope, buf),
Self::Buffer(buf) => {
let buf: Box<[u8]> = match buf {
ZeroCopyBuf::FromV8(buf) => {
let value: &[u8] = buf;
value.into()
}
ZeroCopyBuf::Temp(_) => unreachable!(),
ZeroCopyBuf::ToV8(ref mut x) => {
x.take().expect("ZeroCopyBuf was empty")
}
};
let backing_store =
v8::ArrayBuffer::new_backing_store_from_boxed_slice(buf);
Ok(
v8::ArrayBuffer::with_backing_store(scope, &backing_store.into())
.into(),
)
}
Self::String(s) => crate::to_v8(scope, s),
}
}
Expand Down

0 comments on commit bfc1fb8

Please sign in to comment.