Browse Source

Date Implementation (#596)

Co-authored-by: Jonathan Dickinson <jonathanD@k2.com>
pull/609/head
Jonathan Dickinson 4 years ago committed by GitHub
parent
commit
8fd5533460
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      Cargo.lock
  2. 1
      boa/Cargo.toml
  3. 1428
      boa/src/builtins/date/mod.rs
  4. 1431
      boa/src/builtins/date/tests.rs
  5. 3
      boa/src/builtins/mod.rs
  6. 6
      boa/src/builtins/object/mod.rs
  7. 2
      boa/src/builtins/object/tests.rs
  8. 27
      boa/src/builtins/value/mod.rs
  9. 8
      boa/src/builtins/value/operations.rs
  10. 2
      boa/src/builtins/value/val_type.rs
  11. 4
      boa/src/realm.rs

22
Cargo.lock generated

@ -5,6 +5,7 @@ name = "Boa"
version = "0.9.0"
dependencies = [
"bitflags",
"chrono",
"criterion",
"gc",
"indexmap",
@ -160,6 +161,17 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "chrono"
version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c74d84029116787153e02106bf53e66828452a4b325cc8652b788b5967c0a0b6"
dependencies = [
"num-integer",
"num-traits",
"time",
]
[[package]]
name = "clap"
version = "2.33.1"
@ -1010,6 +1022,16 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "time"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438"
dependencies = [
"libc",
"winapi",
]
[[package]]
name = "tinytemplate"
version = "1.1.0"

1
boa/Cargo.toml

@ -25,6 +25,7 @@ num-integer = "0.1.43"
bitflags = "1.2.1"
indexmap = "1.4.0"
ryu-js = "0.2.0"
chrono = "0.4"
# Optional Dependencies
serde = { version = "1.0.114", features = ["derive"], optional = true }

1428
boa/src/builtins/date/mod.rs

File diff suppressed because it is too large Load Diff

1431
boa/src/builtins/date/tests.rs

File diff suppressed because it is too large Load Diff

3
boa/src/builtins/mod.rs

@ -4,6 +4,7 @@ pub mod array;
pub mod bigint;
pub mod boolean;
pub mod console;
pub mod date;
pub mod error;
pub mod function;
pub mod global_this;
@ -26,6 +27,7 @@ pub(crate) use self::{
bigint::BigInt,
boolean::Boolean,
console::Console,
date::Date,
error::{Error, RangeError, ReferenceError, SyntaxError, TypeError},
global_this::GlobalThis,
infinity::Infinity,
@ -52,6 +54,7 @@ pub fn init(interpreter: &mut Interpreter) {
Array::init,
BigInt::init,
Boolean::init,
Date::init,
Json::init,
Map::init,
Math::init,

6
boa/src/builtins/object/mod.rs

@ -19,7 +19,7 @@ use crate::{
map::ordered_map::OrderedMap,
property::Property,
value::{RcBigInt, RcString, RcSymbol, ResultValue, Value},
BigInt, RegExp,
BigInt, Date, RegExp,
},
exec::Interpreter,
BoaProfiler,
@ -78,6 +78,8 @@ pub enum ObjectData {
Symbol(RcSymbol),
Error,
Ordinary,
Date(Date),
Global,
}
impl Display for ObjectData {
@ -97,6 +99,8 @@ impl Display for ObjectData {
Self::Boolean(_) => "Boolean",
Self::Number(_) => "Number",
Self::BigInt(_) => "BigInt",
Self::Date(_) => "Date",
Self::Global => "Global",
}
)
}

2
boa/src/builtins/object/tests.rs

@ -96,6 +96,8 @@ fn object_is() {
assert_eq!(forward(&mut engine, "Object.is(NaN, 0/0)"), "true");
assert_eq!(forward(&mut engine, "Object.is()"), "true");
assert_eq!(forward(&mut engine, "Object.is(undefined)"), "true");
assert!(engine.realm.global_obj.is_global());
assert!(!engine.realm.global_obj.get_field("Object").is_global());
}
#[test]
fn object_has_own_property() {

27
boa/src/builtins/value/mod.rs

@ -155,6 +155,14 @@ impl Value {
Self::Symbol(RcSymbol::from(symbol))
}
/// Helper function to convert the `Value` to a number and compute its power.
pub fn as_num_to_power(&self, other: Self) -> Self {
match (self, other) {
(Self::BigInt(ref a), Self::BigInt(ref b)) => Self::bigint(a.as_inner().clone().pow(b)),
(a, b) => Self::rational(a.to_number().powf(b.to_number())),
}
}
/// Returns a new empty object
pub fn new_object(global: Option<&Value>) -> Self {
let _timer = BoaProfiler::global().start_event("new_object", "value");
@ -231,8 +239,14 @@ impl Value {
}
}
/// Conversts the `Value` to `JSON`.
/// Converts the `Value` to `JSON`.
pub fn to_json(&self, interpreter: &mut Interpreter) -> Result<JSONValue, Value> {
let to_json = self.get_field("toJSON");
if to_json.is_function() {
let json_value = interpreter.call(&to_json, self, &[])?;
return json_value.to_json(interpreter);
}
match *self {
Self::Null => Ok(JSONValue::Null),
Self::Boolean(b) => Ok(JSONValue::Bool(b)),
@ -289,6 +303,17 @@ impl Value {
true
}
/// Returns true if the value the global for a Realm
pub fn is_global(&self) -> bool {
match self {
Value::Object(object) => match object.borrow().data {
ObjectData::Global => true,
_ => false,
},
_ => false,
}
}
/// Returns true if the value is an object
#[inline]
pub fn is_object(&self) -> bool {

8
boa/src/builtins/value/operations.rs

@ -382,9 +382,13 @@ impl Value {
}
#[inline]
pub fn neg(&self, _: &mut Interpreter) -> ResultValue {
pub fn neg(&self, interpreter: &mut Interpreter) -> ResultValue {
Ok(match *self {
Self::Object(_) | Self::Symbol(_) | Self::Undefined => Self::rational(NAN),
Self::Symbol(_) | Self::Undefined => Self::rational(NAN),
Self::Object(_) => Self::rational(match interpreter.to_numeric_number(self) {
Ok(num) => -num,
Err(_) => NAN,
}),
Self::String(ref str) => Self::rational(match f64::from_str(str) {
Ok(num) => -num,
Err(_) => NAN,

2
boa/src/builtins/value/val_type.rs

@ -14,6 +14,7 @@ pub enum Type {
BigInt,
Object,
Function,
Date,
}
impl Type {
@ -28,6 +29,7 @@ impl Type {
Self::Function => "function",
Self::Object => "object",
Self::BigInt => "bigint",
Self::Date => "date",
}
}
}

4
boa/src/realm.rs

@ -36,6 +36,10 @@ impl Realm {
// Create brand new global object
// Global has no prototype to pass None to new_obj
let global = Value::new_object(None);
// Allow identification of the global object easily
global.set_data(crate::builtins::object::ObjectData::Global);
// We need to clone the global here because its referenced from separate places (only pointer is cloned)
let global_env = new_global_environment(global.clone(), global.clone());

Loading…
Cancel
Save