Browse Source

Feat: Rename Object::prototype() and Object::set_prototype() (#693)

closes boa-dev/boa#607
pull/695/head
João Borges 4 years ago committed by GitHub
parent
commit
3f06e9a6ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      boa/src/builtins/array/mod.rs
  2. 6
      boa/src/builtins/boolean/tests.rs
  3. 4
      boa/src/builtins/json/tests.rs
  4. 2
      boa/src/builtins/map/mod.rs
  5. 8
      boa/src/builtins/object/mod.rs
  6. 5
      boa/src/context.rs
  7. 2
      boa/src/exec/tests.rs
  8. 4
      boa/src/object/mod.rs
  9. 6
      boa/src/value/display.rs
  10. 4
      boa/src/value/mod.rs

7
boa/src/builtins/array/mod.rs

@ -42,7 +42,10 @@ impl Array {
.expect("Could not get global object"), .expect("Could not get global object"),
)); ));
array.set_data(ObjectData::Array); array.set_data(ObjectData::Array);
array.as_object_mut().expect("array object").set_prototype( array
.as_object_mut()
.expect("array object")
.set_prototype_instance(
interpreter interpreter
.realm() .realm()
.environment .environment
@ -108,7 +111,7 @@ impl Array {
this.as_object_mut() this.as_object_mut()
.expect("this should be an array object") .expect("this should be an array object")
.set_prototype(prototype); .set_prototype_instance(prototype);
// This value is used by console.log and other routines to match Object type // This value is used by console.log and other routines to match Object type
// to its Javascript Identifier (global constructor method name) // to its Javascript Identifier (global constructor method name)
this.set_data(ObjectData::Array); this.set_data(ObjectData::Array);

6
boa/src/builtins/boolean/tests.rs

@ -59,7 +59,11 @@ fn instances_have_correct_proto_set() {
let bool_prototype = forward_val(&mut engine, "boolProto").expect("value expected"); let bool_prototype = forward_val(&mut engine, "boolProto").expect("value expected");
assert!(same_value( assert!(same_value(
&bool_instance.as_object().unwrap().prototype().clone(), &bool_instance
.as_object()
.unwrap()
.prototype_instance()
.clone(),
&bool_prototype &bool_prototype
)); ));
} }

4
boa/src/builtins/json/tests.rs

@ -270,13 +270,13 @@ fn json_parse_sets_prototypes() {
.unwrap() .unwrap()
.as_object() .as_object()
.unwrap() .unwrap()
.prototype() .prototype_instance()
.clone(); .clone();
let array_prototype = forward_val(&mut engine, r#"jsonObj.arr"#) let array_prototype = forward_val(&mut engine, r#"jsonObj.arr"#)
.unwrap() .unwrap()
.as_object() .as_object()
.unwrap() .unwrap()
.prototype() .prototype_instance()
.clone(); .clone();
let global_object_prototype = engine let global_object_prototype = engine
.global_object() .global_object()

2
boa/src/builtins/map/mod.rs

@ -232,7 +232,7 @@ impl Map {
this.as_object_mut() this.as_object_mut()
.expect("this is array object") .expect("this is array object")
.set_prototype(prototype); .set_prototype_instance(prototype);
// This value is used by console.log and other routines to match Object type // This value is used by console.log and other routines to match Object type
// to its Javascript Identifier (global constructor method name) // to its Javascript Identifier (global constructor method name)

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

@ -82,16 +82,16 @@ impl Object {
/// Get the `prototype` of an object. /// Get the `prototype` of an object.
pub fn get_prototype_of(_: &Value, args: &[Value], _: &mut Context) -> Result<Value> { pub fn get_prototype_of(_: &Value, args: &[Value], _: &mut Context) -> Result<Value> {
let obj = args.get(0).expect("Cannot get object"); let obj = args.get(0).expect("Cannot get object");
Ok(obj Ok(obj.as_object().map_or_else(Value::undefined, |object| {
.as_object() object.prototype_instance().clone()
.map_or_else(Value::undefined, |object| object.prototype().clone())) }))
} }
/// Set the `prototype` of an object. /// Set the `prototype` of an object.
pub fn set_prototype_of(_: &Value, args: &[Value], _: &mut Context) -> Result<Value> { pub fn set_prototype_of(_: &Value, args: &[Value], _: &mut Context) -> Result<Value> {
let obj = args.get(0).expect("Cannot get object").clone(); let obj = args.get(0).expect("Cannot get object").clone();
let proto = args.get(1).expect("Cannot get object").clone(); let proto = args.get(1).expect("Cannot get object").clone();
obj.as_object_mut().unwrap().set_prototype(proto); obj.as_object_mut().unwrap().set_prototype_instance(proto);
Ok(obj) Ok(obj)
} }

5
boa/src/context.rs

@ -333,7 +333,10 @@ impl Context {
.expect("Could not get global object"), .expect("Could not get global object"),
)); ));
array.set_data(ObjectData::Array); array.set_data(ObjectData::Array);
array.as_object_mut().expect("object").set_prototype( array
.as_object_mut()
.expect("object")
.set_prototype_instance(
self.realm() self.realm()
.environment .environment
.get_binding_value("Array") .get_binding_value("Array")

2
boa/src/exec/tests.rs

@ -759,7 +759,7 @@ mod in_operator {
let bar_obj = bar_val.as_object().unwrap(); let bar_obj = bar_val.as_object().unwrap();
let foo_val = forward_val(&mut engine, "Foo").unwrap(); let foo_val = forward_val(&mut engine, "Foo").unwrap();
assert!(bar_obj assert!(bar_obj
.prototype() .prototype_instance()
.strict_equals(&foo_val.get_field("prototype"))); .strict_equals(&foo_val.get_field("prototype")));
} }
} }

4
boa/src/object/mod.rs

@ -392,11 +392,11 @@ impl Object {
matches!(self.data, ObjectData::Ordinary) matches!(self.data, ObjectData::Ordinary)
} }
pub fn prototype(&self) -> &Value { pub fn prototype_instance(&self) -> &Value {
&self.prototype &self.prototype
} }
pub fn set_prototype(&mut self, prototype: Value) { pub fn set_prototype_instance(&mut self, prototype: Value) {
assert!(prototype.is_null() || prototype.is_object()); assert!(prototype.is_null() || prototype.is_object());
self.prototype = prototype self.prototype = prototype
} }

6
boa/src/value/display.rs

@ -29,18 +29,18 @@ macro_rules! print_obj_value {
(internals of $obj:expr, $display_fn:ident, $indent:expr, $encounters:expr) => { (internals of $obj:expr, $display_fn:ident, $indent:expr, $encounters:expr) => {
{ {
let object = $obj.borrow(); let object = $obj.borrow();
if object.prototype().is_object() { if object.prototype_instance().is_object() {
vec![format!( vec![format!(
"{:>width$}: {}", "{:>width$}: {}",
"__proto__", "__proto__",
$display_fn(object.prototype(), $encounters, $indent.wrapping_add(4), true), $display_fn(object.prototype_instance(), $encounters, $indent.wrapping_add(4), true),
width = $indent, width = $indent,
)] )]
} else { } else {
vec![format!( vec![format!(
"{:>width$}: {}", "{:>width$}: {}",
"__proto__", "__proto__",
object.prototype().display(), object.prototype_instance().display(),
width = $indent, width = $indent,
)] )]
} }

4
boa/src/value/mod.rs

@ -168,7 +168,7 @@ impl Value {
pub fn new_object_from_prototype(proto: Value, data: ObjectData) -> Self { pub fn new_object_from_prototype(proto: Value, data: ObjectData) -> Self {
let mut object = Object::default(); let mut object = Object::default();
object.data = data; object.data = data;
object.set_prototype(proto); object.set_prototype_instance(proto);
Self::object(object) Self::object(object)
} }
@ -472,7 +472,7 @@ impl Value {
return Some(property); return Some(property);
} }
object.prototype().get_property(key) object.prototype_instance().get_property(key)
} }
_ => None, _ => None,
} }

Loading…
Cancel
Save