diff --git a/tsc/internal/bundled/libs/lib.dom.d.ts b/tsc/internal/bundled/libs/lib.dom.d.ts index 7f436fb0854d0..2b75c7e7fe5b6 100644 --- a/tsc/internal/bundled/libs/lib.dom.d.ts +++ b/tsc/internal/bundled/libs/lib.dom.d.ts @@ -43547,6 +43547,29 @@ declare namespace WebAssembly { (message?: string): RuntimeError; }; + /** The **`WebAssembly.SuspendError`** object indicates an error during WebAssembly function suspension. */ + interface SuspendError extends Error { + } + + var SuspendError: { + prototype: SuspendError; + new(message?: string): SuspendError; + (message?: string): SuspendError; + }; + + /** + * The **`WebAssembly.Suspending`** object represents a suspending function — an asynchronous (Promise-based) JavaScript function that, when imported into a Wasm module and called from inside, will result in execution being suspended until the promise resolves. + * + * [MDN Reference](https://developer.mozilla.org/docs/WebAssembly/Reference/JavaScript_interface/Suspending) + */ + interface Suspending { + } + + var Suspending: { + prototype: Suspending; + new(jsFun: Function): Suspending; + }; + /** * The **`WebAssembly.Table`** object is a JavaScript wrapper object — an array-like structure representing a WebAssembly table, which stores homogeneous references. A table created by JavaScript or in WebAssembly code will be accessible and mutable from both JavaScript and WebAssembly. * @@ -43661,7 +43684,7 @@ declare namespace WebAssembly { type AddressValue = number; type ExportValue = Function | Global | Memory | Table; type Exports = Record; - type ImportValue = ExportValue | number; + type ImportValue = ExportValue | Suspending | number; type Imports = Record; type ModuleImports = Record; type ValueType = keyof ValueTypeMap; @@ -43675,6 +43698,8 @@ declare namespace WebAssembly { function instantiate(moduleObject: Module, importObject?: Imports): Promise; /** [MDN Reference](https://developer.mozilla.org/docs/WebAssembly/Reference/JavaScript_interface/instantiateStreaming_static) */ function instantiateStreaming(source: Response | PromiseLike, importObject?: Imports, options?: WebAssemblyCompileOptions): Promise; + /** [MDN Reference](https://developer.mozilla.org/docs/WebAssembly/Reference/JavaScript_interface/promising_static) */ + function promising(wasmFunc: Function): Function; /** [MDN Reference](https://developer.mozilla.org/docs/WebAssembly/Reference/JavaScript_interface/validate_static) */ function validate(bytes: AllowSharedBufferSource, options?: WebAssemblyCompileOptions): boolean; } diff --git a/tsc/internal/bundled/libs/lib.webworker.d.ts b/tsc/internal/bundled/libs/lib.webworker.d.ts index 25f2e29b3261e..fad05b90bcc8e 100644 --- a/tsc/internal/bundled/libs/lib.webworker.d.ts +++ b/tsc/internal/bundled/libs/lib.webworker.d.ts @@ -14896,6 +14896,29 @@ declare namespace WebAssembly { (message?: string): RuntimeError; }; + /** The **`WebAssembly.SuspendError`** object indicates an error during WebAssembly function suspension. */ + interface SuspendError extends Error { + } + + var SuspendError: { + prototype: SuspendError; + new(message?: string): SuspendError; + (message?: string): SuspendError; + }; + + /** + * The **`WebAssembly.Suspending`** object represents a suspending function — an asynchronous (Promise-based) JavaScript function that, when imported into a Wasm module and called from inside, will result in execution being suspended until the promise resolves. + * + * [MDN Reference](https://developer.mozilla.org/docs/WebAssembly/Reference/JavaScript_interface/Suspending) + */ + interface Suspending { + } + + var Suspending: { + prototype: Suspending; + new(jsFun: Function): Suspending; + }; + /** * The **`WebAssembly.Table`** object is a JavaScript wrapper object — an array-like structure representing a WebAssembly table, which stores homogeneous references. A table created by JavaScript or in WebAssembly code will be accessible and mutable from both JavaScript and WebAssembly. * @@ -15010,7 +15033,7 @@ declare namespace WebAssembly { type AddressValue = number; type ExportValue = Function | Global | Memory | Table; type Exports = Record; - type ImportValue = ExportValue | number; + type ImportValue = ExportValue | Suspending | number; type Imports = Record; type ModuleImports = Record; type ValueType = keyof ValueTypeMap; @@ -15024,6 +15047,8 @@ declare namespace WebAssembly { function instantiate(moduleObject: Module, importObject?: Imports): Promise; /** [MDN Reference](https://developer.mozilla.org/docs/WebAssembly/Reference/JavaScript_interface/instantiateStreaming_static) */ function instantiateStreaming(source: Response | PromiseLike, importObject?: Imports, options?: WebAssemblyCompileOptions): Promise; + /** [MDN Reference](https://developer.mozilla.org/docs/WebAssembly/Reference/JavaScript_interface/promising_static) */ + function promising(wasmFunc: Function): Function; /** [MDN Reference](https://developer.mozilla.org/docs/WebAssembly/Reference/JavaScript_interface/validate_static) */ function validate(bytes: AllowSharedBufferSource, options?: WebAssemblyCompileOptions): boolean; } diff --git a/tsc/testdata/baselines/reference/compiler/webAssemblyPromiseIntegration.errors.txt b/tsc/testdata/baselines/reference/compiler/webAssemblyPromiseIntegration.errors.txt new file mode 100644 index 0000000000000..f909020e2139e --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/webAssemblyPromiseIntegration.errors.txt @@ -0,0 +1,44 @@ +webAssemblyPromiseIntegration.ts(33,28): error TS2345: Argument of type 'number' is not assignable to parameter of type 'Function'. +webAssemblyPromiseIntegration.ts(34,23): error TS2345: Argument of type 'string' is not assignable to parameter of type 'Function'. + + +==== webAssemblyPromiseIntegration.ts (2 errors) ==== + function someAsyncFunction() { + return fetch("https://example.com").then(result => result.status); + } + + const suspending: WebAssembly.Suspending = new WebAssembly.Suspending(someAsyncFunction); + + const importObj = { + someAsyncFunction: suspending, + }; + + async function main() { + const { instance } = await WebAssembly.instantiateStreaming(fetch("module.wasm"), { importObj }); + const run: Function = WebAssembly.promising(instance.exports.run as Function); + await run(); + } + + const imports: WebAssembly.Imports = { + env: { + sync: () => 0, + async: new WebAssembly.Suspending(async () => 0), + }, + }; + + function handle(e: unknown) { + if (e instanceof WebAssembly.SuspendError) { + const err: Error = e; + } + } + + const err1: WebAssembly.SuspendError = new WebAssembly.SuspendError("message"); + const err2: WebAssembly.SuspendError = WebAssembly.SuspendError(); + + new WebAssembly.Suspending(42); // error + ~~ +!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'Function'. + WebAssembly.promising("not a function"); // error + ~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'Function'. + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/webAssemblyPromiseIntegration.js b/tsc/testdata/baselines/reference/compiler/webAssemblyPromiseIntegration.js new file mode 100644 index 0000000000000..b5f2f53e2d70c --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/webAssemblyPromiseIntegration.js @@ -0,0 +1,68 @@ +//// [tests/cases/compiler/webAssemblyPromiseIntegration.ts] //// + +//// [webAssemblyPromiseIntegration.ts] +function someAsyncFunction() { + return fetch("https://example.com").then(result => result.status); +} + +const suspending: WebAssembly.Suspending = new WebAssembly.Suspending(someAsyncFunction); + +const importObj = { + someAsyncFunction: suspending, +}; + +async function main() { + const { instance } = await WebAssembly.instantiateStreaming(fetch("module.wasm"), { importObj }); + const run: Function = WebAssembly.promising(instance.exports.run as Function); + await run(); +} + +const imports: WebAssembly.Imports = { + env: { + sync: () => 0, + async: new WebAssembly.Suspending(async () => 0), + }, +}; + +function handle(e: unknown) { + if (e instanceof WebAssembly.SuspendError) { + const err: Error = e; + } +} + +const err1: WebAssembly.SuspendError = new WebAssembly.SuspendError("message"); +const err2: WebAssembly.SuspendError = WebAssembly.SuspendError(); + +new WebAssembly.Suspending(42); // error +WebAssembly.promising("not a function"); // error + + +//// [webAssemblyPromiseIntegration.js] +"use strict"; +function someAsyncFunction() { + return fetch("https://example.com").then(result => result.status); +} +const suspending = new WebAssembly.Suspending(someAsyncFunction); +const importObj = { + someAsyncFunction: suspending, +}; +async function main() { + const { instance } = await WebAssembly.instantiateStreaming(fetch("module.wasm"), { importObj }); + const run = WebAssembly.promising(instance.exports.run); + await run(); +} +const imports = { + env: { + sync: () => 0, + async: new WebAssembly.Suspending(async () => 0), + }, +}; +function handle(e) { + if (e instanceof WebAssembly.SuspendError) { + const err = e; + } +} +const err1 = new WebAssembly.SuspendError("message"); +const err2 = WebAssembly.SuspendError(); +new WebAssembly.Suspending(42); // error +WebAssembly.promising("not a function"); // error diff --git a/tsc/testdata/baselines/reference/compiler/webAssemblyPromiseIntegration.symbols b/tsc/testdata/baselines/reference/compiler/webAssemblyPromiseIntegration.symbols new file mode 100644 index 0000000000000..00b115085a137 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/webAssemblyPromiseIntegration.symbols @@ -0,0 +1,123 @@ +//// [tests/cases/compiler/webAssemblyPromiseIntegration.ts] //// + +=== webAssemblyPromiseIntegration.ts === +function someAsyncFunction() { +>someAsyncFunction : Symbol(someAsyncFunction, Decl(webAssemblyPromiseIntegration.ts, 0, 0)) + + return fetch("https://example.com").then(result => result.status); +>fetch("https://example.com").then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --)) +>fetch : Symbol(fetch, Decl(lib.dom.d.ts, --, --)) +>then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --)) +>result : Symbol(result, Decl(webAssemblyPromiseIntegration.ts, 1, 45)) +>result.status : Symbol(Response.status, Decl(lib.dom.d.ts, --, --)) +>result : Symbol(result, Decl(webAssemblyPromiseIntegration.ts, 1, 45)) +>status : Symbol(Response.status, Decl(lib.dom.d.ts, --, --)) +} + +const suspending: WebAssembly.Suspending = new WebAssembly.Suspending(someAsyncFunction); +>suspending : Symbol(suspending, Decl(webAssemblyPromiseIntegration.ts, 4, 5)) +>WebAssembly : Symbol(WebAssembly, Decl(lib.dom.d.ts, --, --)) +>Suspending : Symbol(WebAssembly.Suspending, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>WebAssembly.Suspending : Symbol(WebAssembly.Suspending, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>WebAssembly : Symbol(WebAssembly, Decl(lib.dom.d.ts, --, --)) +>Suspending : Symbol(WebAssembly.Suspending, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>someAsyncFunction : Symbol(someAsyncFunction, Decl(webAssemblyPromiseIntegration.ts, 0, 0)) + +const importObj = { +>importObj : Symbol(importObj, Decl(webAssemblyPromiseIntegration.ts, 6, 5)) + + someAsyncFunction: suspending, +>someAsyncFunction : Symbol(someAsyncFunction, Decl(webAssemblyPromiseIntegration.ts, 6, 19)) +>suspending : Symbol(suspending, Decl(webAssemblyPromiseIntegration.ts, 4, 5)) + +}; + +async function main() { +>main : Symbol(main, Decl(webAssemblyPromiseIntegration.ts, 8, 2)) + + const { instance } = await WebAssembly.instantiateStreaming(fetch("module.wasm"), { importObj }); +>instance : Symbol(instance, Decl(webAssemblyPromiseIntegration.ts, 11, 11)) +>WebAssembly.instantiateStreaming : Symbol(WebAssembly.instantiateStreaming, Decl(lib.dom.d.ts, --, --)) +>WebAssembly : Symbol(WebAssembly, Decl(lib.dom.d.ts, --, --)) +>instantiateStreaming : Symbol(WebAssembly.instantiateStreaming, Decl(lib.dom.d.ts, --, --)) +>fetch : Symbol(fetch, Decl(lib.dom.d.ts, --, --)) +>importObj : Symbol(importObj, Decl(webAssemblyPromiseIntegration.ts, 11, 87)) + + const run: Function = WebAssembly.promising(instance.exports.run as Function); +>run : Symbol(run, Decl(webAssemblyPromiseIntegration.ts, 12, 9)) +>Function : Symbol(Function, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.decorators.d.ts, --, --)) +>WebAssembly.promising : Symbol(WebAssembly.promising, Decl(lib.dom.d.ts, --, --)) +>WebAssembly : Symbol(WebAssembly, Decl(lib.dom.d.ts, --, --)) +>promising : Symbol(WebAssembly.promising, Decl(lib.dom.d.ts, --, --)) +>instance.exports : Symbol(WebAssembly.Instance.exports, Decl(lib.dom.d.ts, --, --)) +>instance : Symbol(instance, Decl(webAssemblyPromiseIntegration.ts, 11, 11)) +>exports : Symbol(WebAssembly.Instance.exports, Decl(lib.dom.d.ts, --, --)) +>Function : Symbol(Function, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.decorators.d.ts, --, --)) + + await run(); +>run : Symbol(run, Decl(webAssemblyPromiseIntegration.ts, 12, 9)) +} + +const imports: WebAssembly.Imports = { +>imports : Symbol(imports, Decl(webAssemblyPromiseIntegration.ts, 16, 5)) +>WebAssembly : Symbol(WebAssembly, Decl(lib.dom.d.ts, --, --)) +>Imports : Symbol(WebAssembly.Imports, Decl(lib.dom.d.ts, --, --)) + + env: { +>env : Symbol(env, Decl(webAssemblyPromiseIntegration.ts, 16, 38)) + + sync: () => 0, +>sync : Symbol(sync, Decl(webAssemblyPromiseIntegration.ts, 17, 10)) + + async: new WebAssembly.Suspending(async () => 0), +>async : Symbol(async, Decl(webAssemblyPromiseIntegration.ts, 18, 22)) +>WebAssembly.Suspending : Symbol(WebAssembly.Suspending, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>WebAssembly : Symbol(WebAssembly, Decl(lib.dom.d.ts, --, --)) +>Suspending : Symbol(WebAssembly.Suspending, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) + + }, +}; + +function handle(e: unknown) { +>handle : Symbol(handle, Decl(webAssemblyPromiseIntegration.ts, 21, 2)) +>e : Symbol(e, Decl(webAssemblyPromiseIntegration.ts, 23, 16)) + + if (e instanceof WebAssembly.SuspendError) { +>e : Symbol(e, Decl(webAssemblyPromiseIntegration.ts, 23, 16)) +>WebAssembly.SuspendError : Symbol(WebAssembly.SuspendError, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>WebAssembly : Symbol(WebAssembly, Decl(lib.dom.d.ts, --, --)) +>SuspendError : Symbol(WebAssembly.SuspendError, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) + + const err: Error = e; +>err : Symbol(err, Decl(webAssemblyPromiseIntegration.ts, 25, 13)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --)) +>e : Symbol(e, Decl(webAssemblyPromiseIntegration.ts, 23, 16)) + } +} + +const err1: WebAssembly.SuspendError = new WebAssembly.SuspendError("message"); +>err1 : Symbol(err1, Decl(webAssemblyPromiseIntegration.ts, 29, 5)) +>WebAssembly : Symbol(WebAssembly, Decl(lib.dom.d.ts, --, --)) +>SuspendError : Symbol(WebAssembly.SuspendError, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>WebAssembly.SuspendError : Symbol(WebAssembly.SuspendError, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>WebAssembly : Symbol(WebAssembly, Decl(lib.dom.d.ts, --, --)) +>SuspendError : Symbol(WebAssembly.SuspendError, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) + +const err2: WebAssembly.SuspendError = WebAssembly.SuspendError(); +>err2 : Symbol(err2, Decl(webAssemblyPromiseIntegration.ts, 30, 5)) +>WebAssembly : Symbol(WebAssembly, Decl(lib.dom.d.ts, --, --)) +>SuspendError : Symbol(WebAssembly.SuspendError, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>WebAssembly.SuspendError : Symbol(WebAssembly.SuspendError, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>WebAssembly : Symbol(WebAssembly, Decl(lib.dom.d.ts, --, --)) +>SuspendError : Symbol(WebAssembly.SuspendError, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) + +new WebAssembly.Suspending(42); // error +>WebAssembly.Suspending : Symbol(WebAssembly.Suspending, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>WebAssembly : Symbol(WebAssembly, Decl(lib.dom.d.ts, --, --)) +>Suspending : Symbol(WebAssembly.Suspending, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) + +WebAssembly.promising("not a function"); // error +>WebAssembly.promising : Symbol(WebAssembly.promising, Decl(lib.dom.d.ts, --, --)) +>WebAssembly : Symbol(WebAssembly, Decl(lib.dom.d.ts, --, --)) +>promising : Symbol(WebAssembly.promising, Decl(lib.dom.d.ts, --, --)) + diff --git a/tsc/testdata/baselines/reference/compiler/webAssemblyPromiseIntegration.types b/tsc/testdata/baselines/reference/compiler/webAssemblyPromiseIntegration.types new file mode 100644 index 0000000000000..b5a26f76f6542 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/webAssemblyPromiseIntegration.types @@ -0,0 +1,148 @@ +//// [tests/cases/compiler/webAssemblyPromiseIntegration.ts] //// + +=== webAssemblyPromiseIntegration.ts === +function someAsyncFunction() { +>someAsyncFunction : () => Promise + + return fetch("https://example.com").then(result => result.status); +>fetch("https://example.com").then(result => result.status) : Promise +>fetch("https://example.com").then : (onfulfilled?: ((value: Response) => TResult1 | PromiseLike) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike) | null | undefined) => Promise +>fetch("https://example.com") : Promise +>fetch : (input: RequestInfo | URL, init?: RequestInit) => Promise +>"https://example.com" : "https://example.com" +>then : (onfulfilled?: ((value: Response) => TResult1 | PromiseLike) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike) | null | undefined) => Promise +>result => result.status : (result: Response) => number +>result : Response +>result.status : number +>result : Response +>status : number +} + +const suspending: WebAssembly.Suspending = new WebAssembly.Suspending(someAsyncFunction); +>suspending : WebAssembly.Suspending +>WebAssembly : any +>new WebAssembly.Suspending(someAsyncFunction) : WebAssembly.Suspending +>WebAssembly.Suspending : { new (jsFun: Function): WebAssembly.Suspending; prototype: WebAssembly.Suspending; } +>WebAssembly : typeof WebAssembly +>Suspending : { new (jsFun: Function): WebAssembly.Suspending; prototype: WebAssembly.Suspending; } +>someAsyncFunction : () => Promise + +const importObj = { +>importObj : { someAsyncFunction: WebAssembly.Suspending; } +>{ someAsyncFunction: suspending,} : { someAsyncFunction: WebAssembly.Suspending; } + + someAsyncFunction: suspending, +>someAsyncFunction : WebAssembly.Suspending +>suspending : WebAssembly.Suspending + +}; + +async function main() { +>main : () => Promise + + const { instance } = await WebAssembly.instantiateStreaming(fetch("module.wasm"), { importObj }); +>instance : WebAssembly.Instance +>await WebAssembly.instantiateStreaming(fetch("module.wasm"), { importObj }) : WebAssembly.WebAssemblyInstantiatedSource +>WebAssembly.instantiateStreaming(fetch("module.wasm"), { importObj }) : Promise +>WebAssembly.instantiateStreaming : (source: Response | PromiseLike, importObject?: WebAssembly.Imports, options?: WebAssembly.WebAssemblyCompileOptions) => Promise +>WebAssembly : typeof WebAssembly +>instantiateStreaming : (source: Response | PromiseLike, importObject?: WebAssembly.Imports, options?: WebAssembly.WebAssemblyCompileOptions) => Promise +>fetch("module.wasm") : Promise +>fetch : (input: RequestInfo | URL, init?: RequestInit) => Promise +>"module.wasm" : "module.wasm" +>{ importObj } : { importObj: { someAsyncFunction: WebAssembly.Suspending; }; } +>importObj : { someAsyncFunction: WebAssembly.Suspending; } + + const run: Function = WebAssembly.promising(instance.exports.run as Function); +>run : Function +>WebAssembly.promising(instance.exports.run as Function) : Function +>WebAssembly.promising : (wasmFunc: Function) => Function +>WebAssembly : typeof WebAssembly +>promising : (wasmFunc: Function) => Function +>instance.exports.run as Function : Function +>instance.exports.run : WebAssembly.ExportValue +>instance.exports : WebAssembly.Exports +>instance : WebAssembly.Instance +>exports : WebAssembly.Exports +>run : WebAssembly.ExportValue + + await run(); +>await run() : any +>run() : any +>run : Function +} + +const imports: WebAssembly.Imports = { +>imports : WebAssembly.Imports +>WebAssembly : any +>{ env: { sync: () => 0, async: new WebAssembly.Suspending(async () => 0), },} : { env: { sync: () => number; async: WebAssembly.Suspending; }; } + + env: { +>env : { sync: () => number; async: WebAssembly.Suspending; } +>{ sync: () => 0, async: new WebAssembly.Suspending(async () => 0), } : { sync: () => number; async: WebAssembly.Suspending; } + + sync: () => 0, +>sync : () => number +>() => 0 : () => number +>0 : 0 + + async: new WebAssembly.Suspending(async () => 0), +>async : WebAssembly.Suspending +>new WebAssembly.Suspending(async () => 0) : WebAssembly.Suspending +>WebAssembly.Suspending : { new (jsFun: Function): WebAssembly.Suspending; prototype: WebAssembly.Suspending; } +>WebAssembly : typeof WebAssembly +>Suspending : { new (jsFun: Function): WebAssembly.Suspending; prototype: WebAssembly.Suspending; } +>async () => 0 : () => Promise +>0 : 0 + + }, +}; + +function handle(e: unknown) { +>handle : (e: unknown) => void +>e : unknown + + if (e instanceof WebAssembly.SuspendError) { +>e instanceof WebAssembly.SuspendError : boolean +>e : unknown +>WebAssembly.SuspendError : { (message?: string): WebAssembly.SuspendError; new (message?: string): WebAssembly.SuspendError; prototype: WebAssembly.SuspendError; } +>WebAssembly : typeof WebAssembly +>SuspendError : { (message?: string): WebAssembly.SuspendError; new (message?: string): WebAssembly.SuspendError; prototype: WebAssembly.SuspendError; } + + const err: Error = e; +>err : Error +>e : WebAssembly.SuspendError + } +} + +const err1: WebAssembly.SuspendError = new WebAssembly.SuspendError("message"); +>err1 : WebAssembly.SuspendError +>WebAssembly : any +>new WebAssembly.SuspendError("message") : WebAssembly.SuspendError +>WebAssembly.SuspendError : { (message?: string): WebAssembly.SuspendError; new (message?: string): WebAssembly.SuspendError; prototype: WebAssembly.SuspendError; } +>WebAssembly : typeof WebAssembly +>SuspendError : { (message?: string): WebAssembly.SuspendError; new (message?: string): WebAssembly.SuspendError; prototype: WebAssembly.SuspendError; } +>"message" : "message" + +const err2: WebAssembly.SuspendError = WebAssembly.SuspendError(); +>err2 : WebAssembly.SuspendError +>WebAssembly : any +>WebAssembly.SuspendError() : WebAssembly.SuspendError +>WebAssembly.SuspendError : { (message?: string): WebAssembly.SuspendError; new (message?: string): WebAssembly.SuspendError; prototype: WebAssembly.SuspendError; } +>WebAssembly : typeof WebAssembly +>SuspendError : { (message?: string): WebAssembly.SuspendError; new (message?: string): WebAssembly.SuspendError; prototype: WebAssembly.SuspendError; } + +new WebAssembly.Suspending(42); // error +>new WebAssembly.Suspending(42) : WebAssembly.Suspending +>WebAssembly.Suspending : { new (jsFun: Function): WebAssembly.Suspending; prototype: WebAssembly.Suspending; } +>WebAssembly : typeof WebAssembly +>Suspending : { new (jsFun: Function): WebAssembly.Suspending; prototype: WebAssembly.Suspending; } +>42 : 42 + +WebAssembly.promising("not a function"); // error +>WebAssembly.promising("not a function") : Function +>WebAssembly.promising : (wasmFunc: Function) => Function +>WebAssembly : typeof WebAssembly +>promising : (wasmFunc: Function) => Function +>"not a function" : "not a function" + diff --git a/tsc/testdata/baselines/reference/compiler/webAssemblyPromiseIntegrationWebworker.js b/tsc/testdata/baselines/reference/compiler/webAssemblyPromiseIntegrationWebworker.js new file mode 100644 index 0000000000000..7376c79ded00b --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/webAssemblyPromiseIntegrationWebworker.js @@ -0,0 +1,28 @@ +//// [tests/cases/compiler/webAssemblyPromiseIntegrationWebworker.ts] //// + +//// [webAssemblyPromiseIntegrationWebworker.ts] +async function main(bytes: ArrayBuffer) { + const { instance } = await WebAssembly.instantiate(bytes, { + env: { + wait: new WebAssembly.Suspending(() => new Promise(resolve => setTimeout(resolve, 10))), + }, + }); + const run: Function = WebAssembly.promising(instance.exports.run as Function); + await run(); +} + +const err: WebAssembly.SuspendError = new WebAssembly.SuspendError("message"); + + +//// [webAssemblyPromiseIntegrationWebworker.js] +"use strict"; +async function main(bytes) { + const { instance } = await WebAssembly.instantiate(bytes, { + env: { + wait: new WebAssembly.Suspending(() => new Promise(resolve => setTimeout(resolve, 10))), + }, + }); + const run = WebAssembly.promising(instance.exports.run); + await run(); +} +const err = new WebAssembly.SuspendError("message"); diff --git a/tsc/testdata/baselines/reference/compiler/webAssemblyPromiseIntegrationWebworker.symbols b/tsc/testdata/baselines/reference/compiler/webAssemblyPromiseIntegrationWebworker.symbols new file mode 100644 index 0000000000000..98429b185e002 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/webAssemblyPromiseIntegrationWebworker.symbols @@ -0,0 +1,53 @@ +//// [tests/cases/compiler/webAssemblyPromiseIntegrationWebworker.ts] //// + +=== webAssemblyPromiseIntegrationWebworker.ts === +async function main(bytes: ArrayBuffer) { +>main : Symbol(main, Decl(webAssemblyPromiseIntegrationWebworker.ts, 0, 0)) +>bytes : Symbol(bytes, Decl(webAssemblyPromiseIntegrationWebworker.ts, 0, 20)) +>ArrayBuffer : Symbol(ArrayBuffer, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2024.arraybuffer.d.ts, --, --)) + + const { instance } = await WebAssembly.instantiate(bytes, { +>instance : Symbol(instance, Decl(webAssemblyPromiseIntegrationWebworker.ts, 1, 11)) +>WebAssembly.instantiate : Symbol(WebAssembly.instantiate, Decl(lib.webworker.d.ts, --, --), Decl(lib.webworker.d.ts, --, --)) +>WebAssembly : Symbol(WebAssembly, Decl(lib.webworker.d.ts, --, --)) +>instantiate : Symbol(WebAssembly.instantiate, Decl(lib.webworker.d.ts, --, --), Decl(lib.webworker.d.ts, --, --)) +>bytes : Symbol(bytes, Decl(webAssemblyPromiseIntegrationWebworker.ts, 0, 20)) + + env: { +>env : Symbol(env, Decl(webAssemblyPromiseIntegrationWebworker.ts, 1, 63)) + + wait: new WebAssembly.Suspending(() => new Promise(resolve => setTimeout(resolve, 10))), +>wait : Symbol(wait, Decl(webAssemblyPromiseIntegrationWebworker.ts, 2, 14)) +>WebAssembly.Suspending : Symbol(WebAssembly.Suspending, Decl(lib.webworker.d.ts, --, --), Decl(lib.webworker.d.ts, --, --)) +>WebAssembly : Symbol(WebAssembly, Decl(lib.webworker.d.ts, --, --)) +>Suspending : Symbol(WebAssembly.Suspending, Decl(lib.webworker.d.ts, --, --), Decl(lib.webworker.d.ts, --, --)) +>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --)) +>resolve : Symbol(resolve, Decl(webAssemblyPromiseIntegrationWebworker.ts, 3, 69)) +>setTimeout : Symbol(setTimeout, Decl(lib.webworker.d.ts, --, --)) +>resolve : Symbol(resolve, Decl(webAssemblyPromiseIntegrationWebworker.ts, 3, 69)) + + }, + }); + const run: Function = WebAssembly.promising(instance.exports.run as Function); +>run : Symbol(run, Decl(webAssemblyPromiseIntegrationWebworker.ts, 6, 9)) +>Function : Symbol(Function, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.decorators.d.ts, --, --)) +>WebAssembly.promising : Symbol(WebAssembly.promising, Decl(lib.webworker.d.ts, --, --)) +>WebAssembly : Symbol(WebAssembly, Decl(lib.webworker.d.ts, --, --)) +>promising : Symbol(WebAssembly.promising, Decl(lib.webworker.d.ts, --, --)) +>instance.exports : Symbol(WebAssembly.Instance.exports, Decl(lib.webworker.d.ts, --, --)) +>instance : Symbol(instance, Decl(webAssemblyPromiseIntegrationWebworker.ts, 1, 11)) +>exports : Symbol(WebAssembly.Instance.exports, Decl(lib.webworker.d.ts, --, --)) +>Function : Symbol(Function, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.esnext.decorators.d.ts, --, --)) + + await run(); +>run : Symbol(run, Decl(webAssemblyPromiseIntegrationWebworker.ts, 6, 9)) +} + +const err: WebAssembly.SuspendError = new WebAssembly.SuspendError("message"); +>err : Symbol(err, Decl(webAssemblyPromiseIntegrationWebworker.ts, 10, 5)) +>WebAssembly : Symbol(WebAssembly, Decl(lib.webworker.d.ts, --, --)) +>SuspendError : Symbol(WebAssembly.SuspendError, Decl(lib.webworker.d.ts, --, --), Decl(lib.webworker.d.ts, --, --)) +>WebAssembly.SuspendError : Symbol(WebAssembly.SuspendError, Decl(lib.webworker.d.ts, --, --), Decl(lib.webworker.d.ts, --, --)) +>WebAssembly : Symbol(WebAssembly, Decl(lib.webworker.d.ts, --, --)) +>SuspendError : Symbol(WebAssembly.SuspendError, Decl(lib.webworker.d.ts, --, --), Decl(lib.webworker.d.ts, --, --)) + diff --git a/tsc/testdata/baselines/reference/compiler/webAssemblyPromiseIntegrationWebworker.types b/tsc/testdata/baselines/reference/compiler/webAssemblyPromiseIntegrationWebworker.types new file mode 100644 index 0000000000000..e238a8e9fc0ba --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/webAssemblyPromiseIntegrationWebworker.types @@ -0,0 +1,67 @@ +//// [tests/cases/compiler/webAssemblyPromiseIntegrationWebworker.ts] //// + +=== webAssemblyPromiseIntegrationWebworker.ts === +async function main(bytes: ArrayBuffer) { +>main : (bytes: ArrayBuffer) => Promise +>bytes : ArrayBuffer + + const { instance } = await WebAssembly.instantiate(bytes, { +>instance : WebAssembly.Instance +>await WebAssembly.instantiate(bytes, { env: { wait: new WebAssembly.Suspending(() => new Promise(resolve => setTimeout(resolve, 10))), }, }) : WebAssembly.WebAssemblyInstantiatedSource +>WebAssembly.instantiate(bytes, { env: { wait: new WebAssembly.Suspending(() => new Promise(resolve => setTimeout(resolve, 10))), }, }) : Promise +>WebAssembly.instantiate : { (bytes: AllowSharedBufferSource, importObject?: WebAssembly.Imports, options?: WebAssembly.WebAssemblyCompileOptions): Promise; (moduleObject: WebAssembly.Module, importObject?: WebAssembly.Imports): Promise; } +>WebAssembly : typeof WebAssembly +>instantiate : { (bytes: AllowSharedBufferSource, importObject?: WebAssembly.Imports, options?: WebAssembly.WebAssemblyCompileOptions): Promise; (moduleObject: WebAssembly.Module, importObject?: WebAssembly.Imports): Promise; } +>bytes : ArrayBuffer +>{ env: { wait: new WebAssembly.Suspending(() => new Promise(resolve => setTimeout(resolve, 10))), }, } : { env: { wait: WebAssembly.Suspending; }; } + + env: { +>env : { wait: WebAssembly.Suspending; } +>{ wait: new WebAssembly.Suspending(() => new Promise(resolve => setTimeout(resolve, 10))), } : { wait: WebAssembly.Suspending; } + + wait: new WebAssembly.Suspending(() => new Promise(resolve => setTimeout(resolve, 10))), +>wait : WebAssembly.Suspending +>new WebAssembly.Suspending(() => new Promise(resolve => setTimeout(resolve, 10))) : WebAssembly.Suspending +>WebAssembly.Suspending : { new (jsFun: Function): WebAssembly.Suspending; prototype: WebAssembly.Suspending; } +>WebAssembly : typeof WebAssembly +>Suspending : { new (jsFun: Function): WebAssembly.Suspending; prototype: WebAssembly.Suspending; } +>() => new Promise(resolve => setTimeout(resolve, 10)) : () => Promise +>new Promise(resolve => setTimeout(resolve, 10)) : Promise +>Promise : PromiseConstructor +>resolve => setTimeout(resolve, 10) : (resolve: (value: void | PromiseLike) => void) => number +>resolve : (value: void | PromiseLike) => void +>setTimeout(resolve, 10) : number +>setTimeout : (handler: TimerHandler, timeout?: number, ...arguments: any[]) => number +>resolve : (value: void | PromiseLike) => void +>10 : 10 + + }, + }); + const run: Function = WebAssembly.promising(instance.exports.run as Function); +>run : Function +>WebAssembly.promising(instance.exports.run as Function) : Function +>WebAssembly.promising : (wasmFunc: Function) => Function +>WebAssembly : typeof WebAssembly +>promising : (wasmFunc: Function) => Function +>instance.exports.run as Function : Function +>instance.exports.run : WebAssembly.ExportValue +>instance.exports : WebAssembly.Exports +>instance : WebAssembly.Instance +>exports : WebAssembly.Exports +>run : WebAssembly.ExportValue + + await run(); +>await run() : any +>run() : any +>run : Function +} + +const err: WebAssembly.SuspendError = new WebAssembly.SuspendError("message"); +>err : WebAssembly.SuspendError +>WebAssembly : any +>new WebAssembly.SuspendError("message") : WebAssembly.SuspendError +>WebAssembly.SuspendError : { (message?: string): WebAssembly.SuspendError; new (message?: string): WebAssembly.SuspendError; prototype: WebAssembly.SuspendError; } +>WebAssembly : typeof WebAssembly +>SuspendError : { (message?: string): WebAssembly.SuspendError; new (message?: string): WebAssembly.SuspendError; prototype: WebAssembly.SuspendError; } +>"message" : "message" + diff --git a/tsc/testdata/tests/cases/compiler/webAssemblyPromiseIntegration.ts b/tsc/testdata/tests/cases/compiler/webAssemblyPromiseIntegration.ts new file mode 100644 index 0000000000000..2de47f7a94242 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/webAssemblyPromiseIntegration.ts @@ -0,0 +1,38 @@ +// @target: esnext +// @lib: esnext,dom +// @strict: true + +function someAsyncFunction() { + return fetch("https://example.com").then(result => result.status); +} + +const suspending: WebAssembly.Suspending = new WebAssembly.Suspending(someAsyncFunction); + +const importObj = { + someAsyncFunction: suspending, +}; + +async function main() { + const { instance } = await WebAssembly.instantiateStreaming(fetch("module.wasm"), { importObj }); + const run: Function = WebAssembly.promising(instance.exports.run as Function); + await run(); +} + +const imports: WebAssembly.Imports = { + env: { + sync: () => 0, + async: new WebAssembly.Suspending(async () => 0), + }, +}; + +function handle(e: unknown) { + if (e instanceof WebAssembly.SuspendError) { + const err: Error = e; + } +} + +const err1: WebAssembly.SuspendError = new WebAssembly.SuspendError("message"); +const err2: WebAssembly.SuspendError = WebAssembly.SuspendError(); + +new WebAssembly.Suspending(42); // error +WebAssembly.promising("not a function"); // error diff --git a/tsc/testdata/tests/cases/compiler/webAssemblyPromiseIntegrationWebworker.ts b/tsc/testdata/tests/cases/compiler/webAssemblyPromiseIntegrationWebworker.ts new file mode 100644 index 0000000000000..dc5a82627a1d4 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/webAssemblyPromiseIntegrationWebworker.ts @@ -0,0 +1,15 @@ +// @target: esnext +// @lib: esnext,webworker +// @strict: true + +async function main(bytes: ArrayBuffer) { + const { instance } = await WebAssembly.instantiate(bytes, { + env: { + wait: new WebAssembly.Suspending(() => new Promise(resolve => setTimeout(resolve, 10))), + }, + }); + const run: Function = WebAssembly.promising(instance.exports.run as Function); + await run(); +} + +const err: WebAssembly.SuspendError = new WebAssembly.SuspendError("message");