Skip to main content

swamp_vm_host/
lib.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/swamp
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5
6use std::cell::RefCell;
7use std::rc::Rc;
8use swamp_vm::Vm;
9use swamp_vm::host::HostArgs;
10
11pub fn register_context_aware<T: 'static, F>(
12    vm_ref: &mut Vm,
13    id: u16,
14    context: &Rc<RefCell<T>>,
15    mut callback: F,
16) where
17    F: 'static + FnMut(&mut T, HostArgs),
18{
19    let context_clone = context.clone();
20    let wrapper = move |arg: HostArgs| {
21        let mut ctx = context_clone.borrow_mut();
22        callback(&mut *ctx, arg);
23    };
24
25    vm_ref.add_host_function(id, wrapper);
26}