monorepo/kifu/ffi/wasm/src/lib.rs

53 lines
1.1 KiB
Rust
Raw Normal View History

use wasm_bindgen::prelude::*;
2023-05-11 13:39:31 +00:00
// use serde::{Serialize, Deserialize};
use kifu_core::{CoreRequest, CoreResponse};
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
pub fn log(s: &str);
}
2023-04-21 13:25:21 +00:00
/*
#[wasm_bindgen]
2023-05-11 13:39:31 +00:00
#[derive(Serialize, Deserialize)]
2023-04-21 13:25:21 +00:00
pub struct CoreRequest(kifu_core::Request);
2023-05-11 13:39:31 +00:00
*/
2023-05-11 13:39:31 +00:00
/*
#[wasm_bindgen]
2023-04-21 13:25:21 +00:00
impl CoreRequest {
#[wasm_bindgen(constructor)]
pub fn new(request: kifu_core::Request) -> Self {
Self(request)
}
}
*/
2023-04-21 13:25:21 +00:00
/* Somehow uncommenting this code actually causes the module to not load. Maybe a name conflict?
* Don't know.
*/
2023-05-11 13:39:31 +00:00
/*
#[wasm_bindgen]
2023-05-11 13:39:31 +00:00
#[derive(Serialize, Deserialize)]
2023-04-21 13:25:21 +00:00
pub struct CoreResponse(kifu_core::Response);
2023-05-11 13:39:31 +00:00
*/
#[wasm_bindgen]
2023-04-21 13:25:21 +00:00
#[derive(Debug)]
2023-05-11 13:39:31 +00:00
pub struct CoreApp(kifu_core::CoreApp);
#[wasm_bindgen]
2023-04-21 13:25:21 +00:00
impl CoreApp {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
2023-05-11 13:39:31 +00:00
Self(kifu_core::CoreApp::new())
2023-04-21 13:25:21 +00:00
}
#[wasm_bindgen]
2023-05-11 13:39:31 +00:00
pub async fn dispatch(&self, param: &JsValue) -> JsValue {
let request: CoreRequest = serde_wasm_bindgen::from_value(param.clone()).unwrap();
serde_wasm_bindgen::to_value(&self.0.dispatch(request).await).unwrap()
2023-04-21 13:25:21 +00:00
}
}