Skip to content

Commit

Permalink
JS Parser: Support top-level await for modules
Browse files Browse the repository at this point in the history
Closes: #7408
  • Loading branch information
matthiasblaesing committed May 29, 2024
1 parent 136e384 commit 2163885
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 22 deletions.
2 changes: 1 addition & 1 deletion webcommon/javascript2.editor/licenseinfo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@
<file>test/unit/data/testfiles/ecmascript6/parser/ES6/identifier/estimated.js</file>
<file>test/unit/data/testfiles/ecmascript6/parser/ES6/identifier/ethiopic_digits.js</file>
<file>test/unit/data/testfiles/ecmascript6/parser/ES6/identifier/invalid_escaped_surrogate_pairs.js</file>
<file>test/unit/data/testfiles/ecmascript6/parser/ES6/identifier/invalid_expression_await.js</file>
<file>test/unit/data/testfiles/ecmascript6/parser/ES6/identifier/export_await.js</file>
<file>test/unit/data/testfiles/ecmascript6/parser/ES6/identifier/invalid_function_wait.js</file>
<file>test/unit/data/testfiles/ecmascript6/parser/ES6/identifier/invalid_id_smp.js</file>
<file>test/unit/data/testfiles/ecmascript6/parser/ES6/identifier/invalid_lone_surrogate.source.js</file>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
-->

<FunctionNode name='invalid_expression_await.js' kind='MODULE' start='0' end='31'>
<FunctionNode name='export_await.js' kind='MODULE' start='0' end='31'>
<isProgram/>
<ExportNode start='0' end='30'>
<VarNode name='answer' start='11' end='29'>
Expand All @@ -32,14 +32,13 @@
<isInitializedHere/>
</IdentNode>
<!-- VarNode Init -->
<BinaryNode type='ADD' start='20' end='29'>
<!-- BinaryNode lhs -->
<IdentNode start='20' end='25'>
<name>await</name>
</IdentNode>
<!-- BinaryNode rhs -->
<NumberLiteralNode value='1' start='28' end='29'/>
</BinaryNode>
<UnaryNode type='AWAIT' start='20' end='29'>
<!-- UnaryNode Expression -->
<UnaryNode type='ADD' start='26' end='29'>
<!-- UnaryNode Expression -->
<NumberLiteralNode value='1' start='28' end='29'/>
</UnaryNode>
</UnaryNode>
</VarNode>
</ExportNode>
<!-- FunctionNode Parameters -->
Expand All @@ -58,14 +57,13 @@
<isInitializedHere/>
</IdentNode>
<!-- VarNode Init -->
<BinaryNode type='ADD' start='20' end='29'>
<!-- BinaryNode lhs -->
<IdentNode start='20' end='25'>
<name>await</name>
</IdentNode>
<!-- BinaryNode rhs -->
<NumberLiteralNode value='1' start='28' end='29'/>
</BinaryNode>
<UnaryNode type='AWAIT' start='20' end='29'>
<!-- UnaryNode Expression -->
<UnaryNode type='ADD' start='26' end='29'>
<!-- UnaryNode Expression -->
<NumberLiteralNode value='1' start='28' end='29'/>
</UnaryNode>
</UnaryNode>
</VarNode>
</Block>
</FunctionNode>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import dummy from './dummy.mjs';

const demo = await dummy();
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

