diff --git a/NOTICE b/NOTICE index fc6a7f2a42..dd77b67022 100644 --- a/NOTICE +++ b/NOTICE @@ -33,6 +33,7 @@ under the licensing terms detailed in LICENSE: * Piotr Oleś * Saúl Cabrera * Chance Snow +* Jerry Green * Peter Salomonsen Portions of this software are derived from third-party works licensed under @@ -59,6 +60,6 @@ the following terms: The 3-Clause BSD License (https://opensource.org/licenses/BSD-3-Clause) * Arm Optimized Routines: https://github.com/ARM-software/optimized-routines - + Copyright (c) Arm Limited The MIT License (https://opensource.org/licenses/MIT) diff --git a/package.json b/package.json index 01f1d81b6d..e39e12479a 100644 --- a/package.json +++ b/package.json @@ -76,12 +76,13 @@ "check:config": "tsc --noEmit -p src --diagnostics --listFiles", "check:require": "tsc --noEmit --target ESNEXT --module commonjs --experimentalDecorators tests/require/index", "check:lint": "eslint --max-warnings 0 --ext js . && eslint --max-warnings 0 --ext ts .", - "test": "npm run test:parser && npm run test:compiler && npm run test:packages && npm run test:extension && npm run test:asconfig", + "test": "npm run test:parser && npm run test:compiler && npm run test:packages && npm run test:extension && npm run test:asconfig && npm run test:imports", "test:parser": "node tests/parser", "test:compiler": "node --experimental-wasi-unstable-preview1 tests/compiler", "test:packages": "cd tests/packages && npm run test", "test:extension": "cd tests/extension && npm run test", "test:asconfig": "cd tests/asconfig && npm run test", + "test:imports": "cd tests/imports && npm run test", "make": "npm run clean && npm test && npm run build && npm test", "all": "npm run check && npm run make", "docs": "typedoc --tsconfig tsconfig-docs.json --mode modules --name \"AssemblyScript Compiler API\" --out ./docs/api --ignoreCompilerErrors --excludeNotExported --excludePrivate --excludeExternals --exclude **/std/** --includeDeclarations --readme src/README.md", diff --git a/src/asconfig.json b/src/asconfig.json index 2335eca294..bfd1b68beb 100644 --- a/src/asconfig.json +++ b/src/asconfig.json @@ -14,20 +14,20 @@ "untouched": { "binaryFile": "../out/assemblyscript.untouched.wasm", "textFile": "../out/assemblyscript.untouched.wast", - "tsdFile": "../out/assemblyscript.d.ts", + "tsdFile": "../out/assemblyscript.untouched.wasm.d.ts", "debug": true }, "optimized": { "binaryFile": "../out/assemblyscript.optimized.wasm", "textFile": "../out/assemblyscript.optimized.wast", - "tsdFile": "../out/assemblyscript.d.ts", + "tsdFile": "../out/assemblyscript.optimized.wasm.d.ts", "optimizeLevel": 3, "shrinkLevel": 0 }, "rtraced": { "binaryFile": "../out/assemblyscript.rtraced.wasm", "textFile": "../out/assemblyscript.rtraced.wast", - "tsdFile": "../out/assemblyscript.d.ts", + "tsdFile": "../out/assemblyscript.rtraced.wasm.d.ts", "debug": true, "use": "ASC_RTRACE=1", "runPasses": [] @@ -35,13 +35,13 @@ "untouched-bootstrap": { "binaryFile": "../out/assemblyscript.untouched-bootstrap.wasm", "textFile": "../out/assemblyscript.untouched-bootstrap.wast", - "tsdFile": "../out/assemblyscript.d.ts", + "tsdFile": "../out/assemblyscript.untouched-bootstrap.wasm.d.ts", "debug": true }, "optimized-bootstrap": { "binaryFile": "../out/assemblyscript.optimized-bootstrap.wasm", "textFile": "../out/assemblyscript.optimized-bootstrap.wast", - "tsdFile": "../out/assemblyscript.d.ts", + "tsdFile": "../out/assemblyscript.optimized-bootstrap.wasm.d.ts", "optimizeLevel": 3, "shrinkLevel": 0 } diff --git a/src/definitions.ts b/src/definitions.ts index 62b80efbf6..210d1ec3d0 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -604,33 +604,28 @@ export class TSDBuilder extends ExportsWalker { build(): string { var sb = this.sb; var isWasm64 = this.program.options.isWasm64; - sb.push("declare module ASModule {\n"); - sb.push(" type i8 = number;\n"); - sb.push(" type i16 = number;\n"); - sb.push(" type i32 = number;\n"); - sb.push(" type i64 = bigint;\n"); + sb.push("type i8 = number;\n"); + sb.push("type i16 = number;\n"); + sb.push("type i32 = number;\n"); + sb.push("type i64 = bigint;\n"); if (isWasm64) { - sb.push(" type isize = bigint;\n"); + sb.push("type isize = bigint;\n"); } else { - sb.push(" type isize = number;\n"); + sb.push("type isize = number;\n"); } - sb.push(" type u8 = number;\n"); - sb.push(" type u16 = number;\n"); - sb.push(" type u32 = number;\n"); - sb.push(" type u64 = bigint;\n"); + sb.push("type u8 = number;\n"); + sb.push("type u16 = number;\n"); + sb.push("type u32 = number;\n"); + sb.push("type u64 = bigint;\n"); if (isWasm64) { - sb.push(" type usize = bigint;\n"); + sb.push("type usize = bigint;\n"); } else { - sb.push(" type usize = number;\n"); + sb.push("type usize = number;\n"); } - sb.push(" type f32 = number;\n"); - sb.push(" type f64 = number;\n"); - sb.push(" type bool = boolean | number;\n"); - ++this.indentLevel; + sb.push("type f32 = number;\n"); + sb.push("type f64 = number;\n"); + sb.push("type bool = boolean | number;\n"); this.walk(); - --this.indentLevel; - sb.push("}\n"); - sb.push("export default ASModule;\n"); return this.sb.join(""); } } diff --git a/tests/imports/.gitignore b/tests/imports/.gitignore new file mode 100644 index 0000000000..eabe33af8e --- /dev/null +++ b/tests/imports/.gitignore @@ -0,0 +1,2 @@ +*.js +*.wasm* diff --git a/tests/imports/add.as b/tests/imports/add.as new file mode 100644 index 0000000000..f214778675 --- /dev/null +++ b/tests/imports/add.as @@ -0,0 +1,3 @@ +export function add(a: i32, b: i32): i32 { + return a + b +} diff --git a/tests/imports/dynamic-import.test.ts b/tests/imports/dynamic-import.test.ts new file mode 100644 index 0000000000..1d2d4038b8 --- /dev/null +++ b/tests/imports/dynamic-import.test.ts @@ -0,0 +1,10 @@ +(async () => { + const wasm = await import('./module.wasm'); + + const res = wasm.add(1, 2); + if (res !== 3) { + console.error('❌ WASM dynamic import does not work as expected'); + process.exit(1); + } + console.log('✔️ Can dynamically import wasm module with experimental flag'); +})(); diff --git a/tests/imports/package.json b/tests/imports/package.json new file mode 100644 index 0000000000..b81c7a946d --- /dev/null +++ b/tests/imports/package.json @@ -0,0 +1,9 @@ +{ + "type": "module", + "scripts": { + "build": "node ../../bin/asc --extension .as add.as -o module.wasm -d module.wasm.d.ts", + "test": "npm run build && npm run test:static && npm run test:dynamic", + "test:static": "tsc -m es2020 static-import.test.ts && node --experimental-wasm-modules static-import.test.js", + "test:dynamic": "tsc -m es2020 dynamic-import.test.ts && node --experimental-wasm-modules dynamic-import.test.js" + } +} diff --git a/tests/imports/static-import.test.ts b/tests/imports/static-import.test.ts new file mode 100644 index 0000000000..af87b7cc7d --- /dev/null +++ b/tests/imports/static-import.test.ts @@ -0,0 +1,9 @@ +import * as wasm from './module.wasm'; + +const res = wasm.add(1, 2); +if (res !== 3) { + console.error('❌ WASM static import does not work as expected'); + process.exit(1); +} + +console.log('✔️ Can statically import wasm module with experimental flag');