mirror of
https://github.com/fluencelabs/marine-rs-sdk-test
synced 2024-12-04 15:20:18 +00:00
refactor wit generation
This commit is contained in:
parent
8b2c6145fd
commit
be0451accb
@ -58,6 +58,7 @@ pub struct AstFunctionItem {
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "ast_type")]
|
||||
pub enum FCEAst {
|
||||
Function(AstFunctionItem),
|
||||
ExternMod(AstExternModItem),
|
||||
|
@ -23,7 +23,7 @@ use crate::fce_ast_types::FCEAst;
|
||||
use proc_macro2::TokenStream;
|
||||
|
||||
const GENERATED_FUNCS_PREFIX: &str = "__fce_generated_func_";
|
||||
pub const GENERATED_SECTION_NAME: &str = "__fce_generated_section";
|
||||
pub const GENERATED_SECTION_NAME: &str = "__fce_generated_section__";
|
||||
const GENERATED_SECTION_PREFIX: &str = "__fce_generated_static_global_";
|
||||
|
||||
pub(crate) trait TokenStreamGenerator {
|
||||
|
@ -27,7 +27,9 @@ use crate::wasm_type::WasmType;
|
||||
|
||||
impl TokenStreamGenerator for fce_ast_types::AstFunctionItem {
|
||||
fn generate_token_stream(self) -> syn::Result<TokenStream> {
|
||||
let data = serde_json::to_vec(&self).unwrap();
|
||||
// TODO: change serialization protocol
|
||||
let fce_type = fce_ast_types::FCEAst::Function(self.clone());
|
||||
let data = serde_json::to_vec(&fce_type).unwrap();
|
||||
let data_size = data.len();
|
||||
let data = syn::LitByteStr::new(&data, proc_macro2::Span::call_site());
|
||||
|
||||
@ -36,10 +38,11 @@ impl TokenStreamGenerator for fce_ast_types::AstFunctionItem {
|
||||
let func_name =
|
||||
syn::parse_str::<syn::Ident>(&(GENERATED_FUNCS_PREFIX.to_string() + &signature.name))?;
|
||||
let original_func_ident = syn::parse_str::<syn::Ident>(&signature.name)?;
|
||||
let export_func_name = signature.name;
|
||||
//let func_name = syn::parse_str::<syn::Ident>(&(GENERATED_FUNCS_PREFIX.to_string() + &self.name))?;
|
||||
|
||||
let section_name = GENERATED_SECTION_NAME;
|
||||
let section_name = GENERATED_SECTION_NAME.to_string() + &signature.name.replace("-", "_");
|
||||
let export_func_name = signature.name;
|
||||
|
||||
//let section_prefix = syn::parse_str::<syn::Ident>(GENERATED_SECTION_PREFIX)?;
|
||||
let global_static_name = syn::parse_str::<syn::Ident>(
|
||||
&(GENERATED_SECTION_PREFIX.to_string() + &export_func_name),
|
||||
|
@ -27,7 +27,9 @@ use crate::parsed_type::ForeignModeGlueCodeGenerator;
|
||||
|
||||
impl TokenStreamGenerator for fce_ast_types::AstExternModItem {
|
||||
fn generate_token_stream(self) -> syn::Result<TokenStream> {
|
||||
let data = serde_json::to_vec(&self).unwrap();
|
||||
// TODO: change serialization protocol
|
||||
let fce_type = fce_ast_types::FCEAst::ExternMod(self.clone());
|
||||
let data = serde_json::to_vec(&fce_type).unwrap();
|
||||
let data_size = data.len();
|
||||
let data = syn::LitByteStr::new(&data, proc_macro2::Span::call_site());
|
||||
|
||||
@ -36,7 +38,7 @@ impl TokenStreamGenerator for fce_ast_types::AstExternModItem {
|
||||
proc_macro2::Span::call_site(),
|
||||
);
|
||||
|
||||
let section_name = GENERATED_SECTION_NAME;
|
||||
let section_name = GENERATED_SECTION_NAME.to_string() + &self.namespace.replace(".", "_");
|
||||
|
||||
let wasm_import_module_name = &self.namespace;
|
||||
let generated_imports = generate_extern_section_items(&self);
|
||||
|
@ -5,6 +5,9 @@ authors = ["Fluence Labs"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
wit_parser = { path = "/Users/mike/dev/work/fluence/wasm/fce/crates/wit_parser" }
|
||||
walrus = "0.17.0"
|
||||
wit-support = { path = "../wit-support" }
|
||||
wasmer-wit = { package = "wasmer-interface-types", git = "http://github.com/fluencelabs/interface-types", branch = "master", features = ["serde"] }
|
||||
serde = { version = "1.0.110", features = ["derive"] }
|
||||
serde_json = "1.0.56"
|
||||
|
@ -1,6 +1,109 @@
|
||||
mod wit_generator;
|
||||
mod wasm_ast_extractor;
|
||||
|
||||
pub use wit_support::FCEAst;
|
||||
use wit_generator::WITGenerator;
|
||||
|
||||
pub fn generate_export(path: std::path::PathBuf) {}
|
||||
pub use wit_support::FCEAst;
|
||||
use wasmer_wit::ast::Interfaces;
|
||||
|
||||
pub fn embed_wit(path: std::path::PathBuf) {
|
||||
let ast_set = wasm_ast_extractor::wasm_ast_extractor(path.clone()).unwrap();
|
||||
let interfaces = generate_interfaces(&ast_set);
|
||||
wit_parser::embed_wit(path.clone(), path.clone(), &interfaces).unwrap();
|
||||
}
|
||||
|
||||
fn generate_interfaces(ast_set: &[FCEAst]) -> Interfaces<'_> {
|
||||
let mut interfaces = Interfaces::default();
|
||||
generate_default_api(&mut interfaces);
|
||||
|
||||
for ast in ast_set {
|
||||
ast.generate_wit(&mut interfaces);
|
||||
}
|
||||
|
||||
interfaces
|
||||
}
|
||||
|
||||
fn generate_default_api(interfaces: &mut Interfaces) {
|
||||
use wasmer_wit::ast::Type;
|
||||
use wasmer_wit::ast::Export;
|
||||
use wasmer_wit::types::InterfaceType as IType;
|
||||
|
||||
let allocate_inputs = vec![IType::I32];
|
||||
let allocate_outputs = vec![IType::I32];
|
||||
let allocate_func_type = Type::Function {
|
||||
inputs: allocate_inputs,
|
||||
outputs: allocate_outputs,
|
||||
};
|
||||
|
||||
let deallocate_inputs = vec![IType::I32, IType::I32];
|
||||
let deallocate_outputs = vec![];
|
||||
let deallocate_func_type = Type::Function {
|
||||
inputs: deallocate_inputs,
|
||||
outputs: deallocate_outputs,
|
||||
};
|
||||
|
||||
let get_result_inputs = vec![];
|
||||
let get_result_outputs = vec![IType::I32];
|
||||
let get_result_size_func_type = Type::Function {
|
||||
inputs: get_result_inputs.clone(),
|
||||
outputs: get_result_outputs.clone(),
|
||||
};
|
||||
let get_result_ptr_func_type = Type::Function {
|
||||
inputs: get_result_inputs,
|
||||
outputs: get_result_outputs,
|
||||
};
|
||||
|
||||
let set_result_inputs = vec![IType::I32];
|
||||
let set_result_outputs = vec![];
|
||||
let set_result_size_func_type = Type::Function {
|
||||
inputs: set_result_inputs.clone(),
|
||||
outputs: set_result_outputs.clone(),
|
||||
};
|
||||
let set_result_ptr_func_type = Type::Function {
|
||||
inputs: set_result_inputs,
|
||||
outputs: set_result_outputs,
|
||||
};
|
||||
|
||||
interfaces.types.push(allocate_func_type);
|
||||
interfaces.types.push(deallocate_func_type);
|
||||
interfaces.types.push(get_result_size_func_type);
|
||||
interfaces.types.push(get_result_ptr_func_type);
|
||||
interfaces.types.push(set_result_size_func_type);
|
||||
interfaces.types.push(set_result_ptr_func_type);
|
||||
|
||||
let allocate_export = Export {
|
||||
name: "allocate",
|
||||
function_type: 0,
|
||||
};
|
||||
interfaces.exports.push(allocate_export);
|
||||
|
||||
let deallocate_export = Export {
|
||||
name: "deallocate",
|
||||
function_type: 1,
|
||||
};
|
||||
interfaces.exports.push(deallocate_export);
|
||||
|
||||
let get_result_size_export = Export {
|
||||
name: "get_result_size",
|
||||
function_type: 2,
|
||||
};
|
||||
interfaces.exports.push(get_result_size_export);
|
||||
|
||||
let get_result_ptr_export = Export {
|
||||
name: "get_result_ptr",
|
||||
function_type: 3,
|
||||
};
|
||||
interfaces.exports.push(get_result_ptr_export);
|
||||
|
||||
let set_result_size_export = Export {
|
||||
name: "set_result_size",
|
||||
function_type: 4,
|
||||
};
|
||||
interfaces.exports.push(set_result_size_export);
|
||||
|
||||
let set_result_ptr_export = Export {
|
||||
name: "set_result_ptr",
|
||||
function_type: 5,
|
||||
};
|
||||
interfaces.exports.push(set_result_ptr_export);
|
||||
}
|
||||
|
@ -1,19 +1,28 @@
|
||||
use walrus::{IdsToIndices, ModuleConfig};
|
||||
use walrus::ModuleConfig;
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
struct WasmAst {
|
||||
exports: Vec<wit_support::AstFunctionItem>,
|
||||
imports: Vec<wit_support::AstExternModItem>,
|
||||
records: Vec<wit_support::AstRecordItem>,
|
||||
}
|
||||
|
||||
pub(crate) fn wasm_ast_extractor(wasm_path: std::path::PathBuf) -> Result<WasmAst, std::io::Error> {
|
||||
let module = ModuleConfig::new().parse_file(wasm_path)?;
|
||||
pub(crate) fn wasm_ast_extractor(
|
||||
wasm_path: std::path::PathBuf,
|
||||
) -> Result<Vec<wit_support::FCEAst>, std::io::Error> {
|
||||
let module = ModuleConfig::new().parse_file(wasm_path).unwrap();
|
||||
let mut decoded_ast = Vec::new();
|
||||
|
||||
let sections = module
|
||||
.customs
|
||||
.iter()
|
||||
.filter(|(_, section)| section.name().starts_with(wit_support::GENERATED_SECTION_NAME))
|
||||
.map(|section|)
|
||||
.collect::<Vec<_>>();
|
||||
for custom_module in module.customs.iter().filter(|(_, section)| {
|
||||
section
|
||||
.name()
|
||||
.starts_with(wit_support::GENERATED_SECTION_NAME)
|
||||
}) {
|
||||
let default_ids = walrus::IdsToIndices::default();
|
||||
let raw_data = custom_module.1.data(&default_ids);
|
||||
let decoded_json: wit_support::FCEAst = serde_json::from_slice(&raw_data).unwrap();
|
||||
decoded_ast.push(decoded_json);
|
||||
}
|
||||
|
||||
Ok(decoded_ast)
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ use wasmer_wit::types::InterfaceType as IType;
|
||||
use wasmer_wit::ast::Interfaces;
|
||||
use wasmer_wit::interpreter::Instruction;
|
||||
|
||||
trait WITGenerator {
|
||||
pub trait WITGenerator {
|
||||
fn generate_wit<'a>(&'a self, interfaces: &mut Interfaces<'a>);
|
||||
}
|
||||
|
||||
|
@ -128,8 +128,8 @@ impl ForeignModInstructionGenerator for ParsedType {
|
||||
Instruction::CallCore { function_index: 0 },
|
||||
Instruction::Swap2,
|
||||
Instruction::StringLowerMemory,
|
||||
Instruction::CallCore { function_index: 4 },
|
||||
Instruction::CallCore { function_index: 5 },
|
||||
Instruction::CallCore { function_index: 6 },
|
||||
],
|
||||
ParsedType::ByteVector => vec![
|
||||
Instruction::Dup,
|
||||
@ -137,8 +137,8 @@ impl ForeignModInstructionGenerator for ParsedType {
|
||||
Instruction::CallCore { function_index: 0 },
|
||||
Instruction::Swap2,
|
||||
Instruction::StringLowerMemory,
|
||||
Instruction::CallCore { function_index: 4 },
|
||||
Instruction::CallCore { function_index: 5 },
|
||||
Instruction::CallCore { function_index: 6 },
|
||||
],
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
|
@ -42,4 +42,3 @@ pub use fluence_sdk_main::get_result_ptr;
|
||||
pub use fluence_sdk_main::get_result_size;
|
||||
pub use fluence_sdk_main::set_result_ptr;
|
||||
pub use fluence_sdk_main::set_result_size;
|
||||
pub use fluence_sdk_macro
|
||||
|
Loading…
Reference in New Issue
Block a user