diff --git a/deno2/deno.cc b/deno2/deno.cc index 7bca590a70..4fa5b21018 100644 --- a/deno2/deno.cc +++ b/deno2/deno.cc @@ -274,9 +274,10 @@ bool deno_load(Deno* d, const char* name_s, const char* source_s) { return deno::Load(context, name_s, source_s); } -// Called from golang. Must route message to javascript lang. -// non-zero return value indicates error. check deno_last_exception(). -int deno_send(Deno* d, deno_buf buf) { +// Routes message to the javascript callback set with deno_recv(). +// False return value indicates error. Check deno_last_exception() for exception +// text. +bool deno_send(Deno* d, deno_buf buf) { v8::Locker locker(d->isolate); v8::Isolate::Scope isolate_scope(d->isolate); v8::HandleScope handle_scope(d->isolate); @@ -289,8 +290,8 @@ int deno_send(Deno* d, deno_buf buf) { v8::Local recv = v8::Local::New(d->isolate, d->recv); if (recv.IsEmpty()) { - d->last_exception = "V8Deno2.recv has not been called."; - return 1; + d->last_exception = "deno_recv has not been called."; + return false; } v8::Local args[1]; @@ -303,10 +304,10 @@ int deno_send(Deno* d, deno_buf buf) { if (try_catch.HasCaught()) { deno::HandleException(context, try_catch.Exception()); - return 2; + return false; } - return 0; + return true; } void deno_dispose(Deno* d) { diff --git a/deno2/include/deno.h b/deno2/include/deno.h index 1a42ec2b1e..f67a7cf51d 100644 --- a/deno2/include/deno.h +++ b/deno2/include/deno.h @@ -31,8 +31,9 @@ Deno* deno_new(void* data, deno_recv_cb cb); // Get error text with deno_last_exception(). bool deno_load(Deno* d, const char* name_s, const char* source_s); -// Returns nonzero on error. -int deno_send(Deno* d, deno_buf buf); +// Returns false on error. +// Get error text with deno_last_exception(). +bool deno_send(Deno* d, deno_buf buf); const char* deno_last_exception(Deno* d); diff --git a/deno2/js/mock_runtime.js b/deno2/js/mock_runtime.js index a91546f859..cd97666851 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); + }); +} diff --git a/deno2/mock_runtime_test.cc b/deno2/mock_runtime_test.cc index 951710fb92..00651f68ee 100644 --- a/deno2/mock_runtime_test.cc +++ b/deno2/mock_runtime_test.cc @@ -19,6 +19,30 @@ TEST(MockRuntimeTest, ErrorsCorrectly) { EXPECT_FALSE(deno_load(d, "a.js", "throw Error()")); } +deno_buf strbuf(const char* str) { + void* d = reinterpret_cast(const_cast(str)); + return deno_buf{d, strlen(str)}; +} + +TEST(MockRuntimeTest, SendSuccess) { + Deno* d = deno_new(NULL, NULL); + EXPECT_TRUE(deno_load(d, "a.js", "recvabc();")); + EXPECT_TRUE(deno_send(d, strbuf("abc"))); +} + +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"))); +} + +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"))); +} + int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); deno_init();