Browse Source

reduced errors

pull/5/head
Jason Williams 6 years ago
parent
commit
acf33efcc9
  1. 36
      src/lib/exec.rs
  2. 21
      src/lib/js/error.rs

36
src/lib/exec.rs

@ -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",
})) }))
} }

21
src/lib/js/error.rs

@ -1,13 +1,14 @@
use gc::Gc;
use js::function::Function; use js::function::Function;
use js::object::PROTOTYPE; use js::object::PROTOTYPE;
use js::value::{to_value, ResultValue, Value}; use js::value::{to_value, ResultValue, Value, ValueData};
/// Create a new error /// Create a new error
pub fn make_error(args: Vec<Value>, _: Value, _: Value, this: Value) -> ResultValue { pub fn make_error(this: Value, _: Value, args: Vec<Value>) -> ResultValue {
if args.len() >= 1 { if args.len() >= 1 {
this.set_field_slice("message", to_value(args.get(0).unwrap().to_string())); this.set_field_slice("message", to_value(args.get(0).unwrap().to_string()));
} }
Ok(Value::undefined()) Ok(Gc::new(ValueData::Undefined))
} }
/// Get the string representation of the error /// Get the string representation of the error
pub fn to_string(_: Vec<Value>, _: Value, _: Value, this: Value) -> ResultValue { pub fn to_string(_: Vec<Value>, _: Value, _: Value, this: Value) -> ResultValue {
@ -17,12 +18,14 @@ pub fn to_string(_: Vec<Value>, _: Value, _: Value, this: Value) -> ResultValue
} }
/// Create a new `Error` object /// Create a new `Error` object
pub fn _create(global: Value) -> Value { pub fn _create(global: Value) -> Value {
let prototype = Value::new_obj(Some(global)); let prototype = ValueData::new_obj(Some(global));
prototype.set_field_slice("message", to_value("")); let prototype_ptr = prototype;
prototype.set_field_slice("name", to_value("Error")); prototype_ptr.set_field_slice("message", to_value(""));
prototype.set_field_slice("toString", Function::make(to_string, &[])); prototype_ptr.set_field_slice("name", to_value("Error"));
let error = Function::make(make_error, &["message"]); prototype_ptr.set_field_slice("toString", to_value(to_string));
error.set_field_slice(PROTOTYPE, prototype); let error = to_value(make_error);
let error_ptr = error;
error_ptr.set_field_slice(PROTOTYPE, prototype);
error error
} }
/// Initialise the global object with the `Error` object /// Initialise the global object with the `Error` object

Loading…
Cancel
Save