Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1430)

Unified Diff: test/cctest/test-modules.cc

Issue 2351113004: [modules] Expand API to allow linking and use it in d8 (Closed)
Patch Set: Use StrictEquals, remove check for bad instantiation behavior Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « test/cctest/cctest.status ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/cctest/test-modules.cc
diff --git a/test/cctest/test-modules.cc b/test/cctest/test-modules.cc
new file mode 100644
index 0000000000000000000000000000000000000000..c74446bf18c7dc7add364db54a2c7830bff3efa3
--- /dev/null
+++ b/test/cctest/test-modules.cc
@@ -0,0 +1,91 @@
+// Copyright 2016 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "src/flags.h"
+
+#include "test/cctest/cctest.h"
+
+using v8::Context;
+using v8::HandleScope;
+using v8::Isolate;
+using v8::Local;
+using v8::MaybeLocal;
+using v8::Module;
+using v8::ScriptCompiler;
+using v8::ScriptOrigin;
+using v8::String;
+using v8::Value;
+
+static MaybeLocal<Module> AlwaysEmptyResolveCallback(Local<Context> context,
+ Local<String> specifier,
+ Local<Module> referrer,
+ Local<Value> data) {
+ return MaybeLocal<Module>();
+}
+
+static int g_count = 0;
+static MaybeLocal<Module> FailOnSecondCallResolveCallback(
+ Local<Context> context, Local<String> specifier, Local<Module> referrer,
+ Local<Value> data) {
+ if (g_count++ > 0) return MaybeLocal<Module>();
+ Local<String> source_text = v8_str("");
+ ScriptOrigin origin(v8_str("module.js"));
+ ScriptCompiler::Source source(source_text, origin);
+ return ScriptCompiler::CompileModule(CcTest::isolate(), &source)
+ .ToLocalChecked();
+}
+
+TEST(ModuleInstantiationFailures) {
+ Isolate* isolate = CcTest::isolate();
+ HandleScope scope(isolate);
+ LocalContext env;
+
+ Local<String> source_text = v8_str(
+ "import './foo.js';"
+ "export {} from './bar.js';");
+ ScriptOrigin origin(v8_str("file.js"));
+ ScriptCompiler::Source source(source_text, origin);
+ Local<Module> module =
+ ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked();
+ CHECK_EQ(2, module->GetModuleRequestsLength());
+ CHECK(v8_str("./foo.js")->StrictEquals(module->GetModuleRequest(0)));
+ CHECK(v8_str("./bar.js")->StrictEquals(module->GetModuleRequest(1)));
+
+ // Instantiation should fail.
+ CHECK(!module->Instantiate(env.local(), AlwaysEmptyResolveCallback));
+
+ // Start over again...
+ module = ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked();
+
+ // Instantiation should fail if a sub-module fails to resolve.
+ g_count = 0;
+ CHECK(!module->Instantiate(env.local(), FailOnSecondCallResolveCallback));
+}
+
+static MaybeLocal<Module> CompileSpecifierAsModuleResolveCallback(
+ Local<Context> context, Local<String> specifier, Local<Module> referrer,
+ Local<Value> data) {
+ ScriptOrigin origin(v8_str("module.js"));
+ ScriptCompiler::Source source(specifier, origin);
+ return ScriptCompiler::CompileModule(CcTest::isolate(), &source)
+ .ToLocalChecked();
+}
+
+TEST(ModuleEvaluation) {
+ Isolate* isolate = CcTest::isolate();
+ HandleScope scope(isolate);
+ LocalContext env;
+
+ Local<String> source_text = v8_str(
+ "import 'Object.expando = 5';"
+ "import 'Object.expando *= 2';");
+ ScriptOrigin origin(v8_str("file.js"));
+ ScriptCompiler::Source source(source_text, origin);
+ Local<Module> module =
+ ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked();
+ CHECK(module->Instantiate(env.local(),
+ CompileSpecifierAsModuleResolveCallback));
+ CHECK(!module->Evaluate(env.local()).IsEmpty());
+ ExpectInt32("Object.expando", 10);
+}
« no previous file with comments | « test/cctest/cctest.status ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698