2024-02-22 16:57:29 -06:00
|
|
|
//! Stuff related to labels
|
|
|
|
|
2023-10-22 08:08:45 -05:00
|
|
|
use {
|
2023-10-27 20:29:02 -05:00
|
|
|
crate::SharedObject,
|
2024-02-14 04:45:58 -06:00
|
|
|
rhai::{Engine, FuncRegistration, ImmutableString, Module},
|
2023-10-22 08:08:45 -05:00
|
|
|
};
|
|
|
|
|
2024-02-22 16:57:29 -06:00
|
|
|
/// Macro for creating functions for Rhai which
|
|
|
|
/// is bit more friendly
|
|
|
|
///
|
|
|
|
/// ```ignore
|
|
|
|
/// shdm_fns!{
|
|
|
|
/// module: $module;
|
|
|
|
/// shared: $shared => $shname;
|
|
|
|
///
|
|
|
|
/// $vis fn $name($param_name: $param_ty, …) -> $ret { … }
|
|
|
|
/// …
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// - `$module`: Rhai module
|
|
|
|
/// - `$shared`: Data to be shared across the functions
|
|
|
|
/// - `$shname`: The binding name inside functions
|
|
|
|
/// - `$vis`: Function visibility for Rhai
|
|
|
|
/// - Lowercased [`rhai::FnNamespace`] variants
|
|
|
|
/// - `$name`: Function name
|
|
|
|
/// - `$param_name`: Parameter name
|
|
|
|
/// - `$param_ty`: Rust parameter type
|
|
|
|
/// - `$ret`: Optional return type (otherwise infer)
|
2024-02-03 20:08:20 -06:00
|
|
|
macro_rules! shdm_fns {
|
|
|
|
(
|
|
|
|
module: $module:expr;
|
|
|
|
shared: $shared:expr => $shname:ident;
|
|
|
|
|
|
|
|
$(
|
2024-02-14 04:45:58 -06:00
|
|
|
$vis:ident fn $name:ident($($param_name:ident: $param_ty:ty),*) $(-> $ret:ty)? $blk:block
|
2024-02-03 20:08:20 -06:00
|
|
|
)*
|
|
|
|
) => {{
|
|
|
|
let module = $module;
|
|
|
|
let shared = $shared;
|
2024-02-22 16:57:29 -06:00
|
|
|
paste::paste! {
|
|
|
|
$({
|
|
|
|
|
|
|
|
let $shname = SharedObject::clone(&shared);
|
|
|
|
|
|
|
|
FuncRegistration::new(stringify!($name))
|
|
|
|
.with_namespace(rhai::FnNamespace::[<$vis:camel>])
|
|
|
|
.set_into_module::<_, { ["", $(stringify!($param_name)),*].len() - 1 }, false, _, true, _>(
|
|
|
|
module,
|
|
|
|
move |$($param_name: $param_ty),*| $(-> $ret)? {
|
|
|
|
let mut $shname = $shname.borrow_mut();
|
|
|
|
$blk
|
|
|
|
}
|
|
|
|
);
|
|
|
|
})*
|
|
|
|
}
|
2024-02-03 20:08:20 -06:00
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2024-02-22 16:57:29 -06:00
|
|
|
/// Label without any place bound
|
2023-10-27 20:29:02 -05:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub struct UnboundLabel(pub usize);
|
|
|
|
|
|
|
|
pub fn setup(engine: &mut Engine, module: &mut Module, object: SharedObject) {
|
2024-02-03 20:08:20 -06:00
|
|
|
shdm_fns! {
|
|
|
|
module: module;
|
|
|
|
shared: object => obj;
|
|
|
|
|
2024-02-22 16:57:29 -06:00
|
|
|
// Insert unnamed label
|
2024-02-03 20:08:20 -06:00
|
|
|
global fn label() {
|
2023-10-27 20:29:02 -05:00
|
|
|
let symbol = obj.symbol(crate::object::Section::Text);
|
|
|
|
Ok(symbol)
|
2024-02-03 20:08:20 -06:00
|
|
|
}
|
2023-10-27 20:29:02 -05:00
|
|
|
|
2024-02-22 16:57:29 -06:00
|
|
|
// Insert string-labeled label
|
2024-02-03 20:08:20 -06:00
|
|
|
global fn label(label: ImmutableString) {
|
2023-10-27 20:29:02 -05:00
|
|
|
let symbol = obj.symbol(crate::object::Section::Text);
|
|
|
|
obj.labels.insert(label, symbol.0);
|
|
|
|
|
|
|
|
Ok(symbol)
|
2024-02-03 20:08:20 -06:00
|
|
|
}
|
2023-10-27 20:29:02 -05:00
|
|
|
|
2024-02-22 16:57:29 -06:00
|
|
|
// Declare unbound label (to be bound later)
|
2024-02-03 20:08:20 -06:00
|
|
|
global fn declabel() {
|
2023-10-27 20:29:02 -05:00
|
|
|
let index = obj.symbols.len();
|
|
|
|
obj.symbols.push(None);
|
|
|
|
|
|
|
|
Ok(UnboundLabel(index))
|
2024-02-03 20:08:20 -06:00
|
|
|
}
|
2023-10-27 20:29:02 -05:00
|
|
|
|
2024-02-22 16:57:29 -06:00
|
|
|
// Declare unbound label (to be bound later)
|
|
|
|
// with string label
|
2024-02-03 20:08:20 -06:00
|
|
|
global fn declabel(label: ImmutableString) {
|
2023-10-27 20:29:02 -05:00
|
|
|
let index = obj.symbols.len();
|
|
|
|
obj.symbols.push(None);
|
|
|
|
obj.labels.insert(label, index);
|
|
|
|
|
|
|
|
Ok(UnboundLabel(index))
|
2024-02-03 20:08:20 -06:00
|
|
|
}
|
2023-10-27 20:29:02 -05:00
|
|
|
|
2024-02-22 16:57:29 -06:00
|
|
|
// Set location for unbound label
|
2024-02-03 20:08:20 -06:00
|
|
|
global fn here(label: UnboundLabel) {
|
2023-10-27 20:29:02 -05:00
|
|
|
obj.symbols[label.0] = Some(crate::object::SymbolEntry {
|
|
|
|
location: crate::object::Section::Text,
|
|
|
|
offset: obj.sections.text.len(),
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
2024-02-03 20:08:20 -06:00
|
|
|
}
|
2023-10-27 20:29:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
engine.register_type_with_name::<UnboundLabel>("UnboundLabel");
|
2023-10-22 08:08:45 -05:00
|
|
|
}
|