diff --git a/veilid-flutter/lib/veilid_js.dart b/veilid-flutter/lib/veilid_js.dart index de88ccf9..fd30314a 100644 --- a/veilid-flutter/lib/veilid_js.dart +++ b/veilid-flutter/lib/veilid_js.dart @@ -259,8 +259,8 @@ class VeilidJS implements Veilid { @override Future routingContext() async { - int id = jsonDecode( - await _wrapApiPromise(js_util.callMethod(wasm, "routing_context", []))); + int id = + await _wrapApiPromise(js_util.callMethod(wasm, "routing_context", [])); return VeilidRoutingContextJS._(_Ctx(id, this)); } @@ -307,8 +307,10 @@ class VeilidJS implements Veilid { @override Future openTableDB(String name, int columnCount) async { + print('shit'); int id = await _wrapApiPromise( js_util.callMethod(wasm, "open_table_db", [name, columnCount])); + print('qwerqwer'); return VeilidTableDBJS._(_TDB(id, this)); } @@ -319,8 +321,7 @@ class VeilidJS implements Veilid { @override Future debug(String command) async { - return jsonDecode( - await _wrapApiPromise(js_util.callMethod(wasm, "debug", [command]))); + return await _wrapApiPromise(js_util.callMethod(wasm, "debug", [command])); } @override diff --git a/veilid-flutter/rust/src/dart_ffi.rs b/veilid-flutter/rust/src/dart_ffi.rs index 695e283b..182824a2 100644 --- a/veilid-flutter/rust/src/dart_ffi.rs +++ b/veilid-flutter/rust/src/dart_ffi.rs @@ -257,7 +257,7 @@ pub extern "C" fn change_log_level(layer: FfiStr, log_level: FfiStr) { pub extern "C" fn startup_veilid_core(port: i64, stream_port: i64, config: FfiStr) { let config = config.into_opt_string(); let stream = DartIsolateStream::new(stream_port); - DartIsolateWrapper::new(port).spawn_result_json(async move { + DartIsolateWrapper::new(port).spawn_result(async move { let config_json = match config { Some(v) => v, None => { @@ -305,7 +305,7 @@ pub extern "C" fn get_veilid_state(port: i64) { #[no_mangle] pub extern "C" fn attach(port: i64) { - DartIsolateWrapper::new(port).spawn_result_json(async move { + DartIsolateWrapper::new(port).spawn_result(async move { let veilid_api = get_veilid_api().await?; veilid_api.attach().await?; APIRESULT_VOID @@ -314,7 +314,7 @@ pub extern "C" fn attach(port: i64) { #[no_mangle] pub extern "C" fn detach(port: i64) { - DartIsolateWrapper::new(port).spawn_result_json(async move { + DartIsolateWrapper::new(port).spawn_result(async move { let veilid_api = get_veilid_api().await?; veilid_api.detach().await?; APIRESULT_VOID @@ -324,7 +324,7 @@ pub extern "C" fn detach(port: i64) { #[no_mangle] #[instrument] pub extern "C" fn shutdown_veilid_core(port: i64) { - DartIsolateWrapper::new(port).spawn_result_json(async move { + DartIsolateWrapper::new(port).spawn_result(async move { let veilid_api = take_veilid_api().await?; veilid_api.shutdown().await; APIRESULT_VOID @@ -414,7 +414,7 @@ pub extern "C" fn routing_context_app_call(port: i64, id: u32, target: FfiStr, r .as_bytes(), ) .unwrap(); - DartIsolateWrapper::new(port).spawn_result_json(async move { + DartIsolateWrapper::new(port).spawn_result(async move { let veilid_api = get_veilid_api().await?; let routing_table = veilid_api.routing_table()?; let rss = routing_table.route_spec_store(); @@ -450,7 +450,7 @@ pub extern "C" fn routing_context_app_message(port: i64, id: u32, target: FfiStr .as_bytes(), ) .unwrap(); - DartIsolateWrapper::new(port).spawn_result_json(async move { + DartIsolateWrapper::new(port).spawn_result(async move { let veilid_api = get_veilid_api().await?; let routing_table = veilid_api.routing_table()?; let rss = routing_table.route_spec_store(); @@ -529,7 +529,7 @@ pub extern "C" fn import_remote_private_route(port: i64, blob: FfiStr) { pub extern "C" fn release_private_route(port: i64, key: FfiStr) { let key: veilid_core::DHTKey = veilid_core::deserialize_opt_json(key.into_opt_string()).unwrap(); - DartIsolateWrapper::new(port).spawn_result_json(async move { + DartIsolateWrapper::new(port).spawn_result(async move { let veilid_api = get_veilid_api().await?; veilid_api.release_private_route(&key)?; APIRESULT_VOID @@ -657,7 +657,7 @@ pub extern "C" fn release_table_db_transaction(id: u32) -> i32 { #[no_mangle] pub extern "C" fn table_db_transaction_commit(port: i64, id: u32) { - DartIsolateWrapper::new(port).spawn_result_json(async move { + DartIsolateWrapper::new(port).spawn_result(async move { let tdbt = { let tdbts = TABLE_DB_TRANSACTIONS.lock(); let Some(tdbt) = tdbts.get(&id) else { @@ -672,7 +672,7 @@ pub extern "C" fn table_db_transaction_commit(port: i64, id: u32) { } #[no_mangle] pub extern "C" fn table_db_transaction_rollback(port: i64, id: u32) { - DartIsolateWrapper::new(port).spawn_result_json(async move { + DartIsolateWrapper::new(port).spawn_result(async move { let tdbt = { let tdbts = TABLE_DB_TRANSACTIONS.lock(); let Some(tdbt) = tdbts.get(&id) else { @@ -702,7 +702,7 @@ pub extern "C" fn table_db_transaction_store(port: i64, id: u32, col: u32, key: .as_bytes(), ) .unwrap(); - DartIsolateWrapper::new(port).spawn_result_json(async move { + DartIsolateWrapper::new(port).spawn_result(async move { let tdbt = { let tdbts = TABLE_DB_TRANSACTIONS.lock(); let Some(tdbt) = tdbts.get(&id) else { @@ -726,7 +726,7 @@ pub extern "C" fn table_db_transaction_delete(port: i64, id: u32, col: u32, key: .as_bytes(), ) .unwrap(); - DartIsolateWrapper::new(port).spawn_result_json(async move { + DartIsolateWrapper::new(port).spawn_result(async move { let tdbt = { let tdbts = TABLE_DB_TRANSACTIONS.lock(); let Some(tdbt) = tdbts.get(&id) else { @@ -756,7 +756,7 @@ pub extern "C" fn table_db_store(port: i64, id: u32, col: u32, key: FfiStr, valu .as_bytes(), ) .unwrap(); - DartIsolateWrapper::new(port).spawn_result_json(async move { + DartIsolateWrapper::new(port).spawn_result(async move { let table_db = { let table_dbs = TABLE_DBS.lock(); let Some(table_db) = table_dbs.get(&id) else { @@ -802,7 +802,7 @@ pub extern "C" fn table_db_delete(port: i64, id: u32, col: u32, key: FfiStr) { .as_bytes(), ) .unwrap(); - DartIsolateWrapper::new(port).spawn_result_json(async move { + DartIsolateWrapper::new(port).spawn_result(async move { let table_db = { let table_dbs = TABLE_DBS.lock(); let Some(table_db) = table_dbs.get(&id) else { diff --git a/veilid-wasm/src/lib.rs b/veilid-wasm/src/lib.rs index dc85edd8..cff5c174 100644 --- a/veilid-wasm/src/lib.rs +++ b/veilid-wasm/src/lib.rs @@ -7,7 +7,6 @@ extern crate alloc; use alloc::string::String; use alloc::sync::Arc; use alloc::*; -use core::any::{Any, TypeId}; use core::cell::RefCell; use core::fmt::Debug; use futures_util::FutureExt; @@ -61,6 +60,13 @@ pub fn to_json(val: T) -> JsValue { JsValue::from_str(&serialize_json(val)) } +pub fn to_jsvalue(val: T) -> JsValue +where + JsValue: From, +{ + JsValue::from(val) +} + pub fn from_json( val: JsValue, ) -> Result { @@ -77,21 +83,28 @@ pub fn from_json( type APIResult = Result; const APIRESULT_UNDEFINED: APIResult<()> = APIResult::Ok(()); -pub fn wrap_api_future(future: F) -> Promise +pub fn wrap_api_future_json(future: F) -> Promise where F: Future> + 'static, T: Serialize + Debug + 'static, { - future_to_promise(future.map(|res| { - res.map(|v| { - if TypeId::of::<()>() == v.type_id() { - JsValue::UNDEFINED - } else { - to_json(v) - } - }) - .map_err(|e| to_json(e)) - })) + future_to_promise(future.map(|res| res.map(|v| to_json(v)).map_err(|e| to_json(e)))) +} + +pub fn wrap_api_future_plain(future: F) -> Promise +where + F: Future> + 'static, + JsValue: From, + T: 'static, +{ + future_to_promise(future.map(|res| res.map(|v| to_jsvalue(v)).map_err(|e| to_json(e)))) +} + +pub fn wrap_api_future_void(future: F) -> Promise +where + F: Future> + 'static, +{ + future_to_promise(future.map(|res| res.map(|_| JsValue::UNDEFINED).map_err(|e| to_json(e)))) } ///////////////////////////////////////// @@ -206,7 +219,7 @@ pub fn change_log_level(layer: String, log_level: String) { #[wasm_bindgen()] pub fn startup_veilid_core(update_callback_js: Function, json_config: String) -> Promise { let update_callback_js = SendWrapper::new(update_callback_js); - wrap_api_future(async move { + wrap_api_future_void(async move { let update_callback = Arc::new(move |update: VeilidUpdate| { let _ret = match Function::call1(&update_callback_js, &JsValue::UNDEFINED, &to_json(update)) { @@ -230,16 +243,16 @@ pub fn startup_veilid_core(update_callback_js: Function, json_config: String) -> #[wasm_bindgen()] pub fn get_veilid_state() -> Promise { - wrap_api_future(async move { + wrap_api_future_json(async move { let veilid_api = get_veilid_api()?; let core_state = veilid_api.get_state().await?; - Ok(core_state) + APIResult::Ok(core_state) }) } #[wasm_bindgen()] pub fn attach() -> Promise { - wrap_api_future(async move { + wrap_api_future_void(async move { let veilid_api = get_veilid_api()?; veilid_api.attach().await?; APIRESULT_UNDEFINED @@ -248,7 +261,7 @@ pub fn attach() -> Promise { #[wasm_bindgen()] pub fn detach() -> Promise { - wrap_api_future(async move { + wrap_api_future_void(async move { let veilid_api = get_veilid_api()?; veilid_api.detach().await?; APIRESULT_UNDEFINED @@ -257,7 +270,7 @@ pub fn detach() -> Promise { #[wasm_bindgen()] pub fn shutdown_veilid_core() -> Promise { - wrap_api_future(async move { + wrap_api_future_void(async move { let veilid_api = take_veilid_api()?; veilid_api.shutdown().await; APIRESULT_UNDEFINED @@ -276,7 +289,7 @@ fn add_routing_context(routing_context: veilid_core::RoutingContext) -> u32 { #[wasm_bindgen()] pub fn routing_context() -> Promise { - wrap_api_future(async move { + wrap_api_future_plain(async move { let veilid_api = get_veilid_api()?; let routing_context = veilid_api.routing_context(); let new_id = add_routing_context(routing_context); @@ -339,7 +352,7 @@ pub fn routing_context_app_call(id: u32, target: String, request: String) -> Pro let request: Vec = data_encoding::BASE64URL_NOPAD .decode(request.as_bytes()) .unwrap(); - wrap_api_future(async move { + wrap_api_future_plain(async move { let veilid_api = get_veilid_api()?; let routing_table = veilid_api.routing_table()?; let rss = routing_table.route_spec_store(); @@ -372,7 +385,7 @@ pub fn routing_context_app_message(id: u32, target: String, message: String) -> let message: Vec = data_encoding::BASE64URL_NOPAD .decode(message.as_bytes()) .unwrap(); - wrap_api_future(async move { + wrap_api_future_void(async move { let veilid_api = get_veilid_api()?; let routing_table = veilid_api.routing_table()?; let rss = routing_table.route_spec_store(); @@ -401,7 +414,7 @@ pub fn routing_context_app_message(id: u32, target: String, message: String) -> #[wasm_bindgen()] pub fn new_private_route() -> Promise { - wrap_api_future(async move { + wrap_api_future_json(async move { let veilid_api = get_veilid_api()?; let (key, blob) = veilid_api.new_private_route().await?; @@ -417,7 +430,7 @@ pub fn new_custom_private_route(stability: String, sequencing: String) -> Promis let stability: veilid_core::Stability = veilid_core::deserialize_json(&stability).unwrap(); let sequencing: veilid_core::Sequencing = veilid_core::deserialize_json(&sequencing).unwrap(); - wrap_api_future(async move { + wrap_api_future_json(async move { let veilid_api = get_veilid_api()?; let (key, blob) = veilid_api @@ -435,7 +448,7 @@ pub fn import_remote_private_route(blob: String) -> Promise { let blob: Vec = data_encoding::BASE64URL_NOPAD .decode(blob.as_bytes()) .unwrap(); - wrap_api_future(async move { + wrap_api_future_plain(async move { let veilid_api = get_veilid_api()?; let key = veilid_api.import_remote_private_route(blob)?; @@ -447,7 +460,7 @@ pub fn import_remote_private_route(blob: String) -> Promise { #[wasm_bindgen()] pub fn release_private_route(key: String) -> Promise { let key: veilid_core::DHTKey = veilid_core::deserialize_json(&key).unwrap(); - wrap_api_future(async move { + wrap_api_future_void(async move { let veilid_api = get_veilid_api()?; veilid_api.release_private_route(&key)?; APIRESULT_UNDEFINED @@ -459,7 +472,7 @@ pub fn app_call_reply(id: String, message: String) -> Promise { let message: Vec = data_encoding::BASE64URL_NOPAD .decode(message.as_bytes()) .unwrap(); - wrap_api_future(async move { + wrap_api_future_void(async move { let id = match id.parse() { Ok(v) => v, Err(e) => { @@ -467,8 +480,8 @@ pub fn app_call_reply(id: String, message: String) -> Promise { } }; let veilid_api = get_veilid_api()?; - let out = veilid_api.app_call_reply(id, message).await?; - Ok(out) + veilid_api.app_call_reply(id, message).await?; + APIRESULT_UNDEFINED }) } @@ -479,12 +492,13 @@ fn add_table_db(table_db: veilid_core::TableDB) -> u32 { next_id += 1; } tdbs.insert(next_id, table_db); + console_log(&format!("tdb added: {}", next_id)); next_id } #[wasm_bindgen()] pub fn open_table_db(name: String, column_count: u32) -> Promise { - wrap_api_future(async move { + wrap_api_future_plain(async move { let veilid_api = get_veilid_api()?; let tstore = veilid_api.table_store()?; let table_db = tstore @@ -492,12 +506,13 @@ pub fn open_table_db(name: String, column_count: u32) -> Promise { .await .map_err(veilid_core::VeilidAPIError::generic)?; let new_id = add_table_db(table_db); - Ok(new_id) + APIResult::Ok(new_id) }) } #[wasm_bindgen()] pub fn release_table_db(id: u32) -> i32 { + console_log(&format!("tdb released: {}", id)); let mut tdbs = (*TABLE_DBS).borrow_mut(); if tdbs.remove(&id).is_none() { return 0; @@ -507,14 +522,14 @@ pub fn release_table_db(id: u32) -> i32 { #[wasm_bindgen()] pub fn delete_table_db(name: String) -> Promise { - wrap_api_future(async move { + wrap_api_future_plain(async move { let veilid_api = get_veilid_api()?; let tstore = veilid_api.table_store()?; let deleted = tstore .delete(&name) .await .map_err(veilid_core::VeilidAPIError::generic)?; - Ok(deleted) + APIResult::Ok(deleted) }) } @@ -579,7 +594,7 @@ pub fn release_table_db_transaction(id: u32) -> i32 { #[wasm_bindgen()] pub fn table_db_transaction_commit(id: u32) -> Promise { - wrap_api_future(async move { + wrap_api_future_void(async move { let tdbt = { let tdbts = (*TABLE_DB_TRANSACTIONS).borrow(); let Some(tdbt) = tdbts.get(&id) else { @@ -597,7 +612,7 @@ pub fn table_db_transaction_commit(id: u32) -> Promise { #[wasm_bindgen()] pub fn table_db_transaction_rollback(id: u32) -> Promise { - wrap_api_future(async move { + wrap_api_future_void(async move { let tdbt = { let tdbts = (*TABLE_DB_TRANSACTIONS).borrow(); let Some(tdbt) = tdbts.get(&id) else { @@ -619,7 +634,7 @@ pub fn table_db_transaction_store(id: u32, col: u32, key: String, value: String) let value: Vec = data_encoding::BASE64URL_NOPAD .decode(value.as_bytes()) .unwrap(); - wrap_api_future(async move { + wrap_api_future_void(async move { let tdbt = { let tdbts = (*TABLE_DB_TRANSACTIONS).borrow(); let Some(tdbt) = tdbts.get(&id) else { @@ -638,7 +653,7 @@ pub fn table_db_transaction_delete(id: u32, col: u32, key: String) -> Promise { let key: Vec = data_encoding::BASE64URL_NOPAD .decode(key.as_bytes()) .unwrap(); - wrap_api_future(async move { + wrap_api_future_void(async move { let tdbt = { let tdbts = (*TABLE_DB_TRANSACTIONS).borrow(); let Some(tdbt) = tdbts.get(&id) else { @@ -647,8 +662,8 @@ pub fn table_db_transaction_delete(id: u32, col: u32, key: String) -> Promise { tdbt.clone() }; - let out = tdbt.delete(col, &key); - APIResult::Ok(out) + tdbt.delete(col, &key); + APIRESULT_UNDEFINED }) } @@ -660,7 +675,7 @@ pub fn table_db_store(id: u32, col: u32, key: String, value: String) -> Promise let value: Vec = data_encoding::BASE64URL_NOPAD .decode(value.as_bytes()) .unwrap(); - wrap_api_future(async move { + wrap_api_future_void(async move { let table_db = { let table_dbs = (*TABLE_DBS).borrow(); let Some(table_db) = table_dbs.get(&id) else { @@ -682,7 +697,7 @@ pub fn table_db_load(id: u32, col: u32, key: String) -> Promise { let key: Vec = data_encoding::BASE64URL_NOPAD .decode(key.as_bytes()) .unwrap(); - wrap_api_future(async move { + wrap_api_future_plain(async move { let table_db = { let table_dbs = (*TABLE_DBS).borrow(); let Some(table_db) = table_dbs.get(&id) else { @@ -704,7 +719,7 @@ pub fn table_db_delete(id: u32, col: u32, key: String) -> Promise { let key: Vec = data_encoding::BASE64URL_NOPAD .decode(key.as_bytes()) .unwrap(); - wrap_api_future(async move { + wrap_api_future_plain(async move { let table_db = { let table_dbs = (*TABLE_DBS).borrow(); let Some(table_db) = table_dbs.get(&id) else { @@ -723,10 +738,10 @@ pub fn table_db_delete(id: u32, col: u32, key: String) -> Promise { #[wasm_bindgen()] pub fn debug(command: String) -> Promise { - wrap_api_future(async move { + wrap_api_future_plain(async move { let veilid_api = get_veilid_api()?; let out = veilid_api.debug(command).await?; - Ok(out) + APIResult::Ok(out) }) }