Add variants of Array methods taking FnPtr with function names.

This commit is contained in:
Stephen Chung 2021-10-20 11:06:38 +08:00
parent 3001e90775
commit c8e7c970d1
2 changed files with 108 additions and 0 deletions

View File

@ -8,6 +8,7 @@ Enhancements
------------ ------------
* Array methods now avoid cloning as much as possible (although most predicates will involve cloning anyway if passed a closure). * Array methods now avoid cloning as much as possible (although most predicates will involve cloning anyway if passed a closure).
* Array methods that take function pointers (e.g. closures) now optionally take the function name as a string.
* Inlining is disabled for error-path functions because errors are exceptional and scripts usually fail completely when an error is encountered. * Inlining is disabled for error-path functions because errors are exceptional and scripts usually fail completely when an error is encountered.
Deprecated API's Deprecated API's

View File

@ -242,6 +242,14 @@ mod array_functions {
result result
} }
} }
#[rhai_fn(name = "map", return_raw, pure)]
pub fn map_with_fn_name(
ctx: NativeCallContext,
array: &mut Array,
mapper: &str,
) -> Result<Array, Box<EvalAltResult>> {
map(ctx, array, FnPtr::new(mapper)?)
}
#[rhai_fn(return_raw, pure)] #[rhai_fn(return_raw, pure)]
pub fn map( pub fn map(
ctx: NativeCallContext, ctx: NativeCallContext,
@ -285,6 +293,14 @@ mod array_functions {
Ok(ar) Ok(ar)
} }
#[rhai_fn(name = "filter", return_raw, pure)]
pub fn filter_with_fn_name(
ctx: NativeCallContext,
array: &mut Array,
filter_func: &str,
) -> Result<Array, Box<EvalAltResult>> {
filter(ctx, array, FnPtr::new(filter_func)?)
}
#[rhai_fn(return_raw, pure)] #[rhai_fn(return_raw, pure)]
pub fn filter( pub fn filter(
ctx: NativeCallContext, ctx: NativeCallContext,
@ -427,6 +443,14 @@ mod array_functions {
Ok(-1 as INT) Ok(-1 as INT)
} }
#[rhai_fn(name = "index_of", return_raw, pure)] #[rhai_fn(name = "index_of", return_raw, pure)]
pub fn index_of_with_fn_name(
ctx: NativeCallContext,
array: &mut Array,
filter: &str,
) -> Result<INT, Box<EvalAltResult>> {
index_of_filter(ctx, array, FnPtr::new(filter)?)
}
#[rhai_fn(name = "index_of", return_raw, pure)]
pub fn index_of_filter( pub fn index_of_filter(
ctx: NativeCallContext, ctx: NativeCallContext,
array: &mut Array, array: &mut Array,
@ -439,6 +463,15 @@ mod array_functions {
} }
} }
#[rhai_fn(name = "index_of", return_raw, pure)] #[rhai_fn(name = "index_of", return_raw, pure)]
pub fn index_of_with_fn_name_starting_from(
ctx: NativeCallContext,
array: &mut Array,
filter: &str,
start: INT,
) -> Result<INT, Box<EvalAltResult>> {
index_of_filter_starting_from(ctx, array, FnPtr::new(filter)?, start)
}
#[rhai_fn(name = "index_of", return_raw, pure)]
pub fn index_of_filter_starting_from( pub fn index_of_filter_starting_from(
ctx: NativeCallContext, ctx: NativeCallContext,
array: &mut Array, array: &mut Array,
@ -496,6 +529,14 @@ mod array_functions {
Ok(-1 as INT) Ok(-1 as INT)
} }
#[rhai_fn(name = "some", return_raw, pure)]
pub fn some_with_fn_name(
ctx: NativeCallContext,
array: &mut Array,
filter: &str,
) -> Result<bool, Box<EvalAltResult>> {
some(ctx, array, FnPtr::new(filter)?)
}
#[rhai_fn(return_raw, pure)] #[rhai_fn(return_raw, pure)]
pub fn some( pub fn some(
ctx: NativeCallContext, ctx: NativeCallContext,
@ -542,6 +583,14 @@ mod array_functions {
Ok(false) Ok(false)
} }
#[rhai_fn(name = "all", return_raw, pure)]
pub fn all_with_fn_name(
ctx: NativeCallContext,
array: &mut Array,
filter: &str,
) -> Result<bool, Box<EvalAltResult>> {
all(ctx, array, FnPtr::new(filter)?)
}
#[rhai_fn(return_raw, pure)] #[rhai_fn(return_raw, pure)]
pub fn all( pub fn all(
ctx: NativeCallContext, ctx: NativeCallContext,
@ -588,6 +637,14 @@ mod array_functions {
Ok(true) Ok(true)
} }
#[rhai_fn(name = "reduce", return_raw, pure)]
pub fn reduce_with_fn_name(
ctx: NativeCallContext,
array: &mut Array,
reducer: &str,
) -> Result<Dynamic, Box<EvalAltResult>> {
reduce(ctx, array, FnPtr::new(reducer)?)
}
#[rhai_fn(return_raw, pure)] #[rhai_fn(return_raw, pure)]
pub fn reduce( pub fn reduce(
ctx: NativeCallContext, ctx: NativeCallContext,
@ -624,6 +681,15 @@ mod array_functions {
Ok(result) Ok(result)
} }
#[rhai_fn(name = "reduce", return_raw, pure)] #[rhai_fn(name = "reduce", return_raw, pure)]
pub fn reduce_with_fn_name_with_initial(
ctx: NativeCallContext,
array: &mut Array,
reducer: &str,
initial: Dynamic,
) -> Result<Dynamic, Box<EvalAltResult>> {
reduce_with_initial(ctx, array, FnPtr::new(reducer)?, initial)
}
#[rhai_fn(name = "reduce", return_raw, pure)]
pub fn reduce_with_initial( pub fn reduce_with_initial(
ctx: NativeCallContext, ctx: NativeCallContext,
array: &mut Array, array: &mut Array,
@ -659,6 +725,14 @@ mod array_functions {
Ok(result) Ok(result)
} }
#[rhai_fn(name = "reduce_ref", return_raw, pure)]
pub fn reduce_rev_with_fn_name(
ctx: NativeCallContext,
array: &mut Array,
reducer: &str,
) -> Result<Dynamic, Box<EvalAltResult>> {
reduce_rev(ctx, array, FnPtr::new(reducer)?)
}
#[rhai_fn(return_raw, pure)] #[rhai_fn(return_raw, pure)]
pub fn reduce_rev( pub fn reduce_rev(
ctx: NativeCallContext, ctx: NativeCallContext,
@ -694,6 +768,15 @@ mod array_functions {
Ok(result) Ok(result)
} }
#[rhai_fn(name = "reduce_ref", return_raw, pure)]
pub fn reduce_rev_with_fn_name_with_initial(
ctx: NativeCallContext,
array: &mut Array,
reducer: &str,
initial: Dynamic,
) -> Result<Dynamic, Box<EvalAltResult>> {
reduce_rev_with_initial(ctx, array, FnPtr::new(reducer)?, initial)
}
#[rhai_fn(name = "reduce_rev", return_raw, pure)] #[rhai_fn(name = "reduce_rev", return_raw, pure)]
pub fn reduce_rev_with_initial( pub fn reduce_rev_with_initial(
ctx: NativeCallContext, ctx: NativeCallContext,
@ -730,6 +813,14 @@ mod array_functions {
Ok(result) Ok(result)
} }
#[rhai_fn(name = "sort", return_raw)]
pub fn sort_with_fn_name(
ctx: NativeCallContext,
array: &mut Array,
comparer: &str,
) -> Result<(), Box<EvalAltResult>> {
sort(ctx, array, FnPtr::new(comparer)?)
}
#[rhai_fn(return_raw)] #[rhai_fn(return_raw)]
pub fn sort( pub fn sort(
ctx: NativeCallContext, ctx: NativeCallContext,
@ -756,6 +847,14 @@ mod array_functions {
Ok(()) Ok(())
} }
#[rhai_fn(name = "drain", return_raw, pure)]
pub fn drain_with_fn_name(
ctx: NativeCallContext,
array: &mut Array,
filter: &str,
) -> Result<Array, Box<EvalAltResult>> {
drain(ctx, array, FnPtr::new(filter)?)
}
#[rhai_fn(return_raw)] #[rhai_fn(return_raw)]
pub fn drain( pub fn drain(
ctx: NativeCallContext, ctx: NativeCallContext,
@ -850,6 +949,14 @@ mod array_functions {
array.drain(start..start + len).collect() array.drain(start..start + len).collect()
} }
#[rhai_fn(name = "retain", return_raw, pure)]
pub fn retain_with_fn_name(
ctx: NativeCallContext,
array: &mut Array,
filter: &str,
) -> Result<Array, Box<EvalAltResult>> {
retain(ctx, array, FnPtr::new(filter)?)
}
#[rhai_fn(return_raw)] #[rhai_fn(return_raw)]
pub fn retain( pub fn retain(
ctx: NativeCallContext, ctx: NativeCallContext,