Merge branch 'master' into plugins

This commit is contained in:
Stephen Chung
2020-06-08 10:26:51 +08:00
6 changed files with 108 additions and 21 deletions

View File

@@ -8,6 +8,7 @@ use crate::fn_native::{CallableFunction, FnAny, FnCallArgs};
use crate::parser::FnAccess;
use crate::plugin::Plugin;
use crate::result::EvalAltResult;
use crate::utils::ImmutableString;
use crate::stdlib::{any::TypeId, boxed::Box, mem};
@@ -179,9 +180,16 @@ pub fn by_ref<T: Variant + Clone>(data: &mut Dynamic) -> &mut T {
/// Dereference into value.
#[inline(always)]
pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T {
// We consume the argument and then replace it with () - the argument is not supposed to be used again.
// This way, we avoid having to clone the argument again, because it is already a clone when passed here.
mem::take(data).cast::<T>()
if TypeId::of::<T>() == TypeId::of::<&str>() {
// If T is &str, data must be ImmutableString, so map directly to it
let ref_str = data.as_str().unwrap();
let ref_T = unsafe { mem::transmute::<_, &T>(&ref_str) };
ref_T.clone()
} else {
// We consume the argument and then replace it with () - the argument is not supposed to be used again.
// This way, we avoid having to clone the argument again, because it is already a clone when passed here.
mem::take(data).cast::<T>()
}
}
impl<PL: Plugin> RegisterPlugin<PL> for Engine {
@@ -232,6 +240,18 @@ pub fn map_result(
data
}
/// Remap `&str` to `ImmutableString`.
#[inline(always)]
fn map_type_id<T: 'static>() -> TypeId {
let id = TypeId::of::<T>();
if id == TypeId::of::<&str>() {
TypeId::of::<ImmutableString>()
} else {
id
}
}
macro_rules! def_register {
() => {
def_register!(imp from_pure :);
@@ -256,7 +276,7 @@ macro_rules! def_register {
{
fn register_fn(&mut self, name: &str, f: FN) {
self.global_module.set_fn(name, FnAccess::Public,
&[$(TypeId::of::<$par>()),*],
&[$(map_type_id::<$par>()),*],
CallableFunction::$abi(make_func!(f : map_dynamic ; $($par => $clone),*))
);
}
@@ -273,7 +293,7 @@ macro_rules! def_register {
{
fn register_result_fn(&mut self, name: &str, f: FN) {
self.global_module.set_fn(name, FnAccess::Public,
&[$(TypeId::of::<$par>()),*],
&[$(map_type_id::<$par>()),*],
CallableFunction::$abi(make_func!(f : map_result ; $($par => $clone),*))
);
}