1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#![doc(html_root_url = "https://docs.rs/wasm_plugin_guest/0.1.0")]
#![deny(missing_docs)]

//! A low-ish level tool for easily writing WASM based plugins to be hosted by
//! wasm_plugin_host.
//!
//! The goal of wasm_plugin is to make communicating across the host-plugin
//! boundary as simple and idiomatic as possible while being unopinionated
//! about how you actually use the plugin.
//!

#[no_mangle]
static mut MESSAGE_BUFFER: [u8; 1024 * 10] = [0; 1024 * 10];

#[cfg(feature = "inject_getrandom")]
mod getrandom_shim {
    use getrandom::register_custom_getrandom;

    use getrandom::Error;

    extern "C" {
        fn __getrandom(ptr: i32, len: i32);
    }

    #[allow(clippy::unnecessary_wraps)]
    fn external_getrandom(buf: &mut [u8]) -> Result<(), Error> {
        let len = buf.len();
        let ptr = buf.as_ptr();
        unsafe {
            __getrandom(ptr as i32, len as i32);
        }
        Ok(())
    }
    register_custom_getrandom!(external_getrandom);
}

/// Read a message from the buffer used to communicate with the host. You should
/// never need to call this directly.
pub fn read_message<T: serde::de::DeserializeOwned>() -> T {
    let buf = unsafe { &mut MESSAGE_BUFFER };
    bincode::deserialize(buf).unwrap()
}

/// Write a message to the buffer used to communicate with the host. You should
/// never need to call this directly.
pub fn write_message<U>(message: &U) -> i32
where
    U: serde::Serialize,
{
    let buf = unsafe { &mut MESSAGE_BUFFER };
    let message: Vec<u8> = bincode::serialize(message).unwrap();
    let len = message.len();
    buf[0..len].copy_from_slice(&message);
    len as i32
}

/// Export a function with no arguments to the plugin host
#[macro_export]
macro_rules! export_plugin_function_with_no_input {
    ($name:ident, $function:path) => {
        const _: () = {
            #[no_mangle]
            pub extern "C" fn $name() -> i32 {
                wasm_plugin_guest::write_message(&$function())
            }
        };
    };
}

/// Export a function with a single argument to the plugin host
#[macro_export]
macro_rules! export_plugin_function_with_input_message {
    ($name:ident, $function:path) => {
        const _: () = {
            #[no_mangle]
            pub extern "C" fn $name() -> i32 {
                let message = wasm_plugin_guest::read_message();

                wasm_plugin_guest::write_message(&$function(message))
            }
        };
    };
}