|
|
@ -40,6 +40,14 @@ pub struct Interpreter { |
|
|
|
pub scopes: Vec<Scope>, |
|
|
|
pub scopes: Vec<Scope>, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Interpreter { |
|
|
|
|
|
|
|
#[inline(always)] |
|
|
|
|
|
|
|
/// Get the current scope
|
|
|
|
|
|
|
|
pub fn scope(&self) -> Scope { |
|
|
|
|
|
|
|
*self.scopes.get(self.scopes.len() - 1).unwrap() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl Executor for Interpreter { |
|
|
|
impl Executor for Interpreter { |
|
|
|
fn new() -> Interpreter { |
|
|
|
fn new() -> Interpreter { |
|
|
|
let global = ValueData::new_obj(None); |
|
|
|
let global = ValueData::new_obj(None); |
|
|
@ -323,7 +331,7 @@ impl Executor for Interpreter { |
|
|
|
this.borrow() |
|
|
|
this.borrow() |
|
|
|
.set_field_slice(INSTANCE_PROTOTYPE, func.borrow().get_field_slice(PROTOTYPE)); |
|
|
|
.set_field_slice(INSTANCE_PROTOTYPE, func.borrow().get_field_slice(PROTOTYPE)); |
|
|
|
match *func { |
|
|
|
match *func { |
|
|
|
ValueData::Function(ref func) => match *func { |
|
|
|
ValueData::Function(ref func) => match func.clone().into_inner() { |
|
|
|
Function::NativeFunc(ref ntv) => { |
|
|
|
Function::NativeFunc(ref ntv) => { |
|
|
|
let func = ntv.data; |
|
|
|
let func = ntv.data; |
|
|
|
func(this, try!(self.run(callee)), v_args) |
|
|
|
func(this, try!(self.run(callee)), v_args) |
|
|
@ -331,9 +339,9 @@ impl Executor for Interpreter { |
|
|
|
Function::RegularFunc(ref data) => { |
|
|
|
Function::RegularFunc(ref data) => { |
|
|
|
let scope = self.make_scope(this); |
|
|
|
let scope = self.make_scope(this); |
|
|
|
let scope_vars_ptr = scope.vars.borrow(); |
|
|
|
let scope_vars_ptr = scope.vars.borrow(); |
|
|
|
for i in range(0, data.args.len()) { |
|
|
|
for i in 0..data.args.len() { |
|
|
|
let name = data.args.get(i); |
|
|
|
let name = data.args.get(i).unwrap(); |
|
|
|
let expr = v_args.get(i); |
|
|
|
let expr = v_args.get(i).unwrap(); |
|
|
|
scope_vars_ptr.set_field(name.clone(), *expr); |
|
|
|
scope_vars_ptr.set_field(name.clone(), *expr); |
|
|
|
} |
|
|
|
} |
|
|
|
let result = self.run(&data.expr); |
|
|
|
let result = self.run(&data.expr); |
|
|
@ -348,15 +356,15 @@ impl Executor for Interpreter { |
|
|
|
Some(ref v) => self.run(v), |
|
|
|
Some(ref v) => self.run(v), |
|
|
|
None => Ok(Gc::new(ValueData::Undefined)), |
|
|
|
None => Ok(Gc::new(ValueData::Undefined)), |
|
|
|
}, |
|
|
|
}, |
|
|
|
ExprDef::ThrowExpr(ref ex) => Err(try!(self.run(*ex))), |
|
|
|
ExprDef::ThrowExpr(ref ex) => Err(try!(self.run(ex))), |
|
|
|
ExprDef::AssignExpr(ref ref_e, ref val_e) => { |
|
|
|
ExprDef::AssignExpr(ref ref_e, ref val_e) => { |
|
|
|
let val = try!(self.run(val_e)); |
|
|
|
let val = try!(self.run(val_e)); |
|
|
|
match ref_e.def { |
|
|
|
match ref_e.def { |
|
|
|
LocalExpr(ref name) => { |
|
|
|
ExprDef::LocalExpr(ref name) => { |
|
|
|
self.scope().vars.borrow().set_field(name.clone(), val); |
|
|
|
self.scope().vars.borrow().set_field(name.clone(), val); |
|
|
|
} |
|
|
|
} |
|
|
|
GetConstFieldExpr(ref obj, ref field) => { |
|
|
|
ExprDef::GetConstFieldExpr(ref obj, ref field) => { |
|
|
|
let val_obj = try!(self.run(*obj)); |
|
|
|
let val_obj = try!(self.run(obj)); |
|
|
|
val_obj.borrow().set_field(field.clone(), val); |
|
|
|
val_obj.borrow().set_field(field.clone(), val); |
|
|
|
} |
|
|
|
} |
|
|
|
_ => (), |
|
|
|
_ => (), |
|
|
@ -377,13 +385,13 @@ impl Executor for Interpreter { |
|
|
|
Ok(Gc::new(ValueData::Undefined)) |
|
|
|
Ok(Gc::new(ValueData::Undefined)) |
|
|
|
} |
|
|
|
} |
|
|
|
ExprDef::TypeOfExpr(ref val_e) => { |
|
|
|
ExprDef::TypeOfExpr(ref val_e) => { |
|
|
|
let val = try!(self.run(*val_e)); |
|
|
|
let val = try!(self.run(val_e)); |
|
|
|
Ok(to_value(match *val.borrow() { |
|
|
|
Ok(to_value(match *val { |
|
|
|
ValueData::Undefined => "undefined", |
|
|
|
ValueData::Undefined => "undefined", |
|
|
|
ValueData::Null | VObject(_) => "object", |
|
|
|
ValueData::Null | ValueData::Object(_) => "object", |
|
|
|
VBoolean(_) => "boolean", |
|
|
|
ValueData::Boolean(_) => "boolean", |
|
|
|
VNumber(_) | VInteger(_) => "number", |
|
|
|
ValueData::Number(_) | ValueData::Integer(_) => "number", |
|
|
|
VString(_) => "string", |
|
|
|
ValueData::String(_) => "string", |
|
|
|
ValueData::Function(_) => "function", |
|
|
|
ValueData::Function(_) => "function", |
|
|
|
})) |
|
|
|
})) |
|
|
|
} |
|
|
|
} |
|
|
|