Skip to content

Commit

Permalink
Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
schungx committed Jan 30, 2024
1 parent 8359732 commit 2c7e179
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 61 deletions.
5 changes: 1 addition & 4 deletions src/api/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ impl Engine {
name: impl AsRef<str> + Into<Identifier>,
func: FUNC,
) -> &mut Self {
let mut reg = FuncRegistration::new(name.into())
.with_namespace(FnNamespace::Global)
.with_purity(true)
.with_volatility(false);
let mut reg = FuncRegistration::new(name.into()).with_namespace(FnNamespace::Global);

Check warning on line 78 in src/api/register.rs

View workflow job for this annotation

GitHub Actions / Check Wasm build (--target wasm32-unknown-unknown --no-default-features)

variable does not need to be mutable

Check warning on line 78 in src/api/register.rs

View workflow job for this annotation

GitHub Actions / Check Wasm build (--target wasm32-wasi)

variable does not need to be mutable

Check warning on line 78 in src/api/register.rs

View workflow job for this annotation

GitHub Actions / Check Wasm build (--target wasm32-unknown-unknown --no-default-features --features wasm-bindgen)

variable does not need to be mutable

Check warning on line 78 in src/api/register.rs

View workflow job for this annotation

GitHub Actions / NoStdBuild (ubuntu-latest, --profile unix, false)

variable does not need to be mutable

Check warning on line 78 in src/api/register.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

variable does not need to be mutable

Check warning on line 78 in src/api/register.rs

View workflow job for this annotation

GitHub Actions / NoStdBuild (windows-latest, --profile windows, true)

variable does not need to be mutable

Check warning on line 78 in src/api/register.rs

View workflow job for this annotation

GitHub Actions / Build (nightly, ubuntu-latest, true, --features unstable)

variable does not need to be mutable

Check warning on line 78 in src/api/register.rs

View workflow job for this annotation

GitHub Actions / Build (beta, ubuntu-latest, false, --features unstable)

variable does not need to be mutable

Check warning on line 78 in src/api/register.rs

View workflow job for this annotation

GitHub Actions / Build (stable, macos-latest, false)

variable does not need to be mutable

Check warning on line 78 in src/api/register.rs

View workflow job for this annotation

GitHub Actions / Build (stable, windows-latest, false)

variable does not need to be mutable

Check warning on line 78 in src/api/register.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,unicode-xid-ident, stable, false)

variable does not need to be mutable

Check warning on line 78 in src/api/register.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,no_float,decimal, stable, false)

variable does not need to be mutable

Check warning on line 78 in src/api/register.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,serde, stable, false)

variable does not need to be mutable

Check warning on line 78 in src/api/register.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,decimal, stable, false)

variable does not need to be mutable

Check warning on line 78 in src/api/register.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,internals, stable, false)

variable does not need to be mutable

Check warning on line 78 in src/api/register.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, stable, false)

variable does not need to be mutable

Check warning on line 78 in src/api/register.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,debugging, stable, false)

variable does not need to be mutable

