2020-12-12 15:02:58 +00:00
|
|
|
pub extern crate plugin_derive;
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
pub mod retrieve;
|
2021-02-15 16:38:55 +00:00
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
use api::RetrieveError;
|
|
|
|
pub use retrieve::*;
|
2021-02-16 23:11:05 +00:00
|
|
|
|
2021-02-16 19:52:38 +00:00
|
|
|
use std::convert::TryInto;
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
pub use retrieve::*;
|
2021-02-15 16:38:55 +00:00
|
|
|
|
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
|
|
|
|
2021-01-10 20:49:10 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
2020-12-11 23:37:22 +00:00
|
|
|
extern "C" {
|
2021-03-01 18:00:44 +00:00
|
|
|
fn raw_emit_actions(ptr: i64, len: i64);
|
|
|
|
fn raw_retrieve_action(ptr: i64, len: i64) -> i64;
|
2021-02-16 23:11:05 +00:00
|
|
|
pub fn dbg(i: i32);
|
2021-02-15 16:38:55 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
pub fn retrieve_action<T: DeserializeOwned>(_actions: &api::Retrieve) -> Result<T, RetrieveError> {
|
2021-02-16 23:11:05 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
{
|
2021-02-17 13:03:20 +00:00
|
|
|
let ret = bincode::serialize(&_actions).expect("Can't serialize action in emit");
|
2021-02-16 23:11:05 +00:00
|
|
|
unsafe {
|
2021-03-01 18:00:44 +00:00
|
|
|
let ptr = raw_retrieve_action(to_i64(ret.as_ptr() as _), to_i64(ret.len() as _));
|
|
|
|
let ptr = from_i64(ptr);
|
|
|
|
let len =
|
|
|
|
u64::from_le_bytes(std::slice::from_raw_parts(ptr as _, 8).try_into().unwrap());
|
|
|
|
let a = ::std::slice::from_raw_parts((ptr + 8) as _, len as _);
|
2021-02-17 13:03:20 +00:00
|
|
|
bincode::deserialize::<Result<T, RetrieveError>>(&a)
|
|
|
|
.map_err(|x| RetrieveError::BincodeError(x.to_string()))?
|
2021-02-16 23:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
unreachable!()
|
|
|
|
}
|
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
|
|
|
|
2021-01-10 20:49:10 +00:00
|
|
|
pub fn emit_actions(_actions: Vec<api::Action>) {
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
{
|
|
|
|
let ret = bincode::serialize(&_actions).expect("Can't serialize action in emit");
|
|
|
|
unsafe {
|
2021-03-01 18:00:44 +00:00
|
|
|
raw_emit_actions(to_i64(ret.as_ptr() as _), to_i64(ret.len() as _));
|
2021-01-10 20:49:10 +00:00
|
|
|
}
|
2020-12-09 20:47:42 +00:00
|
|
|
}
|
2020-12-11 23:37:22 +00:00
|
|
|
}
|
2020-12-09 20:47:42 +00:00
|
|
|
|
2021-03-01 18:00:44 +00:00
|
|
|
pub fn read_input<T>(ptr: i64, len: i64) -> Result<T, &'static str>
|
2020-12-13 17:40:15 +00:00
|
|
|
where
|
|
|
|
T: DeserializeOwned,
|
|
|
|
{
|
2021-03-01 18:00:44 +00:00
|
|
|
let slice = unsafe { ::std::slice::from_raw_parts(from_i64(ptr) as _, from_i64(len) as _) };
|
2020-12-13 17:40:15 +00:00
|
|
|
bincode::deserialize(slice).map_err(|_| "Failed to deserialize function input")
|
2020-12-09 20:47:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 21:24:02 +00:00
|
|
|
/// This function split a u128 in two u64 encoding them as le bytes
|
2021-03-01 18:00:44 +00:00
|
|
|
pub fn from_u128(i: u128) -> (u64, u64) {
|
2021-02-16 23:11:05 +00:00
|
|
|
let i = i.to_le_bytes();
|
|
|
|
(
|
2021-03-01 18:00:44 +00:00
|
|
|
u64::from_le_bytes(i[0..8].try_into().unwrap()),
|
|
|
|
u64::from_le_bytes(i[8..16].try_into().unwrap()),
|
2021-02-16 23:11:05 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-01 21:24:02 +00:00
|
|
|
/// This function merge two u64 encoded as le in one u128
|
2021-03-01 18:00:44 +00:00
|
|
|
pub fn to_u128(a: u64, b: u64) -> u128 {
|
2021-02-16 19:52:38 +00:00
|
|
|
let a = a.to_le_bytes();
|
|
|
|
let b = b.to_le_bytes();
|
2021-03-01 18:00:44 +00:00
|
|
|
u128::from_le_bytes([a, b].concat().try_into().unwrap())
|
2021-02-16 19:52:38 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 21:24:02 +00:00
|
|
|
/// This function encode a u64 into a i64 using le bytes
|
2021-03-01 18:00:44 +00:00
|
|
|
pub fn to_i64(i: u64) -> i64 { i64::from_le_bytes(i.to_le_bytes()) }
|
|
|
|
|
2021-03-01 21:24:02 +00:00
|
|
|
/// This function decode a i64 into a u64 using le bytes
|
2021-03-01 18:00:44 +00:00
|
|
|
pub fn from_i64(i: i64) -> u64 { u64::from_le_bytes(i.to_le_bytes()) }
|
|
|
|
|
|
|
|
static mut VEC: Vec<u8> = vec![];
|
|
|
|
static mut DATA: Vec<u8> = vec![];
|
|
|
|
|
2021-02-16 19:52:38 +00:00
|
|
|
pub fn write_output(value: impl Serialize) -> i64 {
|
2021-03-01 18:00:44 +00:00
|
|
|
unsafe {
|
|
|
|
VEC = bincode::serialize(&value).expect("Can't serialize event output");
|
|
|
|
DATA = [
|
|
|
|
(VEC.as_ptr() as u64).to_le_bytes(),
|
|
|
|
(VEC.len() as u64).to_le_bytes(),
|
|
|
|
]
|
|
|
|
.concat();
|
|
|
|
to_i64(DATA.as_ptr() as u64)
|
|
|
|
}
|
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
|
2020-12-14 17:22:03 +00:00
|
|
|
/// # Safety
|
|
|
|
/// This function should never be used only intented to by used by the host
|
2020-12-14 16:07:05 +00:00
|
|
|
#[no_mangle]
|
2021-03-01 18:00:44 +00:00
|
|
|
pub unsafe fn wasm_prepare_buffer(size: i32) -> i64 {
|
2020-12-14 20:48:37 +00:00
|
|
|
BUFFERS = vec![0u8; size as usize];
|
2021-03-01 18:00:44 +00:00
|
|
|
BUFFERS.as_ptr() as i64
|
2020-12-14 16:07:05 +00:00
|
|
|
}
|