From 0e07e16dd63992f5f989dc99c891d53d930a2d5b Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 11 Jun 2018 17:01:35 +0200 Subject: Add mock_runtime_test. --- deno2/js/mock_runtime.js | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 deno2/js/mock_runtime.js (limited to 'deno2/js/mock_runtime.js') diff --git a/deno2/js/mock_runtime.js b/deno2/js/mock_runtime.js new file mode 100644 index 000000000..a91546f85 --- /dev/null +++ b/deno2/js/mock_runtime.js @@ -0,0 +1,9 @@ +// A simple runtime that doesn't involve typescript or protobufs to test +// libdeno. +const globalEval = eval; +const window = globalEval("this"); +window['foo'] = () => { + deno_print("Hello world from foo"); + return "foo"; +} + -- cgit v1.2.3 From cbbe8ad9992765bc0883759e4075cf7a4a1918ff Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 11 Jun 2018 18:17:28 +0200 Subject: Add deno_send tests. --- deno2/js/mock_runtime.js | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'deno2/js/mock_runtime.js') diff --git a/deno2/js/mock_runtime.js b/deno2/js/mock_runtime.js index a91546f85..cd9766685 100644 --- a/deno2/js/mock_runtime.js +++ b/deno2/js/mock_runtime.js @@ -7,3 +7,13 @@ window['foo'] = () => { return "foo"; } +function assert(cond) { + if (!cond) throw Error("assert failed"); +} + +function recvabc() { + deno_recv((msg) => { + assert(msg instanceof ArrayBuffer); + assert(msg.byteLength === 3); + }); +} -- cgit v1.2.3 From 9590c87c623ba9654f332e26ba10370915e7ade9 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 11 Jun 2018 19:05:27 +0200 Subject: Add deno_dispose to tests. And fix ArrayBuffer memory problem. --- deno2/js/mock_runtime.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'deno2/js/mock_runtime.js') 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() { -- cgit v1.2.3 From 2443f7efee9c04f9331743a5e7ca5c71396f2004 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 11 Jun 2018 19:18:53 +0200 Subject: Use pub/sub instead of send/recv --- deno2/js/mock_runtime.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'deno2/js/mock_runtime.js') diff --git a/deno2/js/mock_runtime.js b/deno2/js/mock_runtime.js index cdb6031e3..b3ec7f034 100644 --- a/deno2/js/mock_runtime.js +++ b/deno2/js/mock_runtime.js @@ -10,8 +10,8 @@ function assert(cond) { if (!cond) throw Error("mock_runtime.js assert failed"); } -function recvabc() { - deno_recv((msg) => { +function subabc() { + deno_sub((msg) => { assert(msg instanceof ArrayBuffer); assert(msg.byteLength === 3); }); -- cgit v1.2.3 From 482fc3a2ce6ef6c3064486b2a8c3ddc51950a4c5 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 11 Jun 2018 20:18:56 +0200 Subject: Add tests for deno_sub_cb. --- deno2/js/mock_runtime.js | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'deno2/js/mock_runtime.js') diff --git a/deno2/js/mock_runtime.js b/deno2/js/mock_runtime.js index b3ec7f034..97688a08d 100644 --- a/deno2/js/mock_runtime.js +++ b/deno2/js/mock_runtime.js @@ -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"); +} -- cgit v1.2.3 From 314f08672157a0977d7c3eeb72010eb0f717d889 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 11 Jun 2018 21:30:58 +0200 Subject: Better function names in mock_runtime.js --- deno2/js/mock_runtime.js | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'deno2/js/mock_runtime.js') diff --git a/deno2/js/mock_runtime.js b/deno2/js/mock_runtime.js index 97688a08d..538174f79 100644 --- a/deno2/js/mock_runtime.js +++ b/deno2/js/mock_runtime.js @@ -1,27 +1,34 @@ // A simple runtime that doesn't involve typescript or protobufs to test -// libdeno. +// libdeno. Invoked by mock_runtime_test.cc const window = eval("this"); -window["foo"] = () => { - deno_print("Hello world from foo"); - return "foo"; -}; function assert(cond) { if (!cond) throw Error("mock_runtime.js assert failed"); } -function subabc() { +function typedArrayToArrayBuffer(ta) { + return ta.buffer.slice(ta.byteOffset, ta.byteOffset + ta.byteLength); +} + +function CanCallFunction() { + deno_print("Hello world from foo"); + return "foo"; +} + +function PubSuccess() { deno_sub(msg => { - assert(msg instanceof ArrayBuffer); - assert(msg.byteLength === 3); + deno_print("PubSuccess: ok"); }); } -function typedArrayToArrayBuffer(ta) { - return ta.buffer.slice(ta.byteOffset, ta.byteOffset + ta.byteLength); +function PubByteLength() { + deno_sub(msg => { + assert(msg instanceof ArrayBuffer); + assert(msg.byteLength === 3); + }); } -function pubReturnEmpty() { +function SubReturnEmpty() { const ui8 = new Uint8Array("abc".split("").map(c => c.charCodeAt(0))); const ab = typedArrayToArrayBuffer(ui8); let r = deno_pub(ab); @@ -30,7 +37,7 @@ function pubReturnEmpty() { assert(r == null); } -function pubReturnBar() { +function SubReturnBar() { const ui8 = new Uint8Array("abc".split("").map(c => c.charCodeAt(0))); const ab = typedArrayToArrayBuffer(ui8); const r = deno_pub(ab); -- cgit v1.2.3 From 7242f2b5a5118ee5b5cd2295761a703b54cee771 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 11 Jun 2018 21:57:25 +0200 Subject: Add channel to pub/sub --- deno2/js/mock_runtime.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'deno2/js/mock_runtime.js') diff --git a/deno2/js/mock_runtime.js b/deno2/js/mock_runtime.js index 538174f79..081b72c4b 100644 --- a/deno2/js/mock_runtime.js +++ b/deno2/js/mock_runtime.js @@ -16,13 +16,15 @@ function CanCallFunction() { } function PubSuccess() { - deno_sub(msg => { + deno_sub((channel, msg) => { + assert(channel === "PubSuccess"); deno_print("PubSuccess: ok"); }); } function PubByteLength() { - deno_sub(msg => { + deno_sub((channel, msg) => { + assert(channel === "PubByteLength"); assert(msg instanceof ArrayBuffer); assert(msg.byteLength === 3); }); @@ -31,16 +33,16 @@ function PubByteLength() { function SubReturnEmpty() { const ui8 = new Uint8Array("abc".split("").map(c => c.charCodeAt(0))); const ab = typedArrayToArrayBuffer(ui8); - let r = deno_pub(ab); + let r = deno_pub("SubReturnEmpty", ab); assert(r == null); - r = deno_pub(ab); + r = deno_pub("SubReturnEmpty", ab); assert(r == null); } function SubReturnBar() { const ui8 = new Uint8Array("abc".split("").map(c => c.charCodeAt(0))); const ab = typedArrayToArrayBuffer(ui8); - const r = deno_pub(ab); + const r = deno_pub("SubReturnBar", ab); assert(r instanceof ArrayBuffer); assert(r.byteLength === 3); const rui8 = new Uint8Array(r); -- cgit v1.2.3 From f89f576f6da6738617ac30f57b3c832a293434f5 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 11 Jun 2018 22:41:43 +0200 Subject: Use camel-case for deno js api. --- deno2/js/mock_runtime.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'deno2/js/mock_runtime.js') diff --git a/deno2/js/mock_runtime.js b/deno2/js/mock_runtime.js index 081b72c4b..5702ed592 100644 --- a/deno2/js/mock_runtime.js +++ b/deno2/js/mock_runtime.js @@ -11,19 +11,19 @@ function typedArrayToArrayBuffer(ta) { } function CanCallFunction() { - deno_print("Hello world from foo"); + denoPrint("Hello world from foo"); return "foo"; } function PubSuccess() { - deno_sub((channel, msg) => { + denoSub((channel, msg) => { assert(channel === "PubSuccess"); - deno_print("PubSuccess: ok"); + denoPrint("PubSuccess: ok"); }); } function PubByteLength() { - deno_sub((channel, msg) => { + denoSub((channel, msg) => { assert(channel === "PubByteLength"); assert(msg instanceof ArrayBuffer); assert(msg.byteLength === 3); @@ -33,16 +33,16 @@ function PubByteLength() { function SubReturnEmpty() { const ui8 = new Uint8Array("abc".split("").map(c => c.charCodeAt(0))); const ab = typedArrayToArrayBuffer(ui8); - let r = deno_pub("SubReturnEmpty", ab); + let r = denoPub("SubReturnEmpty", ab); assert(r == null); - r = deno_pub("SubReturnEmpty", ab); + r = denoPub("SubReturnEmpty", ab); assert(r == null); } function SubReturnBar() { const ui8 = new Uint8Array("abc".split("").map(c => c.charCodeAt(0))); const ab = typedArrayToArrayBuffer(ui8); - const r = deno_pub("SubReturnBar", ab); + const r = denoPub("SubReturnBar", ab); assert(r instanceof ArrayBuffer); assert(r.byteLength === 3); const rui8 = new Uint8Array(r); -- cgit v1.2.3 From 356fd18c7385f99766fe1e8d843ec85712bbaf76 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 11 Jun 2018 22:51:11 +0200 Subject: Calling denoSub twice should fail. --- deno2/js/mock_runtime.js | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'deno2/js/mock_runtime.js') diff --git a/deno2/js/mock_runtime.js b/deno2/js/mock_runtime.js index 5702ed592..570d6cf28 100644 --- a/deno2/js/mock_runtime.js +++ b/deno2/js/mock_runtime.js @@ -49,3 +49,10 @@ function SubReturnBar() { const rstr = String.fromCharCode(...rui8); assert(rstr === "bar"); } + +function DoubleSubFails() { + // denoSub is an internal function and should only be called once from the + // runtime. + denoSub((channel, msg) => assert(false)); + denoSub((channel, msg) => assert(false)); +} -- cgit v1.2.3 From 7784cc2c1537a23b8b8ffa634b1a9b1ddf88886a Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Tue, 12 Jun 2018 06:36:01 +0200 Subject: Fix protobufjs snapshotting. --- deno2/js/mock_runtime.js | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'deno2/js/mock_runtime.js') diff --git a/deno2/js/mock_runtime.js b/deno2/js/mock_runtime.js index 570d6cf28..07e5a2ca8 100644 --- a/deno2/js/mock_runtime.js +++ b/deno2/js/mock_runtime.js @@ -15,6 +15,17 @@ function CanCallFunction() { return "foo"; } +// This object is created to test snapshotting. +// See DeserializeInternalFieldsCallback and SerializeInternalFieldsCallback. +const snapshotted = new Uint8Array([1, 3, 3, 7]); + +function TypedArraySnapshots() { + assert(snapshotted[0] === 1); + assert(snapshotted[1] === 3); + assert(snapshotted[2] === 3); + assert(snapshotted[3] === 7); +} + function PubSuccess() { denoSub((channel, msg) => { assert(channel === "PubSuccess"); -- cgit v1.2.3