Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to wabt.js #23

Merged
merged 1 commit into from May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -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: <Name of the feature for the README>
;; Proposal: <Link to the proposal’s explainer/repo>
;; Flags: <CLI flags for `wat2wasm`>
;; Features: <Space-separated list of WasmFeatures from wabt.js>

(module
;; More WAT code here
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -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"
}
}
21 changes: 10 additions & 11 deletions rollup-plugins/helpers.mjs
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions rollup-plugins/index-generator.js
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion 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)
Expand Down
2 changes: 1 addition & 1 deletion 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
Expand Down
2 changes: 1 addition & 1 deletion 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)
Expand Down
2 changes: 1 addition & 1 deletion 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
Expand Down
2 changes: 1 addition & 1 deletion 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
Expand Down
2 changes: 1 addition & 1 deletion 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
Expand Down
2 changes: 1 addition & 1 deletion 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
Expand Down
2 changes: 1 addition & 1 deletion 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
Expand Down
2 changes: 1 addition & 1 deletion 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)
Expand Down