From 0f7b3908813d1b1058665a06676b06ea76fe8b13 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 19 Jan 2021 14:22:55 +0800 Subject: [PATCH 1/7] Serialize Timestamp with type name instead of panic. --- src/serde_impl/serialize.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/serde_impl/serialize.rs b/src/serde_impl/serialize.rs index bdab788f..a4d011de 100644 --- a/src/serde_impl/serialize.rs +++ b/src/serde_impl/serialize.rs @@ -34,7 +34,7 @@ impl Serialize for Dynamic { } Union::FnPtr(f, _) => ser.serialize_str(f.fn_name()), #[cfg(not(feature = "no_std"))] - Union::TimeStamp(_, _) => unimplemented!("serialization of timestamp is not supported"), + Union::TimeStamp(x, _) => ser.serialize_str((**x).type_name()), Union::Variant(v, _) => ser.serialize_str((***v).type_name()), From 9acf7b4774185905e2d10f4e57350bb48a23be52 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 19 Jan 2021 14:23:05 +0800 Subject: [PATCH 2/7] Bump ahash to 0.6. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index e433e996..2b12fb19 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ categories = [ "no-std", "embedded", "wasm", "parser-implementations" ] [dependencies] smallvec = { version = "1.6", default-features = false, features = ["union"] } -ahash = { version = "0.5", default-features = false } +ahash = { version = "0.6", default-features = false } rhai_codegen = { version = "0.3", path = "codegen" } [features] From 62462ffeadc866426f9bd3043033872025e72931 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 20 Jan 2021 23:17:28 +0800 Subject: [PATCH 3/7] Add wasm-bindgen and stdweb for WASM builds. --- README.md | 10 +++++----- RELEASES.md | 10 ++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 83428cf0..cfd6da40 100644 --- a/README.md +++ b/README.md @@ -28,22 +28,22 @@ Supported targets and builds Standard features ----------------- -* Easy-to-use language similar to JavaScript+Rust with dynamic typing. -* Fairly low compile-time overhead. -* Fairly efficient evaluation (1 million iterations in 0.3 sec on a single core, 2.3 GHz Linux VM). +* Simple language similar to JavaScript+Rust with dynamic typing. +* Fairly efficient evaluation (1 million iterations in 0.3 sec on a single-core, 2.3 GHz Linux VM). * Tight integration with native Rust [functions](https://rhaiscript.github.io/book/rust/functions.html) and [types]([#custom-types-and-methods](https://rhaiscript.github.io/book/rust/custom.html)), including [getters/setters](https://rhaiscript.github.io/book/rust/getters-setters.html), [methods](https://rhaiscript.github.io/book/rust/custom.html) and [indexers](https://rhaiscript.github.io/book/rust/indexers.html). * Freely pass Rust variables/constants into a script via an external [`Scope`](https://rhaiscript.github.io/book/rust/scope.html) - all clonable Rust types are supported; no need to implement any special trait. * Easily [call a script-defined function](https://rhaiscript.github.io/book/engine/call-fn.html) from Rust. * Relatively little `unsafe` code (yes there are some for performance reasons). * Few dependencies (currently only [`smallvec`](https://crates.io/crates/smallvec) and [`ahash`](https://crates.io/crates/ahash)). * Re-entrant scripting engine can be made `Send + Sync` (via the `sync` feature). -* Scripts are [optimized](https://rhaiscript.github.io/book/engine/optimize.html) (useful for template-based machine-generated scripts) for repeated evaluations. +* Compile once to AST form for repeated evaluations. +* Scripts are [optimized](https://rhaiscript.github.io/book/engine/optimize.html) (useful for template-based machine-generated scripts). * Easy custom API development via [plugins](https://rhaiscript.github.io/book/plugins/index.html) system powered by procedural macros. * [Function overloading](https://rhaiscript.github.io/book/language/overload.html) and [operator overloading](https://rhaiscript.github.io/book/rust/operators.html). * Dynamic dispatch via [function pointers](https://rhaiscript.github.io/book/language/fn-ptr.html) with additional support for [currying](https://rhaiscript.github.io/book/language/fn-curry.html). * [Closures](https://rhaiscript.github.io/book/language/fn-closure.html) (anonymous functions) that can capture shared values. * Some syntactic support for [object-oriented programming (OOP)](https://rhaiscript.github.io/book/language/oop.html). -* Organize code base with dynamically-loadable [modules](https://rhaiscript.github.io/book/language/modules.html). +* Organize code base with dynamically-loadable [modules](https://rhaiscript.github.io/book/language/modules.html), optionally overriding the resolution process * Serialization/deserialization support via [serde](https://crates.io/crates/serde) (requires the `serde` feature). * Support for [minimal builds](https://rhaiscript.github.io/book/start/builds/minimal.html) by excluding unneeded language [features](https://rhaiscript.github.io/book/start/features.html). diff --git a/RELEASES.md b/RELEASES.md index 4fcc5001..4eb31b70 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -4,6 +4,10 @@ Rhai Release Notes Version 0.19.11 =============== +This version streamlines compiling for WASM. + +Rust compiler minimum version is raised to 1.49. + Breaking changes ---------------- @@ -14,10 +18,16 @@ Bug fixes * Fixes compilation errors in `metadata` feature build. +New features +------------ + +* Two new features, `wasm-bindgen` and `stdweb`, to specify the JS interop layer for WASM builds. `wasm-bindgen` used to be required. + Enhancements ------------ * `ahash` is used to hash function call parameters. This should yield speed improvements. +* `Dynamic` and `ImmutableString` now implement `serde::Serialize` and `serde::Deserialize`. Version 0.19.10 From f6553c1426d134ebfea313725441f5621c0e5e04 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 21 Jan 2021 10:32:05 +0800 Subject: [PATCH 4/7] Fix feature builds. --- src/optimize.rs | 8 ++------ src/serde_impl/serialize.rs | 4 ++-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/optimize.rs b/src/optimize.rs index 2cc27819..6a0864f0 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -327,10 +327,7 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut State, preserve_result: bool) { *stmt = if preserve_result { // -> { expr, Noop } - let mut statements = Vec::new(); - statements.push(Stmt::Expr(expr)); - statements.push(mem::take(&mut x.0)); - Stmt::Block(statements, pos) + Stmt::Block(vec![Stmt::Expr(expr), mem::take(&mut x.0)], pos) } else { // -> expr Stmt::Expr(expr) @@ -415,8 +412,7 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut State, preserve_result: bool) { Stmt::Break(pos) => { // Only a single break statement - turn into running the guard expression once state.set_dirty(); - let mut statements = Vec::new(); - statements.push(Stmt::Expr(mem::take(condition))); + let mut statements = vec![Stmt::Expr(mem::take(condition))]; if preserve_result { statements.push(Stmt::Noop(pos)) } diff --git a/src/serde_impl/serialize.rs b/src/serde_impl/serialize.rs index a4d011de..49dbf71a 100644 --- a/src/serde_impl/serialize.rs +++ b/src/serde_impl/serialize.rs @@ -1,6 +1,6 @@ //! Implementations of [`serde::Serialize`]. -use crate::dynamic::Union; +use crate::dynamic::{Union, Variant}; use crate::stdlib::string::ToString; use crate::{Dynamic, ImmutableString}; use serde::ser::{Serialize, SerializeMap, Serializer}; @@ -34,7 +34,7 @@ impl Serialize for Dynamic { } Union::FnPtr(f, _) => ser.serialize_str(f.fn_name()), #[cfg(not(feature = "no_std"))] - Union::TimeStamp(x, _) => ser.serialize_str((**x).type_name()), + Union::TimeStamp(x, _) => ser.serialize_str(x.as_ref().type_name()), Union::Variant(v, _) => ser.serialize_str((***v).type_name()), From 8aae3ac46ccd18fad3971275005a802c0e903986 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 21 Jan 2021 21:49:25 +0800 Subject: [PATCH 5/7] Add function name to NativeCallContext. --- RELEASES.md | 4 +++- src/engine.rs | 10 +++++++--- src/fn_call.rs | 20 ++++++++++++-------- src/fn_native.rs | 42 ++++++++++++++++++++++++++---------------- tests/closures.rs | 3 ++- 5 files changed, 50 insertions(+), 29 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index 4eb31b70..2fec4b9e 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -11,7 +11,8 @@ Rust compiler minimum version is raised to 1.49. Breaking changes ---------------- -Rust compiler requirement raised to 1.49. +* Rust compiler requirement raised to 1.49. +* `NativeCallContext::new` taker an additional parameter containing the name of the function called. Bug fixes --------- @@ -28,6 +29,7 @@ Enhancements * `ahash` is used to hash function call parameters. This should yield speed improvements. * `Dynamic` and `ImmutableString` now implement `serde::Serialize` and `serde::Deserialize`. +* `NativeCallContext` has a new field containing the name of the function called, useful when the same Rust function is registered under multiple names in Rhai. Version 0.19.10 diff --git a/src/engine.rs b/src/engine.rs index c6dce576..150a8eb5 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1971,12 +1971,16 @@ impl Engine { let args = &mut [lhs_ptr_inner, &mut rhs_val]; // Overriding exact implementation - let source = source.or_else(|| state.source.as_ref()); + let source = + source.or_else(|| state.source.as_ref()).map(|s| s.as_str()); if func.is_plugin_fn() { func.get_plugin_fn() - .call((self, source, &*mods, lib).into(), args)?; + .call((self, op.as_ref(), source, &*mods, lib).into(), args)?; } else { - func.get_native_fn()((self, source, &*mods, lib).into(), args)?; + func.get_native_fn()( + (self, op.as_ref(), source, &*mods, lib).into(), + args, + )?; } } // Built-in op-assignment function diff --git a/src/fn_call.rs b/src/fn_call.rs index 5ee9387f..929eb825 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -211,12 +211,13 @@ impl Engine { state.source.as_ref() } else { source.as_ref() - }; + } + .map(|s| s.as_str()); let result = if func.is_plugin_fn() { func.get_plugin_fn() - .call((self, source, mods, lib).into(), args) + .call((self, fn_name, source, mods, lib).into(), args) } else { - func.get_native_fn()((self, source, mods, lib).into(), args) + func.get_native_fn()((self, fn_name, source, mods, lib).into(), args) }; // Restore the original reference @@ -1236,10 +1237,10 @@ impl Engine { result } - Some(f) if f.is_plugin_fn() => f - .get_plugin_fn() - .clone() - .call((self, module.id_raw(), &*mods, lib).into(), args.as_mut()), + Some(f) if f.is_plugin_fn() => f.get_plugin_fn().clone().call( + (self, fn_name, module.id(), &*mods, lib).into(), + args.as_mut(), + ), Some(f) if f.is_native() => { if !f.is_method() { // Clone first argument @@ -1250,7 +1251,10 @@ impl Engine { } } - f.get_native_fn()((self, module.id_raw(), &*mods, lib).into(), args.as_mut()) + f.get_native_fn()( + (self, fn_name, module.id(), &*mods, lib).into(), + args.as_mut(), + ) } Some(f) => unreachable!("unknown function type: {:?}", f), None if def_val.is_some() => Ok(def_val.unwrap().clone()), diff --git a/src/fn_native.rs b/src/fn_native.rs index 6144ac5e..6372c3c7 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -55,48 +55,52 @@ pub type Locked = crate::stdlib::sync::RwLock; /// Context of a native Rust function call. #[derive(Debug, Copy, Clone)] -pub struct NativeCallContext<'e, 's, 'a, 'm, 'pm: 'm> { +pub struct NativeCallContext<'e, 'n, 's, 'a, 'm, 'pm: 'm> { engine: &'e Engine, + fn_name: &'n str, source: Option<&'s str>, pub(crate) mods: Option<&'a Imports>, pub(crate) lib: &'m [&'pm Module], } -impl<'e, 's, 'a, 'm, 'pm: 'm, M: AsRef<[&'pm Module]> + ?Sized> - From<(&'e Engine, Option<&'s ImmutableString>, &'a Imports, &'m M)> - for NativeCallContext<'e, 's, 'a, 'm, 'pm> +impl<'e, 'n, 's, 'a, 'm, 'pm: 'm, M: AsRef<[&'pm Module]> + ?Sized> + From<(&'e Engine, &'n str, Option<&'s str>, &'a Imports, &'m M)> + for NativeCallContext<'e, 'n, 's, 'a, 'm, 'pm> { #[inline(always)] - fn from(value: (&'e Engine, Option<&'s ImmutableString>, &'a Imports, &'m M)) -> Self { + fn from(value: (&'e Engine, &'n str, Option<&'s str>, &'a Imports, &'m M)) -> Self { Self { engine: value.0, - source: value.1.map(|s| s.as_str()), - mods: Some(value.2), - lib: value.3.as_ref(), + fn_name: value.1, + source: value.2, + mods: Some(value.3), + lib: value.4.as_ref(), } } } -impl<'e, 'm, 'pm: 'm, M: AsRef<[&'pm Module]> + ?Sized> From<(&'e Engine, &'m M)> - for NativeCallContext<'e, '_, '_, 'm, 'pm> +impl<'e, 'n, 'm, 'pm: 'm, M: AsRef<[&'pm Module]> + ?Sized> From<(&'e Engine, &'n str, &'m M)> + for NativeCallContext<'e, 'n, '_, '_, 'm, 'pm> { #[inline(always)] - fn from(value: (&'e Engine, &'m M)) -> Self { + fn from(value: (&'e Engine, &'n str, &'m M)) -> Self { Self { engine: value.0, + fn_name: value.1, source: None, mods: None, - lib: value.1.as_ref(), + lib: value.2.as_ref(), } } } -impl<'e, 's, 'a, 'm, 'pm> NativeCallContext<'e, 's, 'a, 'm, 'pm> { +impl<'e, 'n, 's, 'a, 'm, 'pm> NativeCallContext<'e, 'n, 's, 'a, 'm, 'pm> { /// Create a new [`NativeCallContext`]. #[inline(always)] - pub fn new(engine: &'e Engine, lib: &'m impl AsRef<[&'pm Module]>) -> Self { + pub fn new(engine: &'e Engine, fn_name: &'n str, lib: &'m impl AsRef<[&'pm Module]>) -> Self { Self { engine, + fn_name, source: None, mods: None, lib: lib.as_ref(), @@ -109,13 +113,14 @@ impl<'e, 's, 'a, 'm, 'pm> NativeCallContext<'e, 's, 'a, 'm, 'pm> { #[inline(always)] pub fn new_with_all_fields( engine: &'e Engine, - source: &'s Option, + fn_name: &'n str, + source: &'s Option<&str>, imports: &'a mut Imports, lib: &'m impl AsRef<[&'pm Module]>, ) -> Self { Self { engine, - source: source.as_ref().map(|s| s.as_str()), + source: source.clone(), mods: Some(imports), lib: lib.as_ref(), } @@ -125,6 +130,11 @@ impl<'e, 's, 'a, 'm, 'pm> NativeCallContext<'e, 's, 'a, 'm, 'pm> { pub fn engine(&self) -> &Engine { self.engine } + /// Name of the function called. + #[inline(always)] + pub fn fn_name(&self) -> &str { + self.fn_name + } /// The current source. #[inline(always)] pub fn source(&self) -> Option<&str> { diff --git a/tests/closures.rs b/tests/closures.rs index 4a0cd74b..3f9fcfa2 100644 --- a/tests/closures.rs +++ b/tests/closures.rs @@ -286,7 +286,8 @@ fn test_closures_external() -> Result<(), Box> { let lib = [ast.as_ref()]; // Create native call context - let context = NativeCallContext::new(&engine, &lib); + let fn_name = fn_ptr.fn_name().to_string(); + let context = NativeCallContext::new(&engine, &fn_name, &lib); // Closure 'f' captures: the engine, the AST, and the curried function pointer let f = move |x: INT| fn_ptr.call_dynamic(context, None, [x.into()]); From 3b42cc5bb2306c2d24f78c522a63bc7501c92be7 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 23 Jan 2021 09:37:27 +0800 Subject: [PATCH 6/7] Fix bug where plugin module parameters are consumed. --- RELEASES.md | 1 + src/fn_call.rs | 35 +++++++++++++---------------------- tests/plugins.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index 2fec4b9e..904627bc 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -17,6 +17,7 @@ Breaking changes Bug fixes --------- +* Parameters passed to plugin module functions were sometimes erroneously consumed. This is now fixed. * Fixes compilation errors in `metadata` feature build. New features diff --git a/src/fn_call.rs b/src/fn_call.rs index 929eb825..7e277d77 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -1210,16 +1210,18 @@ impl Engine { r => r, }; + // Clone first argument if the function is not a method after-all + if let Some(first) = first_arg_value { + if !func.map(|f| f.is_method()).unwrap_or(true) { + let first_val = args[0].clone(); + args[0] = first; + *args[0] = first_val; + } + } + match func { #[cfg(not(feature = "no_function"))] Some(f) if f.is_script() => { - // Clone first argument - if let Some(first) = first_arg_value { - let first_val = args[0].clone(); - args[0] = first; - *args[0] = first_val; - } - let args = args.as_mut(); let new_scope = &mut Default::default(); let fn_def = f.get_fn_def().clone(); @@ -1241,21 +1243,10 @@ impl Engine { (self, fn_name, module.id(), &*mods, lib).into(), args.as_mut(), ), - Some(f) if f.is_native() => { - if !f.is_method() { - // Clone first argument - if let Some(first) = first_arg_value { - let first_val = args[0].clone(); - args[0] = first; - *args[0] = first_val; - } - } - - f.get_native_fn()( - (self, fn_name, module.id(), &*mods, lib).into(), - args.as_mut(), - ) - } + Some(f) if f.is_native() => f.get_native_fn()( + (self, fn_name, module.id(), &*mods, lib).into(), + args.as_mut(), + ), Some(f) => unreachable!("unknown function type: {:?}", f), None if def_val.is_some() => Ok(def_val.unwrap().clone()), None => EvalAltResult::ErrorFunctionNotFound( diff --git a/tests/plugins.rs b/tests/plugins.rs index 4ba79f6f..e42c4c73 100644 --- a/tests/plugins.rs +++ b/tests/plugins.rs @@ -101,3 +101,32 @@ fn test_plugins_package() -> Result<(), Box> { Ok(()) } + +#[test] +fn test_plugins_parameters() -> Result<(), Box> { + #[export_module] + mod rhai_std { + use rhai::*; + + pub fn noop(_: &str) {} + } + + let mut engine = Engine::new(); + + let std = exported_module!(rhai_std); + + engine.register_static_module("std", std.into()); + + assert_eq!( + engine.eval::( + r#" + let s = "hello"; + std::noop(s); + s + "# + )?, + "hello" + ); + + Ok(()) +} From 049f472ac91b5fd21d32c62c1339a1f3c3436106 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 23 Jan 2021 10:34:38 +0800 Subject: [PATCH 7/7] Fix bug in internals. --- src/fn_native.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/fn_native.rs b/src/fn_native.rs index 6372c3c7..7c69a768 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -120,6 +120,7 @@ impl<'e, 'n, 's, 'a, 'm, 'pm> NativeCallContext<'e, 'n, 's, 'a, 'm, 'pm> { ) -> Self { Self { engine, + fn_name, source: source.clone(), mods: Some(imports), lib: lib.as_ref(),