Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runtime) Cast and check CLI arguments to pass to the instance #281

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 107 additions & 4 deletions src/webassembly.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use std::panic;
use wasmer_runtime::{
self as runtime,
error::{CallResult, Result},
error::{CallError, CallResult, ResolveError, Result},
ImportObject, Instance, Module,
};
use wasmer_runtime_core::{
export::Export,
types::{Type, Value},
};

use wasmer_emscripten::{is_emscripten_module, run_emscripten_instance};

Expand Down Expand Up @@ -85,9 +89,108 @@ pub fn run_instance(
) -> CallResult<()> {
if is_emscripten_module(module) {
run_emscripten_instance(module, instance, path, args)?;

Ok(())
} else {
instance.call("main", &[])?;
};
instance
.exports()
.find_map(
|(export_name, export)| match (export_name.as_ref(), export) {
("main", Export::Function { signature, .. }) => Some(signature),
_ => None,
},
)
.ok_or_else(|| {
CallError::Resolve(ResolveError::ExportNotFound {
name: "main".to_string(),
})
})
.and_then(|signature| {
let signature = signature.clone();
let parameter_types = signature.params();

if args.len() != parameter_types.len() {
Err(CallError::Resolve(ResolveError::Signature {
expected: (*signature).clone(),
found: args.iter().map(|_| Type::I32).collect(),
Copy link
Contributor Author

@Hywan Hywan Mar 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this step, we don't know the type of the arguments yet. For the error, let's assume it's i32.

}))
} else {
args.iter()
.enumerate()
.try_fold(
Vec::with_capacity(args.len()),
|mut accumulator, (nth, argument)| {
if let Some(value) = match parameter_types[nth] {
Type::I32 => argument
.parse::<i32>()
.map(|v| Some(Value::I32(v)))
.unwrap_or_else(|_| {
eprintln!(
"Failed to parse `{:?}` as an `i32`",
argument
);
None
}),
Type::I64 => argument
.parse::<i64>()
.map(|v| Some(Value::I64(v)))
.unwrap_or_else(|_| {
eprintln!(
"Failed to parse `{:?}` as an `i64`",
argument
);
None
}),
Type::F32 => argument
.parse::<f32>()
.map(|v| Some(Value::F32(v)))
.unwrap_or_else(|_| {
eprintln!(
"Failed to parse `{:?}` as an `f32`",
argument
);
None
}),
Type::F64 => argument
.parse::<f64>()
.map(|v| Some(Value::F64(v)))
.unwrap_or_else(|_| {
eprintln!(
"Failed to parse `{:?}` as an `f64`",
argument
);
None
}),
} {
accumulator.push(value);

Ok(())
Some(accumulator)
} else {
None
}
},
)
.map_or_else(
|| {
Err(CallError::Resolve(ResolveError::ExportWrongType {
name: "main".to_string(),
}))
},
|arguments| Ok(arguments),
)
}
})
.map(|arguments| match instance.call("main", &arguments[..]) {
Ok(result) => result
.iter()
.enumerate()
.for_each(|(nth, value)| match value {
Value::I32(e) => println!("result_{} = i32 : {}", nth, e),
Value::I64(e) => println!("result_{} = i64 : {}", nth, e),
Value::F32(e) => println!("result_{} = f32 : {}", nth, e),
Value::F64(e) => println!("result_{} = f64 : {}", nth, e),
}),
Err(error) => eprintln!("{}", error),
})
}
}