Skip to content

Commit

Permalink
Merge pull request #808 from schungx/master
Browse files Browse the repository at this point in the history
Guard against reference loops
  • Loading branch information
schungx committed Jan 5, 2024
2 parents ed4dfaa + 96930dc commit f61beb6
Show file tree
Hide file tree
Showing 24 changed files with 390 additions and 70 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,19 @@ Fixes to bugs found via fuzzing
* Fixed crash when indenting a block doc-comment with Unicode multi-byte space characters.
* Fixed improper parsing of numbers with too many decimal points.
* Fixed exponential running time when raising a decimal number to a very large power (> 1 million) -- it now returns an overflow error.
* Shared values that contain reference loops no longer cause a stack overflow when printing.

Other bug fixes
---------------

* Arrays in object maps now serialize to JSON correctly via `to_json()` when the `serde` feature is not enabled.
* `Engine::format_map_as_json` now serializes arrays correctly.

New features
------------

* Functions defined in plugin modules can now be marked as `volatile` which prevents it from being optimized away even under `OptimizationLevel::Full`.

Enhancements
------------

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ num-traits = { version = "0.2.0", default-features = false }
once_cell = { version = "1.7.0", default-features = false, features = ["race"] }
bitflags = { version = "2.0.0", default-features = false }
smartstring = { version = "1.0.0", default-features = false }
rhai_codegen = { version = "1.5.0", path = "codegen", default-features = false }
rhai_codegen = { version = "1.7.0", path = "codegen" }

no-std-compat = { git = "https://gitlab.com/jD91mZM2/no-std-compat", version = "0.4.1", default-features = false, features = ["alloc"], optional = true }
libm = { version = "0.2.0", default-features = false, optional = true }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Rhai - Embedded Scripting for Rust

