diff --git a/README.md b/README.md index 18f4289..e5d00da 100644 --- a/README.md +++ b/README.md @@ -65,12 +65,12 @@ The _other_ reason is that you _should_ be using `WebAssembly.compile`, `WebAsse ## Contributing -If you want to contribute a new feature test, all you need to do is create a new folder in `src/detectors` and it will be automatically picked up. The folder must contain a `module.wat` file, which will be compiled using [`wat2wasm`][wat2wasm]. +If you want to contribute a new feature test, all you need to do is create a new folder in `src/detectors` and it will be automatically picked up. The folder must contain a `module.wat` file, which will be compiled using [`wabt.js`](https://github.com/AssemblyScript/wabt.js). ```wat ;; Name: ;; Proposal: -;; Flags: +;; Features: (module ;; More WAT code here diff --git a/package-lock.json b/package-lock.json index b58889c..4a79b67 100644 --- a/package-lock.json +++ b/package-lock.json @@ -302,6 +302,12 @@ "resolved": "https://registry.npmjs.org/uc.micro/-/uc.micro-1.0.6.tgz", "integrity": "sha512-8Y75pvTYkLJW2hWQHXxoqRgV7qb9B+9vFEtidML+7koHUFapnVJAZ6cKs+Qjz5Aw3aZWHMC6u0wJE3At+nSGwA==", "dev": true + }, + "wabt": { + "version": "1.0.13-nightly.20200430", + "resolved": "https://registry.npmjs.org/wabt/-/wabt-1.0.13-nightly.20200430.tgz", + "integrity": "sha512-UP5GBb8k13w6vrIYv1y5ztOUzsiWUjysjFkJR6bQ6yaP3EJcS63Dss2a/zLRoYddECOJRMtErwrw8TyS8gcapA==", + "dev": true } } } diff --git a/package.json b/package.json index 012bdfd..db98f60 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "markdown-it": "^10.0.0", "prettier": "^1.18.2", "rollup": "^1.20.3", - "rollup-plugin-terser": "^5.1.1" + "rollup-plugin-terser": "^5.1.1", + "wabt": "^1.0.13-nightly.20200430" } } diff --git a/rollup-plugins/helpers.mjs b/rollup-plugins/helpers.mjs index 03d2c92..236a137 100644 --- a/rollup-plugins/helpers.mjs +++ b/rollup-plugins/helpers.mjs @@ -11,20 +11,19 @@ * limitations under the License. */ -import { exec } from "child_process"; -import { promisify } from "util"; +import initWabt from "wabt"; import { promises as fsp } from "fs"; -const execP = promisify(exec); +const wabt = initWabt(); -export async function compileWat(watPath, flags = []) { - const { stdout } = await execP(`wat2wasm -d ${flags.join(" ")} ${watPath}`); - const hex = stdout - .split("\n") - .filter(line => line.length > 0) - .map(line => line.split(":")[1].replace(/\s+/g, "")) - .join(""); - return Buffer.from(hex, "hex"); +export async function compileWat(watPath, features = []) { + const watSource = await fsp.readFile(watPath, "utf-8"); + const module = wabt.parseWat( + watPath, + watSource, + Object.fromEntries(features.map(flag => [flag, true])) + ); + return module.toBinary({ canonicalize_lebs: true }).buffer; } export async function fileExists(path) { diff --git a/rollup-plugins/index-generator.js b/rollup-plugins/index-generator.js index 227dc26..dc15670 100644 --- a/rollup-plugins/index-generator.js +++ b/rollup-plugins/index-generator.js @@ -37,14 +37,14 @@ export default function({ indexPath, pluginFolder, format }) { `./src/${pluginFolder}/${plugin}/module.wat`, "utf8" ); - const flags = (/;;\s*Flags:\s*(.+)$/im.exec(source) || [ + const features = (/;;\s*Features:\s*(.+)$/im.exec(source) || [ "", "" ])[1].split(" "); const moduleBytes = JSON.stringify([ ...(await compileWat( `./src/${pluginFolder}/${plugin}/module.wat`, - flags + features )) ]); const pluginName = camelCaseify(plugin); diff --git a/src/detectors/bulk-memory/module.wat b/src/detectors/bulk-memory/module.wat index 3d6b6a0..d1404c0 100644 --- a/src/detectors/bulk-memory/module.wat +++ b/src/detectors/bulk-memory/module.wat @@ -1,6 +1,6 @@ ;; Name: Bulk memory operations ;; Proposal: https://github.com/webassembly/bulk-memory-operations -;; Flags: --enable-bulk-memory +;; Features: bulk_memory (module (memory 1) diff --git a/src/detectors/exceptions/module.wat b/src/detectors/exceptions/module.wat index e0fa2c3..88ec3eb 100644 --- a/src/detectors/exceptions/module.wat +++ b/src/detectors/exceptions/module.wat @@ -1,6 +1,6 @@ ;; Name: Exception handling ;; Proposal: https://github.com/WebAssembly/exception-handling -;; Flags: --enable-exceptions +;; Features: exceptions (module (func diff --git a/src/detectors/multi-value/module.wat b/src/detectors/multi-value/module.wat index ab72541..196c5aa 100644 --- a/src/detectors/multi-value/module.wat +++ b/src/detectors/multi-value/module.wat @@ -1,6 +1,6 @@ ;; Name: Multi-value ;; Proposal: https://github.com/WebAssembly/multi-value -;; Flags: --enable-multi-value +;; Features: multi_value (module (func (result i32 i32) diff --git a/src/detectors/reference-types/module.wat b/src/detectors/reference-types/module.wat index 9d9ec0c..4d5dcb8 100644 --- a/src/detectors/reference-types/module.wat +++ b/src/detectors/reference-types/module.wat @@ -1,6 +1,6 @@ ;; Name: Reference Types ;; Proposal: https://github.com/WebAssembly/reference-types -;; Flags: --enable-reference-types +;; Features: reference_types (module (func diff --git a/src/detectors/saturated-float-to-int/module.wat b/src/detectors/saturated-float-to-int/module.wat index 7dd2fc3..9a558ee 100644 --- a/src/detectors/saturated-float-to-int/module.wat +++ b/src/detectors/saturated-float-to-int/module.wat @@ -1,6 +1,6 @@ ;; Name: Non-trapping float-to-int conversions ;; Proposal: https://github.com/WebAssembly/nontrapping-float-to-int-conversions -;; Flags: --enable-saturating-float-to-int +;; Features: sat_float_to_int (module (func diff --git a/src/detectors/sign-extensions/module.wat b/src/detectors/sign-extensions/module.wat index 043fc1b..f07b340 100644 --- a/src/detectors/sign-extensions/module.wat +++ b/src/detectors/sign-extensions/module.wat @@ -1,6 +1,6 @@ ;; Name: Sign-extension operators ;; Proposal: https://github.com/WebAssembly/sign-extension-ops -;; Flags: --enable-sign-extension +;; Features: sign_extension (module (func diff --git a/src/detectors/simd/module.wat b/src/detectors/simd/module.wat index fe18147..e34985d 100644 --- a/src/detectors/simd/module.wat +++ b/src/detectors/simd/module.wat @@ -1,6 +1,6 @@ ;; Name: Fixed-Width SIMD ;; Proposal: https://github.com/webassembly/simd -;; Flags: --enable-simd +;; Features: simd (module (func diff --git a/src/detectors/tail-call/module.wat b/src/detectors/tail-call/module.wat index 6611f13..529ede4 100644 --- a/src/detectors/tail-call/module.wat +++ b/src/detectors/tail-call/module.wat @@ -1,6 +1,6 @@ ;; Name: Tail call ;; Proposal: https://github.com/webassembly/tail-call -;; Flags: --enable-tail-call +;; Features: tail_call (module (func diff --git a/src/detectors/threads/module.wat b/src/detectors/threads/module.wat index 1423133..ec3cd8d 100644 --- a/src/detectors/threads/module.wat +++ b/src/detectors/threads/module.wat @@ -1,6 +1,6 @@ ;; Name: Threads ;; Proposal: https://github.com/webassembly/threads -;; Flags: --enable-threads +;; Features: threads (module (memory 1 1 shared)