diff options
Diffstat (limited to 'libdeno')
-rw-r--r-- | libdeno/api.cc | 5 | ||||
-rw-r--r-- | libdeno/binding.cc | 7 | ||||
-rw-r--r-- | libdeno/deno.h | 5 | ||||
-rw-r--r-- | libdeno/internal.h | 2 | ||||
-rw-r--r-- | libdeno/libdeno_test.cc | 32 |
5 files changed, 41 insertions, 10 deletions
diff --git a/libdeno/api.cc b/libdeno/api.cc index 2c779fb68..5b6155118 100644 --- a/libdeno/api.cc +++ b/libdeno/api.cc @@ -127,7 +127,7 @@ int deno_execute(Deno* d_, void* user_data, const char* js_filename, } int deno_execute_mod(Deno* d_, void* user_data, const char* js_filename, - const char* js_source) { + const char* js_source, int resolve_only) { auto* d = unwrap(d_); deno::UserDataScope user_data_scope(d, user_data); auto* isolate = d->isolate_; @@ -136,7 +136,8 @@ int deno_execute_mod(Deno* d_, void* user_data, const char* js_filename, v8::HandleScope handle_scope(isolate); auto context = d->context_.Get(d->isolate_); CHECK(!context.IsEmpty()); - return deno::ExecuteMod(context, js_filename, js_source) ? 1 : 0; + return deno::ExecuteMod(context, js_filename, js_source, resolve_only) ? 1 + : 0; } int deno_respond(Deno* d_, void* user_data, int32_t req_id, deno_buf buf) { diff --git a/libdeno/binding.cc b/libdeno/binding.cc index a1b3f20f1..d292f2638 100644 --- a/libdeno/binding.cc +++ b/libdeno/binding.cc @@ -590,7 +590,7 @@ void DenoIsolate::ResolveOk(const char* filename, const char* source) { } bool ExecuteMod(v8::Local<v8::Context> context, const char* js_filename, - const char* js_source) { + const char* js_source, bool resolve_only) { auto* isolate = context->GetIsolate(); v8::Isolate::Scope isolate_scope(isolate); v8::HandleScope handle_scope(isolate); @@ -616,6 +616,11 @@ bool ExecuteMod(v8::Local<v8::Context> context, const char* js_filename, } CHECK_EQ(v8::Module::kInstantiated, module->GetStatus()); + + if (resolve_only) { + return true; + } + auto result = module->Evaluate(context); if (result.IsEmpty()) { diff --git a/libdeno/deno.h b/libdeno/deno.h index a061f7452..d80043780 100644 --- a/libdeno/deno.h +++ b/libdeno/deno.h @@ -75,8 +75,11 @@ int deno_execute(Deno* d, void* user_data, const char* js_filename, // when instantiating the Deno object. // Return value: 0 = fail, 1 = success // Get error text with deno_last_exception(). +// If resolve_only is 0, compile and evaluate the module. +// If resolve_only is 1, compile and collect dependencies of the module +// without running the code. int deno_execute_mod(Deno* d, void* user_data, const char* js_filename, - const char* js_source); + const char* js_source, int resolve_only); // deno_respond sends up to one message back for every deno_recv_cb made. // diff --git a/libdeno/internal.h b/libdeno/internal.h index ee783d998..3019bc364 100644 --- a/libdeno/internal.h +++ b/libdeno/internal.h @@ -145,7 +145,7 @@ void DeleteDataRef(DenoIsolate* d, int32_t req_id); bool Execute(v8::Local<v8::Context> context, const char* js_filename, const char* js_source); bool ExecuteMod(v8::Local<v8::Context> context, const char* js_filename, - const char* js_source); + const char* js_source, bool resolve_only); } // namespace deno diff --git a/libdeno/libdeno_test.cc b/libdeno/libdeno_test.cc index 9873987ea..78091be59 100644 --- a/libdeno/libdeno_test.cc +++ b/libdeno/libdeno_test.cc @@ -284,7 +284,7 @@ TEST(LibDenoTest, ModuleResolution) { deno_resolve_ok(d, "b.js", mod_b); }; Deno* d = deno_new(deno_config{0, empty, empty, nullptr, resolve_cb}); - EXPECT_TRUE(deno_execute_mod(d, d, "a.js", mod_a)); + EXPECT_TRUE(deno_execute_mod(d, d, "a.js", mod_a, false)); EXPECT_EQ(count, 1); deno_delete(d); } @@ -299,7 +299,7 @@ TEST(LibDenoTest, ModuleResolutionFail) { // Do not call deno_resolve_ok(); }; Deno* d = deno_new(deno_config{0, empty, empty, nullptr, resolve_cb}); - EXPECT_FALSE(deno_execute_mod(d, d, "a.js", mod_a)); + EXPECT_FALSE(deno_execute_mod(d, d, "a.js", mod_a, false)); EXPECT_EQ(count, 1); deno_delete(d); } @@ -309,7 +309,8 @@ TEST(LibDenoTest, ModuleSnapshot) { EXPECT_TRUE(deno_execute_mod(d1, nullptr, "x.js", "const globalEval = eval\n" "const global = globalEval('this')\n" - "global.a = 1 + 2")); + "global.a = 1 + 2", + 0)); deno_buf test_snapshot = deno_get_snapshot(d1); deno_delete(d1); @@ -321,12 +322,32 @@ TEST(LibDenoTest, ModuleSnapshot) { deno_delete(d2); Deno* d3 = deno_new(config); - EXPECT_TRUE(deno_execute_mod(d3, nullptr, "y.js", y_src)); + EXPECT_TRUE(deno_execute_mod(d3, nullptr, "y.js", y_src, false)); deno_delete(d3); delete[] test_snapshot.data_ptr; } +TEST(LibDenoTest, ModuleResolveOnly) { + static int count = 0; + auto resolve_cb = [](void* user_data, const char* specifier, + const char* referrer) { + EXPECT_STREQ(specifier, "b.js"); + EXPECT_STREQ(referrer, "a.js"); + count++; + auto d = reinterpret_cast<Deno*>(user_data); + deno_resolve_ok(d, "b.js", mod_b); + }; + Deno* d = deno_new(deno_config{0, empty, empty, nullptr, resolve_cb}); + // Code should not execute. If executed, the error would be thrown + EXPECT_TRUE(deno_execute_mod(d, d, "a.js", + "import { retb } from 'b.js'\n" + "throw Error('unreachable');", + true)); + EXPECT_EQ(count, 1); + deno_delete(d); +} + TEST(LibDenoTest, BuiltinModules) { static int count = 0; auto resolve_cb = [](void* user_data, const char* specifier, @@ -347,7 +368,8 @@ TEST(LibDenoTest, BuiltinModules) { "import * as deno from 'deno'\n" "if (retb() != 'b') throw Error('retb');\n" // " libdeno.print('deno ' + JSON.stringify(deno));\n" - "if (deno.foo != 'bar') throw Error('foo');\n")); + "if (deno.foo != 'bar') throw Error('foo');\n", + false)); EXPECT_EQ(count, 1); deno_delete(d); } |