Commit 0ceee9ad authored by Joshua Litt's avatar Joshua Litt Committed by Commit Bot

[top-level-await] Add support for parsing top level await

Adds support for parsing top level await to V8, as well as
many tests.

This is the final cl in the series to add support for top level
await to v8.

Spec is here:
https://tc39.es/proposal-top-level-await/#sec-execute-async-module

Bug: v8:9344
Change-Id: Ie8f17ad8c7c60d1f6996d134ae154416cc1f31e3
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1703878Reviewed-by: 's avatarGeorg Neis <neis@chromium.org>
Reviewed-by: 's avatarAdam Klein <adamk@chromium.org>
Commit-Queue: Joshua Litt <joshualitt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#63946}
parent 256a8167
...@@ -865,6 +865,11 @@ class V8_EXPORT_PRIVATE DeclarationScope : public Scope { ...@@ -865,6 +865,11 @@ class V8_EXPORT_PRIVATE DeclarationScope : public Scope {
return IsClassMembersInitializerFunction(function_kind()); return IsClassMembersInitializerFunction(function_kind());
} }
void set_is_async_module() {
DCHECK(IsModule(function_kind_));
function_kind_ = kAsyncModule;
}
void DeclareThis(AstValueFactory* ast_value_factory); void DeclareThis(AstValueFactory* ast_value_factory);
void DeclareArguments(AstValueFactory* ast_value_factory); void DeclareArguments(AstValueFactory* ast_value_factory);
void DeclareDefaultFunctionVariables(AstValueFactory* ast_value_factory); void DeclareDefaultFunctionVariables(AstValueFactory* ast_value_factory);
...@@ -1143,7 +1148,7 @@ class V8_EXPORT_PRIVATE DeclarationScope : public Scope { ...@@ -1143,7 +1148,7 @@ class V8_EXPORT_PRIVATE DeclarationScope : public Scope {
bool needs_private_name_context_chain_recalc_ : 1; bool needs_private_name_context_chain_recalc_ : 1;
// If the scope is a function scope, this is the function kind. // If the scope is a function scope, this is the function kind.
const FunctionKind function_kind_; FunctionKind function_kind_;
int num_parameters_ = 0; int num_parameters_ = 0;
......
...@@ -727,7 +727,7 @@ extern class JSBoundFunction extends JSObject { ...@@ -727,7 +727,7 @@ extern class JSBoundFunction extends JSObject {
bound_target_function: Callable; bound_target_function: Callable;
// The value that is always passed as the this value when calling the wrapped // The value that is always passed as the this value when calling the wrapped
// function. // function.
bound_this: JSAny; bound_this: JSAny | SourceTextModule;
// A list of values whose elements are used as the first arguments to any call // A list of values whose elements are used as the first arguments to any call
// to the wrapped function. // to the wrapped function.
bound_arguments: FixedArray; bound_arguments: FixedArray;
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "src/objects/field-type.h" #include "src/objects/field-type.h"
#include "src/objects/foreign-inl.h" #include "src/objects/foreign-inl.h"
#include "src/objects/free-space-inl.h" #include "src/objects/free-space-inl.h"
#include "src/objects/function-kind.h"
#include "src/objects/hash-table-inl.h" #include "src/objects/hash-table-inl.h"
#include "src/objects/js-array-inl.h" #include "src/objects/js-array-inl.h"
#include "src/objects/layout-descriptor.h" #include "src/objects/layout-descriptor.h"
...@@ -1050,7 +1051,7 @@ void SharedFunctionInfo::SharedFunctionInfoVerify(Isolate* isolate) { ...@@ -1050,7 +1051,7 @@ void SharedFunctionInfo::SharedFunctionInfoVerify(Isolate* isolate) {
if (scope_info().length() > 0) { if (scope_info().length() > 0) {
ScopeInfo info = scope_info(); ScopeInfo info = scope_info();
CHECK(kind() == info.function_kind()); CHECK(kind() == info.function_kind());
CHECK_EQ(kind() == kModule, info.scope_type() == MODULE_SCOPE); CHECK_EQ(internal::IsModule(kind()), info.scope_type() == MODULE_SCOPE);
} }
if (IsApiFunction()) { if (IsApiFunction()) {
......
...@@ -3058,7 +3058,7 @@ Handle<SourceTextModule> Factory::NewSourceTextModule( ...@@ -3058,7 +3058,7 @@ Handle<SourceTextModule> Factory::NewSourceTextModule(
module->set_dfs_ancestor_index(-1); module->set_dfs_ancestor_index(-1);
module->set_top_level_capability(roots.undefined_value()); module->set_top_level_capability(roots.undefined_value());
module->set_flags(0); module->set_flags(0);
module->set_async(false); module->set_async(IsAsyncModule(code->kind()));
module->set_async_evaluating(false); module->set_async_evaluating(false);
module->set_async_parent_modules(*async_parent_modules); module->set_async_parent_modules(*async_parent_modules);
module->set_pending_async_dependencies(0); module->set_pending_async_dependencies(0);
......
...@@ -3084,7 +3084,8 @@ void BytecodeGenerator::BuildAsyncReturn(int source_position) { ...@@ -3084,7 +3084,8 @@ void BytecodeGenerator::BuildAsyncReturn(int source_position) {
.StoreAccumulatorInRegister(args[2]) // done .StoreAccumulatorInRegister(args[2]) // done
.CallRuntime(Runtime::kInlineAsyncGeneratorResolve, args); .CallRuntime(Runtime::kInlineAsyncGeneratorResolve, args);
} else { } else {
DCHECK(IsAsyncFunction(info()->literal()->kind())); DCHECK(IsAsyncFunction(info()->literal()->kind()) ||
IsAsyncModule(info()->literal()->kind()));
RegisterList args = register_allocator()->NewRegisterList(3); RegisterList args = register_allocator()->NewRegisterList(3);
builder() builder()
->MoveRegister(generator_object(), args[0]) // generator ->MoveRegister(generator_object(), args[0]) // generator
...@@ -6094,8 +6095,9 @@ void BytecodeGenerator::BuildGeneratorObjectVariableInitialization() { ...@@ -6094,8 +6095,9 @@ void BytecodeGenerator::BuildGeneratorObjectVariableInitialization() {
RegisterAllocationScope register_scope(this); RegisterAllocationScope register_scope(this);
RegisterList args = register_allocator()->NewRegisterList(2); RegisterList args = register_allocator()->NewRegisterList(2);
Runtime::FunctionId function_id = Runtime::FunctionId function_id =
(IsAsyncFunction(info()->literal()->kind()) && ((IsAsyncFunction(info()->literal()->kind()) &&
!IsAsyncGeneratorFunction(info()->literal()->kind())) !IsAsyncGeneratorFunction(info()->literal()->kind())) ||
IsAsyncModule(info()->literal()->kind()))
? Runtime::kInlineAsyncFunctionEnter ? Runtime::kInlineAsyncFunctionEnter
: Runtime::kInlineCreateJSGeneratorObject; : Runtime::kInlineCreateJSGeneratorObject;
builder() builder()
......
...@@ -197,7 +197,7 @@ int Context::FunctionMapIndex(LanguageMode language_mode, FunctionKind kind, ...@@ -197,7 +197,7 @@ int Context::FunctionMapIndex(LanguageMode language_mode, FunctionKind kind,
base = IsAsyncFunction(kind) ? ASYNC_GENERATOR_FUNCTION_MAP_INDEX base = IsAsyncFunction(kind) ? ASYNC_GENERATOR_FUNCTION_MAP_INDEX
: GENERATOR_FUNCTION_MAP_INDEX; : GENERATOR_FUNCTION_MAP_INDEX;
} else if (IsAsyncFunction(kind)) { } else if (IsAsyncFunction(kind) || IsAsyncModule(kind)) {
CHECK_FOLLOWS4(ASYNC_FUNCTION_MAP_INDEX, ASYNC_FUNCTION_WITH_NAME_MAP_INDEX, CHECK_FOLLOWS4(ASYNC_FUNCTION_MAP_INDEX, ASYNC_FUNCTION_WITH_NAME_MAP_INDEX,
ASYNC_FUNCTION_WITH_HOME_OBJECT_MAP_INDEX, ASYNC_FUNCTION_WITH_HOME_OBJECT_MAP_INDEX,
ASYNC_FUNCTION_WITH_NAME_AND_HOME_OBJECT_MAP_INDEX); ASYNC_FUNCTION_WITH_NAME_AND_HOME_OBJECT_MAP_INDEX);
......
...@@ -14,6 +14,7 @@ enum FunctionKind : uint8_t { ...@@ -14,6 +14,7 @@ enum FunctionKind : uint8_t {
// BEGIN constructable functions // BEGIN constructable functions
kNormalFunction, kNormalFunction,
kModule, kModule,
kAsyncModule,
// BEGIN class constructors // BEGIN class constructors
// BEGIN base constructors // BEGIN base constructors
kBaseConstructor, kBaseConstructor,
...@@ -61,7 +62,11 @@ inline bool IsArrowFunction(FunctionKind kind) { ...@@ -61,7 +62,11 @@ inline bool IsArrowFunction(FunctionKind kind) {
} }
inline bool IsModule(FunctionKind kind) { inline bool IsModule(FunctionKind kind) {
return kind == FunctionKind::kModule; return IsInRange(kind, FunctionKind::kModule, FunctionKind::kAsyncModule);
}
inline bool IsAsyncModule(FunctionKind kind) {
return kind == FunctionKind::kAsyncModule;
} }
inline bool IsAsyncGeneratorFunction(FunctionKind kind) { inline bool IsAsyncGeneratorFunction(FunctionKind kind) {
...@@ -163,6 +168,8 @@ inline const char* FunctionKind2String(FunctionKind kind) { ...@@ -163,6 +168,8 @@ inline const char* FunctionKind2String(FunctionKind kind) {
return "AsyncFunction"; return "AsyncFunction";
case FunctionKind::kModule: case FunctionKind::kModule:
return "Module"; return "Module";
case FunctionKind::kAsyncModule:
return "AsyncModule";
case FunctionKind::kClassMembersInitializerFunction: case FunctionKind::kClassMembersInitializerFunction:
return "ClassMembersInitializerFunction"; return "ClassMembersInitializerFunction";
case FunctionKind::kDefaultBaseConstructor: case FunctionKind::kDefaultBaseConstructor:
......
...@@ -65,6 +65,7 @@ ParseInfo::ParseInfo(Isolate* isolate, AccountingAllocator* zone_allocator) ...@@ -65,6 +65,7 @@ ParseInfo::ParseInfo(Isolate* isolate, AccountingAllocator* zone_allocator)
set_allow_harmony_optional_chaining(FLAG_harmony_optional_chaining); set_allow_harmony_optional_chaining(FLAG_harmony_optional_chaining);
set_allow_harmony_nullish(FLAG_harmony_nullish); set_allow_harmony_nullish(FLAG_harmony_nullish);
set_allow_harmony_private_methods(FLAG_harmony_private_methods); set_allow_harmony_private_methods(FLAG_harmony_private_methods);
set_allow_harmony_top_level_await(FLAG_harmony_top_level_await);
} }
ParseInfo::ParseInfo(Isolate* isolate) ParseInfo::ParseInfo(Isolate* isolate)
......
...@@ -110,6 +110,8 @@ class V8_EXPORT_PRIVATE ParseInfo { ...@@ -110,6 +110,8 @@ class V8_EXPORT_PRIVATE ParseInfo {
set_collect_source_positions) set_collect_source_positions)
FLAG_ACCESSOR(kAllowHarmonyNullish, allow_harmony_nullish, FLAG_ACCESSOR(kAllowHarmonyNullish, allow_harmony_nullish,
set_allow_harmony_nullish) set_allow_harmony_nullish)
FLAG_ACCESSOR(kAllowHarmonyTopLevelAwait, allow_harmony_top_level_await,
set_allow_harmony_top_level_await)
#undef FLAG_ACCESSOR #undef FLAG_ACCESSOR
...@@ -319,6 +321,7 @@ class V8_EXPORT_PRIVATE ParseInfo { ...@@ -319,6 +321,7 @@ class V8_EXPORT_PRIVATE ParseInfo {
kIsOneshotIIFE = 1 << 27, kIsOneshotIIFE = 1 << 27,
kCollectSourcePositions = 1 << 28, kCollectSourcePositions = 1 << 28,
kAllowHarmonyNullish = 1 << 29, kAllowHarmonyNullish = 1 << 29,
kAllowHarmonyTopLevelAwait = 1 << 30,
}; };
//------------- Inputs to parsing and scope analysis ----------------------- //------------- Inputs to parsing and scope analysis -----------------------
......
...@@ -267,6 +267,7 @@ class ParserBase { ...@@ -267,6 +267,7 @@ class ParserBase {
allow_harmony_dynamic_import_(false), allow_harmony_dynamic_import_(false),
allow_harmony_import_meta_(false), allow_harmony_import_meta_(false),
allow_harmony_private_methods_(false), allow_harmony_private_methods_(false),
allow_harmony_top_level_await_(false),
allow_eval_cache_(true) { allow_eval_cache_(true) {
pointer_buffer_.reserve(32); pointer_buffer_.reserve(32);
variable_buffer_.reserve(32); variable_buffer_.reserve(32);
...@@ -280,6 +281,7 @@ class ParserBase { ...@@ -280,6 +281,7 @@ class ParserBase {
ALLOW_ACCESSORS(harmony_dynamic_import) ALLOW_ACCESSORS(harmony_dynamic_import)
ALLOW_ACCESSORS(harmony_import_meta) ALLOW_ACCESSORS(harmony_import_meta)
ALLOW_ACCESSORS(harmony_private_methods) ALLOW_ACCESSORS(harmony_private_methods)
ALLOW_ACCESSORS(harmony_top_level_await)
ALLOW_ACCESSORS(eval_cache) ALLOW_ACCESSORS(eval_cache)
#undef ALLOW_ACCESSORS #undef ALLOW_ACCESSORS
...@@ -1473,6 +1475,7 @@ class ParserBase { ...@@ -1473,6 +1475,7 @@ class ParserBase {
bool allow_harmony_dynamic_import_; bool allow_harmony_dynamic_import_;
bool allow_harmony_import_meta_; bool allow_harmony_import_meta_;
bool allow_harmony_private_methods_; bool allow_harmony_private_methods_;
bool allow_harmony_top_level_await_;
bool allow_eval_cache_; bool allow_eval_cache_;
}; };
...@@ -3080,7 +3083,9 @@ ParserBase<Impl>::ParseUnaryExpression() { ...@@ -3080,7 +3083,9 @@ ParserBase<Impl>::ParseUnaryExpression() {
Token::Value op = peek(); Token::Value op = peek();
if (Token::IsUnaryOrCountOp(op)) return ParseUnaryOrPrefixExpression(); if (Token::IsUnaryOrCountOp(op)) return ParseUnaryOrPrefixExpression();
if (is_async_function() && op == Token::AWAIT) { if ((is_async_function() || (allow_harmony_top_level_await() &&
IsModule(function_state_->kind()))) &&
op == Token::AWAIT) {
return ParseAwaitExpression(); return ParseAwaitExpression();
} }
return ParsePostfixExpression(); return ParsePostfixExpression();
......
...@@ -427,6 +427,7 @@ Parser::Parser(ParseInfo* info) ...@@ -427,6 +427,7 @@ Parser::Parser(ParseInfo* info)
set_allow_harmony_nullish(info->allow_harmony_nullish()); set_allow_harmony_nullish(info->allow_harmony_nullish());
set_allow_harmony_optional_chaining(info->allow_harmony_optional_chaining()); set_allow_harmony_optional_chaining(info->allow_harmony_optional_chaining());
set_allow_harmony_private_methods(info->allow_harmony_private_methods()); set_allow_harmony_private_methods(info->allow_harmony_private_methods());
set_allow_harmony_top_level_await(info->allow_harmony_top_level_await());
for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
++feature) { ++feature) {
use_counts_[feature] = 0; use_counts_[feature] = 0;
...@@ -576,8 +577,32 @@ FunctionLiteral* Parser::DoParseProgram(Isolate* isolate, ParseInfo* info) { ...@@ -576,8 +577,32 @@ FunctionLiteral* Parser::DoParseProgram(Isolate* isolate, ParseInfo* info) {
BuildInitialYield(kNoSourcePosition, kGeneratorFunction); BuildInitialYield(kNoSourcePosition, kGeneratorFunction);
body.Add( body.Add(
factory()->NewExpressionStatement(initial_yield, kNoSourcePosition)); factory()->NewExpressionStatement(initial_yield, kNoSourcePosition));
if (allow_harmony_top_level_await()) {
ParseModuleItemList(&body); // First parse statements into a buffer. Then, if there was a
// top level await, create an inner block and rewrite the body of the
// module as an async function. Otherwise merge the statements back
// into the main body.
BlockT block = impl()->NullBlock();
{
StatementListT statements(pointer_buffer());
ParseModuleItemList(&statements);
// Modules will always have an initial yield. If there are any
// additional suspends, i.e. awaits, then we treat the module as an
// AsyncModule.
if (function_state.suspend_count() > 1) {
scope->set_is_async_module();
block = factory()->NewBlock(true, statements);
} else {
statements.MergeInto(&body);
}
}
if (IsAsyncModule(scope->function_kind())) {
impl()->RewriteAsyncFunctionBody(
&body, block, factory()->NewUndefinedLiteral(kNoSourcePosition));
}
} else {
ParseModuleItemList(&body);
}
if (!has_error() && if (!has_error() &&
!module()->Validate(this->scope()->AsModuleScope(), !module()->Validate(this->scope()->AsModuleScope(),
pending_error_handler(), zone())) { pending_error_handler(), zone())) {
......
#
# Autogenerated by generate-bytecode-expectations.
#
---
wrap: no
module: yes
top level: yes
top level await: yes
---
snippet: "
await 42;
"
frame size: 8
parameter count: 2
bytecode array length: 142
bytecodes: [
B(SwitchOnGeneratorState), R(0), U8(0), U8(2),
B(LdaConstant), U8(2),
B(Star), R(3),
B(Mov), R(arg0), R(2),
B(CallRuntime), U16(Runtime::kPushModuleContext), R(2), U8(2),
B(PushContext), R(2),
B(Mov), R(closure), R(3),
B(Mov), R(this), R(4),
B(InvokeIntrinsic), U8(Runtime::k_AsyncFunctionEnter), R(3), U8(2),
B(Star), R(0),
/* 0 E> */ B(StackCheck),
/* 0 E> */ B(SuspendGenerator), R(0), R(0), U8(3), U8(0),
B(ResumeGenerator), R(0), R(0), U8(3),
B(Star), R(3),
B(InvokeIntrinsic), U8(Runtime::k_GeneratorGetResumeMode), R(0), U8(1),
B(SwitchOnSmiNoFeedback), U8(3), U8(2), I8(0),
B(Ldar), R(3),
/* 0 E> */ B(Throw),
B(Ldar), R(3),
/* 10 S> */ B(Return),
B(Mov), R(3), R(1),
B(Ldar), R(1),
B(Mov), R(context), R(3),
/* 0 S> */ B(LdaSmi), I8(42),
B(Star), R(5),
B(Mov), R(0), R(4),
B(InvokeIntrinsic), U8(Runtime::k_AsyncFunctionAwaitUncaught), R(4), U8(2),
/* 0 E> */ B(SuspendGenerator), R(0), R(0), U8(4), U8(1),
B(ResumeGenerator), R(0), R(0), U8(4),
B(Star), R(4),
B(InvokeIntrinsic), U8(Runtime::k_GeneratorGetResumeMode), R(0), U8(1),
B(Star), R(5),
B(LdaZero),
B(TestReferenceEqual), R(5),
B(JumpIfTrue), U8(5),
B(Ldar), R(4),
B(ReThrow),
B(LdaUndefined),
B(Star), R(5),
B(LdaTrue),
B(Star), R(6),
B(Mov), R(0), R(4),
B(InvokeIntrinsic), U8(Runtime::k_AsyncFunctionResolve), R(4), U8(3),
/* 10 S> */ B(Return),
B(Star), R(4),
B(CreateCatchContext), R(4), U8(5),
B(Star), R(3),
B(LdaTheHole),
B(SetPendingMessage),
B(Ldar), R(3),
B(PushContext), R(4),
B(LdaImmutableCurrentContextSlot), U8(4),
B(Star), R(6),
B(LdaTrue),
B(Star), R(7),
B(Mov), R(0), R(5),
B(InvokeIntrinsic), U8(Runtime::k_AsyncFunctionReject), R(5), U8(3),
/* 10 S> */ B(Return),
]
constant pool: [
Smi [36],
Smi [80],
SCOPE_INFO_TYPE,
Smi [10],
Smi [7],
SCOPE_INFO_TYPE,
]
handlers: [
[64, 114, 114],
]
---
snippet: "
await import(\"foo\");
"
frame size: 8
parameter count: 2
bytecode array length: 152
bytecodes: [
B(SwitchOnGeneratorState), R(0), U8(0), U8(2),
B(LdaConstant), U8(2),
B(Star), R(3),
B(Mov), R(arg0), R(2),
B(CallRuntime), U16(Runtime::kPushModuleContext), R(2), U8(2),
B(PushContext), R(2),
B(Mov), R(closure), R(3),
B(Mov), R(this), R(4),
B(InvokeIntrinsic), U8(Runtime::k_AsyncFunctionEnter), R(3), U8(2),
B(Star), R(0),
/* 0 E> */ B(StackCheck),
/* 0 E> */ B(SuspendGenerator), R(0), R(0), U8(3), U8(0),
B(ResumeGenerator), R(0), R(0), U8(3),
B(Star), R(3),
B(InvokeIntrinsic), U8(Runtime::k_GeneratorGetResumeMode), R(0), U8(1),
B(SwitchOnSmiNoFeedback), U8(3), U8(2), I8(0),
B(Ldar), R(3),
/* 0 E> */ B(Throw),
B(Ldar), R(3),
/* 21 S> */ B(Return),
B(Mov), R(3), R(1),
B(Ldar), R(1),
B(Mov), R(context), R(3),
/* 0 S> */ B(LdaConstant), U8(5),
B(Star), R(5),
B(Mov), R(closure), R(4),
B(CallRuntime), U16(Runtime::kDynamicImportCall), R(4), U8(2),
B(Star), R(5),
B(Mov), R(0), R(4),
B(InvokeIntrinsic), U8(Runtime::k_AsyncFunctionAwaitUncaught), R(4), U8(2),
/* 0 E> */ B(SuspendGenerator), R(0), R(0), U8(4), U8(1),
B(ResumeGenerator), R(0), R(0), U8(4),
B(Star), R(4),
B(InvokeIntrinsic), U8(Runtime::k_GeneratorGetResumeMode), R(0), U8(1),
B(Star), R(5),
B(LdaZero),
B(TestReferenceEqual), R(5),
B(JumpIfTrue), U8(5),
B(Ldar), R(4),
B(ReThrow),
B(LdaUndefined),
B(Star), R(5),
B(LdaTrue),
B(Star), R(6),
B(Mov), R(0), R(4),
B(InvokeIntrinsic), U8(Runtime::k_AsyncFunctionResolve), R(4), U8(3),
/* 21 S> */ B(Return),
B(Star), R(4),
B(CreateCatchContext), R(4), U8(6),
B(Star), R(3),
B(LdaTheHole),
B(SetPendingMessage),
B(Ldar), R(3),
B(PushContext), R(4),
B(LdaImmutableCurrentContextSlot), U8(4),
B(Star), R(6),
B(LdaTrue),
B(Star), R(7),
B(Mov), R(0), R(5),
B(InvokeIntrinsic), U8(Runtime::k_AsyncFunctionReject), R(5), U8(3),
/* 21 S> */ B(Return),
]
constant pool: [
Smi [36],
Smi [90],
SCOPE_INFO_TYPE,
Smi [10],
Smi [7],
ONE_BYTE_INTERNALIZED_STRING_TYPE ["foo"],
SCOPE_INFO_TYPE,
]
handlers: [
[64, 124, 124],
]
---
snippet: "
await 42;
async function foo() {
await 42;
}
foo();
"
frame size: 9
parameter count: 2
bytecode array length: 153
bytecodes: [
B(SwitchOnGeneratorState), R(0), U8(0), U8(2),
B(LdaConstant), U8(2),
B(Star), R(4),
B(Mov), R(arg0), R(3),
B(CallRuntime), U16(Runtime::kPushModuleContext), R(3), U8(2),
B(PushContext), R(3),
B(Mov), R(closure), R(4),
B(Mov), R(this), R(5),
B(InvokeIntrinsic), U8(Runtime::k_AsyncFunctionEnter), R(4), U8(2),
B(Star), R(0),
B(CreateClosure), U8(3), U8(0), U8(0),
B(Star), R(1),
/* 0 E> */ B(StackCheck),
B(Ldar), R(0),
/* 0 E> */ B(SuspendGenerator), R(0), R(0), U8(4), U8(0),
B(ResumeGenerator), R(0), R(0), U8(4),
B(Star), R(4),
B(InvokeIntrinsic), U8(Runtime::k_GeneratorGetResumeMode), R(0), U8(1),
B(SwitchOnSmiNoFeedback), U8(4), U8(2), I8(0),
B(Ldar), R(4),
/* 0 E> */ B(Throw),
B(Ldar), R(4),
/* 54 S> */ B(Return),
B(Mov), R(4), R(2),
B(Ldar), R(2),
B(Mov), R(context), R(4),
/* 0 S> */ B(LdaSmi), I8(42),
B(Star), R(6),
B(Mov), R(0), R(5),
B(InvokeIntrinsic), U8(Runtime::k_AsyncFunctionAwaitUncaught), R(5), U8(2),
/* 0 E> */ B(SuspendGenerator), R(0), R(0), U8(5), U8(1),
B(ResumeGenerator), R(0), R(0), U8(5),
B(Star), R(5),
B(InvokeIntrinsic), U8(Runtime::k_GeneratorGetResumeMode), R(0), U8(1),
B(Star), R(6),
B(LdaZero),
B(TestReferenceEqual), R(6),
B(JumpIfTrue), U8(5),
B(Ldar), R(5),
B(ReThrow),
/* 47 S> */ B(CallUndefinedReceiver0), R(1), U8(0),
B(LdaUndefined),
B(Star), R(6),
B(LdaTrue),
B(Star), R(7),
B(Mov), R(0), R(5),
B(InvokeIntrinsic), U8(Runtime::k_AsyncFunctionResolve), R(5), U8(3),
/* 54 S> */ B(Return),
B(Star), R(5),
B(CreateCatchContext), R(5), U8(6),
B(Star), R(4),
B(LdaTheHole),
B(SetPendingMessage),
B(Ldar), R(4),
B(PushContext), R(5),
B(LdaImmutableCurrentContextSlot), U8(4),
B(Star), R(7),
B(LdaTrue),
B(Star), R(8),
B(Mov), R(0), R(6),
B(InvokeIntrinsic), U8(Runtime::k_AsyncFunctionReject), R(6), U8(3),
/* 54 S> */ B(Return),
]
constant pool: [
Smi [44],
Smi [88],
SCOPE_INFO_TYPE,
SHARED_FUNCTION_INFO_TYPE,
Smi [10],
Smi [7],
SCOPE_INFO_TYPE,
]
handlers: [
[72, 125, 125],
]
---
snippet: "
import * as foo from \"bar\";
await import(\"goo\");
"
frame size: 9
parameter count: 2
bytecode array length: 164
bytecodes: [
B(SwitchOnGeneratorState), R(0), U8(0), U8(2),
B(LdaConstant), U8(2),
B(Star), R(4),
B(Mov), R(arg0), R(3),
B(CallRuntime), U16(Runtime::kPushModuleContext), R(3), U8(2),
B(PushContext), R(3),
B(Mov), R(closure), R(4),
B(Mov), R(this), R(5),
B(InvokeIntrinsic), U8(Runtime::k_AsyncFunctionEnter), R(4), U8(2),
B(Star), R(0),
B(LdaZero),
B(Star), R(4),
B(CallRuntime), U16(Runtime::kGetModuleNamespace), R(4), U8(1),
B(Star), R(1),
/* 0 E> */ B(StackCheck),
B(Ldar), R(0),
/* 0 E> */ B(SuspendGenerator), R(0), R(0), U8(4), U8(0),
B(ResumeGenerator), R(0), R(0), U8(4),
B(Star), R(4),
B(InvokeIntrinsic), U8(Runtime::k_GeneratorGetResumeMode), R(0), U8(1),
B(SwitchOnSmiNoFeedback), U8(3), U8(2), I8(0),
B(Ldar), R(4),
/* 0 E> */ B(Throw),
B(Ldar), R(4),
/* 49 S> */ B(Return),
B(Mov), R(4), R(2),
B(Ldar), R(2),
B(Mov), R(context), R(4),
/* 28 S> */ B(LdaConstant), U8(5),
B(Star), R(6),
B(Mov), R(closure), R(5),
B(CallRuntime), U16(Runtime::kDynamicImportCall), R(5), U8(2),
B(Star), R(6),
B(Mov), R(0), R(5),
B(InvokeIntrinsic), U8(Runtime::k_AsyncFunctionAwaitUncaught), R(5), U8(2),
/* 28 E> */ B(SuspendGenerator), R(0), R(0), U8(5), U8(1),
B(ResumeGenerator), R(0), R(0), U8(5),
B(Star), R(5),
B(InvokeIntrinsic), U8(Runtime::k_GeneratorGetResumeMode), R(0), U8(1),
B(Star), R(6),
B(LdaZero),
B(TestReferenceEqual), R(6),
B(JumpIfTrue), U8(5),
B(Ldar), R(5),
B(ReThrow),
B(LdaUndefined),
B(Star), R(6),
B(LdaTrue),
B(Star), R(7),
B(Mov), R(0), R(5),
B(InvokeIntrinsic), U8(Runtime::k_AsyncFunctionResolve), R(5), U8(3),
/* 49 S> */ B(Return),
B(Star), R(5),
B(CreateCatchContext), R(5), U8(6),
B(Star), R(4),
B(LdaTheHole),
B(SetPendingMessage),
B(Ldar), R(4),
B(PushContext), R(5),
B(LdaImmutableCurrentContextSlot), U8(4),
B(Star), R(7),
B(LdaTrue),
B(Star), R(8),
B(Mov), R(0), R(6),
B(InvokeIntrinsic), U8(Runtime::k_AsyncFunctionReject), R(6), U8(3),
/* 49 S> */ B(Return),
]
constant pool: [
Smi [48],
Smi [102],
SCOPE_INFO_TYPE,
Smi [10],
Smi [7],
ONE_BYTE_INTERNALIZED_STRING_TYPE ["goo"],
SCOPE_INFO_TYPE,
]
handlers: [
[76, 136, 136],
]
...@@ -47,6 +47,7 @@ class ProgramOptions final { ...@@ -47,6 +47,7 @@ class ProgramOptions final {
oneshot_opt_(false), oneshot_opt_(false),
async_iteration_(false), async_iteration_(false),
private_methods_(false), private_methods_(false),
top_level_await_(false),
verbose_(false) {} verbose_(false) {}
bool Validate() const; bool Validate() const;
...@@ -70,6 +71,7 @@ class ProgramOptions final { ...@@ -70,6 +71,7 @@ class ProgramOptions final {
bool oneshot_opt() const { return oneshot_opt_; } bool oneshot_opt() const { return oneshot_opt_; }
bool async_iteration() const { return async_iteration_; } bool async_iteration() const { return async_iteration_; }
bool private_methods() const { return private_methods_; } bool private_methods() const { return private_methods_; }
bool top_level_await() const { return top_level_await_; }
bool verbose() const { return verbose_; } bool verbose() const { return verbose_; }
bool suppress_runtime_errors() const { return baseline() && !verbose_; } bool suppress_runtime_errors() const { return baseline() && !verbose_; }
std::vector<std::string> input_filenames() const { return input_filenames_; } std::vector<std::string> input_filenames() const { return input_filenames_; }
...@@ -90,6 +92,7 @@ class ProgramOptions final { ...@@ -90,6 +92,7 @@ class ProgramOptions final {
bool oneshot_opt_; bool oneshot_opt_;
bool async_iteration_; bool async_iteration_;
bool private_methods_; bool private_methods_;
bool top_level_await_;
bool verbose_; bool verbose_;
std::vector<std::string> input_filenames_; std::vector<std::string> input_filenames_;
std::string output_filename_; std::string output_filename_;
...@@ -196,6 +199,8 @@ ProgramOptions ProgramOptions::FromCommandLine(int argc, char** argv) { ...@@ -196,6 +199,8 @@ ProgramOptions ProgramOptions::FromCommandLine(int argc, char** argv) {
options.async_iteration_ = true; options.async_iteration_ = true;
} else if (strcmp(argv[i], "--private-methods") == 0) { } else if (strcmp(argv[i], "--private-methods") == 0) {
options.private_methods_ = true; options.private_methods_ = true;
} else if (strcmp(argv[i], "--harmony-top-level-await") == 0) {
options.top_level_await_ = true;
} else if (strcmp(argv[i], "--verbose") == 0) { } else if (strcmp(argv[i], "--verbose") == 0) {
options.verbose_ = true; options.verbose_ = true;
} else if (strncmp(argv[i], "--output=", 9) == 0) { } else if (strncmp(argv[i], "--output=", 9) == 0) {
...@@ -318,6 +323,8 @@ void ProgramOptions::UpdateFromHeader(std::istream* stream) { ...@@ -318,6 +323,8 @@ void ProgramOptions::UpdateFromHeader(std::istream* stream) {
async_iteration_ = ParseBoolean(line.c_str() + 17); async_iteration_ = ParseBoolean(line.c_str() + 17);
} else if (line.compare(0, 17, "private methods: ") == 0) { } else if (line.compare(0, 17, "private methods: ") == 0) {
private_methods_ = ParseBoolean(line.c_str() + 17); private_methods_ = ParseBoolean(line.c_str() + 17);
} else if (line.compare(0, 17, "top level await: ") == 0) {
top_level_await_ = ParseBoolean(line.c_str() + 17);
} else if (line == "---") { } else if (line == "---") {
break; break;
} else if (line.empty()) { } else if (line.empty()) {
...@@ -342,6 +349,7 @@ void ProgramOptions::PrintHeader(std::ostream* stream) const { ...@@ -342,6 +349,7 @@ void ProgramOptions::PrintHeader(std::ostream* stream) const {
if (oneshot_opt_) *stream << "\noneshot opt: yes"; if (oneshot_opt_) *stream << "\noneshot opt: yes";
if (async_iteration_) *stream << "\nasync iteration: yes"; if (async_iteration_) *stream << "\nasync iteration: yes";
if (private_methods_) *stream << "\nprivate methods: yes"; if (private_methods_) *stream << "\nprivate methods: yes";
if (top_level_await_) *stream << "\ntop level await: yes";
*stream << "\n\n"; *stream << "\n\n";
} }
...@@ -451,6 +459,7 @@ void GenerateExpectationsFile(std::ostream* stream, ...@@ -451,6 +459,7 @@ void GenerateExpectationsFile(std::ostream* stream,
} }
if (options.private_methods()) i::FLAG_harmony_private_methods = true; if (options.private_methods()) i::FLAG_harmony_private_methods = true;
if (options.top_level_await()) i::FLAG_harmony_top_level_await = true;
*stream << "#\n# Autogenerated by generate-bytecode-expectations.\n#\n\n"; *stream << "#\n# Autogenerated by generate-bytecode-expectations.\n#\n\n";
options.PrintHeader(stream); options.PrintHeader(stream);
...@@ -459,6 +468,7 @@ void GenerateExpectationsFile(std::ostream* stream, ...@@ -459,6 +468,7 @@ void GenerateExpectationsFile(std::ostream* stream,
} }
i::FLAG_harmony_private_methods = false; i::FLAG_harmony_private_methods = false;
i::FLAG_harmony_top_level_await = false;
} }
bool WriteExpectationsFile(const std::vector<std::string>& snippet_list, bool WriteExpectationsFile(const std::vector<std::string>& snippet_list,
...@@ -519,6 +529,7 @@ void PrintUsage(const char* exec_path) { ...@@ -519,6 +529,7 @@ void PrintUsage(const char* exec_path) {
"Specify the name of the test function.\n" "Specify the name of the test function.\n"
" --top-level Process top level code, not the top-level function.\n" " --top-level Process top level code, not the top-level function.\n"
" --private-methods Enable harmony_private_methods flag.\n" " --private-methods Enable harmony_private_methods flag.\n"
" --top-level-await Enable await at the module level.\n"
" --output=file.name\n" " --output=file.name\n"
" Specify the output file. If not specified, output goes to " " Specify the output file. If not specified, output goes to "
"stdout.\n" "stdout.\n"
......
...@@ -3142,6 +3142,35 @@ TEST(Modules) { ...@@ -3142,6 +3142,35 @@ TEST(Modules) {
LoadGolden("Modules.golden"))); LoadGolden("Modules.golden")));
} }
TEST(AsyncModules) {
bool previous_top_level_await_flag = i::FLAG_harmony_top_level_await;
i::FLAG_harmony_top_level_await = true;
InitializedIgnitionHandleScope scope;
BytecodeExpectationsPrinter printer(CcTest::isolate());
printer.set_wrap(false);
printer.set_module(true);
printer.set_top_level(true);
const char* snippets[] = {
"await 42;\n",
"await import(\"foo\");\n",
"await 42;\n"
"async function foo() {\n"
" await 42;\n"
"}\n"
"foo();\n",
"import * as foo from \"bar\";\n"
"await import(\"goo\");\n",
};
CHECK(CompareTexts(BuildActual(printer, snippets),
LoadGolden("AsyncModules.golden")));
i::FLAG_harmony_top_level_await = previous_top_level_await_flag;
}
TEST(SuperCallAndSpread) { TEST(SuperCallAndSpread) {
InitializedIgnitionHandleScope scope; InitializedIgnitionHandleScope scope;
BytecodeExpectationsPrinter printer(CcTest::isolate()); BytecodeExpectationsPrinter printer(CcTest::isolate());
......
...@@ -659,4 +659,231 @@ TEST(ModuleNamespace) { ...@@ -659,4 +659,231 @@ TEST(ModuleNamespace) {
} }
i::FLAG_harmony_top_level_await = prev_top_level_await; i::FLAG_harmony_top_level_await = prev_top_level_await;
} }
TEST(ModuleEvaluationTopLevelAwait) {
bool previous_top_level_await_flag_value = i::FLAG_harmony_top_level_await;
i::FLAG_harmony_top_level_await = true;
Isolate* isolate = CcTest::isolate();
HandleScope scope(isolate);
LocalContext env;
v8::TryCatch try_catch(isolate);
const char* sources[] = {
"await 42",
"import 'await 42';",
"import '42'; import 'await 42';",
};
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();
CHECK_EQ(Module::kUninstantiated, module->GetStatus());
CHECK(module
->InstantiateModule(env.local(),
CompileSpecifierAsModuleResolveCallback)
.FromJust());
CHECK_EQ(Module::kInstantiated, module->GetStatus());
Local<Promise> promise =
Local<Promise>::Cast(module->Evaluate(env.local()).ToLocalChecked());
CHECK_EQ(Module::kEvaluated, module->GetStatus());
CHECK_EQ(promise->State(), v8::Promise::kFulfilled);
CHECK(promise->Result()->IsUndefined());
CHECK(!try_catch.HasCaught());
}
i::FLAG_harmony_top_level_await = previous_top_level_await_flag_value;
}
TEST(ModuleEvaluationTopLevelAwaitError) {
bool previous_top_level_await_flag_value = i::FLAG_harmony_top_level_await;
i::FLAG_harmony_top_level_await = true;
Isolate* isolate = CcTest::isolate();
HandleScope scope(isolate);
LocalContext env;
const char* sources[] = {
"await 42; throw 'boom';",
"import 'await 42; throw \"boom\";';",
"import '42'; import 'await 42; throw \"boom\";';",
};
for (auto src : sources) {
v8::TryCatch try_catch(isolate);
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();
CHECK_EQ(Module::kUninstantiated, module->GetStatus());
CHECK(module
->InstantiateModule(env.local(),
CompileSpecifierAsModuleResolveCallback)
.FromJust());
CHECK_EQ(Module::kInstantiated, module->GetStatus());
Local<Promise> promise =
Local<Promise>::Cast(module->Evaluate(env.local()).ToLocalChecked());
CHECK_EQ(Module::kErrored, module->GetStatus());
CHECK_EQ(promise->State(), v8::Promise::kRejected);
CHECK(promise->Result()->StrictEquals(v8_str("boom")));
CHECK(module->GetException()->StrictEquals(v8_str("boom")));
// TODO(joshualitt) I am not sure, but this might not be supposed to throw
// because it is async.
CHECK(!try_catch.HasCaught());
}
i::FLAG_harmony_top_level_await = previous_top_level_await_flag_value;
}
namespace {
struct DynamicImportData {
DynamicImportData(Isolate* isolate_, Local<Promise::Resolver> resolver_,
Local<Context> context_, bool should_resolve_)
: isolate(isolate_), should_resolve(should_resolve_) {
resolver.Reset(isolate, resolver_);
context.Reset(isolate, context_);
}
Isolate* isolate;
v8::Global<Promise::Resolver> resolver;
v8::Global<Context> context;
bool should_resolve;
};
void DoHostImportModuleDynamically(void* import_data) {
std::unique_ptr<DynamicImportData> import_data_(
static_cast<DynamicImportData*>(import_data));
Isolate* isolate(import_data_->isolate);
HandleScope handle_scope(isolate);
Local<Promise::Resolver> resolver(import_data_->resolver.Get(isolate));
Local<Context> realm(import_data_->context.Get(isolate));
Context::Scope context_scope(realm);
if (import_data_->should_resolve) {
resolver->Resolve(realm, True(isolate)).ToChecked();
} else {
resolver->Reject(realm, v8_str("boom")).ToChecked();
}
}
v8::MaybeLocal<v8::Promise> HostImportModuleDynamicallyCallbackResolve(
Local<Context> context, Local<v8::ScriptOrModule> referrer,
Local<String> specifier) {
Isolate* isolate = context->GetIsolate();
Local<v8::Promise::Resolver> resolver =
v8::Promise::Resolver::New(context).ToLocalChecked();
DynamicImportData* data =
new DynamicImportData(isolate, resolver, context, true);
isolate->EnqueueMicrotask(DoHostImportModuleDynamically, data);
return resolver->GetPromise();
}
v8::MaybeLocal<v8::Promise> HostImportModuleDynamicallyCallbackReject(
Local<Context> context, Local<v8::ScriptOrModule> referrer,
Local<String> specifier) {
Isolate* isolate = context->GetIsolate();
Local<v8::Promise::Resolver> resolver =
v8::Promise::Resolver::New(context).ToLocalChecked();
DynamicImportData* data =
new DynamicImportData(isolate, resolver, context, false);
isolate->EnqueueMicrotask(DoHostImportModuleDynamically, data);
return resolver->GetPromise();
}
} // namespace
TEST(ModuleEvaluationTopLevelAwaitDynamicImport) {
bool previous_top_level_await_flag_value = i::FLAG_harmony_top_level_await;
bool previous_dynamic_import_flag_value = i::FLAG_harmony_dynamic_import;
i::FLAG_harmony_top_level_await = true;
i::FLAG_harmony_dynamic_import = true;
Isolate* isolate = CcTest::isolate();
HandleScope scope(isolate);
isolate->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit);
isolate->SetHostImportModuleDynamicallyCallback(
HostImportModuleDynamicallyCallbackResolve);
LocalContext env;
v8::TryCatch try_catch(isolate);
const char* sources[] = {
"await import('foo');",
"import 'await import(\"foo\");';",
"import '42'; import 'await import(\"foo\");';",
};
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();
CHECK_EQ(Module::kUninstantiated, module->GetStatus());
CHECK(module
->InstantiateModule(env.local(),
CompileSpecifierAsModuleResolveCallback)
.FromJust());
CHECK_EQ(Module::kInstantiated, module->GetStatus());
Local<Promise> promise =
Local<Promise>::Cast(module->Evaluate(env.local()).ToLocalChecked());
CHECK_EQ(Module::kEvaluated, module->GetStatus());
CHECK_EQ(promise->State(), v8::Promise::kPending);
CHECK(!try_catch.HasCaught());
isolate->RunMicrotasks();
CHECK_EQ(promise->State(), v8::Promise::kFulfilled);
}
i::FLAG_harmony_top_level_await = previous_top_level_await_flag_value;
i::FLAG_harmony_dynamic_import = previous_dynamic_import_flag_value;
}
TEST(ModuleEvaluationTopLevelAwaitDynamicImportError) {
bool previous_top_level_await_flag_value = i::FLAG_harmony_top_level_await;
bool previous_dynamic_import_flag_value = i::FLAG_harmony_dynamic_import;
i::FLAG_harmony_top_level_await = true;
i::FLAG_harmony_dynamic_import = true;
Isolate* isolate = CcTest::isolate();
HandleScope scope(isolate);
isolate->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit);
isolate->SetHostImportModuleDynamicallyCallback(
HostImportModuleDynamicallyCallbackReject);
LocalContext env;
v8::TryCatch try_catch(isolate);
const char* sources[] = {
"await import('foo');",
"import 'await import(\"foo\");';",
"import '42'; import 'await import(\"foo\");';",
};
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();
CHECK_EQ(Module::kUninstantiated, module->GetStatus());
CHECK(module
->InstantiateModule(env.local(),
CompileSpecifierAsModuleResolveCallback)
.FromJust());
CHECK_EQ(Module::kInstantiated, module->GetStatus());
Local<Promise> promise =
Local<Promise>::Cast(module->Evaluate(env.local()).ToLocalChecked());
CHECK_EQ(Module::kEvaluated, module->GetStatus());
CHECK_EQ(promise->State(), v8::Promise::kPending);
CHECK(!try_catch.HasCaught());
isolate->RunMicrotasks();
CHECK_EQ(Module::kErrored, module->GetStatus());
CHECK_EQ(promise->State(), v8::Promise::kRejected);
CHECK(promise->Result()->StrictEquals(v8_str("boom")));
CHECK(module->GetException()->StrictEquals(v8_str("boom")));
CHECK(!try_catch.HasCaught());
}
i::FLAG_harmony_top_level_await = previous_top_level_await_flag_value;
i::FLAG_harmony_dynamic_import = previous_dynamic_import_flag_value;
}
} // anonymous namespace } // anonymous namespace
// Copyright 2019 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.
//
// MODULE
//
// Flags: --harmony-top-level-await
import "modules-skip-1-top-level-await-fail.mjs"
*modules-skip-1-top-level-await-fail.mjs:7: ReferenceError: x is not defined
await x;
^
// Copyright 2019 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.
//
// MODULE
//
// Flags: --harmony-top-level-await
import "modules-skip-2-top-level-await-fail.mjs"
*modules-skip-2-top-level-await-fail.mjs:7: ReferenceError: ththsths is not defined
ththsths
^
// Copyright 2019 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.
//
// MODULE
await x;
// Copyright 2019 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.
import "modules-skip-3-top-level-await-fail.mjs"
ththsths
// Copyright 2019 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.
await 42;
// Copyright 2019 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.
// Flags: --harmony-top-level-await --allow-natives-syntax
// Flags: --harmony-dynamic-import
var ran = false;
async function test1() {
try {
let x = await import('modules-skip-8.mjs');
%AbortJS('failure: should be unreachable');
} catch(e) {
assertEquals('x is not defined', e.message);
ran = true;
}
}
test1();
%PerformMicrotaskCheckpoint();
assertTrue(ran);
ran = false;
async function test2() {
try {
let x = await import('modules-skip-9.mjs');
%AbortJS('failure: should be unreachable');
} catch(e) {
assertInstanceof(e, SyntaxError);
assertEquals(
"The requested module 'modules-skip-empty.mjs' does not provide an " +
"export named 'default'",
e.message);
ran = true;
}
}
test2();
%PerformMicrotaskCheckpoint();
assertTrue(ran);
ran = false;
async function test3() {
try {
let x = await import('nonexistent-file.mjs');
%AbortJS('failure: should be unreachable');
} catch(e) {
assertTrue(e.startsWith('Error reading'));
ran = true;
}
}
test3();
%PerformMicrotaskCheckpoint();
assertTrue(ran);
// Copyright 2019 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.
// Flags: --harmony-top-level-await
assertEquals(globalThis.test262, ['1', '2', '3', '4']);
import 'modules-skip-1-rqstd-order.mjs';
import 'modules-skip-2-rqstd-order.mjs';
import 'modules-skip-3-rqstd-order.mjs';
import 'modules-skip-4-rqstd-order.mjs';
// Copyright 2019 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.
// Flags: --harmony-top-level-await
assertEquals(globalThis.test262, [
'1_dir_a', '2_dir_a', '3_dir_a', '4_dir_a',
'1', '2', '3', '4',
'1_dir_b', '2_dir_b', '3_dir_b', '4_dir_b']);
import 'modules-skip-1-rqstd-order-top-level-await.mjs';
import 'modules-skip-2-rqstd-order-top-level-await.mjs';
import 'modules-skip-3-rqstd-order-top-level-await.mjs';
import 'modules-skip-4-rqstd-order-top-level-await.mjs';
// Copyright 2019 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.
// Flags: --harmony-top-level-await
assertEquals(globalThis.test262, [
'1', '2_dir_a', '3', '4_dir_a', '2', '4', '2_dir_b', '4_dir_b']);
import 'modules-skip-1-rqstd-order.mjs';
import 'modules-skip-2-rqstd-order-top-level-await.mjs';
import 'modules-skip-3-rqstd-order.mjs';
import 'modules-skip-4-rqstd-order-top-level-await.mjs';
// Copyright 2019 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.
// Flags: --harmony-top-level-await
assertEquals(globalThis.test262, [
'1_dir_a', '2_dir_a', '3_dir_a', '4_dir_a',
'1', '2', '3', '4',
'1_dir_b', '2_dir_b', '3_dir_b', '4_dir_b',
'1_ind', '2_ind', '3_ind', '4_ind',
]);
import 'modules-skip-1-rqstd-order-indirect-top-level-await.mjs';
import 'modules-skip-2-rqstd-order-indirect-top-level-await.mjs';
import 'modules-skip-3-rqstd-order-indirect-top-level-await.mjs';
import 'modules-skip-4-rqstd-order-indirect-top-level-await.mjs';
// Copyright 2019 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.
// Flags: --harmony-top-level-await
assertEquals(globalThis.test262, [
'1', '2_dir_a', '3_dir_a', '4',
'2', '3', '2_dir_b', '3_dir_b',
'2_ind',
]);
import 'modules-skip-1-rqstd-order.mjs';
import 'modules-skip-2-rqstd-order-indirect-top-level-await.mjs';
import 'modules-skip-3-rqstd-order-top-level-await.mjs';
import 'modules-skip-4-rqstd-order.mjs';
// Copyright 2019 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.
// Flags: --harmony-top-level-await
assertEquals(globalThis.test262, [
'1_dir_a', '2_dir_a', '3', '4_dir_a',
'1', '2', '4', '1_dir_b', '2_dir_b',
'4_dir_b', '2_ind',
]);
import 'modules-skip-1-rqstd-order-top-level-await.mjs';
import 'modules-skip-2-rqstd-order-indirect-top-level-await.mjs';
import 'modules-skip-3-rqstd-order.mjs';
import 'modules-skip-4-rqstd-order-top-level-await.mjs';
// Copyright 2019 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.
// Flags: --harmony-top-level-await
assertEquals(globalThis.test262, [
'1_udir_a', '1_udir_b', '2',
]);
import 'modules-skip-1-rqstd-order-unreached-top-level-await.mjs';
import 'modules-skip-2-rqstd-order.mjs';
// Copyright 2019 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.
// Flags: --harmony-top-level-await
assertEquals(globalThis.test262, [
'1_udir_a', '1_udir_b', '2', '1_uind'
]);
import 'modules-skip-1-rqstd-order-indirect-unreached-top-level-await.mjs';
import 'modules-skip-2-rqstd-order.mjs';
// Copyright 2019 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.
// Flags: --allow-natives-syntax --harmony-dynamic-import --harmony-top-level-await
let promise_resolved = false;
let m = import('modules-skip-1.mjs');
m.then(
() => { promise_resolved = true; },
() => { %AbortJS('Promise rejected'); });
await m;
assertEquals(promise_resolved, true);
// Copyright 2019 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.
// Flags: --harmony-dynamic-import --harmony-top-level-await
let m = import('modules-skip-1.mjs');
let m_namespace = await m;
assertEquals(42, m_namespace.life());
// Copyright 2019 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.
// Flags: --harmony-dynamic-import --harmony-top-level-await
let m1 = import('modules-skip-1.mjs');
let m1_namespace = await m1;
let m2 = import('modules-skip-3.mjs');
let m2_namespace = await m2;
assertEquals(42, m1_namespace.life());
assertEquals('42', m2_namespace.stringlife);
// Copyright 2019 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.
// Flags: --harmony-dynamic-import --harmony-top-level-await
import * as m from 'modules-skip-1-top-level-await.mjs'
assertEquals(42, m.life());
// Copyright 2019 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.
// Flags: --harmony-dynamic-import --harmony-top-level-await
import * as m from 'modules-skip-2-top-level-await.mjs'
assertEquals(42, m.life());
assertEquals('42', m.stringlife);
// Copyright 2019 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.
// Flags: --harmony-dynamic-import --harmony-top-level-await
import * as m from 'modules-skip-3-top-level-await.mjs'
assertEquals(42, m.life());
assertEquals('42', m.stringlife);
// Copyright 2019 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.
//
// Flags: --harmony-top-level-await --harmony-dynamic-import
import * as m from 'modules-skip-6-top-level-await.mjs';
assertEquals(m.m1.life(), m.m2.life());
// Copyright 2019 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.
// Flags: --harmony-dynamic-import --harmony-top-level-await
import * as m from 'modules-skip-7-top-level-await.mjs'
assertEquals(42, m.life);
// Copyright 2019 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.
//
// Flags: --harmony-top-level-await --harmony-dynamic-import
import * as m1 from 'modules-skip-1-top-level-await-cycle.mjs'
import * as m2 from 'modules-skip-2-top-level-await-cycle.mjs'
import * as m3 from 'modules-skip-3-top-level-await-cycle.mjs'
assertSame(m1.m1.m.m.life, m1.m2.m.m.life);
assertSame(m1.m1.m.m.life, m2.m.m.life);
assertSame(m1.m1.m.m.life, m3.m.m.life);
let m4 = await import('modules-skip-1.mjs');
assertSame(m1.m1.m.m.life, m4.life);
// Copyright 2019 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.
// Flags: --harmony-dynamic-import --harmony-top-level-await
let ran = false;
let m = import('modules-skip-2.mjs');
await m.then(
() => {
assertUnreachable();
},
(e) => {
assertEquals(e.message, '42 is not the answer');
ran = true;
});
assertEquals(ran, true);
// Copyright 2019 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.
// Flags: --harmony-dynamic-import --harmony-top-level-await
let ran = false;
try {
await import('modules-skip-2.mjs');
assertUnreachable();
} catch (e) {
assertEquals(e.message, '42 is not the answer');
ran = true;
}
assertEquals(ran, true);
// Copyright 2019 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.
// Flags: --harmony-dynamic-import --harmony-top-level-await
let ran = false;
try {
await import('modules-skip-4-top-level-await.mjs');
assertUnreachable();
} catch (e) {
assertEquals(e.message, '42 is not the answer');
ran = true;
}
assertEquals(ran, true);
// Copyright 2019 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.
import 'modules-skip-1-rqstd-order-top-level-await.mjs'
Function('return this;')().test262.push('1_ind');
// Copyright 2019 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.
import 'modules-skip-1-rqstd-order-unreached-top-level-await.mjs';
Function('return this;')().test262.push('1_uind');
// Copyright 2019 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.
if (typeof Function('return this;')().test262 === 'undefined') {
Function('return this;')().test262 = ['1_dir_a'];
} else {
Function('return this;')().test262.push('1_dir_a');
}
let m = import('modules-skip-1-rqstd-order.mjs');
await m;
Function('return this;')().test262.push('1_dir_b');
// Copyright 2019 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.
if (typeof Function('return this;')().test262 === 'undefined') {
Function('return this;')().test262 = ['1_udir_a'];
} else {
Function('return this;')().test262.push('1_udir_a');
}
if (false) {
assertUnreachable();
await 42;
}
Function('return this;')().test262.push('1_udir_b');
// Copyright 2019 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.
if (typeof Function('return this;')().test262 === 'undefined') {
Function('return this;')().test262 = ['1'];
} else {
Function('return this;')().test262.push('1');
}
// Copyright 2019 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.
import * as m1 from 'modules-skip-2-top-level-await-cycle.mjs';
import * as m2 from 'modules-skip-3-top-level-await-cycle.mjs';
export { m1, m2 };
// Copyright 2019 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.
let m = import('modules-skip-1.mjs');
let m_namespace = await m;
export function life() {
return m_namespace.life();
}
// Copyright 2019 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.
import 'modules-skip-2-rqstd-order-top-level-await.mjs'
Function('return this;')().test262.push('2_ind');
// Copyright 2019 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.
Function('return this;')().test262.push('2_dir_a');
let m = import('modules-skip-2-rqstd-order.mjs');
await m;
Function('return this;')().test262.push('2_dir_b');
// Copyright 2019 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.
Function('return this;')().test262.push('2');
// Copyright 2019 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.
import * as m from 'modules-skip-4-top-level-await-cycle.mjs';
export { m };
// Copyright 2019 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.
import * as m1 from 'modules-skip-3.mjs'
let m2 = import('modules-skip-1-top-level-await.mjs');
let m2_namespace = await m2;
export let stringlife = m1.stringlife;
export function life() {
return m2_namespace.life();
}
// Copyright 2019 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.
import 'modules-skip-3-rqstd-order-top-level-await.mjs'
Function('return this;')().test262.push('3_ind');
// Copyright 2019 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.
Function('return this;')().test262.push('3_dir_a');
let m = import('modules-skip-3-rqstd-order.mjs');
await m;
Function('return this;')().test262.push('3_dir_b');
// Copyright 2019 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.
Function('return this;')().test262.push('3');
// Copyright 2019 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.
import * as m from 'modules-skip-4-top-level-await-cycle.mjs';
export { m };
// Copyright 2019 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.
import * as m1 from 'modules-skip-1-top-level-await.mjs';
import * as m2 from 'modules-skip-3.mjs';
export function life() {
return m1.life();
}
export let stringlife = m2.stringlife;
// Copyright 2019 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.
import 'modules-skip-4-rqstd-order-top-level-await.mjs'
Function('return this;')().test262.push('4_ind');
// Copyright 2019 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.
Function('return this;')().test262.push('4_dir_a');
let m = import('modules-skip-4-rqstd-order.mjs');
await m;
Function('return this;')().test262.push('4_dir_b');
// Copyright 2019 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.
Function('return this;')().test262.push('4');
// Copyright 2019 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.
let m = await import('modules-skip-1.mjs');
export { m };
// Copyright 2019 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.
import 'modules-skip-5-top-level-await.mjs';
assertUnreachable();
// Copyright 2019 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.
await import('modules-skip-2.mjs')
// Copyright 2019 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.
import * as m1 from 'modules-skip-3-top-level-await.mjs';
let m2 = await import('modules-skip-1.mjs');
export { m1, m2 };
// Copyright 2019 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.
function sleeping_promise() {
return new Promise((resolve) => setTimeout(resolve));
}
export let life;
await sleeping_promise();
life = -1;
await sleeping_promise();
life = (await import('modules-skip-1.mjs')).life();
...@@ -55,11 +55,11 @@ FEATURE_FLAGS = { ...@@ -55,11 +55,11 @@ FEATURE_FLAGS = {
'WeakRef': '--harmony-weak-refs', 'WeakRef': '--harmony-weak-refs',
'host-gc-required': '--expose-gc-as=v8GC', 'host-gc-required': '--expose-gc-as=v8GC',
'optional-chaining': '--harmony-optional-chaining', 'optional-chaining': '--harmony-optional-chaining',
'top-level-await': '--harmony-top-level-await',
} }
SKIPPED_FEATURES = set(['class-methods-private', SKIPPED_FEATURES = set(['class-methods-private',
'class-static-methods-private', 'class-static-methods-private'])
'top-level-await'])
DATA = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data") DATA = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
......
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