Commit 1744727d authored by neis's avatar neis Committed by Commit bot

[interpreter] Add some bytecode tests for modules.

R=rmcilroy@chromium.org
BUG=

Review-Url: https://codereview.chromium.org/2393453003
Cr-Commit-Position: refs/heads/master@{#39963}
parent 3c39bac4
......@@ -46,13 +46,19 @@ std::string BytecodeExpectationsPrinter::WrapCodeInFunction(
return program_stream.str();
}
v8::Local<v8::Script> BytecodeExpectationsPrinter::Compile(
v8::Local<v8::Script> BytecodeExpectationsPrinter::CompileScript(
const char* program) const {
v8::Local<v8::String> source = V8StringFromUTF8(program);
return v8::Script::Compile(isolate_->GetCurrentContext(), source)
.ToLocalChecked();
}
v8::Local<v8::Module> BytecodeExpectationsPrinter::CompileModule(
const char* program) const {
v8::ScriptCompiler::Source source(V8StringFromUTF8(program));
return v8::ScriptCompiler::CompileModule(isolate_, &source).ToLocalChecked();
}
void BytecodeExpectationsPrinter::Run(v8::Local<v8::Script> script) const {
(void)script->Run(isolate_->GetCurrentContext());
}
......@@ -73,6 +79,13 @@ BytecodeExpectationsPrinter::GetBytecodeArrayForGlobal(
return bytecodes;
}
i::Handle<i::BytecodeArray>
BytecodeExpectationsPrinter::GetBytecodeArrayForModule(
v8::Local<v8::Module> module) const {
i::Handle<i::Module> i_module = v8::Utils::OpenHandle(*module);
return i::handle(i_module->shared()->bytecode_array(), i_isolate());
}
i::Handle<i::BytecodeArray>
BytecodeExpectationsPrinter::GetBytecodeArrayForScript(
v8::Local<v8::Script> script) const {
......@@ -326,14 +339,19 @@ void BytecodeExpectationsPrinter::PrintExpectation(
wrap_ ? WrapCodeInFunction(test_function_name_.c_str(), snippet)
: snippet;
v8::Local<v8::Script> script = Compile(source_code.c_str());
i::Handle<i::BytecodeArray> bytecode_array;
if (top_level_) {
bytecode_array = GetBytecodeArrayForScript(script);
if (module_) {
CHECK(top_level_ && !wrap_);
v8::Local<v8::Module> module = CompileModule(source_code.c_str());
bytecode_array = GetBytecodeArrayForModule(module);
} else {
Run(script);
bytecode_array = GetBytecodeArrayForGlobal(test_function_name_.c_str());
v8::Local<v8::Script> script = CompileScript(source_code.c_str());
if (top_level_) {
bytecode_array = GetBytecodeArrayForScript(script);
} else {
Run(script);
bytecode_array = GetBytecodeArrayForGlobal(test_function_name_.c_str());
}
}
stream << "---\n";
......
......@@ -28,6 +28,7 @@ class BytecodeExpectationsPrinter final {
public:
explicit BytecodeExpectationsPrinter(v8::Isolate* i)
: isolate_(i),
module_(false),
wrap_(true),
top_level_(false),
test_function_name_(kDefaultTopFunctionName) {}
......@@ -35,6 +36,9 @@ class BytecodeExpectationsPrinter final {
void PrintExpectation(std::ostream& stream, // NOLINT
const std::string& snippet) const;
void set_module(bool module) { module_ = module; }
bool module() const { return module_; }
void set_wrap(bool wrap) { wrap_ = wrap; }
bool wrap() const { return wrap_; }
......@@ -80,10 +84,13 @@ class BytecodeExpectationsPrinter final {
std::string WrapCodeInFunction(const char* function_name,
const std::string& function_body) const;
v8::Local<v8::Script> Compile(const char* program) const;
v8::Local<v8::Script> CompileScript(const char* program) const;
v8::Local<v8::Module> CompileModule(const char* program) const;
void Run(v8::Local<v8::Script> script) const;
i::Handle<i::BytecodeArray> GetBytecodeArrayForGlobal(
const char* global_name) const;
i::Handle<v8::internal::BytecodeArray> GetBytecodeArrayForModule(
v8::Local<v8::Module> module) const;
i::Handle<v8::internal::BytecodeArray> GetBytecodeArrayForScript(
v8::Local<v8::Script> script) const;
......@@ -92,6 +99,7 @@ class BytecodeExpectationsPrinter final {
}
v8::Isolate* isolate_;
bool module_;
bool wrap_;
bool top_level_;
std::string test_function_name_;
......
This diff is collapsed.
......@@ -40,6 +40,7 @@ class ProgramOptions final {
read_from_stdin_(false),
rebaseline_(false),
wrap_(true),
module_(false),
top_level_(false),
do_expressions_(false),
verbose_(false) {}
......@@ -57,6 +58,7 @@ class ProgramOptions final {
}
bool rebaseline() const { return rebaseline_; }
bool wrap() const { return wrap_; }
bool module() const { return module_; }
bool top_level() const { return top_level_; }
bool do_expressions() const { return do_expressions_; }
bool verbose() const { return verbose_; }
......@@ -72,6 +74,7 @@ class ProgramOptions final {
bool read_from_stdin_;
bool rebaseline_;
bool wrap_;
bool module_;
bool top_level_;
bool do_expressions_;
bool verbose_;
......@@ -156,6 +159,8 @@ ProgramOptions ProgramOptions::FromCommandLine(int argc, char** argv) {
options.rebaseline_ = true;
} else if (strcmp(argv[i], "--no-wrap") == 0) {
options.wrap_ = false;
} else if (strcmp(argv[i], "--module") == 0) {
options.module_ = true;
} else if (strcmp(argv[i], "--top-level") == 0) {
options.top_level_ = true;
} else if (strcmp(argv[i], "--do-expressions") == 0) {
......@@ -234,6 +239,12 @@ bool ProgramOptions::Validate() const {
return false;
}
if (module_ && (!top_level_ || wrap_)) {
REPORT_ERROR(
"The flag --module currently requires --top-level and --no-wrap.");
return false;
}
return true;
}
......@@ -246,7 +257,9 @@ void ProgramOptions::UpdateFromHeader(std::istream& stream) {
}
while (std::getline(stream, line)) {
if (line.compare(0, 6, "wrap: ") == 0) {
if (line.compare(0, 8, "module: ") == 0) {
module_ = ParseBoolean(line.c_str() + 8);
} else if (line.compare(0, 6, "wrap: ") == 0) {
wrap_ = ParseBoolean(line.c_str() + 6);
} else if (line.compare(0, 20, "test function name: ") == 0) {
test_function_name_ = line.c_str() + 20;
......@@ -273,6 +286,7 @@ void ProgramOptions::PrintHeader(std::ostream& stream) const { // NOLINT
stream << "\ntest function name: " << test_function_name_;
}
if (module_) stream << "\nmodule: yes";
if (top_level_) stream << "\ntop level: yes";
if (do_expressions_) stream << "\ndo expressions: yes";
......@@ -372,6 +386,7 @@ void GenerateExpectationsFile(std::ostream& stream, // NOLINT
BytecodeExpectationsPrinter printer(platform.isolate());
printer.set_wrap(options.wrap());
printer.set_module(options.module());
printer.set_top_level(options.top_level());
if (!options.test_function_name().empty()) {
printer.set_test_function_name(options.test_function_name());
......@@ -425,6 +440,7 @@ void PrintUsage(const char* exec_path) {
" --stdin Read from standard input instead of file.\n"
" --rebaseline Rebaseline input snippet file.\n"
" --no-wrap Do not wrap the snippet in a function.\n"
" --module Compile as JavaScript module.\n"
" --test-function-name=foo "
"Specify the name of the test function.\n"
" --top-level Process top level code, not the top-level function.\n"
......
......@@ -2203,6 +2203,47 @@ TEST(Generators) {
LoadGolden("Generators.golden")));
}
TEST(Modules) {
InitializedIgnitionHandleScope scope;
BytecodeExpectationsPrinter printer(CcTest::isolate());
printer.set_wrap(false);
printer.set_module(true);
printer.set_top_level(true);
const char* snippets[] = {
"import \"bar\";\n",
"import {foo} from \"bar\";\n",
"import {foo as goo} from \"bar\";\n"
"goo(42);\n"
"{ let x; { goo(42) } };\n",
"export var foo = 42;\n"
"foo++;\n"
"{ let x; { foo++ } };\n",
"export let foo = 42;\n"
"foo++;\n"
"{ let x; { foo++ } };\n",
"export const foo = 42;\n"
"foo++;\n"
"{ let x; { foo++ } };\n",
"export default (function () {});\n",
"export default (class {});\n",
"export {foo as goo} from \"bar\"\n",
"export * from \"bar\"\n",
};
CHECK(CompareTexts(BuildActual(printer, snippets),
LoadGolden("Modules.golden")));
}
} // namespace interpreter
} // namespace internal
} // namespace v8
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment