Skip to content

Commit

Permalink
Add volatile functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
schungx committed Jan 5, 2024
1 parent c0b6aac commit a1fe26d
Show file tree
Hide file tree
Showing 19 changed files with 174 additions and 38 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ 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 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 Down
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
8 changes: 7 additions & 1 deletion src/api/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ impl Engine {
}
/// Register a custom function with the [`Engine`].
///
/// # Assumptions
///
/// * The function is assumed to be _pure_ unless it is a property setter or an index setter.
///
/// * The function is assumed to be _volatile_ -- i.e. it does not guarantee the same result for the same input(s).
///
/// # Example
///
/// ```
Expand Down Expand Up @@ -100,7 +106,7 @@ impl Engine {
let is_pure =
is_pure && (F::num_params() != 2 || !fn_name.starts_with(crate::engine::FN_SET));

let func = func.into_callable_function(fn_name.into(), is_pure);
let func = func.into_callable_function(fn_name.into(), is_pure, true);

self.global_namespace_mut().set_fn(
name,
Expand Down
2 changes: 1 addition & 1 deletion src/bin/rhai-repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ fn main() {
println!("{0:=<1$}", "", title.len());

#[cfg(not(feature = "no_optimize"))]
let mut optimize_level = rhai::OptimizationLevel::Simple;
let mut optimize_level = rhai::OptimizationLevel::Full;

// Initialize scripting engine
let mut engine = Engine::new();
Expand Down
Loading

0 comments on commit a1fe26d

Please sign in to comment.