mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-06 04:38:25 +00:00
Added WebAssembly Module converter
This commit is contained in:
@@ -40,7 +40,7 @@ wasm-bindgen-test = "0.3.0"
|
|||||||
maintenance = { status = "actively-developed" }
|
maintenance = { status = "actively-developed" }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["std", "wasm-types-polyfill"]
|
default = ["std", "wasm-types-polyfill", "wat"]
|
||||||
wasm-types-polyfill = ["wasmparser"]
|
wasm-types-polyfill = ["wasmparser"]
|
||||||
std = []
|
std = []
|
||||||
core = []
|
core = []
|
||||||
|
|||||||
@@ -506,3 +506,14 @@ impl fmt::Debug for Module {
|
|||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<WebAssembly::Module> for Module {
|
||||||
|
fn from(module: WebAssembly::Module) -> Module {
|
||||||
|
Module {
|
||||||
|
store: Store::default(),
|
||||||
|
module,
|
||||||
|
name: None,
|
||||||
|
type_hints: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -86,13 +86,6 @@ impl<T: Copy, Ty> WasmPtr<T, Ty> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn align_pointer(ptr: usize, align: usize) -> usize {
|
|
||||||
// clears bits below aligment amount (assumes power of 2) to align pointer
|
|
||||||
debug_assert!(align.count_ones() == 1);
|
|
||||||
ptr & !(align - 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Methods for `WasmPtr`s to data that can be dereferenced, namely to types
|
/// Methods for `WasmPtr`s to data that can be dereferenced, namely to types
|
||||||
/// that implement [`ValueType`], meaning that they're valid for all possible
|
/// that implement [`ValueType`], meaning that they're valid for all possible
|
||||||
/// bit patterns.
|
/// bit patterns.
|
||||||
@@ -109,8 +102,7 @@ impl<T: Copy + ValueType> WasmPtr<T, Item> {
|
|||||||
if total_len > memory.size().bytes().0 || mem::size_of::<T>() == 0 {
|
if total_len > memory.size().bytes().0 || mem::size_of::<T>() == 0 {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let offset = align_pointer(self.offset as usize, mem::align_of::<T>()) as u32;
|
let subarray = memory.uint8view().subarray(self.offset, total_len as u32);
|
||||||
let subarray = memory.uint8view().subarray(offset, total_len as u32);
|
|
||||||
Some(WasmCell::new(subarray))
|
Some(WasmCell::new(subarray))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -140,14 +132,13 @@ impl<T: Copy + ValueType> WasmPtr<T, Array> {
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let offset = align_pointer(self.offset as usize, mem::align_of::<T>()) as u32;
|
|
||||||
|
|
||||||
Some(
|
Some(
|
||||||
(0..length)
|
(0..length)
|
||||||
.map(|i| {
|
.map(|i| {
|
||||||
let subarray = memory
|
let subarray = memory.uint8view().subarray(
|
||||||
.uint8view()
|
self.offset + i * item_size,
|
||||||
.subarray(offset + i * item_size, offset + (i + 1) * item_size);
|
self.offset + (i + 1) * item_size,
|
||||||
|
);
|
||||||
WasmCell::new(subarray)
|
WasmCell::new(subarray)
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>(),
|
.collect::<Vec<_>>(),
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use js_sys::{Uint8Array, WebAssembly};
|
||||||
use wasm_bindgen_test::*;
|
use wasm_bindgen_test::*;
|
||||||
use wasmer_js::*;
|
use wasmer_js::*;
|
||||||
|
|
||||||
@@ -22,6 +23,16 @@ fn module_set_name() {
|
|||||||
assert_eq!(module.name(), Some("new_name"));
|
assert_eq!(module.name(), Some("new_name"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen_test]
|
||||||
|
fn module_from_jsmodule() {
|
||||||
|
let wat = br#"(module $name)"#;
|
||||||
|
let binary = wat2wasm(wat).unwrap();
|
||||||
|
let js_bytes = unsafe { Uint8Array::view(&binary) };
|
||||||
|
let js_module = WebAssembly::Module::new(&js_bytes.into()).unwrap();
|
||||||
|
let module: Module = js_module.into();
|
||||||
|
assert_eq!(module.store(), &Store::default());
|
||||||
|
}
|
||||||
|
|
||||||
#[wasm_bindgen_test]
|
#[wasm_bindgen_test]
|
||||||
fn imports() {
|
fn imports() {
|
||||||
let store = Store::default();
|
let store = Store::default();
|
||||||
|
|||||||
Reference in New Issue
Block a user