summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--deno2/deno.cc12
-rw-r--r--deno2/js/mock_runtime.js5
-rw-r--r--deno2/mock_runtime_test.cc6
3 files changed, 15 insertions, 8 deletions
diff --git a/deno2/deno.cc b/deno2/deno.cc
index 4fa5b2101..0c9bbd918 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<v8::Function> recv =
- v8::Local<v8::Function>::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<v8::Value> 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 cd9766685..cdb6031e3 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 00651f68e..22b596275 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) {