|
|
@ -1,6 +1,6 @@ |
|
|
|
use gc::{Gc, GcCell}; |
|
|
|
|
|
|
|
use crate::js::function::{Function, NativeFunction, NativeFunctionData}; |
|
|
|
use crate::js::function::{Function, NativeFunction, NativeFunctionData}; |
|
|
|
use crate::js::object::{ObjectData, Property, INSTANCE_PROTOTYPE, PROTOTYPE}; |
|
|
|
use crate::js::object::{ObjectData, Property, INSTANCE_PROTOTYPE, PROTOTYPE}; |
|
|
|
|
|
|
|
use gc::{Gc, GcCell}; |
|
|
|
use serde_json::map::Map; |
|
|
|
use serde_json::map::Map; |
|
|
|
use serde_json::Number as JSONNumber; |
|
|
|
use serde_json::Number as JSONNumber; |
|
|
|
use serde_json::Value as JSONValue; |
|
|
|
use serde_json::Value as JSONValue; |
|
|
@ -37,7 +37,8 @@ pub enum ValueData { |
|
|
|
Integer(i32), |
|
|
|
Integer(i32), |
|
|
|
/// `Object` - An object, such as `Math`, represented by a binary tree of string keys to Javascript values
|
|
|
|
/// `Object` - An object, such as `Math`, represented by a binary tree of string keys to Javascript values
|
|
|
|
/// Some Objects will need an internal slot to hold private values, so our second ObjectData is for that
|
|
|
|
/// Some Objects will need an internal slot to hold private values, so our second ObjectData is for that
|
|
|
|
Object(GcCell<ObjectData>), |
|
|
|
/// The second object storage is optional for now
|
|
|
|
|
|
|
|
Object(GcCell<ObjectData>, GcCell<ObjectData>), |
|
|
|
/// `Function` - A runnable block of code, such as `Math.sqrt`, which can take some variables and return a useful value or act upon an object
|
|
|
|
/// `Function` - A runnable block of code, such as `Math.sqrt`, which can take some variables and return a useful value or act upon an object
|
|
|
|
Function(GcCell<Function>), |
|
|
|
Function(GcCell<Function>), |
|
|
|
} |
|
|
|
} |
|
|
@ -46,6 +47,7 @@ impl ValueData { |
|
|
|
/// Returns a new empty object
|
|
|
|
/// Returns a new empty object
|
|
|
|
pub fn new_obj(global: Option<Value>) -> Value { |
|
|
|
pub fn new_obj(global: Option<Value>) -> Value { |
|
|
|
let mut obj: ObjectData = HashMap::new(); |
|
|
|
let mut obj: ObjectData = HashMap::new(); |
|
|
|
|
|
|
|
let mut private_obj: ObjectData = HashMap::new(); |
|
|
|
if global.is_some() { |
|
|
|
if global.is_some() { |
|
|
|
let obj_proto = global |
|
|
|
let obj_proto = global |
|
|
|
.unwrap() |
|
|
|
.unwrap() |
|
|
@ -53,20 +55,27 @@ impl ValueData { |
|
|
|
.get_field_slice(PROTOTYPE); |
|
|
|
.get_field_slice(PROTOTYPE); |
|
|
|
obj.insert(INSTANCE_PROTOTYPE.to_string(), Property::new(obj_proto)); |
|
|
|
obj.insert(INSTANCE_PROTOTYPE.to_string(), Property::new(obj_proto)); |
|
|
|
} |
|
|
|
} |
|
|
|
Gc::new(ValueData::Object(GcCell::new(obj))) |
|
|
|
Gc::new(ValueData::Object( |
|
|
|
|
|
|
|
GcCell::new(obj), |
|
|
|
|
|
|
|
GcCell::new(private_obj), |
|
|
|
|
|
|
|
)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Similar to `new_obj`, but you can pass a prototype to create from
|
|
|
|
/// Similar to `new_obj`, but you can pass a prototype to create from
|
|
|
|
pub fn new_obj_from_prototype(proto: Value) -> Value { |
|
|
|
pub fn new_obj_from_prototype(proto: Value) -> Value { |
|
|
|
let mut obj: ObjectData = HashMap::new(); |
|
|
|
let mut obj: ObjectData = HashMap::new(); |
|
|
|
|
|
|
|
let mut private_obj: ObjectData = HashMap::new(); |
|
|
|
obj.insert(INSTANCE_PROTOTYPE.to_string(), Property::new(proto)); |
|
|
|
obj.insert(INSTANCE_PROTOTYPE.to_string(), Property::new(proto)); |
|
|
|
Gc::new(ValueData::Object(GcCell::new(obj))) |
|
|
|
Gc::new(ValueData::Object( |
|
|
|
|
|
|
|
GcCell::new(obj), |
|
|
|
|
|
|
|
GcCell::new(private_obj), |
|
|
|
|
|
|
|
)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Returns true if the value is an object
|
|
|
|
/// Returns true if the value is an object
|
|
|
|
pub fn is_object(&self) -> bool { |
|
|
|
pub fn is_object(&self) -> bool { |
|
|
|
match *self { |
|
|
|
match *self { |
|
|
|
ValueData::Object(_) => true, |
|
|
|
ValueData::Object(_, _) => true, |
|
|
|
_ => false, |
|
|
|
_ => false, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -115,7 +124,7 @@ impl ValueData { |
|
|
|
/// [toBoolean](https://tc39.github.io/ecma262/#sec-toboolean)
|
|
|
|
/// [toBoolean](https://tc39.github.io/ecma262/#sec-toboolean)
|
|
|
|
pub fn is_true(&self) -> bool { |
|
|
|
pub fn is_true(&self) -> bool { |
|
|
|
match *self { |
|
|
|
match *self { |
|
|
|
ValueData::Object(_) => true, |
|
|
|
ValueData::Object(_, _) => true, |
|
|
|
ValueData::String(ref s) if !s.is_empty() => true, |
|
|
|
ValueData::String(ref s) if !s.is_empty() => true, |
|
|
|
ValueData::Number(n) if n >= 1.0 && n % 1.0 == 0.0 => true, |
|
|
|
ValueData::Number(n) if n >= 1.0 && n % 1.0 == 0.0 => true, |
|
|
|
ValueData::Integer(n) if n > 1 => true, |
|
|
|
ValueData::Integer(n) if n > 1 => true, |
|
|
@ -127,7 +136,7 @@ impl ValueData { |
|
|
|
/// Converts the value into a 64-bit floating point number
|
|
|
|
/// Converts the value into a 64-bit floating point number
|
|
|
|
pub fn to_num(&self) -> f64 { |
|
|
|
pub fn to_num(&self) -> f64 { |
|
|
|
match *self { |
|
|
|
match *self { |
|
|
|
ValueData::Object(_) | ValueData::Undefined | ValueData::Function(_) => NAN, |
|
|
|
ValueData::Object(_, _) | ValueData::Undefined | ValueData::Function(_) => NAN, |
|
|
|
ValueData::String(ref str) => match FromStr::from_str(str) { |
|
|
|
ValueData::String(ref str) => match FromStr::from_str(str) { |
|
|
|
Ok(num) => num, |
|
|
|
Ok(num) => num, |
|
|
|
Err(_) => NAN, |
|
|
|
Err(_) => NAN, |
|
|
@ -142,7 +151,7 @@ impl ValueData { |
|
|
|
/// Converts the value into a 32-bit integer
|
|
|
|
/// Converts the value into a 32-bit integer
|
|
|
|
pub fn to_int(&self) -> i32 { |
|
|
|
pub fn to_int(&self) -> i32 { |
|
|
|
match *self { |
|
|
|
match *self { |
|
|
|
ValueData::Object(_) |
|
|
|
ValueData::Object(_, _) |
|
|
|
| ValueData::Undefined |
|
|
|
| ValueData::Undefined |
|
|
|
| ValueData::Null |
|
|
|
| ValueData::Null |
|
|
|
| ValueData::Boolean(false) |
|
|
|
| ValueData::Boolean(false) |
|
|
@ -164,25 +173,46 @@ impl ValueData { |
|
|
|
// This is only for primitive strings, String() objects have their lengths calculated in string.rs
|
|
|
|
// This is only for primitive strings, String() objects have their lengths calculated in string.rs
|
|
|
|
if self.is_string() && field == "length" { |
|
|
|
if self.is_string() && field == "length" { |
|
|
|
if let ValueData::String(ref s) = *self { |
|
|
|
if let ValueData::String(ref s) = *self { |
|
|
|
return Some(Property::new(to_value(s.len() as i32))) |
|
|
|
return Some(Property::new(to_value(s.len() as i32))); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let obj: ObjectData = match *self { |
|
|
|
let obj: ObjectData = match *self { |
|
|
|
ValueData::Object(ref obj) => { |
|
|
|
ValueData::Object(ref obj, _) => { |
|
|
|
let hash = obj.clone(); |
|
|
|
let hash = obj.clone(); |
|
|
|
hash.into_inner() |
|
|
|
hash.into_inner() |
|
|
|
} |
|
|
|
} |
|
|
|
// Accesing .object on borrow() seems to automatically dereference it, so we don't need the *
|
|
|
|
// Accesing .object on borrow() seems to automatically dereference it, so we don't need the *
|
|
|
|
ValueData::Function(ref func) => match func.clone().into_inner() { |
|
|
|
ValueData::Function(ref func) => match func.clone().into_inner() { |
|
|
|
Function::NativeFunc(ref func) => func.object.clone(), |
|
|
|
Function::NativeFunc(ref func) => func.object.clone(), |
|
|
|
Function::RegularFunc(ref func) => func.object.clone() |
|
|
|
Function::RegularFunc(ref func) => func.object.clone(), |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
_ => return None, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
match obj.get(&field) { |
|
|
|
|
|
|
|
Some(val) => Some(val.clone()), |
|
|
|
|
|
|
|
None => match obj.get(&INSTANCE_PROTOTYPE.to_string()) { |
|
|
|
|
|
|
|
Some(prop) => prop.value.get_prop(field), |
|
|
|
|
|
|
|
None => None, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Resolve the property in the object
|
|
|
|
|
|
|
|
/// Returns a copy of the Property
|
|
|
|
|
|
|
|
pub fn get_private_prop(&self, field: String) -> Option<Property> { |
|
|
|
|
|
|
|
let obj: ObjectData = match *self { |
|
|
|
|
|
|
|
ValueData::Object(_, ref obj) => { |
|
|
|
|
|
|
|
let hash = obj.clone(); |
|
|
|
|
|
|
|
hash.into_inner() |
|
|
|
} |
|
|
|
} |
|
|
|
_ => return None, |
|
|
|
_ => return None, |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
match obj.get(&field) { |
|
|
|
match obj.get(&field) { |
|
|
|
Some(val) => Some(val.clone()), |
|
|
|
Some(val) => Some(val.clone()), |
|
|
|
None => match obj.get(&PROTOTYPE.to_string()) { |
|
|
|
None => match obj.get(&INSTANCE_PROTOTYPE.to_string()) { |
|
|
|
Some(prop) => prop.value.get_prop(field), |
|
|
|
Some(prop) => prop.value.get_prop(field), |
|
|
|
None => None, |
|
|
|
None => None, |
|
|
|
}, |
|
|
|
}, |
|
|
@ -190,9 +220,44 @@ impl ValueData { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Resolve the property in the object and get its value, or undefined if this is not an object or the field doesn't exist
|
|
|
|
/// Resolve the property in the object and get its value, or undefined if this is not an object or the field doesn't exist
|
|
|
|
|
|
|
|
/// get_field recieves a Property from get_prop(). It should then return the [[Get]] result value if that's set, otherwise fall back to [[Value]]
|
|
|
|
|
|
|
|
pub fn get_private_field(&self, field: String) -> Value { |
|
|
|
|
|
|
|
match self.get_private_prop(field) { |
|
|
|
|
|
|
|
Some(prop) => prop.value.clone(), |
|
|
|
|
|
|
|
None => Gc::new(ValueData::Undefined), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Resolve the property in the object and get its value, or undefined if this is not an object or the field doesn't exist
|
|
|
|
|
|
|
|
/// get_field recieves a Property from get_prop(). It should then return the [[Get]] result value if that's set, otherwise fall back to [[Value]]
|
|
|
|
pub fn get_field(&self, field: String) -> Value { |
|
|
|
pub fn get_field(&self, field: String) -> Value { |
|
|
|
match self.get_prop(field) { |
|
|
|
match self.get_prop(field) { |
|
|
|
Some(prop) => prop.value.clone(), |
|
|
|
Some(prop) => { |
|
|
|
|
|
|
|
// If the Property has [[Get]] set to a function, we should run that and return the Value
|
|
|
|
|
|
|
|
let prop_getter = match *prop.get { |
|
|
|
|
|
|
|
ValueData::Function(ref v) => match *v.borrow() { |
|
|
|
|
|
|
|
Function::NativeFunc(ref ntv) => { |
|
|
|
|
|
|
|
let func = ntv.data; |
|
|
|
|
|
|
|
Some( |
|
|
|
|
|
|
|
func( |
|
|
|
|
|
|
|
Gc::new(self.clone()), |
|
|
|
|
|
|
|
Gc::new(self.clone()), |
|
|
|
|
|
|
|
vec![Gc::new(self.clone())], |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
.unwrap(), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
_ => None, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
_ => None, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If the getter is populated, use that. If not use [[Value]] instead
|
|
|
|
|
|
|
|
match prop_getter { |
|
|
|
|
|
|
|
Some(val) => val, |
|
|
|
|
|
|
|
None => prop.value.clone(), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
None => Gc::new(ValueData::Undefined), |
|
|
|
None => Gc::new(ValueData::Undefined), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -205,7 +270,7 @@ impl ValueData { |
|
|
|
/// Set the field in the value
|
|
|
|
/// Set the field in the value
|
|
|
|
pub fn set_field(&self, field: String, val: Value) -> Value { |
|
|
|
pub fn set_field(&self, field: String, val: Value) -> Value { |
|
|
|
match *self { |
|
|
|
match *self { |
|
|
|
ValueData::Object(ref obj) => { |
|
|
|
ValueData::Object(ref obj, _) => { |
|
|
|
obj.borrow_mut() |
|
|
|
obj.borrow_mut() |
|
|
|
.insert(field.clone(), Property::new(val.clone())); |
|
|
|
.insert(field.clone(), Property::new(val.clone())); |
|
|
|
} |
|
|
|
} |
|
|
@ -229,10 +294,27 @@ impl ValueData { |
|
|
|
self.set_field(field.to_string(), val) |
|
|
|
self.set_field(field.to_string(), val) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Set the private field in the value
|
|
|
|
|
|
|
|
pub fn set_private_field(&self, field: String, val: Value) -> Value { |
|
|
|
|
|
|
|
match *self { |
|
|
|
|
|
|
|
ValueData::Object(_, ref obj) => { |
|
|
|
|
|
|
|
obj.borrow_mut() |
|
|
|
|
|
|
|
.insert(field.clone(), Property::new(val.clone())); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
_ => (), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
val |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Set the private field in the value
|
|
|
|
|
|
|
|
pub fn set_private_field_slice<'a>(&self, field: &'a str, val: Value) -> Value { |
|
|
|
|
|
|
|
self.set_private_field(field.to_string(), val) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Set the property in the value
|
|
|
|
/// Set the property in the value
|
|
|
|
pub fn set_prop(&self, field: String, prop: Property) -> Property { |
|
|
|
pub fn set_prop(&self, field: String, prop: Property) -> Property { |
|
|
|
match *self { |
|
|
|
match *self { |
|
|
|
ValueData::Object(ref obj) => { |
|
|
|
ValueData::Object(ref obj, _) => { |
|
|
|
obj.borrow_mut().insert(field.clone(), prop.clone()); |
|
|
|
obj.borrow_mut().insert(field.clone(), prop.clone()); |
|
|
|
} |
|
|
|
} |
|
|
|
ValueData::Function(ref func) => { |
|
|
|
ValueData::Function(ref func) => { |
|
|
@ -261,6 +343,7 @@ impl ValueData { |
|
|
|
JSONValue::Bool(v) => ValueData::Boolean(v), |
|
|
|
JSONValue::Bool(v) => ValueData::Boolean(v), |
|
|
|
JSONValue::Array(vs) => { |
|
|
|
JSONValue::Array(vs) => { |
|
|
|
let mut i = 0; |
|
|
|
let mut i = 0; |
|
|
|
|
|
|
|
let mut private_data: ObjectData = HashMap::new(); |
|
|
|
let mut data: ObjectData = FromIterator::from_iter(vs.iter().map(|json| { |
|
|
|
let mut data: ObjectData = FromIterator::from_iter(vs.iter().map(|json| { |
|
|
|
i += 1; |
|
|
|
i += 1; |
|
|
|
( |
|
|
|
( |
|
|
@ -272,14 +355,15 @@ impl ValueData { |
|
|
|
"length".to_string(), |
|
|
|
"length".to_string(), |
|
|
|
Property::new(to_value(vs.len() as i32)), |
|
|
|
Property::new(to_value(vs.len() as i32)), |
|
|
|
); |
|
|
|
); |
|
|
|
ValueData::Object(GcCell::new(data)) |
|
|
|
ValueData::Object(GcCell::new(data), GcCell::new(private_data)) |
|
|
|
} |
|
|
|
} |
|
|
|
JSONValue::Object(obj) => { |
|
|
|
JSONValue::Object(obj) => { |
|
|
|
|
|
|
|
let mut private_data: ObjectData = HashMap::new(); |
|
|
|
let data: ObjectData = FromIterator::from_iter( |
|
|
|
let data: ObjectData = FromIterator::from_iter( |
|
|
|
obj.iter() |
|
|
|
obj.iter() |
|
|
|
.map(|(key, json)| (key.clone(), Property::new(to_value(json.clone())))), |
|
|
|
.map(|(key, json)| (key.clone(), Property::new(to_value(json.clone())))), |
|
|
|
); |
|
|
|
); |
|
|
|
ValueData::Object(GcCell::new(data)) |
|
|
|
ValueData::Object(GcCell::new(data), GcCell::new(private_data)) |
|
|
|
} |
|
|
|
} |
|
|
|
JSONValue::Null => ValueData::Null, |
|
|
|
JSONValue::Null => ValueData::Null, |
|
|
|
} |
|
|
|
} |
|
|
@ -289,7 +373,7 @@ impl ValueData { |
|
|
|
match *self { |
|
|
|
match *self { |
|
|
|
ValueData::Null | ValueData::Undefined => JSONValue::Null, |
|
|
|
ValueData::Null | ValueData::Undefined => JSONValue::Null, |
|
|
|
ValueData::Boolean(b) => JSONValue::Bool(b), |
|
|
|
ValueData::Boolean(b) => JSONValue::Bool(b), |
|
|
|
ValueData::Object(ref obj) => { |
|
|
|
ValueData::Object(ref obj, _) => { |
|
|
|
let mut nobj = Map::new(); |
|
|
|
let mut nobj = Map::new(); |
|
|
|
for (k, v) in obj.borrow().iter() { |
|
|
|
for (k, v) in obj.borrow().iter() { |
|
|
|
if k != INSTANCE_PROTOTYPE { |
|
|
|
if k != INSTANCE_PROTOTYPE { |
|
|
@ -335,8 +419,8 @@ impl Display for ValueData { |
|
|
|
_ => v.to_string(), |
|
|
|
_ => v.to_string(), |
|
|
|
} |
|
|
|
} |
|
|
|
), |
|
|
|
), |
|
|
|
ValueData::Object(ref v) => { |
|
|
|
ValueData::Object(ref v, _) => { |
|
|
|
r#try!(write!(f, "{}", "{")); |
|
|
|
write!(f, "{}", "{")?; |
|
|
|
match v.borrow().iter().last() { |
|
|
|
match v.borrow().iter().last() { |
|
|
|
Some((last_key, _)) => { |
|
|
|
Some((last_key, _)) => { |
|
|
|
for (key, val) in v.borrow().iter() { |
|
|
|
for (key, val) in v.borrow().iter() { |
|
|
@ -567,14 +651,18 @@ impl<T: FromValue> FromValue for Vec<T> { |
|
|
|
|
|
|
|
|
|
|
|
impl ToValue for ObjectData { |
|
|
|
impl ToValue for ObjectData { |
|
|
|
fn to_value(&self) -> Value { |
|
|
|
fn to_value(&self) -> Value { |
|
|
|
Gc::new(ValueData::Object(GcCell::new(self.clone()))) |
|
|
|
let mut private_obj: ObjectData = HashMap::new(); |
|
|
|
|
|
|
|
Gc::new(ValueData::Object( |
|
|
|
|
|
|
|
GcCell::new(self.clone()), |
|
|
|
|
|
|
|
GcCell::new(private_obj), |
|
|
|
|
|
|
|
)) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl FromValue for ObjectData { |
|
|
|
impl FromValue for ObjectData { |
|
|
|
fn from_value(v: Value) -> Result<ObjectData, &'static str> { |
|
|
|
fn from_value(v: Value) -> Result<ObjectData, &'static str> { |
|
|
|
match *v { |
|
|
|
match *v { |
|
|
|
ValueData::Object(ref obj) => Ok(obj.clone().into_inner()), |
|
|
|
ValueData::Object(ref obj, _) => Ok(obj.clone().into_inner()), |
|
|
|
ValueData::Function(ref func) => Ok(match *func.borrow().deref() { |
|
|
|
ValueData::Function(ref func) => Ok(match *func.borrow().deref() { |
|
|
|
Function::NativeFunc(ref data) => data.object.clone(), |
|
|
|
Function::NativeFunc(ref data) => data.object.clone(), |
|
|
|
Function::RegularFunc(ref data) => data.object.clone(), |
|
|
|
Function::RegularFunc(ref data) => data.object.clone(), |
|
|
|