Allow receiving wasm as a function via wasm_fn (#629)

Allow receiving wasm as a function via wasm_fn (for bundlers)
This commit is contained in:
Giulio Zausa 2022-03-10 21:39:49 +01:00 committed by GitHub
parent d1a949d881
commit f42e204ed3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -154,75 +154,80 @@ function V86Starter(options)
"__indirect_function_table": wasm_table, "__indirect_function_table": wasm_table,
}; };
let v86_bin = DEBUG ? "v86-debug.wasm" : "v86.wasm"; let wasm_fn = options["wasm_fn"];
let v86_bin_fallback = "v86-fallback.wasm";
if(options["wasm_path"]) if(!wasm_fn)
{ {
v86_bin = options["wasm_path"]; wasm_fn = async function(env)
const slash = v86_bin.lastIndexOf("/");
const dir = slash === -1 ? "" : v86_bin.substr(0, slash);
v86_bin_fallback = dir + "/" + v86_bin_fallback;
}
else if(typeof window === "undefined" && typeof __dirname === "string")
{
v86_bin = __dirname + "/" + v86_bin;
v86_bin_fallback = __dirname + "/" + v86_bin_fallback;
}
else
{
v86_bin = "build/" + v86_bin;
v86_bin_fallback = "build/" + v86_bin_fallback;
}
v86util.load_file(v86_bin, {
done: bytes =>
{ {
WebAssembly return new Promise(resolve => {
.instantiate(bytes, { "env": wasm_shared_funcs }) let v86_bin = DEBUG ? "v86-debug.wasm" : "v86.wasm";
.then(({ instance }) => { let v86_bin_fallback = "v86-fallback.wasm";
const imports = wasm_shared_funcs;
const exports = instance["exports"];
wasm_memory = exports.memory;
exports["rust_init"]();
const emulator = this.v86 = new v86(this.emulator_bus, { exports, wasm_table }); if(options["wasm_path"])
cpu = emulator.cpu; {
v86_bin = options["wasm_path"];
const slash = v86_bin.lastIndexOf("/");
const dir = slash === -1 ? "" : v86_bin.substr(0, slash);
v86_bin_fallback = dir + "/" + v86_bin_fallback;
}
else if(typeof window === "undefined" && typeof __dirname === "string")
{
v86_bin = __dirname + "/" + v86_bin;
v86_bin_fallback = __dirname + "/" + v86_bin_fallback;
}
else
{
v86_bin = "build/" + v86_bin;
v86_bin_fallback = "build/" + v86_bin_fallback;
}
this.continue_init(emulator, options); v86util.load_file(v86_bin, {
}, err => { done: bytes =>
v86util.load_file(v86_bin_fallback, { {
done: bytes => { WebAssembly
WebAssembly .instantiate(bytes, env)
.instantiate(bytes, { "env": wasm_shared_funcs }) .then(({ instance }) => {
.then(({ instance }) => { resolve(instance.exports);
const imports = wasm_shared_funcs; }, err => {
const exports = instance["exports"]; v86util.load_file(v86_bin_fallback, {
wasm_memory = exports.memory; done: bytes => {
exports["rust_init"](); WebAssembly
.instantiate(bytes, env)
const emulator = this.v86 = new v86(this.emulator_bus, { exports, wasm_table }); .then(({ instance }) => {
cpu = emulator.cpu; resolve(instance.exports);
});
this.continue_init(emulator, options); },
}); });
}, });
}); },
}); progress: e =>
}, {
progress: e => this.emulator_bus.send("download-progress", {
{ file_index: 0,
this.emulator_bus.send("download-progress", { file_count: 1,
file_index: 0, file_name: v86_bin,
file_count: 1,
file_name: v86_bin,
lengthComputable: e.lengthComputable, lengthComputable: e.lengthComputable,
total: e.total, total: e.total,
loaded: e.loaded, loaded: e.loaded,
});
}
});
}); });
} };
}); }
wasm_fn({ "env": wasm_shared_funcs })
.then((exports) => {
wasm_memory = exports.memory;
exports["rust_init"]();
const emulator = this.v86 = new v86(this.emulator_bus, { exports, wasm_table });
cpu = emulator.cpu;
this.continue_init(emulator, options);
});
} }
V86Starter.prototype.continue_init = async function(emulator, options) V86Starter.prototype.continue_init = async function(emulator, options)