Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
},
"tasks": {
"fmt": "deno fmt && cargo fmt",
"build": "deno run -A ./main.ts -p wasmbuild",
"build:bindgen-upgrade": "WASMBUILD_BINDGEN_UPGRADE=1 deno task build",
"build": "WASMBUILD_BINDGEN_UPGRADE=1 deno run -A ./main.ts -p wasmbuild",
"build:lkg": "deno run -A jsr:@deno/wasmbuild@^0.15.4 -p wasmbuild",
"test": "cd tests && rm -rf lib lib_out_js_file lib_inline lib_no_cache && deno task test:main && deno task test:js-ext && deno task test:inline && deno test -A && deno task test:check",
"test": "cd tests && rm -rf lib lib_out_js_file lib_inline lib_no_cache && deno task test:main && deno task test:js-ext && deno task test:inline && deno test -A && deno task test:check && deno task test:start",
"test:main": "cd tests && deno run -A ../main.ts -p deno_test",
"test:js-ext": "deno task test:main --js-ext mjs --out lib_out_js_file && cat tests/lib_out_js_file/deno_test.mjs > /dev/null",
"test:check": "deno task test:main --check",
"test:inline": "deno task test:main --inline --out lib_inline"
"test:inline": "deno task test:main --inline --out lib_inline",
"test:start": "cd tests && deno run -A ../main.ts -p deno_test --features start && deno test -A test.ts"
},
"imports": {
"@david/path": "jsr:@david/path@^0.2.0",
Expand Down
2 changes: 2 additions & 0 deletions lib/bindgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface BindgenOutput {
ts: BindgenTextFileOutput;
snippets: Map<string, string[]>;
localModules: Map<string, string>;
start: string | undefined;
wasm: {
name: string;
bytes: number[];
Expand Down Expand Up @@ -72,6 +73,7 @@ async function generateForSelfBuild(filePath: Path): Promise<BindgenOutput> {
},
localModules: new Map(),
snippets: new Map(),
start: undefined,
wasm: {
name: "wasmbuild_bg.wasm",
bytes: Array.from(wasmBytes),
Expand Down
1 change: 1 addition & 0 deletions lib/commands/build_command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ import * as wasm from "./${output.wasmFileName}";
export * from "./${output.bindingJsBg.path.basename()}";
import { __wbg_set_wasm } from "./${output.bindingJsBg.path.basename()}";
__wbg_set_wasm(wasm);
${output.hasStart ? "wasm.__wbindgen_start();" : ""}
`),
}, {
path: output.bindingJsBg.path,
Expand Down
4 changes: 4 additions & 0 deletions lib/pre_build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export interface PreBuildOutput {
path: Path;
text: string;
};
/// If the wasm module has a #[wasm_bindgen(start)] attribute
hasStart: boolean;
sourceHash: string;
wasmFileName: string;
}
Expand Down Expand Up @@ -146,6 +148,8 @@ export async function runPreBuild(

${await getFormattedText(getLibraryDts(bindgenOutput))}`,
},
hasStart: bindgenOutput.start != null &&
bindgenOutput.start.includes("__wbindgen_start"),
sourceHash,
wasmFileName: bindgenOutput.wasm.name,
};
Expand Down
3 changes: 2 additions & 1 deletion lib/wasmbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
// deno-lint-ignore-file
// deno-fmt-ignore-file
// @ts-self-types="./wasmbuild.d.ts"
// source-hash: 42abdfbeb5cf210c856f23fe256f0194e6faf8d0

// source-hash: 83d37b1995d7d2a25021c644fccb57e2aa5331d6
import * as wasm from "./wasmbuild_bg.wasm";
export * from "./wasmbuild_bg.js";
import { __wbg_set_wasm } from "./wasmbuild_bg.js";
__wbg_set_wasm(wasm);

Binary file modified lib/wasmbuild_bg.wasm
Binary file not shown.
2 changes: 2 additions & 0 deletions rs_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub struct Output {
pub ts: Option<BindgenTextFileOutput>,
pub snippets: HashMap<String, Vec<String>>,
pub local_modules: HashMap<String, String>,
pub start: Option<String>,
pub wasm: BindgenBytesFileOutput,
}

Expand Down Expand Up @@ -85,6 +86,7 @@ fn inner(name: &str, ext: &str, wasm_bytes: Vec<u8>) -> Result<Output> {
},
snippets: x.snippets().clone(),
local_modules: x.local_modules().clone(),
start: x.start().cloned(),
wasm: BindgenBytesFileOutput {
name: format!("{}.wasm", name),
bytes: x.wasm_mut().emit_wasm(),
Expand Down
3 changes: 3 additions & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ edition = "2021"
authors = ["the Deno authors"]
license = "MIT"

[features]
start = []

[dependencies]
wasm-bindgen = "0.2.90"

Expand Down
12 changes: 11 additions & 1 deletion tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
use std::sync::Mutex;

use wasm_bindgen::prelude::*;

static VALUE: Mutex<u32> = Mutex::new(0);

#[wasm_bindgen(module = "/add.js")]
extern "C" {
fn add(a: u32, b: u32) -> u32;
}

#[wasm_bindgen]
pub fn greet(name: &str) -> String {
let result = add(1, 2);
let result = add(1, 2) + *VALUE.lock().unwrap();
format!("Hello, {}! Result: {}", name, result)
}

#[cfg(feature = "start")]
#[wasm_bindgen(start)]
pub fn main_js() {
*VALUE.lock().unwrap() = 1;
}
9 changes: 8 additions & 1 deletion tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import * as wasm from "./lib/deno_test.js";
import * as wasm2 from "./lib_inline/deno_test.js";

Deno.test("test works export", () => {
assertEquals(wasm.greet("Deno"), "Hello, Deno! Result: 3");
function hasStart() {
return Deno.readTextFileSync(import.meta.dirname + "/lib/deno_test.js")
.includes("__wbindgen_start");
}
assertEquals(
wasm.greet("Deno"),
hasStart() ? "Hello, Deno! Result: 4" : "Hello, Deno! Result: 3",
);
});

Deno.test("test inline works", () => {
Expand Down
Loading