use hyperlight_host::func::HostFunction;
use hyperlight_host::sandbox::SandboxConfiguration;
use hyperlight_host::{GuestBinary, MultiUseSandbox, UninitializedSandbox};
use hyperlight_testing::{c_simple_guest_as_string, simple_guest_as_string};
fn rust_guest_path() -> String {
simple_guest_as_string().unwrap()
}
fn c_guest_path() -> String {
c_simple_guest_as_string().unwrap()
}
pub fn new_rust_sandbox() -> MultiUseSandbox {
UninitializedSandbox::new(GuestBinary::FilePath(rust_guest_path()), None)
.unwrap()
.evolve()
.unwrap()
}
pub fn new_rust_uninit_sandbox() -> UninitializedSandbox {
UninitializedSandbox::new(GuestBinary::FilePath(rust_guest_path()), None).unwrap()
}
pub fn with_rust_sandbox<F>(f: F)
where
F: FnOnce(MultiUseSandbox),
{
let sandbox = UninitializedSandbox::new(GuestBinary::FilePath(rust_guest_path()), None)
.unwrap()
.evolve()
.unwrap();
f(sandbox);
}
pub fn with_rust_sandbox_cfg<F>(cfg: SandboxConfiguration, f: F)
where
F: FnOnce(MultiUseSandbox),
{
let sandbox = UninitializedSandbox::new(GuestBinary::FilePath(rust_guest_path()), Some(cfg))
.unwrap()
.evolve()
.unwrap();
f(sandbox);
}
pub fn with_rust_uninit_sandbox<F>(f: F)
where
F: FnOnce(UninitializedSandbox),
{
let sandbox =
UninitializedSandbox::new(GuestBinary::FilePath(rust_guest_path()), None).unwrap();
f(sandbox);
}
pub fn with_c_sandbox<F>(f: F)
where
F: FnOnce(MultiUseSandbox),
{
let sandbox = UninitializedSandbox::new(GuestBinary::FilePath(c_guest_path()), None)
.unwrap()
.evolve()
.unwrap();
f(sandbox);
}
pub fn with_c_uninit_sandbox<F>(f: F)
where
F: FnOnce(UninitializedSandbox),
{
let sandbox = UninitializedSandbox::new(GuestBinary::FilePath(c_guest_path()), None).unwrap();
f(sandbox);
}
pub fn with_all_sandboxes_cfg<F>(cfg: Option<SandboxConfiguration>, f: F)
where
F: Fn(MultiUseSandbox),
{
for path in [rust_guest_path(), c_guest_path()] {
let sandbox = UninitializedSandbox::new(GuestBinary::FilePath(path), cfg)
.unwrap()
.evolve()
.unwrap();
f(sandbox);
}
}
pub fn with_all_sandboxes<F>(f: F)
where
F: Fn(MultiUseSandbox),
{
with_all_sandboxes_cfg(None, f);
}
pub fn with_all_uninit_sandboxes<F>(f: F)
where
F: Fn(UninitializedSandbox),
{
for path in [rust_guest_path(), c_guest_path()] {
let sandbox = UninitializedSandbox::new(GuestBinary::FilePath(path), None).unwrap();
f(sandbox);
}
}
pub fn with_all_sandboxes_with_writer<F>(writer: HostFunction<i32, (String,)>, f: F)
where
F: Fn(MultiUseSandbox),
{
for path in [rust_guest_path(), c_guest_path()] {
let mut sandbox = UninitializedSandbox::new(GuestBinary::FilePath(path), None).unwrap();
sandbox.register_print(writer.clone()).unwrap();
let sandbox = sandbox.evolve().unwrap();
f(sandbox);
}
}