Add tag to Dynamic.

This commit is contained in:
Stephen Chung
2021-05-02 23:57:35 +08:00
parent 13d5092c4d
commit bb5dc7b637
9 changed files with 441 additions and 288 deletions

45
src/packages/lang_core.rs Normal file
View File

@@ -0,0 +1,45 @@
use crate::def_package;
use crate::dynamic::Tag;
use crate::plugin::*;
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(Box::new(EvalAltResult::ErrorArithmetic(
format!(
"{} is too small to fit into a tag (must be between {} and {})",
tag,
Tag::MIN,
Tag::MAX
),
Position::NONE,
)))
} else if tag > Tag::MAX as INT {
Err(Box::new(EvalAltResult::ErrorArithmetic(
format!(
"{} is too large to fit into a tag (must be between {} and {})",
tag,
Tag::MIN,
Tag::MAX
),
Position::NONE,
)))
} else {
value.set_tag(tag as Tag);
Ok(())
}
}
}
def_package!(crate:LanguageCorePackage:"Language core functions.", lib, {
combine_with_exported_module!(lib, "language_core", core_functions);
});

View File

@@ -6,6 +6,7 @@ pub(crate) mod arithmetic;
mod array_basic;
mod fn_basic;
mod iter_basic;
mod lang_core;
mod logic;
mod map_basic;
mod math_basic;
@@ -86,6 +87,7 @@ macro_rules! def_package {
}
impl $package {
#[allow(dead_code)]
pub fn new() -> Self {
let mut module = $root::Module::new();
<Self as $root::packages::Package>::init(&mut module);

View File

@@ -1,6 +1,7 @@
use super::arithmetic::ArithmeticPackage;
use super::fn_basic::BasicFnPackage;
use super::iter_basic::BasicIteratorPackage;
use super::lang_core::LanguageCorePackage;
use super::logic::LogicPackage;
use super::string_basic::BasicStringPackage;
#[cfg(feature = "no_std")]
@@ -9,6 +10,7 @@ use std::prelude::v1::*;
use crate::def_package;
def_package!(crate:CorePackage:"_Core_ package containing basic facilities.", lib, {
LanguageCorePackage::init(lib);
ArithmeticPackage::init(lib);
LogicPackage::init(lib);
BasicStringPackage::init(lib);