Skip to content

Commit

Permalink
Merge pull request #285 from wasmerio/public-instance
Browse files Browse the repository at this point in the history
Return the raw instance when instantiating. Fix #284
  • Loading branch information
syrusakbary authored Dec 14, 2021
2 parents 88e57c9 + 9538f36 commit 17c4ff3
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class WASI {
constructor(config: any);
readonly fs: MemFS;

instantiate(module: any, imports: object): void;
instantiate(module: any, imports: object): WebAssembly.Instance;
// Start the WASI Instance, it returns the status code when calling the start
// function
start(): number;
Expand Down
5 changes: 3 additions & 2 deletions pkg/wasmer_wasi_js.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ export class WASI {
/**
* @param {any} module
* @param {object} imports
* @returns {WebAssembly.Instance}
*/
instantiate(module: any, imports: object): void;
instantiate(module: any, imports: object): WebAssembly.Instance;
/**
* Start the WASI Instance, it returns the status code when calling the start
* function
Expand Down Expand Up @@ -186,7 +187,7 @@ export interface InitOutput {
readonly __wbg_wasi_free: (a: number) => void;
readonly wasi_new: (a: number) => number;
readonly wasi_fs: (a: number) => number;
readonly wasi_instantiate: (a: number, b: number, c: number) => void;
readonly wasi_instantiate: (a: number, b: number, c: number) => number;
readonly wasi_start: (a: number) => number;
readonly wasi_getStdoutBuffer: (a: number, b: number) => void;
readonly wasi_getStdoutString: (a: number, b: number) => void;
Expand Down
4 changes: 3 additions & 1 deletion pkg/wasmer_wasi_js.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,9 +511,11 @@ export class WASI {
/**
* @param {any} module
* @param {object} imports
* @returns {WebAssembly.Instance}
*/
instantiate(module, imports) {
wasm.wasi_instantiate(this.ptr, addHeapObject(module), addHeapObject(imports));
var ret = wasm.wasi_instantiate(this.ptr, addHeapObject(module), addHeapObject(imports));
return takeObject(ret);
}
/**
* Start the WASI Instance, it returns the status code when calling the start
Expand Down
Binary file modified pkg/wasmer_wasi_js_bg.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion pkg/wasmer_wasi_js_bg.wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function jsvirtualfile_seek(a: number, b: number): number;
export function __wbg_wasi_free(a: number): void;
export function wasi_new(a: number): number;
export function wasi_fs(a: number): number;
export function wasi_instantiate(a: number, b: number, c: number): void;
export function wasi_instantiate(a: number, b: number, c: number): number;
export function wasi_start(a: number): number;
export function wasi_getStdoutBuffer(a: number, b: number): void;
export function wasi_getStdoutString(a: number, b: number): void;
Expand Down
7 changes: 5 additions & 2 deletions src/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl WASI {
Ok(mem_fs.clone())
}

pub fn instantiate(&mut self, module: JsValue, imports: js_sys::Object) -> Result<(), JsValue> {
pub fn instantiate(&mut self, module: JsValue, imports: js_sys::Object) -> Result<js_sys::WebAssembly::Instance, JsValue> {
let module: js_sys::WebAssembly::Module = module.dyn_into().map_err(|_e| {
js_sys::Error::new(
"You must provide a module to the WASI new. `let module = new WASI({}, module);`",
Expand All @@ -137,8 +137,11 @@ impl WASI {

let instance = Instance::new(&module, &resolver)
.map_err(|e| js_sys::Error::new(&format!("Failed to instantiate WASI: {}`", e)))?;

let raw_instance = instance.raw().clone();
self.instantiated = Some(InstantiatedWASI { resolver, instance });
Ok(())

Ok(raw_instance)
}

/// Start the WASI Instance, it returns the status code when calling the start
Expand Down

0 comments on commit 17c4ff3

Please sign in to comment.