Hard code constant checking.
This commit is contained in:
parent
ffc8a7f85c
commit
55922b5c20
@ -90,13 +90,24 @@ impl Engine {
|
|||||||
#[cfg(not(feature = "metadata"))]
|
#[cfg(not(feature = "metadata"))]
|
||||||
let param_type_names: Option<&[&str]> = None;
|
let param_type_names: Option<&[&str]> = None;
|
||||||
|
|
||||||
|
let fn_name = name.as_ref();
|
||||||
|
let no_const = false;
|
||||||
|
|
||||||
|
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
|
||||||
|
let no_const = no_const || (F::num_params() == 3 && fn_name == crate::engine::FN_IDX_SET);
|
||||||
|
#[cfg(not(feature = "no_object"))]
|
||||||
|
let no_const =
|
||||||
|
no_const || (F::num_params() == 2 && fn_name.starts_with(crate::engine::FN_SET));
|
||||||
|
|
||||||
|
let func = func.into_callable_function(fn_name.into(), no_const);
|
||||||
|
|
||||||
self.global_namespace_mut().set_fn(
|
self.global_namespace_mut().set_fn(
|
||||||
name,
|
name,
|
||||||
FnNamespace::Global,
|
FnNamespace::Global,
|
||||||
FnAccess::Public,
|
FnAccess::Public,
|
||||||
param_type_names,
|
param_type_names,
|
||||||
param_types,
|
param_types,
|
||||||
func.into_callable_function(),
|
func,
|
||||||
);
|
);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -56,7 +56,7 @@ pub struct ScriptFnDef {
|
|||||||
///
|
///
|
||||||
/// Each line in non-block doc-comments starts with `///`.
|
/// Each line in non-block doc-comments starts with `///`.
|
||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
pub comments: Box<[Box<str>]>,
|
pub comments: Box<[crate::Identifier]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for ScriptFnDef {
|
impl fmt::Display for ScriptFnDef {
|
||||||
|
@ -74,10 +74,7 @@ impl Engine {
|
|||||||
///
|
///
|
||||||
/// [`Position`] in [`EvalAltResult`][crate::EvalAltResult] is always [`NONE`][Position::NONE]
|
/// [`Position`] in [`EvalAltResult`][crate::EvalAltResult] is always [`NONE`][Position::NONE]
|
||||||
/// and should be set afterwards.
|
/// and should be set afterwards.
|
||||||
pub(crate) fn raise_err_if_over_data_size_limit(
|
pub(crate) fn throw_on_size(&self, (_arr, _map, s): (usize, usize, usize)) -> RhaiResultOf<()> {
|
||||||
&self,
|
|
||||||
(_arr, _map, s): (usize, usize, usize),
|
|
||||||
) -> RhaiResultOf<()> {
|
|
||||||
if self
|
if self
|
||||||
.limits
|
.limits
|
||||||
.max_string_len
|
.max_string_len
|
||||||
@ -127,9 +124,10 @@ impl Engine {
|
|||||||
|
|
||||||
let sizes = value.borrow().calc_data_sizes(true);
|
let sizes = value.borrow().calc_data_sizes(true);
|
||||||
|
|
||||||
self.raise_err_if_over_data_size_limit(sizes)
|
self.throw_on_size(sizes)
|
||||||
.map(|_| value)
|
.map_err(|err| err.fill_position(pos))?;
|
||||||
.map_err(|err| err.fill_position(pos))
|
|
||||||
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Raise an error if the size of a [`Dynamic`] is out of limits (if any).
|
/// Raise an error if the size of a [`Dynamic`] is out of limits (if any).
|
||||||
|
@ -329,7 +329,7 @@ impl Engine {
|
|||||||
total_data_sizes.1 + val_sizes.1,
|
total_data_sizes.1 + val_sizes.1,
|
||||||
total_data_sizes.2 + val_sizes.2,
|
total_data_sizes.2 + val_sizes.2,
|
||||||
);
|
);
|
||||||
self.raise_err_if_over_data_size_limit(total_data_sizes)
|
self.throw_on_size(total_data_sizes)
|
||||||
.map_err(|err| err.fill_position(item_expr.position()))?;
|
.map_err(|err| err.fill_position(item_expr.position()))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -360,7 +360,7 @@ impl Engine {
|
|||||||
total_data_sizes.1 + delta.1,
|
total_data_sizes.1 + delta.1,
|
||||||
total_data_sizes.2 + delta.2,
|
total_data_sizes.2 + delta.2,
|
||||||
);
|
);
|
||||||
self.raise_err_if_over_data_size_limit(total_data_sizes)
|
self.throw_on_size(total_data_sizes)
|
||||||
.map_err(|err| err.fill_position(value_expr.position()))?;
|
.map_err(|err| err.fill_position(value_expr.position()))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,7 +142,7 @@ impl Engine {
|
|||||||
let args = &mut [&mut *lock_guard, &mut new_val];
|
let args = &mut [&mut *lock_guard, &mut new_val];
|
||||||
|
|
||||||
if self.fast_operators() {
|
if self.fast_operators() {
|
||||||
if let Some(func) =
|
if let Some((func, ctx)) =
|
||||||
get_builtin_op_assignment_fn(op_assign_token.clone(), args[0], args[1])
|
get_builtin_op_assignment_fn(op_assign_token.clone(), args[0], args[1])
|
||||||
{
|
{
|
||||||
// Built-in found
|
// Built-in found
|
||||||
@ -152,7 +152,11 @@ impl Engine {
|
|||||||
global.level += 1;
|
global.level += 1;
|
||||||
let global = &*RestoreOnDrop::lock(global, move |g| g.level = orig_level);
|
let global = &*RestoreOnDrop::lock(global, move |g| g.level = orig_level);
|
||||||
|
|
||||||
let context = (self, op, None, global, *op_pos).into();
|
let context = if ctx {
|
||||||
|
Some((self, op, None, global, *op_pos).into())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
return func(context, args).map(|_| ());
|
return func(context, args).map(|_| ());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,12 +71,12 @@ fn is_numeric(type_id: TypeId) -> bool {
|
|||||||
|
|
||||||
/// A function that returns `true`.
|
/// A function that returns `true`.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn const_true_fn(_: NativeCallContext, _: &mut [&mut Dynamic]) -> RhaiResult {
|
fn const_true_fn(_: Option<NativeCallContext>, _: &mut [&mut Dynamic]) -> RhaiResult {
|
||||||
Ok(Dynamic::TRUE)
|
Ok(Dynamic::TRUE)
|
||||||
}
|
}
|
||||||
/// A function that returns `false`.
|
/// A function that returns `false`.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn const_false_fn(_: NativeCallContext, _: &mut [&mut Dynamic]) -> RhaiResult {
|
fn const_false_fn(_: Option<NativeCallContext>, _: &mut [&mut Dynamic]) -> RhaiResult {
|
||||||
Ok(Dynamic::FALSE)
|
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();
|
let type2 = y.type_id();
|
||||||
|
|
||||||
macro_rules! impl_op {
|
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 x = &*args[0].read_lock::<$xx>().expect(BUILTIN);
|
||||||
let y = &*args[1].read_lock::<$yy>().expect(BUILTIN);
|
let y = &*args[1].read_lock::<$yy>().expect(BUILTIN);
|
||||||
Ok((x $op y).into())
|
Ok((x $op y).into())
|
||||||
} };
|
}, false) };
|
||||||
($xx:ident . $func:ident ( $yy:ty )) => { |_, args| {
|
($xx:ident . $func:ident ( $yy:ty )) => { (|_, args| {
|
||||||
let x = &*args[0].read_lock::<$xx>().expect(BUILTIN);
|
let x = &*args[0].read_lock::<$xx>().expect(BUILTIN);
|
||||||
let y = &*args[1].read_lock::<$yy>().expect(BUILTIN);
|
let y = &*args[1].read_lock::<$yy>().expect(BUILTIN);
|
||||||
Ok(x.$func(y).into())
|
Ok(x.$func(y).into())
|
||||||
} };
|
}, false) };
|
||||||
($xx:ident . $func:ident ( $yy:ident . $yyy:ident () )) => { |_, args| {
|
($xx:ident . $func:ident ( $yy:ident . $yyy:ident () )) => { (|_, args| {
|
||||||
let x = &*args[0].read_lock::<$xx>().expect(BUILTIN);
|
let x = &*args[0].read_lock::<$xx>().expect(BUILTIN);
|
||||||
let y = &*args[1].read_lock::<$yy>().expect(BUILTIN);
|
let y = &*args[1].read_lock::<$yy>().expect(BUILTIN);
|
||||||
Ok(x.$func(y.$yyy()).into())
|
Ok(x.$func(y.$yyy()).into())
|
||||||
} };
|
}, false) };
|
||||||
($func:ident ( $op:tt )) => { |_, args| {
|
($func:ident ( $op:tt )) => { (|_, args| {
|
||||||
let (x, y) = $func(args);
|
let (x, y) = $func(args);
|
||||||
Ok((x $op y).into())
|
Ok((x $op y).into())
|
||||||
} };
|
}, false) };
|
||||||
($base:ty => $xx:ident $op:tt $yy:ident) => { |_, args| {
|
($base:ty => $xx:ident $op:tt $yy:ident) => { (|_, args| {
|
||||||
let x = args[0].$xx().expect(BUILTIN) as $base;
|
let x = args[0].$xx().expect(BUILTIN) as $base;
|
||||||
let y = args[1].$yy().expect(BUILTIN) as $base;
|
let y = args[1].$yy().expect(BUILTIN) as $base;
|
||||||
Ok((x $op y).into())
|
Ok((x $op y).into())
|
||||||
} };
|
}, false) };
|
||||||
($base:ty => $xx:ident . $func:ident ( $yy:ident as $yyy:ty)) => { |_, args| {
|
($base:ty => $xx:ident . $func:ident ( $yy:ident as $yyy:ty)) => { (|_, args| {
|
||||||
let x = args[0].$xx().expect(BUILTIN) as $base;
|
let x = args[0].$xx().expect(BUILTIN) as $base;
|
||||||
let y = args[1].$yy().expect(BUILTIN) as $base;
|
let y = args[1].$yy().expect(BUILTIN) as $base;
|
||||||
Ok(x.$func(y as $yyy).into())
|
Ok(x.$func(y as $yyy).into())
|
||||||
} };
|
}, false) };
|
||||||
($base:ty => $func:ident ( $xx:ident, $yy:ident )) => { |_, args| {
|
($base:ty => $func:ident ( $xx:ident, $yy:ident )) => { (|_, args| {
|
||||||
let x = args[0].$xx().expect(BUILTIN) as $base;
|
let x = args[0].$xx().expect(BUILTIN) as $base;
|
||||||
let y = args[1].$yy().expect(BUILTIN) as $base;
|
let y = args[1].$yy().expect(BUILTIN) as $base;
|
||||||
$func(x, y).map(Into::into)
|
$func(x, y).map(Into::into)
|
||||||
} };
|
}, false) };
|
||||||
(from $base:ty => $xx:ident $op:tt $yy:ident) => { |_, args| {
|
(from $base:ty => $xx:ident $op:tt $yy:ident) => { (|_, args| {
|
||||||
let x = <$base>::from(args[0].$xx().expect(BUILTIN));
|
let x = <$base>::from(args[0].$xx().expect(BUILTIN));
|
||||||
let y = <$base>::from(args[1].$yy().expect(BUILTIN));
|
let y = <$base>::from(args[1].$yy().expect(BUILTIN));
|
||||||
Ok((x $op y).into())
|
Ok((x $op y).into())
|
||||||
} };
|
}, false) };
|
||||||
(from $base:ty => $xx:ident . $func:ident ( $yy:ident )) => { |_, args| {
|
(from $base:ty => $xx:ident . $func:ident ( $yy:ident )) => { (|_, args| {
|
||||||
let x = <$base>::from(args[0].$xx().expect(BUILTIN));
|
let x = <$base>::from(args[0].$xx().expect(BUILTIN));
|
||||||
let y = <$base>::from(args[1].$yy().expect(BUILTIN));
|
let y = <$base>::from(args[1].$yy().expect(BUILTIN));
|
||||||
Ok(x.$func(y).into())
|
Ok(x.$func(y).into())
|
||||||
} };
|
}, false) };
|
||||||
(from $base:ty => $func:ident ( $xx:ident, $yy:ident )) => { |_, args| {
|
(from $base:ty => $func:ident ( $xx:ident, $yy:ident )) => { (|_, args| {
|
||||||
let x = <$base>::from(args[0].$xx().expect(BUILTIN));
|
let x = <$base>::from(args[0].$xx().expect(BUILTIN));
|
||||||
let y = <$base>::from(args[1].$yy().expect(BUILTIN));
|
let y = <$base>::from(args[1].$yy().expect(BUILTIN));
|
||||||
$func(x, y).map(Into::into)
|
$func(x, y).map(Into::into)
|
||||||
} };
|
}, false) };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for common patterns
|
// 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>() {
|
if type1 == TypeId::of::<ImmutableString>() {
|
||||||
return match op {
|
return match op {
|
||||||
Plus => Some(|_ctx, args| {
|
Plus => Some((
|
||||||
let s1 = &*args[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
|_ctx, args| {
|
||||||
let s2 = &*args[1].read_lock::<ImmutableString>().expect(BUILTIN);
|
let s1 = &*args[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||||
|
let s2 = &*args[1].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||||
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
_ctx.engine()
|
_ctx.unwrap()
|
||||||
.raise_err_if_over_data_size_limit((0, 0, s1.len() + s2.len()))?;
|
.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)),
|
Minus => Some(impl_op!(ImmutableString - ImmutableString)),
|
||||||
EqualsTo => Some(impl_op!(ImmutableString == ImmutableString)),
|
EqualsTo => Some(impl_op!(ImmutableString == ImmutableString)),
|
||||||
NotEqualsTo => 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>() {
|
if type1 == TypeId::of::<char>() {
|
||||||
return match op {
|
return match op {
|
||||||
Plus => Some(|_ctx, args| {
|
Plus => Some((
|
||||||
let x = args[0].as_char().expect(BUILTIN);
|
|_ctx, args| {
|
||||||
let y = args[1].as_char().expect(BUILTIN);
|
let x = args[0].as_char().expect(BUILTIN);
|
||||||
|
let y = args[1].as_char().expect(BUILTIN);
|
||||||
|
|
||||||
let mut result = SmartString::new_const();
|
let mut result = SmartString::new_const();
|
||||||
result.push(x);
|
result.push(x);
|
||||||
result.push(y);
|
result.push(y);
|
||||||
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
_ctx.engine()
|
_ctx.unwrap().engine().throw_on_size((0, 0, result.len()))?;
|
||||||
.raise_err_if_over_data_size_limit((0, 0, result.len()))?;
|
|
||||||
|
|
||||||
Ok(result.into())
|
Ok(result.into())
|
||||||
}),
|
},
|
||||||
|
cfg!(not(feature = "unchecked")),
|
||||||
|
)),
|
||||||
EqualsTo => Some(impl_op!(char => as_char == as_char)),
|
EqualsTo => Some(impl_op!(char => as_char == as_char)),
|
||||||
NotEqualsTo => 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)),
|
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;
|
use crate::Blob;
|
||||||
|
|
||||||
return match op {
|
return match op {
|
||||||
Plus => Some(|_ctx, args| {
|
Plus => Some((
|
||||||
let b2 = &*args[1].read_lock::<Blob>().expect(BUILTIN);
|
|_ctx, args| {
|
||||||
if b2.is_empty() {
|
let b2 = &*args[1].read_lock::<Blob>().expect(BUILTIN);
|
||||||
return Ok(args[0].flatten_clone());
|
if b2.is_empty() {
|
||||||
}
|
return Ok(args[0].flatten_clone());
|
||||||
let b1 = &*args[0].read_lock::<Blob>().expect(BUILTIN);
|
}
|
||||||
if b1.is_empty() {
|
let b1 = &*args[0].read_lock::<Blob>().expect(BUILTIN);
|
||||||
return Ok(args[1].flatten_clone());
|
if b1.is_empty() {
|
||||||
}
|
return Ok(args[1].flatten_clone());
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
_ctx.engine()
|
_ctx.unwrap()
|
||||||
.raise_err_if_over_data_size_limit((b1.len() + b2.len(), 0, 0))?;
|
.engine()
|
||||||
|
.throw_on_size((b1.len() + b2.len(), 0, 0))?;
|
||||||
|
|
||||||
let mut blob = b1.clone();
|
let mut blob = b1.clone();
|
||||||
blob.extend(b2);
|
blob.extend(b2);
|
||||||
Ok(Dynamic::from_blob(blob))
|
Ok(Dynamic::from_blob(blob))
|
||||||
}),
|
},
|
||||||
|
cfg!(not(feature = "unchecked")),
|
||||||
|
)),
|
||||||
EqualsTo => Some(impl_op!(Blob == Blob)),
|
EqualsTo => Some(impl_op!(Blob == Blob)),
|
||||||
NotEqualsTo => Some(impl_op!(Blob != Blob)),
|
NotEqualsTo => Some(impl_op!(Blob != Blob)),
|
||||||
_ => None,
|
_ => None,
|
||||||
@ -284,9 +294,9 @@ pub fn get_builtin_binary_op_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Option<F
|
|||||||
|
|
||||||
if type1 == TypeId::of::<()>() {
|
if type1 == TypeId::of::<()>() {
|
||||||
return match op {
|
return match op {
|
||||||
EqualsTo => Some(const_true_fn),
|
EqualsTo => Some((const_true_fn, false)),
|
||||||
NotEqualsTo | GreaterThan | GreaterThanEqualsTo | LessThan | LessThanEqualsTo => {
|
NotEqualsTo | GreaterThan | GreaterThanEqualsTo | LessThan | LessThanEqualsTo => {
|
||||||
Some(const_false_fn)
|
Some((const_false_fn, false))
|
||||||
}
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
@ -388,20 +398,22 @@ pub fn get_builtin_binary_op_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Option<F
|
|||||||
}
|
}
|
||||||
|
|
||||||
return match op {
|
return match op {
|
||||||
Plus => Some(|_ctx, args| {
|
Plus => Some((
|
||||||
let x = args[0].as_char().expect(BUILTIN);
|
|_ctx, args| {
|
||||||
let y = &*args[1].read_lock::<ImmutableString>().expect(BUILTIN);
|
let x = args[0].as_char().expect(BUILTIN);
|
||||||
|
let y = &*args[1].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||||
|
|
||||||
let mut result = SmartString::new_const();
|
let mut result = SmartString::new_const();
|
||||||
result.push(x);
|
result.push(x);
|
||||||
result.push_str(y);
|
result.push_str(y);
|
||||||
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
_ctx.engine()
|
_ctx.unwrap().engine().throw_on_size((0, 0, result.len()))?;
|
||||||
.raise_err_if_over_data_size_limit((0, 0, result.len()))?;
|
|
||||||
|
|
||||||
Ok(result.into())
|
Ok(result.into())
|
||||||
}),
|
},
|
||||||
|
cfg!(not(feature = "unchecked")),
|
||||||
|
)),
|
||||||
EqualsTo => Some(impl_op!(get_s1s2(==))),
|
EqualsTo => Some(impl_op!(get_s1s2(==))),
|
||||||
NotEqualsTo => Some(impl_op!(get_s1s2(!=))),
|
NotEqualsTo => Some(impl_op!(get_s1s2(!=))),
|
||||||
GreaterThan => 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 {
|
return match op {
|
||||||
Plus => Some(|_ctx, args| {
|
Plus => Some((
|
||||||
let x = &*args[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
|_ctx, args| {
|
||||||
let y = args[1].as_char().expect(BUILTIN);
|
let x = &*args[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||||
let result = x + y;
|
let y = args[1].as_char().expect(BUILTIN);
|
||||||
|
let result = x + y;
|
||||||
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
_ctx.engine()
|
_ctx.unwrap().engine().throw_on_size((0, 0, result.len()))?;
|
||||||
.raise_err_if_over_data_size_limit((0, 0, result.len()))?;
|
|
||||||
|
|
||||||
Ok(result.into())
|
Ok(result.into())
|
||||||
}),
|
},
|
||||||
Minus => Some(|_, args| {
|
cfg!(not(feature = "unchecked")),
|
||||||
let x = &*args[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
)),
|
||||||
let y = args[1].as_char().expect(BUILTIN);
|
Minus => Some((
|
||||||
Ok((x - y).into())
|
|_, 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(==))),
|
EqualsTo => Some(impl_op!(get_s1s2(==))),
|
||||||
NotEqualsTo => Some(impl_op!(get_s1s2(!=))),
|
NotEqualsTo => Some(impl_op!(get_s1s2(!=))),
|
||||||
GreaterThan => 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
|
// () op string
|
||||||
if (type1, type2) == (TypeId::of::<()>(), TypeId::of::<ImmutableString>()) {
|
if (type1, type2) == (TypeId::of::<()>(), TypeId::of::<ImmutableString>()) {
|
||||||
return match op {
|
return match op {
|
||||||
Plus => Some(|_, args| Ok(args[1].clone())),
|
Plus => Some((|_, args| Ok(args[1].clone()), false)),
|
||||||
EqualsTo | GreaterThan | GreaterThanEqualsTo | LessThan | LessThanEqualsTo => {
|
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,
|
_ => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
// string op ()
|
// string op ()
|
||||||
if (type1, type2) == (TypeId::of::<ImmutableString>(), TypeId::of::<()>()) {
|
if (type1, type2) == (TypeId::of::<ImmutableString>(), TypeId::of::<()>()) {
|
||||||
return match op {
|
return match op {
|
||||||
Plus => Some(|_, args| Ok(args[0].clone())),
|
Plus => Some((|_, args| Ok(args[0].clone()), false)),
|
||||||
EqualsTo | GreaterThan | GreaterThanEqualsTo | LessThan | LessThanEqualsTo => {
|
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,
|
_ => 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>() {
|
if type2 == TypeId::of::<char>() {
|
||||||
return match op {
|
return match op {
|
||||||
Plus => Some(|_ctx, args| {
|
Plus => Some((
|
||||||
let mut blob = args[0].read_lock::<Blob>().expect(BUILTIN).clone();
|
|_ctx, args| {
|
||||||
let mut buf = [0_u8; 4];
|
let mut blob = args[0].read_lock::<Blob>().expect(BUILTIN).clone();
|
||||||
let x = args[1].as_char().expect(BUILTIN).encode_utf8(&mut buf);
|
let mut buf = [0_u8; 4];
|
||||||
|
let x = args[1].as_char().expect(BUILTIN).encode_utf8(&mut buf);
|
||||||
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
_ctx.engine().raise_err_if_over_data_size_limit((
|
_ctx.unwrap()
|
||||||
blob.len() + x.len(),
|
.engine()
|
||||||
0,
|
.throw_on_size((blob.len() + x.len(), 0, 0))?;
|
||||||
0,
|
|
||||||
))?;
|
|
||||||
|
|
||||||
blob.extend(x.as_bytes());
|
blob.extend(x.as_bytes());
|
||||||
Ok(Dynamic::from_blob(blob))
|
Ok(Dynamic::from_blob(blob))
|
||||||
}),
|
},
|
||||||
|
cfg!(not(feature = "unchecked")),
|
||||||
|
)),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -511,8 +529,8 @@ pub fn get_builtin_binary_op_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Option<F
|
|||||||
)
|
)
|
||||||
{
|
{
|
||||||
return match op {
|
return match op {
|
||||||
NotEqualsTo => Some(const_true_fn),
|
NotEqualsTo => Some((const_true_fn, false)),
|
||||||
Equals => Some(const_false_fn),
|
Equals => Some((const_false_fn, false)),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -542,9 +560,9 @@ pub fn get_builtin_binary_op_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Option<F
|
|||||||
} else if type1 != type2 {
|
} else if type1 != type2 {
|
||||||
// If the types are not the same, default to not compare
|
// If the types are not the same, default to not compare
|
||||||
match op {
|
match op {
|
||||||
NotEqualsTo => Some(const_true_fn),
|
NotEqualsTo => Some((const_true_fn, false)),
|
||||||
EqualsTo | GreaterThan | GreaterThanEqualsTo | LessThan | LessThanEqualsTo => {
|
EqualsTo | GreaterThan | GreaterThanEqualsTo | LessThan | LessThanEqualsTo => {
|
||||||
Some(const_false_fn)
|
Some((const_false_fn, false))
|
||||||
}
|
}
|
||||||
_ => None,
|
_ => 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
|
// Default comparison operators for different types
|
||||||
if type2 != type1 {
|
if type2 != type1 {
|
||||||
return match op {
|
return match op {
|
||||||
NotEqualsTo => Some(const_true_fn),
|
NotEqualsTo => Some((const_true_fn, false)),
|
||||||
EqualsTo | GreaterThan | GreaterThanEqualsTo | LessThan | LessThanEqualsTo => {
|
EqualsTo | GreaterThan | GreaterThanEqualsTo | LessThan | LessThanEqualsTo => {
|
||||||
Some(const_false_fn)
|
Some((const_false_fn, false))
|
||||||
}
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
@ -578,43 +596,43 @@ pub fn get_builtin_op_assignment_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Opti
|
|||||||
let type2 = y.type_id();
|
let type2 = y.type_id();
|
||||||
|
|
||||||
macro_rules! impl_op {
|
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 x = args[0].$yy().expect(BUILTIN);
|
||||||
let y = args[1].$yy().expect(BUILTIN) as $x;
|
let y = args[1].$yy().expect(BUILTIN) as $x;
|
||||||
Ok((*args[0].write_lock::<$x>().expect(BUILTIN) = x $op y).into())
|
Ok((*args[0].write_lock::<$x>().expect(BUILTIN) = x $op y).into())
|
||||||
} };
|
}, false) };
|
||||||
($x:ident $op:tt $yy:ident) => { |_, args| {
|
($x:ident $op:tt $yy:ident) => { (|_, args| {
|
||||||
let y = args[1].$yy().expect(BUILTIN) as $x;
|
let y = args[1].$yy().expect(BUILTIN) as $x;
|
||||||
Ok((*args[0].write_lock::<$x>().expect(BUILTIN) $op y).into())
|
Ok((*args[0].write_lock::<$x>().expect(BUILTIN) $op y).into())
|
||||||
} };
|
}, false) };
|
||||||
($x:ident $op:tt $yy:ident as $yyy:ty) => { |_, args| {
|
($x:ident $op:tt $yy:ident as $yyy:ty) => { (|_, args| {
|
||||||
let y = args[1].$yy().expect(BUILTIN) as $yyy;
|
let y = args[1].$yy().expect(BUILTIN) as $yyy;
|
||||||
Ok((*args[0].write_lock::<$x>().expect(BUILTIN) $op y).into())
|
Ok((*args[0].write_lock::<$x>().expect(BUILTIN) $op y).into())
|
||||||
} };
|
}, false) };
|
||||||
($x:ty => $xx:ident . $func:ident ( $yy:ident as $yyy:ty )) => { |_, args| {
|
($x:ty => $xx:ident . $func:ident ( $yy:ident as $yyy:ty )) => { (|_, args| {
|
||||||
let x = args[0].$xx().expect(BUILTIN);
|
let x = args[0].$xx().expect(BUILTIN);
|
||||||
let y = args[1].$yy().expect(BUILTIN) as $x;
|
let y = args[1].$yy().expect(BUILTIN) as $x;
|
||||||
Ok((*args[0].write_lock::<$x>().expect(BUILTIN) = x.$func(y as $yyy)).into())
|
Ok((*args[0].write_lock::<$x>().expect(BUILTIN) = x.$func(y as $yyy)).into())
|
||||||
} };
|
}, false) };
|
||||||
($x:ty => $func:ident ( $xx:ident, $yy:ident )) => { |_, args| {
|
($x:ty => $func:ident ( $xx:ident, $yy:ident )) => { (|_, args| {
|
||||||
let x = args[0].$xx().expect(BUILTIN);
|
let x = args[0].$xx().expect(BUILTIN);
|
||||||
let y = args[1].$yy().expect(BUILTIN) as $x;
|
let y = args[1].$yy().expect(BUILTIN) as $x;
|
||||||
Ok((*args[0].write_lock().expect(BUILTIN) = $func(x, y)?).into())
|
Ok((*args[0].write_lock().expect(BUILTIN) = $func(x, y)?).into())
|
||||||
} };
|
}, false) };
|
||||||
(from $x:ident $op:tt $yy:ident) => { |_, args| {
|
(from $x:ident $op:tt $yy:ident) => { (|_, args| {
|
||||||
let y = <$x>::from(args[1].$yy().expect(BUILTIN));
|
let y = <$x>::from(args[1].$yy().expect(BUILTIN));
|
||||||
Ok((*args[0].write_lock::<$x>().expect(BUILTIN) $op y).into())
|
Ok((*args[0].write_lock::<$x>().expect(BUILTIN) $op y).into())
|
||||||
} };
|
}, false) };
|
||||||
(from $x:ty => $xx:ident . $func:ident ( $yy:ident )) => { |_, args| {
|
(from $x:ty => $xx:ident . $func:ident ( $yy:ident )) => { (|_, args| {
|
||||||
let x = args[0].$xx().expect(BUILTIN);
|
let x = args[0].$xx().expect(BUILTIN);
|
||||||
let y = <$x>::from(args[1].$yy().expect(BUILTIN));
|
let y = <$x>::from(args[1].$yy().expect(BUILTIN));
|
||||||
Ok((*args[0].write_lock::<$x>().expect(BUILTIN) = x.$func(y)).into())
|
Ok((*args[0].write_lock::<$x>().expect(BUILTIN) = x.$func(y)).into())
|
||||||
} };
|
}, false) };
|
||||||
(from $x:ty => $func:ident ( $xx:ident, $yy:ident )) => { |_, args| {
|
(from $x:ty => $func:ident ( $xx:ident, $yy:ident )) => { (|_, args| {
|
||||||
let x = args[0].$xx().expect(BUILTIN);
|
let x = args[0].$xx().expect(BUILTIN);
|
||||||
let y = <$x>::from(args[1].$yy().expect(BUILTIN));
|
let y = <$x>::from(args[1].$yy().expect(BUILTIN));
|
||||||
Ok((*args[0].write_lock().expect(BUILTIN) = $func(x, y)?).into())
|
Ok((*args[0].write_lock().expect(BUILTIN) = $func(x, y)?).into())
|
||||||
} };
|
}, false) };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for common patterns
|
// 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>() {
|
if type1 == TypeId::of::<char>() {
|
||||||
return match op {
|
return match op {
|
||||||
PlusAssign => Some(|_, args| {
|
PlusAssign => Some((
|
||||||
let y = args[1].as_char().expect(BUILTIN);
|
|_, args| {
|
||||||
let x = &mut *args[0].write_lock::<Dynamic>().expect(BUILTIN);
|
let y = args[1].as_char().expect(BUILTIN);
|
||||||
|
let x = &mut *args[0].write_lock::<Dynamic>().expect(BUILTIN);
|
||||||
|
|
||||||
let mut buf = SmartString::new_const();
|
let mut buf = SmartString::new_const();
|
||||||
write!(&mut buf, "{y}").unwrap();
|
write!(&mut buf, "{y}").unwrap();
|
||||||
buf.push(y);
|
buf.push(y);
|
||||||
|
|
||||||
Ok((*x = buf.into()).into())
|
Ok((*x = buf.into()).into())
|
||||||
}),
|
},
|
||||||
|
false,
|
||||||
|
)),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if type1 == TypeId::of::<ImmutableString>() {
|
if type1 == TypeId::of::<ImmutableString>() {
|
||||||
return match op {
|
return match op {
|
||||||
PlusAssign => Some(|_ctx, args| {
|
PlusAssign => Some((
|
||||||
let (first, second) = args.split_first_mut().expect(BUILTIN);
|
|_ctx, args| {
|
||||||
let x = &mut *first.write_lock::<ImmutableString>().expect(BUILTIN);
|
let (first, second) = args.split_first_mut().expect(BUILTIN);
|
||||||
let y = &*second[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
let x = &mut *first.write_lock::<ImmutableString>().expect(BUILTIN);
|
||||||
|
let y = &*second[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||||
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
if !x.is_empty() && !y.is_empty() {
|
if !x.is_empty() && !y.is_empty() {
|
||||||
let total_len = x.len() + y.len();
|
let total_len = x.len() + y.len();
|
||||||
_ctx.engine()
|
_ctx.unwrap().engine().throw_on_size((0, 0, total_len))?;
|
||||||
.raise_err_if_over_data_size_limit((0, 0, total_len))?;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Ok((*x += y).into())
|
Ok((*x += y).into())
|
||||||
}),
|
},
|
||||||
MinusAssign => Some(|_, args| {
|
cfg!(not(feature = "unchecked")),
|
||||||
let (first, second) = args.split_first_mut().expect(BUILTIN);
|
)),
|
||||||
let x = &mut *first.write_lock::<ImmutableString>().expect(BUILTIN);
|
MinusAssign => Some((
|
||||||
let y = &*second[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
|_, args| {
|
||||||
Ok((*x -= y).into())
|
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,
|
_ => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -715,27 +741,31 @@ pub fn get_builtin_op_assignment_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Opti
|
|||||||
use crate::Array;
|
use crate::Array;
|
||||||
|
|
||||||
return match op {
|
return match op {
|
||||||
PlusAssign => Some(|_ctx, args| {
|
PlusAssign => Some((
|
||||||
let x = std::mem::take(args[1]).into_array().expect(BUILTIN);
|
|_ctx, args| {
|
||||||
|
let x = std::mem::take(args[1]).into_array().expect(BUILTIN);
|
||||||
|
|
||||||
if x.is_empty() {
|
if x.is_empty() {
|
||||||
return Ok(Dynamic::UNIT);
|
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"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
if !_array_is_empty {
|
if !_array_is_empty {
|
||||||
_ctx.engine().check_data_size(
|
_ctx.unwrap().engine().check_data_size(
|
||||||
&*args[0].read_lock().expect(BUILTIN),
|
&*args[0].read_lock().expect(BUILTIN),
|
||||||
crate::Position::NONE,
|
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,
|
_ => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -747,19 +777,20 @@ pub fn get_builtin_op_assignment_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Opti
|
|||||||
use crate::Blob;
|
use crate::Blob;
|
||||||
|
|
||||||
return match op {
|
return match op {
|
||||||
PlusAssign => Some(|_ctx, args| {
|
PlusAssign => Some((
|
||||||
let blob2 = std::mem::take(args[1]).into_blob().expect(BUILTIN);
|
|_ctx, args| {
|
||||||
let blob1 = &mut *args[0].write_lock::<Blob>().expect(BUILTIN);
|
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"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
_ctx.engine().raise_err_if_over_data_size_limit((
|
_ctx.unwrap()
|
||||||
blob1.len() + blob2.len(),
|
.engine()
|
||||||
0,
|
.throw_on_size((blob1.len() + blob2.len(), 0, 0))?;
|
||||||
0,
|
|
||||||
))?;
|
|
||||||
|
|
||||||
Ok(append(blob1, blob2).into())
|
Ok(append(blob1, blob2).into())
|
||||||
}),
|
},
|
||||||
|
cfg!(not(feature = "unchecked")),
|
||||||
|
)),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -833,17 +864,21 @@ pub fn get_builtin_op_assignment_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Opti
|
|||||||
// string op= char
|
// string op= char
|
||||||
if (type1, type2) == (TypeId::of::<ImmutableString>(), TypeId::of::<char>()) {
|
if (type1, type2) == (TypeId::of::<ImmutableString>(), TypeId::of::<char>()) {
|
||||||
return match op {
|
return match op {
|
||||||
PlusAssign => Some(|_ctx, args| {
|
PlusAssign => Some((
|
||||||
let mut buf = [0_u8; 4];
|
|_ctx, args| {
|
||||||
let ch = &*args[1].as_char().expect(BUILTIN).encode_utf8(&mut buf);
|
let mut buf = [0_u8; 4];
|
||||||
let mut x = args[0].write_lock::<ImmutableString>().expect(BUILTIN);
|
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"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
_ctx.engine()
|
_ctx.unwrap()
|
||||||
.raise_err_if_over_data_size_limit((0, 0, x.len() + ch.len()))?;
|
.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)),
|
MinusAssign => Some(impl_op!(ImmutableString -= as_char as char)),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
@ -851,28 +886,32 @@ pub fn get_builtin_op_assignment_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Opti
|
|||||||
// char op= string
|
// char op= string
|
||||||
if (type1, type2) == (TypeId::of::<char>(), TypeId::of::<ImmutableString>()) {
|
if (type1, type2) == (TypeId::of::<char>(), TypeId::of::<ImmutableString>()) {
|
||||||
return match op {
|
return match op {
|
||||||
PlusAssign => Some(|_ctx, args| {
|
PlusAssign => Some((
|
||||||
let ch = {
|
|_ctx, args| {
|
||||||
let s = &*args[1].read_lock::<ImmutableString>().expect(BUILTIN);
|
let ch = {
|
||||||
|
let s = &*args[1].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||||
|
|
||||||
if s.is_empty() {
|
if s.is_empty() {
|
||||||
return Ok(Dynamic::UNIT);
|
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"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
_ctx.engine()
|
_ctx.unwrap()
|
||||||
.raise_err_if_over_data_size_limit((0, 0, ch.len() + s.len()))?;
|
.engine()
|
||||||
|
.throw_on_size((0, 0, ch.len() + s.len()))?;
|
||||||
|
|
||||||
ch.push_str(s);
|
ch.push_str(s);
|
||||||
ch
|
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,
|
_ => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -885,21 +924,24 @@ pub fn get_builtin_op_assignment_fn(op: Token, x: &Dynamic, y: &Dynamic) -> Opti
|
|||||||
use crate::Array;
|
use crate::Array;
|
||||||
|
|
||||||
return match op {
|
return match op {
|
||||||
PlusAssign => Some(|_ctx, args| {
|
PlusAssign => Some((
|
||||||
{
|
|_ctx, args| {
|
||||||
let x = std::mem::take(args[1]);
|
{
|
||||||
let array = &mut *args[0].write_lock::<Array>().expect(BUILTIN);
|
let x = std::mem::take(args[1]);
|
||||||
push(array, x);
|
let array = &mut *args[0].write_lock::<Array>().expect(BUILTIN);
|
||||||
}
|
push(array, x);
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
_ctx.engine().check_data_size(
|
_ctx.unwrap().engine().check_data_size(
|
||||||
&*args[0].read_lock().expect(BUILTIN),
|
&*args[0].read_lock().expect(BUILTIN),
|
||||||
crate::Position::NONE,
|
crate::Position::NONE,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(Dynamic::UNIT)
|
Ok(Dynamic::UNIT)
|
||||||
}),
|
},
|
||||||
|
cfg!(not(feature = "unchecked")),
|
||||||
|
)),
|
||||||
_ => None,
|
_ => 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::*;
|
use crate::packages::blob_basic::blob_functions::*;
|
||||||
|
|
||||||
return match op {
|
return match op {
|
||||||
PlusAssign => Some(|_ctx, args| {
|
PlusAssign => Some((
|
||||||
let x = args[1].as_int().expect(BUILTIN);
|
|_ctx, args| {
|
||||||
let blob = &mut *args[0].write_lock::<Blob>().expect(BUILTIN);
|
let x = args[1].as_int().expect(BUILTIN);
|
||||||
|
let blob = &mut *args[0].write_lock::<Blob>().expect(BUILTIN);
|
||||||
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
_ctx.engine()
|
_ctx.unwrap()
|
||||||
.raise_err_if_over_data_size_limit((blob.len() + 1, 0, 0))?;
|
.engine()
|
||||||
|
.throw_on_size((blob.len() + 1, 0, 0))?;
|
||||||
|
|
||||||
Ok(push(blob, x).into())
|
Ok(push(blob, x).into())
|
||||||
}),
|
},
|
||||||
|
cfg!(not(feature = "unchecked")),
|
||||||
|
)),
|
||||||
_ => None,
|
_ => 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::*;
|
use crate::packages::blob_basic::blob_functions::*;
|
||||||
|
|
||||||
return match op {
|
return match op {
|
||||||
PlusAssign => Some(|_ctx, args| {
|
PlusAssign => Some((
|
||||||
let x = args[1].as_char().expect(BUILTIN);
|
|_ctx, args| {
|
||||||
let blob = &mut *args[0].write_lock::<Blob>().expect(BUILTIN);
|
let x = args[1].as_char().expect(BUILTIN);
|
||||||
|
let blob = &mut *args[0].write_lock::<Blob>().expect(BUILTIN);
|
||||||
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
_ctx.engine()
|
_ctx.unwrap()
|
||||||
.raise_err_if_over_data_size_limit((blob.len() + 1, 0, 0))?;
|
.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,
|
_ => 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::*;
|
use crate::packages::blob_basic::blob_functions::*;
|
||||||
|
|
||||||
return match op {
|
return match op {
|
||||||
PlusAssign => Some(|_ctx, args| {
|
PlusAssign => Some((
|
||||||
let (first, second) = args.split_first_mut().expect(BUILTIN);
|
|_ctx, args| {
|
||||||
let blob = &mut *first.write_lock::<Blob>().expect(BUILTIN);
|
let (first, second) = args.split_first_mut().expect(BUILTIN);
|
||||||
let s = &*second[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
let blob = &mut *first.write_lock::<Blob>().expect(BUILTIN);
|
||||||
|
let s = &*second[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||||
|
|
||||||
if s.is_empty() {
|
if s.is_empty() {
|
||||||
return Ok(Dynamic::UNIT);
|
return Ok(Dynamic::UNIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
_ctx.engine().raise_err_if_over_data_size_limit((
|
_ctx.unwrap()
|
||||||
blob.len() + s.len(),
|
.engine()
|
||||||
0,
|
.throw_on_size((blob.len() + s.len(), 0, 0))?;
|
||||||
0,
|
|
||||||
))?;
|
|
||||||
|
|
||||||
Ok(append_str(blob, s).into())
|
Ok(append_str(blob, s).into())
|
||||||
}),
|
},
|
||||||
|
cfg!(not(feature = "unchecked")),
|
||||||
|
)),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -271,25 +271,25 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Try to find a built-in version
|
// Try to find a built-in version
|
||||||
let builtin = args.and_then(|args| match op_token {
|
let builtin =
|
||||||
Token::NONE => None,
|
args.and_then(|args| match op_token {
|
||||||
token if token.is_op_assignment() => {
|
Token::NONE => None,
|
||||||
let (first_arg, rest_args) = args.split_first().unwrap();
|
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(
|
get_builtin_op_assignment_fn(token, first_arg, rest_args[0])
|
||||||
|f| FnResolutionCacheEntry {
|
.map(|(f, ctx)| FnResolutionCacheEntry {
|
||||||
func: CallableFunction::Method(Shared::new(f)),
|
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,
|
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) {
|
return if cache.filter.is_absent_and_set(hash) {
|
||||||
// Do not cache "one-hit wonders"
|
// Do not cache "one-hit wonders"
|
||||||
@ -400,22 +400,26 @@ impl Engine {
|
|||||||
// Run external function
|
// Run external function
|
||||||
let is_method = func.is_method();
|
let is_method = func.is_method();
|
||||||
let src = source.as_ref().map(|s| s.as_str());
|
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 mut _result = if let Some(f) = func.get_plugin_fn() {
|
||||||
let f = func.get_plugin_fn().unwrap();
|
|
||||||
if !f.is_pure() && !args.is_empty() && args[0].is_read_only() {
|
if !f.is_pure() && !args.is_empty() && args[0].is_read_only() {
|
||||||
Err(ERR::ErrorNonPureMethodCallOnConstant(name.to_string(), pos).into())
|
Err(ERR::ErrorNonPureMethodCallOnConstant(name.to_string(), pos).into())
|
||||||
} else {
|
} else {
|
||||||
|
let context = (self, name, src, &*global, pos).into();
|
||||||
f.call(context, args)
|
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 {
|
} else {
|
||||||
func.get_native_fn().unwrap()(context, args)
|
unreachable!();
|
||||||
.and_then(|r| self.check_data_size(r, pos))
|
}
|
||||||
.map_err(|err| err.fill_position(pos))
|
.and_then(|r| self.check_data_size(r, pos))
|
||||||
};
|
.map_err(|err| err.fill_position(pos));
|
||||||
|
|
||||||
#[cfg(feature = "debugging")]
|
#[cfg(feature = "debugging")]
|
||||||
if self.is_debugger_registered() {
|
if self.is_debugger_registered() {
|
||||||
@ -1398,7 +1402,11 @@ impl Engine {
|
|||||||
|
|
||||||
Some(f) if f.is_native() => {
|
Some(f) if f.is_native() => {
|
||||||
let func = f.get_native_fn().expect("native function");
|
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))
|
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];
|
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
|
// Built-in found
|
||||||
let orig_level = global.level;
|
let orig_level = global.level;
|
||||||
global.level += 1;
|
global.level += 1;
|
||||||
let global = &*RestoreOnDrop::lock(global, move |g| g.level = orig_level);
|
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);
|
return func(context, operands);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,10 +14,10 @@ use std::prelude::v1::*;
|
|||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub enum CallableFunction {
|
pub enum CallableFunction {
|
||||||
/// A pure native Rust function with all arguments passed by value.
|
/// 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,
|
/// A native Rust object method with the first argument passed by reference,
|
||||||
/// and the rest passed by value.
|
/// and the rest passed by value.
|
||||||
Method(Shared<FnAny>),
|
Method(Shared<FnAny>, bool),
|
||||||
/// An iterator function.
|
/// An iterator function.
|
||||||
Iterator(Shared<IteratorFn>),
|
Iterator(Shared<IteratorFn>),
|
||||||
/// A plugin function,
|
/// A plugin function,
|
||||||
@ -136,6 +136,17 @@ impl CallableFunction {
|
|||||||
Self::Script(..) => false,
|
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.
|
/// Get the access mode.
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
@ -156,7 +167,7 @@ impl CallableFunction {
|
|||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn get_native_fn(&self) -> Option<&Shared<FnAny>> {
|
pub fn get_native_fn(&self) -> Option<&Shared<FnAny>> {
|
||||||
match self {
|
match self {
|
||||||
Self::Pure(f) | Self::Method(f) => Some(f),
|
Self::Pure(f, ..) | Self::Method(f, ..) => Some(f),
|
||||||
Self::Iterator(..) | Self::Plugin(..) => None,
|
Self::Iterator(..) | Self::Plugin(..) => None,
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
@ -546,13 +546,16 @@ pub fn locked_write<T>(value: &Locked<T>) -> LockGuardMut<T> {
|
|||||||
|
|
||||||
/// General Rust function trail object.
|
/// General Rust function trail object.
|
||||||
#[cfg(not(feature = "sync"))]
|
#[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.
|
/// General Rust function trail object.
|
||||||
#[cfg(feature = "sync")]
|
#[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.
|
/// 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.
|
/// Function that gets an iterator from a type.
|
||||||
#[cfg(not(feature = "sync"))]
|
#[cfg(not(feature = "sync"))]
|
||||||
|
@ -9,7 +9,7 @@ use super::call::FnCallArgs;
|
|||||||
use super::callable_function::CallableFunction;
|
use super::callable_function::CallableFunction;
|
||||||
use super::native::{SendSync, Shared};
|
use super::native::{SendSync, Shared};
|
||||||
use crate::types::dynamic::{DynamicWriteLock, Variant};
|
use crate::types::dynamic::{DynamicWriteLock, Variant};
|
||||||
use crate::{reify, Dynamic, NativeCallContext, RhaiResultOf};
|
use crate::{reify, Dynamic, Identifier, NativeCallContext, RhaiResultOf};
|
||||||
#[cfg(feature = "no_std")]
|
#[cfg(feature = "no_std")]
|
||||||
use std::prelude::v1::*;
|
use std::prelude::v1::*;
|
||||||
use std::{
|
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> {
|
pub trait RegisterNativeFunction<ARGS, const NUM: usize, const CTX: bool, RET, const FALL: bool> {
|
||||||
/// Convert this function into a [`CallableFunction`].
|
/// Convert this function into a [`CallableFunction`].
|
||||||
#[must_use]
|
#[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.
|
/// Get the type ID's of this function's parameters.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
fn param_types() -> [TypeId; NUM];
|
fn param_types() -> [TypeId; NUM];
|
||||||
/// Get the number of parameters for this function.
|
/// Get the number of parameters for this function.
|
||||||
|
#[inline(always)]
|
||||||
#[must_use]
|
#[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.
|
/// _(metadata)_ Get the type names of this function's parameters.
|
||||||
/// Exported under the `metadata` feature only.
|
/// Exported under the `metadata` feature only.
|
||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
@ -106,27 +112,14 @@ pub trait RegisterNativeFunction<ARGS, const NUM: usize, const CTX: bool, RET, c
|
|||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! check_constant {
|
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")))]
|
#[cfg(any(not(feature = "no_object"), not(feature = "no_index")))]
|
||||||
if stringify!($abi) == "Method" {
|
if stringify!($abi) == "Method" && $no_const && $args[0].is_read_only() {
|
||||||
let mut deny = false;
|
return Err(crate::ERR::ErrorNonPureMethodCallOnConstant(
|
||||||
|
$fn_name.to_string(),
|
||||||
#[cfg(not(feature = "no_index"))]
|
crate::Position::NONE,
|
||||||
if $n == 3 && !deny {
|
)
|
||||||
deny = $ctx.fn_name() == crate::engine::FN_IDX_SET && $args[0].is_read_only();
|
.into());
|
||||||
}
|
|
||||||
#[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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -147,16 +140,16 @@ macro_rules! def_register {
|
|||||||
impl<
|
impl<
|
||||||
FN: Fn($($param),*) -> RET + SendSync + 'static,
|
FN: Fn($($param),*) -> RET + SendSync + 'static,
|
||||||
$($par: Variant + Clone,)*
|
$($par: Variant + Clone,)*
|
||||||
RET: Variant + Clone
|
RET: Variant + Clone,
|
||||||
> RegisterNativeFunction<($($mark,)*), $n, false, RET, false> for FN {
|
> RegisterNativeFunction<($($mark,)*), $n, false, RET, false> for FN {
|
||||||
#[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] }
|
#[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 param_names() -> [&'static str;$n] { [$(type_name::<$param>()),*] }
|
||||||
#[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RET>() }
|
#[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RET>() }
|
||||||
#[inline(always)] fn into_callable_function(self) -> CallableFunction {
|
#[inline(always)] fn into_callable_function(self, fn_name: Identifier, no_const: bool) -> CallableFunction {
|
||||||
CallableFunction::$abi(Shared::new(move |ctx: NativeCallContext, args: &mut FnCallArgs| {
|
CallableFunction::$abi(Shared::new(move |_, args: &mut FnCallArgs| {
|
||||||
// The arguments are assumed to be of the correct number and types!
|
// 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 drain = args.iter_mut();
|
||||||
$(let mut $par = $clone(drain.next().unwrap()); )*
|
$(let mut $par = $clone(drain.next().unwrap()); )*
|
||||||
@ -166,23 +159,25 @@ macro_rules! def_register {
|
|||||||
|
|
||||||
// Map the result
|
// Map the result
|
||||||
Ok(Dynamic::from(r))
|
Ok(Dynamic::from(r))
|
||||||
}))
|
}), false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<
|
impl<
|
||||||
FN: for<'a> Fn(NativeCallContext<'a>, $($param),*) -> RET + SendSync + 'static,
|
FN: for<'a> Fn(NativeCallContext<'a>, $($param),*) -> RET + SendSync + 'static,
|
||||||
$($par: Variant + Clone,)*
|
$($par: Variant + Clone,)*
|
||||||
RET: Variant + Clone
|
RET: Variant + Clone,
|
||||||
> RegisterNativeFunction<($($mark,)*), $n, true, RET, false> for FN {
|
> RegisterNativeFunction<($($mark,)*), $n, true, RET, false> for FN {
|
||||||
#[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] }
|
#[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 param_names() -> [&'static str;$n] { [$(type_name::<$param>()),*] }
|
||||||
#[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RET>() }
|
#[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RET>() }
|
||||||
#[inline(always)] fn into_callable_function(self) -> CallableFunction {
|
#[inline(always)] fn into_callable_function(self, fn_name: Identifier, no_const: bool) -> CallableFunction {
|
||||||
CallableFunction::$abi(Shared::new(move |ctx: NativeCallContext, args: &mut FnCallArgs| {
|
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!
|
// 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 drain = args.iter_mut();
|
||||||
$(let mut $par = $clone(drain.next().unwrap()); )*
|
$(let mut $par = $clone(drain.next().unwrap()); )*
|
||||||
@ -192,7 +187,7 @@ macro_rules! def_register {
|
|||||||
|
|
||||||
// Map the result
|
// Map the result
|
||||||
Ok(Dynamic::from(r))
|
Ok(Dynamic::from(r))
|
||||||
}))
|
}), true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -202,21 +197,21 @@ macro_rules! def_register {
|
|||||||
RET: Variant + Clone
|
RET: Variant + Clone
|
||||||
> RegisterNativeFunction<($($mark,)*), $n, false, RET, true> for FN {
|
> RegisterNativeFunction<($($mark,)*), $n, false, RET, true> for FN {
|
||||||
#[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] }
|
#[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 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() -> TypeId { TypeId::of::<RhaiResultOf<RET>>() }
|
||||||
#[cfg(feature = "metadata")] #[inline(always)] fn return_type_name() -> &'static str { type_name::<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 {
|
#[inline(always)] fn into_callable_function(self, fn_name: Identifier, no_const: bool) -> CallableFunction {
|
||||||
CallableFunction::$abi(Shared::new(move |ctx: NativeCallContext, args: &mut FnCallArgs| {
|
CallableFunction::$abi(Shared::new(move |_, args: &mut FnCallArgs| {
|
||||||
// The arguments are assumed to be of the correct number and types!
|
// 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 drain = args.iter_mut();
|
||||||
$(let mut $par = $clone(drain.next().unwrap()); )*
|
$(let mut $par = $clone(drain.next().unwrap()); )*
|
||||||
|
|
||||||
// Call the function with each argument value
|
// Call the function with each argument value
|
||||||
self($($arg),*).map(Dynamic::from)
|
self($($arg),*).map(Dynamic::from)
|
||||||
}))
|
}), false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -226,21 +221,23 @@ macro_rules! def_register {
|
|||||||
RET: Variant + Clone
|
RET: Variant + Clone
|
||||||
> RegisterNativeFunction<($($mark,)*), $n, true, RET, true> for FN {
|
> RegisterNativeFunction<($($mark,)*), $n, true, RET, true> for FN {
|
||||||
#[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] }
|
#[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 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() -> TypeId { TypeId::of::<RhaiResultOf<RET>>() }
|
||||||
#[cfg(feature = "metadata")] #[inline(always)] fn return_type_name() -> &'static str { type_name::<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 {
|
#[inline(always)] fn into_callable_function(self, fn_name: Identifier, no_const: bool) -> CallableFunction {
|
||||||
CallableFunction::$abi(Shared::new(move |ctx: NativeCallContext, args: &mut FnCallArgs| {
|
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!
|
// 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 drain = args.iter_mut();
|
||||||
$(let mut $par = $clone(drain.next().unwrap()); )*
|
$(let mut $par = $clone(drain.next().unwrap()); )*
|
||||||
|
|
||||||
// Call the function with each argument value
|
// Call the function with each argument value
|
||||||
self(ctx, $($arg),*).map(Dynamic::from)
|
self(ctx, $($arg),*).map(Dynamic::from)
|
||||||
}))
|
}), true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,7 +72,7 @@ pub struct FuncInfoMetadata {
|
|||||||
/// Function access mode.
|
/// Function access mode.
|
||||||
pub access: FnAccess,
|
pub access: FnAccess,
|
||||||
/// Function name.
|
/// Function name.
|
||||||
pub name: ImmutableString,
|
pub name: Identifier,
|
||||||
/// Number of parameters.
|
/// Number of parameters.
|
||||||
pub num_params: usize,
|
pub num_params: usize,
|
||||||
/// Parameter types (if applicable).
|
/// Parameter types (if applicable).
|
||||||
@ -85,7 +85,7 @@ pub struct FuncInfoMetadata {
|
|||||||
pub return_type: Identifier,
|
pub return_type: Identifier,
|
||||||
/// Comments.
|
/// Comments.
|
||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
pub comments: Box<[Box<str>]>,
|
pub comments: Box<[Identifier]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A type containing a single registered function.
|
/// A type containing a single registered function.
|
||||||
@ -879,13 +879,12 @@ impl Module {
|
|||||||
/// In other words, the number of entries should be one larger than the number of parameters.
|
/// In other words, the number of entries should be one larger than the number of parameters.
|
||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn update_fn_metadata<S: AsRef<str>>(
|
pub fn update_fn_metadata<S: Into<Identifier>>(
|
||||||
&mut self,
|
&mut self,
|
||||||
hash_fn: u64,
|
hash_fn: u64,
|
||||||
arg_names: impl IntoIterator<Item = S>,
|
arg_names: impl IntoIterator<Item = S>,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
let mut param_names: FnArgsVec<_> =
|
let mut param_names: FnArgsVec<_> = arg_names.into_iter().map(Into::into).collect();
|
||||||
arg_names.into_iter().map(|s| s.as_ref().into()).collect();
|
|
||||||
|
|
||||||
if let Some(f) = self.functions.as_mut().and_then(|m| m.get_mut(&hash_fn)) {
|
if let Some(f) = self.functions.as_mut().and_then(|m| m.get_mut(&hash_fn)) {
|
||||||
let (param_names, return_type_name) = if param_names.len() > f.metadata.num_params {
|
let (param_names, return_type_name) = if param_names.len() > f.metadata.num_params {
|
||||||
@ -927,7 +926,7 @@ impl Module {
|
|||||||
/// Each line in non-block doc-comments should start with `///`.
|
/// Each line in non-block doc-comments should start with `///`.
|
||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn update_fn_metadata_with_comments<A: AsRef<str>, C: AsRef<str>>(
|
pub fn update_fn_metadata_with_comments<A: Into<Identifier>, C: Into<Identifier>>(
|
||||||
&mut self,
|
&mut self,
|
||||||
hash_fn: u64,
|
hash_fn: u64,
|
||||||
arg_names: impl IntoIterator<Item = A>,
|
arg_names: impl IntoIterator<Item = A>,
|
||||||
@ -940,7 +939,7 @@ impl Module {
|
|||||||
.and_then(|m| m.get_mut(&hash_fn))
|
.and_then(|m| m.get_mut(&hash_fn))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.metadata
|
.metadata
|
||||||
.comments = comments.into_iter().map(|s| s.as_ref().into()).collect();
|
.comments = comments.into_iter().map(Into::into).collect();
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@ -1201,8 +1200,9 @@ impl Module {
|
|||||||
arg_types: impl AsRef<[TypeId]>,
|
arg_types: impl AsRef<[TypeId]>,
|
||||||
func: impl Fn(NativeCallContext, &mut FnCallArgs) -> RhaiResultOf<T> + SendSync + 'static,
|
func: impl Fn(NativeCallContext, &mut FnCallArgs) -> RhaiResultOf<T> + SendSync + 'static,
|
||||||
) -> u64 {
|
) -> u64 {
|
||||||
let f =
|
let f = move |ctx: Option<NativeCallContext>, args: &mut FnCallArgs| {
|
||||||
move |ctx: NativeCallContext, args: &mut FnCallArgs| func(ctx, args).map(Dynamic::from);
|
func(ctx.unwrap(), args).map(Dynamic::from)
|
||||||
|
};
|
||||||
|
|
||||||
self.set_fn(
|
self.set_fn(
|
||||||
name,
|
name,
|
||||||
@ -1210,7 +1210,7 @@ impl Module {
|
|||||||
access,
|
access,
|
||||||
None,
|
None,
|
||||||
arg_types,
|
arg_types,
|
||||||
CallableFunction::Method(Shared::new(f)),
|
CallableFunction::Method(Shared::new(f), true),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1246,13 +1246,24 @@ impl Module {
|
|||||||
T: Variant + Clone,
|
T: Variant + Clone,
|
||||||
F: RegisterNativeFunction<A, N, C, T, true>,
|
F: RegisterNativeFunction<A, N, C, T, true>,
|
||||||
{
|
{
|
||||||
|
let fn_name = name.into();
|
||||||
|
let no_const = false;
|
||||||
|
|
||||||
|
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
|
||||||
|
let no_const = no_const || (F::num_params() == 3 && fn_name == crate::engine::FN_IDX_SET);
|
||||||
|
#[cfg(not(feature = "no_object"))]
|
||||||
|
let no_const =
|
||||||
|
no_const || (F::num_params() == 2 && fn_name.starts_with(crate::engine::FN_SET));
|
||||||
|
|
||||||
|
let func = func.into_callable_function(fn_name.clone(), no_const);
|
||||||
|
|
||||||
self.set_fn(
|
self.set_fn(
|
||||||
name,
|
fn_name,
|
||||||
FnNamespace::Internal,
|
FnNamespace::Internal,
|
||||||
FnAccess::Public,
|
FnAccess::Public,
|
||||||
None,
|
None,
|
||||||
F::param_types(),
|
F::param_types(),
|
||||||
func.into_callable_function(),
|
func,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1282,13 +1293,16 @@ impl Module {
|
|||||||
T: Variant + Clone,
|
T: Variant + Clone,
|
||||||
F: RegisterNativeFunction<(Mut<A>,), 1, C, T, true> + SendSync + 'static,
|
F: RegisterNativeFunction<(Mut<A>,), 1, C, T, true> + SendSync + 'static,
|
||||||
{
|
{
|
||||||
|
let fn_name = crate::engine::make_getter(name.as_ref());
|
||||||
|
let func = func.into_callable_function(fn_name.clone(), false);
|
||||||
|
|
||||||
self.set_fn(
|
self.set_fn(
|
||||||
crate::engine::make_getter(name.as_ref()).as_str(),
|
fn_name,
|
||||||
FnNamespace::Global,
|
FnNamespace::Global,
|
||||||
FnAccess::Public,
|
FnAccess::Public,
|
||||||
None,
|
None,
|
||||||
F::param_types(),
|
F::param_types(),
|
||||||
func.into_callable_function(),
|
func,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1323,13 +1337,16 @@ impl Module {
|
|||||||
T: Variant + Clone,
|
T: Variant + Clone,
|
||||||
F: RegisterNativeFunction<(Mut<A>, T), 2, C, (), true> + SendSync + 'static,
|
F: RegisterNativeFunction<(Mut<A>, T), 2, C, (), true> + SendSync + 'static,
|
||||||
{
|
{
|
||||||
|
let fn_name = crate::engine::make_setter(name.as_ref());
|
||||||
|
let func = func.into_callable_function(fn_name.clone(), true);
|
||||||
|
|
||||||
self.set_fn(
|
self.set_fn(
|
||||||
crate::engine::make_setter(name.as_ref()).as_str(),
|
fn_name,
|
||||||
FnNamespace::Global,
|
FnNamespace::Global,
|
||||||
FnAccess::Public,
|
FnAccess::Public,
|
||||||
None,
|
None,
|
||||||
F::param_types(),
|
F::param_types(),
|
||||||
func.into_callable_function(),
|
func,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1437,7 +1454,7 @@ impl Module {
|
|||||||
FnAccess::Public,
|
FnAccess::Public,
|
||||||
None,
|
None,
|
||||||
F::param_types(),
|
F::param_types(),
|
||||||
func.into_callable_function(),
|
func.into_callable_function(crate::engine::FN_IDX_GET.into(), false),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1498,7 +1515,7 @@ impl Module {
|
|||||||
FnAccess::Public,
|
FnAccess::Public,
|
||||||
None,
|
None,
|
||||||
F::param_types(),
|
F::param_types(),
|
||||||
func.into_callable_function(),
|
func.into_callable_function(crate::engine::FN_IDX_SET.into(), true),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1137,8 +1137,12 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, _chaining: bool) {
|
|||||||
// Overloaded operators can override built-in.
|
// Overloaded operators can override built-in.
|
||||||
_ if x.args.len() == 2 && x.op_token != Token::NONE && (state.engine.fast_operators() || !state.engine.has_native_fn_override(x.hashes.native(), &arg_types)) => {
|
_ if x.args.len() == 2 && x.op_token != Token::NONE && (state.engine.fast_operators() || !state.engine.has_native_fn_override(x.hashes.native(), &arg_types)) => {
|
||||||
if let Some(result) = get_builtin_binary_op_fn(x.op_token.clone(), &arg_values[0], &arg_values[1])
|
if let Some(result) = get_builtin_binary_op_fn(x.op_token.clone(), &arg_values[0], &arg_values[1])
|
||||||
.and_then(|f| {
|
.and_then(|(f, ctx)| {
|
||||||
let context = (state.engine, x.name.as_str(),None, &state.global, *pos).into();
|
let context = if ctx {
|
||||||
|
Some((state.engine, x.name.as_str(),None, &state.global, *pos).into())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
let (first, second) = arg_values.split_first_mut().unwrap();
|
let (first, second) = arg_values.split_first_mut().unwrap();
|
||||||
(f)(context, &mut [ first, &mut second[0] ]).ok()
|
(f)(context, &mut [ first, &mut second[0] ]).ok()
|
||||||
}) {
|
}) {
|
||||||
|
@ -261,8 +261,7 @@ pub mod array_functions {
|
|||||||
m1 += m2;
|
m1 += m2;
|
||||||
s1 += s2;
|
s1 += s2;
|
||||||
|
|
||||||
_ctx.engine()
|
_ctx.engine().throw_on_size((a1, m1, s1))?;
|
||||||
.raise_err_if_over_data_size_limit((a1, m1, s1))?;
|
|
||||||
|
|
||||||
guard.push(item.clone());
|
guard.push(item.clone());
|
||||||
arr_len += 1;
|
arr_len += 1;
|
||||||
|
@ -81,8 +81,7 @@ pub mod blob_functions {
|
|||||||
|
|
||||||
// Check if blob will be over max size limit
|
// Check if blob will be over max size limit
|
||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
_ctx.engine()
|
_ctx.engine().throw_on_size((len, 0, 0))?;
|
||||||
.raise_err_if_over_data_size_limit((len, 0, 0))?;
|
|
||||||
|
|
||||||
let mut blob = Blob::new();
|
let mut blob = Blob::new();
|
||||||
blob.resize(len, (value & 0x0000_00ff) as u8);
|
blob.resize(len, (value & 0x0000_00ff) as u8);
|
||||||
|
@ -220,7 +220,7 @@ fn collect_fn_metadata(
|
|||||||
"comments".into(),
|
"comments".into(),
|
||||||
func.comments
|
func.comments
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| engine.get_interned_string(s.as_ref()).into())
|
.map(|s| engine.get_interned_string(s.as_str()).into())
|
||||||
.collect::<Array>()
|
.collect::<Array>()
|
||||||
.into(),
|
.into(),
|
||||||
);
|
);
|
||||||
|
@ -3567,16 +3567,14 @@ impl Engine {
|
|||||||
|
|
||||||
/// Parse a function definition.
|
/// Parse a function definition.
|
||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
fn parse_fn(
|
fn parse_fn<S: Into<Identifier>>(
|
||||||
&self,
|
&self,
|
||||||
input: &mut TokenStream,
|
input: &mut TokenStream,
|
||||||
state: &mut ParseState,
|
state: &mut ParseState,
|
||||||
lib: &mut FnLib,
|
lib: &mut FnLib,
|
||||||
access: crate::FnAccess,
|
access: crate::FnAccess,
|
||||||
settings: ParseSettings,
|
settings: ParseSettings,
|
||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(feature = "metadata")] comments: impl IntoIterator<Item = S>,
|
||||||
#[cfg(feature = "metadata")]
|
|
||||||
comments: impl IntoIterator<Item = String>,
|
|
||||||
) -> ParseResult<ScriptFnDef> {
|
) -> ParseResult<ScriptFnDef> {
|
||||||
let settings = settings;
|
let settings = settings;
|
||||||
|
|
||||||
@ -3660,13 +3658,8 @@ impl Engine {
|
|||||||
body,
|
body,
|
||||||
#[cfg(not(feature = "no_module"))]
|
#[cfg(not(feature = "no_module"))]
|
||||||
environ: None,
|
environ: None,
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
comments: comments
|
comments: comments.into_iter().map(Into::into).collect(),
|
||||||
.into_iter()
|
|
||||||
.map(|s| s.into_boxed_str())
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.into_boxed_slice(),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user