Skip to content

Commit

Permalink
Fixed tests. Improved formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Aug 30, 2022
1 parent 59e0d9f commit e5bc2fc
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 29 deletions.
48 changes: 28 additions & 20 deletions pkg/wasmer_wasi_js.js
Original file line number Diff line number Diff line change
Expand Up @@ -763,10 +763,6 @@ async function init(input) {
var ret = getObject(arg0).call(getObject(arg1));
return addHeapObject(ret);
}, arguments) };
imports.wbg.__wbg_instanceof_Function_07f0fd0979b11789 = function(arg0) {
var ret = getObject(arg0) instanceof Function;
return ret;
};
imports.wbg.__wbindgen_memory = function() {
var ret = wasm.memory;
return addHeapObject(ret);
Expand Down Expand Up @@ -794,18 +790,10 @@ async function init(input) {
var ret = getObject(arg0) instanceof WebAssembly.Module;
return ret;
};
imports.wbg.__wbg_instanceof_Table_7aa3c4bf4a3e48a4 = function(arg0) {
var ret = getObject(arg0) instanceof WebAssembly.Table;
return ret;
};
imports.wbg.__wbg_get_11af051ff97b40e3 = function() { return handleError(function (arg0, arg1) {
var ret = getObject(arg0).get(arg1 >>> 0);
return addHeapObject(ret);
}, arguments) };
imports.wbg.__wbg_instanceof_Memory_625744f21df3a5ec = function(arg0) {
var ret = getObject(arg0) instanceof WebAssembly.Memory;
return ret;
};
imports.wbg.__wbg_get_4d0f21c2f823742e = function() { return handleError(function (arg0, arg1) {
var ret = Reflect.get(getObject(arg0), getObject(arg1));
return addHeapObject(ret);
Expand Down Expand Up @@ -847,10 +835,6 @@ async function init(input) {
getInt32Memory0()[arg0 / 4 + 1] = len0;
getInt32Memory0()[arg0 / 4 + 0] = ptr0;
};
imports.wbg.__wbg_instanceof_Global_6f02d66d02a0b291 = function(arg0) {
var ret = getObject(arg0) instanceof WebAssembly.Global;
return ret;
};
imports.wbg.__wbg_wasmerruntimeerror_new = function(arg0) {
var ret = WasmerRuntimeError.__wrap(arg0);
return addHeapObject(ret);
Expand Down Expand Up @@ -925,6 +909,14 @@ async function init(input) {
var ret = new WebAssembly.Instance(getObject(arg0), getObject(arg1));
return addHeapObject(ret);
}, arguments) };
imports.wbg.__wbindgen_is_falsy = function(arg0) {
var ret = !getObject(arg0);
return ret;
};
imports.wbg.__wbg_instanceof_Instance_38f5bc5383db505e = function(arg0) {
var ret = getObject(arg0) instanceof WebAssembly.Instance;
return ret;
};
imports.wbg.__wbg_newwithlength_75ee2b96c288e6bc = function(arg0) {
var ret = new Array(arg0 >>> 0);
return addHeapObject(ret);
Expand All @@ -947,10 +939,6 @@ async function init(input) {
var ret = WebAssembly.Module.exports(getObject(arg0));
return addHeapObject(ret);
};
imports.wbg.__wbindgen_bigint_new = function(arg0, arg1) {
var ret = BigInt(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
};
imports.wbg.__wbg_new_949bbc1147195c4e = function() {
var ret = new Array();
return addHeapObject(ret);
Expand All @@ -964,10 +952,30 @@ async function init(input) {
var ret = typeof(v) === 'boolean' ? (v ? 1 : 0) : 2;
return ret;
};
imports.wbg.__wbindgen_bigint_new = function(arg0, arg1) {
var ret = BigInt(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
};
imports.wbg.__wbg_instanceof_Object_66786225e0dbc8ba = function(arg0) {
var ret = getObject(arg0) instanceof Object;
return ret;
};
imports.wbg.__wbg_instanceof_Table_7aa3c4bf4a3e48a4 = function(arg0) {
var ret = getObject(arg0) instanceof WebAssembly.Table;
return ret;
};
imports.wbg.__wbg_instanceof_Function_07f0fd0979b11789 = function(arg0) {
var ret = getObject(arg0) instanceof Function;
return ret;
};
imports.wbg.__wbg_instanceof_Memory_625744f21df3a5ec = function(arg0) {
var ret = getObject(arg0) instanceof WebAssembly.Memory;
return ret;
};
imports.wbg.__wbg_instanceof_Global_6f02d66d02a0b291 = function(arg0) {
var ret = getObject(arg0) instanceof WebAssembly.Global;
return ret;
};

if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) {
input = fetch(input);
Expand Down
Binary file modified pkg/wasmer_wasi_js_bg.wasm
Binary file not shown.
26 changes: 17 additions & 9 deletions src/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,7 @@ impl WASI {
Ok(import_object.as_jsobject(&self.store).into())
}

fn get_wasmer_imports(
&mut self,
module: &Module
) -> Result<Imports, JsValue> {
fn get_wasmer_imports(&mut self, module: &Module) -> Result<Imports, JsValue> {
let import_object = self
.wasi_env
.import_object(&mut self.store, module)
Expand All @@ -173,10 +170,9 @@ impl WASI {

let import_object = self.get_wasmer_imports(&module)?;
let mut custom_imports = if let Some(base_imports) = imports {
Imports::new_from_js_object(&mut self.store, &module, base_imports )
Imports::new_from_js_object(&mut self.store, &module, base_imports)
.map_err(|e| js_sys::Error::new(&format!("Failed to get user imports: {}", e)))?
}
else {
} else {
Imports::new()
};

Expand All @@ -194,9 +190,21 @@ impl WASI {
/// Start the WASI Instance, it returns the status code when calling the start
/// function
pub fn start(&mut self, instance: js_sys::WebAssembly::Instance) -> Result<u32, JsValue> {

if instance.is_falsy() {
return Err(js_sys::Error::new(
"`wasi.start` now receives the instance as the first argument (received none)",
)
.into());
}
let instance: js_sys::WebAssembly::Instance = instance.dyn_into().map_err(|e| {
js_sys::Error::new(&format!(
"`wasi.start` now receives the instance as the first argument. Received: {:?}",
e
))
})?;
let module = self.module.as_ref().unwrap();
let instance = Instance::from_module_and_instance(&mut self.store, module, instance).map_err(|e| js_sys::Error::new(&format!("Can't get the Wasmer Instance: {:?}", e)))?;
let instance = Instance::from_module_and_instance(&mut self.store, module, instance)
.map_err(|e| js_sys::Error::new(&format!("Can't get the Wasmer Instance: {:?}", e)))?;
self.wasi_env
.data_mut(&mut self.store)
.set_memory(instance.exports.get_memory("memory").unwrap().clone());
Expand Down
15 changes: 15 additions & 0 deletions tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ test('testing wasm', async() => {
console.log(`${stdout}(exit code: ${exitCode})`);
})

test('wasi start empty fails', async() => {
let moduleBytes = fs.readFileSync(__dirname + '/test.wasm');
let wasi = new WASI({});
const module = await WebAssembly.compile(moduleBytes);
let imports = wasi.get_imports(module);
// console.log(imports);
let instance = await WebAssembly.instantiate(module, {
...imports,
'module': {
'external': function() { console.log("external: hello world!") }
}
});
expect(() => wasi.start()).toThrow("`wasi.start` now receives the instance as the first argument (received none)");
});

test('get imports', async() => {
let moduleBytes = fs.readFileSync(__dirname + '/test.wasm');
let wasi = new WASI({});
Expand Down

0 comments on commit e5bc2fc

Please sign in to comment.