2024-02-14 04:45:58 -06:00
|
|
|
use rhai::{CustomType, Engine, FuncRegistration, ImmutableString};
|
2023-10-27 20:29:02 -05:00
|
|
|
|
|
|
|
use {
|
|
|
|
crate::{object::SymbolRef, SharedObject},
|
|
|
|
rhai::Module,
|
|
|
|
};
|
|
|
|
|
|
|
|
macro_rules! gen_data_insertions {
|
|
|
|
($module:expr, $obj:expr, [$($ty:ident),* $(,)?] $(,)?) => {{
|
|
|
|
let (module, obj) = ($module, $obj);
|
|
|
|
$({
|
|
|
|
let obj = ::std::rc::Rc::clone(obj);
|
|
|
|
|
2024-02-14 04:45:58 -06:00
|
|
|
FuncRegistration::new(stringify!($ty))
|
|
|
|
.with_namespace(rhai::FnNamespace::Global)
|
|
|
|
.set_into_module::<_, 1, false, _, true, _>(module, move |arr: ::rhai::Array| {
|
|
|
|
let obj = &mut *obj.borrow_mut();
|
|
|
|
let symbol = obj.symbol($crate::object::Section::Data);
|
2023-10-27 20:29:02 -05:00
|
|
|
|
2024-02-14 04:45:58 -06:00
|
|
|
obj.sections
|
|
|
|
.data
|
|
|
|
.reserve(arr.len() * ::std::mem::size_of::<$ty>());
|
2023-10-27 20:29:02 -05:00
|
|
|
|
2024-02-14 04:45:58 -06:00
|
|
|
for item in arr {
|
|
|
|
obj.sections.data.extend(
|
|
|
|
match item.as_int() {
|
|
|
|
Ok(num) => $ty::try_from(num).map_err(|_| "i64".to_owned()),
|
|
|
|
Err(ty) => Err(ty.to_owned()),
|
|
|
|
}
|
|
|
|
.map_err(|err| {
|
|
|
|
::rhai::EvalAltResult::ErrorMismatchDataType(
|
|
|
|
stringify!($ty).to_owned(),
|
|
|
|
err,
|
|
|
|
::rhai::Position::NONE,
|
|
|
|
)
|
|
|
|
})?
|
|
|
|
.to_le_bytes(),
|
|
|
|
);
|
|
|
|
}
|
2023-10-27 20:29:02 -05:00
|
|
|
|
2024-02-14 04:45:58 -06:00
|
|
|
Ok(DataRef {
|
|
|
|
symbol,
|
|
|
|
len: obj.sections.data.len() - symbol.0,
|
|
|
|
})
|
|
|
|
});
|
2023-10-27 20:29:02 -05:00
|
|
|
})*
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub struct DataRef {
|
|
|
|
pub symbol: SymbolRef,
|
|
|
|
pub len: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CustomType for DataRef {
|
|
|
|
fn build(mut builder: rhai::TypeBuilder<Self>) {
|
|
|
|
builder
|
|
|
|
.with_name("DataRef")
|
|
|
|
.with_get("symbol", |this: &mut Self| this.symbol)
|
|
|
|
.with_get("len", |this: &mut Self| this.len as u64 as i64);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn module(engine: &mut Engine, obj: SharedObject) -> Module {
|
|
|
|
let mut module = Module::new();
|
2024-02-14 04:45:58 -06:00
|
|
|
|
2023-10-27 20:29:02 -05:00
|
|
|
gen_data_insertions!(&mut module, &obj, [i8, i16, i32, i64]);
|
|
|
|
|
2024-02-14 04:45:58 -06:00
|
|
|
FuncRegistration::new("str")
|
|
|
|
.with_namespace(rhai::FnNamespace::Global)
|
|
|
|
.set_into_module::<_, 1, false, _, true, _>(&mut module, move |s: ImmutableString| {
|
2023-10-27 20:29:02 -05:00
|
|
|
let obj = &mut *obj.borrow_mut();
|
|
|
|
let symbol = obj.symbol(crate::object::Section::Data);
|
|
|
|
|
|
|
|
obj.sections.data.extend(s.as_bytes());
|
|
|
|
Ok(DataRef {
|
|
|
|
symbol,
|
|
|
|
len: s.len(),
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
engine.build_type::<DataRef>();
|
|
|
|
module
|
|
|
|
}
|