Skip to content

Commit

Permalink
Merge pull request #472 from schungx/v1.1-fixes
Browse files Browse the repository at this point in the history
Bug fixes.
  • Loading branch information
schungx committed Nov 1, 2021
2 parents 3c40c30 + 5ae584f commit bbb9038
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 85 deletions.
28 changes: 14 additions & 14 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches:
- master
- bug-fixes
- v1.1-fixes
pull_request: {}

jobs:
Expand All @@ -19,22 +19,22 @@ jobs:
flags:
- ""
- "--features metadata,serde,internals"
- "--features unchecked"
- "--features sync"
- "--features no_position"
- "--features no_optimize"
- "--features no_float"
- "--features unchecked,serde,metadata,internals"
- "--features sync,serde,metadata,internals"
- "--features no_position,serde,metadata,internals"
- "--features no_optimize,serde,metadata,internals"
- "--features no_float,serde,metadata,internals"
- "--features f32_float,serde,metadata,internals"
- "--features decimal"
- "--features decimal,serde,metadata,internals"
- "--features no_float,decimal"
- "--tests --features only_i32,serde,metadata,internals"
- "--features only_i64"
- "--features no_index"
- "--features no_object"
- "--features no_function"
- "--features no_module"
- "--features no_closure"
- "--features unicode-xid-ident"
- "--features only_i64,serde,metadata,internals"
- "--features no_index,serde,metadata,internals"
- "--features no_object,serde,metadata,internals"
- "--features no_function,serde,metadata,internals"
- "--features no_module,serde,metadata,internals"
- "--features no_closure,serde,metadata,internals"
- "--features unicode-xid-ident,serde,metadata,internals"
- "--features sync,no_function,no_float,no_position,no_optimize,no_module,no_closure,metadata,serde,unchecked"
- "--features no_function,no_float,no_position,no_index,no_object,no_optimize,no_module,no_closure,unchecked"
toolchain: [stable]
Expand Down
15 changes: 11 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
Rhai Release Notes
==================

Version 1.1.1
=============

Bug fixes
---------

* Assignment to indexing expression with dot expressions inside no longer cause a compilation error.
* The `no_module` and `internals` features now work together without a compilation error.
* String literal operations (such as `"hello" + ", world"`) now optimizes correctly.


Version 1.1.0
=============

Expand Down Expand Up @@ -60,10 +71,6 @@ Enhancements
* `StaticVec` is changed to keep three items inline instead of four.


Version 1.0.7
=============


Version 1.0.6
=============

Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = [".", "codegen"]

[package]
name = "rhai"
version = "1.1.0"
version = "1.1.1"
edition = "2018"
authors = ["Jonathan Turner", "Lukáš Hozda", "Stephen Chung", "jhwgh1968"]
description = "Embedded scripting for Rust"
Expand All @@ -20,7 +20,7 @@ smallvec = { version = "1.6", default-features = false, features = ["union"] }
ahash = { version = "0.7", default-features = false }
num-traits = { version = "0.2", default-features = false }
smartstring = { version = "0.2.7", default-features = false }
rhai_codegen = { version = "1.0", path = "codegen", default-features = false }
rhai_codegen = { version = "1.1", path = "codegen", default-features = false }

