Browse Source

resolved many errors

pull/5/head
Jason Williams 6 years ago
parent
commit
e63b3e76d4
  1. 30
      src/lib/exec.rs
  2. 9
      src/lib/js/console.rs
  3. 5
      src/lib/js/json.rs
  4. 45
      src/lib/js/math.rs
  5. 27
      src/lib/js/object.rs

30
src/lib/exec.rs

@ -7,7 +7,7 @@ use std::borrow::Borrow;
use std::collections::HashMap; use std::collections::HashMap;
use syntax::ast::constant::Const; use syntax::ast::constant::Const;
use syntax::ast::expr::{Expr, ExprDef}; use syntax::ast::expr::{Expr, ExprDef};
use syntax::ast::op::BinOp; use syntax::ast::op::{BinOp, CompOp};
/// A variable scope /// A variable scope
pub struct Scope { pub struct Scope {
/// The value of `this` in the scope /// The value of `this` in the scope
@ -195,13 +195,13 @@ impl Executor for Interpreter {
}) })
} }
ExprDef::SwitchExpr(ref val_e, ref vals, ref default) => { ExprDef::SwitchExpr(ref val_e, ref vals, ref default) => {
let val = try!(self.run(val_e)).borrow().clone(); let val = try!(self.run(val_e)).clone();
let mut result = Gc::new(ValueData::Null); let mut result = Gc::new(ValueData::Null);
let mut matched = false; let mut matched = false;
for tup in vals.iter() { for tup in vals.iter() {
let tup: &(Expr, Vec<Expr>) = tup; let tup: &(Expr, Vec<Expr>) = tup;
match *tup { match *tup {
(ref cond, ref block) if (val == *try!(self.run(cond)).borrow()) => { (ref cond, ref block) if (val == try!(self.run(cond))) => {
matched = true; matched = true;
let last_expr = block.last().unwrap(); let last_expr = block.last().unwrap();
for expr in block.iter() { for expr in block.iter() {
@ -299,18 +299,18 @@ impl Executor for Interpreter {
let v_a = v_r_a.borrow(); let v_a = v_r_a.borrow();
let v_b = v_r_b.borrow(); let v_b = v_r_b.borrow();
Ok(to_value(match *op { Ok(to_value(match *op {
CompEqual if v_a.is_object() => v_r_a == v_r_b, CompOp::Equal if v_a.is_object() => v_r_a == v_r_b,
CompEqual => v_a == v_b, CompOp::Equal => v_a == v_b,
CompNotEqual if v_a.is_object() => v_r_a != v_r_b, CompOp::NotEqual if v_a.is_object() => v_r_a != v_r_b,
CompNotEqual => v_a != v_b, CompOp::NotEqual => v_a != v_b,
CompStrictEqual if v_a.is_object() => v_r_a == v_r_b, CompOp::StrictEqual if v_a.is_object() => v_r_a == v_r_b,
CompStrictEqual => v_a == v_b, CompOp::StrictEqual => v_a == v_b,
CompStrictNotEqual if v_a.is_object() => v_r_a != v_r_b, CompOp::StrictNotEqual if v_a.is_object() => v_r_a != v_r_b,
CompStrictNotEqual => v_a != v_b, CompOp::StrictNotEqual => v_a != v_b,
CompGreaterThan => v_a.to_num() > v_b.to_num(), CompOp::GreaterThan => v_a.to_num() > v_b.to_num(),
CompGreaterThanOrEqual => v_a.to_num() >= v_b.to_num(), CompOp::GreaterThanOrEqual => v_a.to_num() >= v_b.to_num(),
CompLessThan => v_a.to_num() < v_b.to_num(), CompOp::LessThan => v_a.to_num() < v_b.to_num(),
CompLessThanOrEqual => v_a.to_num() <= v_b.to_num(), CompOp::LessThanOrEqual => v_a.to_num() <= v_b.to_num(),
})) }))
} }
ExprDef::BinOpExpr(BinOp::Log(ref op), ref a, ref b) => { ExprDef::BinOpExpr(BinOp::Log(ref op), ref a, ref b) => {

9
src/lib/js/console.rs

@ -1,4 +1,5 @@
use gc::Gc; use gc::Gc;
use js::function::NativeFunctionData;
use js::value::{from_value, to_value, ResultValue, Value, ValueData}; use js::value::{from_value, to_value, ResultValue, Value, ValueData};
use std::iter::FromIterator; use std::iter::FromIterator;
use time::{now, strftime}; use time::{now, strftime};
@ -10,7 +11,7 @@ pub fn log(_: Value, _: Value, args: Vec<Value>) -> ResultValue {
Ok(Gc::new(ValueData::Undefined)) Ok(Gc::new(ValueData::Undefined))
} }
/// Print a javascript value to the standard error stream /// Print a javascript value to the standard error stream
pub fn error(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue { pub fn error(_: Value, _: Value, args: Vec<Value>) -> ResultValue {
let args: Vec<String> = FromIterator::from_iter( let args: Vec<String> = FromIterator::from_iter(
args.iter() args.iter()
.map(|x| from_value::<String>(x.clone()).unwrap()), .map(|x| from_value::<String>(x.clone()).unwrap()),
@ -21,9 +22,9 @@ pub fn error(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue {
/// Create a new `console` object /// Create a new `console` object
pub fn _create(global: Value) -> Value { pub fn _create(global: Value) -> Value {
let console = ValueData::new_obj(Some(global)); let console = ValueData::new_obj(Some(global));
console.set_field_slice("log", to_value(log)); console.set_field_slice("log", to_value(log as NativeFunctionData));
console.set_field_slice("error", to_value(error)); console.set_field_slice("error", to_value(error as NativeFunctionData));
console.set_field_slice("exception", to_value(error)); console.set_field_slice("exception", to_value(error as NativeFunctionData));
console console
} }
/// Initialise the global object with the `console` object /// Initialise the global object with the `console` object

5
src/lib/js/json.rs

@ -1,3 +1,4 @@
use js::function::NativeFunctionData;
/// The JSON Object /// The JSON Object
/// https://tc39.github.io/ecma262/#sec-json-object /// https://tc39.github.io/ecma262/#sec-json-object
use js::value::{to_value, ResultValue, Value, ValueData}; use js::value::{to_value, ResultValue, Value, ValueData};
@ -21,8 +22,8 @@ pub fn stringify(_: Value, _: Value, args: Vec<Value>) -> ResultValue {
/// Create a new `JSON` object /// Create a new `JSON` object
pub fn _create(global: Value) -> Value { pub fn _create(global: Value) -> Value {
let object = ValueData::new_obj(Some(global)); let object = ValueData::new_obj(Some(global));
object.set_field_slice("stringify", to_value(stringify)); object.set_field_slice("stringify", to_value(stringify as NativeFunctionData));
object.set_field_slice("parse", to_value(parse)); object.set_field_slice("parse", to_value(parse as NativeFunctionData));
object object
} }

45
src/lib/js/math.rs

@ -1,4 +1,5 @@
use js::value::{from_value, to_value, ResultValue, Value}; use js::function::NativeFunctionData;
use js::value::{from_value, to_value, ResultValue, Value, ValueData};
use rand::random; use rand::random;
use std::f64; use std::f64;
@ -141,7 +142,7 @@ pub fn pow(_: Value, _: Value, args: Vec<Value>) -> ResultValue {
})) }))
} }
/// Generate a random floating-point number between 0 and 1 /// Generate a random floating-point number between 0 and 1
pub fn _random(_: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue { pub fn _random(_: Value, _: Value, args: Vec<Value>) -> ResultValue {
Ok(to_value(random::<f64>())) Ok(to_value(random::<f64>()))
} }
/// Round a number to the nearest integer /// Round a number to the nearest integer
@ -186,7 +187,7 @@ pub fn tan(_: Value, _: Value, args: Vec<Value>) -> ResultValue {
} }
/// Create a new `Math` object /// Create a new `Math` object
pub fn _create(global: Value) -> Value { pub fn _create(global: Value) -> Value {
let math = Value::new_obj(Some(global)); let math = ValueData::new_obj(Some(global));
math.set_field_slice("E", to_value(f64::consts::E)); math.set_field_slice("E", to_value(f64::consts::E));
math.set_field_slice("LN2", to_value(f64::consts::LN_2)); math.set_field_slice("LN2", to_value(f64::consts::LN_2));
math.set_field_slice("LN10", to_value(f64::consts::LN_10)); math.set_field_slice("LN10", to_value(f64::consts::LN_10));
@ -195,25 +196,25 @@ pub fn _create(global: Value) -> Value {
math.set_field_slice("SQRT1_2", to_value(0.5f64.sqrt())); math.set_field_slice("SQRT1_2", to_value(0.5f64.sqrt()));
math.set_field_slice("SQRT2", to_value(f64::consts::SQRT_2)); math.set_field_slice("SQRT2", to_value(f64::consts::SQRT_2));
math.set_field_slice("PI", to_value(f64::consts::PI)); math.set_field_slice("PI", to_value(f64::consts::PI));
math.set_field_slice("abs", to_value(abs)); math.set_field_slice("abs", to_value(abs as NativeFunctionData));
math.set_field_slice("acos", to_value(acos)); math.set_field_slice("acos", to_value(acos as NativeFunctionData));
math.set_field_slice("asin", to_value(asin)); math.set_field_slice("asin", to_value(asin as NativeFunctionData));
math.set_field_slice("atan", to_value(atan)); math.set_field_slice("atan", to_value(atan as NativeFunctionData));
math.set_field_slice("atan2", to_value(atan2)); math.set_field_slice("atan2", to_value(atan2 as NativeFunctionData));
math.set_field_slice("cbrt", to_value(cbrt)); math.set_field_slice("cbrt", to_value(cbrt as NativeFunctionData));
math.set_field_slice("ceil", to_value(ceil)); math.set_field_slice("ceil", to_value(ceil as NativeFunctionData));
math.set_field_slice("cos", to_value(cos)); math.set_field_slice("cos", to_value(cos as NativeFunctionData));
math.set_field_slice("exp", to_value(exp)); math.set_field_slice("exp", to_value(exp as NativeFunctionData));
math.set_field_slice("floor", to_value(floor, &["num"])); math.set_field_slice("floor", to_value(floor as NativeFunctionData));
math.set_field_slice("log", to_value(log)); math.set_field_slice("log", to_value(log as NativeFunctionData));
math.set_field_slice("max", to_value(max)); math.set_field_slice("max", to_value(max as NativeFunctionData));
math.set_field_slice("min", to_value(min)); math.set_field_slice("min", to_value(min as NativeFunctionData));
math.set_field_slice("pow", to_value(pow)); math.set_field_slice("pow", to_value(pow as NativeFunctionData));
math.set_field_slice("random", to_value(_random)); math.set_field_slice("random", to_value(_random as NativeFunctionData));
math.set_field_slice("round", to_value(round)); math.set_field_slice("round", to_value(round as NativeFunctionData));
math.set_field_slice("sin", to_value(sin)); math.set_field_slice("sin", to_value(sin as NativeFunctionData));
math.set_field_slice("sqrt", to_value(sqrt)); math.set_field_slice("sqrt", to_value(sqrt as NativeFunctionData));
math.set_field_slice("tan", to_value(tan)); math.set_field_slice("tan", to_value(tan as NativeFunctionData));
math math
} }
/// Initialise the `Math` object on the global object /// Initialise the `Math` object on the global object

27
src/lib/js/object.rs

@ -1,4 +1,5 @@
use gc::Gc; use gc::Gc;
use js::function::NativeFunctionData;
use js::value::{from_value, to_value, FromValue, ResultValue, ToValue, Value, ValueData}; use js::value::{from_value, to_value, FromValue, ResultValue, ToValue, Value, ValueData};
use std::collections::HashMap; use std::collections::HashMap;
pub static PROTOTYPE: &'static str = "prototype"; pub static PROTOTYPE: &'static str = "prototype";
@ -66,7 +67,7 @@ impl FromValue for Property {
} }
/// Create a new object /// Create a new object
pub fn make_object(_: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue { pub fn make_object(_: Value, _: Value, args: Vec<Value>) -> ResultValue {
Ok(Gc::new(ValueData::Undefined)) Ok(Gc::new(ValueData::Undefined))
} }
@ -112,16 +113,28 @@ pub fn has_own_prop(this: Value, _: Value, args: Vec<Value>) -> ResultValue {
/// Create a new `Object` object /// Create a new `Object` object
pub fn _create(global: Value) -> Value { pub fn _create(global: Value) -> Value {
let object = to_value(make_object); let object = to_value(make_object as NativeFunctionData);
let object_ptr = object; let object_ptr = object;
let prototype = ValueData::new_obj(Some(global)); let prototype = ValueData::new_obj(Some(global));
prototype.set_field_slice("hasOwnProperty", to_value(has_own_prop)); prototype.set_field_slice(
prototype.set_field_slice("toString", to_value(to_string)); "hasOwnProperty",
to_value(has_own_prop as NativeFunctionData),
);
prototype.set_field_slice("toString", to_value(to_string as NativeFunctionData));
object_ptr.set_field_slice("length", to_value(1i32)); object_ptr.set_field_slice("length", to_value(1i32));
object_ptr.set_field_slice(PROTOTYPE, prototype); object_ptr.set_field_slice(PROTOTYPE, prototype);
object_ptr.set_field_slice("setPrototypeOf", to_value(set_proto_of)); object_ptr.set_field_slice(
object_ptr.set_field_slice("getPrototypeOf", to_value(get_proto_of)); "setPrototypeOf",
object_ptr.set_field_slice("defineProperty", to_value(define_prop)); to_value(set_proto_of as NativeFunctionData),
);
object_ptr.set_field_slice(
"getPrototypeOf",
to_value(get_proto_of as NativeFunctionData),
);
object_ptr.set_field_slice(
"defineProperty",
to_value(define_prop as NativeFunctionData),
);
object object
} }

Loading…
Cancel
Save