Skip to content

Commit

Permalink
Make all tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Nov 19, 2021
1 parent bf00785 commit 9653a10
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ crate-type = ["cdylib"]
[dependencies]
js-sys = "0.3.55"
wasm-bindgen = "0.2.73"
wasmer = { path = "../wasmer/lib/api", default-features = false, features = ["js", "std"] }
wasmer = { path = "../wasmer/lib/api", default-features = false, features = ["js-default"] }
wasmer-wasi = { path = "../wasmer/lib/wasi", default-features = false, features = ["js"] }
21 changes: 16 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,23 @@ impl WASI {
let mut wasi_env = WasiState::new(&args.get(0).unwrap_or(&"".to_string()))
.args(if args.len() > 0 { &args[1..] } else { &[] })
.envs(env)
.finalize()
.unwrap();
.finalize().map_err(|e| {
js_sys::Error::new(
&format!("Failed to create the WasiState: {}`", e)
)
})?;

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);`",
)
})?;
let module: Module = module.into();
let import_object = wasi_env.import_object(&module).unwrap();
let import_object = wasi_env.import_object(&module).map_err(|e| {
js_sys::Error::new(
&format!("Failed to create the Import Object: {}`", e)
)
})?;

Ok(WASI {
wasi_env,
Expand All @@ -76,15 +83,19 @@ impl WASI {
})
}

async pub fn instantiate(&mut self, imports: js_sys::Object) -> Result<(), JsValue> {
unimplemented!()
}

#[wasm_bindgen(js_name = getImports)]
pub fn get_imports(&mut self) -> js_sys::Object {
self.import_object.clone().into()
}

/// Start the WASI Instance, it returns the status code when calling the start
/// function
pub fn start(&self, instance: js_sys::WebAssembly::Instance) -> Result<u8, JsValue> {
let instance = Instance::from_module_and_instance(&self.module, instance);
pub fn start(&self, instance: js_sys::WebAssembly::Instance) -> Result<u32, JsValue> {
let instance = Instance::from_module_and_instance(&self.module, instance).map_err(|e| js_sys::Error::new(&format!("Error while creating the Wasmer instance: {}", e)))?;
let externs = self
.import_object
.clone()
Expand Down
37 changes: 21 additions & 16 deletions tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,35 @@ const fs = require('fs');
const { init, WASI } = require('../dist/Library.cjs.js');

async function doWasi(moduleBytes, config) {
await init();
const module = await WebAssembly.compile(moduleBytes);
let wasi = new WASI(config, module);
let imports = wasi.getImports();
console.log(imports);
let instance = WebAssembly.instantiate(module, imports);
let instance = await WebAssembly.instantiate(module, imports);
wasi.start(instance);
return wasi;
}

// test('envvar works', async () => {
// await init();
// let wasi = await doWasi(fs.readFileSync(__dirname + '/envvar.wasm'), {
// env: {
// DOG: "X",
// TEST: "VALUE",
// TEST2: "VALUE2"
// }
// });
// expect(wasi.getStdoutString()).toBe("a");
// });
test('envvar works', async () => {
let wasi = await doWasi(fs.readFileSync(__dirname + '/envvar.wasm'), {
env: {
DOG: "X",
TEST: "VALUE",
TEST2: "VALUE2"
}
});
expect(wasi.getStdoutString()).toBe(`Env vars:
DOG=X
TEST2=VALUE2
TEST=VALUE
DOG Ok("X")
DOG_TYPE Err(NotPresent)
SET VAR Ok("HELLO")
`);
});

test('demo works', async () => {
await init();
let contents = fs.readFileSync(__dirname + '/demo.wasm');
console.log(contents);
let wasi = await doWasi(contents, {});
expect(wasi.getStdoutString()).toBe("a");
expect(wasi.getStdoutString()).toBe("hello world\n");
});

0 comments on commit 9653a10

Please sign in to comment.