Hard code constant checking.
This commit is contained in:
@@ -71,12 +71,12 @@ fn is_numeric(type_id: TypeId) -> bool {
|
||||
|
||||
/// A function that returns `true`.
|
||||
#[inline(always)]
|
||||
fn const_true_fn(_: NativeCallContext, _: &mut [&mut Dynamic]) -> RhaiResult {
|
||||
fn const_true_fn(_: Option<NativeCallContext>, _: &mut [&mut Dynamic]) -> RhaiResult {
|
||||
Ok(Dynamic::TRUE)
|
||||
}
|
||||
/// A function that returns `false`.
|
||||
#[inline(always)]
|
||||
fn const_false_fn(_: NativeCallContext, _: &mut [&mut Dynamic]) -> RhaiResult {
|
||||
fn const_false_fn(_: Option<NativeCallContext>, _: &mut [&mut Dynamic]) -> RhaiResult {
|
||||
Ok(Dynamic::FALSE)
|
||||
}
|
||||
|
||||
@@ -89,55 +89,55 @@ pub fn get_builtin_binary_op_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Option<F
|
||||
let type2 = y.type_id();
|
||||
|
||||
macro_rules! impl_op {
|
||||
($xx:ident $op:tt $yy:ident) => { |_, args| {
|
||||
($xx:ident $op:tt $yy:ident) => { (|_, args| {
|
||||
let x = &*args[0].read_lock::<$xx>().expect(BUILTIN);
|
||||
let y = &*args[1].read_lock::<$yy>().expect(BUILTIN);
|
||||
Ok((x $op y).into())
|
||||
} };
|
||||
($xx:ident . $func:ident ( $yy:ty )) => { |_, args| {
|
||||
}, false) };
|
||||
($xx:ident . $func:ident ( $yy:ty )) => { (|_, args| {
|
||||
let x = &*args[0].read_lock::<$xx>().expect(BUILTIN);
|
||||
let y = &*args[1].read_lock::<$yy>().expect(BUILTIN);
|
||||
Ok(x.$func(y).into())
|
||||
} };
|
||||
($xx:ident . $func:ident ( $yy:ident . $yyy:ident () )) => { |_, args| {
|
||||
}, false) };
|
||||
($xx:ident . $func:ident ( $yy:ident . $yyy:ident () )) => { (|_, args| {
|
||||
let x = &*args[0].read_lock::<$xx>().expect(BUILTIN);
|
||||
let y = &*args[1].read_lock::<$yy>().expect(BUILTIN);
|
||||
Ok(x.$func(y.$yyy()).into())
|
||||
} };
|
||||
($func:ident ( $op:tt )) => { |_, args| {
|
||||
}, false) };
|
||||
($func:ident ( $op:tt )) => { (|_, args| {
|
||||
let (x, y) = $func(args);
|
||||
Ok((x $op y).into())
|
||||
} };
|
||||
($base:ty => $xx:ident $op:tt $yy:ident) => { |_, args| {
|
||||
}, false) };
|
||||
($base:ty => $xx:ident $op:tt $yy:ident) => { (|_, args| {
|
||||
let x = args[0].$xx().expect(BUILTIN) as $base;
|
||||
let y = args[1].$yy().expect(BUILTIN) as $base;
|
||||
Ok((x $op y).into())
|
||||
} };
|
||||
($base:ty => $xx:ident . $func:ident ( $yy:ident as $yyy:ty)) => { |_, args| {
|
||||
}, false) };
|
||||
($base:ty => $xx:ident . $func:ident ( $yy:ident as $yyy:ty)) => { (|_, args| {
|
||||
let x = args[0].$xx().expect(BUILTIN) as $base;
|
||||
let y = args[1].$yy().expect(BUILTIN) as $base;
|
||||
Ok(x.$func(y as $yyy).into())
|
||||
} };
|
||||
($base:ty => $func:ident ( $xx:ident, $yy:ident )) => { |_, args| {
|
||||
}, false) };
|
||||
($base:ty => $func:ident ( $xx:ident, $yy:ident )) => { (|_, args| {
|
||||
let x = args[0].$xx().expect(BUILTIN) as $base;
|
||||
let y = args[1].$yy().expect(BUILTIN) as $base;
|
||||
$func(x, y).map(Into::into)
|
||||
} };
|
||||
(from $base:ty => $xx:ident $op:tt $yy:ident) => { |_, args| {
|
||||
}, false) };
|
||||
(from $base:ty => $xx:ident $op:tt $yy:ident) => { (|_, args| {
|
||||
let x = <$base>::from(args[0].$xx().expect(BUILTIN));
|
||||
let y = <$base>::from(args[1].$yy().expect(BUILTIN));
|
||||
Ok((x $op y).into())
|
||||
} };
|
||||
(from $base:ty => $xx:ident . $func:ident ( $yy:ident )) => { |_, args| {
|
||||
}, false) };
|
||||
(from $base:ty => $xx:ident . $func:ident ( $yy:ident )) => { (|_, args| {
|
||||
let x = <$base>::from(args[0].$xx().expect(BUILTIN));
|
||||
let y = <$base>::from(args[1].$yy().expect(BUILTIN));
|
||||
Ok(x.$func(y).into())
|
||||
} };
|
||||
(from $base:ty => $func:ident ( $xx:ident, $yy:ident )) => { |_, args| {
|
||||
}, false) };
|
||||
(from $base:ty => $func:ident ( $xx:ident, $yy:ident )) => { (|_, args| {
|
||||
let x = <$base>::from(args[0].$xx().expect(BUILTIN));
|
||||
let y = <$base>::from(args[1].$yy().expect(BUILTIN));
|
||||
$func(x, y).map(Into::into)
|
||||
} };
|
||||
}, false) };
|
||||
}
|
||||
|
||||
// Check for common patterns
|
||||
@@ -206,16 +206,20 @@ pub fn get_builtin_binary_op_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Option<F
|
||||
|
||||
if type1 == TypeId::of::<ImmutableString>() {
|
||||
return match op {
|
||||
Plus => Some(|_ctx, args| {
|
||||
let s1 = &*args[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||
let s2 = &*args[1].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||
Plus => Some((
|
||||
|_ctx, args| {
|
||||
let s1 = &*args[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||
let s2 = &*args[1].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.engine()
|
||||
.raise_err_if_over_data_size_limit((0, 0, s1.len() + s2.len()))?;
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.unwrap()
|
||||
.engine()
|
||||
.throw_on_size((0, 0, s1.len() + s2.len()))?;
|
||||
|
||||
Ok((s1 + s2).into())
|
||||
}),
|
||||
Ok((s1 + s2).into())
|
||||
},
|
||||
cfg!(not(feature = "unchecked")),
|
||||
)),
|
||||
Minus => Some(impl_op!(ImmutableString - ImmutableString)),
|
||||
EqualsTo => Some(impl_op!(ImmutableString == ImmutableString)),
|
||||
NotEqualsTo => Some(impl_op!(ImmutableString != ImmutableString)),
|
||||
@@ -229,20 +233,22 @@ pub fn get_builtin_binary_op_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Option<F
|
||||
|
||||
if type1 == TypeId::of::<char>() {
|
||||
return match op {
|
||||
Plus => Some(|_ctx, args| {
|
||||
let x = args[0].as_char().expect(BUILTIN);
|
||||
let y = args[1].as_char().expect(BUILTIN);
|
||||
Plus => Some((
|
||||
|_ctx, args| {
|
||||
let x = args[0].as_char().expect(BUILTIN);
|
||||
let y = args[1].as_char().expect(BUILTIN);
|
||||
|
||||
let mut result = SmartString::new_const();
|
||||
result.push(x);
|
||||
result.push(y);
|
||||
let mut result = SmartString::new_const();
|
||||
result.push(x);
|
||||
result.push(y);
|
||||
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.engine()
|
||||
.raise_err_if_over_data_size_limit((0, 0, result.len()))?;
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.unwrap().engine().throw_on_size((0, 0, result.len()))?;
|
||||
|
||||
Ok(result.into())
|
||||
}),
|
||||
Ok(result.into())
|
||||
},
|
||||
cfg!(not(feature = "unchecked")),
|
||||
)),
|
||||
EqualsTo => Some(impl_op!(char => as_char == as_char)),
|
||||
NotEqualsTo => Some(impl_op!(char => as_char != as_char)),
|
||||
GreaterThan => Some(impl_op!(char => as_char > as_char)),
|
||||
@@ -258,24 +264,28 @@ pub fn get_builtin_binary_op_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Option<F
|
||||
use crate::Blob;
|
||||
|
||||
return match op {
|
||||
Plus => Some(|_ctx, args| {
|
||||
let b2 = &*args[1].read_lock::<Blob>().expect(BUILTIN);
|
||||
if b2.is_empty() {
|
||||
return Ok(args[0].flatten_clone());
|
||||
}
|
||||
let b1 = &*args[0].read_lock::<Blob>().expect(BUILTIN);
|
||||
if b1.is_empty() {
|
||||
return Ok(args[1].flatten_clone());
|
||||
}
|
||||
Plus => Some((
|
||||
|_ctx, args| {
|
||||
let b2 = &*args[1].read_lock::<Blob>().expect(BUILTIN);
|
||||
if b2.is_empty() {
|
||||
return Ok(args[0].flatten_clone());
|
||||
}
|
||||
let b1 = &*args[0].read_lock::<Blob>().expect(BUILTIN);
|
||||
if b1.is_empty() {
|
||||
return Ok(args[1].flatten_clone());
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.engine()
|
||||
.raise_err_if_over_data_size_limit((b1.len() + b2.len(), 0, 0))?;
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.unwrap()
|
||||
.engine()
|
||||
.throw_on_size((b1.len() + b2.len(), 0, 0))?;
|
||||
|
||||
let mut blob = b1.clone();
|
||||
blob.extend(b2);
|
||||
Ok(Dynamic::from_blob(blob))
|
||||
}),
|
||||
let mut blob = b1.clone();
|
||||
blob.extend(b2);
|
||||
Ok(Dynamic::from_blob(blob))
|
||||
},
|
||||
cfg!(not(feature = "unchecked")),
|
||||
)),
|
||||
EqualsTo => Some(impl_op!(Blob == Blob)),
|
||||
NotEqualsTo => Some(impl_op!(Blob != Blob)),
|
||||
_ => None,
|
||||
@@ -284,9 +294,9 @@ pub fn get_builtin_binary_op_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Option<F
|
||||
|
||||
if type1 == TypeId::of::<()>() {
|
||||
return match op {
|
||||
EqualsTo => Some(const_true_fn),
|
||||
EqualsTo => Some((const_true_fn, false)),
|
||||
NotEqualsTo | GreaterThan | GreaterThanEqualsTo | LessThan | LessThanEqualsTo => {
|
||||
Some(const_false_fn)
|
||||
Some((const_false_fn, false))
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
@@ -388,20 +398,22 @@ pub fn get_builtin_binary_op_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Option<F
|
||||
}
|
||||
|
||||
return match op {
|
||||
Plus => Some(|_ctx, args| {
|
||||
let x = args[0].as_char().expect(BUILTIN);
|
||||
let y = &*args[1].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||
Plus => Some((
|
||||
|_ctx, args| {
|
||||
let x = args[0].as_char().expect(BUILTIN);
|
||||
let y = &*args[1].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||
|
||||
let mut result = SmartString::new_const();
|
||||
result.push(x);
|
||||
result.push_str(y);
|
||||
let mut result = SmartString::new_const();
|
||||
result.push(x);
|
||||
result.push_str(y);
|
||||
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.engine()
|
||||
.raise_err_if_over_data_size_limit((0, 0, result.len()))?;
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.unwrap().engine().throw_on_size((0, 0, result.len()))?;
|
||||
|
||||
Ok(result.into())
|
||||
}),
|
||||
Ok(result.into())
|
||||
},
|
||||
cfg!(not(feature = "unchecked")),
|
||||
)),
|
||||
EqualsTo => Some(impl_op!(get_s1s2(==))),
|
||||
NotEqualsTo => Some(impl_op!(get_s1s2(!=))),
|
||||
GreaterThan => Some(impl_op!(get_s1s2(>))),
|
||||
@@ -423,22 +435,27 @@ pub fn get_builtin_binary_op_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Option<F
|
||||
}
|
||||
|
||||
return match op {
|
||||
Plus => Some(|_ctx, args| {
|
||||
let x = &*args[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||
let y = args[1].as_char().expect(BUILTIN);
|
||||
let result = x + y;
|
||||
Plus => Some((
|
||||
|_ctx, args| {
|
||||
let x = &*args[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||
let y = args[1].as_char().expect(BUILTIN);
|
||||
let result = x + y;
|
||||
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.engine()
|
||||
.raise_err_if_over_data_size_limit((0, 0, result.len()))?;
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.unwrap().engine().throw_on_size((0, 0, result.len()))?;
|
||||
|
||||
Ok(result.into())
|
||||
}),
|
||||
Minus => Some(|_, args| {
|
||||
let x = &*args[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||
let y = args[1].as_char().expect(BUILTIN);
|
||||
Ok((x - y).into())
|
||||
}),
|
||||
Ok(result.into())
|
||||
},
|
||||
cfg!(not(feature = "unchecked")),
|
||||
)),
|
||||
Minus => Some((
|
||||
|_, args| {
|
||||
let x = &*args[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||
let y = args[1].as_char().expect(BUILTIN);
|
||||
Ok((x - y).into())
|
||||
},
|
||||
false,
|
||||
)),
|
||||
EqualsTo => Some(impl_op!(get_s1s2(==))),
|
||||
NotEqualsTo => Some(impl_op!(get_s1s2(!=))),
|
||||
GreaterThan => Some(impl_op!(get_s1s2(>))),
|
||||
@@ -451,22 +468,22 @@ pub fn get_builtin_binary_op_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Option<F
|
||||
// () op string
|
||||
if (type1, type2) == (TypeId::of::<()>(), TypeId::of::<ImmutableString>()) {
|
||||
return match op {
|
||||
Plus => Some(|_, args| Ok(args[1].clone())),
|
||||
Plus => Some((|_, args| Ok(args[1].clone()), false)),
|
||||
EqualsTo | GreaterThan | GreaterThanEqualsTo | LessThan | LessThanEqualsTo => {
|
||||
Some(const_false_fn)
|
||||
Some((const_false_fn, false))
|
||||
}
|
||||
NotEqualsTo => Some(const_true_fn),
|
||||
NotEqualsTo => Some((const_true_fn, false)),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
// string op ()
|
||||
if (type1, type2) == (TypeId::of::<ImmutableString>(), TypeId::of::<()>()) {
|
||||
return match op {
|
||||
Plus => Some(|_, args| Ok(args[0].clone())),
|
||||
Plus => Some((|_, args| Ok(args[0].clone()), false)),
|
||||
EqualsTo | GreaterThan | GreaterThanEqualsTo | LessThan | LessThanEqualsTo => {
|
||||
Some(const_false_fn)
|
||||
Some((const_false_fn, false))
|
||||
}
|
||||
NotEqualsTo => Some(const_true_fn),
|
||||
NotEqualsTo => Some((const_true_fn, false)),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
@@ -478,21 +495,22 @@ pub fn get_builtin_binary_op_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Option<F
|
||||
|
||||
if type2 == TypeId::of::<char>() {
|
||||
return match op {
|
||||
Plus => Some(|_ctx, args| {
|
||||
let mut blob = args[0].read_lock::<Blob>().expect(BUILTIN).clone();
|
||||
let mut buf = [0_u8; 4];
|
||||
let x = args[1].as_char().expect(BUILTIN).encode_utf8(&mut buf);
|
||||
Plus => Some((
|
||||
|_ctx, args| {
|
||||
let mut blob = args[0].read_lock::<Blob>().expect(BUILTIN).clone();
|
||||
let mut buf = [0_u8; 4];
|
||||
let x = args[1].as_char().expect(BUILTIN).encode_utf8(&mut buf);
|
||||
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.engine().raise_err_if_over_data_size_limit((
|
||||
blob.len() + x.len(),
|
||||
0,
|
||||
0,
|
||||
))?;
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.unwrap()
|
||||
.engine()
|
||||
.throw_on_size((blob.len() + x.len(), 0, 0))?;
|
||||
|
||||
blob.extend(x.as_bytes());
|
||||
Ok(Dynamic::from_blob(blob))
|
||||
}),
|
||||
blob.extend(x.as_bytes());
|
||||
Ok(Dynamic::from_blob(blob))
|
||||
},
|
||||
cfg!(not(feature = "unchecked")),
|
||||
)),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
@@ -511,8 +529,8 @@ pub fn get_builtin_binary_op_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Option<F
|
||||
)
|
||||
{
|
||||
return match op {
|
||||
NotEqualsTo => Some(const_true_fn),
|
||||
Equals => Some(const_false_fn),
|
||||
NotEqualsTo => Some((const_true_fn, false)),
|
||||
Equals => Some((const_false_fn, false)),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
@@ -542,9 +560,9 @@ pub fn get_builtin_binary_op_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Option<F
|
||||
} else if type1 != type2 {
|
||||
// If the types are not the same, default to not compare
|
||||
match op {
|
||||
NotEqualsTo => Some(const_true_fn),
|
||||
NotEqualsTo => Some((const_true_fn, false)),
|
||||
EqualsTo | GreaterThan | GreaterThanEqualsTo | LessThan | LessThanEqualsTo => {
|
||||
Some(const_false_fn)
|
||||
Some((const_false_fn, false))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
@@ -557,9 +575,9 @@ pub fn get_builtin_binary_op_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Option<F
|
||||
// Default comparison operators for different types
|
||||
if type2 != type1 {
|
||||
return match op {
|
||||
NotEqualsTo => Some(const_true_fn),
|
||||
NotEqualsTo => Some((const_true_fn, false)),
|
||||
EqualsTo | GreaterThan | GreaterThanEqualsTo | LessThan | LessThanEqualsTo => {
|
||||
Some(const_false_fn)
|
||||
Some((const_false_fn, false))
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
@@ -578,43 +596,43 @@ pub fn get_builtin_op_assignment_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Opti
|
||||
let type2 = y.type_id();
|
||||
|
||||
macro_rules! impl_op {
|
||||
($x:ty = x $op:tt $yy:ident) => { |_, args| {
|
||||
($x:ty = x $op:tt $yy:ident) => { (|_, args| {
|
||||
let x = args[0].$yy().expect(BUILTIN);
|
||||
let y = args[1].$yy().expect(BUILTIN) as $x;
|
||||
Ok((*args[0].write_lock::<$x>().expect(BUILTIN) = x $op y).into())
|
||||
} };
|
||||
($x:ident $op:tt $yy:ident) => { |_, args| {
|
||||
}, false) };
|
||||
($x:ident $op:tt $yy:ident) => { (|_, args| {
|
||||
let y = args[1].$yy().expect(BUILTIN) as $x;
|
||||
Ok((*args[0].write_lock::<$x>().expect(BUILTIN) $op y).into())
|
||||
} };
|
||||
($x:ident $op:tt $yy:ident as $yyy:ty) => { |_, args| {
|
||||
}, false) };
|
||||
($x:ident $op:tt $yy:ident as $yyy:ty) => { (|_, args| {
|
||||
let y = args[1].$yy().expect(BUILTIN) as $yyy;
|
||||
Ok((*args[0].write_lock::<$x>().expect(BUILTIN) $op y).into())
|
||||
} };
|
||||
($x:ty => $xx:ident . $func:ident ( $yy:ident as $yyy:ty )) => { |_, args| {
|
||||
}, false) };
|
||||
($x:ty => $xx:ident . $func:ident ( $yy:ident as $yyy:ty )) => { (|_, args| {
|
||||
let x = args[0].$xx().expect(BUILTIN);
|
||||
let y = args[1].$yy().expect(BUILTIN) as $x;
|
||||
Ok((*args[0].write_lock::<$x>().expect(BUILTIN) = x.$func(y as $yyy)).into())
|
||||
} };
|
||||
($x:ty => $func:ident ( $xx:ident, $yy:ident )) => { |_, args| {
|
||||
}, false) };
|
||||
($x:ty => $func:ident ( $xx:ident, $yy:ident )) => { (|_, args| {
|
||||
let x = args[0].$xx().expect(BUILTIN);
|
||||
let y = args[1].$yy().expect(BUILTIN) as $x;
|
||||
Ok((*args[0].write_lock().expect(BUILTIN) = $func(x, y)?).into())
|
||||
} };
|
||||
(from $x:ident $op:tt $yy:ident) => { |_, args| {
|
||||
}, false) };
|
||||
(from $x:ident $op:tt $yy:ident) => { (|_, args| {
|
||||
let y = <$x>::from(args[1].$yy().expect(BUILTIN));
|
||||
Ok((*args[0].write_lock::<$x>().expect(BUILTIN) $op y).into())
|
||||
} };
|
||||
(from $x:ty => $xx:ident . $func:ident ( $yy:ident )) => { |_, args| {
|
||||
}, false) };
|
||||
(from $x:ty => $xx:ident . $func:ident ( $yy:ident )) => { (|_, args| {
|
||||
let x = args[0].$xx().expect(BUILTIN);
|
||||
let y = <$x>::from(args[1].$yy().expect(BUILTIN));
|
||||
Ok((*args[0].write_lock::<$x>().expect(BUILTIN) = x.$func(y)).into())
|
||||
} };
|
||||
(from $x:ty => $func:ident ( $xx:ident, $yy:ident )) => { |_, args| {
|
||||
}, false) };
|
||||
(from $x:ty => $func:ident ( $xx:ident, $yy:ident )) => { (|_, args| {
|
||||
let x = args[0].$xx().expect(BUILTIN);
|
||||
let y = <$x>::from(args[1].$yy().expect(BUILTIN));
|
||||
Ok((*args[0].write_lock().expect(BUILTIN) = $func(x, y)?).into())
|
||||
} };
|
||||
}, false) };
|
||||
}
|
||||
|
||||
// Check for common patterns
|
||||
@@ -668,42 +686,50 @@ pub fn get_builtin_op_assignment_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Opti
|
||||
|
||||
if type1 == TypeId::of::<char>() {
|
||||
return match op {
|
||||
PlusAssign => Some(|_, args| {
|
||||
let y = args[1].as_char().expect(BUILTIN);
|
||||
let x = &mut *args[0].write_lock::<Dynamic>().expect(BUILTIN);
|
||||
PlusAssign => Some((
|
||||
|_, args| {
|
||||
let y = args[1].as_char().expect(BUILTIN);
|
||||
let x = &mut *args[0].write_lock::<Dynamic>().expect(BUILTIN);
|
||||
|
||||
let mut buf = SmartString::new_const();
|
||||
write!(&mut buf, "{y}").unwrap();
|
||||
buf.push(y);
|
||||
let mut buf = SmartString::new_const();
|
||||
write!(&mut buf, "{y}").unwrap();
|
||||
buf.push(y);
|
||||
|
||||
Ok((*x = buf.into()).into())
|
||||
}),
|
||||
Ok((*x = buf.into()).into())
|
||||
},
|
||||
false,
|
||||
)),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
|
||||
if type1 == TypeId::of::<ImmutableString>() {
|
||||
return match op {
|
||||
PlusAssign => Some(|_ctx, args| {
|
||||
let (first, second) = args.split_first_mut().expect(BUILTIN);
|
||||
let x = &mut *first.write_lock::<ImmutableString>().expect(BUILTIN);
|
||||
let y = &*second[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||
PlusAssign => Some((
|
||||
|_ctx, args| {
|
||||
let (first, second) = args.split_first_mut().expect(BUILTIN);
|
||||
let x = &mut *first.write_lock::<ImmutableString>().expect(BUILTIN);
|
||||
let y = &*second[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
if !x.is_empty() && !y.is_empty() {
|
||||
let total_len = x.len() + y.len();
|
||||
_ctx.engine()
|
||||
.raise_err_if_over_data_size_limit((0, 0, total_len))?;
|
||||
}
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
if !x.is_empty() && !y.is_empty() {
|
||||
let total_len = x.len() + y.len();
|
||||
_ctx.unwrap().engine().throw_on_size((0, 0, total_len))?;
|
||||
}
|
||||
|
||||
Ok((*x += y).into())
|
||||
}),
|
||||
MinusAssign => Some(|_, args| {
|
||||
let (first, second) = args.split_first_mut().expect(BUILTIN);
|
||||
let x = &mut *first.write_lock::<ImmutableString>().expect(BUILTIN);
|
||||
let y = &*second[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||
Ok((*x -= y).into())
|
||||
}),
|
||||
Ok((*x += y).into())
|
||||
},
|
||||
cfg!(not(feature = "unchecked")),
|
||||
)),
|
||||
MinusAssign => Some((
|
||||
|_, args| {
|
||||
let (first, second) = args.split_first_mut().expect(BUILTIN);
|
||||
let x = &mut *first.write_lock::<ImmutableString>().expect(BUILTIN);
|
||||
let y = &*second[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||
Ok((*x -= y).into())
|
||||
},
|
||||
false,
|
||||
)),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
@@ -715,27 +741,31 @@ pub fn get_builtin_op_assignment_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Opti
|
||||
use crate::Array;
|
||||
|
||||
return match op {
|
||||
PlusAssign => Some(|_ctx, args| {
|
||||
let x = std::mem::take(args[1]).into_array().expect(BUILTIN);
|
||||
PlusAssign => Some((
|
||||
|_ctx, args| {
|
||||
let x = std::mem::take(args[1]).into_array().expect(BUILTIN);
|
||||
|
||||
if x.is_empty() {
|
||||
return Ok(Dynamic::UNIT);
|
||||
}
|
||||
if x.is_empty() {
|
||||
return Ok(Dynamic::UNIT);
|
||||
}
|
||||
|
||||
let _array_is_empty = args[0].read_lock::<Array>().expect(BUILTIN).is_empty();
|
||||
let _array_is_empty =
|
||||
args[0].read_lock::<Array>().expect(BUILTIN).is_empty();
|
||||
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
if !_array_is_empty {
|
||||
_ctx.engine().check_data_size(
|
||||
&*args[0].read_lock().expect(BUILTIN),
|
||||
crate::Position::NONE,
|
||||
)?;
|
||||
}
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
if !_array_is_empty {
|
||||
_ctx.unwrap().engine().check_data_size(
|
||||
&*args[0].read_lock().expect(BUILTIN),
|
||||
crate::Position::NONE,
|
||||
)?;
|
||||
}
|
||||
|
||||
let array = &mut *args[0].write_lock::<Array>().expect(BUILTIN);
|
||||
let array = &mut *args[0].write_lock::<Array>().expect(BUILTIN);
|
||||
|
||||
Ok(append(array, x).into())
|
||||
}),
|
||||
Ok(append(array, x).into())
|
||||
},
|
||||
cfg!(not(feature = "unchecked")),
|
||||
)),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
@@ -747,19 +777,20 @@ pub fn get_builtin_op_assignment_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Opti
|
||||
use crate::Blob;
|
||||
|
||||
return match op {
|
||||
PlusAssign => Some(|_ctx, args| {
|
||||
let blob2 = std::mem::take(args[1]).into_blob().expect(BUILTIN);
|
||||
let blob1 = &mut *args[0].write_lock::<Blob>().expect(BUILTIN);
|
||||
PlusAssign => Some((
|
||||
|_ctx, args| {
|
||||
let blob2 = std::mem::take(args[1]).into_blob().expect(BUILTIN);
|
||||
let blob1 = &mut *args[0].write_lock::<Blob>().expect(BUILTIN);
|
||||
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.engine().raise_err_if_over_data_size_limit((
|
||||
blob1.len() + blob2.len(),
|
||||
0,
|
||||
0,
|
||||
))?;
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.unwrap()
|
||||
.engine()
|
||||
.throw_on_size((blob1.len() + blob2.len(), 0, 0))?;
|
||||
|
||||
Ok(append(blob1, blob2).into())
|
||||
}),
|
||||
Ok(append(blob1, blob2).into())
|
||||
},
|
||||
cfg!(not(feature = "unchecked")),
|
||||
)),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
@@ -833,17 +864,21 @@ pub fn get_builtin_op_assignment_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Opti
|
||||
// string op= char
|
||||
if (type1, type2) == (TypeId::of::<ImmutableString>(), TypeId::of::<char>()) {
|
||||
return match op {
|
||||
PlusAssign => Some(|_ctx, args| {
|
||||
let mut buf = [0_u8; 4];
|
||||
let ch = &*args[1].as_char().expect(BUILTIN).encode_utf8(&mut buf);
|
||||
let mut x = args[0].write_lock::<ImmutableString>().expect(BUILTIN);
|
||||
PlusAssign => Some((
|
||||
|_ctx, args| {
|
||||
let mut buf = [0_u8; 4];
|
||||
let ch = &*args[1].as_char().expect(BUILTIN).encode_utf8(&mut buf);
|
||||
let mut x = args[0].write_lock::<ImmutableString>().expect(BUILTIN);
|
||||
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.engine()
|
||||
.raise_err_if_over_data_size_limit((0, 0, x.len() + ch.len()))?;
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.unwrap()
|
||||
.engine()
|
||||
.throw_on_size((0, 0, x.len() + ch.len()))?;
|
||||
|
||||
Ok((*x += ch).into())
|
||||
}),
|
||||
Ok((*x += ch).into())
|
||||
},
|
||||
cfg!(not(feature = "unchecked")),
|
||||
)),
|
||||
MinusAssign => Some(impl_op!(ImmutableString -= as_char as char)),
|
||||
_ => None,
|
||||
};
|
||||
@@ -851,28 +886,32 @@ pub fn get_builtin_op_assignment_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Opti
|
||||
// char op= string
|
||||
if (type1, type2) == (TypeId::of::<char>(), TypeId::of::<ImmutableString>()) {
|
||||
return match op {
|
||||
PlusAssign => Some(|_ctx, args| {
|
||||
let ch = {
|
||||
let s = &*args[1].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||
PlusAssign => Some((
|
||||
|_ctx, args| {
|
||||
let ch = {
|
||||
let s = &*args[1].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||
|
||||
if s.is_empty() {
|
||||
return Ok(Dynamic::UNIT);
|
||||
}
|
||||
if s.is_empty() {
|
||||
return Ok(Dynamic::UNIT);
|
||||
}
|
||||
|
||||
let mut ch = args[0].as_char().expect(BUILTIN).to_string();
|
||||
let mut ch = args[0].as_char().expect(BUILTIN).to_string();
|
||||
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.engine()
|
||||
.raise_err_if_over_data_size_limit((0, 0, ch.len() + s.len()))?;
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.unwrap()
|
||||
.engine()
|
||||
.throw_on_size((0, 0, ch.len() + s.len()))?;
|
||||
|
||||
ch.push_str(s);
|
||||
ch
|
||||
};
|
||||
ch.push_str(s);
|
||||
ch
|
||||
};
|
||||
|
||||
*args[0].write_lock::<Dynamic>().expect(BUILTIN) = ch.into();
|
||||
*args[0].write_lock::<Dynamic>().expect(BUILTIN) = ch.into();
|
||||
|
||||
Ok(Dynamic::UNIT)
|
||||
}),
|
||||
Ok(Dynamic::UNIT)
|
||||
},
|
||||
cfg!(not(feature = "unchecked")),
|
||||
)),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
@@ -885,21 +924,24 @@ pub fn get_builtin_op_assignment_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Opti
|
||||
use crate::Array;
|
||||
|
||||
return match op {
|
||||
PlusAssign => Some(|_ctx, args| {
|
||||
{
|
||||
let x = std::mem::take(args[1]);
|
||||
let array = &mut *args[0].write_lock::<Array>().expect(BUILTIN);
|
||||
push(array, x);
|
||||
}
|
||||
PlusAssign => Some((
|
||||
|_ctx, args| {
|
||||
{
|
||||
let x = std::mem::take(args[1]);
|
||||
let array = &mut *args[0].write_lock::<Array>().expect(BUILTIN);
|
||||
push(array, x);
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.engine().check_data_size(
|
||||
&*args[0].read_lock().expect(BUILTIN),
|
||||
crate::Position::NONE,
|
||||
)?;
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.unwrap().engine().check_data_size(
|
||||
&*args[0].read_lock().expect(BUILTIN),
|
||||
crate::Position::NONE,
|
||||
)?;
|
||||
|
||||
Ok(Dynamic::UNIT)
|
||||
}),
|
||||
Ok(Dynamic::UNIT)
|
||||
},
|
||||
cfg!(not(feature = "unchecked")),
|
||||
)),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
@@ -914,16 +956,20 @@ pub fn get_builtin_op_assignment_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Opti
|
||||
use crate::packages::blob_basic::blob_functions::*;
|
||||
|
||||
return match op {
|
||||
PlusAssign => Some(|_ctx, args| {
|
||||
let x = args[1].as_int().expect(BUILTIN);
|
||||
let blob = &mut *args[0].write_lock::<Blob>().expect(BUILTIN);
|
||||
PlusAssign => Some((
|
||||
|_ctx, args| {
|
||||
let x = args[1].as_int().expect(BUILTIN);
|
||||
let blob = &mut *args[0].write_lock::<Blob>().expect(BUILTIN);
|
||||
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.engine()
|
||||
.raise_err_if_over_data_size_limit((blob.len() + 1, 0, 0))?;
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.unwrap()
|
||||
.engine()
|
||||
.throw_on_size((blob.len() + 1, 0, 0))?;
|
||||
|
||||
Ok(push(blob, x).into())
|
||||
}),
|
||||
Ok(push(blob, x).into())
|
||||
},
|
||||
cfg!(not(feature = "unchecked")),
|
||||
)),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
@@ -934,16 +980,20 @@ pub fn get_builtin_op_assignment_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Opti
|
||||
use crate::packages::blob_basic::blob_functions::*;
|
||||
|
||||
return match op {
|
||||
PlusAssign => Some(|_ctx, args| {
|
||||
let x = args[1].as_char().expect(BUILTIN);
|
||||
let blob = &mut *args[0].write_lock::<Blob>().expect(BUILTIN);
|
||||
PlusAssign => Some((
|
||||
|_ctx, args| {
|
||||
let x = args[1].as_char().expect(BUILTIN);
|
||||
let blob = &mut *args[0].write_lock::<Blob>().expect(BUILTIN);
|
||||
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.engine()
|
||||
.raise_err_if_over_data_size_limit((blob.len() + 1, 0, 0))?;
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.unwrap()
|
||||
.engine()
|
||||
.throw_on_size((blob.len() + 1, 0, 0))?;
|
||||
|
||||
Ok(append_char(blob, x).into())
|
||||
}),
|
||||
Ok(append_char(blob, x).into())
|
||||
},
|
||||
cfg!(not(feature = "unchecked")),
|
||||
)),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
@@ -954,24 +1004,25 @@ pub fn get_builtin_op_assignment_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Opti
|
||||
use crate::packages::blob_basic::blob_functions::*;
|
||||
|
||||
return match op {
|
||||
PlusAssign => Some(|_ctx, args| {
|
||||
let (first, second) = args.split_first_mut().expect(BUILTIN);
|
||||
let blob = &mut *first.write_lock::<Blob>().expect(BUILTIN);
|
||||
let s = &*second[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||
PlusAssign => Some((
|
||||
|_ctx, args| {
|
||||
let (first, second) = args.split_first_mut().expect(BUILTIN);
|
||||
let blob = &mut *first.write_lock::<Blob>().expect(BUILTIN);
|
||||
let s = &*second[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||
|
||||
if s.is_empty() {
|
||||
return Ok(Dynamic::UNIT);
|
||||
}
|
||||
if s.is_empty() {
|
||||
return Ok(Dynamic::UNIT);
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.engine().raise_err_if_over_data_size_limit((
|
||||
blob.len() + s.len(),
|
||||
0,
|
||||
0,
|
||||
))?;
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
_ctx.unwrap()
|
||||
.engine()
|
||||
.throw_on_size((blob.len() + s.len(), 0, 0))?;
|
||||
|
||||
Ok(append_str(blob, s).into())
|
||||
}),
|
||||
Ok(append_str(blob, s).into())
|
||||
},
|
||||
cfg!(not(feature = "unchecked")),
|
||||
)),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
|
@@ -271,25 +271,25 @@ impl Engine {
|
||||
}
|
||||
|
||||
// Try to find a built-in version
|
||||
let builtin = args.and_then(|args| match op_token {
|
||||
Token::NONE => None,
|
||||
token if token.is_op_assignment() => {
|
||||
let (first_arg, rest_args) = args.split_first().unwrap();
|
||||
let builtin =
|
||||
args.and_then(|args| match op_token {
|
||||
Token::NONE => None,
|
||||
token if token.is_op_assignment() => {
|
||||
let (first_arg, rest_args) = args.split_first().unwrap();
|
||||
|
||||
get_builtin_op_assignment_fn(token, first_arg, rest_args[0]).map(
|
||||
|f| FnResolutionCacheEntry {
|
||||
func: CallableFunction::Method(Shared::new(f)),
|
||||
get_builtin_op_assignment_fn(token, first_arg, rest_args[0])
|
||||
.map(|(f, ctx)| FnResolutionCacheEntry {
|
||||
func: CallableFunction::Method(Shared::new(f), ctx),
|
||||
source: None,
|
||||
})
|
||||
}
|
||||
token => get_builtin_binary_op_fn(token, args[0], args[1]).map(
|
||||
|(f, ctx)| FnResolutionCacheEntry {
|
||||
func: CallableFunction::Method(Shared::new(f), ctx),
|
||||
source: None,
|
||||
},
|
||||
)
|
||||
}
|
||||
token => get_builtin_binary_op_fn(token, args[0], args[1]).map(|f| {
|
||||
FnResolutionCacheEntry {
|
||||
func: CallableFunction::Method(Shared::new(f)),
|
||||
source: None,
|
||||
}
|
||||
}),
|
||||
});
|
||||
),
|
||||
});
|
||||
|
||||
return if cache.filter.is_absent_and_set(hash) {
|
||||
// Do not cache "one-hit wonders"
|
||||
@@ -400,22 +400,26 @@ impl Engine {
|
||||
// Run external function
|
||||
let is_method = func.is_method();
|
||||
let src = source.as_ref().map(|s| s.as_str());
|
||||
let context = (self, name, src, &*global, pos).into();
|
||||
|
||||
let mut _result = if func.is_plugin_fn() {
|
||||
let f = func.get_plugin_fn().unwrap();
|
||||
let mut _result = if let Some(f) = func.get_plugin_fn() {
|
||||
if !f.is_pure() && !args.is_empty() && args[0].is_read_only() {
|
||||
Err(ERR::ErrorNonPureMethodCallOnConstant(name.to_string(), pos).into())
|
||||
} else {
|
||||
let context = (self, name, src, &*global, pos).into();
|
||||
f.call(context, args)
|
||||
.and_then(|r| self.check_data_size(r, pos))
|
||||
.map_err(|err| err.fill_position(pos))
|
||||
}
|
||||
} else if let Some(f) = func.get_native_fn() {
|
||||
let context = if func.has_context() {
|
||||
Some((self, name, src, &*global, pos).into())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
f(context, args)
|
||||
} else {
|
||||
func.get_native_fn().unwrap()(context, args)
|
||||
.and_then(|r| self.check_data_size(r, pos))
|
||||
.map_err(|err| err.fill_position(pos))
|
||||
};
|
||||
unreachable!();
|
||||
}
|
||||
.and_then(|r| self.check_data_size(r, pos))
|
||||
.map_err(|err| err.fill_position(pos));
|
||||
|
||||
#[cfg(feature = "debugging")]
|
||||
if self.is_debugger_registered() {
|
||||
@@ -1398,7 +1402,11 @@ impl Engine {
|
||||
|
||||
Some(f) if f.is_native() => {
|
||||
let func = f.get_native_fn().expect("native function");
|
||||
let context = (self, fn_name, module.id(), &*global, pos).into();
|
||||
let context = if f.has_context() {
|
||||
Some((self, fn_name, module.id(), &*global, pos).into())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
func(context, &mut args).and_then(|r| self.check_data_size(r, pos))
|
||||
}
|
||||
|
||||
@@ -1518,14 +1526,19 @@ impl Engine {
|
||||
|
||||
let operands = &mut [&mut lhs, &mut rhs];
|
||||
|
||||
if let Some(func) = get_builtin_binary_op_fn(op_token.clone(), operands[0], operands[1])
|
||||
if let Some((func, ctx)) =
|
||||
get_builtin_binary_op_fn(op_token.clone(), operands[0], operands[1])
|
||||
{
|
||||
// Built-in found
|
||||
let orig_level = global.level;
|
||||
global.level += 1;
|
||||
let global = &*RestoreOnDrop::lock(global, move |g| g.level = orig_level);
|
||||
|
||||
let context = (self, name.as_str(), None, global, pos).into();
|
||||
let context = if ctx {
|
||||
Some((self, name.as_str(), None, global, pos).into())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
return func(context, operands);
|
||||
}
|
||||
|
||||
|
@@ -14,10 +14,10 @@ use std::prelude::v1::*;
|
||||
#[non_exhaustive]
|
||||
pub enum CallableFunction {
|
||||
/// A pure native Rust function with all arguments passed by value.
|
||||
Pure(Shared<FnAny>),
|
||||
Pure(Shared<FnAny>, bool),
|
||||
/// A native Rust object method with the first argument passed by reference,
|
||||
/// and the rest passed by value.
|
||||
Method(Shared<FnAny>),
|
||||
Method(Shared<FnAny>, bool),
|
||||
/// An iterator function.
|
||||
Iterator(Shared<IteratorFn>),
|
||||
/// A plugin function,
|
||||
@@ -136,6 +136,17 @@ impl CallableFunction {
|
||||
Self::Script(..) => false,
|
||||
}
|
||||
}
|
||||
/// Is there a [`NativeCallContext`] parameter?
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn has_context(&self) -> bool {
|
||||
match self {
|
||||
Self::Pure(.., ctx) | Self::Method(.., ctx) => *ctx,
|
||||
Self::Plugin(..) | Self::Iterator(..) => false,
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
Self::Script(..) => false,
|
||||
}
|
||||
}
|
||||
/// Get the access mode.
|
||||
#[inline]
|
||||
#[must_use]
|
||||
@@ -156,7 +167,7 @@ impl CallableFunction {
|
||||
#[must_use]
|
||||
pub fn get_native_fn(&self) -> Option<&Shared<FnAny>> {
|
||||
match self {
|
||||
Self::Pure(f) | Self::Method(f) => Some(f),
|
||||
Self::Pure(f, ..) | Self::Method(f, ..) => Some(f),
|
||||
Self::Iterator(..) | Self::Plugin(..) => None,
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
|
@@ -546,13 +546,16 @@ pub fn locked_write<T>(value: &Locked<T>) -> LockGuardMut<T> {
|
||||
|
||||
/// General Rust function trail object.
|
||||
#[cfg(not(feature = "sync"))]
|
||||
pub type FnAny = dyn Fn(NativeCallContext, &mut FnCallArgs) -> RhaiResult;
|
||||
pub type FnAny = dyn Fn(Option<NativeCallContext>, &mut FnCallArgs) -> RhaiResult;
|
||||
/// General Rust function trail object.
|
||||
#[cfg(feature = "sync")]
|
||||
pub type FnAny = dyn Fn(NativeCallContext, &mut FnCallArgs) -> RhaiResult + Send + Sync;
|
||||
pub type FnAny = dyn Fn(Option<NativeCallContext>, &mut FnCallArgs) -> RhaiResult + Send + Sync;
|
||||
|
||||
/// Built-in function trait object.
|
||||
pub type FnBuiltin = fn(NativeCallContext, &mut FnCallArgs) -> RhaiResult;
|
||||
pub type FnBuiltin = (
|
||||
fn(Option<NativeCallContext>, &mut FnCallArgs) -> RhaiResult,
|
||||
bool,
|
||||
);
|
||||
|
||||
/// Function that gets an iterator from a type.
|
||||
#[cfg(not(feature = "sync"))]
|
||||
|
@@ -9,7 +9,7 @@ use super::call::FnCallArgs;
|
||||
use super::callable_function::CallableFunction;
|
||||
use super::native::{SendSync, Shared};
|
||||
use crate::types::dynamic::{DynamicWriteLock, Variant};
|
||||
use crate::{reify, Dynamic, NativeCallContext, RhaiResultOf};
|
||||
use crate::{reify, Dynamic, Identifier, NativeCallContext, RhaiResultOf};
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
use std::{
|
||||
@@ -78,13 +78,19 @@ pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T {
|
||||
pub trait RegisterNativeFunction<ARGS, const NUM: usize, const CTX: bool, RET, const FALL: bool> {
|
||||
/// Convert this function into a [`CallableFunction`].
|
||||
#[must_use]
|
||||
fn into_callable_function(self) -> CallableFunction;
|
||||
fn into_callable_function(self, name: Identifier, no_const: bool) -> CallableFunction;
|
||||
/// Get the type ID's of this function's parameters.
|
||||
#[must_use]
|
||||
fn param_types() -> [TypeId; NUM];
|
||||
/// Get the number of parameters for this function.
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
fn num_params() -> usize;
|
||||
fn num_params() -> usize {
|
||||
NUM
|
||||
}
|
||||
/// Is there a [`NativeCallContext`] parameter for this function?
|
||||
#[must_use]
|
||||
fn has_context() -> bool;
|
||||
/// _(metadata)_ Get the type names of this function's parameters.
|
||||
/// Exported under the `metadata` feature only.
|
||||
#[cfg(feature = "metadata")]
|
||||
@@ -106,27 +112,14 @@ pub trait RegisterNativeFunction<ARGS, const NUM: usize, const CTX: bool, RET, c
|
||||
}
|
||||
|
||||
macro_rules! check_constant {
|
||||
($abi:ident, $n:expr, $ctx:ident, $args:ident) => {
|
||||
($abi:ident, $n:expr, $fn_name:ident, $no_const:ident, $args:ident) => {
|
||||
#[cfg(any(not(feature = "no_object"), not(feature = "no_index")))]
|
||||
if stringify!($abi) == "Method" {
|
||||
let mut deny = false;
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
if $n == 3 && !deny {
|
||||
deny = $ctx.fn_name() == crate::engine::FN_IDX_SET && $args[0].is_read_only();
|
||||
}
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
if $n == 2 && !deny {
|
||||
deny = $ctx.fn_name().starts_with(crate::engine::FN_SET) && $args[0].is_read_only();
|
||||
}
|
||||
|
||||
if deny {
|
||||
return Err(crate::ERR::ErrorNonPureMethodCallOnConstant(
|
||||
$ctx.fn_name().to_string(),
|
||||
crate::Position::NONE,
|
||||
)
|
||||
.into());
|
||||
}
|
||||
if stringify!($abi) == "Method" && $no_const && $args[0].is_read_only() {
|
||||
return Err(crate::ERR::ErrorNonPureMethodCallOnConstant(
|
||||
$fn_name.to_string(),
|
||||
crate::Position::NONE,
|
||||
)
|
||||
.into());
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -147,16 +140,16 @@ macro_rules! def_register {
|
||||
impl<
|
||||
FN: Fn($($param),*) -> RET + SendSync + 'static,
|
||||
$($par: Variant + Clone,)*
|
||||
RET: Variant + Clone
|
||||
RET: Variant + Clone,
|
||||
> RegisterNativeFunction<($($mark,)*), $n, false, RET, false> for FN {
|
||||
#[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] }
|
||||
#[inline(always)] fn num_params() -> usize { $n }
|
||||
#[inline(always)] fn has_context() -> bool { false }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> [&'static str;$n] { [$(type_name::<$param>()),*] }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RET>() }
|
||||
#[inline(always)] fn into_callable_function(self) -> CallableFunction {
|
||||
CallableFunction::$abi(Shared::new(move |ctx: NativeCallContext, args: &mut FnCallArgs| {
|
||||
#[inline(always)] fn into_callable_function(self, fn_name: Identifier, no_const: bool) -> CallableFunction {
|
||||
CallableFunction::$abi(Shared::new(move |_, args: &mut FnCallArgs| {
|
||||
// The arguments are assumed to be of the correct number and types!
|
||||
check_constant!($abi, $n, ctx, args);
|
||||
check_constant!($abi, $n, fn_name, no_const, args);
|
||||
|
||||
let mut drain = args.iter_mut();
|
||||
$(let mut $par = $clone(drain.next().unwrap()); )*
|
||||
@@ -166,23 +159,25 @@ macro_rules! def_register {
|
||||
|
||||
// Map the result
|
||||
Ok(Dynamic::from(r))
|
||||
}))
|
||||
}), false)
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
FN: for<'a> Fn(NativeCallContext<'a>, $($param),*) -> RET + SendSync + 'static,
|
||||
$($par: Variant + Clone,)*
|
||||
RET: Variant + Clone
|
||||
RET: Variant + Clone,
|
||||
> RegisterNativeFunction<($($mark,)*), $n, true, RET, false> for FN {
|
||||
#[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] }
|
||||
#[inline(always)] fn num_params() -> usize { $n }
|
||||
#[inline(always)] fn has_context() -> bool { true }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> [&'static str;$n] { [$(type_name::<$param>()),*] }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RET>() }
|
||||
#[inline(always)] fn into_callable_function(self) -> CallableFunction {
|
||||
CallableFunction::$abi(Shared::new(move |ctx: NativeCallContext, args: &mut FnCallArgs| {
|
||||
#[inline(always)] fn into_callable_function(self, fn_name: Identifier, no_const: bool) -> CallableFunction {
|
||||
CallableFunction::$abi(Shared::new(move |ctx: Option<NativeCallContext>, args: &mut FnCallArgs| {
|
||||
let ctx = ctx.unwrap();
|
||||
|
||||
// The arguments are assumed to be of the correct number and types!
|
||||
check_constant!($abi, $n, ctx, args);
|
||||
check_constant!($abi, $n, fn_name, no_const, args);
|
||||
|
||||
let mut drain = args.iter_mut();
|
||||
$(let mut $par = $clone(drain.next().unwrap()); )*
|
||||
@@ -192,7 +187,7 @@ macro_rules! def_register {
|
||||
|
||||
// Map the result
|
||||
Ok(Dynamic::from(r))
|
||||
}))
|
||||
}), true)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,21 +197,21 @@ macro_rules! def_register {
|
||||
RET: Variant + Clone
|
||||
> RegisterNativeFunction<($($mark,)*), $n, false, RET, true> for FN {
|
||||
#[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] }
|
||||
#[inline(always)] fn num_params() -> usize { $n }
|
||||
#[inline(always)] fn has_context() -> bool { false }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> [&'static str;$n] { [$(type_name::<$param>()),*] }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RhaiResultOf<RET>>() }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn return_type_name() -> &'static str { type_name::<RhaiResultOf<RET>>() }
|
||||
#[inline(always)] fn into_callable_function(self) -> CallableFunction {
|
||||
CallableFunction::$abi(Shared::new(move |ctx: NativeCallContext, args: &mut FnCallArgs| {
|
||||
#[inline(always)] fn into_callable_function(self, fn_name: Identifier, no_const: bool) -> CallableFunction {
|
||||
CallableFunction::$abi(Shared::new(move |_, args: &mut FnCallArgs| {
|
||||
// The arguments are assumed to be of the correct number and types!
|
||||
check_constant!($abi, $n, ctx, args);
|
||||
check_constant!($abi, $n, fn_name, no_const, args);
|
||||
|
||||
let mut drain = args.iter_mut();
|
||||
$(let mut $par = $clone(drain.next().unwrap()); )*
|
||||
|
||||
// Call the function with each argument value
|
||||
self($($arg),*).map(Dynamic::from)
|
||||
}))
|
||||
}), false)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,21 +221,23 @@ macro_rules! def_register {
|
||||
RET: Variant + Clone
|
||||
> RegisterNativeFunction<($($mark,)*), $n, true, RET, true> for FN {
|
||||
#[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] }
|
||||
#[inline(always)] fn num_params() -> usize { $n }
|
||||
#[inline(always)] fn has_context() -> bool { true }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> [&'static str;$n] { [$(type_name::<$param>()),*] }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RhaiResultOf<RET>>() }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn return_type_name() -> &'static str { type_name::<RhaiResultOf<RET>>() }
|
||||
#[inline(always)] fn into_callable_function(self) -> CallableFunction {
|
||||
CallableFunction::$abi(Shared::new(move |ctx: NativeCallContext, args: &mut FnCallArgs| {
|
||||
#[inline(always)] fn into_callable_function(self, fn_name: Identifier, no_const: bool) -> CallableFunction {
|
||||
CallableFunction::$abi(Shared::new(move |ctx: Option<NativeCallContext>, args: &mut FnCallArgs| {
|
||||
let ctx = ctx.unwrap();
|
||||
|
||||
// The arguments are assumed to be of the correct number and types!
|
||||
check_constant!($abi, $n, ctx, args);
|
||||
check_constant!($abi, $n, fn_name, no_const, args);
|
||||
|
||||
let mut drain = args.iter_mut();
|
||||
$(let mut $par = $clone(drain.next().unwrap()); )*
|
||||
|
||||
// Call the function with each argument value
|
||||
self(ctx, $($arg),*).map(Dynamic::from)
|
||||
}))
|
||||
}), true)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user