| Crates.io | virtual_exec_macro |
| lib.rs | virtual_exec_macro |
| version | 0.1.0 |
| created_at | 2025-12-08 00:48:48.189555+00 |
| updated_at | 2025-12-08 00:48:48.189555+00 |
| description | Macro definition sub-crate for virtual_exec - A sandbox execution environment which allowed limited execution of expression safely (WIP) |
| homepage | |
| repository | https://github.com/i-am-unknown-81514525/virtual_exec |
| max_upload_size | |
| id | 1972571 |
| size | 54,648 |
A macro crate to provide compile time sandbox building to increase runtime performance as
it doesn't have to re-evaluate the string expression, as replacement of
virtual_exec_parser::parse.
**Note that this cannot be used as easily as virtual_exec::exec as it provide a higher-level API
Example:
use std::cell::{RefCell, Ref};
use std::collections::HashMap;
use std::rc::Rc;
use bumpalo::Bump;
use virtual_exec_macro::parse;
use virtual_exec_type::ast::core::ASTNode;
use virtual_exec_type::base::{Value, ValueContainer, ValueKind};
use virtual_exec_type::builtin::Mapping;
use virtual_exec_type::exec_ctx::ExecutionContext;
#[test]
fn test_simple_assignment_and_expr() {
let module = parse!(
a = 10;
a = a + 5;
a;
);
let arena_rc = Rc::new(RefCell::new(Bump::new()));
let mut global_scope = Mapping { mapping: HashMap::new() };
let initial_value: Value<'static> = {
let arena_borrow: Ref<Bump> = arena_rc.borrow();
let arena_ref: &Bump = &arena_borrow;
let long_lived_arena: &'static Bump = unsafe { std::mem::transmute(arena_ref) };
ValueContainer::new(ValueKind::None, long_lived_arena)
};
global_scope.mapping.insert("a".to_string(), Rc::new(RefCell::new(initial_value)));
let mapping = vec![Rc::new(RefCell::new(global_scope))];
let ctx = Rc::new(RefCell::new(ExecutionContext::new(arena_rc.clone(), 1000, mapping.clone())));
let result = module.eval(ctx);
assert!(result.is_ok(), "Evaluation failed: {:?}", result.err());
let value = (&mapping).get(0).unwrap().borrow().mapping.get("a").unwrap().borrow().kind.clone();
match value {
ValueKind::Int(i) => assert_eq!(i.value, 15),
_ => panic!("Expected an integer result, but got {:?}", value),
}
}