2020-12-09 20:47:42 +00:00
|
|
|
#![feature(const_fn)]
|
|
|
|
|
2020-12-12 15:02:58 +00:00
|
|
|
pub extern crate plugin_derive;
|
|
|
|
|
2020-12-12 13:01:54 +00:00
|
|
|
pub use plugin_api as api;
|
|
|
|
pub use plugin_derive::*;
|
2020-12-11 23:37:22 +00:00
|
|
|
|
2020-12-13 17:40:15 +00:00
|
|
|
use serde::{de::DeserializeOwned, Serialize};
|
2020-12-09 20:47:42 +00:00
|
|
|
|
2020-12-11 23:37:22 +00:00
|
|
|
extern "C" {
|
2020-12-12 19:26:42 +00:00
|
|
|
fn raw_emit_actions(ptr: *const u8, len: usize);
|
2020-12-09 20:47:42 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 17:40:15 +00:00
|
|
|
pub fn emit_action(action: api::Action) { emit_actions(vec![action]) }
|
2020-12-12 19:26:42 +00:00
|
|
|
|
|
|
|
pub fn emit_actions(actions: Vec<api::Action>) {
|
2020-12-14 16:07:05 +00:00
|
|
|
let ret = bincode::serialize(&actions).expect("Can't serialize action in emit");
|
2020-12-11 23:37:22 +00:00
|
|
|
unsafe {
|
2020-12-12 19:26:42 +00:00
|
|
|
raw_emit_actions(ret.as_ptr(), ret.len());
|
2020-12-09 20:47:42 +00:00
|
|
|
}
|
2020-12-11 23:37:22 +00:00
|
|
|
}
|
2020-12-09 20:47:42 +00:00
|
|
|
|
2020-12-13 17:40:15 +00:00
|
|
|
pub fn read_input<T>(ptr: i32, len: u32) -> Result<T, &'static str>
|
|
|
|
where
|
|
|
|
T: DeserializeOwned,
|
|
|
|
{
|
|
|
|
let slice = unsafe { ::std::slice::from_raw_parts(ptr as _, len as _) };
|
|
|
|
bincode::deserialize(slice).map_err(|_| "Failed to deserialize function input")
|
2020-12-09 20:47:42 +00:00
|
|
|
}
|
|
|
|
|
2020-12-11 23:37:22 +00:00
|
|
|
pub fn write_output(value: impl Serialize) -> i32 {
|
2020-12-14 16:07:05 +00:00
|
|
|
let ret = bincode::serialize(&value).expect("Can't serialize event output");
|
2020-12-11 23:37:22 +00:00
|
|
|
let len = ret.len() as u32;
|
|
|
|
unsafe {
|
|
|
|
::std::ptr::write(1 as _, len);
|
|
|
|
}
|
|
|
|
ret.as_ptr() as _
|
2020-12-12 13:01:54 +00:00
|
|
|
}
|
2020-12-14 16:07:05 +00:00
|
|
|
|
|
|
|
static mut BUFFERS: Vec<u8> = Vec::new();
|
|
|
|
|
|
|
|
/// Allocate buffer from wasm linear memory
|
|
|
|
#[no_mangle]
|
2020-12-14 16:43:32 +00:00
|
|
|
pub unsafe fn wasm_prepare_buffer(size: i32) -> i32 {
|
|
|
|
BUFFERS = Vec::<u8>::with_capacity(size as usize);
|
|
|
|
BUFFERS.as_ptr() as i32
|
2020-12-14 16:07:05 +00:00
|
|
|
}
|