75 lines
2.0 KiB
Rust
Raw Normal View History

2021-06-28 23:58:43 +08:00
mod c;
use crate::c::forget_rust;
use flowy_sdk::*;
use flowy_sys::prelude::*;
2021-07-02 20:47:52 +08:00
use std::{cell::RefCell, ffi::CStr, future::Future, os::raw::c_char};
#[no_mangle]
pub extern "C" fn init_sdk(path: *mut c_char) -> i64 {
let c_str: &CStr = unsafe { CStr::from_ptr(path) };
let path: &str = c_str.to_str().unwrap();
2021-07-02 20:47:52 +08:00
FlowySDK::init_log();
FlowySDK::init(path);
return 1;
}
2021-07-02 20:47:52 +08:00
#[derive(serde::Deserialize)]
2021-06-30 23:11:27 +08:00
pub struct FFICommand {
event: String,
payload: Vec<u8>,
}
impl FFICommand {
2021-07-02 20:47:52 +08:00
pub fn from_bytes(bytes: Vec<u8>) -> Self {
let command: FFICommand = serde_json::from_slice(&bytes).unwrap();
command
}
2021-06-30 23:11:27 +08:00
pub fn from_u8_pointer(pointer: *const u8, len: usize) -> Self {
let bytes = unsafe { std::slice::from_raw_parts(pointer, len) }.to_vec();
2021-07-02 20:47:52 +08:00
FFICommand::from_bytes(bytes)
2021-06-30 23:11:27 +08:00
}
}
#[no_mangle]
pub extern "C" fn async_command(port: i64, input: *const u8, len: usize) {
2021-06-30 23:11:27 +08:00
let FFICommand { event, payload } = FFICommand::from_u8_pointer(input, len);
2021-07-02 20:47:52 +08:00
log::info!("Event: {:?}", event);
2021-06-30 23:11:27 +08:00
2021-07-02 20:47:52 +08:00
let mut request = DispatchRequest::new(port, event).callback(|_, resp| {
2021-06-30 23:11:27 +08:00
log::info!("async resp: {:?}", resp);
});
2021-06-30 23:11:27 +08:00
if !payload.is_empty() {
request = request.payload(Payload::Bytes(payload));
2021-06-30 23:11:27 +08:00
}
2021-06-30 23:11:27 +08:00
async_send(request);
2021-07-02 20:47:52 +08:00
spawn_future(async { vec![] }, 123);
}
#[no_mangle]
2021-06-30 23:11:27 +08:00
pub extern "C" fn sync_command(input: *const u8, len: usize) -> *const u8 { unimplemented!() }
2021-06-28 23:58:43 +08:00
#[inline(never)]
#[no_mangle]
pub extern "C" fn link_me_please() {}
2021-07-02 20:47:52 +08:00
#[inline(always)]
fn spawn_future<F>(future: F, port: i64)
where
F: Future<Output = Vec<u8>> + Send + 'static,
{
let isolate = allo_isolate::Isolate::new(port);
isolate.catch_unwind(future);
// if let Err(e) = isolate.catch_unwind(future) {
// if let Some(msg) = e.downcast_ref::<&str>() {
// log::error!("🔥 {:?}", msg);
// } else {
// log::error!("no info provided for that panic 😡");
// }
// }
}