<FunctionNode name='topLevelAwait.js' kind='MODULE' start='0' end='888'>
<isProgram/>
<ImportNode start='826' end='856'>
<ImportClauseNode start='833' end='838'>
<IdentNode start='833' end='838'>
<name>dummy</name>
</IdentNode>
</ImportClauseNode>
<FromNode start='839' end='856'>
<StringLiteralNode value='./dummy.mjs' start='845' end='843'/>
</FromNode>
</ImportNode>
<!-- FunctionNode Parameters -->
<!-- FunctionNode Body -->
<Block start='0' end='887'>
<isFunctionBody/>
<isSynthetic/>
<!-- Block Statements -->
<VarNode name='demo' start='866' end='886'>
<hasInit/>
<isAssignment/>
<isBlockScoped/>
<isConst/>
<!-- VarNode Assignment Dest -->
<IdentNode start='866' end='870'>
<name>demo</name>
<isDeclaredHere/>
<isInitializedHere/>
</IdentNode>
<!-- VarNode Init -->
<UnaryNode type='AWAIT' start='873' end='886'>
<!-- UnaryNode Expression -->
<CallNode start='879' end='886'>
<!-- CallNode Arguments -->
<!-- CallNode Function -->
<IdentNode start='879' end='884'>
<name>dummy</name>
</IdentNode>
</CallNode>
</UnaryNode>
</VarNode>
</Block>
</FunctionNode>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import |>GLOBAL:dummy<| |>CUSTOM2:from<| './dummy.mjs';

const demo = |>CUSTOM2:await<| |>GLOBAL:dummy<|();
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public void testAsyncFunction07() throws Exception {
checkSemantic("testfiles/parser/asyncFunctions/asyncFunctions7.js");
}

public void testTopLevelAwait() throws Exception {
checkSemantic("testfiles/parser/topLevelAwait.js");
}

public void testObjectAsParam() throws Exception {
// TODO arguments can not be handled as global
checkSemantic("testfiles/model/objectAsParameter.js");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ public void testAsyncFunction07() throws Exception {
checkAstResult("testfiles/parser/asyncFunctions/asyncFunctions7.js");
}

public void testTopLevelAwait() throws Exception {
checkAstResult("testfiles/parser/topLevelAwait.js");
}

public void testBinary01() throws Exception {
checkAstResult("testfiles/ecmascript6/parser/expression/binary/binary01.js");
}
Expand Down Expand Up @@ -2094,8 +2098,8 @@ public void testInvalidEscapedSurrogatePairs() throws Exception {
checkAstResult("testfiles/ecmascript6/parser/ES6/identifier/invalid_escaped_surrogate_pairs.js");
}

public void testInvalidExpressionAwait() throws Exception {
checkAstResult("testfiles/ecmascript6/parser/ES6/identifier/invalid_expression_await.js");
public void testExportAwait() throws Exception {
checkAstResult("testfiles/ecmascript6/parser/ES6/identifier/export_await.js");
}

public void testInvalidFunctionWait() throws Exception {
Expand Down
6 changes: 4 additions & 2 deletions webcommon/libs.nashorn/src/com/oracle/js/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2571,7 +2571,7 @@ private Expression yieldExpression(boolean noIn) {
}

private Expression awaitExpression() {
assert inAsyncFunction();
assert inAsyncFunction() || isModule;
// Capture await token.
long awaitToken = token;
nextOrEOL();
Expand Down Expand Up @@ -4732,7 +4732,9 @@ private Expression unaryExpression() {
return verifyIncDecExpression(unaryToken, opType, lhs, false);

default:
if (isAwait(token) && inAsyncFunction() && isAtLeastES7()) {
if (isAwait(token)
&& ((inAsyncFunction() && isAtLeastES7())
|| (isModule && isAtLeastES13()))) {
return awaitExpression();
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,16 @@ public void testMetaProperties() {
assertNotNull(findNode(programm2, n -> n instanceof IdentNode && "new".equals(((IdentNode) n).getName()), IdentNode.class));
}

@Test
public void testTopLevelAwait() {
// Validate top-level await is support for ES13 modules
assertParses(13, true, false, "await Promise.resolve(1);");
// Validate top-level await is not support for ES12 modules
assertParses(12, true, true, "await Promise.resolve(1);");
// Validate top-level await is not supported for ES13 non-modules
assertParses(13, false, true, "await Promise.resolve(1);");
}

private Predicate<Node> functionNodeWithName(String name) {
return n -> n instanceof FunctionNode && name.equals(((FunctionNode) n).getName());
}
Expand Down

0 comments on commit 2163885

Please sign in to comment.