2016-09-20 23:38:58 +00:00
|
|
|
// 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"
|
|
|
|
|
2016-09-22 21:03:19 +00:00
|
|
|
namespace {
|
|
|
|
|
2016-09-20 23:38:58 +00:00
|
|
|
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;
|
|
|
|
|
2017-01-19 06:59:20 +00:00
|
|
|
ScriptOrigin ModuleOrigin(Local<v8::Value> resource_name, Isolate* isolate) {
|
|
|
|
ScriptOrigin origin(resource_name, Local<v8::Integer>(), Local<v8::Integer>(),
|
|
|
|
Local<v8::Boolean>(), Local<v8::Integer>(),
|
|
|
|
Local<v8::Value>(), Local<v8::Boolean>(),
|
|
|
|
Local<v8::Boolean>(), True(isolate));
|
|
|
|
return origin;
|
|
|
|
}
|
|
|
|
|
2017-04-19 18:28:09 +00:00
|
|
|
MaybeLocal<Module> FailAlwaysResolveCallback(Local<Context> context,
|
|
|
|
Local<String> specifier,
|
|
|
|
Local<Module> referrer) {
|
|
|
|
Isolate* isolate = context->GetIsolate();
|
|
|
|
isolate->ThrowException(v8_str("boom"));
|
2016-09-20 23:38:58 +00:00
|
|
|
return MaybeLocal<Module>();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int g_count = 0;
|
2016-09-22 21:03:19 +00:00
|
|
|
MaybeLocal<Module> FailOnSecondCallResolveCallback(Local<Context> context,
|
|
|
|
Local<String> specifier,
|
2016-10-11 19:22:03 +00:00
|
|
|
Local<Module> referrer) {
|
2017-04-19 18:28:09 +00:00
|
|
|
Isolate* isolate = CcTest::isolate();
|
|
|
|
if (g_count++ > 0) {
|
|
|
|
isolate->ThrowException(v8_str("booom"));
|
|
|
|
return MaybeLocal<Module>();
|
|
|
|
}
|
2016-09-20 23:38:58 +00:00
|
|
|
Local<String> source_text = v8_str("");
|
2017-04-19 18:28:09 +00:00
|
|
|
ScriptOrigin origin = ModuleOrigin(v8_str("module.js"), isolate);
|
2016-09-20 23:38:58 +00:00
|
|
|
ScriptCompiler::Source source(source_text, origin);
|
2017-04-19 18:28:09 +00:00
|
|
|
return ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked();
|
2016-09-20 23:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ModuleInstantiationFailures) {
|
|
|
|
Isolate* isolate = CcTest::isolate();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
LocalContext env;
|
2017-04-19 18:28:09 +00:00
|
|
|
v8::TryCatch try_catch(isolate);
|
2016-09-20 23:38:58 +00:00
|
|
|
|
|
|
|
Local<String> source_text = v8_str(
|
|
|
|
"import './foo.js';"
|
2017-06-21 12:30:56 +00:00
|
|
|
"\nexport {} from './bar.js';");
|
2017-01-19 06:59:20 +00:00
|
|
|
ScriptOrigin origin = ModuleOrigin(v8_str("file.js"), CcTest::isolate());
|
2016-09-20 23:38:58 +00:00
|
|
|
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)));
|
2017-06-21 12:30:56 +00:00
|
|
|
v8::Location loc = module->GetModuleRequestLocation(0);
|
|
|
|
CHECK_EQ(0, loc.GetLineNumber());
|
|
|
|
CHECK_EQ(7, loc.GetColumnNumber());
|
|
|
|
|
2016-09-20 23:38:58 +00:00
|
|
|
CHECK(v8_str("./bar.js")->StrictEquals(module->GetModuleRequest(1)));
|
2017-06-21 12:30:56 +00:00
|
|
|
loc = module->GetModuleRequestLocation(1);
|
|
|
|
CHECK_EQ(1, loc.GetLineNumber());
|
|
|
|
CHECK_EQ(15, loc.GetColumnNumber());
|
2016-09-20 23:38:58 +00:00
|
|
|
|
|
|
|
// Instantiation should fail.
|
2017-04-19 18:28:09 +00:00
|
|
|
{
|
|
|
|
v8::TryCatch inner_try_catch(isolate);
|
2017-05-29 11:57:50 +00:00
|
|
|
CHECK(module->InstantiateModule(env.local(), FailAlwaysResolveCallback)
|
|
|
|
.IsNothing());
|
2017-04-19 18:28:09 +00:00
|
|
|
CHECK(inner_try_catch.HasCaught());
|
|
|
|
CHECK(inner_try_catch.Exception()->StrictEquals(v8_str("boom")));
|
|
|
|
}
|
2016-09-20 23:38:58 +00:00
|
|
|
|
|
|
|
// Start over again...
|
|
|
|
module = ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked();
|
|
|
|
|
|
|
|
// Instantiation should fail if a sub-module fails to resolve.
|
|
|
|
g_count = 0;
|
2017-04-19 18:28:09 +00:00
|
|
|
{
|
|
|
|
v8::TryCatch inner_try_catch(isolate);
|
2017-05-29 11:57:50 +00:00
|
|
|
CHECK(
|
|
|
|
module->InstantiateModule(env.local(), FailOnSecondCallResolveCallback)
|
|
|
|
.IsNothing());
|
2017-04-19 18:28:09 +00:00
|
|
|
CHECK(inner_try_catch.HasCaught());
|
|
|
|
CHECK(inner_try_catch.Exception()->StrictEquals(v8_str("booom")));
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(!try_catch.HasCaught());
|
2016-09-20 23:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static MaybeLocal<Module> CompileSpecifierAsModuleResolveCallback(
|
2016-10-11 19:22:03 +00:00
|
|
|
Local<Context> context, Local<String> specifier, Local<Module> referrer) {
|
2017-01-19 06:59:20 +00:00
|
|
|
ScriptOrigin origin = ModuleOrigin(v8_str("module.js"), CcTest::isolate());
|
2016-09-20 23:38:58 +00:00
|
|
|
ScriptCompiler::Source source(specifier, origin);
|
|
|
|
return ScriptCompiler::CompileModule(CcTest::isolate(), &source)
|
|
|
|
.ToLocalChecked();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ModuleEvaluation) {
|
|
|
|
Isolate* isolate = CcTest::isolate();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
LocalContext env;
|
2017-04-19 18:28:09 +00:00
|
|
|
v8::TryCatch try_catch(isolate);
|
2016-09-20 23:38:58 +00:00
|
|
|
|
|
|
|
Local<String> source_text = v8_str(
|
|
|
|
"import 'Object.expando = 5';"
|
|
|
|
"import 'Object.expando *= 2';");
|
2017-01-19 06:59:20 +00:00
|
|
|
ScriptOrigin origin = ModuleOrigin(v8_str("file.js"), CcTest::isolate());
|
2016-09-20 23:38:58 +00:00
|
|
|
ScriptCompiler::Source source(source_text, origin);
|
|
|
|
Local<Module> module =
|
|
|
|
ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked();
|
2017-05-29 11:57:50 +00:00
|
|
|
CHECK(module
|
|
|
|
->InstantiateModule(env.local(),
|
|
|
|
CompileSpecifierAsModuleResolveCallback)
|
|
|
|
.FromJust());
|
2016-09-20 23:38:58 +00:00
|
|
|
CHECK(!module->Evaluate(env.local()).IsEmpty());
|
|
|
|
ExpectInt32("Object.expando", 10);
|
2017-04-19 18:28:09 +00:00
|
|
|
|
|
|
|
CHECK(!try_catch.HasCaught());
|
2016-09-20 23:38:58 +00:00
|
|
|
}
|
2016-09-22 21:03:19 +00:00
|
|
|
|
2017-02-28 10:11:54 +00:00
|
|
|
TEST(ModuleEvaluationCompletion1) {
|
|
|
|
Isolate* isolate = CcTest::isolate();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
LocalContext env;
|
2017-04-19 18:28:09 +00:00
|
|
|
v8::TryCatch try_catch(isolate);
|
2017-02-28 10:11:54 +00:00
|
|
|
|
|
|
|
const char* sources[] = {
|
2017-02-28 12:40:46 +00:00
|
|
|
"",
|
|
|
|
"var a = 1",
|
|
|
|
"import '42'",
|
|
|
|
"export * from '42'",
|
|
|
|
"export {} from '42'",
|
|
|
|
"export {}",
|
|
|
|
"var a = 1; export {a}",
|
2017-02-28 10:11:54 +00:00
|
|
|
"export function foo() {}",
|
2017-02-28 12:40:46 +00:00
|
|
|
"export class C extends null {}",
|
|
|
|
"export let a = 1",
|
|
|
|
"export default 1",
|
|
|
|
"export default function foo() {}",
|
|
|
|
"export default function () {}",
|
2017-02-28 10:11:54 +00:00
|
|
|
"export default (function () {})",
|
2017-02-28 12:40:46 +00:00
|
|
|
"export default class C extends null {}",
|
2017-02-28 10:11:54 +00:00
|
|
|
"export default (class C extends null {})",
|
|
|
|
"for (var i = 0; i < 5; ++i) {}",
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto src : sources) {
|
|
|
|
Local<String> source_text = v8_str(src);
|
|
|
|
ScriptOrigin origin = ModuleOrigin(v8_str("file.js"), CcTest::isolate());
|
|
|
|
ScriptCompiler::Source source(source_text, origin);
|
|
|
|
Local<Module> module =
|
|
|
|
ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked();
|
2017-05-29 11:57:50 +00:00
|
|
|
CHECK(module
|
|
|
|
->InstantiateModule(env.local(),
|
|
|
|
CompileSpecifierAsModuleResolveCallback)
|
|
|
|
.FromJust());
|
2017-02-28 10:11:54 +00:00
|
|
|
CHECK(module->Evaluate(env.local()).ToLocalChecked()->IsUndefined());
|
|
|
|
}
|
2017-04-19 18:28:09 +00:00
|
|
|
|
|
|
|
CHECK(!try_catch.HasCaught());
|
2017-02-28 10:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ModuleEvaluationCompletion2) {
|
|
|
|
Isolate* isolate = CcTest::isolate();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
LocalContext env;
|
2017-04-19 18:28:09 +00:00
|
|
|
v8::TryCatch try_catch(isolate);
|
2017-02-28 10:11:54 +00:00
|
|
|
|
|
|
|
const char* sources[] = {
|
2017-02-28 12:40:46 +00:00
|
|
|
"'gaga'; ",
|
|
|
|
"'gaga'; var a = 1",
|
|
|
|
"'gaga'; import '42'",
|
|
|
|
"'gaga'; export * from '42'",
|
|
|
|
"'gaga'; export {} from '42'",
|
|
|
|
"'gaga'; export {}",
|
|
|
|
"'gaga'; var a = 1; export {a}",
|
2017-02-28 10:11:54 +00:00
|
|
|
"'gaga'; export function foo() {}",
|
2017-02-28 12:40:46 +00:00
|
|
|
"'gaga'; export class C extends null {}",
|
|
|
|
"'gaga'; export let a = 1",
|
|
|
|
"'gaga'; export default 1",
|
2017-02-28 10:11:54 +00:00
|
|
|
"'gaga'; export default function foo() {}",
|
|
|
|
"'gaga'; export default function () {}",
|
|
|
|
"'gaga'; export default (function () {})",
|
2017-02-28 12:40:46 +00:00
|
|
|
"'gaga'; export default class C extends null {}",
|
2017-02-28 10:11:54 +00:00
|
|
|
"'gaga'; export default (class C extends null {})",
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto src : sources) {
|
|
|
|
Local<String> source_text = v8_str(src);
|
|
|
|
ScriptOrigin origin = ModuleOrigin(v8_str("file.js"), CcTest::isolate());
|
|
|
|
ScriptCompiler::Source source(source_text, origin);
|
|
|
|
Local<Module> module =
|
|
|
|
ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked();
|
2017-05-29 11:57:50 +00:00
|
|
|
CHECK(module
|
|
|
|
->InstantiateModule(env.local(),
|
|
|
|
CompileSpecifierAsModuleResolveCallback)
|
|
|
|
.FromJust());
|
2017-02-28 10:11:54 +00:00
|
|
|
CHECK(module->Evaluate(env.local())
|
|
|
|
.ToLocalChecked()
|
|
|
|
->StrictEquals(v8_str("gaga")));
|
|
|
|
}
|
2017-04-19 18:28:09 +00:00
|
|
|
|
|
|
|
CHECK(!try_catch.HasCaught());
|
2017-02-28 10:11:54 +00:00
|
|
|
}
|
|
|
|
|
2016-09-22 21:03:19 +00:00
|
|
|
} // anonymous namespace
|