rhai/src/packages/lang_core.rs

50 lines
1.5 KiB
Rust
Raw Normal View History

2021-05-02 17:57:35 +02:00
use crate::def_package;
use crate::plugin::*;
2021-11-13 15:36:23 +01:00
use crate::types::dynamic::Tag;
2021-05-02 17:57:35 +02:00
use crate::{Dynamic, EvalAltResult, INT};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
#[export_module]
mod core_functions {
#[rhai_fn(name = "tag", get = "tag", pure)]
pub fn get_tag(value: &mut Dynamic) -> INT {
value.tag() as INT
}
#[rhai_fn(name = "set_tag", set = "tag", return_raw)]
pub fn set_tag(value: &mut Dynamic, tag: INT) -> Result<(), Box<EvalAltResult>> {
if tag < Tag::MIN as INT {
Err(EvalAltResult::ErrorArithmetic(
2021-05-02 17:57:35 +02:00
format!(
"{} is too small to fit into a tag (must be between {} and {})",
tag,
Tag::MIN,
Tag::MAX
),
Position::NONE,
2021-06-29 15:58:05 +02:00
)
.into())
2021-05-02 17:57:35 +02:00
} else if tag > Tag::MAX as INT {
Err(EvalAltResult::ErrorArithmetic(
2021-05-02 17:57:35 +02:00
format!(
"{} is too large to fit into a tag (must be between {} and {})",
tag,
Tag::MIN,
Tag::MAX
),
Position::NONE,
2021-06-29 15:58:05 +02:00
)
.into())
2021-05-02 17:57:35 +02:00
} else {
value.set_tag(tag as Tag);
Ok(())
}
}
}
def_package!(crate:LanguageCorePackage:"Language core functions.", lib, {
lib.standard = true;
2021-05-02 17:57:35 +02:00
combine_with_exported_module!(lib, "language_core", core_functions);
});