Add testing-environ feature.

This commit is contained in:
Stephen Chung 2023-03-02 13:16:15 +08:00
parent 86ccb37029
commit a2f8d057f8
3 changed files with 57 additions and 22 deletions

View File

@ -43,28 +43,28 @@ jobs:
os: [ubuntu-latest] os: [ubuntu-latest]
flags: flags:
- "" - ""
- "--features debugging" - "--features testing-environ,debugging"
- "--features metadata,serde,internals" - "--features testing-environ,metadata,serde,internals"
- "--features unchecked,serde,metadata,internals,debugging" - "--features testing-environ,unchecked,serde,metadata,internals,debugging"
- "--features sync,serde,metadata,internals,debugging" - "--features testing-environ,sync,serde,metadata,internals,debugging"
- "--features no_position,serde,metadata,internals,debugging" - "--features testing-environ,no_position,serde,metadata,internals,debugging"
- "--features no_optimize,serde,metadata,internals,debugging" - "--features testing-environ,no_optimize,serde,metadata,internals,debugging"
- "--features no_float,serde,metadata,internals,debugging" - "--features testing-environ,no_float,serde,metadata,internals,debugging"
- "--features f32_float,serde,metadata,internals,debugging" - "--features testing-environ,f32_float,serde,metadata,internals,debugging"
- "--features decimal,serde,metadata,internals,debugging" - "--features testing-environ,decimal,serde,metadata,internals,debugging"
- "--features no_custom_syntax,serde,metadata,internals,debugging" - "--features testing-environ,no_custom_syntax,serde,metadata,internals,debugging"
- "--features no_float,decimal" - "--features testing-environ,no_float,decimal"
- "--tests --features only_i32,serde,metadata,internals,debugging" - "--tests --features testing-environ,only_i32,serde,metadata,internals,debugging"
- "--features only_i64,serde,metadata,internals,debugging" - "--features testing-environ,only_i64,serde,metadata,internals,debugging"
- "--features no_index,serde,metadata,internals,debugging" - "--features testing-environ,no_index,serde,metadata,internals,debugging"
- "--features no_object,serde,metadata,internals,debugging" - "--features testing-environ,no_object,serde,metadata,internals,debugging"
- "--features no_function,serde,metadata,internals,debugging" - "--features testing-environ,no_function,serde,metadata,internals,debugging"
- "--features no_module,serde,metadata,internals,debugging" - "--features testing-environ,no_module,serde,metadata,internals,debugging"
- "--features no_time,serde,metadata,internals,debugging" - "--features testing-environ,no_time,serde,metadata,internals,debugging"
- "--features no_closure,serde,metadata,internals,debugging" - "--features testing-environ,no_closure,serde,metadata,internals,debugging"
- "--features unicode-xid-ident,serde,metadata,internals,debugging" - "--features testing-environ,unicode-xid-ident,serde,metadata,internals,debugging"
- "--features sync,no_time,no_function,no_float,no_position,no_optimize,no_module,no_closure,no_custom_syntax,metadata,serde,unchecked,debugging" - "--features testing-environ,sync,no_time,no_function,no_float,no_position,no_optimize,no_module,no_closure,no_custom_syntax,metadata,serde,unchecked,debugging"
- "--features no_time,no_function,no_float,no_position,no_index,no_object,no_optimize,no_module,no_closure,no_custom_syntax,unchecked" - "--features testing-environ,no_time,no_function,no_float,no_position,no_index,no_object,no_optimize,no_module,no_closure,no_custom_syntax,unchecked"
toolchain: [stable] toolchain: [stable]
experimental: [false] experimental: [false]
include: include:

View File

@ -117,6 +117,11 @@ std = ["ahash/std", "num-traits/std", "smartstring/std"]
## Use [`stdweb`](https://crates.io/crates/stdweb) as JavaScript interface. ## Use [`stdweb`](https://crates.io/crates/stdweb) as JavaScript interface.
stdweb = ["getrandom/js", "instant/stdweb"] stdweb = ["getrandom/js", "instant/stdweb"]
#! ### Features used in testing environments only
## Running under a testing environment.
testing-environ = []
[[bin]] [[bin]]
name = "rhai-repl" name = "rhai-repl"
required-features = ["rustyline"] required-features = ["rustyline"]

View File

@ -715,8 +715,24 @@ impl Module {
// None + function name + number of arguments. // None + function name + number of arguments.
let num_params = fn_def.params.len(); let num_params = fn_def.params.len();
let hash_script = crate::calc_fn_hash(None, &fn_def.name, num_params); let hash_script = crate::calc_fn_hash(None, &fn_def.name, num_params);
#[cfg(feature = "testing-environ")]
if let Some(f) = self
.functions
.get_or_insert_with(|| {
StraightHashMap::with_capacity_and_hasher(FN_MAP_SIZE, Default::default())
})
.get(&hash_script)
{
panic!(
"PANIC ATTACK!!! Function hash {} already exists when registering function {:#?}:\n{:#?}",
hash_script, fn_def, f
);
}
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
let params_info = fn_def.params.iter().map(Into::into).collect(); let params_info = fn_def.params.iter().map(Into::into).collect();
self.functions self.functions
.get_or_insert_with(|| { .get_or_insert_with(|| {
StraightHashMap::with_capacity_and_hasher(FN_MAP_SIZE, Default::default()) StraightHashMap::with_capacity_and_hasher(FN_MAP_SIZE, Default::default())
@ -1046,6 +1062,20 @@ impl Module {
let hash_script = calc_fn_hash(None, name, param_types.len()); let hash_script = calc_fn_hash(None, name, param_types.len());
let hash_fn = calc_fn_hash_full(hash_script, param_types.iter().copied()); let hash_fn = calc_fn_hash_full(hash_script, param_types.iter().copied());
#[cfg(feature = "testing-environ")]
if let Some(f) = self
.functions
.get_or_insert_with(|| {
StraightHashMap::with_capacity_and_hasher(FN_MAP_SIZE, Default::default())
})
.get(&hash_script)
{
panic!(
"PANIC ATTACK!!! Function hash {} already exists when registering function {}:\n{:#?}",
hash_script, name, f
);
}
if is_dynamic { if is_dynamic {
self.dynamic_functions_filter self.dynamic_functions_filter
.get_or_insert_with(Default::default) .get_or_insert_with(Default::default)