Index: test/mjsunit/harmony/async-await-basic.js |
diff --git a/test/mjsunit/harmony/async-await-basic.js b/test/mjsunit/harmony/async-await-basic.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a86850a4ed1d884754005950bd7f3e5518efddf9 |
--- /dev/null |
+++ b/test/mjsunit/harmony/async-await-basic.js |
@@ -0,0 +1,218 @@ |
+// 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. |
+ |
+// Flags: --harmony-async-await |
+ |
+// TODO(caitp): give async functions a non-constructor map |
neis
2016/05/11 09:45:17
This comment is outdated, isn't it?
|
+ |
+// Do not install `AsyncFunction` constructor on global object |
+assertEquals(undefined, this.AsyncFunction); |
+let AsyncFunction = (async function() {}).constructor; |
+ |
+// Let functionPrototype be the intrinsic object %AsyncFunctionPrototype%. |
+async function asyncFunctionForProto() {} |
+assertEquals(AsyncFunction.prototype, |
+ Object.getPrototypeOf(asyncFunctionForProto)); |
+assertEquals(AsyncFunction.prototype, |
+ Object.getPrototypeOf(async function() {})); |
+assertEquals(AsyncFunction.prototype, Object.getPrototypeOf(async () => {})); |
+assertEquals(AsyncFunction.prototype, |
+ Object.getPrototypeOf({ async method() {} }.method)); |
+assertEquals(AsyncFunction.prototype, Object.getPrototypeOf(AsyncFunction())); |
+ |
+// AsyncFunctionCreate does not produce an object with a Prototype |
+assertEquals(undefined, asyncFunctionForProto.prototype); |
+assertEquals(false, asyncFunctionForProto.hasOwnProperty("prototype")); |
+assertEquals(undefined, (async function() {}).prototype); |
+assertEquals(false, (async function() {}).hasOwnProperty("prototype")); |
+assertEquals(undefined, (async() => {}).prototype); |
+assertEquals(false, (async() => {}).hasOwnProperty("prototype")); |
+assertEquals(undefined, ({ async method() {} }).method.prototype); |
+assertEquals(false, ({ async method() {} }).method.hasOwnProperty("prototype")); |
+assertEquals(undefined, AsyncFunction().prototype); |
+assertEquals(false, AsyncFunction().hasOwnProperty("prototype")); |
+ |
+// AsyncFunction.prototype[ @@toStringTag ] |
+var descriptor = |
+ Object.getOwnPropertyDescriptor(AsyncFunction.prototype, |
+ Symbol.toStringTag); |
+assertEquals("AsyncFunction", descriptor.value); |
+assertEquals(false, descriptor.enumerable); |
+assertEquals(false, descriptor.writable); |
+assertEquals(true, descriptor.configurable); |
+ |
+assertEquals(1, AsyncFunction.length); |
+ |
+// Let F be ! FunctionAllocate(functionPrototype, Strict, "non-constructor") |
+async function asyncNonConstructorDecl() {} |
+assertThrows( |
+ () => new asyncNonConstructorDecl(), TypeError); |
+assertThrows( |
+ () => new (async function() {}), TypeError); |
+assertThrows( |
+ () => new ({ async nonConstructor() {} }).nonConstructor(), TypeError); |
+assertThrows( |
+ () => new (() => "not a constructor!"), TypeError); |
+assertThrows( |
+ () => new (AsyncFunction()), TypeError); |
+ |
+// Normal completion |
+async function asyncDecl() { return "test"; } |
+assertEqualsAsync("test", asyncDecl); |
+assertEqualsAsync("test2", async function() { return "test2"; }); |
+assertEqualsAsync("test3", async () => "test3"); |
+assertEqualsAsync("test4", () => ({ async f() { return "test4"; } }).f()); |
+assertEqualsAsync("test5", () => AsyncFunction("no", "return 'test' + no;")(5)); |
+ |
+class MyError extends Error {}; |
+ |
+// Throw completion |
+async function asyncDeclThrower(e) { throw new MyError(e); } |
+assertThrowsAsync(() => asyncDeclThrower("boom!"), MyError, "boom!"); |
+assertThrowsAsync( |
+ () => (async function(e) { throw new MyError(e); })("boom!!!"), |
+ MyError, "boom!!!"); |
+assertThrowsAsync( |
+ () => (async e => { throw new MyError(e) })("boom!!"), MyError, "boom!!"); |
+assertThrowsAsync( |
+ () => ({ async thrower(e) { throw new MyError(e); } }).thrower("boom!1!"), |
+ MyError, "boom!1!"); |
+assertThrowsAsync( |
+ () => AsyncFunction("msg", "throw new MyError(msg)")("boom!2!!"), |
+ MyError, "boom!2!!"); |
+ |
+function resolveLater(value) { return Promise.resolve(value); } |
+function rejectLater(error) { return Promise.reject(error); } |
+ |
+// Resume after Normal completion |
+var log = []; |
+async function resumeAfterNormal(value) { |
+ log.push("start:" + value); |
+ value = await resolveLater(value + 1); |
+ log.push("resume:" + value); |
+ value = await resolveLater(value + 1); |
+ log.push("resume:" + value); |
+ return value + 1; |
+} |
+ |
+assertEqualsAsync(4, () => resumeAfterNormal(1)); |
+assertEquals("start:1 resume:2 resume:3", log.join(" ")); |
+ |
+var O = { |
+ async resumeAfterNormal(value) { |
+ log.push("start:" + value); |
+ value = await resolveLater(value + 1); |
+ log.push("resume:" + value); |
+ value = await resolveLater(value + 1); |
+ log.push("resume:" + value); |
+ return value + 1; |
+ } |
+}; |
+log = []; |
+assertEqualsAsync(5, () => O.resumeAfterNormal(2)); |
+assertEquals("start:2 resume:3 resume:4", log.join(" ")); |
+ |
+var resumeAfterNormalArrow = async (value) => { |
+ try { |
+ log.push("start:" + value); |
neis
2016/05/11 09:45:17
indentation
|
+ value = await resolveLater(value + 1); |
+ log.push("resume:" + value); |
+ value = await resolveLater(value + 1); |
+ log.push("resume:" + value); |
+ return value + 1; |
+ } catch (e) { |
+ print(e.stack); |
+ } |
+}; |
+log = []; |
+assertEqualsAsync(6, () => resumeAfterNormalArrow(3)); |
+assertEquals("start:3 resume:4 resume:5", log.join(" ")); |
+ |
+var resumeAfterNormalEval = AsyncFunction("value", ` |
+ log.push("start:" + value); |
+ value = await resolveLater(value + 1); |
+ log.push("resume:" + value); |
+ value = await resolveLater(value + 1); |
+ log.push("resume:" + value); |
+ return value + 1;`); |
+log = []; |
+assertEqualsAsync(7, () => resumeAfterNormalEval(4)); |
+assertEquals("start:4 resume:5 resume:6", log.join(" ")); |
+ |
+// Resume after Throw completion |
+async function resumeAfterThrow(value) { |
+ log.push("start:" + value); |
+ try { |
+ value = await rejectLater("throw1"); |
+ } catch (e) { |
+ log.push("resume:" + e); |
+ } |
+ try { |
+ value = await rejectLater("throw2"); |
+ } catch (e) { |
+ log.push("resume:" + e); |
+ } |
+ return value + 1; |
+} |
+ |
+log = []; |
+assertEqualsAsync(2, () => resumeAfterThrow(1)); |
+assertEquals("start:1 resume:throw1 resume:throw2", log.join(" ")); |
+ |
+var O = { |
+ async resumeAfterThrow(value) { |
+ log.push("start:" + value); |
+ try { |
+ value = await rejectLater("throw1"); |
+ } catch (e) { |
+ log.push("resume:" + e); |
+ } |
+ try { |
+ value = await rejectLater("throw2"); |
+ } catch (e) { |
+ log.push("resume:" + e); |
+ } |
+ return value + 1; |
+ } |
+} |
+log = []; |
+assertEqualsAsync(3, () => O.resumeAfterThrow(2)); |
+assertEquals("start:2 resume:throw1 resume:throw2", log.join(" ")); |
+ |
+var resumeAfterThrowArrow = async (value) => { |
+ log.push("start:" + value); |
+ try { |
+ value = await rejectLater("throw1"); |
+ } catch (e) { |
+ log.push("resume:" + e); |
+ } |
+ try { |
+ value = await rejectLater("throw2"); |
+ } catch (e) { |
+ log.push("resume:" + e); |
+ } |
+ return value + 1; |
+}; |
+ |
+log = []; |
+ |
+assertEqualsAsync(4, () => resumeAfterThrowArrow(3)); |
+assertEquals("start:3 resume:throw1 resume:throw2", log.join(" ")); |
+ |
+var resumeAfterThrowEval = AsyncFunction("value", ` |
+ log.push("start:" + value); |
+ try { |
+ value = await rejectLater("throw1"); |
+ } catch (e) { |
+ log.push("resume:" + e); |
+ } |
+ try { |
+ value = await rejectLater("throw2"); |
+ } catch (e) { |
+ log.push("resume:" + e); |
+ } |
+ return value + 1;`); |
+log = []; |
+assertEqualsAsync(5, () => resumeAfterThrowEval(4)); |
+assertEquals("start:4 resume:throw1 resume:throw2", log.join(" ")); |