Support parsing async modifiers for functions. (#3460)
Only parsing is implemented, so the async functions currently behave like normal function except they return with a resolved Promise object when the function is terminated correctly. JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
// Copyright JS Foundation and other contributors, http://js.foundation
|
||||
//
|
||||
// Licensed 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.
|
||||
|
||||
/* This test checks async modifiers (nothing else). */
|
||||
|
||||
function check_promise(p, value)
|
||||
{
|
||||
assert(p instanceof Promise)
|
||||
|
||||
p.then(function(v) {
|
||||
assert(v === value)
|
||||
})
|
||||
}
|
||||
|
||||
/* Async functions */
|
||||
|
||||
async function f(a) {
|
||||
return a
|
||||
}
|
||||
|
||||
check_promise(f(1), 1)
|
||||
|
||||
f = async function (a) { return a }
|
||||
check_promise(f(2), 2)
|
||||
|
||||
f = (async function (a) { return a })
|
||||
check_promise(f(3), 3)
|
||||
|
||||
f = [async function (a) { return a }]
|
||||
check_promise(f[0](4), 4)
|
||||
|
||||
/* These four are parser tests. */
|
||||
async => {}
|
||||
async async => {}
|
||||
(async => {})
|
||||
(async async => {})
|
||||
|
||||
f = async => async;
|
||||
assert(f(5) === 5)
|
||||
|
||||
f = async async => async;
|
||||
check_promise(f(6), 6)
|
||||
|
||||
f = (async => async)
|
||||
assert(f(7) === 7)
|
||||
|
||||
f = (async async => async)
|
||||
check_promise(f(8), 8)
|
||||
|
||||
f = [async => async]
|
||||
assert(f[0](9) === 9)
|
||||
|
||||
f = [async async => async]
|
||||
check_promise(f[0](10), 10)
|
||||
|
||||
f = async (a, b) => a + b;
|
||||
check_promise(f(10, 1), 11)
|
||||
|
||||
f = (async (a, b) => a + b);
|
||||
check_promise(f(10, 2), 12)
|
||||
|
||||
f = [async (a, b) => a + b];
|
||||
check_promise(f[0](10, 3), 13)
|
||||
|
||||
f = true ? async () => 14 : 0;
|
||||
check_promise(f(), 14)
|
||||
|
||||
f = (1, async async => async)
|
||||
check_promise(f(15), 15)
|
||||
|
||||
/* Functions contain async references */
|
||||
|
||||
function f1() {
|
||||
var async = 1;
|
||||
|
||||
/* The arrow function after the newline should be ignored. */
|
||||
var v1 = async
|
||||
async => async
|
||||
|
||||
/* The function statement after the newline should not be an async function. */
|
||||
var v2 = async
|
||||
function g() { return 2 }
|
||||
|
||||
async
|
||||
function h() { return 3 }
|
||||
|
||||
assert(v1 === 1)
|
||||
assert(v2 === 1)
|
||||
assert(g() === 2)
|
||||
assert(h() === 3)
|
||||
}
|
||||
f1();
|
||||
|
||||
function f2() {
|
||||
var async = 1;
|
||||
|
||||
function g() { async = 2; }
|
||||
g();
|
||||
|
||||
assert(async == 2);
|
||||
}
|
||||
f2();
|
||||
|
||||
function f3() {
|
||||
var v = 3;
|
||||
var async = () => v = 4;
|
||||
|
||||
function g() { async(); }
|
||||
g();
|
||||
|
||||
assert(v === 4);
|
||||
}
|
||||
f3();
|
||||
|
||||
function f4() {
|
||||
var v = 5;
|
||||
var async = (a, b) => v = a + b;
|
||||
|
||||
function g() { async(((v)), ((v))); }
|
||||
g();
|
||||
|
||||
assert(v === 10);
|
||||
}
|
||||
f4();
|
||||
|
||||
function f5() {
|
||||
var v = 0;
|
||||
var async = (a, b) => v = a + b;
|
||||
|
||||
function g() { async((async(1,2)), ((async(3,4)))); }
|
||||
g();
|
||||
|
||||
assert(v === 10);
|
||||
}
|
||||
f5();
|
||||
@@ -0,0 +1,69 @@
|
||||
// Copyright JS Foundation and other contributors, http://js.foundation
|
||||
//
|
||||
// Licensed 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.
|
||||
|
||||
/* This test checks async modifiers (nothing else). */
|
||||
|
||||
function check_syntax_error (code)
|
||||
{
|
||||
try {
|
||||
eval (code)
|
||||
assert (false)
|
||||
} catch (e) {
|
||||
assert (e instanceof SyntaxError)
|
||||
}
|
||||
}
|
||||
|
||||
check_syntax_error("function async f() {}")
|
||||
check_syntax_error("(a,b) async => 1")
|
||||
/* SyntaxError because arrow declaration is an assignment expression. */
|
||||
check_syntax_error("async * (a,b) => 1")
|
||||
check_syntax_error("({ *async f() {} })")
|
||||
check_syntax_error("class C { async static f() {} }")
|
||||
check_syntax_error("class C { * async f() {} }")
|
||||
check_syntax_error("class C { static * async f() {} }")
|
||||
|
||||
|
||||
function check_promise(p, value)
|
||||
{
|
||||
assert(p instanceof Promise)
|
||||
|
||||
p.then(function(v) {
|
||||
assert(v === value)
|
||||
})
|
||||
}
|
||||
|
||||
var o = {
|
||||
async f() { return 1 },
|
||||
async() { return 2 },
|
||||
async *x() {}, /* Parser test, async iterators are needed to work. */
|
||||
}
|
||||
|
||||
check_promise(o.f(), 1)
|
||||
assert(o.async() === 2)
|
||||
|
||||
class C {
|
||||
async f() { return 3 }
|
||||
async *x() {} /* Parser test, async iterators are needed to work. */
|
||||
static async f() { return 4 }
|
||||
static async *y() {} /* Parser test, async iterators are needed to work. */
|
||||
async() { return 5 }
|
||||
static async() { return 6 }
|
||||
}
|
||||
|
||||
var c = new C
|
||||
|
||||
check_promise(c.f(), 3)
|
||||
check_promise(C.f(), 4)
|
||||
assert(c.async() === 5)
|
||||
assert(C.async() === 6)
|
||||
@@ -223,7 +223,7 @@ main (void)
|
||||
/* Check the snapshot data. Unused bytes should be filled with zeroes */
|
||||
const uint8_t expected_data[] =
|
||||
{
|
||||
0x4A, 0x52, 0x52, 0x59, 0x23, 0x00, 0x00, 0x00,
|
||||
0x4A, 0x52, 0x52, 0x59, 0x24, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00,
|
||||
0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
|
||||
0x03, 0x00, 0x01, 0x00, 0x41, 0x00, 0x01, 0x00,
|
||||
|
||||
Reference in New Issue
Block a user