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

Side by Side Diff: test/cctest/test-modules.cc

Issue 2611643002: [modules] Add an IsModule flag to ScriptOriginOptions. (Closed)
Patch Set: Remove convenience function from API. Created 3 years, 11 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 unified diff | Download patch
« no previous file with comments | « test/cctest/test-compiler.cc ('k') | test/cctest/test-serialize.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 the V8 project authors. All rights reserved. 1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/flags.h" 5 #include "src/flags.h"
6 6
7 #include "test/cctest/cctest.h" 7 #include "test/cctest/cctest.h"
8 8
9 namespace { 9 namespace {
10 10
11 using v8::Context; 11 using v8::Context;
12 using v8::HandleScope; 12 using v8::HandleScope;
13 using v8::Isolate; 13 using v8::Isolate;
14 using v8::Local; 14 using v8::Local;
15 using v8::MaybeLocal; 15 using v8::MaybeLocal;
16 using v8::Module; 16 using v8::Module;
17 using v8::ScriptCompiler; 17 using v8::ScriptCompiler;
18 using v8::ScriptOrigin; 18 using v8::ScriptOrigin;
19 using v8::String; 19 using v8::String;
20 using v8::Value; 20 using v8::Value;
21 21
22 ScriptOrigin ModuleOrigin(Local<v8::Value> resource_name, Isolate* isolate) {
23 ScriptOrigin origin(resource_name, Local<v8::Integer>(), Local<v8::Integer>(),
24 Local<v8::Boolean>(), Local<v8::Integer>(),
25 Local<v8::Value>(), Local<v8::Boolean>(),
26 Local<v8::Boolean>(), True(isolate));
27 return origin;
28 }
29
22 MaybeLocal<Module> AlwaysEmptyResolveCallback(Local<Context> context, 30 MaybeLocal<Module> AlwaysEmptyResolveCallback(Local<Context> context,
23 Local<String> specifier, 31 Local<String> specifier,
24 Local<Module> referrer) { 32 Local<Module> referrer) {
25 return MaybeLocal<Module>(); 33 return MaybeLocal<Module>();
26 } 34 }
27 35
28 static int g_count = 0; 36 static int g_count = 0;
29 MaybeLocal<Module> FailOnSecondCallResolveCallback(Local<Context> context, 37 MaybeLocal<Module> FailOnSecondCallResolveCallback(Local<Context> context,
30 Local<String> specifier, 38 Local<String> specifier,
31 Local<Module> referrer) { 39 Local<Module> referrer) {
32 if (g_count++ > 0) return MaybeLocal<Module>(); 40 if (g_count++ > 0) return MaybeLocal<Module>();
33 Local<String> source_text = v8_str(""); 41 Local<String> source_text = v8_str("");
34 ScriptOrigin origin(v8_str("module.js")); 42 ScriptOrigin origin = ModuleOrigin(v8_str("module.js"), CcTest::isolate());
35 ScriptCompiler::Source source(source_text, origin); 43 ScriptCompiler::Source source(source_text, origin);
36 return ScriptCompiler::CompileModule(CcTest::isolate(), &source) 44 return ScriptCompiler::CompileModule(CcTest::isolate(), &source)
37 .ToLocalChecked(); 45 .ToLocalChecked();
38 } 46 }
39 47
40 TEST(ModuleInstantiationFailures) { 48 TEST(ModuleInstantiationFailures) {
41 Isolate* isolate = CcTest::isolate(); 49 Isolate* isolate = CcTest::isolate();
42 HandleScope scope(isolate); 50 HandleScope scope(isolate);
43 LocalContext env; 51 LocalContext env;
44 52
45 Local<String> source_text = v8_str( 53 Local<String> source_text = v8_str(
46 "import './foo.js';" 54 "import './foo.js';"
47 "export {} from './bar.js';"); 55 "export {} from './bar.js';");
48 ScriptOrigin origin(v8_str("file.js")); 56 ScriptOrigin origin = ModuleOrigin(v8_str("file.js"), CcTest::isolate());
49 ScriptCompiler::Source source(source_text, origin); 57 ScriptCompiler::Source source(source_text, origin);
50 Local<Module> module = 58 Local<Module> module =
51 ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked(); 59 ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked();
52 CHECK_EQ(2, module->GetModuleRequestsLength()); 60 CHECK_EQ(2, module->GetModuleRequestsLength());
53 CHECK(v8_str("./foo.js")->StrictEquals(module->GetModuleRequest(0))); 61 CHECK(v8_str("./foo.js")->StrictEquals(module->GetModuleRequest(0)));
54 CHECK(v8_str("./bar.js")->StrictEquals(module->GetModuleRequest(1))); 62 CHECK(v8_str("./bar.js")->StrictEquals(module->GetModuleRequest(1)));
55 63
56 // Instantiation should fail. 64 // Instantiation should fail.
57 CHECK(!module->Instantiate(env.local(), AlwaysEmptyResolveCallback)); 65 CHECK(!module->Instantiate(env.local(), AlwaysEmptyResolveCallback));
58 66
59 // Start over again... 67 // Start over again...
60 module = ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked(); 68 module = ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked();
61 69
62 // Instantiation should fail if a sub-module fails to resolve. 70 // Instantiation should fail if a sub-module fails to resolve.
63 g_count = 0; 71 g_count = 0;
64 CHECK(!module->Instantiate(env.local(), FailOnSecondCallResolveCallback)); 72 CHECK(!module->Instantiate(env.local(), FailOnSecondCallResolveCallback));
65 } 73 }
66 74
67 static MaybeLocal<Module> CompileSpecifierAsModuleResolveCallback( 75 static MaybeLocal<Module> CompileSpecifierAsModuleResolveCallback(
68 Local<Context> context, Local<String> specifier, Local<Module> referrer) { 76 Local<Context> context, Local<String> specifier, Local<Module> referrer) {
69 ScriptOrigin origin(v8_str("module.js")); 77 ScriptOrigin origin = ModuleOrigin(v8_str("module.js"), CcTest::isolate());
70 ScriptCompiler::Source source(specifier, origin); 78 ScriptCompiler::Source source(specifier, origin);
71 return ScriptCompiler::CompileModule(CcTest::isolate(), &source) 79 return ScriptCompiler::CompileModule(CcTest::isolate(), &source)
72 .ToLocalChecked(); 80 .ToLocalChecked();
73 } 81 }
74 82
75 TEST(ModuleEvaluation) { 83 TEST(ModuleEvaluation) {
76 Isolate* isolate = CcTest::isolate(); 84 Isolate* isolate = CcTest::isolate();
77 HandleScope scope(isolate); 85 HandleScope scope(isolate);
78 LocalContext env; 86 LocalContext env;
79 87
80 Local<String> source_text = v8_str( 88 Local<String> source_text = v8_str(
81 "import 'Object.expando = 5';" 89 "import 'Object.expando = 5';"
82 "import 'Object.expando *= 2';"); 90 "import 'Object.expando *= 2';");
83 ScriptOrigin origin(v8_str("file.js")); 91 ScriptOrigin origin = ModuleOrigin(v8_str("file.js"), CcTest::isolate());
84 ScriptCompiler::Source source(source_text, origin); 92 ScriptCompiler::Source source(source_text, origin);
85 Local<Module> module = 93 Local<Module> module =
86 ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked(); 94 ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked();
87 CHECK(module->Instantiate(env.local(), 95 CHECK(module->Instantiate(env.local(),
88 CompileSpecifierAsModuleResolveCallback)); 96 CompileSpecifierAsModuleResolveCallback));
89 CHECK(!module->Evaluate(env.local()).IsEmpty()); 97 CHECK(!module->Evaluate(env.local()).IsEmpty());
90 ExpectInt32("Object.expando", 10); 98 ExpectInt32("Object.expando", 10);
91 } 99 }
92 100
93 } // anonymous namespace 101 } // anonymous namespace
OLDNEW
« no previous file with comments | « test/cctest/test-compiler.cc ('k') | test/cctest/test-serialize.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698