1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! Module defining functions for evaluating an expression.
use super::{Caches, EvalContext, GlobalRuntimeState, Target};
use crate::ast::Expr;
use crate::packages::string_basic::{print_with_func, FUNC_TO_STRING};
use crate::types::dynamic::AccessMode;
use crate::{Dynamic, Engine, RhaiResult, RhaiResultOf, Scope, SmartString, ERR};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{convert::TryInto, fmt::Write, num::NonZeroUsize};
impl Engine {
/// Search for a module within an imports stack.
#[cfg(not(feature = "no_module"))]
#[inline]
#[must_use]
pub(crate) fn search_imports(
&self,
global: &GlobalRuntimeState,
namespace: &crate::ast::Namespace,
) -> Option<crate::SharedModule> {
debug_assert!(!namespace.is_empty());
let root = namespace.root();
// Qualified - check if the root module is directly indexed
if !global.always_search_scope {
if let Some(index) = namespace.index {
let offset = global.num_imports() - index.get();
if let m @ Some(_) = global.get_shared_import(offset) {
return m;
}
}
}
// Do a text-match search if the index doesn't work
global.find_import(root).map_or_else(
|| self.global_sub_modules.get(root).cloned(),
|offset| global.get_shared_import(offset),
)
}
/// Search for a variable within the scope
///
/// # Panics
///
/// Panics if `expr` is not [`Expr::Variable`].
pub(crate) fn search_scope_only<'s>(
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
scope: &'s mut Scope,
this_ptr: Option<&'s mut Dynamic>,
expr: &Expr,
) -> RhaiResultOf<Target<'s>> {
// Make sure that the pointer indirection is taken only when absolutely necessary.
// A bare script-function name is a function pointer, not a variable read.
//
// Checked ahead of `always_search_scope` rather than inside the match:
// that flag means "do not trust the parse-time variable indices", and a
// name resolving to a function has no index to distrust. Gating this on
// it made the name stop resolving for the rest of a run as soon as
// anything set it — an `eval` that changes the scope, a variable
// resolver that does, or a program still holding an AST fragment — so
// `fn f(x) { x } eval("let m = 1;"); [1].map(f)` reported `f` as an
// unknown variable while the same script without the `eval` worked.
//
// Still only for a variable with no cached index, which is what it was
// before: an index means the parser resolved the name to a real
// variable, and that variable keeps winning over a function sharing its
// name.
#[cfg(not(feature = "no_function"))]
if let Expr::Variable(v, None, ..) = expr {
if let Some(func) = global
.lib
.iter()
.flat_map(|m| m.iter_fn())
.filter(|(f, _)| f.is_script())
.filter(|(_, m)| m.name == v.1.as_str())
.map(|(f, _)| f)
.next()
{
// Embedded environment for scripted function
let env = func
.get_shared_encapsulated_environ()
.cloned()
.unwrap_or_else(|| {
// Create a new environment with the current module
crate::Shared::new((&*global).into())
});
let fn_def = func.get_script_fn_def().unwrap();
let val: Dynamic = crate::FnPtr {
name: v.1.clone(),
curry: <_>::default(),
env: Some(env),
typ: crate::types::fn_ptr::FnPtrType::Script {
num_params: fn_def.params.len(),
hash: crate::calc_fn_hash(None, &fn_def.name, fn_def.params.len()),
},
}
.into();
return Ok(val.into());
}
}
let index = match expr {
// Check if the variable is `this`
Expr::ThisPtr(..) => unreachable!("Expr::ThisPtr should have been handled outside"),
_ if global.always_search_scope => 0,
Expr::Variable(_, Some(i), ..) => i.get() as usize,
Expr::Variable(v, None, ..) => {
#[cfg(not(feature = "no_module"))]
debug_assert!(v.2.is_empty(), "variable should not be namespace-qualified");
v.0.map_or(0, NonZeroUsize::get)
}
_ => unreachable!("Expr::Variable expected but gets {:?}", expr),
};
// Check the variable resolver, if any
if let Some(ref resolve_var) = self.resolve_var {
let orig_scope_len = scope.len();
let context = EvalContext::new(self, global, caches, scope, this_ptr);
let var_name = expr.get_variable_name(true).unwrap();
let resolved_var = resolve_var(var_name, index, context);
if orig_scope_len != scope.len() {
// The scope is changed, always search from now on
global.always_search_scope = true;
}
match resolved_var {
Ok(Some(mut result)) => {
result.set_access_mode(AccessMode::ReadOnly);
return Ok(result.into());
}
Ok(None) => (),
Err(err) => return Err(err.fill_position(expr.position())),
}
}
let index = if index > 0 {
scope.len() - index
} else {
// Find the variable in the scope
let var_name = expr.get_variable_name(true).unwrap();
match scope.search(var_name) {
Some(index) => index,
None => {
return self
.global_modules
.iter()
.find_map(|m| m.get_var(var_name))
.map_or_else(
|| {
Err(ERR::ErrorVariableNotFound(
var_name.to_string(),
expr.position(),
)
.into())
},
|val| Ok(val.into()),
)
}
}
};
let val = scope.get_mut_by_index(index);
val.try_into()
}
/// Search for a variable within the scope or within imports,
/// depending on whether the variable name is namespace-qualified.
pub(crate) fn search_namespace<'s>(
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
scope: &'s mut Scope,
this_ptr: Option<&'s mut Dynamic>,
expr: &Expr,
) -> RhaiResultOf<Target<'s>> {
match expr {
Expr::Variable(_, Some(_), _) => {
self.search_scope_only(global, caches, scope, this_ptr, expr)
}
Expr::Variable(v, None, ..) => match &**v {
// Qualified variable access
#[cfg(not(feature = "no_module"))]
(_, var_name, ns, hash_var) if !ns.is_empty() => {
// foo:bar::baz::VARIABLE
if let Some(module) = self.search_imports(global, ns) {
return module.get_qualified_var(*hash_var).map_or_else(
|| {
let sep = crate::engine::NAMESPACE_SEPARATOR;
Err(ERR::ErrorVariableNotFound(
format!("{ns}{sep}{var_name}"),
ns.position(),
)
.into())
},
|mut target| {
// Module variables are constant
target.set_access_mode(AccessMode::ReadOnly);
Ok(target.into())
},
);
}
// global::VARIABLE
#[cfg(not(feature = "no_function"))]
if ns.path.len() == 1 && ns.root() == crate::engine::KEYWORD_GLOBAL {
if let Some(ref constants) = global.constants {
if let Some(value) = crate::func::locked_write(constants)
.unwrap()
.get_mut(var_name.as_str())
{
let mut target: Target = value.clone().into();
// Module variables are constant
target.as_mut().set_access_mode(AccessMode::ReadOnly);
return Ok(target);
}
}
let sep = crate::engine::NAMESPACE_SEPARATOR;
return Err(ERR::ErrorVariableNotFound(
format!("{ns}{sep}{var_name}"),
ns.position(),
)
.into());
}
Err(ERR::ErrorModuleNotFound(ns.to_string(), ns.position()).into())
}
// Normal variable access
_ => self.search_scope_only(global, caches, scope, this_ptr, expr),
},
_ => unreachable!("Expr::Variable expected but gets {:?}", expr),
}
}
/// Evaluate an expression.
pub(crate) fn eval_expr(
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
scope: &mut Scope,
mut this_ptr: Option<&mut Dynamic>,
expr: &Expr,
) -> RhaiResult {
self.track_operation(global, expr.position())?;
#[cfg(feature = "debugging")]
let reset = self.dbg_reset(global, caches, scope, this_ptr.as_deref_mut(), expr)?;
#[cfg(feature = "debugging")]
defer! { global if Some(reset) => move |g| g.debugger_mut().reset_status(reset) }
// Constants
let is_simple_constant = match expr {
Expr::DynamicConstant(..)
| Expr::IntegerConstant(..)
| Expr::CharConstant(..)
| Expr::StringConstant(..)
| Expr::BoolConstant(..)
| Expr::Unit(..) => true,
#[cfg(not(feature = "no_float"))]
Expr::FloatConstant(..) => true,
_ => false,
};
if is_simple_constant {
if let Some(value) = expr.get_literal_value(Some(global)) {
return Ok(value);
}
}
// Other expressions
match expr {
Expr::FnCall(x, pos) => {
self.eval_fn_call_expr(global, caches, scope, this_ptr, x, *pos)
}
Expr::ThisPtr(var_pos) => this_ptr
.ok_or_else(|| ERR::ErrorUnboundThis(*var_pos).into())
.cloned(),
Expr::Variable(..) => self
.search_namespace(global, caches, scope, this_ptr, expr)
.map(Target::take_or_clone),
Expr::InterpolatedString(x, _) => {
let mut concat = SmartString::new_const();
for expr in &**x {
let item = &mut self
.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), expr)?
.flatten();
let pos = expr.position();
if item.is_string() {
write!(concat, "{item}").unwrap();
} else {
let source = global.source();
let context = &(self, FUNC_TO_STRING, source, &*global, pos).into();
let display = print_with_func(FUNC_TO_STRING, context, item);
write!(concat, "{display}").unwrap();
}
#[cfg(not(feature = "unchecked"))]
self.throw_on_size((0, 0, concat.len()))
.map_err(|err| err.fill_position(pos))?;
}
Ok(self.get_interned_string(concat).into())
}
#[cfg(not(feature = "no_index"))]
Expr::Array(x, ..) => {
let mut array = crate::Array::with_capacity(x.len());
#[cfg(not(feature = "unchecked"))]
let mut total_data_sizes = (0, 0, 0);
for item_expr in &**x {
let value = self
.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), item_expr)?
.flatten();
#[cfg(not(feature = "unchecked"))]
if self.has_data_size_limit() {
let val_sizes = crate::eval::calc_data_sizes(&value, true);
total_data_sizes = (
total_data_sizes.0 + val_sizes.0 + 1,
total_data_sizes.1 + val_sizes.1,
total_data_sizes.2 + val_sizes.2,
);
self.throw_on_size(total_data_sizes)
.map_err(|err| err.fill_position(item_expr.position()))?;
}
array.push(value);
}
Ok(Dynamic::from_array(array))
}
#[cfg(not(feature = "no_object"))]
Expr::Map(x, ..) => {
let mut map = x.1.clone();
#[cfg(not(feature = "unchecked"))]
let mut total_data_sizes = (0, 0, 0);
for (key, value_expr) in &x.0 {
let value = self
.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), value_expr)?
.flatten();
#[cfg(not(feature = "unchecked"))]
if self.has_data_size_limit() {
let delta = crate::eval::calc_data_sizes(&value, true);
total_data_sizes = (
total_data_sizes.0 + delta.0,
total_data_sizes.1 + delta.1 + 1,
total_data_sizes.2 + delta.2,
);
self.throw_on_size(total_data_sizes)
.map_err(|err| err.fill_position(value_expr.position()))?;
}
*map.get_mut(key.as_str()).unwrap() = value;
}
Ok(Dynamic::from_map(map))
}
Expr::And(x, ..) => {
let mut value = Dynamic::TRUE;
for expr in &***x {
if !self
.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), expr)?
.as_bool()
.map_err(|typ| self.make_type_mismatch_err::<bool>(typ, expr.position()))?
{
value = Dynamic::FALSE;
break;
}
}
Ok(value)
}
Expr::Or(x, ..) => {
let mut value = Dynamic::FALSE;
for expr in &***x {
if self
.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), expr)?
.as_bool()
.map_err(|typ| self.make_type_mismatch_err::<bool>(typ, expr.position()))?
{
value = Dynamic::TRUE;
break;
}
}
Ok(value)
}
Expr::Coalesce(x, ..) => {
let mut value = Dynamic::UNIT;
for expr in &***x {
value = self.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), expr)?;
if !value.is_unit() {
break;
}
}
Ok(value)
}
#[cfg(not(feature = "no_custom_syntax"))]
Expr::Custom(custom, pos) => {
let expressions: crate::StaticVec<_> =
custom.inputs.iter().map(Into::into).collect();
// The first token acts as the custom syntax's key
let key_token = custom.tokens.first().unwrap();
// The key should exist, unless the AST is compiled in a different Engine
let custom_def = self.custom_syntax.get(key_token.as_str()).ok_or_else(|| {
Box::new(ERR::ErrorCustomSyntax(
format!("Invalid custom syntax prefix: {key_token}"),
custom.tokens.iter().map(<_>::to_string).collect(),
*pos,
))
})?;
let mut context = EvalContext::new(self, global, caches, scope, this_ptr);
(custom_def.func)(&mut context, &expressions, &custom.state)
.and_then(|r| self.check_data_size(r, expr.start_position()))
}
Expr::Stmt(x) => {
self.eval_stmt_block(global, caches, scope, this_ptr, x.statements(), true)
}
#[cfg(not(feature = "no_index"))]
Expr::Index(..) => {
self.eval_dot_index_chain(global, caches, scope, this_ptr, expr, None)
}
#[cfg(not(feature = "no_object"))]
Expr::Dot(..) => self.eval_dot_index_chain(global, caches, scope, this_ptr, expr, None),
#[allow(unreachable_patterns)]
_ => unreachable!("expression cannot be evaluated: {:?}", expr),
}
}
}