Add no_inidex feature to disable arrays and indexing.

This commit is contained in:
Stephen Chung
2020-03-10 17:10:33 +08:00
parent f3bcb2a10d
commit 52b5732bcb
10 changed files with 225 additions and 145 deletions

View File

@@ -14,6 +14,7 @@ use std::{
};
/// An dynamic array of `Dynamic` values.
#[cfg(not(feature = "no_index"))]
pub type Array = Vec<Dynamic>;
pub type FnCallArgs<'a> = Vec<&'a mut Variant>;
@@ -29,6 +30,7 @@ pub(crate) const FUNC_GETTER: &'static str = "get$";
pub(crate) const FUNC_SETTER: &'static str = "set$";
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[cfg(not(feature = "no_index"))]
enum IndexSourceType {
Array,
String,
@@ -82,8 +84,9 @@ impl Engine<'_> {
// User-friendly names for built-in types
let type_names = [
(type_name::<String>(), "string"),
(type_name::<Array>(), "array"),
(type_name::<Dynamic>(), "dynamic"),
#[cfg(not(feature = "no_index"))]
(type_name::<Array>(), "array"),
]
.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
@@ -251,7 +254,7 @@ impl Engine<'_> {
match dot_rhs {
// xxx.fn_name(args)
Expr::FunctionCall(fn_name, args, def_val, pos) => {
let mut args: Array = args
let mut args = args
.iter()
.map(|arg| self.eval_expr(scope, arg))
.collect::<Result<Vec<_>, _>>()?;
@@ -271,6 +274,7 @@ impl Engine<'_> {
}
// xxx.idx_lhs[idx_expr]
#[cfg(not(feature = "no_index"))]
Expr::Index(idx_lhs, idx_expr, idx_pos) => {
let (expr, _) = match idx_lhs.as_ref() {
// xxx.id[idx_expr]
@@ -309,6 +313,7 @@ impl Engine<'_> {
.and_then(|mut v| self.get_dot_val_helper(scope, v.as_mut(), rhs))
}
// xxx.idx_lhs[idx_expr].rhs
#[cfg(not(feature = "no_index"))]
Expr::Index(idx_lhs, idx_expr, idx_pos) => {
let (expr, _) = match idx_lhs.as_ref() {
// xxx.id[idx_expr].rhs
@@ -371,6 +376,7 @@ impl Engine<'_> {
}
// idx_lhs[idx_expr].???
#[cfg(not(feature = "no_index"))]
Expr::Index(idx_lhs, idx_expr, idx_pos) => {
let (src_type, src, idx, mut target) =
self.eval_index_expr(scope, idx_lhs, idx_expr, *idx_pos)?;
@@ -414,6 +420,7 @@ impl Engine<'_> {
}
/// Evaluate the value of an index (must evaluate to i64)
#[cfg(not(feature = "no_index"))]
fn eval_index_value(
&mut self,
scope: &mut Scope,
@@ -426,6 +433,7 @@ impl Engine<'_> {
}
/// Get the value at the indexed position of a base type
#[cfg(not(feature = "no_index"))]
fn get_indexed_value(
&self,
val: Dynamic,
@@ -473,6 +481,7 @@ impl Engine<'_> {
}
/// Evaluate an index expression
#[cfg(not(feature = "no_index"))]
fn eval_index_expr<'a>(
&mut self,
scope: &mut Scope,
@@ -505,6 +514,7 @@ impl Engine<'_> {
}
/// Replace a character at an index position in a mutable string
#[cfg(not(feature = "no_index"))]
fn str_replace_char(s: &mut String, idx: usize, new_ch: char) {
let mut chars: Vec<char> = s.chars().collect();
let ch = *chars.get(idx).expect("string index out of bounds");
@@ -518,6 +528,7 @@ impl Engine<'_> {
}
/// Update the value at an index position in a variable inside the scope
#[cfg(not(feature = "no_index"))]
fn update_indexed_var_in_scope(
src_type: IndexSourceType,
scope: &mut Scope,
@@ -550,6 +561,7 @@ impl Engine<'_> {
}
/// Update the value at an index position
#[cfg(not(feature = "no_index"))]
fn update_indexed_value(
mut target: Dynamic,
idx: usize,
@@ -593,6 +605,7 @@ impl Engine<'_> {
// xxx.lhs[idx_expr]
// TODO - Allow chaining of indexing!
#[cfg(not(feature = "no_index"))]
Expr::Index(lhs, idx_expr, idx_pos) => match lhs.as_ref() {
// xxx.id[idx_expr]
Expr::Identifier(id, pos) => {
@@ -636,6 +649,7 @@ impl Engine<'_> {
// xxx.lhs[idx_expr].rhs
// TODO - Allow chaining of indexing!
#[cfg(not(feature = "no_index"))]
Expr::Index(lhs, idx_expr, idx_pos) => match lhs.as_ref() {
// xxx.id[idx_expr].rhs
Expr::Identifier(id, pos) => {
@@ -720,6 +734,7 @@ impl Engine<'_> {
// lhs[idx_expr].???
// TODO - Allow chaining of indexing!
#[cfg(not(feature = "no_index"))]
Expr::Index(lhs, idx_expr, idx_pos) => {
let (src_type, src, idx, mut target) =
self.eval_index_expr(scope, lhs, idx_expr, *idx_pos)?;
@@ -762,6 +777,7 @@ impl Engine<'_> {
}
// lhs[idx_expr]
#[cfg(not(feature = "no_index"))]
Expr::Index(lhs, idx_expr, idx_pos) => self
.eval_index_expr(scope, lhs, idx_expr, *idx_pos)
.map(|(_, _, _, x)| x),
@@ -785,6 +801,7 @@ impl Engine<'_> {
}
// idx_lhs[idx_expr] = rhs
#[cfg(not(feature = "no_index"))]
Expr::Index(idx_lhs, idx_expr, idx_pos) => {
let (src_type, src, idx, _) =
self.eval_index_expr(scope, idx_lhs, idx_expr, *idx_pos)?;
@@ -818,6 +835,7 @@ impl Engine<'_> {
Expr::Dot(lhs, rhs, _) => self.get_dot_val(scope, lhs, rhs),
#[cfg(not(feature = "no_index"))]
Expr::Array(contents, _) => {
let mut arr = Vec::new();
@@ -836,7 +854,7 @@ impl Engine<'_> {
let mut args = args
.iter()
.map(|expr| self.eval_expr(scope, expr))
.collect::<Result<Array, _>>()?;
.collect::<Result<Vec<Dynamic>, _>>()?;
self.call_fn_raw(
fn_name,