Skip to content

Fix wasm-bindgen patch #7970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 1, 2024
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
91 changes: 73 additions & 18 deletions rerun_js/web-viewer/build-wasm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const exec = (cmd) => {
child_process.execSync(cmd, { cwd: __dirname, stdio: "inherit" });
};

function wasm(mode) {
function buildWebViewer(mode) {
switch (mode) {
case "debug": {
return exec(
Expand All @@ -31,13 +31,9 @@ function wasm(mode) {
}
}

child_process.execSync(
"cargo run -p re_dev_tools -- build-web-viewer --debug --target no-modules-base -o rerun_js/web-viewer",
{ cwd: __dirname, stdio: "inherit" },
);
Comment on lines -34 to -37
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused by what these lines were doing previously.

Copy link
Member Author

@jprochazk jprochazk Nov 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was redundant, remainded here after I moved it to a function with copy instead of cut 😄

In practice it doubled build times, because everything was being built in both debug and release:


function script() {
async function re_viewer_js(mode) {
let code = fs.readFileSync(path.join(__dirname, "re_viewer.js"), "utf-8");
await checkHash(mode, "re_viewer.js", code);

// this transforms the module, wrapping it in a default-exported function.
// calling the function produces a new "instance" of the module, because
Expand All @@ -64,11 +60,10 @@ ${code}
function deinit() {
__wbg_init.__wbindgen_wasm_module = null;
wasm = null;
cachedFloat32Memory0 = null;
cachedFloat64Memory0 = null;
cachedInt32Memory0 = null;
cachedUint32Memory0 = null;
cachedUint8Memory0 = null;
cachedFloat32ArrayMemory0 = null;
cachedInt32ArrayMemory0 = null;
cachedUint32ArrayMemory0 = null;
cachedUint8ArrayMemory0 = null;
}

return Object.assign(__wbg_init, { initSync, deinit }, __exports);
Expand All @@ -94,8 +89,9 @@ return Object.assign(__wbg_init, { initSync, deinit }, __exports);
fs.writeFileSync(path.join(__dirname, "re_viewer.js"), code);
}

function types() {
async function re_viewer_d_ts(mode) {
let code = fs.readFileSync(path.join(__dirname, "re_viewer.d.ts"), "utf-8");
await checkHash(mode, "re_viewer.d.ts", code);

// this transformation just re-exports WebHandle and adds a default export inside the `.d.ts` file

Expand All @@ -108,18 +104,77 @@ export default function(): wasm_bindgen;
fs.writeFileSync(path.join(__dirname, "re_viewer.d.ts"), code);
}

async function hash(data) {
const buffer = await crypto.subtle.digest("sha-256", data);
return Array.from(new Uint8Array(buffer))
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
}

async function checkHash(mode, id, data) {
const storedHash = hashes?.[mode]?.[id];
const computedHash = await hash(new TextEncoder().encode(data));

if (updateHashes) {
hashes[mode] ??= {};
hashes[mode][id] = computedHash;
return;
}

if (storedHash !== computedHash) {
console.error(`
==============================================================
Output of "${id}" changed.
Update the \`build-wasm.mjs\` script to handle the new output,
then run \`pixi run node build-wasm.mjs --update-hashes\`.
==============================================================
`);
process.exit(1);
}
}

async function run(mode) {
buildWebViewer(mode);
await re_viewer_js(mode);
await re_viewer_d_ts(mode);
}

const args = util.parseArgs({
options: {
mode: {
type: "string",
},
"update-hashes": {
type: "boolean",
},
},
});

if (!args.values.mode) {
throw new Error("Missing required argument: mode");
let updateHashes = !!args.values["update-hashes"];
let hashes;
try {
hashes = JSON.parse(
fs.readFileSync(path.join(__dirname, "hashes.json"), "utf-8"),
);
} catch (e) {
hashes = {};
}

wasm(args.values.mode);
script();
types();
try {
if (updateHashes) {
await run("release");
await run("debug");
fs.writeFileSync(
path.join(__dirname, "hashes.json"),
JSON.stringify(hashes),
);
} else {
if (!args.values.mode) {
throw new Error("Missing required argument: mode");
}

await run(args.values.mode);
}
} catch (e) {
console.error(e.message);
}
1 change: 1 addition & 0 deletions rerun_js/web-viewer/hashes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"release":{"re_viewer.js":"9548c50002f6c6844e50ceb154641a4ca547413a54eb3214b18909848776492f","re_viewer.d.ts":"d533906cbd59ac7b2f4a962a24daad55c5df854343414b8b6db08e2d956fdee3"},"debug":{"re_viewer.js":"74ec126e7deeb831c72c7a2ccffb98c3c63fb7268f27feffdfb2da286db65816","re_viewer.d.ts":"c642a7f1990d414d9953f0b215d8114418502b3b9f0240ca81b039766d5236b0"}}
2 changes: 1 addition & 1 deletion rerun_js/web-viewer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@
"dts-buddy": "^0.3.0",
"typescript": "^5.2.2"
}
}
}
Loading