-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vm): support wasm module (#5131)
- Loading branch information
Showing
12 changed files
with
205 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ export default antfu( | |
'**/bench.json', | ||
'**/fixtures', | ||
'test/core/src/self', | ||
'test/core/src/wasm-bindgen-no-cyclic', | ||
'test/workspaces/results.json', | ||
'test/reporters/fixtures/with-syntax-error.test.js', | ||
'test/network-imports/public/[email protected]', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
The recent version of the wasm-bindgen bundler output does not use cyclic imports between wasm and js. | ||
|
||
For this non-cyclic version to work, both `index_bg.js` and `index_bg.wasm` need to be externalized | ||
since otherwise a dual package hazard on `index_bg.js` would make it non-functional. | ||
|
||
The code is copied from https://github.com/rustwasm/wasm-bindgen/tree/8198d2d25920e1f4fc593e9f8eb9d199e004d731/examples/hello_world | ||
|
||
```sh | ||
npm i | ||
npm run build | ||
# then | ||
# 1. copy `examples/hello_world/pkg` to this directory | ||
# 2. add { "type": "module" } to `package.json` | ||
# (this will be automatically included after https://github.com/rustwasm/wasm-pack/pull/1061) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* tslint:disable */ | ||
/* eslint-disable */ | ||
/** | ||
* @param {string} name | ||
*/ | ||
export function greet(name: string): void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import * as wasm from "./index_bg.wasm"; | ||
import { __wbg_set_wasm } from "./index_bg.js"; | ||
__wbg_set_wasm(wasm); | ||
export * from "./index_bg.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
let wasm; | ||
export function __wbg_set_wasm(val) { | ||
wasm = val; | ||
} | ||
|
||
|
||
const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder; | ||
|
||
let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true }); | ||
|
||
cachedTextDecoder.decode(); | ||
|
||
let cachedUint8Memory0 = null; | ||
|
||
function getUint8Memory0() { | ||
if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) { | ||
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer); | ||
} | ||
return cachedUint8Memory0; | ||
} | ||
|
||
function getStringFromWasm0(ptr, len) { | ||
ptr = ptr >>> 0; | ||
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len)); | ||
} | ||
|
||
function logError(f, args) { | ||
try { | ||
return f.apply(this, args); | ||
} catch (e) { | ||
let error = (function () { | ||
try { | ||
return e instanceof Error ? `${e.message}\n\nStack:\n${e.stack}` : e.toString(); | ||
} catch(_) { | ||
return "<failed to stringify thrown value>"; | ||
} | ||
}()); | ||
console.error("wasm-bindgen: imported JS function that was not marked as `catch` threw an error:", error); | ||
throw e; | ||
} | ||
} | ||
|
||
let WASM_VECTOR_LEN = 0; | ||
|
||
const lTextEncoder = typeof TextEncoder === 'undefined' ? (0, module.require)('util').TextEncoder : TextEncoder; | ||
|
||
let cachedTextEncoder = new lTextEncoder('utf-8'); | ||
|
||
const encodeString = (typeof cachedTextEncoder.encodeInto === 'function' | ||
? function (arg, view) { | ||
return cachedTextEncoder.encodeInto(arg, view); | ||
} | ||
: function (arg, view) { | ||
const buf = cachedTextEncoder.encode(arg); | ||
view.set(buf); | ||
return { | ||
read: arg.length, | ||
written: buf.length | ||
}; | ||
}); | ||
|
||
function passStringToWasm0(arg, malloc, realloc) { | ||
|
||
if (typeof(arg) !== 'string') throw new Error('expected a string argument'); | ||
|
||
if (realloc === undefined) { | ||
const buf = cachedTextEncoder.encode(arg); | ||
const ptr = malloc(buf.length, 1) >>> 0; | ||
getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf); | ||
WASM_VECTOR_LEN = buf.length; | ||
return ptr; | ||
} | ||
|
||
let len = arg.length; | ||
let ptr = malloc(len, 1) >>> 0; | ||
|
||
const mem = getUint8Memory0(); | ||
|
||
let offset = 0; | ||
|
||
for (; offset < len; offset++) { | ||
const code = arg.charCodeAt(offset); | ||
if (code > 0x7F) break; | ||
mem[ptr + offset] = code; | ||
} | ||
|
||
if (offset !== len) { | ||
if (offset !== 0) { | ||
arg = arg.slice(offset); | ||
} | ||
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0; | ||
const view = getUint8Memory0().subarray(ptr + offset, ptr + len); | ||
const ret = encodeString(arg, view); | ||
if (ret.read !== arg.length) throw new Error('failed to pass whole string'); | ||
offset += ret.written; | ||
} | ||
|
||
WASM_VECTOR_LEN = offset; | ||
return ptr; | ||
} | ||
/** | ||
* @param {string} name | ||
*/ | ||
export function greet(name) { | ||
const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); | ||
const len0 = WASM_VECTOR_LEN; | ||
wasm.greet(ptr0, len0); | ||
} | ||
|
||
export function __wbg_alert_9ea5a791b0d4c7a3() { return logError(function (arg0, arg1) { | ||
alert(getStringFromWasm0(arg0, arg1)); | ||
}, arguments) }; | ||
|
||
export function __wbindgen_throw(arg0, arg1) { | ||
throw new Error(getStringFromWasm0(arg0, arg1)); | ||
}; | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* tslint:disable */ | ||
/* eslint-disable */ | ||
export const memory: WebAssembly.Memory; | ||
export function greet(a: number, b: number): void; | ||
export function __wbindgen_malloc(a: number, b: number): number; | ||
export function __wbindgen_realloc(a: number, b: number, c: number, d: number): number; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"type": "module", | ||
"name": "hello_world", | ||
"collaborators": [ | ||
"The wasm-bindgen Developers" | ||
], | ||
"version": "0.1.0", | ||
"files": [ | ||
"index_bg.wasm", | ||
"index.js", | ||
"index_bg.js", | ||
"index.d.ts" | ||
], | ||
"module": "index.js", | ||
"types": "index.d.ts", | ||
"sideEffects": [ | ||
"./index.js", | ||
"./snippets/*" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters