diff --git a/Cargo.toml b/Cargo.toml index 09c3b90..d750ce4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,15 +10,13 @@ version = "0.1.0" default = [] [dependencies] -wami_macro = {path="./lib/wami_macro"} leb128 = {path="./lib/leb128"} +libwat2wasm = { path = "./lib/libwat2wasm" } +wami_macro = {path="./lib/wami_macro"} libm = {version="0.2.11"} smallvec = {version = "1.13.2", default-features = false} -[dev-dependencies] -wa_asm = { path = "./lib/wa_asm" } - [workspace] members = [ "example/cli", @@ -26,5 +24,5 @@ members = [ "tools/wat2wasm", "lib/wami_macro", "lib/leb128", - "lib/wa_asm", + "lib/libwat2wasm", ] diff --git a/README.md b/README.md index 1989cd7..176afdf 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ fn main() { ) ) "#; - let bin = WasmAssembler::to_wasm("hello.wat", src.as_bytes().to_vec()).unwrap(); + let bin = WebAssembly::wat2wasm("hello.wat", src.as_bytes().to_vec()).unwrap(); let instance = WebAssembly::instantiate(&bin, &Env {}).unwrap(); instance.exports().main().unwrap(); } diff --git a/example/hello/Cargo.toml b/example/hello/Cargo.toml index c1f27e6..f4c788e 100644 --- a/example/hello/Cargo.toml +++ b/example/hello/Cargo.toml @@ -8,4 +8,3 @@ version = "0.1.0" [dependencies] wami = { path="../../" } -wa_asm = { path = "../../lib/wa_asm" } diff --git a/example/hello/src/main.rs b/example/hello/src/main.rs index d2b0c43..e8698d2 100644 --- a/example/hello/src/main.rs +++ b/example/hello/src/main.rs @@ -1,5 +1,4 @@ // Hello world -use wa_asm::WasmAssembler; use wami::prelude::*; fn main() { @@ -18,7 +17,7 @@ fn main() { ) ) "#; - let bin = WasmAssembler::to_wasm("hello.wat", src.as_bytes().to_vec()).unwrap(); + let bin = WebAssembly::wat2wasm("hello.wat", src.as_bytes().to_vec()).unwrap(); let instance = WebAssembly::instantiate(&bin, &Env {}).unwrap(); instance.exports().main().unwrap(); } diff --git a/lib/wa_asm/Cargo.toml b/lib/libwat2wasm/Cargo.toml similarity index 91% rename from lib/wa_asm/Cargo.toml rename to lib/libwat2wasm/Cargo.toml index 32fa28a..28f4427 100644 --- a/lib/wa_asm/Cargo.toml +++ b/lib/libwat2wasm/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "wa_asm" +name = "libwat2wasm" version = "0.1.0" edition = "2024" diff --git a/lib/wa_asm/build.rs b/lib/libwat2wasm/build.rs similarity index 100% rename from lib/wa_asm/build.rs rename to lib/libwat2wasm/build.rs diff --git a/lib/wa_asm/src/_generated/keyword.rs b/lib/libwat2wasm/src/_generated/keyword.rs similarity index 100% rename from lib/wa_asm/src/_generated/keyword.rs rename to lib/libwat2wasm/src/_generated/keyword.rs diff --git a/lib/wa_asm/src/_generated/opcode.rs b/lib/libwat2wasm/src/_generated/opcode.rs similarity index 100% rename from lib/wa_asm/src/_generated/opcode.rs rename to lib/libwat2wasm/src/_generated/opcode.rs diff --git a/lib/wa_asm/src/_generated/valtype.rs b/lib/libwat2wasm/src/_generated/valtype.rs similarity index 100% rename from lib/wa_asm/src/_generated/valtype.rs rename to lib/libwat2wasm/src/_generated/valtype.rs diff --git a/lib/wa_asm/src/ast/data.rs b/lib/libwat2wasm/src/ast/data.rs similarity index 100% rename from lib/wa_asm/src/ast/data.rs rename to lib/libwat2wasm/src/ast/data.rs diff --git a/lib/wa_asm/src/ast/elem.rs b/lib/libwat2wasm/src/ast/elem.rs similarity index 100% rename from lib/wa_asm/src/ast/elem.rs rename to lib/libwat2wasm/src/ast/elem.rs diff --git a/lib/wa_asm/src/ast/export.rs b/lib/libwat2wasm/src/ast/export.rs similarity index 100% rename from lib/wa_asm/src/ast/export.rs rename to lib/libwat2wasm/src/ast/export.rs diff --git a/lib/wa_asm/src/ast/function.rs b/lib/libwat2wasm/src/ast/function.rs similarity index 100% rename from lib/wa_asm/src/ast/function.rs rename to lib/libwat2wasm/src/ast/function.rs diff --git a/lib/wa_asm/src/ast/global.rs b/lib/libwat2wasm/src/ast/global.rs similarity index 100% rename from lib/wa_asm/src/ast/global.rs rename to lib/libwat2wasm/src/ast/global.rs diff --git a/lib/wa_asm/src/ast/identifier.rs b/lib/libwat2wasm/src/ast/identifier.rs similarity index 100% rename from lib/wa_asm/src/ast/identifier.rs rename to lib/libwat2wasm/src/ast/identifier.rs diff --git a/lib/wa_asm/src/ast/import.rs b/lib/libwat2wasm/src/ast/import.rs similarity index 100% rename from lib/wa_asm/src/ast/import.rs rename to lib/libwat2wasm/src/ast/import.rs diff --git a/lib/wa_asm/src/ast/literal.rs b/lib/libwat2wasm/src/ast/literal.rs similarity index 100% rename from lib/wa_asm/src/ast/literal.rs rename to lib/libwat2wasm/src/ast/literal.rs diff --git a/lib/wa_asm/src/ast/memory.rs b/lib/libwat2wasm/src/ast/memory.rs similarity index 100% rename from lib/wa_asm/src/ast/memory.rs rename to lib/libwat2wasm/src/ast/memory.rs diff --git a/lib/wa_asm/src/ast/mod.rs b/lib/libwat2wasm/src/ast/mod.rs similarity index 100% rename from lib/wa_asm/src/ast/mod.rs rename to lib/libwat2wasm/src/ast/mod.rs diff --git a/lib/wa_asm/src/ast/start.rs b/lib/libwat2wasm/src/ast/start.rs similarity index 100% rename from lib/wa_asm/src/ast/start.rs rename to lib/libwat2wasm/src/ast/start.rs diff --git a/lib/wa_asm/src/ast/table.rs b/lib/libwat2wasm/src/ast/table.rs similarity index 100% rename from lib/wa_asm/src/ast/table.rs rename to lib/libwat2wasm/src/ast/table.rs diff --git a/lib/wa_asm/src/ast/tests.rs b/lib/libwat2wasm/src/ast/tests.rs similarity index 100% rename from lib/wa_asm/src/ast/tests.rs rename to lib/libwat2wasm/src/ast/tests.rs diff --git a/lib/wa_asm/src/ast/types.rs b/lib/libwat2wasm/src/ast/types.rs similarity index 100% rename from lib/wa_asm/src/ast/types.rs rename to lib/libwat2wasm/src/ast/types.rs diff --git a/lib/wa_asm/src/error/assemble_error.rs b/lib/libwat2wasm/src/error/assemble_error.rs similarity index 100% rename from lib/wa_asm/src/error/assemble_error.rs rename to lib/libwat2wasm/src/error/assemble_error.rs diff --git a/lib/wa_asm/src/error/mod.rs b/lib/libwat2wasm/src/error/mod.rs similarity index 100% rename from lib/wa_asm/src/error/mod.rs rename to lib/libwat2wasm/src/error/mod.rs diff --git a/lib/wa_asm/src/ir/code.rs b/lib/libwat2wasm/src/ir/code.rs similarity index 100% rename from lib/wa_asm/src/ir/code.rs rename to lib/libwat2wasm/src/ir/code.rs diff --git a/lib/wa_asm/src/ir/data.rs b/lib/libwat2wasm/src/ir/data.rs similarity index 100% rename from lib/wa_asm/src/ir/data.rs rename to lib/libwat2wasm/src/ir/data.rs diff --git a/lib/wa_asm/src/ir/elem.rs b/lib/libwat2wasm/src/ir/elem.rs similarity index 100% rename from lib/wa_asm/src/ir/elem.rs rename to lib/libwat2wasm/src/ir/elem.rs diff --git a/lib/wa_asm/src/ir/export.rs b/lib/libwat2wasm/src/ir/export.rs similarity index 100% rename from lib/wa_asm/src/ir/export.rs rename to lib/libwat2wasm/src/ir/export.rs diff --git a/lib/wa_asm/src/ir/func.rs b/lib/libwat2wasm/src/ir/func.rs similarity index 100% rename from lib/wa_asm/src/ir/func.rs rename to lib/libwat2wasm/src/ir/func.rs diff --git a/lib/wa_asm/src/ir/global.rs b/lib/libwat2wasm/src/ir/global.rs similarity index 100% rename from lib/wa_asm/src/ir/global.rs rename to lib/libwat2wasm/src/ir/global.rs diff --git a/lib/wa_asm/src/ir/import.rs b/lib/libwat2wasm/src/ir/import.rs similarity index 100% rename from lib/wa_asm/src/ir/import.rs rename to lib/libwat2wasm/src/ir/import.rs diff --git a/lib/wa_asm/src/ir/index.rs b/lib/libwat2wasm/src/ir/index.rs similarity index 100% rename from lib/wa_asm/src/ir/index.rs rename to lib/libwat2wasm/src/ir/index.rs diff --git a/lib/wa_asm/src/ir/memory.rs b/lib/libwat2wasm/src/ir/memory.rs similarity index 100% rename from lib/wa_asm/src/ir/memory.rs rename to lib/libwat2wasm/src/ir/memory.rs diff --git a/lib/wa_asm/src/ir/mod.rs b/lib/libwat2wasm/src/ir/mod.rs similarity index 100% rename from lib/wa_asm/src/ir/mod.rs rename to lib/libwat2wasm/src/ir/mod.rs diff --git a/lib/wa_asm/src/ir/table.rs b/lib/libwat2wasm/src/ir/table.rs similarity index 100% rename from lib/wa_asm/src/ir/table.rs rename to lib/libwat2wasm/src/ir/table.rs diff --git a/lib/wa_asm/src/ir/types.rs b/lib/libwat2wasm/src/ir/types.rs similarity index 100% rename from lib/wa_asm/src/ir/types.rs rename to lib/libwat2wasm/src/ir/types.rs diff --git a/lib/wa_asm/src/lexer/mod.rs b/lib/libwat2wasm/src/lexer/mod.rs similarity index 100% rename from lib/wa_asm/src/lexer/mod.rs rename to lib/libwat2wasm/src/lexer/mod.rs diff --git a/lib/wa_asm/src/lexer/tests.rs b/lib/libwat2wasm/src/lexer/tests.rs similarity index 100% rename from lib/wa_asm/src/lexer/tests.rs rename to lib/libwat2wasm/src/lexer/tests.rs diff --git a/lib/wa_asm/src/lexer/utf8.rs b/lib/libwat2wasm/src/lexer/utf8.rs similarity index 100% rename from lib/wa_asm/src/lexer/utf8.rs rename to lib/libwat2wasm/src/lexer/utf8.rs diff --git a/lib/wa_asm/src/lib.rs b/lib/libwat2wasm/src/lib.rs similarity index 87% rename from lib/wa_asm/src/lib.rs rename to lib/libwat2wasm/src/lib.rs index 2ccd84a..b2f34b2 100644 --- a/lib/wa_asm/src/lib.rs +++ b/lib/libwat2wasm/src/lib.rs @@ -48,20 +48,7 @@ impl WasmAssembler { .map_err(|e| e.to_detail_string(file_name, &src, tokens.line_positions())) } - // pub fn explain_ast(file_name: &str, src: Vec) -> Result { - // Self::_from_src(file_name, src, |tokens| ast::AstModule::parse(tokens)) - // .map(|v| format!("{:#?}", v)) - // } - - // pub fn explain_ir(file_name: &str, src: Vec) -> Result { - // Self::_from_src(file_name, src, |tokens| { - // let ast_module = ast::AstModule::parse(tokens)?; - // ir::Module::from_ast(ast_module) - // }) - // .map(|v| format!("{:#?}", v)) - // } - - pub fn to_wasm(file_name: &str, src: Vec) -> Result, String> { + pub fn assemble(file_name: &str, src: Vec) -> Result, String> { Self::_from_src(file_name, src, |tokens: &mut TokenStream| { let ir_module = ir::Module::from_ast(ast::AstModule::parse(tokens)?)?; diff --git a/lib/wa_asm/src/misc/keyword.txt b/lib/libwat2wasm/src/misc/keyword.txt similarity index 100% rename from lib/wa_asm/src/misc/keyword.txt rename to lib/libwat2wasm/src/misc/keyword.txt diff --git a/lib/wa_asm/src/misc/opcode.csv b/lib/libwat2wasm/src/misc/opcode.csv similarity index 100% rename from lib/wa_asm/src/misc/opcode.csv rename to lib/libwat2wasm/src/misc/opcode.csv diff --git a/lib/wa_asm/src/misc/valtype.txt b/lib/libwat2wasm/src/misc/valtype.txt similarity index 100% rename from lib/wa_asm/src/misc/valtype.txt rename to lib/libwat2wasm/src/misc/valtype.txt diff --git a/lib/wa_asm/src/tests.rs b/lib/libwat2wasm/src/tests.rs similarity index 100% rename from lib/wa_asm/src/tests.rs rename to lib/libwat2wasm/src/tests.rs diff --git a/lib/wa_asm/src/types/mod.rs b/lib/libwat2wasm/src/types/mod.rs similarity index 100% rename from lib/wa_asm/src/types/mod.rs rename to lib/libwat2wasm/src/types/mod.rs diff --git a/lib/wa_asm/src/wasm/code.rs b/lib/libwat2wasm/src/wasm/code.rs similarity index 100% rename from lib/wa_asm/src/wasm/code.rs rename to lib/libwat2wasm/src/wasm/code.rs diff --git a/lib/wa_asm/src/wasm/expr.rs b/lib/libwat2wasm/src/wasm/expr.rs similarity index 100% rename from lib/wa_asm/src/wasm/expr.rs rename to lib/libwat2wasm/src/wasm/expr.rs diff --git a/lib/wa_asm/src/wasm/mod.rs b/lib/libwat2wasm/src/wasm/mod.rs similarity index 100% rename from lib/wa_asm/src/wasm/mod.rs rename to lib/libwat2wasm/src/wasm/mod.rs diff --git a/lib/wa_asm/src/wasm/section_id.rs b/lib/libwat2wasm/src/wasm/section_id.rs similarity index 100% rename from lib/wa_asm/src/wasm/section_id.rs rename to lib/libwat2wasm/src/wasm/section_id.rs diff --git a/src/tests.rs b/src/tests.rs index 5abb81d..54cb581 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -7,7 +7,6 @@ use core::f64::consts::PI; use leb128::*; use std::assert_matches::assert_matches; use std::sync::OnceLock; -use wa_asm::WasmAssembler; struct Env; @@ -98,7 +97,7 @@ fn shared_instance() -> WasmInstance { static BINARY: OnceLock> = OnceLock::new(); let wasm = BINARY.get_or_init(|| { let src = include_bytes!("../test/tester.wat").to_vec(); - WasmAssembler::to_wasm("tester.wat", src).unwrap() + WebAssembly::wat2wasm("tester.wat", src).unwrap() }); WebAssembly::instantiate(&wasm, &Env {}).unwrap() } diff --git a/src/wasm.rs b/src/wasm.rs index bbb6ddb..d15556d 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -15,6 +15,7 @@ use core::slice; use core::str; use global::WasmGlobal; use leb128::*; +use libwat2wasm::WasmAssembler; use smallvec::SmallVec; pub struct WebAssembly; @@ -40,9 +41,9 @@ impl WebAssembly { } /// Instantiate wasm module - pub fn instantiate( + pub fn instantiate( bytes: &[u8], - env: &Env, + env: &ENV, ) -> Result> { Self::compile(bytes)?.instantiate(env) } @@ -58,6 +59,11 @@ impl WebAssembly { pub fn validate(bytes: &[u8]) -> bool { Self::compile(bytes).is_ok() } + + /// Translates WebAssembly Text format into binary format + pub fn wat2wasm(file_name: &str, src: Vec) -> Result, String> { + WasmAssembler::assemble(file_name, src) + } } pub type WasmResult = Result>; @@ -226,7 +232,7 @@ impl WasmModule { Ok(module) } - pub fn instantiate(mut self, env: &Env) -> Result> { + pub fn instantiate(mut self, env: &ENV) -> Result> { let mut func_idx = 0; for import in &self.imports { match &import.desc { diff --git a/tools/wat2wasm/Cargo.toml b/tools/wat2wasm/Cargo.toml index c58f996..d568946 100644 --- a/tools/wat2wasm/Cargo.toml +++ b/tools/wat2wasm/Cargo.toml @@ -6,4 +6,4 @@ edition = "2024" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -wa_asm = { path = "../../lib/wa_asm" } +libwat2wasm = { path = "../../lib/libwat2wasm" } diff --git a/tools/wat2wasm/src/main.rs b/tools/wat2wasm/src/main.rs index d3e899c..285b269 100644 --- a/tools/wat2wasm/src/main.rs +++ b/tools/wat2wasm/src/main.rs @@ -1,12 +1,12 @@ //! WebAssembly Assembler CLI Frontend +use libwat2wasm::*; use std::{ env::{self, args}, fs::{File, read_to_string}, io::{self, Write}, process, }; -use wa_asm::*; fn usage() -> ! { let mut args = env::args_os(); @@ -54,7 +54,7 @@ fn main() { }; let src = read_to_string(path_input.as_str()).unwrap(); - let binary = match WasmAssembler::to_wasm(path_input.as_str(), src.into_bytes()) { + let binary = match WasmAssembler::assemble(path_input.as_str(), src.into_bytes()) { Ok(v) => v, Err(e) => { panic!("{}", e);