Add no_object feature to disable objects.

This commit is contained in:
Stephen Chung
2020-03-29 17:15:12 +08:00
parent a8a4ed2967
commit ef6c6ea6d2
16 changed files with 71 additions and 15 deletions

View File

@@ -75,6 +75,7 @@ impl<'e> Engine<'e> {
/// # Ok(())
/// # }
/// ```
#[cfg(not(feature = "no_object"))]
pub fn register_type<T: Any + Clone>(&mut self) {
self.register_type_with_name::<T>(type_name::<T>());
}
@@ -122,6 +123,7 @@ impl<'e> Engine<'e> {
/// # Ok(())
/// # }
/// ```
#[cfg(not(feature = "no_object"))]
pub fn register_type_with_name<T: Any + Clone>(&mut self, name: &str) {
// Add the pretty-print type name into the map
self.type_names
@@ -173,6 +175,7 @@ impl<'e> Engine<'e> {
/// # Ok(())
/// # }
/// ```
#[cfg(not(feature = "no_object"))]
pub fn register_get<T: Any + Clone, U: Any + Clone>(
&mut self,
name: &str,
@@ -215,6 +218,7 @@ impl<'e> Engine<'e> {
/// # Ok(())
/// # }
/// ```
#[cfg(not(feature = "no_object"))]
pub fn register_set<T: Any + Clone, U: Any + Clone>(
&mut self,
name: &str,
@@ -262,6 +266,7 @@ impl<'e> Engine<'e> {
/// # Ok(())
/// # }
/// ```
#[cfg(not(feature = "no_object"))]
pub fn register_get_set<T: Any + Clone, U: Any + Clone>(
&mut self,
name: &str,
@@ -864,7 +869,7 @@ impl<'e> Engine<'e> {
/// let mut engine = Engine::new();
///
/// // Set 'retain_functions' in 'consume' to keep the function definitions
/// engine.consume(true, "fn add(x, y) { x.len() + y }")?;
/// engine.consume(true, "fn add(x, y) { len(x) + y }")?;
///
/// // Call the script-defined function
/// let result: i64 = engine.call_fn("add", (String::from("abc"), 123_i64))?;

View File

@@ -344,6 +344,7 @@ impl Engine<'_> {
///
/// Otherwise, if `src` is `Some`, then it holds a name and index into `scope`; using `get_mut` on
/// `scope` can retrieve a mutable reference to the variable's value to use as `this`.
#[cfg(not(feature = "no_object"))]
fn get_dot_val_helper(
&mut self,
scope: &mut Scope,
@@ -487,6 +488,7 @@ impl Engine<'_> {
}
/// Evaluate a dot chain getter
#[cfg(not(feature = "no_object"))]
fn get_dot_val(
&mut self,
scope: &mut Scope,
@@ -741,6 +743,7 @@ impl Engine<'_> {
}
/// Chain-evaluate a dot setter
#[cfg(not(feature = "no_object"))]
fn set_dot_val_helper(
&mut self,
scope: &mut Scope,
@@ -858,6 +861,7 @@ impl Engine<'_> {
}
// Evaluate a dot chain setter
#[cfg(not(feature = "no_object"))]
fn set_dot_val(
&mut self,
scope: &mut Scope,
@@ -1021,6 +1025,7 @@ impl Engine<'_> {
}
// dot_lhs.dot_rhs = rhs
#[cfg(not(feature = "no_object"))]
Expr::Dot(dot_lhs, dot_rhs, _) => self.set_dot_val(
scope,
dot_lhs,
@@ -1041,6 +1046,7 @@ impl Engine<'_> {
}
}
#[cfg(not(feature = "no_object"))]
Expr::Dot(lhs, rhs, _) => self.get_dot_val(scope, lhs, rhs, level),
#[cfg(not(feature = "no_index"))]

View File

@@ -334,6 +334,7 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr {
expr => Expr::Assignment(id, Box::new(optimize_expr(expr, state)), pos),
},
// lhs.rhs
#[cfg(not(feature = "no_object"))]
Expr::Dot(lhs, rhs, pos) => Expr::Dot(
Box::new(optimize_expr(*lhs, state)),
Box::new(optimize_expr(*rhs, state)),

View File

@@ -357,6 +357,7 @@ pub enum Expr {
/// expr = expr
Assignment(Box<Expr>, Box<Expr>, Position),
/// lhs.rhs
#[cfg(not(feature = "no_object"))]
Dot(Box<Expr>, Box<Expr>, Position),
/// expr[expr]
#[cfg(not(feature = "no_index"))]
@@ -443,10 +444,12 @@ impl Expr {
| Expr::False(pos)
| Expr::Unit(pos) => *pos,
Expr::Assignment(expr, _, _)
| Expr::Dot(expr, _, _)
| Expr::And(expr, _)
| Expr::Or(expr, _) => expr.position(),
Expr::Assignment(expr, _, _) | Expr::And(expr, _) | Expr::Or(expr, _) => {
expr.position()
}
#[cfg(not(feature = "no_object"))]
Expr::Dot(expr, _, _) => expr.position(),
#[cfg(not(feature = "no_float"))]
Expr::FloatConstant(_, pos) => *pos,
@@ -1767,6 +1770,7 @@ fn parse_assignment(lhs: Expr, rhs: Expr, pos: Position) -> Result<Expr, ParseEr
},
// dot_lhs.dot_rhs
#[cfg(not(feature = "no_object"))]
Expr::Dot(dot_lhs, dot_rhs, _) => match dot_lhs.as_ref() {
// var.dot_rhs
Expr::Variable(_, _) if is_top => valid_assignment_chain(dot_rhs, false),
@@ -1875,6 +1879,7 @@ fn parse_binary_op<'a>(
Token::PlusAssign => parse_op_assignment("+", current_lhs, rhs, pos)?,
Token::MinusAssign => parse_op_assignment("-", current_lhs, rhs, pos)?,
#[cfg(not(feature = "no_object"))]
Token::Period => {
fn change_var_to_property(expr: Expr) -> Expr {
match expr {