Add Dynamic::is_XXX API.

This commit is contained in:
Stephen Chung
2022-11-09 12:44:57 +08:00
parent ad018aaae3
commit ce046422f0
22 changed files with 327 additions and 91 deletions

View File

@@ -145,7 +145,7 @@ impl Engine {
format!(
"{fn_name} ({})",
args.iter()
.map(|a| if a.is::<ImmutableString>() {
.map(|a| if a.is_string() {
"&str | ImmutableString | String"
} else {
self.map_type_name(a.type_name())
@@ -574,7 +574,7 @@ impl Engine {
// Handle is_def_fn()
#[cfg(not(feature = "no_function"))]
crate::engine::KEYWORD_IS_DEF_FN
if args.len() == 2 && args[0].is::<FnPtr>() && args[1].is::<crate::INT>() =>
if args.len() == 2 && args[0].is_fnptr() && args[1].is_int() =>
{
let fn_name = args[0].read_lock::<ImmutableString>().expect("`FnPtr`");
let num_params = args[1].as_int().expect("`INT`");
@@ -731,7 +731,7 @@ impl Engine {
let is_ref_mut = target.is_ref();
let (result, updated) = match fn_name {
KEYWORD_FN_PTR_CALL if target.is::<FnPtr>() => {
KEYWORD_FN_PTR_CALL if target.is_fnptr() => {
// FnPtr call
let fn_ptr = target.read_lock::<FnPtr>().expect("`FnPtr`");
@@ -775,7 +775,7 @@ impl Engine {
if call_args.is_empty() {
let typ = self.map_type_name(target.type_name());
return Err(self.make_type_mismatch_err::<FnPtr>(typ, fn_call_pos));
} else if !call_args[0].is::<FnPtr>() {
} else if !call_args[0].is_fnptr() {
let typ = self.map_type_name(call_args[0].type_name());
return Err(self.make_type_mismatch_err::<FnPtr>(typ, first_arg_pos));
}
@@ -827,7 +827,7 @@ impl Engine {
)
}
KEYWORD_FN_PTR_CURRY => {
if !target.is::<FnPtr>() {
if !target.is_fnptr() {
let typ = self.map_type_name(target.type_name());
return Err(self.make_type_mismatch_err::<FnPtr>(typ, fn_call_pos));
}
@@ -969,7 +969,7 @@ impl Engine {
let (arg_value, arg_pos) =
self.get_arg_value(global, caches, lib, scope, this_ptr, arg)?;
if !arg_value.is::<FnPtr>() {
if !arg_value.is_fnptr() {
let typ = self.map_type_name(arg_value.type_name());
return Err(self.make_type_mismatch_err::<FnPtr>(typ, arg_pos));
}
@@ -1025,7 +1025,7 @@ impl Engine {
let (arg_value, arg_pos) =
self.get_arg_value(global, caches, lib, scope, this_ptr, first)?;
if !arg_value.is::<FnPtr>() {
if !arg_value.is_fnptr() {
let typ = self.map_type_name(arg_value.type_name());
return Err(self.make_type_mismatch_err::<FnPtr>(typ, arg_pos));
}

View File

@@ -23,6 +23,7 @@ pub use callable_function::CallableFunction;
pub use func::Func;
pub use hashing::{calc_fn_hash, calc_fn_hash_full, calc_var_hash, get_hasher, StraightHashMap};
#[cfg(feature = "internals")]
#[allow(deprecated)]
pub use native::NativeCallContextStore;
pub use native::{
locked_read, locked_write, shared_get_mut, shared_make_mut, shared_take, shared_take_or_clone,

View File

@@ -81,6 +81,11 @@ pub struct NativeCallContext<'a> {
/// _(internals)_ Context of a native Rust function call.
/// Exported under the `internals` feature only.
///
/// # WARNING - Volatile Type
///
/// This type is volatile and may change in the future.
#[deprecated = "This type is NOT deprecated, but it is considered volatile and may change in the future."]
#[cfg(feature = "internals")]
#[derive(Debug, Clone)]
pub struct NativeCallContextStore {
@@ -97,8 +102,14 @@ pub struct NativeCallContextStore {
}
#[cfg(feature = "internals")]
#[allow(deprecated)]
impl NativeCallContextStore {
/// Create a [`NativeCallContext`] from a [`NativeCallContextClone`].
///
/// # WARNING - Unstable API
///
/// This API is volatile and may change in the future.
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
#[inline(always)]
#[must_use]
pub fn create_context<'a>(&'a self, engine: &'a Engine) -> NativeCallContext<'a> {
@@ -167,9 +178,15 @@ impl<'a> NativeCallContext<'a> {
/// _(internals)_ Create a [`NativeCallContext`] from a [`NativeCallContextClone`].
/// Exported under the `internals` feature only.
///
/// # WARNING - Unstable API
///
/// This API is volatile and may change in the future.
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
#[cfg(feature = "internals")]
#[inline]
#[must_use]
#[allow(deprecated)]
pub fn from_stored_data(engine: &'a Engine, context: &'a NativeCallContextStore) -> Self {
Self {
engine,
@@ -182,9 +199,15 @@ impl<'a> NativeCallContext<'a> {
}
/// _(internals)_ Store this [`NativeCallContext`] into a [`NativeCallContextClone`].
/// Exported under the `internals` feature only.
///
/// # WARNING - Unstable API
///
/// This API is volatile and may change in the future.
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
#[cfg(feature = "internals")]
#[inline]
#[must_use]
#[allow(deprecated)]
pub fn store_data(&self) -> NativeCallContextStore {
NativeCallContextStore {
fn_name: self.fn_name.to_string(),

View File

@@ -111,10 +111,10 @@ impl Engine {
#[cfg(not(feature = "no_module"))]
let (lib, constants) = if let Some(ref environ) = fn_def.environ {
let crate::ast::EncapsulatedEnviron {
lib: fn_lib,
imports,
constants,
} = environ.as_ref();
lib: ref fn_lib,
ref imports,
ref constants,
} = **environ;
imports
.iter()