diff --git a/deno2/deno.cc b/deno2/deno.cc index 4fa5b21018..0c9bbd9185 100644 --- a/deno2/deno.cc +++ b/deno2/deno.cc @@ -276,7 +276,7 @@ bool deno_load(Deno* d, const char* name_s, const char* source_s) { // Routes message to the javascript callback set with deno_recv(). // False return value indicates error. Check deno_last_exception() for exception -// text. +// text. Caller owns buf. bool deno_send(Deno* d, deno_buf buf) { v8::Locker locker(d->isolate); v8::Isolate::Scope isolate_scope(d->isolate); @@ -287,16 +287,18 @@ bool deno_send(Deno* d, deno_buf buf) { v8::TryCatch try_catch(d->isolate); - v8::Local recv = - v8::Local::New(d->isolate, d->recv); + auto recv = d->recv.Get(d->isolate); if (recv.IsEmpty()) { d->last_exception = "deno_recv has not been called."; return false; } + // TODO(ry) support zero copy. + auto ab = v8::ArrayBuffer::New(d->isolate, buf.len); + memcpy(ab->GetContents().Data(), buf.data, buf.len); + v8::Local args[1]; - args[0] = v8::ArrayBuffer::New(d->isolate, buf.data, buf.len, - v8::ArrayBufferCreationMode::kInternalized); + args[0] = ab; assert(!args[0].IsEmpty()); assert(!try_catch.HasCaught()); diff --git a/deno2/js/mock_runtime.js b/deno2/js/mock_runtime.js index cd97666851..cdb6031e31 100644 --- a/deno2/js/mock_runtime.js +++ b/deno2/js/mock_runtime.js @@ -1,14 +1,13 @@ // A simple runtime that doesn't involve typescript or protobufs to test // libdeno. -const globalEval = eval; -const window = globalEval("this"); +const window = eval("this"); window['foo'] = () => { deno_print("Hello world from foo"); return "foo"; } function assert(cond) { - if (!cond) throw Error("assert failed"); + if (!cond) throw Error("mock_runtime.js assert failed"); } function recvabc() { diff --git a/deno2/mock_runtime_test.cc b/deno2/mock_runtime_test.cc index 00651f68ee..22b5962753 100644 --- a/deno2/mock_runtime_test.cc +++ b/deno2/mock_runtime_test.cc @@ -7,16 +7,19 @@ TEST(MockRuntimeTest, InitializesCorrectly) { Deno* d = deno_new(NULL, NULL); EXPECT_TRUE(deno_load(d, "a.js", "1 + 2")); + deno_dispose(d); } TEST(MockRuntimeTest, CanCallFoo) { Deno* d = deno_new(NULL, NULL); EXPECT_TRUE(deno_load(d, "a.js", "if (foo() != 'foo') throw Error();")); + deno_dispose(d); } TEST(MockRuntimeTest, ErrorsCorrectly) { Deno* d = deno_new(NULL, NULL); EXPECT_FALSE(deno_load(d, "a.js", "throw Error()")); + deno_dispose(d); } deno_buf strbuf(const char* str) { @@ -28,6 +31,7 @@ TEST(MockRuntimeTest, SendSuccess) { Deno* d = deno_new(NULL, NULL); EXPECT_TRUE(deno_load(d, "a.js", "recvabc();")); EXPECT_TRUE(deno_send(d, strbuf("abc"))); + deno_dispose(d); } TEST(MockRuntimeTest, SendByteLength) { @@ -35,12 +39,14 @@ TEST(MockRuntimeTest, SendByteLength) { EXPECT_TRUE(deno_load(d, "a.js", "recvabc();")); // We send the wrong sized message, it should throw. EXPECT_FALSE(deno_send(d, strbuf("abcd"))); + deno_dispose(d); } TEST(MockRuntimeTest, SendNoCallback) { Deno* d = deno_new(NULL, NULL); // We didn't call deno_recv(), sending should fail. EXPECT_FALSE(deno_send(d, strbuf("abc"))); + deno_dispose(d); } int main(int argc, char** argv) {