Fix and clean spec runner.
This commit is contained in:
parent
5188a0e29c
commit
ef7e525f4d
|
@ -143,6 +143,7 @@ impl ModuleImportResolver for SpecModule {
|
||||||
struct SpecDriver {
|
struct SpecDriver {
|
||||||
spec_module: SpecModule,
|
spec_module: SpecModule,
|
||||||
instances: HashMap<String, ModuleRef>,
|
instances: HashMap<String, ModuleRef>,
|
||||||
|
last_module: Option<ModuleRef>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SpecDriver {
|
impl SpecDriver {
|
||||||
|
@ -150,6 +151,7 @@ impl SpecDriver {
|
||||||
SpecDriver {
|
SpecDriver {
|
||||||
spec_module: SpecModule::new(),
|
spec_module: SpecModule::new(),
|
||||||
instances: HashMap::new(),
|
instances: HashMap::new(),
|
||||||
|
last_module: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,15 +159,27 @@ impl SpecDriver {
|
||||||
&mut self.spec_module
|
&mut self.spec_module
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_module(&mut self, name: String, module: ModuleRef) {
|
fn add_module(&mut self, name: Option<String>, module: ModuleRef) {
|
||||||
|
self.last_module = Some(module.clone());
|
||||||
|
if let Some(name) = name {
|
||||||
self.instances.insert(name, module);
|
self.instances.insert(name, module);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn module(&self, name: &str) -> Result<ModuleRef, InterpreterError> {
|
fn module(&self, name: &str) -> Result<ModuleRef, InterpreterError> {
|
||||||
self.instances.get(name).cloned().ok_or_else(|| {
|
self.instances.get(name).cloned().ok_or_else(|| {
|
||||||
InterpreterError::Instantiation(format!("Module not registered {}", name))
|
InterpreterError::Instantiation(format!("Module not registered {}", name))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn module_or_last(&self, name: Option<&str>) -> Result<ModuleRef, InterpreterError> {
|
||||||
|
match name {
|
||||||
|
Some(name) => self.module(name),
|
||||||
|
None => self.last_module.clone().ok_or_else(|| {
|
||||||
|
InterpreterError::Instantiation("No modules registered".into())
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ImportResolver for SpecDriver {
|
impl ImportResolver for SpecDriver {
|
||||||
|
@ -263,11 +277,8 @@ fn load_module(
|
||||||
.run_start(spec_driver.spec_module())
|
.run_start(spec_driver.spec_module())
|
||||||
.expect("Run start failed");
|
.expect("Run start failed");
|
||||||
|
|
||||||
let module_name = name.as_ref()
|
let module_name = name.clone();
|
||||||
.map(|s| s.as_ref())
|
spec_driver.add_module(module_name, instance.clone());
|
||||||
.unwrap_or("wasm_test")
|
|
||||||
.trim_left_matches('$');
|
|
||||||
spec_driver.add_module(module_name.to_owned(), instance.clone());
|
|
||||||
|
|
||||||
instance
|
instance
|
||||||
}
|
}
|
||||||
|
@ -311,10 +322,8 @@ fn run_action(
|
||||||
ref field,
|
ref field,
|
||||||
ref args,
|
ref args,
|
||||||
} => {
|
} => {
|
||||||
let module = module.clone().unwrap_or("wasm_test".into());
|
let module = program.module_or_last(module.as_ref().map(|x| x.as_ref())).expect(&format!(
|
||||||
let module = module.trim_left_matches('$');
|
"Expected program to have loaded module {:?}",
|
||||||
let module = program.module(&module).expect(&format!(
|
|
||||||
"Expected program to have loaded module {}",
|
|
||||||
module
|
module
|
||||||
));
|
));
|
||||||
module.invoke_export(
|
module.invoke_export(
|
||||||
|
@ -328,10 +337,8 @@ fn run_action(
|
||||||
ref field,
|
ref field,
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
let module = module.clone().unwrap_or("wasm_test".into());
|
let module = program.module_or_last(module.as_ref().map(|x| x.as_ref())).expect(&format!(
|
||||||
let module = module.trim_left_matches('$');
|
"Expected program to have loaded module {:?}",
|
||||||
let module = program.module(&module).expect(&format!(
|
|
||||||
"Expected program to have loaded module {}",
|
|
||||||
module
|
module
|
||||||
));
|
));
|
||||||
let field = jstring_to_rstring(&field);
|
let field = jstring_to_rstring(&field);
|
||||||
|
@ -418,7 +425,6 @@ pub fn spec(name: &str) {
|
||||||
serde_json::from_reader(&mut f).expect("Failed to deserialize JSON file");
|
serde_json::from_reader(&mut f).expect("Failed to deserialize JSON file");
|
||||||
|
|
||||||
let mut spec_driver = SpecDriver::new();
|
let mut spec_driver = SpecDriver::new();
|
||||||
let mut last_module = None;
|
|
||||||
for command in &spec.commands {
|
for command in &spec.commands {
|
||||||
println!("command {:?}", command);
|
println!("command {:?}", command);
|
||||||
match command {
|
match command {
|
||||||
|
@ -427,7 +433,7 @@ pub fn spec(name: &str) {
|
||||||
ref filename,
|
ref filename,
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
last_module = Some(load_module(tmpdir.as_ref(), &filename, &name, &mut spec_driver));
|
load_module(tmpdir.as_ref(), &filename, &name, &mut spec_driver);
|
||||||
}
|
}
|
||||||
&test::Command::AssertReturn {
|
&test::Command::AssertReturn {
|
||||||
line,
|
line,
|
||||||
|
@ -535,19 +541,16 @@ pub fn spec(name: &str) {
|
||||||
Err(e) => println!("assert_uninstantiable - success ({:?})", e),
|
Err(e) => println!("assert_uninstantiable - success ({:?})", e),
|
||||||
},
|
},
|
||||||
&test::Command::Register {
|
&test::Command::Register {
|
||||||
|
line,
|
||||||
ref name,
|
ref name,
|
||||||
ref as_name,
|
ref as_name,
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
match name {
|
let module = match spec_driver.module_or_last(name.as_ref().map(|x| x.as_ref())) {
|
||||||
&Some(ref name) => assert_eq!(name.trim_left_matches('$'), as_name), // we have already registered this module without $ prefix
|
Ok(module) => module,
|
||||||
&None => spec_driver.add_module(
|
Err(e) => panic!("No such module, at line {} - ({:?})", e, line),
|
||||||
as_name.clone(),
|
};
|
||||||
last_module
|
spec_driver.add_module(Some(as_name.clone()), module);
|
||||||
.take()
|
|
||||||
.expect("Last module must be set for this command"),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
&test::Command::Action { line, ref action } => {
|
&test::Command::Action { line, ref action } => {
|
||||||
match run_action(&mut spec_driver, action) {
|
match run_action(&mut spec_driver, action) {
|
||||||
|
|
Loading…
Reference in New Issue