lots of stuff
This commit is contained in:
@@ -64,8 +64,8 @@ pub struct Instance {
|
||||
// TODO: set to a random uuid
|
||||
pub self_namespace: Arc<Mutex<Namespace>>,
|
||||
pub op_tx: channel::Sender<Operation>,
|
||||
context: Vec<MizeId>,
|
||||
threads: Vec<String>,
|
||||
threads: Arc<Mutex<Vec<(u32, String)>>>,
|
||||
next_thread_id: Arc<Mutex<u32>>,
|
||||
give_msg_wait: Arc<Mutex<HashMap<MizeId, Vec<Sender<ItemData>>>>>,
|
||||
create_msg_wait: Arc<Mutex<Option<Sender<MizeId>>>>,
|
||||
update_thread_busy: Arc<Mutex<bool>>,
|
||||
@@ -100,8 +100,8 @@ impl Instance {
|
||||
namespace, self_namespace, op_tx,
|
||||
namespace_pool: Arc::new(Mutex::new(namespace_pool_raw)),
|
||||
modules: Arc::new(Mutex::new(HashMap::new())),
|
||||
context: vec![],
|
||||
threads: vec![],
|
||||
threads: Arc::new(Mutex::new(Vec::new())),
|
||||
next_thread_id: Arc::new(Mutex::new(0)),
|
||||
next_con_id: Arc::new(Mutex::new(1)),
|
||||
give_msg_wait, create_msg_wait,
|
||||
update_thread_busy: Arc::new(Mutex::new(false)),
|
||||
@@ -151,16 +151,15 @@ impl Instance {
|
||||
crate::platform::any::instance_init(self);
|
||||
|
||||
// end of platform specific init code
|
||||
self.load_module("Blob")?;
|
||||
|
||||
let mut modules_inner = self.modules.lock()?;
|
||||
|
||||
let module = modules_inner.get_mut("Blob").unwrap();
|
||||
self.load_module("String")?;
|
||||
|
||||
println!("right before segfault");
|
||||
module.init(&self);
|
||||
//let mut modules_inner = self.modules.lock()?;
|
||||
//let module = modules_inner.get_mut("String").unwrap();
|
||||
//println!("right before segfault");
|
||||
//module.init(&self);
|
||||
|
||||
//self.load_module("String")?;
|
||||
|
||||
debug!("INSTANCE INIT DONE");
|
||||
Ok(())
|
||||
@@ -398,8 +397,33 @@ impl Instance {
|
||||
}
|
||||
|
||||
pub fn spawn(&mut self, name: &str, func: impl FnOnce() -> MizeResult<()> + Send + 'static) -> MizeResult<()> {
|
||||
self.threads.push(name.to_owned());
|
||||
thread::spawn(move || func());
|
||||
let threads_inner = self.threads.lock()?;
|
||||
let mut next_thread_id = self.next_thread_id.lock()?;
|
||||
|
||||
threads_inner.push((next_thread_id, name.to_owned()));
|
||||
|
||||
let to_spawn = move || {
|
||||
debug!("spawning thread: {}", name);
|
||||
let my_thread_id = next_thread_id;
|
||||
|
||||
func()?;
|
||||
|
||||
let mut threads_inner = self.threads.lock()?;
|
||||
threads_inner = threads_inner.into_iter().filter(|el| match el {
|
||||
(my_thread_id, _) => false,
|
||||
(_, _) => true,
|
||||
});
|
||||
debug!("thread '{}' stopped", name);
|
||||
};
|
||||
|
||||
*next_thread_id += 1;
|
||||
|
||||
#[cfg(feature = "os-target")]
|
||||
thread::spawn(move || to_spawn());
|
||||
|
||||
#[cfg(feature = "wasm-target")]
|
||||
crate::platform::wasm::wasm_spawn(move || to_spawn())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
|
||||
#![ allow( warnings ) ]
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() {
|
||||
println!("hello from main fn");
|
||||
}
|
||||
|
||||
static PROTO_VERSION: u8 = 1;
|
||||
|
||||
// the core part has the code that can run on any platform
|
||||
@@ -43,6 +48,9 @@ pub mod platform {
|
||||
#[cfg(feature = "os-target")]
|
||||
pub use super::os::load_module;
|
||||
|
||||
#[cfg(feature = "wasm-target")]
|
||||
pub use super::super::load_module;
|
||||
|
||||
#[cfg(not(any(feature = "os-target", feature = "wasm-target")))]
|
||||
pub use super::super::load_module;
|
||||
|
||||
|
||||
@@ -1,9 +1,25 @@
|
||||
mod utils;
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
use std::ptr::NonNull;
|
||||
use std::panic;
|
||||
|
||||
use crate::instance::Instance;
|
||||
use crate::instance::{self, Instance};
|
||||
use crate::error::MizeResult;
|
||||
use crate::Module;
|
||||
|
||||
|
||||
// console_log macro
|
||||
#[wasm_bindgen]
|
||||
extern "C" {
|
||||
#[wasm_bindgen(js_namespace = console)]
|
||||
fn log(s: &str);
|
||||
}
|
||||
macro_rules! console_log {
|
||||
// Note that this is using the `log` function imported above during
|
||||
// `bare_bones`
|
||||
($($t:tt)*) => (log(&format_args!($($t)*).to_string()))
|
||||
}
|
||||
//end of console_log macro
|
||||
|
||||
|
||||
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
|
||||
// allocator.
|
||||
@@ -11,19 +27,71 @@ use crate::error::MizeResult;
|
||||
#[global_allocator]
|
||||
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
||||
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
fn alert(s: &str);
|
||||
|
||||
// A function imitating `std::thread::spawn`.
|
||||
// thanks to: https://www.tweag.io/blog/2022-11-24-wasm-threads-and-messages/
|
||||
pub fn wasm_spawn(f: impl FnOnce() + Send + 'static) -> MizeResult<()> {
|
||||
let worker = web_sys::Worker::new("./worker.js")?;
|
||||
// Double-boxing because `dyn FnOnce` is unsized and so `Box<dyn FnOnce()>` is a fat pointer.
|
||||
// But `Box<Box<dyn FnOnce()>>` is just a plain pointer, and since wasm has 32-bit pointers,
|
||||
// we can cast it to a `u32` and back.
|
||||
let ptr = Box::into_raw(Box::new(Box::new(f) as Box<dyn FnOnce()>));
|
||||
let msg = js_sys::Array::new();
|
||||
// Send the worker a reference to our memory chunk, so it can initialize a wasm module
|
||||
// using the same memory.
|
||||
msg.push(&wasm_bindgen::memory());
|
||||
// Also send the worker the address of the closure we want to execute.
|
||||
msg.push(&JsValue::from(ptr as u32));
|
||||
worker.post_message(&msg);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn greet() {
|
||||
alert("Hello, wasm-game-of-life!");
|
||||
// This function is here for `worker.js` to call.
|
||||
pub fn worker_entry_point(addr: u32) {
|
||||
// Interpret the address we were given as a pointer to a closure to call.
|
||||
let closure = unsafe { Box::from_raw(ptr as *mut Box<dyn FnOnce()>) };
|
||||
(*closure)();
|
||||
}
|
||||
|
||||
|
||||
pub fn wasm_instance_init(instance: &mut Instance) -> MizeResult<()> {
|
||||
println!("Hello world from wasm_instance_init!!!!!!!!!!");
|
||||
alert("Hello world from wasm_instance_init!!!!!!!!!!");
|
||||
console_log!("Hello world from wasm_instance_init!!!!!!!!!!");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct JsInstance {
|
||||
inner: NonNull<Instance>,
|
||||
}
|
||||
|
||||
/*
|
||||
#[wasm_bindgen]
|
||||
pub struct JsModule {
|
||||
inner: NonNull<Box<dyn Module + Send + Sync>>,
|
||||
//inner: *mut Mme,
|
||||
}
|
||||
*/
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl JsInstance {
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new() -> JsInstance {
|
||||
panic::set_hook(Box::new(console_error_panic_hook::hook));
|
||||
|
||||
let instance = match Instance::new() {
|
||||
Ok(val) => val,
|
||||
Err(e) => {
|
||||
console_log!("Instance::new() failed with: {:?}", e);
|
||||
panic!()
|
||||
},
|
||||
};
|
||||
let mut js_instance = JsInstance { inner: NonNull::from(Box::leak(Box::new(instance))) };
|
||||
return js_instance;
|
||||
}
|
||||
|
||||
//#[wasm_bindgen]
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user