Add tag to Dynamic.
This commit is contained in:
parent
13d5092c4d
commit
bb5dc7b637
14
src/ast.rs
14
src/ast.rs
@ -1419,7 +1419,7 @@ impl fmt::Debug for FnCallHashes {
|
||||
}
|
||||
|
||||
impl FnCallHashes {
|
||||
/// Create a [`FnCallHash`] with only the native Rust hash.
|
||||
/// Create a [`FnCallHashes`] with only the native Rust hash.
|
||||
#[inline(always)]
|
||||
pub fn from_native(hash: u64) -> Self {
|
||||
Self {
|
||||
@ -1427,7 +1427,7 @@ impl FnCallHashes {
|
||||
native: hash,
|
||||
}
|
||||
}
|
||||
/// Create a [`FnCallHash`] with both native Rust and script function hashes set to the same value.
|
||||
/// Create a [`FnCallHashes`] with both native Rust and script function hashes set to the same value.
|
||||
#[inline(always)]
|
||||
pub fn from_script(hash: u64) -> Self {
|
||||
Self {
|
||||
@ -1435,7 +1435,7 @@ impl FnCallHashes {
|
||||
native: hash,
|
||||
}
|
||||
}
|
||||
/// Create a [`FnCallHash`] with both native Rust and script function hashes.
|
||||
/// Create a [`FnCallHashes`] with both native Rust and script function hashes.
|
||||
#[inline(always)]
|
||||
pub fn from_script_and_native(script: u64, native: u64) -> Self {
|
||||
Self {
|
||||
@ -1443,21 +1443,21 @@ impl FnCallHashes {
|
||||
native,
|
||||
}
|
||||
}
|
||||
/// Is this [`FnCallHash`] native Rust only?
|
||||
/// Is this [`FnCallHashes`] native Rust only?
|
||||
#[inline(always)]
|
||||
pub fn is_native_only(&self) -> bool {
|
||||
self.script.is_none()
|
||||
}
|
||||
/// Get the script function hash from this [`FnCallHash`].
|
||||
/// Get the script function hash from this [`FnCallHashes`].
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the [`FnCallHash`] is native Rust only.
|
||||
/// Panics if the [`FnCallHashes`] is native Rust only.
|
||||
#[inline(always)]
|
||||
pub fn script_hash(&self) -> u64 {
|
||||
self.script.unwrap()
|
||||
}
|
||||
/// Get the naive Rust function hash from this [`FnCallHash`].
|
||||
/// Get the naive Rust function hash from this [`FnCallHashes`].
|
||||
#[inline(always)]
|
||||
pub fn native_hash(&self) -> u64 {
|
||||
self.native
|
||||
|
540
src/dynamic.rs
540
src/dynamic.rs
File diff suppressed because it is too large
Load Diff
@ -1676,7 +1676,7 @@ impl Engine {
|
||||
|
||||
match target {
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
Dynamic(Union::Array(arr, _)) => {
|
||||
Dynamic(Union::Array(arr, _, _)) => {
|
||||
// val_array[idx]
|
||||
let index = _idx
|
||||
.as_int()
|
||||
@ -1718,7 +1718,7 @@ impl Engine {
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
Dynamic(Union::Map(map, _)) => {
|
||||
Dynamic(Union::Map(map, _, _)) => {
|
||||
// val_map[idx]
|
||||
let index = &*_idx.read_lock::<ImmutableString>().ok_or_else(|| {
|
||||
self.make_type_mismatch_err::<ImmutableString>(_idx.type_name(), idx_pos)
|
||||
@ -1735,7 +1735,7 @@ impl Engine {
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
Dynamic(Union::Str(s, _)) => {
|
||||
Dynamic(Union::Str(s, _, _)) => {
|
||||
// val_string[idx]
|
||||
let index = _idx
|
||||
.as_int()
|
||||
@ -2569,7 +2569,7 @@ impl Engine {
|
||||
Ok(_) => Ok(Dynamic::UNIT),
|
||||
Err(result_err) => match *result_err {
|
||||
// Re-throw exception
|
||||
EvalAltResult::ErrorRuntime(Dynamic(Union::Unit(_, _)), pos) => {
|
||||
EvalAltResult::ErrorRuntime(Dynamic(Union::Unit(_, _, _)), pos) => {
|
||||
err.set_position(pos);
|
||||
Err(err)
|
||||
}
|
||||
@ -2777,18 +2777,18 @@ impl Engine {
|
||||
fn calc_size(value: &Dynamic) -> (usize, usize, usize) {
|
||||
match value {
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
Dynamic(Union::Array(arr, _)) => {
|
||||
Dynamic(Union::Array(arr, _, _)) => {
|
||||
let mut arrays = 0;
|
||||
let mut maps = 0;
|
||||
|
||||
arr.iter().for_each(|value| match value {
|
||||
Dynamic(Union::Array(_, _)) => {
|
||||
Dynamic(Union::Array(_, _, _)) => {
|
||||
let (a, m, _) = calc_size(value);
|
||||
arrays += a;
|
||||
maps += m;
|
||||
}
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
Dynamic(Union::Map(_, _)) => {
|
||||
Dynamic(Union::Map(_, _, _)) => {
|
||||
let (a, m, _) = calc_size(value);
|
||||
arrays += a;
|
||||
maps += m;
|
||||
@ -2799,18 +2799,18 @@ impl Engine {
|
||||
(arrays, maps, 0)
|
||||
}
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
Dynamic(Union::Map(map, _)) => {
|
||||
Dynamic(Union::Map(map, _, _)) => {
|
||||
let mut arrays = 0;
|
||||
let mut maps = 0;
|
||||
|
||||
map.values().for_each(|value| match value {
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
Dynamic(Union::Array(_, _)) => {
|
||||
Dynamic(Union::Array(_, _, _)) => {
|
||||
let (a, m, _) = calc_size(value);
|
||||
arrays += a;
|
||||
maps += m;
|
||||
}
|
||||
Dynamic(Union::Map(_, _)) => {
|
||||
Dynamic(Union::Map(_, _, _)) => {
|
||||
let (a, m, _) = calc_size(value);
|
||||
arrays += a;
|
||||
maps += m;
|
||||
@ -2820,7 +2820,7 @@ impl Engine {
|
||||
|
||||
(arrays, maps, 0)
|
||||
}
|
||||
Dynamic(Union::Str(s, _)) => (0, 0, s.len()),
|
||||
Dynamic(Union::Str(s, _, _)) => (0, 0, s.len()),
|
||||
_ => (0, 0, 0),
|
||||
}
|
||||
}
|
||||
|
45
src/packages/lang_core.rs
Normal file
45
src/packages/lang_core.rs
Normal 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);
|
||||
});
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -3135,22 +3135,22 @@ impl Engine {
|
||||
pub fn map_dynamic_to_expr(value: Dynamic, pos: Position) -> Option<Expr> {
|
||||
match value.0 {
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
Union::Float(value, _) => Some(Expr::FloatConstant(value, pos)),
|
||||
Union::Float(value, _, _) => Some(Expr::FloatConstant(value, pos)),
|
||||
|
||||
#[cfg(feature = "decimal")]
|
||||
Union::Decimal(value, _) => Some(Expr::DynamicConstant(Box::new((*value).into()), pos)),
|
||||
Union::Decimal(value, _, _) => Some(Expr::DynamicConstant(Box::new((*value).into()), pos)),
|
||||
|
||||
Union::Unit(_, _) => Some(Expr::Unit(pos)),
|
||||
Union::Int(value, _) => Some(Expr::IntegerConstant(value, pos)),
|
||||
Union::Char(value, _) => Some(Expr::CharConstant(value, pos)),
|
||||
Union::Str(value, _) => Some(Expr::StringConstant(value, pos)),
|
||||
Union::Bool(value, _) => Some(Expr::BoolConstant(value, pos)),
|
||||
Union::Unit(_, _, _) => Some(Expr::Unit(pos)),
|
||||
Union::Int(value, _, _) => Some(Expr::IntegerConstant(value, pos)),
|
||||
Union::Char(value, _, _) => Some(Expr::CharConstant(value, pos)),
|
||||
Union::Str(value, _, _) => Some(Expr::StringConstant(value, pos)),
|
||||
Union::Bool(value, _, _) => Some(Expr::BoolConstant(value, pos)),
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
Union::Array(array, _) => Some(Expr::DynamicConstant(Box::new((*array).into()), pos)),
|
||||
Union::Array(array, _, _) => Some(Expr::DynamicConstant(Box::new((*array).into()), pos)),
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
Union::Map(map, _) => Some(Expr::DynamicConstant(Box::new((*map).into()), pos)),
|
||||
Union::Map(map, _, _) => Some(Expr::DynamicConstant(Box::new((*map).into()), pos)),
|
||||
|
||||
_ => None,
|
||||
}
|
||||
|
@ -126,53 +126,53 @@ impl<'de> Deserializer<'de> for &mut DynamicDeserializer<'de> {
|
||||
|
||||
fn deserialize_any<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
|
||||
match &self.value.0 {
|
||||
Union::Unit(_, _) => self.deserialize_unit(visitor),
|
||||
Union::Bool(_, _) => self.deserialize_bool(visitor),
|
||||
Union::Str(_, _) => self.deserialize_str(visitor),
|
||||
Union::Char(_, _) => self.deserialize_char(visitor),
|
||||
Union::Unit(_, _, _) => self.deserialize_unit(visitor),
|
||||
Union::Bool(_, _, _) => self.deserialize_bool(visitor),
|
||||
Union::Str(_, _, _) => self.deserialize_str(visitor),
|
||||
Union::Char(_, _, _) => self.deserialize_char(visitor),
|
||||
|
||||
#[cfg(not(feature = "only_i32"))]
|
||||
Union::Int(_, _) => self.deserialize_i64(visitor),
|
||||
Union::Int(_, _, _) => self.deserialize_i64(visitor),
|
||||
#[cfg(feature = "only_i32")]
|
||||
Union::Int(_, _) => self.deserialize_i32(visitor),
|
||||
Union::Int(_, _, _) => self.deserialize_i32(visitor),
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
#[cfg(not(feature = "f32_float"))]
|
||||
Union::Float(_, _) => self.deserialize_f64(visitor),
|
||||
Union::Float(_, _, _) => self.deserialize_f64(visitor),
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
#[cfg(feature = "f32_float")]
|
||||
Union::Float(_, _) => self.deserialize_f32(visitor),
|
||||
Union::Float(_, _, _) => self.deserialize_f32(visitor),
|
||||
|
||||
#[cfg(feature = "decimal")]
|
||||
#[cfg(not(feature = "f32_float"))]
|
||||
Union::Decimal(_, _) => self.deserialize_f64(visitor),
|
||||
Union::Decimal(_, _, _) => self.deserialize_f64(visitor),
|
||||
#[cfg(feature = "decimal")]
|
||||
#[cfg(feature = "f32_float")]
|
||||
Union::Decimal(_, _) => self.deserialize_f32(visitor),
|
||||
Union::Decimal(_, _, _) => self.deserialize_f32(visitor),
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
Union::Array(_, _) => self.deserialize_seq(visitor),
|
||||
Union::Array(_, _, _) => self.deserialize_seq(visitor),
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
Union::Map(_, _) => self.deserialize_map(visitor),
|
||||
Union::FnPtr(_, _) => self.type_error(),
|
||||
Union::Map(_, _, _) => self.deserialize_map(visitor),
|
||||
Union::FnPtr(_, _, _) => self.type_error(),
|
||||
#[cfg(not(feature = "no_std"))]
|
||||
Union::TimeStamp(_, _) => self.type_error(),
|
||||
Union::TimeStamp(_, _, _) => self.type_error(),
|
||||
|
||||
Union::Variant(value, _) if value.is::<i8>() => self.deserialize_i8(visitor),
|
||||
Union::Variant(value, _) if value.is::<i16>() => self.deserialize_i16(visitor),
|
||||
Union::Variant(value, _) if value.is::<i32>() => self.deserialize_i32(visitor),
|
||||
Union::Variant(value, _) if value.is::<i64>() => self.deserialize_i64(visitor),
|
||||
Union::Variant(value, _) if value.is::<i128>() => self.deserialize_i128(visitor),
|
||||
Union::Variant(value, _) if value.is::<u8>() => self.deserialize_u8(visitor),
|
||||
Union::Variant(value, _) if value.is::<u16>() => self.deserialize_u16(visitor),
|
||||
Union::Variant(value, _) if value.is::<u32>() => self.deserialize_u32(visitor),
|
||||
Union::Variant(value, _) if value.is::<u64>() => self.deserialize_u64(visitor),
|
||||
Union::Variant(value, _) if value.is::<u128>() => self.deserialize_u128(visitor),
|
||||
Union::Variant(value, _, _) if value.is::<i8>() => self.deserialize_i8(visitor),
|
||||
Union::Variant(value, _, _) if value.is::<i16>() => self.deserialize_i16(visitor),
|
||||
Union::Variant(value, _, _) if value.is::<i32>() => self.deserialize_i32(visitor),
|
||||
Union::Variant(value, _, _) if value.is::<i64>() => self.deserialize_i64(visitor),
|
||||
Union::Variant(value, _, _) if value.is::<i128>() => self.deserialize_i128(visitor),
|
||||
Union::Variant(value, _, _) if value.is::<u8>() => self.deserialize_u8(visitor),
|
||||
Union::Variant(value, _, _) if value.is::<u16>() => self.deserialize_u16(visitor),
|
||||
Union::Variant(value, _, _) if value.is::<u32>() => self.deserialize_u32(visitor),
|
||||
Union::Variant(value, _, _) if value.is::<u64>() => self.deserialize_u64(visitor),
|
||||
Union::Variant(value, _, _) if value.is::<u128>() => self.deserialize_u128(visitor),
|
||||
|
||||
Union::Variant(_, _) => self.type_error(),
|
||||
Union::Variant(_, _, _) => self.type_error(),
|
||||
|
||||
#[cfg(not(feature = "no_closure"))]
|
||||
Union::Shared(_, _) => self.type_error(),
|
||||
Union::Shared(_, _, _) => self.type_error(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -12,26 +12,26 @@ use serde::ser::SerializeMap;
|
||||
impl Serialize for Dynamic {
|
||||
fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
|
||||
match &self.0 {
|
||||
Union::Unit(_, _) => ser.serialize_unit(),
|
||||
Union::Bool(x, _) => ser.serialize_bool(*x),
|
||||
Union::Str(s, _) => ser.serialize_str(s.as_str()),
|
||||
Union::Char(c, _) => ser.serialize_str(&c.to_string()),
|
||||
Union::Unit(_, _, _) => ser.serialize_unit(),
|
||||
Union::Bool(x, _, _) => ser.serialize_bool(*x),
|
||||
Union::Str(s, _, _) => ser.serialize_str(s.as_str()),
|
||||
Union::Char(c, _, _) => ser.serialize_str(&c.to_string()),
|
||||
|
||||
#[cfg(not(feature = "only_i32"))]
|
||||
Union::Int(x, _) => ser.serialize_i64(*x),
|
||||
Union::Int(x, _, _) => ser.serialize_i64(*x),
|
||||
#[cfg(feature = "only_i32")]
|
||||
Union::Int(x, _) => ser.serialize_i32(*x),
|
||||
Union::Int(x, _, _) => ser.serialize_i32(*x),
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
#[cfg(not(feature = "f32_float"))]
|
||||
Union::Float(x, _) => ser.serialize_f64(**x),
|
||||
Union::Float(x, _, _) => ser.serialize_f64(**x),
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
#[cfg(feature = "f32_float")]
|
||||
Union::Float(x, _) => ser.serialize_f32(**x),
|
||||
Union::Float(x, _, _) => ser.serialize_f32(**x),
|
||||
|
||||
#[cfg(feature = "decimal")]
|
||||
#[cfg(not(feature = "f32_float"))]
|
||||
Union::Decimal(x, _) => {
|
||||
Union::Decimal(x, _, _) => {
|
||||
use rust_decimal::prelude::ToPrimitive;
|
||||
|
||||
if let Some(v) = x.to_f64() {
|
||||
@ -42,7 +42,7 @@ impl Serialize for Dynamic {
|
||||
}
|
||||
#[cfg(feature = "decimal")]
|
||||
#[cfg(feature = "f32_float")]
|
||||
Union::Decimal(x, _) => {
|
||||
Union::Decimal(x, _, _) => {
|
||||
use rust_decimal::prelude::ToPrimitive;
|
||||
|
||||
if let Some(v) = x.to_f32() {
|
||||
@ -53,27 +53,27 @@ impl Serialize for Dynamic {
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
Union::Array(a, _) => (**a).serialize(ser),
|
||||
Union::Array(a, _, _) => (**a).serialize(ser),
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
Union::Map(m, _) => {
|
||||
Union::Map(m, _, _) => {
|
||||
let mut map = ser.serialize_map(Some(m.len()))?;
|
||||
for (k, v) in m.iter() {
|
||||
map.serialize_entry(k.as_str(), v)?;
|
||||
}
|
||||
map.end()
|
||||
}
|
||||
Union::FnPtr(f, _) => ser.serialize_str(f.fn_name()),
|
||||
Union::FnPtr(f, _, _) => ser.serialize_str(f.fn_name()),
|
||||
#[cfg(not(feature = "no_std"))]
|
||||
Union::TimeStamp(x, _) => ser.serialize_str(x.as_ref().type_name()),
|
||||
Union::TimeStamp(x, _, _) => ser.serialize_str(x.as_ref().type_name()),
|
||||
|
||||
Union::Variant(v, _) => ser.serialize_str((***v).type_name()),
|
||||
Union::Variant(v, _, _) => ser.serialize_str((***v).type_name()),
|
||||
|
||||
#[cfg(not(feature = "no_closure"))]
|
||||
#[cfg(not(feature = "sync"))]
|
||||
Union::Shared(cell, _) => cell.borrow().serialize(ser),
|
||||
Union::Shared(cell, _, _) => cell.borrow().serialize(ser),
|
||||
#[cfg(not(feature = "no_closure"))]
|
||||
#[cfg(feature = "sync")]
|
||||
Union::Shared(cell, _) => cell.read().unwrap().serialize(ser),
|
||||
Union::Shared(cell, _, _) => cell.read().unwrap().serialize(ser),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user