#[cfg(not(target_arch = "wasm32"))]
compile_error!("This module should only be included when compiling to wasm");
use std::ffi::{CStr, CString};
use std::fmt::Debug;
use std::mem;
use std::os::raw::{c_char, c_void};
use crate::query::{Query, Response};
extern "C" {
fn send_query(query: *const c_char) -> *const c_char;
fn _log(message: *const c_char);
}
#[no_mangle]
pub extern "C" fn alloc(size: usize) -> *const c_void {
let buf = Vec::with_capacity(size);
let ptr = buf.as_ptr();
mem::forget(buf);
return ptr as *const c_void;
}
#[no_mangle]
pub extern "C" fn dealloc(ptr: *mut c_void, cap: usize) {
unsafe {
let _buf = Vec::from_raw_parts(ptr, 0, cap);
}
}
#[no_mangle]
pub extern "C" fn dealloc_str(ptr: *mut c_char) {
unsafe {
let _ = CString::from_raw(ptr);
}
}
pub(crate) fn _debug<T: Debug>(value: T) {
let raw_str = CString::new(format!("{:?}", value)).unwrap().into_raw();
unsafe { _log(raw_str) };
}
pub struct RendererProcess {}
impl RendererProcess {
pub fn new() -> Self {
Self {}
}
pub fn send_query(&mut self, query: Query) -> Option<Response> {
let query_str = serde_json::to_string(&query).unwrap();
let c_str = CString::new(query_str).unwrap();
let raw_str = c_str.into_raw();
let response_cstr = unsafe { CStr::from_ptr(send_query(raw_str)) };
if let Query::Request(_) = query {
let response_str = response_cstr.to_str().expect("String provided by JavaScript was not valid UTF-8");
let response = serde_json::from_str(response_str).expect("String provided by JavaScript was not valid JSON");
Some(response)
} else {
None
}
}
}