#[cfg(feature = "metadata")]
{
Expand Down
88 changes: 35 additions & 53 deletions src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,9 @@ impl FuncRegistration {
f.num_params = arg_types.as_ref().len();
f.param_types.extend(arg_types.as_ref().iter().copied());

if f.name == crate::engine::FN_IDX_GET || f.name == crate::engine::FN_IDX_SET {
if (f.name == crate::engine::FN_IDX_GET && f.num_params == 2)

Check failure on line 448 in src/module/mod.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,no_time,no_function,no_float,no_position,no_inde...

cannot find value `FN_IDX_GET` in module `crate::engine`
|| (f.name == crate::engine::FN_IDX_SET && f.num_params == 3)

Check failure on line 449 in src/module/mod.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,no_time,no_function,no_float,no_position,no_inde...

cannot find value `FN_IDX_SET` in module `crate::engine`
{
if let Some(&type_id) = f.param_types.first() {
#[cfg(not(feature = "no_index"))]
assert!(
Expand Down Expand Up @@ -1657,18 +1659,25 @@ impl Module {
///
/// # Panics
///
/// Panics if the type is [`Array`][crate::Array] or [`Map`][crate::Map].
/// Indexers for arrays, object maps and strings cannot be registered.
/// Panics if the type is [`Array`][crate::Array], [`Map`][crate::Map], [`String`],
/// [`ImmutableString`][crate::ImmutableString], `&str` or [`INT`][crate::INT].
///
/// Indexers for arrays, object maps, strings and integers cannot be registered.
///
/// # Example
///
/// ```
/// use rhai::{Module, ImmutableString};
///
/// #[derive(Clone)]
/// struct TestStruct(i64);
///
/// let mut module = Module::new();
///
/// let hash = module.set_indexer_get_fn(
/// |x: &mut i64, y: ImmutableString| Ok(*x + y.len() as i64)
/// |x: &mut TestStruct, y: ImmutableString| Ok(x.0 + y.len() as i64)
/// );
///
/// assert!(module.contains_fn(hash));
/// ```
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
Expand All @@ -1680,24 +1689,6 @@ impl Module {
R: Variant + Clone,
FUNC: RhaiNativeFunc<(Mut<A>, B), 2, X, R, true> + SendSync + 'static,
{
#[cfg(not(feature = "no_index"))]
assert!(
TypeId::of::<A>() != TypeId::of::<crate::Array>(),
"Cannot register indexer for arrays."
);
#[cfg(not(feature = "no_object"))]
assert!(
TypeId::of::<A>() != TypeId::of::<crate::Map>(),
"Cannot register indexer for object maps."
);

assert!(
TypeId::of::<A>() != TypeId::of::<String>()
&& TypeId::of::<A>() != TypeId::of::<&str>()
&& TypeId::of::<A>() != TypeId::of::<ImmutableString>(),
"Cannot register indexer for strings."
);

FuncRegistration::new(crate::engine::FN_IDX_GET)
.with_namespace(FnNamespace::Global)
.with_purity(true)
Expand All @@ -1722,24 +1713,26 @@ impl Module {
///
/// # Panics
///
/// Panics if the type is [`Array`][crate::Array] or [`Map`][crate::Map].
/// Indexers for arrays, object maps and strings cannot be registered.
///
/// # Panics
/// Panics if the type is [`Array`][crate::Array], [`Map`][crate::Map], [`String`],
/// [`ImmutableString`][crate::ImmutableString], `&str` or [`INT`][crate::INT].
///
/// Panics if the type is [`Array`][crate::Array] or [`Map`][crate::Map].
/// Indexers for arrays, object maps and strings cannot be registered.
/// Indexers for arrays, object maps, strings and integers cannot be registered.
///
/// # Example
///
/// ```
/// use rhai::{Module, ImmutableString};
///
/// #[derive(Clone)]
/// struct TestStruct(i64);
///
/// let mut module = Module::new();
/// let hash = module.set_indexer_set_fn(|x: &mut i64, y: ImmutableString, value: i64| {
/// *x = y.len() as i64 + value;
///
/// let hash = module.set_indexer_set_fn(|x: &mut TestStruct, y: ImmutableString, value: i64| {
/// *x = TestStruct(y.len() as i64 + value);
/// Ok(())
/// });
/// });
///
/// assert!(module.contains_fn(hash));
/// ```
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
Expand All @@ -1751,24 +1744,6 @@ impl Module {
R: Variant + Clone,
FUNC: RhaiNativeFunc<(Mut<A>, B, R), 3, X, (), true> + SendSync + 'static,
{
#[cfg(not(feature = "no_index"))]
assert!(
TypeId::of::<A>() != TypeId::of::<crate::Array>(),
"Cannot register indexer for arrays."
);
#[cfg(not(feature = "no_object"))]
assert!(
TypeId::of::<A>() != TypeId::of::<crate::Map>(),
"Cannot register indexer for object maps."
);

assert!(
TypeId::of::<A>() != TypeId::of::<String>()
&& TypeId::of::<A>() != TypeId::of::<&str>()
&& TypeId::of::<A>() != TypeId::of::<ImmutableString>(),
"Cannot register indexer for strings."
);

FuncRegistration::new(crate::engine::FN_IDX_SET)
.with_namespace(FnNamespace::Global)
.with_purity(false)
Expand All @@ -1787,19 +1762,26 @@ impl Module {
///
/// # Panics
///
/// Panics if the type is [`Array`][crate::Array] or [`Map`][crate::Map].
/// Indexers for arrays, object maps and strings cannot be registered.
/// Panics if the type is [`Array`][crate::Array], [`Map`][crate::Map], [`String`],
/// [`ImmutableString`][crate::ImmutableString], `&str` or [`INT`][crate::INT].
///
/// Indexers for arrays, object maps, strings and integers cannot be registered.
///
/// # Example
///
/// ```
/// use rhai::{Module, ImmutableString};
///
/// #[derive(Clone)]
/// struct TestStruct(i64);
///
/// let mut module = Module::new();
///
/// let (hash_get, hash_set) = module.set_indexer_get_set_fn(
/// |x: &mut i64, y: ImmutableString| Ok(*x + y.len() as i64),
/// |x: &mut i64, y: ImmutableString, value: i64| { *x = y.len() as i64 + value; Ok(()) }
/// |x: &mut TestStruct, y: ImmutableString| Ok(x.0 + y.len() as i64),
/// |x: &mut TestStruct, y: ImmutableString, value: i64| { *x = TestStruct(y.len() as i64 + value); Ok(()) }
/// );
///
/// assert!(module.contains_fn(hash_get));
/// assert!(module.contains_fn(hash_set));
/// ```
Expand Down
9 changes: 5 additions & 4 deletions tests/optimizer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![cfg(not(feature = "no_optimize"))]
use rhai::{CustomType, Engine, FuncRegistration, Module, OptimizationLevel, Scope, TypeBuilder, INT};
use rhai::{Engine, FuncRegistration, Module, OptimizationLevel, Scope, INT};

Check warning on line 2 in tests/optimizer.rs

View workflow job for this annotation

GitHub Actions / Build (nightly, ubuntu-latest, true, --features unstable)

unused import: `Module`

Check warning on line 2 in tests/optimizer.rs

View workflow job for this annotation

GitHub Actions / Build (beta, ubuntu-latest, false, --features unstable)

unused import: `Module`

Check warning on line 2 in tests/optimizer.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,no_function,serde,metadata,internals,debugging, ...

unused import: `Module`

Check warning on line 2 in tests/optimizer.rs

View workflow job for this annotation

GitHub Actions / Build (stable, macos-latest, false)

unused import: `Module`

Check warning on line 2 in tests/optimizer.rs

View workflow job for this annotation

GitHub Actions / Build (stable, windows-latest, false)

unused import: `Module`

Check warning on line 2 in tests/optimizer.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,unicode-xid-ident, stable, false)

unused import: `Module`

Check warning on line 2 in tests/optimizer.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,no_float,decimal, stable, false)

unused import: `Module`

Check warning on line 2 in tests/optimizer.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,decimal, stable, false)

unused import: `Module`

Check warning on line 2 in tests/optimizer.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, stable, false)

unused import: `Module`

#[test]
fn test_optimizer() {
Expand Down Expand Up @@ -147,7 +147,7 @@ fn test_optimizer_re_optimize() {

#[test]
fn test_optimizer_full() {
#[derive(Debug, Clone, CustomType)]
#[derive(Debug, Clone)]
struct TestStruct(INT);

let mut engine = Engine::new();
Expand All @@ -170,15 +170,16 @@ fn test_optimizer_full() {
);

engine
.build_type::<TestStruct>()
.register_type_with_name::<TestStruct>("TestStruct")
.register_fn("ts", |n: INT| TestStruct(n))
.register_fn("value", |ts: &mut TestStruct| ts.0)
.register_fn("+", |ts1: &mut TestStruct, ts2: TestStruct| TestStruct(ts1.0 + ts2.0));

let ast = engine
.compile(
"
const FOO = ts(40) + ts(2);
field0(FOO)
value(FOO)
",
)
.unwrap();
Expand Down

0 comments on commit 2c7e179

Please sign in to comment.