From 356fd18c7385f99766fe1e8d843ec85712bbaf76 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 11 Jun 2018 22:51:11 +0200 Subject: [PATCH] Calling denoSub twice should fail. --- deno2/deno.cc | 5 +++++ deno2/js/mock_runtime.js | 7 +++++++ deno2/mock_runtime_test.cc | 6 ++++++ 3 files changed, 18 insertions(+) diff --git a/deno2/deno.cc b/deno2/deno.cc index 67b74bd636..26222d3b8f 100644 --- a/deno2/deno.cc +++ b/deno2/deno.cc @@ -117,6 +117,11 @@ void Sub(const v8::FunctionCallbackInfo& args) { v8::HandleScope handle_scope(isolate); + if (!d->sub.IsEmpty()) { + isolate->ThrowException(v8_str("denoSub already called.")); + return; + } + v8::Local v = args[0]; assert(v->IsFunction()); v8::Local func = v8::Local::Cast(v); diff --git a/deno2/js/mock_runtime.js b/deno2/js/mock_runtime.js index 5702ed592e..570d6cf28d 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)); +} diff --git a/deno2/mock_runtime_test.cc b/deno2/mock_runtime_test.cc index 44037044a6..ea5ef4bf3e 100644 --- a/deno2/mock_runtime_test.cc +++ b/deno2/mock_runtime_test.cc @@ -79,6 +79,12 @@ TEST(MockRuntimeTest, SubReturnBar) { deno_delete(d); } +TEST(MockRuntimeTest, DoubleSubFails) { + Deno* d = deno_new(NULL, NULL); + EXPECT_FALSE(deno_execute(d, "a.js", "DoubleSubFails()")); + deno_delete(d); +} + int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); deno_init();