Files
hello_js_reverse_skill/templates/wasm-loader/utils/env-patch.js
T
白夜 fb14cf7916 refactor: 将文件从子目录提升到仓库根目录
所有文件从 hello_js_reverse_skill/ 子目录移至仓库根目录,
使项目结构更清晰,便于作为 Skill 直接引用。

Made-with: Cursor
2026-03-18 22:55:27 +08:00

98 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* WASM 环境补全模块
* 为不同类型的 WASM(Emscripten、wasm-bindgen 等)提供最小运行环境
*/
/**
* 补全 wasm-bindgen (Rust) 生成的 WASM 所需的环境
* 常见于 Rust 编写的加密模块
*/
function patchWasmBindgen() {
class Window {
constructor() {
this.document = {
body: {},
head: {},
createElement: () => ({}),
getElementById: () => null,
querySelector: () => null,
};
}
}
const win = new Window();
win.window = win;
win.self = win;
win.globalThis = win;
globalThis.Window = Window;
globalThis.window = win;
globalThis.self = win;
globalThis.document = win.document;
// wasm-bindgen 可能使用 TextEncoder/TextDecoder
if (typeof TextEncoder === 'undefined') {
const { TextEncoder, TextDecoder } = require('util');
globalThis.TextEncoder = TextEncoder;
globalThis.TextDecoder = TextDecoder;
}
return win;
}
/**
* 补全 Emscripten 生成的 WASM 所需的环境
*/
function patchEmscripten() {
return {
print: console.log,
printErr: console.error,
TOTAL_MEMORY: 16777216,
TOTAL_STACK: 5242880,
noInitialRun: true,
noExitRuntime: true,
onRuntimeInitialized: null,
preRun: [],
postRun: [],
};
}
/**
* 补全 Go WASM 所需的环境
*/
function patchGoWasm() {
globalThis.process = globalThis.process || { argv: ['node'], env: {} };
globalThis.fs = globalThis.fs || require('fs');
// Go WASM 需要 performance.now
if (typeof performance === 'undefined') {
globalThis.performance = {
now: () => Date.now(),
};
}
// Go WASM 需要 crypto.getRandomValues
if (typeof globalThis.crypto === 'undefined') {
const nodeCrypto = require('crypto');
globalThis.crypto = {
getRandomValues: (arr) => {
const bytes = nodeCrypto.randomBytes(arr.length);
arr.set(bytes);
return arr;
},
};
}
}
/**
* 清理环境补丁(测试完成后调用)
*/
function cleanupPatches() {
delete globalThis.Window;
delete globalThis.window;
delete globalThis.self;
delete globalThis.document;
}
module.exports = { patchWasmBindgen, patchEmscripten, patchGoWasm, cleanupPatches };