[features]
default = ["ahash/std", "num-traits/std"]
Expand Down
18 changes: 8 additions & 10 deletions codegen/src/rhai_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub fn generate_body(
continue;
}
let fn_token_name = syn::Ident::new(
&format!("{}_token", function.name().to_string()),
&format!("{}_token", function.name()),
function.name().span(),
);
let reg_names = function.exported_names();
Expand Down Expand Up @@ -251,37 +251,35 @@ pub fn check_rename_collisions(fns: &[ExportedFn]) -> Result<(), syn::Error> {
if let Some(other_span) = renames.insert(key, current_span) {
let mut err = syn::Error::new(
current_span,
format!("duplicate Rhai signature for '{}'", &fn_name),
format!("duplicate Rhai signature for '{}'", fn_name),
);
err.combine(syn::Error::new(
other_span,
format!("duplicated function renamed '{}'", &fn_name),
format!("duplicated function renamed '{}'", fn_name),
));
return Err(err);
}
}
} else {
let ident = item_fn.name();
if let Some(other_span) = fn_defs.insert(ident.to_string(), ident.span()) {
let mut err = syn::Error::new(
ident.span(),
format!("duplicate function '{}'", ident.to_string()),
);
let mut err =
syn::Error::new(ident.span(), format!("duplicate function '{}'", ident));
err.combine(syn::Error::new(
other_span,
format!("duplicated function '{}'", ident.to_string()),
format!("duplicated function '{}'", ident),
));
return Err(err);
}
let key = make_key(ident, item_fn);
if let Some(fn_span) = renames.get(&key) {
let mut err = syn::Error::new(
ident.span(),
format!("duplicate Rhai signature for '{}'", &ident),
format!("duplicate Rhai signature for '{}'", ident),
);
err.combine(syn::Error::new(
*fn_span,
format!("duplicated function '{}'", &ident),
format!("duplicated function '{}'", ident),
));
return Err(err);
}
Expand Down
4 changes: 2 additions & 2 deletions codegen/ui_tests/rhai_mod_unknown_type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ help: a struct with a similar name exists
| ~~~~~
help: consider importing one of these items
|
11 | use core::fmt::Pointer;
|
11 | use std::fmt::Pointer;
|
11 | use syn::__private::fmt::Pointer;
|
11 | use core::fmt::Pointer;
|
4 changes: 3 additions & 1 deletion examples/serde.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#[cfg(not(feature = "serde"))]
#[cfg(any(not(feature = "serde"), feature = "no_object"))]
fn main() {
println!("This example requires the 'serde' feature to run.");
println!("Try: cargo run --features serde --example serde");
}

#[cfg(feature = "serde")]
#[cfg(not(feature = "no_object"))]
fn main() {
example::ser();
println!();
example::de();
}

#[cfg(feature = "serde")]
#[cfg(not(feature = "no_object"))]
mod example {
use rhai::serde::{from_dynamic, to_dynamic};
use rhai::{Dynamic, Engine, Map};
Expand Down
3 changes: 1 addition & 2 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,8 +717,7 @@ impl AST {
///
/// Not available under `no_function`.
#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "no_module"))]
#[inline(always)]
#[inline]
pub(crate) fn iter_fn_def(&self) -> impl Iterator<Item = &ScriptFnDef> {
self.functions
.iter_script_fn()
Expand Down
18 changes: 18 additions & 0 deletions src/fn_builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,24 @@ pub fn get_builtin_binary_op_fn(
_ => None,
};
}
// () op string
if types_pair == (TypeId::of::<()>(), TypeId::of::<ImmutableString>()) {
return match op {
"+" => Some(|_, args| Ok(args[1].clone())),
"==" | ">" | ">=" | "<" | "<=" => Some(|_, _| Ok(Dynamic::FALSE)),
"!=" => Some(|_, _| Ok(Dynamic::TRUE)),
_ => None,
};
}
// string op ()
if types_pair == (TypeId::of::<ImmutableString>(), TypeId::of::<()>()) {
return match op {
"+" => Some(|_, args| Ok(args[0].clone())),
"==" | ">" | ">=" | "<" | "<=" => Some(|_, _| Ok(Dynamic::FALSE)),
"!=" => Some(|_, _| Ok(Dynamic::TRUE)),
_ => None,
};
}

// map op string
#[cfg(not(feature = "no_object"))]
Expand Down
2 changes: 1 addition & 1 deletion src/fn_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ impl Engine {
return Ok((Dynamic::UNIT, false));
}

let scope: &mut Scope = &mut Default::default();
let scope = &mut Scope::new();

// Move captured variables into scope
#[cfg(not(feature = "no_closure"))]
Expand Down
42 changes: 23 additions & 19 deletions src/optimize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,7 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut OptimizerState, preserve_result: b

// switch const { ... }
Stmt::Switch(match_expr, x, pos) if match_expr.is_constant() => {
let value = match_expr
.get_literal_value()
.expect("`match_expr` is constant");
let value = match_expr.get_literal_value().expect("constant");
let hasher = &mut get_hasher();
value.hash(hasher);
let hash = hasher.finish();
Expand Down Expand Up @@ -878,7 +876,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, chaining: bool) {
#[cfg(not(feature = "no_index"))]
Expr::Array(_, _) if expr.is_constant() => {
state.set_dirty();
*expr = Expr::DynamicConstant(expr.get_literal_value().expect("`expr` is constant").into(), expr.position());
*expr = Expr::DynamicConstant(expr.get_literal_value().expect("constant").into(), expr.position());
}
// [ items .. ]
#[cfg(not(feature = "no_index"))]
Expand All @@ -887,7 +885,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, chaining: bool) {
#[cfg(not(feature = "no_object"))]
Expr::Map(_, _) if expr.is_constant() => {
state.set_dirty();
*expr = Expr::DynamicConstant(expr.get_literal_value().expect("`expr` is constant").into(), expr.position());
*expr = Expr::DynamicConstant(expr.get_literal_value().expect("constant").into(), expr.position());
}
// #{ key:value, .. }
#[cfg(not(feature = "no_object"))]
Expand Down Expand Up @@ -981,31 +979,37 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, chaining: bool) {
=> {
let arg_values = &mut x.args.iter().map(|e| match e {
Expr::Stack(slot, _) => x.constants[*slot].clone(),
_ => e.get_literal_value().expect("`e` is constant")
_ => e.get_literal_value().expect("constant")
}).collect::<StaticVec<_>>();

let arg_types: StaticVec<_> = arg_values.iter().map(Dynamic::type_id).collect();

let result = match x.name.as_str() {
KEYWORD_TYPE_OF if arg_values.len() == 1 => Some(state.engine.map_type_name(arg_values[0].type_name()).into()),
match x.name.as_str() {
KEYWORD_TYPE_OF if arg_values.len() == 1 => {
state.set_dirty();
*expr = Expr::from_dynamic(state.engine.map_type_name(arg_values[0].type_name()).into(), *pos);
return;
}
#[cfg(not(feature = "no_closure"))]
KEYWORD_IS_SHARED if arg_values.len() == 1 => Some(Dynamic::FALSE),
KEYWORD_IS_SHARED if arg_values.len() == 1 => {
state.set_dirty();
*expr = Expr::from_dynamic(Dynamic::FALSE, *pos);
return;
}
// Overloaded operators can override built-in.
_ if x.args.len() == 2 && !state.has_native_fn(x.hashes.native, arg_types.as_ref()) => {
get_builtin_binary_op_fn(x.name.as_ref(), &arg_values[0], &arg_values[1])
if let Some(result) = get_builtin_binary_op_fn(x.name.as_ref(), &arg_values[0], &arg_values[1])
.and_then(|f| {
let ctx = (state.engine, x.name.as_ref(), state.lib).into();
let (first, second) = arg_values.split_first_mut().expect("`arg_values` is not empty");
(f)(ctx, &mut [ first, &mut second[0] ]).ok()
})
}) {
state.set_dirty();
*expr = Expr::from_dynamic(result, *pos);
return;
}
}
_ => None
};

if let Some(result) = result {
state.set_dirty();
*expr = Expr::from_dynamic(result, *pos);
return;
_ => ()
}

x.args.iter_mut().for_each(|a| optimize_expr(a, state, false));
Expand Down Expand Up @@ -1035,7 +1039,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, chaining: bool) {
if !has_script_fn {
let arg_values = &mut x.args.iter().map(|e| match e {
Expr::Stack(slot, _) => x.constants[*slot].clone(),
_ => e.get_literal_value().expect("`e` is constant")
_ => e.get_literal_value().expect("constant")
}).collect::<StaticVec<_>>();

let result = match x.name.as_str() {
Expand Down
11 changes: 3 additions & 8 deletions src/packages/string_more.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,19 @@ mod string_functions {
s
}

#[rhai_fn(name = "+", name = "append")]
#[rhai_fn(name = "append")]
pub fn add_append_str(string1: ImmutableString, string2: ImmutableString) -> ImmutableString {
string1 + string2
}
#[rhai_fn(name = "+", name = "append")]
#[rhai_fn(name = "append")]
pub fn add_append_char(string: ImmutableString, character: char) -> ImmutableString {
string + character
}

#[rhai_fn(name = "+", name = "append")]
#[rhai_fn(name = "append")]
pub fn add_append_unit(string: ImmutableString, item: ()) -> ImmutableString {
let _item = item;
string
}
#[rhai_fn(name = "+")]
pub fn add_prepend_unit(_item: (), string: ImmutableString) -> ImmutableString {
string
}

#[rhai_fn(name = "len", get = "len")]
pub fn len(string: &str) -> INT {
Expand Down

0 comments on commit bbb9038

Please sign in to comment.