![GitHub last commit](https://img.shields.io/github/last-commit/rhaiscript/rhai?logo=github)
[![Build Status](https://github.com/rhaiscript/rhai/workflows/Build/badge.svg)](https://github.com/rhaiscript/rhai/actions)
[![Stars](https://img.shields.io/github/stars/rhaiscript/rhai?logo=github)](https://github.com/rhaiscript/rhai)
[![Stars](https://img.shields.io/github/stars/rhaiscript/rhai?style=flat&logo=github)](https://github.com/rhaiscript/rhai)
[![License](https://img.shields.io/crates/l/rhai)](https://github.com/license/rhaiscript/rhai)
[![crates.io](https://img.shields.io/crates/v/rhai?logo=rust)](https://crates.io/crates/rhai/)
[![crates.io](https://img.shields.io/crates/d/rhai?logo=rust)](https://crates.io/crates/rhai/)
Expand Down
4 changes: 2 additions & 2 deletions codegen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rhai_codegen"
version = "1.6.1"
version = "1.7.0"
edition = "2018"
resolver = "2"
authors = ["jhwgh1968", "Stephen Chung"]
Expand All @@ -24,5 +24,5 @@ syn = { version = "2.0.0", features = ["full", "parsing", "printing", "proc-macr
quote = "1.0.0"

[dev-dependencies]
rhai = { path = "..", version = "1.16.0", features = ["metadata"] }
rhai = { path = "..", version = "1.17.0", features = ["metadata"] }
trybuild = "1.0.0"
6 changes: 6 additions & 0 deletions codegen/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub struct ExportedFnParams {
pub name: Vec<String>,
pub return_raw: Option<Span>,
pub pure: Option<Span>,
pub volatile: Option<Span>,
pub skip: bool,
pub special: FnSpecialAccess,
pub namespace: FnNamespaceAccess,
Expand Down Expand Up @@ -130,6 +131,7 @@ impl ExportedParams for ExportedFnParams {
let mut name = Vec::new();
let mut return_raw = None;
let mut pure = None;
let mut volatile = None;
let mut skip = false;
let mut namespace = FnNamespaceAccess::Unset;
let mut special = FnSpecialAccess::None;
Expand Down Expand Up @@ -186,6 +188,7 @@ impl ExportedParams for ExportedFnParams {
}

("pure", None) => pure = Some(item_span),
("volatile", None) => volatile = Some(item_span),
("return_raw", None) => return_raw = Some(item_span),
("skip", None) => skip = true,
("global", None) => match namespace {
Expand Down Expand Up @@ -255,6 +258,7 @@ impl ExportedParams for ExportedFnParams {
name,
return_raw,
pure,
volatile,
skip,
special,
namespace,
Expand Down Expand Up @@ -664,6 +668,7 @@ impl ExportedFn {
let arg_count = self.arg_count();
let is_method_call = self.mutable_receiver();
let is_pure = !self.mutable_receiver() || self.params().pure.is_some();
let is_volatile = self.params().volatile.is_some();
let pass_context = self.pass_context;

let mut unpack_statements = Vec::new();
Expand Down Expand Up @@ -858,6 +863,7 @@ impl ExportedFn {

#[inline(always)] fn is_method_call(&self) -> bool { #is_method_call }
#[inline(always)] fn is_pure(&self) -> bool { #is_pure }
#[inline(always)] fn is_volatile(&self) -> bool { #is_volatile }
#[inline(always)] fn has_context(&self) -> bool { #pass_context }
}
}
Expand Down
8 changes: 3 additions & 5 deletions codegen/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,9 @@ impl ExportedParams for ExportedModParams {
}
}

Ok(ExportedModParams {
name,
skip,
scope: scope.unwrap_or_default(),
})
let scope = scope.unwrap_or_default();

Ok(ExportedModParams { name, skip, scope })
}
}

Expand Down
8 changes: 8 additions & 0 deletions codegen/src/test/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_pure(&self) -> bool { true }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
#[allow(unused)]
Expand Down Expand Up @@ -326,6 +327,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_pure(&self) -> bool { true }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
#[allow(unused)]
Expand Down Expand Up @@ -366,6 +368,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_pure(&self) -> bool { true }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { true }
}
#[allow(unused)]
Expand Down Expand Up @@ -408,6 +411,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_pure(&self) -> bool { true }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
#[allow(unused)]
Expand Down Expand Up @@ -443,6 +447,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_pure(&self) -> bool { true }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
};
Expand Down Expand Up @@ -478,6 +483,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_pure(&self) -> bool { true }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
#[allow(unused)]
Expand Down Expand Up @@ -519,6 +525,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { true }
#[inline(always)] fn is_pure(&self) -> bool { false }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
#[allow(unused)]
Expand Down Expand Up @@ -560,6 +567,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_pure(&self) -> bool { true }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
#[allow(unused)]
Expand Down
25 changes: 25 additions & 0 deletions codegen/src/test/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_pure(&self) -> bool { true }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -496,6 +497,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_pure(&self) -> bool { true }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -557,6 +559,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_pure(&self) -> bool { true }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -617,6 +620,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_pure(&self) -> bool { true }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -691,6 +695,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_pure(&self) -> bool { true }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}

Expand All @@ -712,6 +717,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_pure(&self) -> bool { true }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -773,6 +779,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_pure(&self) -> bool { true }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -841,6 +848,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_pure(&self) -> bool { true }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -918,6 +926,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { true }
#[inline(always)] fn is_pure(&self) -> bool { false }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -1133,6 +1142,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_pure(&self) -> bool { true }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -1227,6 +1237,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_pure(&self) -> bool { true }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -1287,6 +1298,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { false }
#[inline(always)] fn is_pure(&self) -> bool { true }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -1349,6 +1361,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { true }
#[inline(always)] fn is_pure(&self) -> bool { true }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -1409,6 +1422,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { true }
#[inline(always)] fn is_pure(&self) -> bool { false }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -1473,6 +1487,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { true }
#[inline(always)] fn is_pure(&self) -> bool { false }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -1559,6 +1574,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { true }
#[inline(always)] fn is_pure(&self) -> bool { false }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -1646,6 +1662,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { true }
#[inline(always)] fn is_pure(&self) -> bool { false }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -1710,6 +1727,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { true }
#[inline(always)] fn is_pure(&self) -> bool { false }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -1772,6 +1790,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { true }
#[inline(always)] fn is_pure(&self) -> bool { false }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -1837,6 +1856,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { true }
#[inline(always)] fn is_pure(&self) -> bool { false }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -1899,6 +1919,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { true }
#[inline(always)] fn is_pure(&self) -> bool { false }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -1969,6 +1990,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { true }
#[inline(always)] fn is_pure(&self) -> bool { false }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -2034,6 +2056,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { true }
#[inline(always)] fn is_pure(&self) -> bool { false }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -2097,6 +2120,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { true }
#[inline(always)] fn is_pure(&self) -> bool { false }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down Expand Up @@ -2163,6 +2187,7 @@ mod generate_tests {

#[inline(always)] fn is_method_call(&self) -> bool { true }
#[inline(always)] fn is_pure(&self) -> bool { false }
#[inline(always)] fn is_volatile(&self) -> bool { false }
#[inline(always)] fn has_context(&self) -> bool { false }
}
}
Expand Down

0 comments on commit f61beb6

Please sign in to comment.