Fix test module linking
This commit is contained in:
parent
9482438360
commit
8a92d8f9b6
@ -3,12 +3,14 @@ use std::{
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
use parity_wasm::elements::Module;
|
||||
use wasm_ast::builder::TypeInfo;
|
||||
use wast::{
|
||||
core::{Expression, Instruction},
|
||||
AssertExpression, WastExecute, WastInvoke,
|
||||
};
|
||||
|
||||
use target::{Target, TypedModule};
|
||||
use target::{get_name_from_id, Target};
|
||||
|
||||
mod target;
|
||||
|
||||
@ -42,7 +44,7 @@ impl LuaJIT {
|
||||
}
|
||||
|
||||
fn write_call_of(handler: &str, data: &WastInvoke, w: &mut dyn Write) -> Result<()> {
|
||||
let name = TypedModule::resolve_id(data.module);
|
||||
let name = get_name_from_id(data.module);
|
||||
let func = data.name;
|
||||
|
||||
write!(w, "{handler}(")?;
|
||||
@ -78,7 +80,7 @@ impl Target for LuaJIT {
|
||||
writeln!(w)
|
||||
}
|
||||
WastExecute::Get { module, global } => {
|
||||
let name = TypedModule::resolve_id(*module);
|
||||
let name = get_name_from_id(*module);
|
||||
|
||||
write!(w, "assert_neq(")?;
|
||||
write!(w, r#"loaded["{name}"].global_list["{global}"].value"#)?;
|
||||
@ -108,7 +110,7 @@ impl Target for LuaJIT {
|
||||
writeln!(w, "}})")
|
||||
}
|
||||
WastExecute::Get { module, global } => {
|
||||
let name = TypedModule::resolve_id(*module);
|
||||
let name = get_name_from_id(*module);
|
||||
|
||||
write!(w, "assert_eq(")?;
|
||||
write!(w, r#"loaded["{name}"].global_list["{global}"].value"#)?;
|
||||
@ -132,10 +134,18 @@ impl Target for LuaJIT {
|
||||
writeln!(w, "local rt = (function() {runtime} end)()")
|
||||
}
|
||||
|
||||
fn write_module(typed: &TypedModule, w: &mut dyn Write) -> Result<()> {
|
||||
write!(w, r#"loaded["{}"] = (function() "#, typed.name())?;
|
||||
codegen_luajit::from_module_typed(typed.module(), typed.type_info(), w)?;
|
||||
writeln!(w, "end)()(nil)")
|
||||
fn write_module(data: &Module, name: Option<&str>, w: &mut dyn Write) -> Result<()> {
|
||||
let type_info = TypeInfo::from_module(data);
|
||||
|
||||
write!(w, r#"loaded["temp"] = (function() "#)?;
|
||||
codegen_luajit::from_module_typed(data, &type_info, w)?;
|
||||
writeln!(w, "end)()(linked)")?;
|
||||
|
||||
if let Some(name) = name {
|
||||
writeln!(w, r#"loaded["{name}"] = loaded["temp"]"#)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,12 +3,14 @@ use std::{
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
use parity_wasm::elements::Module;
|
||||
use wasm_ast::builder::TypeInfo;
|
||||
use wast::{
|
||||
core::{Expression, Instruction},
|
||||
AssertExpression, WastExecute, WastInvoke,
|
||||
};
|
||||
|
||||
use target::{Target, TypedModule};
|
||||
use target::{get_name_from_id, Target};
|
||||
|
||||
mod target;
|
||||
|
||||
@ -49,7 +51,7 @@ impl Luau {
|
||||
}
|
||||
|
||||
fn write_call_of(handler: &str, data: &WastInvoke, w: &mut dyn Write) -> Result<()> {
|
||||
let name = TypedModule::resolve_id(data.module);
|
||||
let name = get_name_from_id(data.module);
|
||||
let func = data.name;
|
||||
|
||||
write!(w, "{handler}(")?;
|
||||
@ -85,7 +87,7 @@ impl Target for Luau {
|
||||
writeln!(w)
|
||||
}
|
||||
WastExecute::Get { module, global } => {
|
||||
let name = TypedModule::resolve_id(*module);
|
||||
let name = get_name_from_id(*module);
|
||||
|
||||
write!(w, "assert_neq(")?;
|
||||
write!(w, r#"loaded["{name}"].global_list["{global}"].value"#)?;
|
||||
@ -115,7 +117,7 @@ impl Target for Luau {
|
||||
writeln!(w, "}})")
|
||||
}
|
||||
WastExecute::Get { module, global } => {
|
||||
let name = TypedModule::resolve_id(*module);
|
||||
let name = get_name_from_id(*module);
|
||||
|
||||
write!(w, "assert_eq(")?;
|
||||
write!(w, r#"loaded["{name}"].global_list["{global}"].value"#)?;
|
||||
@ -139,10 +141,18 @@ impl Target for Luau {
|
||||
writeln!(w, "local rt = (function() {runtime} end)()")
|
||||
}
|
||||
|
||||
fn write_module(typed: &TypedModule, w: &mut dyn Write) -> Result<()> {
|
||||
write!(w, r#"loaded["{}"] = (function() "#, typed.name())?;
|
||||
codegen_luau::from_module_typed(typed.module(), typed.type_info(), w)?;
|
||||
writeln!(w, "end)()(nil)")
|
||||
fn write_module(data: &Module, name: Option<&str>, w: &mut dyn Write) -> Result<()> {
|
||||
let type_info = TypeInfo::from_module(data);
|
||||
|
||||
write!(w, r#"loaded["temp"] = (function() "#)?;
|
||||
codegen_luau::from_module_typed(data, &type_info, w)?;
|
||||
writeln!(w, "end)()(linked)")?;
|
||||
|
||||
if let Some(name) = name {
|
||||
writeln!(w, r#"loaded["{name}"] = loaded["temp"]"#)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -11,8 +11,6 @@ use wast::{
|
||||
WastDirective, WastExecute, WastInvoke, Wat,
|
||||
};
|
||||
|
||||
use wasm_ast::builder::TypeInfo;
|
||||
|
||||
macro_rules! impl_write_number_nan {
|
||||
($name:ident, $name_nan:ident, $numeric:ty, $pattern:ty) => {
|
||||
pub fn $name(number: $numeric, w: &mut dyn Write) -> Result<()> {
|
||||
@ -44,38 +42,6 @@ macro_rules! impl_write_number_nan {
|
||||
impl_write_number_nan!(write_f32, write_f32_nan, f32, wast::token::Float32);
|
||||
impl_write_number_nan!(write_f64, write_f64_nan, f64, wast::token::Float64);
|
||||
|
||||
pub struct TypedModule<'a> {
|
||||
name: &'a str,
|
||||
module: &'a BinModule,
|
||||
type_info: TypeInfo<'a>,
|
||||
}
|
||||
|
||||
impl<'a> TypedModule<'a> {
|
||||
pub fn resolve_id(id: Option<Id>) -> &str {
|
||||
id.map_or("temp", |v| v.name())
|
||||
}
|
||||
|
||||
pub fn name(&self) -> &str {
|
||||
self.name
|
||||
}
|
||||
|
||||
pub fn module(&self) -> &BinModule {
|
||||
self.module
|
||||
}
|
||||
|
||||
pub fn type_info(&self) -> &TypeInfo<'a> {
|
||||
&self.type_info
|
||||
}
|
||||
|
||||
fn from_id(id: Option<Id<'a>>, module: &'a BinModule) -> Self {
|
||||
Self {
|
||||
module,
|
||||
name: Self::resolve_id(id),
|
||||
type_info: TypeInfo::from_module(module),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn try_into_ast_module(data: QuoteWat) -> Option<AstModule> {
|
||||
if let QuoteWat::Wat(Wat::Module(data)) = data {
|
||||
Some(data)
|
||||
@ -99,6 +65,10 @@ fn parse_and_validate<'a>(buffer: &'a ParseBuffer) -> Option<Wast<'a>> {
|
||||
observer.then(|| parsed)
|
||||
}
|
||||
|
||||
pub fn get_name_from_id(id: Option<Id>) -> &str {
|
||||
id.as_ref().map_or("temp", Id::name)
|
||||
}
|
||||
|
||||
pub trait Target: Sized {
|
||||
fn executable() -> String;
|
||||
|
||||
@ -118,20 +88,21 @@ pub trait Target: Sized {
|
||||
|
||||
fn write_runtime(w: &mut dyn Write) -> Result<()>;
|
||||
|
||||
fn write_module(typed: &TypedModule, w: &mut dyn Write) -> Result<()>;
|
||||
fn write_module(data: &BinModule, name: Option<&str>, w: &mut dyn Write) -> Result<()>;
|
||||
|
||||
fn write_variant(variant: WastDirective, w: &mut dyn Write) -> Result<()> {
|
||||
match variant {
|
||||
WastDirective::Wat(data) => {
|
||||
let mut ast = try_into_ast_module(data).expect("Must be a module");
|
||||
let bytes = ast.encode().unwrap();
|
||||
let temp = parity_wasm::deserialize_buffer(&bytes).unwrap();
|
||||
let typed = TypedModule::from_id(ast.id, &temp);
|
||||
|
||||
Self::write_module(&typed, w)?;
|
||||
let data = parity_wasm::deserialize_buffer(&bytes).unwrap();
|
||||
let name = ast.id.as_ref().map(Id::name);
|
||||
|
||||
Self::write_module(&data, name, w)?;
|
||||
}
|
||||
WastDirective::Register { name, module, .. } => {
|
||||
let pre = TypedModule::resolve_id(module);
|
||||
let pre = get_name_from_id(module);
|
||||
|
||||
Self::write_register(name, pre, w)?;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user