Skip to content

Commit

Permalink
feat: wasm text format and import support
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed Jul 28, 2023
1 parent 1c5e836 commit 9fee6e5
Show file tree
Hide file tree
Showing 15 changed files with 552 additions and 127 deletions.
153 changes: 94 additions & 59 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crates/turbopack-ecmascript/src/chunk/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
};

#[turbo_tasks::value(shared)]
#[derive(Default)]
#[derive(Default, Clone)]
pub struct EcmascriptChunkItemContent {
pub inner_code: Rope,
pub source_map: Option<Vc<ParseResultSourceMap>>,
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack-ecmascript/src/references/esm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use self::{
base::EsmAssetReference,
binding::EsmBinding,
dynamic::EsmAsyncAssetReference,
export::EsmExports,
export::{EsmExport, EsmExports},
meta::{ImportMetaBinding, ImportMetaRef},
module_item::EsmModuleItem,
url::UrlAssetReference,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Adapted from webpack
https://github.com/webpack/webpack/blob/6be4065ade1e252c1d8dcba4af0f43e32af1bdc1/examples/wasm-complex/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
describe("complex wasm", () => {
it("should be possible to use imported memory", async () => {
// magic.js is an async module, so we import it inside this function to make sure the entrypoint isn't async.
const { get, set } = await import("./magic.js");

set(42);
expect(get()).toEqual(42);
set(123);
expect(get()).toEqual(123);
});

it("should be possible to use imported functions", async () => {
// magic.js is an async module, so we import it inside this function to make sure the entrypoint isn't async.
const { getNumber } = await import("./magic.js");

// random numbers
expect(getNumber()).toBeGreaterThanOrEqual(0);
expect(getNumber()).toBeGreaterThanOrEqual(0);
});
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function getNumber() {
return 42;
}

export function getRandomNumber() {
return Math.floor(Math.random() * 256);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// reexporting
export * from "./magic.wat";
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(module
(type $t0 (func (result i32)))
(type $t1 (func (param i32)))
(import "./memory.js" "memory" (memory 1))
(import "./magic-number.js" "getRandomNumber" (func $getRandomNumber (type $t0)))
(func $get (export "get") (type $t0) (result i32)
(i32.load
(i32.const 0)))
(func $set (export "set") (type $t1) (param $p i32)
(i32.store
(i32.const 0)
(get_local $p)))
(func $getNumber (export "getNumber") (type $t0) (result i32)
(call $getRandomNumber))
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
async function getMemoryFromParentInWorker() {
await new Promise(r => setTimeout(r, 200));
// fake
return new WebAssembly.Memory({ initial: 1 });
}

export const memory = await getMemoryFromParentInWorker();
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
it("should handle wasm imports", async () => {
// math.js is an async module, so we import it in here
// magic.js is an async module, so we import it inside this function to make sure the entrypoint isn't async.
const {
add,
factorial,
Expand Down
5 changes: 3 additions & 2 deletions crates/turbopack-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ bench = false
[dependencies]
anyhow = { workspace = true }
indexmap = { workspace = true }

indoc = { workspace = true }
turbo-tasks = { workspace = true }
turbo-tasks-fs = { workspace = true }
turbo-tasks-hash = { workspace = true }
turbopack-core = { workspace = true }
turbopack-css = { workspace = true }
turbopack-ecmascript = { workspace = true }

serde = { workspace = true }
wat = "1.0.69"
wasmparser = "0.110.0"

[build-dependencies]
turbo-tasks-build = { workspace = true }
Loading

0 comments on commit 9fee6e5

Please sign in to comment.