|
| 1 | +extern crate proc_macro; |
| 2 | + |
| 3 | +use hyperlight_component_util::*; |
| 4 | + |
| 5 | +/// Create host bindings for the wasm component type in the file |
| 6 | +/// passed in (or `$WIT_WORLD`, if nothign is passed in). This will |
| 7 | +/// produce all relevant types and trait implementations for the |
| 8 | +/// component type, as well as functions allowing the component to be |
| 9 | +/// instantiated inside a sandbox. |
| 10 | +/// |
| 11 | +/// This includes both a primitive `register_host_functions`, which can |
| 12 | +/// be used to directly register the host functions on any sandbox |
| 13 | +/// (and which can easily be used with Hyperlight-Wasm), as well as an |
| 14 | +/// `instantiate()` method on the component trait that makes |
| 15 | +/// instantiating the sandbox particularly ergonomic in core |
| 16 | +/// Hyperlight. |
| 17 | +#[proc_macro] |
| 18 | +pub fn host_bindgen(input: proc_macro::TokenStream) -> proc_macro::TokenStream { |
| 19 | + let path: Option<syn::LitStr> = syn::parse_macro_input!(input as Option<syn::LitStr>); |
| 20 | + let path = path |
| 21 | + .map(|x| x.value().into()) |
| 22 | + .unwrap_or_else(|| std::env::var_os("WIT_WORLD").unwrap()); |
| 23 | + util::read_wit_type_from_file(path, |kebab_name, ct| { |
| 24 | + let decls = emit::run_state(false, false, |s| { |
| 25 | + rtypes::emit_toplevel(s, &kebab_name, ct); |
| 26 | + host::emit_toplevel(s, &kebab_name, ct); |
| 27 | + }); |
| 28 | + util::emit_decls(decls).into() |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +/// Create the hyperlight_guest_init() function (which should be |
| 33 | +/// called in hyperlight_main()) for the wasm component type in the |
| 34 | +/// file passed in (or `$WIT_WORLD`, if nothing is passed in). This |
| 35 | +/// function registers Hyperlight functions for component exports |
| 36 | +/// (which are implemented by calling into the trait provided) and |
| 37 | +/// implements the relevant traits for a trivial Host type (by calling |
| 38 | +/// into the Hyperlight host). |
| 39 | +#[proc_macro] |
| 40 | +pub fn guest_bindgen(input: proc_macro::TokenStream) -> proc_macro::TokenStream { |
| 41 | + let path: Option<syn::LitStr> = syn::parse_macro_input!(input as Option<syn::LitStr>); |
| 42 | + let path = path |
| 43 | + .map(|x| x.value().into()) |
| 44 | + .unwrap_or_else(|| std::env::var_os("WIT_WORLD").unwrap()); |
| 45 | + util::read_wit_type_from_file(path, |kebab_name, ct| { |
| 46 | + let decls = emit::run_state(true, false, |s| { |
| 47 | + // Emit type/trait definitions for all instances in the world |
| 48 | + rtypes::emit_toplevel(s, &kebab_name, ct); |
| 49 | + // Emit the host/guest function registrations |
| 50 | + guest::emit_toplevel(s, &kebab_name, ct); |
| 51 | + }); |
| 52 | + // Use util::emit_decls() to choose between emitting the token |
| 53 | + // stream directly and emitting an include!() pointing at a |
| 54 | + // temporary file, depending on whether the user has requested |
| 55 | + // a debug temporary file be created. |
| 56 | + util::emit_decls(decls).into() |
| 57 | + }) |
| 58 | +} |
0 commit comments