Skip to content

Commit

Permalink
Add tests for deno_sub_cb.
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Jun 11, 2018
1 parent 2443f7e commit 482fc3a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 deletions.
16 changes: 6 additions & 10 deletions deno2/deno.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,12 @@ void Pub(const v8::FunctionCallbackInfo<v8::Value>& args) {

auto retbuf = d->cb(d, deno_buf{buf, buflen});
if (retbuf.data) {
auto ab = v8::ArrayBuffer::New(d->isolate, retbuf.data, retbuf.len,
v8::ArrayBufferCreationMode::kInternalized);
/*
// I'm slightly worried the above v8::ArrayBuffer construction leaks memory
// the following might be a safer way to do it.
// TODO(ry) Support zero-copy.
auto ab = v8::ArrayBuffer::New(d->isolate, retbuf.len);
auto contents = ab->GetContents();
memcpy(contents.Data(), retbuf.data, retbuf.len);
free(retbuf.data);
*/
memcpy(ab->GetContents().Data(), retbuf.data, retbuf.len);
args.GetReturnValue().Set(handle_scope.Escape(ab));
} else {
args.GetReturnValue().Set(v8::Null(d->isolate));
}
}

Expand Down Expand Up @@ -207,7 +202,8 @@ v8::StartupData MakeSnapshot(v8::StartupData* prev_natives_blob,
v8::Context::Scope context_scope(context);

auto global = context->Global();

// TODO(ry) Add a global namespace object "deno" and move print, sub, and
// pub inside that object.
auto print_tmpl = v8::FunctionTemplate::New(isolate, Print);
auto print_val = print_tmpl->GetFunction(context).ToLocalChecked();
CHECK(
Expand Down
30 changes: 27 additions & 3 deletions deno2/js/mock_runtime.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
// A simple runtime that doesn't involve typescript or protobufs to test
// libdeno.
const window = eval("this");
window['foo'] = () => {
window["foo"] = () => {
deno_print("Hello world from foo");
return "foo";
}
};

function assert(cond) {
if (!cond) throw Error("mock_runtime.js assert failed");
}

function subabc() {
deno_sub((msg) => {
deno_sub(msg => {
assert(msg instanceof ArrayBuffer);
assert(msg.byteLength === 3);
});
}

function typedArrayToArrayBuffer(ta) {
return ta.buffer.slice(ta.byteOffset, ta.byteOffset + ta.byteLength);
}

function pubReturnEmpty() {
const ui8 = new Uint8Array("abc".split("").map(c => c.charCodeAt(0)));
const ab = typedArrayToArrayBuffer(ui8);
let r = deno_pub(ab);
assert(r == null);
r = deno_pub(ab);
assert(r == null);
}

function pubReturnBar() {
const ui8 = new Uint8Array("abc".split("").map(c => c.charCodeAt(0)));
const ab = typedArrayToArrayBuffer(ui8);
const r = deno_pub(ab);
assert(r instanceof ArrayBuffer);
assert(r.byteLength === 3);
const rui8 = new Uint8Array(r);
const rstr = String.fromCharCode(...rui8);
assert(rstr === "bar");
}
34 changes: 34 additions & 0 deletions deno2/mock_runtime_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,40 @@ TEST(MockRuntimeTest, PubNoCallback) {
deno_dispose(d);
}

TEST(MockRuntimeTest, SubReturnEmpty) {
static int count = 0;
Deno* d = deno_new(NULL, [](Deno* _, deno_buf buf) {
count++;
EXPECT_EQ(static_cast<size_t>(3), buf.len);
// TODO(ry) buf.data should just be a char*.
char* data = reinterpret_cast<char*>(buf.data);
EXPECT_EQ(data[0], 'a');
EXPECT_EQ(data[1], 'b');
EXPECT_EQ(data[2], 'c');
return deno_buf{nullptr, 0};
});
EXPECT_TRUE(deno_load(d, "a.js", "pubReturnEmpty()"));
EXPECT_EQ(count, 2);
deno_dispose(d);
}

TEST(MockRuntimeTest, SubReturnBar) {
static int count = 0;
Deno* d = deno_new(NULL, [](Deno* _, deno_buf buf) {
count++;
EXPECT_EQ(static_cast<size_t>(3), buf.len);
// TODO(ry) buf.data should just be a char*.
char* data = reinterpret_cast<char*>(buf.data);
EXPECT_EQ(data[0], 'a');
EXPECT_EQ(data[1], 'b');
EXPECT_EQ(data[2], 'c');
return strbuf("bar");
});
EXPECT_TRUE(deno_load(d, "a.js", "pubReturnBar()"));
EXPECT_EQ(count, 1);
deno_dispose(d);
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
deno_init();
Expand Down

0 comments on commit 482fc3a

Please sign in to comment.