From 4d4ef147f71c74cd0f6dbd1c1a95a3ee6f9ca93e Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Mon, 8 Jan 2018 09:03:33 -0600 Subject: [PATCH] deps: cherry-pick 0c35b72 from upstream V8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Original commit message: [api,modules] Allow GetModuleNamespace on unevaluated modules. Bug: v8:7217 Cq-Include-Trybots: master.tryserver.chromium.linux:linux_chromium_rel_ng Change-Id: I97b067254355eb91e12b92eba92631cbc3ce8000 Reviewed-on: https://chromium-review.googlesource.com/839280 Commit-Queue: Georg Neis Reviewed-by: Adam Klein Cr-Commit-Position: refs/heads/master@{#50395} PR-URL: https://github.com/nodejs/node/pull/18038 Refs: https://github.com/v8/v8/commit/0c35b7252aaeedf4871a78935075808439726853 Reviewed-By: Michaƫl Zasso Reviewed-By: Colin Ihrig Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Tiancheng "Timothy" Gu --- common.gypi | 2 +- deps/v8/src/api.cc | 5 +- deps/v8/test/cctest/test-modules.cc | 111 ++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 4 deletions(-) diff --git a/common.gypi b/common.gypi index 4459d4f9d923f3..84914640271fb1 100644 --- a/common.gypi +++ b/common.gypi @@ -27,7 +27,7 @@ # Reset this number to 0 on major V8 upgrades. # Increment by one for each non-official patch applied to deps/v8. - 'v8_embedder_string': '-node.5', + 'v8_embedder_string': '-node.6', # Enable disassembler for `--print-code` v8 options 'v8_enable_disassembler': 1, diff --git a/deps/v8/src/api.cc b/deps/v8/src/api.cc index f1bb82c72e7447..e0938a70bd7eee 100644 --- a/deps/v8/src/api.cc +++ b/deps/v8/src/api.cc @@ -2230,9 +2230,8 @@ Location Module::GetModuleRequestLocation(int i) const { Local Module::GetModuleNamespace() { Utils::ApiCheck( - GetStatus() == kEvaluated, "v8::Module::GetModuleNamespace", - "v8::Module::GetModuleNamespace can only be used on a module with " - "status kEvaluated"); + GetStatus() >= kInstantiated, "v8::Module::GetModuleNamespace", + "v8::Module::GetModuleNamespace must be used on an instantiated module"); i::Handle self = Utils::OpenHandle(this); i::Handle module_namespace = i::Module::GetModuleNamespace(self); diff --git a/deps/v8/test/cctest/test-modules.cc b/deps/v8/test/cctest/test-modules.cc index 6de2efc6cafee3..b61b10bcea99dd 100644 --- a/deps/v8/test/cctest/test-modules.cc +++ b/deps/v8/test/cctest/test-modules.cc @@ -281,4 +281,115 @@ TEST(ModuleEvaluationCompletion2) { CHECK(!try_catch.HasCaught()); } +TEST(ModuleNamespace) { + Isolate* isolate = CcTest::isolate(); + HandleScope scope(isolate); + LocalContext env; + v8::TryCatch try_catch(isolate); + + Local ReferenceError = + CompileRun("ReferenceError")->ToObject(env.local()).ToLocalChecked(); + + Local source_text = v8_str( + "import {a, b} from 'export var a = 1; export let b = 2';" + "export function geta() {return a};" + "export function getb() {return b};" + "export let radio = 3;" + "export var gaga = 4;"); + ScriptOrigin origin = ModuleOrigin(v8_str("file.js"), CcTest::isolate()); + ScriptCompiler::Source source(source_text, origin); + Local module = + ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked(); + CHECK_EQ(Module::kUninstantiated, module->GetStatus()); + CHECK(module + ->InstantiateModule(env.local(), + CompileSpecifierAsModuleResolveCallback) + .FromJust()); + CHECK_EQ(Module::kInstantiated, module->GetStatus()); + Local ns = module->GetModuleNamespace(); + CHECK_EQ(Module::kInstantiated, module->GetStatus()); + Local nsobj = ns->ToObject(env.local()).ToLocalChecked(); + + // a, b + CHECK(nsobj->Get(env.local(), v8_str("a")).ToLocalChecked()->IsUndefined()); + CHECK(nsobj->Get(env.local(), v8_str("b")).ToLocalChecked()->IsUndefined()); + + // geta + { + auto geta = nsobj->Get(env.local(), v8_str("geta")).ToLocalChecked(); + auto a = geta.As() + ->Call(env.local(), geta, 0, nullptr) + .ToLocalChecked(); + CHECK(a->IsUndefined()); + } + + // getb + { + v8::TryCatch inner_try_catch(isolate); + auto getb = nsobj->Get(env.local(), v8_str("getb")).ToLocalChecked(); + CHECK( + getb.As()->Call(env.local(), getb, 0, nullptr).IsEmpty()); + CHECK(inner_try_catch.HasCaught()); + CHECK(inner_try_catch.Exception() + ->InstanceOf(env.local(), ReferenceError) + .FromJust()); + } + + // radio + { + v8::TryCatch inner_try_catch(isolate); + // https://bugs.chromium.org/p/v8/issues/detail?id=7235 + // CHECK(nsobj->Get(env.local(), v8_str("radio")).IsEmpty()); + CHECK(nsobj->Get(env.local(), v8_str("radio")) + .ToLocalChecked() + ->IsUndefined()); + CHECK(inner_try_catch.HasCaught()); + CHECK(inner_try_catch.Exception() + ->InstanceOf(env.local(), ReferenceError) + .FromJust()); + } + + // gaga + { + auto gaga = nsobj->Get(env.local(), v8_str("gaga")).ToLocalChecked(); + CHECK(gaga->IsUndefined()); + } + + CHECK(!try_catch.HasCaught()); + CHECK_EQ(Module::kInstantiated, module->GetStatus()); + module->Evaluate(env.local()).ToLocalChecked(); + CHECK_EQ(Module::kEvaluated, module->GetStatus()); + + // geta + { + auto geta = nsobj->Get(env.local(), v8_str("geta")).ToLocalChecked(); + auto a = geta.As() + ->Call(env.local(), geta, 0, nullptr) + .ToLocalChecked(); + CHECK_EQ(1, a->Int32Value(env.local()).FromJust()); + } + + // getb + { + auto getb = nsobj->Get(env.local(), v8_str("getb")).ToLocalChecked(); + auto b = getb.As() + ->Call(env.local(), getb, 0, nullptr) + .ToLocalChecked(); + CHECK_EQ(2, b->Int32Value(env.local()).FromJust()); + } + + // radio + { + auto radio = nsobj->Get(env.local(), v8_str("radio")).ToLocalChecked(); + CHECK_EQ(3, radio->Int32Value(env.local()).FromJust()); + } + + // gaga + { + auto gaga = nsobj->Get(env.local(), v8_str("gaga")).ToLocalChecked(); + CHECK_EQ(4, gaga->Int32Value(env.local()).FromJust()); + } + + CHECK(!try_catch.HasCaught()); +} } // anonymous namespace