Skip to content

Commit

Permalink
Add deno_dispose to tests.
Browse files Browse the repository at this point in the history
And fix ArrayBuffer memory problem.
  • Loading branch information
ry committed Jun 11, 2018
1 parent cbbe8ad commit 9590c87
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
12 changes: 7 additions & 5 deletions deno2/deno.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());

Expand Down
5 changes: 2 additions & 3 deletions deno2/js/mock_runtime.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
6 changes: 6 additions & 0 deletions deno2/mock_runtime_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -28,19 +31,22 @@ 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) {
Deno* d = deno_new(NULL, NULL);
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) {
Expand Down

0 comments on commit 9590c87

Please sign in to comment.