Browse Source

errors fixed

pull/5/head
Jason Williams 6 years ago
parent
commit
f0096c97c4
  1. 33
      src/lib/exec.rs
  2. 2
      src/lib/js/math.rs
  3. 2
      src/lib/js/object.rs
  4. 2
      src/lib/js/string.rs
  5. 18
      src/lib/js/value.rs
  6. 10
      src/lib/syntax/parser.rs

33
src/lib/exec.rs

@ -139,12 +139,15 @@ impl Executor for Interpreter {
let (this, func) = match callee.def {
ExprDef::GetConstFieldExpr(ref obj, ref field) => {
let obj = try!(self.run(obj));
(obj, obj.borrow().get_field(field.clone()))
(obj.clone(), obj.borrow().get_field(field.clone()))
}
ExprDef::GetFieldExpr(ref obj, ref field) => {
let obj = try!(self.run(obj));
let field = try!(self.run(field));
(obj, obj.borrow().get_field(field.borrow().to_string()))
(
obj.clone(),
obj.borrow().get_field(field.borrow().to_string()),
)
}
_ => (self.global.clone(), try!(self.run(&callee.clone()))),
};
@ -164,7 +167,7 @@ impl Executor for Interpreter {
for i in 0..data.args.len() {
let name = data.args.get(i).unwrap();
let expr = v_args.get(i).unwrap();
scope_vars_ptr.set_field(name.clone(), *expr);
scope_vars_ptr.set_field(name.clone(), expr.clone());
}
let result = self.run(&data.expr);
self.destroy_scope();
@ -220,14 +223,14 @@ impl Executor for Interpreter {
Ok(result)
}
ExprDef::ObjectDeclExpr(ref map) => {
let obj = ValueData::new_obj(Some(self.global));
let obj = ValueData::new_obj(Some(self.global.clone()));
for (key, val) in map.iter() {
obj.borrow().set_field(key.clone(), try!(self.run(val)));
}
Ok(obj)
}
ExprDef::ArrayDeclExpr(ref arr) => {
let arr_map = ValueData::new_obj(Some(self.global));
let arr_map = ValueData::new_obj(Some(self.global.clone()));
let mut index: i32 = 0;
for val in arr.iter() {
let val = try!(self.run(val));
@ -248,7 +251,9 @@ impl Executor for Interpreter {
Function::RegularFunc(RegularFunction::new(*expr.clone(), args.clone()));
let val = Gc::new(ValueData::Function(GcCell::new(function)));
if name.is_some() {
self.global.borrow().set_field(name.clone().unwrap(), val);
self.global
.borrow()
.set_field(name.clone().unwrap(), val.clone());
}
Ok(val)
}
@ -260,8 +265,8 @@ impl Executor for Interpreter {
ExprDef::BinOpExpr(BinOp::Num(ref op), ref a, ref b) => {
let v_r_a = try!(self.run(a));
let v_r_b = try!(self.run(b));
let v_a = *v_r_a;
let v_b = *v_r_b;
let v_a = (*v_r_a).clone();
let v_b = (*v_r_b).clone();
Ok(Gc::new(match *op {
NumOp::Add => v_a + v_b,
NumOp::Sub => v_a - v_b,
@ -272,7 +277,7 @@ impl Executor for Interpreter {
}
ExprDef::UnaryOpExpr(ref op, ref a) => {
let v_r_a = try!(self.run(a));
let v_a = *v_r_a;
let v_a = (*v_r_a).clone();
Ok(match *op {
UnaryOp::Minus => to_value(-v_a.to_num()),
UnaryOp::Plus => to_value(v_a.to_num()),
@ -283,8 +288,8 @@ impl Executor for Interpreter {
ExprDef::BinOpExpr(BinOp::Bit(ref op), ref a, ref b) => {
let v_r_a = try!(self.run(a));
let v_r_b = try!(self.run(b));
let v_a = *v_r_a;
let v_b = *v_r_b;
let v_a = (*v_r_a).clone();
let v_b = (*v_r_b).clone();
Ok(Gc::new(match *op {
BitOp::And => v_a & v_b,
BitOp::Or => v_a | v_b,
@ -342,7 +347,7 @@ impl Executor for Interpreter {
for i in 0..data.args.len() {
let name = data.args.get(i).unwrap();
let expr = v_args.get(i).unwrap();
scope_vars_ptr.set_field(name.clone(), *expr);
scope_vars_ptr.set_field(name.clone(), (*expr).clone());
}
let result = self.run(&data.expr);
self.destroy_scope();
@ -368,14 +373,14 @@ impl Executor for Interpreter {
}
ExprDef::GetConstFieldExpr(ref obj, ref field) => {
let val_obj = try!(self.run(obj));
val_obj.borrow().set_field(field.clone(), val);
val_obj.borrow().set_field(field.clone(), val.clone());
}
_ => (),
}
Ok(val)
}
ExprDef::VarDeclExpr(ref vars) => {
let scope_vars = self.scope().vars;
let scope_vars = self.scope().vars.clone();
let scope_vars_ptr = scope_vars.borrow();
for var in vars.iter() {
let (name, value) = var.clone();

2
src/lib/js/math.rs

@ -142,7 +142,7 @@ pub fn pow(_: Value, _: Value, args: Vec<Value>) -> ResultValue {
}))
}
/// Generate a random floating-point number between 0 and 1
pub fn _random(_: Value, _: Value, args: Vec<Value>) -> ResultValue {
pub fn _random(_: Value, _: Value, _args: Vec<Value>) -> ResultValue {
Ok(to_value(random::<f64>()))
}
/// Round a number to the nearest integer

2
src/lib/js/object.rs

@ -67,7 +67,7 @@ impl FromValue for Property {
}
/// Create a new object
pub fn make_object(_: Value, _: Value, args: Vec<Value>) -> ResultValue {
pub fn make_object(_: Value, _: Value, _args: Vec<Value>) -> ResultValue {
Ok(Gc::new(ValueData::Undefined))
}

2
src/lib/js/string.rs

@ -31,5 +31,5 @@ pub fn _create(global: Value) -> Value {
}
/// Initialise the `String` object on the global object
pub fn init(global: Value) {
global.set_field_slice("String", _create(global));
global.set_field_slice("String", _create(global.clone()));
}

18
src/lib/js/value.rs

@ -21,7 +21,7 @@ pub type ResultValue = Result<Value, Value>;
pub type Value = Gc<ValueData>;
/// A Javascript value
#[derive(Trace, Finalize, Debug)]
#[derive(Trace, Finalize, Debug, Clone)]
pub enum ValueData {
/// `null` - A null value, for when a value doesn't exist
Null,
@ -214,7 +214,7 @@ impl ValueData {
pub fn set_prop(&self, field: String, prop: Property) -> Property {
match *self {
ValueData::Object(ref obj) => {
obj.borrow_mut().insert(field.clone(), prop);
obj.borrow_mut().insert(field.clone(), prop.clone());
}
ValueData::Function(ref func) => {
match *func.borrow_mut().deref_mut() {
@ -334,7 +334,7 @@ impl Display for ValueData {
ValueData::Integer(v) => write!(f, "{}", v),
ValueData::Function(ref v) => match *v.borrow() {
Function::NativeFunc(_) => write!(f, "{}", "function() { [native code] }"),
Function::RegularFunc(rf) => {
Function::RegularFunc(ref rf) => {
write!(f, "function({}){}", rf.args.join(", "), rf.expr)
}
},
@ -344,7 +344,7 @@ impl Display for ValueData {
impl PartialEq for ValueData {
fn eq(&self, other: &ValueData) -> bool {
match (self.clone().deref(), other.clone().deref()) {
match (self.clone(), other.clone()) {
// TODO: fix this
// _ if self.ptr.to_inner() == &other.ptr.to_inner() => true,
_ if self.is_null_or_undefined() && other.is_null_or_undefined() => true,
@ -357,8 +357,8 @@ impl PartialEq for ValueData {
{
true
}
(ValueData::Number(a), _) if *a == other.to_num() => true,
(_, ValueData::Number(a)) if *a == self.to_num() => true,
(ValueData::Number(a), _) if a == other.to_num() => true,
(_, ValueData::Number(a)) if a == self.to_num() => true,
(ValueData::Integer(a), ValueData::Integer(b)) if a == b => true,
_ => false,
}
@ -368,8 +368,8 @@ impl PartialEq for ValueData {
impl Add for ValueData {
type Output = ValueData;
fn add(self, other: ValueData) -> ValueData {
return match (self, other) {
(ValueData::String(s), other) | (other, ValueData::String(s)) => {
return match (self.clone(), other.clone()) {
(ValueData::String(ref s), ref other) | (ref other, ValueData::String(ref s)) => {
ValueData::String(s.clone() + &other.to_string())
}
(_, _) => ValueData::Number(self.to_num() + other.to_num()),
@ -555,7 +555,7 @@ impl ToValue for ObjectData {
impl FromValue for ObjectData {
fn from_value(v: Value) -> Result<ObjectData, &'static str> {
match *v {
ValueData::Object(ref obj) => Ok(obj.clone().borrow().deref().clone()),
ValueData::Object(ref obj) => Ok(obj.clone().into_inner()),
ValueData::Function(ref func) => Ok(match *func.borrow().deref() {
Function::NativeFunc(ref data) => data.object.clone(),
Function::RegularFunc(ref data) => data.object.clone(),

10
src/lib/syntax/parser.rs

@ -58,11 +58,7 @@ impl Parser {
// In the case of `BlockExpr` the Positions seem unnecessary
// TODO: refactor this or the `mk!` perhaps?
Ok(
Expr::new(
ExprDef::BlockExpr(exprs)
)
)
Ok(Expr::new(ExprDef::BlockExpr(exprs)))
}
fn get_token(&self, pos: usize) -> Result<Token, ParseError> {
@ -339,7 +335,7 @@ impl Parser {
// at this point it's probably gonna be an arrow function
let mut args = vec![
match next.def {
ExprDef::LocalExpr(name) => name,
ExprDef::LocalExpr(ref name) => (*name).clone(),
_ => "".to_string(),
},
match try!(self.get_token(self.pos)).data {
@ -624,7 +620,7 @@ impl Parser {
self.pos += 1;
let mut args = Vec::with_capacity(1);
match result.def {
ExprDef::LocalExpr(name) => args.push(name),
ExprDef::LocalExpr(ref name) => args.push((*name).clone()),
_ => return Err(ParseError::ExpectedExpr("identifier", result)),
}
let next = try!(self.parse());

Loading…
Cancel
Save