mirror of
https://github.com/fluencelabs/wasmer
synced 2025-05-10 17:47:14 +00:00
Refactored emscripten integratoin
This commit is contained in:
parent
219bd68256
commit
d56da9c80c
@ -1,53 +0,0 @@
|
|||||||
use crate::webassembly::{ImportObject, Instance};
|
|
||||||
use libc::{printf, putchar};
|
|
||||||
|
|
||||||
extern "C" fn _printf(memory_offset: i32, extra: i32, instance: &Instance) -> i32 {
|
|
||||||
let mem = &instance.memories[0];
|
|
||||||
return unsafe {
|
|
||||||
let base_memory_offset = mem.mmap.as_ptr().offset(memory_offset as isize) as *const i8;
|
|
||||||
printf(base_memory_offset, extra)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn generate_libc_env<'a, 'b>() -> ImportObject<&'a str, &'b str> {
|
|
||||||
let mut import_object = ImportObject::new();
|
|
||||||
import_object.set("env", "printf", _printf as *const u8);
|
|
||||||
import_object.set("env", "putchar", putchar as *const u8);
|
|
||||||
import_object
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::generate_libc_env;
|
|
||||||
use crate::webassembly::{instantiate, Export, Instance};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_putchar() {
|
|
||||||
let wasm_bytes = include_wast2wasm_bytes!("tests/putchar.wast");
|
|
||||||
let import_object = generate_libc_env();
|
|
||||||
let result_object = instantiate(wasm_bytes, import_object).expect("Not compiled properly");
|
|
||||||
let module = result_object.module;
|
|
||||||
let instance = result_object.instance;
|
|
||||||
let func_index = match module.info.exports.get("main") {
|
|
||||||
Some(&Export::Function(index)) => index,
|
|
||||||
_ => panic!("Function not found"),
|
|
||||||
};
|
|
||||||
let main: fn(&Instance) = get_instance_function!(instance, func_index);
|
|
||||||
main(&instance);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_print() {
|
|
||||||
let wasm_bytes = include_wast2wasm_bytes!("tests/printf.wast");
|
|
||||||
let import_object = generate_libc_env();
|
|
||||||
let result_object = instantiate(wasm_bytes, import_object).expect("Not compiled properly");
|
|
||||||
let module = result_object.module;
|
|
||||||
let instance = result_object.instance;
|
|
||||||
let func_index = match module.info.exports.get("main") {
|
|
||||||
Some(&Export::Function(index)) => index,
|
|
||||||
_ => panic!("Function not found"),
|
|
||||||
};
|
|
||||||
let main: fn(&Instance) = get_instance_function!(instance, func_index);
|
|
||||||
main(&instance);
|
|
||||||
}
|
|
||||||
}
|
|
43
src/linkers/emscripten/mod.rs
Normal file
43
src/linkers/emscripten/mod.rs
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
use crate::webassembly::ImportObject;
|
||||||
|
|
||||||
|
mod printf;
|
||||||
|
mod putchar;
|
||||||
|
|
||||||
|
pub fn generate_emscripten_env<'a, 'b>() -> ImportObject<&'a str, &'b str> {
|
||||||
|
let mut import_object = ImportObject::new();
|
||||||
|
import_object.set("env", "printf", printf::printf as *const u8);
|
||||||
|
import_object.set("env", "putchar", putchar::putchar as *const u8);
|
||||||
|
import_object
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::generate_emscripten_env;
|
||||||
|
use crate::webassembly::{instantiate, Export, Instance};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_putchar() {
|
||||||
|
let wasm_bytes = include_wast2wasm_bytes!("tests/putchar.wast");
|
||||||
|
let import_object = generate_emscripten_env();
|
||||||
|
let result_object = instantiate(wasm_bytes, import_object).expect("Not compiled properly");
|
||||||
|
let func_index = match result_object.module.info.exports.get("main") {
|
||||||
|
Some(&Export::Function(index)) => index,
|
||||||
|
_ => panic!("Function not found"),
|
||||||
|
};
|
||||||
|
let main: fn(&Instance) = get_instance_function!(result_object.instance, func_index);
|
||||||
|
main(&result_object.instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_print() {
|
||||||
|
let wasm_bytes = include_wast2wasm_bytes!("tests/printf.wast");
|
||||||
|
let import_object = generate_emscripten_env();
|
||||||
|
let result_object = instantiate(wasm_bytes, import_object).expect("Not compiled properly");
|
||||||
|
let func_index = match result_object.module.info.exports.get("main") {
|
||||||
|
Some(&Export::Function(index)) => index,
|
||||||
|
_ => panic!("Function not found"),
|
||||||
|
};
|
||||||
|
let main: fn(&Instance) = get_instance_function!(result_object.instance, func_index);
|
||||||
|
main(&result_object.instance);
|
||||||
|
}
|
||||||
|
}
|
11
src/linkers/emscripten/printf.rs
Normal file
11
src/linkers/emscripten/printf.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
use libc::printf as _printf;
|
||||||
|
|
||||||
|
use crate::webassembly::Instance;
|
||||||
|
|
||||||
|
pub extern "C" fn printf(memory_offset: i32, extra: i32, instance: &Instance) -> i32 {
|
||||||
|
let mem = &instance.memories[0];
|
||||||
|
return unsafe {
|
||||||
|
let base_memory_offset = mem.mmap.as_ptr().offset(memory_offset as isize) as *const i8;
|
||||||
|
_printf(base_memory_offset, extra)
|
||||||
|
};
|
||||||
|
}
|
1
src/linkers/emscripten/putchar.rs
Normal file
1
src/linkers/emscripten/putchar.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub use libc::putchar;
|
4
src/linkers/mod.rs
Normal file
4
src/linkers/mod.rs
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
pub mod emscripten;
|
||||||
|
|
||||||
|
pub use self::emscripten::generate_emscripten_env;
|
@ -37,7 +37,7 @@ use wabt::wat2wasm;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod macros;
|
mod macros;
|
||||||
pub mod common;
|
pub mod common;
|
||||||
pub mod integrations;
|
pub mod linkers;
|
||||||
pub mod sighandler;
|
pub mod sighandler;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod spectests;
|
mod spectests;
|
||||||
@ -77,7 +77,7 @@ fn execute_wasm(wasm_path: PathBuf) -> Result<(), String> {
|
|||||||
wasm_binary = wat2wasm(wasm_binary).map_err(|err| String::from(err.description()))?;
|
wasm_binary = wat2wasm(wasm_binary).map_err(|err| String::from(err.description()))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let import_object = integrations::generate_libc_env();
|
let import_object = linkers::generate_emscripten_env();
|
||||||
let webassembly::ResultObject { module, instance } =
|
let webassembly::ResultObject { module, instance } =
|
||||||
webassembly::instantiate(wasm_binary, import_object)
|
webassembly::instantiate(wasm_binary, import_object)
|
||||||
.map_err(|err| format!("{}", err))?;
|
.map_err(|err| format!("{}", err))?;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user