Skip to content

Commit

Permalink
Add i64 to the set of JS-compatible wasm types in syncWebAssembly
Browse files Browse the repository at this point in the history
… mode

(Note: this fix is mainly meant for being backported to webpack 4, since `syncWebAssembly` is deprecated anyway, but I've implemented it against webpack 5 first.)

It's now supported in all major browsers, and is represented by `bigint` in JS.

There was an existing test that used `i64` as an example of a non-JS-compatible type; I replaced that with `v128`.

Ref rustwasm/wasm-bindgen#3095
  • Loading branch information
Liamolucko committed Oct 5, 2022
1 parent 9fcaa24 commit 7528146
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/wasm-sync/WebAssemblyParser.js
Expand Up @@ -17,7 +17,7 @@ const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDe
/** @typedef {import("../Parser").ParserState} ParserState */
/** @typedef {import("../Parser").PreparsedAst} PreparsedAst */

const JS_COMPAT_TYPES = new Set(["i32", "f32", "f64"]);
const JS_COMPAT_TYPES = new Set(["i32", "i64", "f32", "f64"]);

/**
* @param {t.Signature} signature the func signature
Expand Down
2 changes: 1 addition & 1 deletion test/cases/wasm/imports-complex-types/index.js
@@ -1,6 +1,6 @@
it("should allow to run a WebAssembly module with non-js-compatible imports", function() {
return import("./wasm.wasm").then(function(wasm) {
const result = wasm.testI64();
const result = wasm.testV128();
expect(result).toEqual(42);
});
});
Binary file modified test/cases/wasm/imports-complex-types/other.wasm
Binary file not shown.
11 changes: 9 additions & 2 deletions test/cases/wasm/imports-complex-types/test.filter.js
@@ -1,5 +1,12 @@
var supportsWebAssembly = require("../../../helpers/supportsWebAssembly");
const fs = require("fs");

module.exports = function(config) {
return supportsWebAssembly();
try {
// Compile the wasm module to test that the runtime supports v128 in the first place.
// Use synchronous instantation because this has to be synchronous.
new WebAssembly.Module(fs.readFileSync(`${__dirname}/wasm.wasm`));
return true;
} catch {
return false;
}
};
Binary file modified test/cases/wasm/imports-complex-types/wasm.wasm
Binary file not shown.
9 changes: 9 additions & 0 deletions test/configCases/wasm/bigints/index.js
@@ -0,0 +1,9 @@
import { getI64, takeI64 } from "./wasm.wat";

it("should allow converting i64s to JS bigints", function() {
expect(getI64()).toEqual(42n);
});

it("should allow converting JS bigints to i64s", function() {
takeI64(42n);
})
19 changes: 19 additions & 0 deletions test/configCases/wasm/bigints/test.filter.js
@@ -0,0 +1,19 @@
const fs = require("fs");

module.exports = function(config) {
try {
// Compile the wasm module to test that the runtime supports i64 in the first place.
// Use synchronous instantation because this has to be synchronous.
const module = new WebAssembly.Module(fs.readFileSync(`${__dirname}/wasm.wasm`));
const instance = new WebAssembly.Instance(module);
// Test that WebAssembly bigint integration is supported.
// Note that what we're testing isn't that the *runtime* supports it,
// it's that *webpack* supports it; the test relies on it already
// being supported by the runtime, so we check that here by calling a
// function that returns `i64`, outside of webpack.
instance.exports.getI64();
return true;
} catch {
return false;
}
};
4 changes: 4 additions & 0 deletions test/configCases/wasm/bigints/wasm.wat
@@ -0,0 +1,4 @@
(module
(func (export "getI64") (result i64)
i64.const 42)
(func (export "takeI64") (param i64)))
16 changes: 16 additions & 0 deletions test/configCases/wasm/bigints/webpack.config.js
@@ -0,0 +1,16 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
entry: "./index",
module: {
rules: [
{
test: /\.wat$/,
loader: "wast-loader",
type: "webassembly/sync"
}
]
},
experiments: {
syncWebAssembly: true
}
};

0 comments on commit 7528146

Please sign in to comment.