diff --git a/libdeno/binding.cc b/libdeno/binding.cc index 6d88af3603..bc155b6db6 100644 --- a/libdeno/binding.cc +++ b/libdeno/binding.cc @@ -168,25 +168,18 @@ static v8::Local ImportBuf(v8::Isolate* isolate, deno_buf buf) { } } -static deno_buf ExportBuf(v8::Isolate* isolate, - v8::Local view) { +static deno_buf GetContents(v8::Isolate* isolate, + v8::Local view) { auto ab = view->Buffer(); - auto contents = ab->Externalize(); - + auto contents = ab->GetContents(); deno_buf buf; buf.alloc_ptr = reinterpret_cast(contents.Data()); buf.alloc_len = contents.ByteLength(); buf.data_ptr = buf.alloc_ptr + view->ByteOffset(); buf.data_len = view->ByteLength(); - - // Prevent JS from modifying buffer contents after exporting. - ab->Neuter(); - return buf; } -static void FreeBuf(deno_buf buf) { free(buf.alloc_ptr); } - // Sets the recv callback. void Recv(const v8::FunctionCallbackInfo& args) { v8::Isolate* isolate = args.GetIsolate(); @@ -218,20 +211,13 @@ void Send(const v8::FunctionCallbackInfo& args) { CHECK_EQ(args.Length(), 1); v8::Local ab_v = args[0]; CHECK(ab_v->IsArrayBufferView()); - auto buf = ExportBuf(isolate, v8::Local::Cast(ab_v)); + auto buf = GetContents(isolate, v8::Local::Cast(ab_v)); DCHECK_EQ(d->currentArgs, nullptr); d->currentArgs = &args; d->cb(d, buf); - // Buffer is only valid until the end of the callback. - // TODO(piscisaureus): - // It's possible that data in the buffer is needed after the callback - // returns, e.g. when the handler offloads work to a thread pool, therefore - // make the callback responsible for releasing the buffer. - FreeBuf(buf); - d->currentArgs = nullptr; } diff --git a/libdeno/libdeno_test.cc b/libdeno/libdeno_test.cc index d3650788fd..c8b0adfd02 100644 --- a/libdeno/libdeno_test.cc +++ b/libdeno/libdeno_test.cc @@ -152,18 +152,6 @@ TEST(LibDenoTest, JSSendArrayBufferViewTypes) { deno_delete(d); } -TEST(LibDenoTest, JSSendNeutersBuffer) { - static int count = 0; - Deno* d = deno_new(nullptr, [](auto _, auto buf) { - count++; - EXPECT_EQ(buf.data_len, 1u); - EXPECT_EQ(buf.data_ptr[0], 42); - }); - EXPECT_TRUE(deno_execute(d, "a.js", "JSSendNeutersBuffer()")); - EXPECT_EQ(count, 1); - deno_delete(d); -} - TEST(LibDenoTest, TypedArraySnapshots) { Deno* d = deno_new(nullptr, nullptr); EXPECT_TRUE(deno_execute(d, "a.js", "TypedArraySnapshots()")); diff --git a/libdeno/libdeno_test.js b/libdeno/libdeno_test.js index d51973ef00..ec8e4c7520 100644 --- a/libdeno/libdeno_test.js +++ b/libdeno/libdeno_test.js @@ -103,18 +103,6 @@ global.JSSendArrayBufferViewTypes = () => { libdeno.send(dv); }; -global.JSSendNeutersBuffer = () => { - // Buffer should be neutered after transferring it to the native side. - const u8 = new Uint8Array([42]); - assert(u8.byteLength === 1); - assert(u8.buffer.byteLength === 1); - assert(u8[0] === 42); - const r = libdeno.send(u8); - assert(u8.byteLength === 0); - assert(u8.buffer.byteLength === 0); - assert(u8[0] === undefined); -}; - // The following join has caused SnapshotBug to segfault when using kKeep. [].join("");