Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
neri committed Dec 27, 2024
1 parent a1d9554 commit 16ff690
Show file tree
Hide file tree
Showing 54 changed files with 20 additions and 32 deletions.
8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,19 @@ 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",
"example/hello",
"tools/wat2wasm",
"lib/wami_macro",
"lib/leb128",
"lib/wa_asm",
"lib/libwat2wasm",
]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
1 change: 0 additions & 1 deletion example/hello/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ version = "0.1.0"

[dependencies]
wami = { path="../../" }
wa_asm = { path = "../../lib/wa_asm" }
3 changes: 1 addition & 2 deletions example/hello/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Hello world
use wa_asm::WasmAssembler;
use wami::prelude::*;

fn main() {
Expand All @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/wa_asm/Cargo.toml → lib/libwat2wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "wa_asm"
name = "libwat2wasm"
version = "0.1.0"
edition = "2024"

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 1 addition & 14 deletions lib/wa_asm/src/lib.rs → lib/libwat2wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>) -> Result<String, String> {
// Self::_from_src(file_name, src, |tokens| ast::AstModule::parse(tokens))
// .map(|v| format!("{:#?}", v))
// }

// pub fn explain_ir(file_name: &str, src: Vec<u8>) -> Result<String, String> {
// 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<u8>) -> Result<Vec<u8>, String> {
pub fn assemble(file_name: &str, src: Vec<u8>) -> Result<Vec<u8>, String> {
Self::_from_src(file_name, src, |tokens: &mut TokenStream<Keyword>| {
let ir_module = ir::Module::from_ast(ast::AstModule::parse(tokens)?)?;

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -98,7 +97,7 @@ fn shared_instance() -> WasmInstance {
static BINARY: OnceLock<Vec<u8>> = 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()
}
Expand Down
12 changes: 9 additions & 3 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use core::slice;
use core::str;
use global::WasmGlobal;
use leb128::*;
use libwat2wasm::WasmAssembler;
use smallvec::SmallVec;

pub struct WebAssembly;
Expand All @@ -40,9 +41,9 @@ impl WebAssembly {
}

/// Instantiate wasm module
pub fn instantiate<Env: WasmEnv>(
pub fn instantiate<ENV: WasmEnv>(
bytes: &[u8],
env: &Env,
env: &ENV,
) -> Result<WasmInstance, Box<dyn Error>> {
Self::compile(bytes)?.instantiate(env)
}
Expand All @@ -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<u8>) -> Result<Vec<u8>, String> {
WasmAssembler::assemble(file_name, src)
}
}

pub type WasmResult<T> = Result<T, Box<dyn Error>>;
Expand Down Expand Up @@ -226,7 +232,7 @@ impl WasmModule {
Ok(module)
}

pub fn instantiate<Env: WasmEnv>(mut self, env: &Env) -> Result<WasmInstance, Box<dyn Error>> {
pub fn instantiate<ENV: WasmEnv>(mut self, env: &ENV) -> Result<WasmInstance, Box<dyn Error>> {
let mut func_idx = 0;
for import in &self.imports {
match &import.desc {
Expand Down
2 changes: 1 addition & 1 deletion tools/wat2wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
4 changes: 2 additions & 2 deletions tools/wat2wasm/src/main.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 16ff690

Please sign in to comment.