Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ jobs:
if: contains(matrix.os, 'ubuntu')
run: deno lint && cargo clippy

- name: Install wasm-bindgen-cli
run: cargo install wasm-bindgen-cli --version 0.2.100 --locked

- name: Check Wasm up-to-date
run: deno task build --check

Expand Down
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 && USES_START=1 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
4 changes: 2 additions & 2 deletions lib/utils/sha1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @license MIT
*/

export type Message = string | number[] | ArrayBuffer;
export type Message = string | number[] | ArrayBuffer | Uint8Array<ArrayBuffer>;

const HEX_CHARS = "0123456789abcdef".split("");
const EXTRA = [-2147483648, 8388608, 32768, 128] as const;
Expand Down Expand Up @@ -442,7 +442,7 @@ export class HmacSha1 extends Sha1 {
#oKeyPad: number[];
constructor(secretKey: Message, sharedMemory = false) {
super(sharedMemory);
let key: number[] | Uint8Array | undefined;
let key: number[] | Uint8Array<ArrayBuffer> | undefined;
if (typeof secretKey === "string") {
const bytes: number[] = [];
const length: number = secretKey.length;
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);

126 changes: 47 additions & 79 deletions lib/wasmbuild_bg.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ export function __wbg_set_wasm(val) {
wasm = val;
}

const heap = new Array(128).fill(undefined);

heap.push(undefined, null, true, false);

function getObject(idx) {
return heap[idx];
}

let WASM_VECTOR_LEN = 0;

let cachedUint8ArrayMemory0 = null;
Expand Down Expand Up @@ -100,17 +92,6 @@ function getDataViewMemory0() {
return cachedDataViewMemory0;
}

let heap_next = heap.length;

function addHeapObject(obj) {
if (heap_next === heap.length) heap.push(heap.length + 1);
const idx = heap_next;
heap_next = heap[idx];

heap[idx] = obj;
return idx;
}

const lTextDecoder = typeof TextDecoder === "undefined"
? (0, module.require)("util").TextDecoder
: TextDecoder;
Expand All @@ -129,62 +110,48 @@ function getStringFromWasm0(ptr, len) {
);
}

function dropObject(idx) {
if (idx < 132) return;
heap[idx] = heap_next;
heap_next = idx;
}

function takeObject(idx) {
const ret = getObject(idx);
dropObject(idx);
return ret;
}

function passArray8ToWasm0(arg, malloc) {
const ptr = malloc(arg.length * 1, 1) >>> 0;
getUint8ArrayMemory0().set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}

function takeFromExternrefTable0(idx) {
const value = wasm.__wbindgen_export_2.get(idx);
wasm.__externref_table_dealloc(idx);
return value;
}
/**
* @param {string} name
* @param {string} ext
* @param {Uint8Array} wasm_bytes
* @returns {any}
*/
export function generate_bindgen(name, ext, wasm_bytes) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passStringToWasm0(
name,
wasm.__wbindgen_malloc,
wasm.__wbindgen_realloc,
);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passStringToWasm0(
ext,
wasm.__wbindgen_malloc,
wasm.__wbindgen_realloc,
);
const len1 = WASM_VECTOR_LEN;
const ptr2 = passArray8ToWasm0(wasm_bytes, wasm.__wbindgen_malloc);
const len2 = WASM_VECTOR_LEN;
wasm.generate_bindgen(retptr, ptr0, len0, ptr1, len1, ptr2, len2);
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
if (r2) {
throw takeObject(r1);
}
return takeObject(r0);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
const ptr0 = passStringToWasm0(
name,
wasm.__wbindgen_malloc,
wasm.__wbindgen_realloc,
);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passStringToWasm0(
ext,
wasm.__wbindgen_malloc,
wasm.__wbindgen_realloc,
);
const len1 = WASM_VECTOR_LEN;
const ptr2 = passArray8ToWasm0(wasm_bytes, wasm.__wbindgen_malloc);
const len2 = WASM_VECTOR_LEN;
const ret = wasm.generate_bindgen(ptr0, len0, ptr1, len1, ptr2, len2);
if (ret[2]) {
throw takeFromExternrefTable0(ret[1]);
}
return takeFromExternrefTable0(ret[0]);
}

export function __wbg_String_8f0eb39a4a4c2f66(arg0, arg1) {
const ret = String(getObject(arg1));
const ret = String(arg1);
const ptr1 = passStringToWasm0(
ret,
wasm.__wbindgen_malloc,
Expand All @@ -197,64 +164,65 @@ export function __wbg_String_8f0eb39a4a4c2f66(arg0, arg1) {

export function __wbg_new_405e22f390576ce2() {
const ret = new Object();
return addHeapObject(ret);
return ret;
}

export function __wbg_new_5e0be73521bc8c17() {
const ret = new Map();
return addHeapObject(ret);
return ret;
}

export function __wbg_new_78feb108b6472713() {
const ret = new Array();
return addHeapObject(ret);
return ret;
}

export function __wbg_new_c68d7209be747379(arg0, arg1) {
const ret = new Error(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
return ret;
}

export function __wbg_set_37837023f3d740e8(arg0, arg1, arg2) {
getObject(arg0)[arg1 >>> 0] = takeObject(arg2);
arg0[arg1 >>> 0] = arg2;
}

export function __wbg_set_3f1d0b984ed272ed(arg0, arg1, arg2) {
getObject(arg0)[takeObject(arg1)] = takeObject(arg2);
arg0[arg1] = arg2;
}

export function __wbg_set_8fc6bf8a5b1071d1(arg0, arg1, arg2) {
const ret = getObject(arg0).set(getObject(arg1), getObject(arg2));
return addHeapObject(ret);
const ret = arg0.set(arg1, arg2);
return ret;
}

export function __wbindgen_error_new(arg0, arg1) {
const ret = new Error(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
return ret;
}

export function __wbindgen_init_externref_table() {
const table = wasm.__wbindgen_export_2;
const offset = table.grow(4);
table.set(0, undefined);
table.set(offset + 0, undefined);
table.set(offset + 1, null);
table.set(offset + 2, true);
table.set(offset + 3, false);
}

export function __wbindgen_is_string(arg0) {
const ret = typeof (getObject(arg0)) === "string";
const ret = typeof arg0 === "string";
return ret;
}

export function __wbindgen_number_new(arg0) {
const ret = arg0;
return addHeapObject(ret);
}

export function __wbindgen_object_clone_ref(arg0) {
const ret = getObject(arg0);
return addHeapObject(ret);
}

export function __wbindgen_object_drop_ref(arg0) {
takeObject(arg0);
return ret;
}

export function __wbindgen_string_new(arg0, arg1) {
const ret = getStringFromWasm0(arg0, arg1);
return addHeapObject(ret);
return ret;
}

export function __wbindgen_throw(arg0, arg1) {
Expand Down
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
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.77.0"
channel = "1.85.0"
components = [ "clippy", "rustfmt" ]
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;
}
7 changes: 6 additions & 1 deletion tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ 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");
assertEquals(
wasm.greet("Deno"),
Deno.env.get("USES_START")
? "Hello, Deno! Result: 4"
: "Hello, Deno! Result: 3",
);
});

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