mirror of
https://github.com/exoticorn/microw8.git
synced 2026-01-20 19:26:43 +01:00
add platform module providing implementation of random functions
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
all: loader.wasm
|
||||
all: loader.wasm platform.wasm
|
||||
|
||||
loader.wasm: loader.cwa
|
||||
curlywas loader.cwa
|
||||
|
||||
platform.wasm: platform.cwa
|
||||
curlywas platform.cwa
|
||||
16
platform/platform.cwa
Normal file
16
platform/platform.cwa
Normal file
@@ -0,0 +1,16 @@
|
||||
import "env.memory" memory(4);
|
||||
|
||||
global mut randomState: i32 = 37;
|
||||
|
||||
export fn random() -> i32 {
|
||||
let state: i32;
|
||||
randomState = (state := (
|
||||
state := randomState ^ (randomState << 13)
|
||||
) ^ (state >> 17)
|
||||
) ^ (state << 5);
|
||||
randomState * 625341585
|
||||
}
|
||||
|
||||
export fn randomSeed(s: i32) {
|
||||
randomState = (((s + !(s >> 31)) as i64 * 8445297036689579347i64) >> 31i64) as i32;
|
||||
}
|
||||
@@ -61,6 +61,8 @@ fn main() -> Result<()> {
|
||||
|
||||
let mut loader = Loader::new(&engine)?;
|
||||
|
||||
let platform_module = wasmtime::Module::new(&engine, include_bytes!("../platform/platform.wasm"))?;
|
||||
|
||||
let module = wasmtime::Module::new(&engine, loader.load(&uw8_module)?)?;
|
||||
|
||||
let mut store = wasmtime::Store::new(&engine, ());
|
||||
@@ -93,6 +95,12 @@ fn main() -> Result<()> {
|
||||
)?;
|
||||
}
|
||||
|
||||
let platform_instance = linker.instantiate(&mut store, &platform_module)?;
|
||||
|
||||
for export in platform_instance.exports(&mut store) {
|
||||
linker.define("env", export.name(), export.into_func().expect("platform surely only exports functions"))?;
|
||||
}
|
||||
|
||||
let instance = linker.instantiate(&mut store, &module)?;
|
||||
let tic = instance.get_typed_func::<i32, (), _>(&mut store, "tic")?;
|
||||
|
||||
|
||||
@@ -65,6 +65,9 @@ impl BaseModule {
|
||||
add_function(&mut functions, &type_map, "pow", &[F32, F32], Some(F32));
|
||||
add_function(&mut functions, &type_map, "log", &[F32], Some(F32));
|
||||
|
||||
add_function(&mut functions, &type_map, "random", &[], Some(I32));
|
||||
add_function(&mut functions, &type_map, "randomSeed", &[I32], None);
|
||||
|
||||
for i in functions.len()..64 {
|
||||
add_function(
|
||||
&mut functions,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import loaderUrl from "data-url:../../platform/loader.wasm";
|
||||
import platformUrl from "data-url:../../platform/platform.wasm";
|
||||
import baseUrl from "data-url:../../uw8-tool/base1.wasm";
|
||||
|
||||
async function loadWasm(url, imports) {
|
||||
@@ -96,6 +97,12 @@ async function runModule(data) {
|
||||
importObject.env['g_reserved' + i] = 0;
|
||||
}
|
||||
|
||||
let platform_instance = await loadWasm(platformUrl, importObject);
|
||||
|
||||
for(let name in platform_instance.exports) {
|
||||
importObject.env[name] = platform_instance.exports[name]
|
||||
}
|
||||
|
||||
let instance = new WebAssembly.Instance(await WebAssembly.compile(data), importObject);
|
||||
|
||||
let buffer = imageData.data;
|
||||
|
||||
Reference in New Issue
Block a user