Change expect("exists") to unwrap().
This commit is contained in:
parent
b1b4361d08
commit
bc6bf6c6ba
@ -97,18 +97,16 @@ impl Engine {
|
|||||||
resolver: &StaticModuleResolver,
|
resolver: &StaticModuleResolver,
|
||||||
imports: &mut BTreeSet<crate::Identifier>,
|
imports: &mut BTreeSet<crate::Identifier>,
|
||||||
) {
|
) {
|
||||||
ast.walk(
|
ast.walk(&mut |path| match path.last().unwrap() {
|
||||||
&mut |path| match path.last().expect("contains current node") {
|
// Collect all `import` statements with a string constant path
|
||||||
// Collect all `import` statements with a string constant path
|
ASTNode::Stmt(Stmt::Import(Expr::StringConstant(s, _), _, _))
|
||||||
ASTNode::Stmt(Stmt::Import(Expr::StringConstant(s, _), _, _))
|
if !resolver.contains_path(s) && !imports.contains(s.as_str()) =>
|
||||||
if !resolver.contains_path(s) && !imports.contains(s.as_str()) =>
|
{
|
||||||
{
|
imports.insert(s.clone().into());
|
||||||
imports.insert(s.clone().into());
|
true
|
||||||
true
|
}
|
||||||
}
|
_ => true,
|
||||||
_ => true,
|
});
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut ast = self.compile_scripts_with_scope(scope, &[script])?;
|
let mut ast = self.compile_scripts_with_scope(scope, &[script])?;
|
||||||
|
@ -4,7 +4,6 @@ use crate::func::{FnCallArgs, RegisterNativeFunction, SendSync};
|
|||||||
use crate::types::dynamic::Variant;
|
use crate::types::dynamic::Variant;
|
||||||
use crate::{
|
use crate::{
|
||||||
Engine, FnAccess, FnNamespace, Identifier, Module, NativeCallContext, RhaiResultOf, Shared,
|
Engine, FnAccess, FnNamespace, Identifier, Module, NativeCallContext, RhaiResultOf, Shared,
|
||||||
SmartString,
|
|
||||||
};
|
};
|
||||||
use std::any::{type_name, TypeId};
|
use std::any::{type_name, TypeId};
|
||||||
#[cfg(feature = "no_std")]
|
#[cfg(feature = "no_std")]
|
||||||
@ -15,13 +14,14 @@ impl Engine {
|
|||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub(crate) fn global_namespace(&self) -> &Module {
|
pub(crate) fn global_namespace(&self) -> &Module {
|
||||||
self.global_modules.first().expect("not empty")
|
self.global_modules.first().unwrap()
|
||||||
}
|
}
|
||||||
/// Get a mutable reference to the global namespace module
|
/// Get a mutable reference to the global namespace module
|
||||||
/// (which is the first module in `global_modules`).
|
/// (which is the first module in `global_modules`).
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub(crate) fn global_namespace_mut(&mut self) -> &mut Module {
|
pub(crate) fn global_namespace_mut(&mut self) -> &mut Module {
|
||||||
Shared::get_mut(self.global_modules.first_mut().expect("not empty")).expect("not shared")
|
let module = self.global_modules.first_mut().unwrap();
|
||||||
|
Shared::get_mut(module).expect("not shared")
|
||||||
}
|
}
|
||||||
/// Register a custom function with the [`Engine`].
|
/// Register a custom function with the [`Engine`].
|
||||||
///
|
///
|
||||||
@ -69,18 +69,20 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
let param_type_names: Option<crate::StaticVec<_>> =
|
let param_type_names: crate::StaticVec<_> =
|
||||||
Some(param_type_names.iter().map(|ty| ty.as_str()).collect());
|
param_type_names.iter().map(|ty| ty.as_str()).collect();
|
||||||
|
#[cfg(feature = "metadata")]
|
||||||
|
let param_type_names = Some(param_type_names.as_ref());
|
||||||
|
|
||||||
#[cfg(not(feature = "metadata"))]
|
#[cfg(not(feature = "metadata"))]
|
||||||
let param_type_names: Option<[&str; 0]> = None;
|
let param_type_names: Option<&[&str]> = None;
|
||||||
|
|
||||||
self.global_namespace_mut().set_fn(
|
self.global_namespace_mut().set_fn(
|
||||||
name,
|
name,
|
||||||
FnNamespace::Global,
|
FnNamespace::Global,
|
||||||
FnAccess::Public,
|
FnAccess::Public,
|
||||||
param_type_names.as_ref().map(<_>::as_ref),
|
param_type_names,
|
||||||
¶m_types,
|
param_types,
|
||||||
func.into_callable_function(),
|
func.into_callable_function(),
|
||||||
);
|
);
|
||||||
self
|
self
|
||||||
@ -127,18 +129,20 @@ impl Engine {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
let param_type_names: Option<crate::StaticVec<_>> =
|
let param_type_names: crate::StaticVec<_> =
|
||||||
Some(param_type_names.iter().map(|ty| ty.as_str()).collect());
|
param_type_names.iter().map(|ty| ty.as_str()).collect();
|
||||||
|
#[cfg(feature = "metadata")]
|
||||||
|
let param_type_names = Some(param_type_names.as_ref());
|
||||||
|
|
||||||
#[cfg(not(feature = "metadata"))]
|
#[cfg(not(feature = "metadata"))]
|
||||||
let param_type_names: Option<[&str; 0]> = None;
|
let param_type_names: Option<&[&str]> = None;
|
||||||
|
|
||||||
self.global_namespace_mut().set_fn(
|
self.global_namespace_mut().set_fn(
|
||||||
name,
|
name,
|
||||||
FnNamespace::Global,
|
FnNamespace::Global,
|
||||||
FnAccess::Public,
|
FnAccess::Public,
|
||||||
param_type_names.as_ref().map(<_>::as_ref),
|
param_type_names,
|
||||||
¶m_types,
|
param_types,
|
||||||
func.into_callable_function(),
|
func.into_callable_function(),
|
||||||
);
|
);
|
||||||
self
|
self
|
||||||
@ -280,8 +284,8 @@ impl Engine {
|
|||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn register_type_with_name_raw(
|
pub fn register_type_with_name_raw(
|
||||||
&mut self,
|
&mut self,
|
||||||
fully_qualified_type_path: impl Into<SmartString>,
|
fully_qualified_type_path: impl Into<Identifier>,
|
||||||
name: impl Into<SmartString>,
|
name: impl Into<Identifier>,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
// Add the pretty-print type name into the map
|
// Add the pretty-print type name into the map
|
||||||
self.type_names
|
self.type_names
|
||||||
|
@ -509,18 +509,15 @@ impl Expr {
|
|||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
Self::Array(x, _) if self.is_constant() => {
|
Self::Array(x, _) if self.is_constant() => {
|
||||||
let mut arr = crate::Array::with_capacity(x.len());
|
let mut arr = crate::Array::with_capacity(x.len());
|
||||||
arr.extend(
|
arr.extend(x.iter().map(|v| v.get_literal_value().unwrap()));
|
||||||
x.iter()
|
|
||||||
.map(|v| v.get_literal_value().expect("constant value")),
|
|
||||||
);
|
|
||||||
Dynamic::from_array(arr)
|
Dynamic::from_array(arr)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
Self::Map(x, _) if self.is_constant() => {
|
Self::Map(x, _) if self.is_constant() => {
|
||||||
Dynamic::from_map(x.0.iter().fold(x.1.clone(), |mut map, (k, v)| {
|
Dynamic::from_map(x.0.iter().fold(x.1.clone(), |mut map, (k, v)| {
|
||||||
let value_ref = map.get_mut(k.name.as_str()).expect("contains all keys");
|
let value_ref = map.get_mut(k.name.as_str()).unwrap();
|
||||||
*value_ref = v.get_literal_value().expect("constant value");
|
*value_ref = v.get_literal_value().unwrap();
|
||||||
map
|
map
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
@ -827,7 +824,7 @@ impl Expr {
|
|||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
path.pop().expect("contains current node");
|
path.pop().unwrap();
|
||||||
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
@ -660,7 +660,7 @@ impl Stmt {
|
|||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
path.pop().expect("contains current node");
|
path.pop().unwrap();
|
||||||
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
@ -849,7 +849,7 @@ impl EvalState {
|
|||||||
// Push a new function resolution cache if the stack is empty
|
// Push a new function resolution cache if the stack is empty
|
||||||
self.push_fn_resolution_cache();
|
self.push_fn_resolution_cache();
|
||||||
}
|
}
|
||||||
self.fn_resolution_caches.last_mut().expect("not empty")
|
self.fn_resolution_caches.last_mut().unwrap()
|
||||||
}
|
}
|
||||||
/// Push an empty function resolution cache onto the stack and make it current.
|
/// Push an empty function resolution cache onto the stack and make it current.
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
@ -1200,11 +1200,11 @@ impl Engine {
|
|||||||
|
|
||||||
if let Some(index) = index {
|
if let Some(index) = index {
|
||||||
let offset = global.num_imported_modules() - index.get();
|
let offset = global.num_imported_modules() - index.get();
|
||||||
Some(global.get_shared_module(offset).expect("within range"))
|
Some(global.get_shared_module(offset).unwrap())
|
||||||
} else {
|
} else {
|
||||||
global
|
global
|
||||||
.find_module(root)
|
.find_module(root)
|
||||||
.map(|n| global.get_shared_module(n).expect("valid index"))
|
.map(|n| global.get_shared_module(n).unwrap())
|
||||||
.or_else(|| self.global_sub_modules.get(root).cloned())
|
.or_else(|| self.global_sub_modules.get(root).cloned())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1335,7 +1335,7 @@ impl Engine {
|
|||||||
level: 0,
|
level: 0,
|
||||||
};
|
};
|
||||||
match resolve_var(
|
match resolve_var(
|
||||||
expr.get_variable_name(true).expect("`Variable`"),
|
expr.get_variable_name(true).expect("`Expr::Variable`"),
|
||||||
index,
|
index,
|
||||||
&context,
|
&context,
|
||||||
) {
|
) {
|
||||||
@ -1352,7 +1352,7 @@ impl Engine {
|
|||||||
scope.len() - index
|
scope.len() - index
|
||||||
} else {
|
} else {
|
||||||
// Find the variable in the scope
|
// Find the variable in the scope
|
||||||
let var_name = expr.get_variable_name(true).expect("`Variable`");
|
let var_name = expr.get_variable_name(true).expect("`Expr::Variable`");
|
||||||
scope
|
scope
|
||||||
.get_index(var_name)
|
.get_index(var_name)
|
||||||
.ok_or_else(|| ERR::ErrorVariableNotFound(var_name.to_string(), var_pos))?
|
.ok_or_else(|| ERR::ErrorVariableNotFound(var_name.to_string(), var_pos))?
|
||||||
@ -1386,7 +1386,7 @@ impl Engine {
|
|||||||
let _terminate_chaining = terminate_chaining;
|
let _terminate_chaining = terminate_chaining;
|
||||||
|
|
||||||
// Pop the last index value
|
// Pop the last index value
|
||||||
let idx_val = idx_values.pop().expect("not empty");
|
let idx_val = idx_values.pop().unwrap();
|
||||||
|
|
||||||
match chain_type {
|
match chain_type {
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
@ -2389,13 +2389,16 @@ impl Engine {
|
|||||||
let mut map = Dynamic::from_map(x.1.clone());
|
let mut map = Dynamic::from_map(x.1.clone());
|
||||||
|
|
||||||
for (Ident { name, .. }, value_expr) in x.0.iter() {
|
for (Ident { name, .. }, value_expr) in x.0.iter() {
|
||||||
*map.write_lock::<crate::Map>()
|
let key = name.as_str();
|
||||||
.expect("`Map`")
|
let value = self
|
||||||
.get_mut(name.as_str())
|
|
||||||
.expect("exists") = self
|
|
||||||
.eval_expr(scope, global, state, lib, this_ptr, value_expr, level)?
|
.eval_expr(scope, global, state, lib, this_ptr, value_expr, level)?
|
||||||
.flatten();
|
.flatten();
|
||||||
|
|
||||||
|
*map.write_lock::<crate::Map>()
|
||||||
|
.expect("`Map`")
|
||||||
|
.get_mut(key)
|
||||||
|
.unwrap() = value;
|
||||||
|
|
||||||
self.check_data_size(&map, value_expr.position())?;
|
self.check_data_size(&map, value_expr.position())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2446,8 +2449,8 @@ impl Engine {
|
|||||||
|
|
||||||
Expr::Custom(custom, _) => {
|
Expr::Custom(custom, _) => {
|
||||||
let expressions: StaticVec<_> = custom.inputs.iter().map(Into::into).collect();
|
let expressions: StaticVec<_> = custom.inputs.iter().map(Into::into).collect();
|
||||||
let key_token = custom.tokens.first().expect("not empty");
|
let key_token = custom.tokens.first().unwrap();
|
||||||
let custom_def = self.custom_syntax.get(key_token).expect("must match");
|
let custom_def = self.custom_syntax.get(key_token).unwrap();
|
||||||
let mut context = EvalContext {
|
let mut context = EvalContext {
|
||||||
engine: self,
|
engine: self,
|
||||||
scope,
|
scope,
|
||||||
@ -2808,7 +2811,7 @@ impl Engine {
|
|||||||
let (mut lhs_ptr, pos) =
|
let (mut lhs_ptr, pos) =
|
||||||
self.search_namespace(scope, global, state, lib, this_ptr, lhs_expr)?;
|
self.search_namespace(scope, global, state, lib, this_ptr, lhs_expr)?;
|
||||||
|
|
||||||
let var_name = lhs_expr.get_variable_name(false).expect("`Variable`");
|
let var_name = lhs_expr.get_variable_name(false).expect("`Expr::Variable`");
|
||||||
|
|
||||||
if !lhs_ptr.is_ref() {
|
if !lhs_ptr.is_ref() {
|
||||||
return Err(ERR::ErrorAssignmentToConstant(var_name.to_string(), pos).into());
|
return Err(ERR::ErrorAssignmentToConstant(var_name.to_string(), pos).into());
|
||||||
@ -3212,11 +3215,11 @@ impl Engine {
|
|||||||
if err_pos.is_none() {
|
if err_pos.is_none() {
|
||||||
// No position info
|
// No position info
|
||||||
} else {
|
} else {
|
||||||
let line = err_pos.line().expect("line number") as INT;
|
let line = err_pos.line().unwrap() as INT;
|
||||||
let position = if err_pos.is_beginning_of_line() {
|
let position = if err_pos.is_beginning_of_line() {
|
||||||
0
|
0
|
||||||
} else {
|
} else {
|
||||||
err_pos.position().expect("character position")
|
err_pos.position().unwrap()
|
||||||
} as INT;
|
} as INT;
|
||||||
err_map.insert("line".into(), line.into());
|
err_map.insert("line".into(), line.into());
|
||||||
err_map.insert("position".into(), position.into());
|
err_map.insert("position".into(), position.into());
|
||||||
|
@ -274,8 +274,7 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
let (first_arg, rest_args) =
|
let (first_arg, rest_args) = args.split_first().unwrap();
|
||||||
args.split_first().expect("two arguments");
|
|
||||||
|
|
||||||
get_builtin_op_assignment_fn(fn_name, *first_arg, rest_args[0])
|
get_builtin_op_assignment_fn(fn_name, *first_arg, rest_args[0])
|
||||||
.map(|f| FnResolutionCacheEntry {
|
.map(|f| FnResolutionCacheEntry {
|
||||||
@ -624,7 +623,7 @@ impl Engine {
|
|||||||
|
|
||||||
let result = if _is_method_call {
|
let result = if _is_method_call {
|
||||||
// Method call of script function - map first argument to `this`
|
// Method call of script function - map first argument to `this`
|
||||||
let (first_arg, rest_args) = args.split_first_mut().expect("not empty");
|
let (first_arg, rest_args) = args.split_first_mut().unwrap();
|
||||||
|
|
||||||
let level = _level + 1;
|
let level = _level + 1;
|
||||||
|
|
||||||
@ -1114,7 +1113,7 @@ impl Engine {
|
|||||||
// avoid cloning the value
|
// avoid cloning the value
|
||||||
if curry.is_empty() && !a_expr.is_empty() && a_expr[0].is_variable_access(false) {
|
if curry.is_empty() && !a_expr.is_empty() && a_expr[0].is_variable_access(false) {
|
||||||
// func(x, ...) -> x.func(...)
|
// func(x, ...) -> x.func(...)
|
||||||
let (first_expr, rest_expr) = a_expr.split_first().expect("not empty");
|
let (first_expr, rest_expr) = a_expr.split_first().unwrap();
|
||||||
|
|
||||||
rest_expr.iter().try_for_each(|expr| {
|
rest_expr.iter().try_for_each(|expr| {
|
||||||
self.get_arg_value(scope, global, state, lib, this_ptr, level, expr, constants)
|
self.get_arg_value(scope, global, state, lib, this_ptr, level, expr, constants)
|
||||||
@ -1215,7 +1214,7 @@ impl Engine {
|
|||||||
args.extend(arg_values.iter_mut());
|
args.extend(arg_values.iter_mut());
|
||||||
} else {
|
} else {
|
||||||
// Turn it into a method call only if the object is not shared and not a simple value
|
// Turn it into a method call only if the object is not shared and not a simple value
|
||||||
let (first, rest) = arg_values.split_first_mut().expect("not empty");
|
let (first, rest) = arg_values.split_first_mut().unwrap();
|
||||||
first_arg_value = Some(first);
|
first_arg_value = Some(first);
|
||||||
let obj_ref = target.take_ref().expect("ref");
|
let obj_ref = target.take_ref().expect("ref");
|
||||||
args.push(obj_ref);
|
args.push(obj_ref);
|
||||||
|
@ -649,7 +649,7 @@ impl Module {
|
|||||||
|
|
||||||
if let Some(f) = self.functions.get_mut(&hash_fn) {
|
if let Some(f) = self.functions.get_mut(&hash_fn) {
|
||||||
let (param_names, return_type_name) = if param_names.len() > f.params {
|
let (param_names, return_type_name) = if param_names.len() > f.params {
|
||||||
let return_type = param_names.pop().expect("exists");
|
let return_type = param_names.pop().unwrap();
|
||||||
(param_names, return_type)
|
(param_names, return_type)
|
||||||
} else {
|
} else {
|
||||||
(param_names, Default::default())
|
(param_names, Default::default())
|
||||||
@ -697,7 +697,7 @@ impl Module {
|
|||||||
let comments = comments.as_ref();
|
let comments = comments.as_ref();
|
||||||
|
|
||||||
if !comments.is_empty() {
|
if !comments.is_empty() {
|
||||||
let f = self.functions.get_mut(&hash_fn).expect("exists");
|
let f = self.functions.get_mut(&hash_fn).unwrap();
|
||||||
f.comments = Some(comments.iter().map(|s| s.as_ref().into()).collect());
|
f.comments = Some(comments.iter().map(|s| s.as_ref().into()).collect());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -782,7 +782,7 @@ impl Module {
|
|||||||
.map(|&s| s.into())
|
.map(|&s| s.into())
|
||||||
.collect::<StaticVec<_>>();
|
.collect::<StaticVec<_>>();
|
||||||
let return_type = if names.len() > arg_types.as_ref().len() {
|
let return_type = if names.len() > arg_types.as_ref().len() {
|
||||||
names.pop().expect("exists")
|
names.pop().unwrap()
|
||||||
} else {
|
} else {
|
||||||
Default::default()
|
Default::default()
|
||||||
};
|
};
|
||||||
@ -860,7 +860,7 @@ impl Module {
|
|||||||
let comments = comments.as_ref();
|
let comments = comments.as_ref();
|
||||||
|
|
||||||
if !comments.is_empty() {
|
if !comments.is_empty() {
|
||||||
let f = self.functions.get_mut(&hash).expect("exists");
|
let f = self.functions.get_mut(&hash).unwrap();
|
||||||
f.comments = Some(comments.iter().map(|s| s.as_ref().into()).collect());
|
f.comments = Some(comments.iter().map(|s| s.as_ref().into()).collect());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1569,11 +1569,11 @@ impl Module {
|
|||||||
match aliases.len() {
|
match aliases.len() {
|
||||||
0 => (),
|
0 => (),
|
||||||
1 => {
|
1 => {
|
||||||
let alias = aliases.pop().expect("not empty");
|
let alias = aliases.pop().unwrap();
|
||||||
module.set_var(alias, value);
|
module.set_var(alias, value);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let last_alias = aliases.pop().expect("not empty");
|
let last_alias = aliases.pop().unwrap();
|
||||||
aliases.into_iter().for_each(|alias| {
|
aliases.into_iter().for_each(|alias| {
|
||||||
module.set_var(alias, value.clone());
|
module.set_var(alias, value.clone());
|
||||||
});
|
});
|
||||||
|
@ -326,14 +326,14 @@ fn optimize_stmt_block(
|
|||||||
&& !last_stmt.returns_value() =>
|
&& !last_stmt.returns_value() =>
|
||||||
{
|
{
|
||||||
state.set_dirty();
|
state.set_dirty();
|
||||||
statements.pop().expect(">= 2 elements");
|
statements.pop().unwrap();
|
||||||
}
|
}
|
||||||
// { ...; return val; } -> { ...; val }
|
// { ...; return val; } -> { ...; val }
|
||||||
[.., Stmt::Return(options, ref mut expr, pos)]
|
[.., Stmt::Return(options, ref mut expr, pos)]
|
||||||
if reduce_return && !options.contains(AST_OPTION_BREAK_OUT) =>
|
if reduce_return && !options.contains(AST_OPTION_BREAK_OUT) =>
|
||||||
{
|
{
|
||||||
state.set_dirty();
|
state.set_dirty();
|
||||||
*statements.last_mut().expect(">= 2 elements") = expr
|
*statements.last_mut().unwrap() = expr
|
||||||
.as_mut()
|
.as_mut()
|
||||||
.map_or_else(|| Stmt::Noop(pos), |e| Stmt::Expr(mem::take(e)));
|
.map_or_else(|| Stmt::Noop(pos), |e| Stmt::Expr(mem::take(e)));
|
||||||
}
|
}
|
||||||
@ -350,10 +350,9 @@ fn optimize_stmt_block(
|
|||||||
{
|
{
|
||||||
state.set_dirty();
|
state.set_dirty();
|
||||||
if second_last_stmt.returns_value() {
|
if second_last_stmt.returns_value() {
|
||||||
*statements.last_mut().expect(">= 2 elements") =
|
*statements.last_mut().unwrap() = Stmt::Noop(last_stmt.position());
|
||||||
Stmt::Noop(last_stmt.position());
|
|
||||||
} else {
|
} else {
|
||||||
statements.pop().expect(">= 2 elements");
|
statements.pop().unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => break,
|
_ => break,
|
||||||
@ -371,7 +370,7 @@ fn optimize_stmt_block(
|
|||||||
if reduce_return && !options.contains(AST_OPTION_BREAK_OUT) =>
|
if reduce_return && !options.contains(AST_OPTION_BREAK_OUT) =>
|
||||||
{
|
{
|
||||||
state.set_dirty();
|
state.set_dirty();
|
||||||
statements.pop().expect(">= 2 elements");
|
statements.pop().unwrap();
|
||||||
}
|
}
|
||||||
// { ...; return pure_val; } -> { ... }
|
// { ...; return pure_val; } -> { ... }
|
||||||
[.., Stmt::Return(options, Some(ref expr), _)]
|
[.., Stmt::Return(options, Some(ref expr), _)]
|
||||||
@ -380,11 +379,11 @@ fn optimize_stmt_block(
|
|||||||
&& expr.is_pure() =>
|
&& expr.is_pure() =>
|
||||||
{
|
{
|
||||||
state.set_dirty();
|
state.set_dirty();
|
||||||
statements.pop().expect(">= 2 elements");
|
statements.pop().unwrap();
|
||||||
}
|
}
|
||||||
[.., ref last_stmt] if is_pure(last_stmt) => {
|
[.., ref last_stmt] if is_pure(last_stmt) => {
|
||||||
state.set_dirty();
|
state.set_dirty();
|
||||||
statements.pop().expect("not empty");
|
statements.pop().unwrap();
|
||||||
}
|
}
|
||||||
_ => break,
|
_ => break,
|
||||||
}
|
}
|
||||||
@ -431,10 +430,8 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut OptimizerState, preserve_result: b
|
|||||||
let value = mem::take(&mut x2.args[1]);
|
let value = mem::take(&mut x2.args[1]);
|
||||||
|
|
||||||
if let Expr::Stack(slot, pos) = value {
|
if let Expr::Stack(slot, pos) = value {
|
||||||
x.2 = Expr::from_dynamic(
|
x.2 =
|
||||||
mem::take(x2.constants.get_mut(slot).expect("valid slot")),
|
Expr::from_dynamic(mem::take(x2.constants.get_mut(slot).unwrap()), pos);
|
||||||
pos,
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
x.2 = value;
|
x.2 = value;
|
||||||
}
|
}
|
||||||
@ -502,7 +499,7 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut OptimizerState, preserve_result: b
|
|||||||
|
|
||||||
// switch const { ... }
|
// switch const { ... }
|
||||||
Stmt::Switch(match_expr, x, pos) if match_expr.is_constant() => {
|
Stmt::Switch(match_expr, x, pos) if match_expr.is_constant() => {
|
||||||
let value = match_expr.get_literal_value().expect("constant");
|
let value = match_expr.get_literal_value().unwrap();
|
||||||
let hasher = &mut get_hasher();
|
let hasher = &mut get_hasher();
|
||||||
value.hash(hasher);
|
value.hash(hasher);
|
||||||
let hash = hasher.finish();
|
let hash = hasher.finish();
|
||||||
@ -870,13 +867,13 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, chaining: bool) {
|
|||||||
(Expr::StringConstant(s, pos), Expr::IntegerConstant(i, _)) if *i >= 0 && (*i as usize) < s.chars().count() => {
|
(Expr::StringConstant(s, pos), Expr::IntegerConstant(i, _)) if *i >= 0 && (*i as usize) < s.chars().count() => {
|
||||||
// String literal indexing - get the character
|
// String literal indexing - get the character
|
||||||
state.set_dirty();
|
state.set_dirty();
|
||||||
*expr = Expr::CharConstant(s.chars().nth(*i as usize).expect("valid index"), *pos);
|
*expr = Expr::CharConstant(s.chars().nth(*i as usize).unwrap(), *pos);
|
||||||
}
|
}
|
||||||
// string[-int]
|
// string[-int]
|
||||||
(Expr::StringConstant(s, pos), Expr::IntegerConstant(i, _)) if *i < 0 && i.checked_abs().map(|n| n as usize <= s.chars().count()).unwrap_or(false) => {
|
(Expr::StringConstant(s, pos), Expr::IntegerConstant(i, _)) if *i < 0 && i.checked_abs().map(|n| n as usize <= s.chars().count()).unwrap_or(false) => {
|
||||||
// String literal indexing - get the character
|
// String literal indexing - get the character
|
||||||
state.set_dirty();
|
state.set_dirty();
|
||||||
*expr = Expr::CharConstant(s.chars().rev().nth(i.abs() as usize - 1).expect("valid index"), *pos);
|
*expr = Expr::CharConstant(s.chars().rev().nth(i.abs() as usize - 1).unwrap(), *pos);
|
||||||
}
|
}
|
||||||
// var[rhs]
|
// var[rhs]
|
||||||
(Expr::Variable(_, _, _), rhs) => optimize_expr(rhs, state, true),
|
(Expr::Variable(_, _, _), rhs) => optimize_expr(rhs, state, true),
|
||||||
@ -920,7 +917,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, chaining: bool) {
|
|||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
Expr::Array(_, _) if expr.is_constant() => {
|
Expr::Array(_, _) if expr.is_constant() => {
|
||||||
state.set_dirty();
|
state.set_dirty();
|
||||||
*expr = Expr::DynamicConstant(expr.get_literal_value().expect("constant").into(), expr.position());
|
*expr = Expr::DynamicConstant(expr.get_literal_value().unwrap().into(), expr.position());
|
||||||
}
|
}
|
||||||
// [ items .. ]
|
// [ items .. ]
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
@ -929,7 +926,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, chaining: bool) {
|
|||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
Expr::Map(_, _) if expr.is_constant() => {
|
Expr::Map(_, _) if expr.is_constant() => {
|
||||||
state.set_dirty();
|
state.set_dirty();
|
||||||
*expr = Expr::DynamicConstant(expr.get_literal_value().expect("constant").into(), expr.position());
|
*expr = Expr::DynamicConstant(expr.get_literal_value().unwrap().into(), expr.position());
|
||||||
}
|
}
|
||||||
// #{ key:value, .. }
|
// #{ key:value, .. }
|
||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
@ -997,7 +994,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, chaining: bool) {
|
|||||||
=> {
|
=> {
|
||||||
let arg_values = &mut x.args.iter().map(|e| match e {
|
let arg_values = &mut x.args.iter().map(|e| match e {
|
||||||
Expr::Stack(slot, _) => x.constants[*slot].clone(),
|
Expr::Stack(slot, _) => x.constants[*slot].clone(),
|
||||||
_ => e.get_literal_value().expect("constant")
|
_ => e.get_literal_value().unwrap()
|
||||||
}).collect::<StaticVec<_>>();
|
}).collect::<StaticVec<_>>();
|
||||||
|
|
||||||
let arg_types: StaticVec<_> = arg_values.iter().map(Dynamic::type_id).collect();
|
let arg_types: StaticVec<_> = arg_values.iter().map(Dynamic::type_id).collect();
|
||||||
@ -1024,7 +1021,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, chaining: bool) {
|
|||||||
let lib = &[];
|
let lib = &[];
|
||||||
|
|
||||||
let context = (state.engine, x.name.as_str(), lib).into();
|
let context = (state.engine, x.name.as_str(), lib).into();
|
||||||
let (first, second) = arg_values.split_first_mut().expect("not empty");
|
let (first, second) = arg_values.split_first_mut().unwrap();
|
||||||
(f)(context, &mut [ first, &mut second[0] ]).ok()
|
(f)(context, &mut [ first, &mut second[0] ]).ok()
|
||||||
}) {
|
}) {
|
||||||
state.set_dirty();
|
state.set_dirty();
|
||||||
@ -1063,7 +1060,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, chaining: bool) {
|
|||||||
if !has_script_fn {
|
if !has_script_fn {
|
||||||
let arg_values = &mut x.args.iter().map(|e| match e {
|
let arg_values = &mut x.args.iter().map(|e| match e {
|
||||||
Expr::Stack(slot, _) => x.constants[*slot].clone(),
|
Expr::Stack(slot, _) => x.constants[*slot].clone(),
|
||||||
_ => e.get_literal_value().expect("constant")
|
_ => e.get_literal_value().unwrap()
|
||||||
}).collect::<StaticVec<_>>();
|
}).collect::<StaticVec<_>>();
|
||||||
|
|
||||||
let result = match x.name.as_str() {
|
let result = match x.name.as_str() {
|
||||||
@ -1098,7 +1095,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, chaining: bool) {
|
|||||||
// constant-name
|
// constant-name
|
||||||
Expr::Variable(_, pos, x) if x.1.is_none() && state.find_constant(&x.2).is_some() => {
|
Expr::Variable(_, pos, x) if x.1.is_none() && state.find_constant(&x.2).is_some() => {
|
||||||
// Replace constant with value
|
// Replace constant with value
|
||||||
*expr = Expr::from_dynamic(state.find_constant(&x.2).expect("exists").clone(), *pos);
|
*expr = Expr::from_dynamic(state.find_constant(&x.2).unwrap().clone(), *pos);
|
||||||
state.set_dirty();
|
state.set_dirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -178,7 +178,7 @@ mod string_functions {
|
|||||||
#[rhai_fn(name = "to_upper")]
|
#[rhai_fn(name = "to_upper")]
|
||||||
pub fn to_upper_char(character: char) -> char {
|
pub fn to_upper_char(character: char) -> char {
|
||||||
let mut stream = character.to_uppercase();
|
let mut stream = character.to_uppercase();
|
||||||
let ch = stream.next().expect("not empty");
|
let ch = stream.next().unwrap();
|
||||||
if stream.next().is_some() {
|
if stream.next().is_some() {
|
||||||
character
|
character
|
||||||
} else {
|
} else {
|
||||||
@ -192,7 +192,7 @@ mod string_functions {
|
|||||||
#[rhai_fn(name = "to_lower")]
|
#[rhai_fn(name = "to_lower")]
|
||||||
pub fn to_lower_char(character: char) -> char {
|
pub fn to_lower_char(character: char) -> char {
|
||||||
let mut stream = character.to_lowercase();
|
let mut stream = character.to_lowercase();
|
||||||
let ch = stream.next().expect("not empty");
|
let ch = stream.next().unwrap();
|
||||||
if stream.next().is_some() {
|
if stream.next().is_some() {
|
||||||
character
|
character
|
||||||
} else {
|
} else {
|
||||||
|
@ -1304,11 +1304,7 @@ fn parse_primary(
|
|||||||
Token::Custom(key) | Token::Reserved(key) | Token::Identifier(key)
|
Token::Custom(key) | Token::Reserved(key) | Token::Identifier(key)
|
||||||
if state.engine.custom_syntax.contains_key(&**key) =>
|
if state.engine.custom_syntax.contains_key(&**key) =>
|
||||||
{
|
{
|
||||||
let (key, syntax) = state
|
let (key, syntax) = state.engine.custom_syntax.get_key_value(&**key).unwrap();
|
||||||
.engine
|
|
||||||
.custom_syntax
|
|
||||||
.get_key_value(&**key)
|
|
||||||
.expect("exists");
|
|
||||||
let (_, pos) = input.next().expect(NEVER_ENDS);
|
let (_, pos) = input.next().expect(NEVER_ENDS);
|
||||||
let settings2 = settings.level_up();
|
let settings2 = settings.level_up();
|
||||||
parse_custom_syntax(input, state, lib, settings2, key, syntax, pos)?
|
parse_custom_syntax(input, state, lib, settings2, key, syntax, pos)?
|
||||||
@ -1714,11 +1710,7 @@ fn make_assignment_stmt(
|
|||||||
Expr::Variable(i, var_pos, ref x) => {
|
Expr::Variable(i, var_pos, ref x) => {
|
||||||
let (index, _, name) = x.as_ref();
|
let (index, _, name) = x.as_ref();
|
||||||
let index = i.map_or_else(
|
let index = i.map_or_else(
|
||||||
|| {
|
|| index.expect("either long or short index is `None`").get(),
|
||||||
index
|
|
||||||
.expect("the long index is `Some` when the short index is `None`")
|
|
||||||
.get()
|
|
||||||
},
|
|
||||||
|n| n.get() as usize,
|
|n| n.get() as usize,
|
||||||
);
|
);
|
||||||
match state.stack[state.stack.len() - index].1 {
|
match state.stack[state.stack.len() - index].1 {
|
||||||
@ -2013,8 +2005,8 @@ fn parse_binary_op(
|
|||||||
| Token::GreaterThanEqualsTo => FnCallExpr { args, ..op_base }.into_fn_call_expr(pos),
|
| Token::GreaterThanEqualsTo => FnCallExpr { args, ..op_base }.into_fn_call_expr(pos),
|
||||||
|
|
||||||
Token::Or => {
|
Token::Or => {
|
||||||
let rhs = args.pop().expect("two arguments");
|
let rhs = args.pop().unwrap();
|
||||||
let current_lhs = args.pop().expect("two arguments");
|
let current_lhs = args.pop().unwrap();
|
||||||
Expr::Or(
|
Expr::Or(
|
||||||
BinaryExpr {
|
BinaryExpr {
|
||||||
lhs: current_lhs.ensure_bool_expr()?,
|
lhs: current_lhs.ensure_bool_expr()?,
|
||||||
@ -2025,8 +2017,8 @@ fn parse_binary_op(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
Token::And => {
|
Token::And => {
|
||||||
let rhs = args.pop().expect("two arguments");
|
let rhs = args.pop().unwrap();
|
||||||
let current_lhs = args.pop().expect("two arguments");
|
let current_lhs = args.pop().unwrap();
|
||||||
Expr::And(
|
Expr::And(
|
||||||
BinaryExpr {
|
BinaryExpr {
|
||||||
lhs: current_lhs.ensure_bool_expr()?,
|
lhs: current_lhs.ensure_bool_expr()?,
|
||||||
@ -3026,7 +3018,7 @@ fn parse_try_catch(
|
|||||||
|
|
||||||
if err_var.is_some() {
|
if err_var.is_some() {
|
||||||
// Remove the error variable from the stack
|
// Remove the error variable from the stack
|
||||||
state.stack.pop().expect("not empty");
|
state.stack.pop().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Stmt::TryCatch(
|
Ok(Stmt::TryCatch(
|
||||||
|
@ -565,7 +565,7 @@ where
|
|||||||
) -> RhaiResultOf<V::Value> {
|
) -> RhaiResultOf<V::Value> {
|
||||||
// Deserialize each value item coming out of the iterator.
|
// Deserialize each value item coming out of the iterator.
|
||||||
seed.deserialize(&mut DynamicDeserializer::from_dynamic(
|
seed.deserialize(&mut DynamicDeserializer::from_dynamic(
|
||||||
self.values.next().expect("exists"),
|
self.values.next().unwrap(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1216,7 +1216,7 @@ pub fn parse_string_literal(
|
|||||||
|
|
||||||
#[cfg(not(feature = "no_position"))]
|
#[cfg(not(feature = "no_position"))]
|
||||||
{
|
{
|
||||||
let start_position = start.position().expect("start position");
|
let start_position = start.position().unwrap();
|
||||||
skip_whitespace_until = start_position + 1;
|
skip_whitespace_until = start_position + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1237,8 +1237,7 @@ pub fn parse_string_literal(
|
|||||||
|
|
||||||
// Whitespace to skip
|
// Whitespace to skip
|
||||||
#[cfg(not(feature = "no_position"))]
|
#[cfg(not(feature = "no_position"))]
|
||||||
_ if next_char.is_whitespace()
|
_ if next_char.is_whitespace() && pos.position().unwrap() < skip_whitespace_until => {}
|
||||||
&& pos.position().expect("position") < skip_whitespace_until => {}
|
|
||||||
|
|
||||||
// All other characters
|
// All other characters
|
||||||
_ => {
|
_ => {
|
||||||
@ -1632,7 +1631,7 @@ fn get_next_token_inner(
|
|||||||
|(err, err_pos)| (Token::LexError(err), err_pos),
|
|(err, err_pos)| (Token::LexError(err), err_pos),
|
||||||
|(result, _)| {
|
|(result, _)| {
|
||||||
let mut chars = result.chars();
|
let mut chars = result.chars();
|
||||||
let first = chars.next().expect("not empty");
|
let first = chars.next().unwrap();
|
||||||
|
|
||||||
if chars.next().is_some() {
|
if chars.next().is_some() {
|
||||||
(
|
(
|
||||||
|
@ -300,7 +300,7 @@ impl EvalAltResult {
|
|||||||
format!("{:?}", self)
|
format!("{:?}", self)
|
||||||
.split('(')
|
.split('(')
|
||||||
.next()
|
.next()
|
||||||
.expect("debug format of error is `ErrorXXX(...)`")
|
.expect("`ErrorXXX(...)`")
|
||||||
.into(),
|
.into(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ impl StringsInterner {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if dict.contains_key(text.as_ref()) {
|
if dict.contains_key(text.as_ref()) {
|
||||||
dict.get(text.as_ref()).expect("exists").clone()
|
dict.get(text.as_ref()).unwrap().clone()
|
||||||
} else {
|
} else {
|
||||||
let value: ImmutableString = mapper(text.as_ref()).into();
|
let value: ImmutableString = mapper(text.as_ref()).into();
|
||||||
dict.insert(text.as_ref().into(), value.clone());
|
dict.insert(text.as_ref().into(), value.clone());
|
||||||
|
@ -406,7 +406,7 @@ impl<'a> Scope<'a> {
|
|||||||
self.push(name, value);
|
self.push(name, value);
|
||||||
}
|
}
|
||||||
Some((index, AccessMode::ReadWrite)) => {
|
Some((index, AccessMode::ReadWrite)) => {
|
||||||
let value_ref = self.values.get_mut(index).expect("valid index");
|
let value_ref = self.values.get_mut(index).unwrap();
|
||||||
*value_ref = Dynamic::from(value);
|
*value_ref = Dynamic::from(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -446,7 +446,7 @@ impl<'a> Scope<'a> {
|
|||||||
}
|
}
|
||||||
Some((_, AccessMode::ReadOnly)) => panic!("variable {} is constant", name.as_ref()),
|
Some((_, AccessMode::ReadOnly)) => panic!("variable {} is constant", name.as_ref()),
|
||||||
Some((index, AccessMode::ReadWrite)) => {
|
Some((index, AccessMode::ReadWrite)) => {
|
||||||
let value_ref = self.values.get_mut(index).expect("valid index");
|
let value_ref = self.values.get_mut(index).unwrap();
|
||||||
*value_ref = Dynamic::from(value);
|
*value_ref = Dynamic::from(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -492,7 +492,7 @@ impl<'a> Scope<'a> {
|
|||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub(crate) fn get_mut_by_index(&mut self, index: usize) -> &mut Dynamic {
|
pub(crate) fn get_mut_by_index(&mut self, index: usize) -> &mut Dynamic {
|
||||||
self.values.get_mut(index).expect("valid index")
|
self.values.get_mut(index).unwrap()
|
||||||
}
|
}
|
||||||
/// Add an alias to an entry in the [`Scope`].
|
/// Add an alias to an entry in the [`Scope`].
|
||||||
///
|
///
|
||||||
@ -502,7 +502,7 @@ impl<'a> Scope<'a> {
|
|||||||
#[cfg(not(feature = "no_module"))]
|
#[cfg(not(feature = "no_module"))]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn add_entry_alias(&mut self, index: usize, alias: Identifier) -> &mut Self {
|
pub(crate) fn add_entry_alias(&mut self, index: usize, alias: Identifier) -> &mut Self {
|
||||||
let (_, aliases) = self.names.get_mut(index).expect("valid index");
|
let (_, aliases) = self.names.get_mut(index).unwrap();
|
||||||
match aliases {
|
match aliases {
|
||||||
None => {
|
None => {
|
||||||
let mut list = StaticVec::new_const();
|
let mut list = StaticVec::new_const();
|
||||||
|
Loading…
Reference in New Issue
Block a user