From 3f06e9a6caec872b4645fe3604ad75c85591c73c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Borges?= Date: Sat, 12 Sep 2020 09:18:59 +0100 Subject: [PATCH] Feat: Rename Object::prototype() and Object::set_prototype() (#693) closes boa-dev/boa#607 --- boa/src/builtins/array/mod.rs | 21 ++++++++++++--------- boa/src/builtins/boolean/tests.rs | 6 +++++- boa/src/builtins/json/tests.rs | 4 ++-- boa/src/builtins/map/mod.rs | 2 +- boa/src/builtins/object/mod.rs | 8 ++++---- boa/src/context.rs | 17 ++++++++++------- boa/src/exec/tests.rs | 2 +- boa/src/object/mod.rs | 4 ++-- boa/src/value/display.rs | 6 +++--- boa/src/value/mod.rs | 4 ++-- 10 files changed, 42 insertions(+), 32 deletions(-) diff --git a/boa/src/builtins/array/mod.rs b/boa/src/builtins/array/mod.rs index a8476ec343..c9fe1bacab 100644 --- a/boa/src/builtins/array/mod.rs +++ b/boa/src/builtins/array/mod.rs @@ -42,14 +42,17 @@ impl Array { .expect("Could not get global object"), )); array.set_data(ObjectData::Array); - array.as_object_mut().expect("array object").set_prototype( - interpreter - .realm() - .environment - .get_binding_value("Array") - .expect("Array was not initialized") - .get_field(PROTOTYPE), - ); + array + .as_object_mut() + .expect("array object") + .set_prototype_instance( + interpreter + .realm() + .environment + .get_binding_value("Array") + .expect("Array was not initialized") + .get_field(PROTOTYPE), + ); array.set_field("length", Value::from(0)); Ok(array) } @@ -108,7 +111,7 @@ impl Array { this.as_object_mut() .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 // to its Javascript Identifier (global constructor method name) this.set_data(ObjectData::Array); diff --git a/boa/src/builtins/boolean/tests.rs b/boa/src/builtins/boolean/tests.rs index 9008983d5f..eef4101028 100644 --- a/boa/src/builtins/boolean/tests.rs +++ b/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"); assert!(same_value( - &bool_instance.as_object().unwrap().prototype().clone(), + &bool_instance + .as_object() + .unwrap() + .prototype_instance() + .clone(), &bool_prototype )); } diff --git a/boa/src/builtins/json/tests.rs b/boa/src/builtins/json/tests.rs index d091cd423b..14ec37ced6 100644 --- a/boa/src/builtins/json/tests.rs +++ b/boa/src/builtins/json/tests.rs @@ -270,13 +270,13 @@ fn json_parse_sets_prototypes() { .unwrap() .as_object() .unwrap() - .prototype() + .prototype_instance() .clone(); let array_prototype = forward_val(&mut engine, r#"jsonObj.arr"#) .unwrap() .as_object() .unwrap() - .prototype() + .prototype_instance() .clone(); let global_object_prototype = engine .global_object() diff --git a/boa/src/builtins/map/mod.rs b/boa/src/builtins/map/mod.rs index 4437700c88..98918556c5 100644 --- a/boa/src/builtins/map/mod.rs +++ b/boa/src/builtins/map/mod.rs @@ -232,7 +232,7 @@ impl Map { this.as_object_mut() .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 // to its Javascript Identifier (global constructor method name) diff --git a/boa/src/builtins/object/mod.rs b/boa/src/builtins/object/mod.rs index 851e655ca8..3bd9093a21 100644 --- a/boa/src/builtins/object/mod.rs +++ b/boa/src/builtins/object/mod.rs @@ -82,16 +82,16 @@ impl Object { /// Get the `prototype` of an object. pub fn get_prototype_of(_: &Value, args: &[Value], _: &mut Context) -> Result { let obj = args.get(0).expect("Cannot get object"); - Ok(obj - .as_object() - .map_or_else(Value::undefined, |object| object.prototype().clone())) + Ok(obj.as_object().map_or_else(Value::undefined, |object| { + object.prototype_instance().clone() + })) } /// Set the `prototype` of an object. pub fn set_prototype_of(_: &Value, args: &[Value], _: &mut Context) -> Result { let obj = args.get(0).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) } diff --git a/boa/src/context.rs b/boa/src/context.rs index 6839b21c6c..1eacbfacee 100644 --- a/boa/src/context.rs +++ b/boa/src/context.rs @@ -333,13 +333,16 @@ impl Context { .expect("Could not get global object"), )); array.set_data(ObjectData::Array); - array.as_object_mut().expect("object").set_prototype( - self.realm() - .environment - .get_binding_value("Array") - .expect("Array was not initialized") - .get_field(PROTOTYPE), - ); + array + .as_object_mut() + .expect("object") + .set_prototype_instance( + self.realm() + .environment + .get_binding_value("Array") + .expect("Array was not initialized") + .get_field(PROTOTYPE), + ); array.set_field("0", key); array.set_field("1", value); array.set_field("length", Value::from(2)); diff --git a/boa/src/exec/tests.rs b/boa/src/exec/tests.rs index 74c9dd0954..246fbe6c59 100644 --- a/boa/src/exec/tests.rs +++ b/boa/src/exec/tests.rs @@ -759,7 +759,7 @@ mod in_operator { let bar_obj = bar_val.as_object().unwrap(); let foo_val = forward_val(&mut engine, "Foo").unwrap(); assert!(bar_obj - .prototype() + .prototype_instance() .strict_equals(&foo_val.get_field("prototype"))); } } diff --git a/boa/src/object/mod.rs b/boa/src/object/mod.rs index 8c47b4ca44..667a65ed37 100644 --- a/boa/src/object/mod.rs +++ b/boa/src/object/mod.rs @@ -392,11 +392,11 @@ impl Object { matches!(self.data, ObjectData::Ordinary) } - pub fn prototype(&self) -> &Value { + pub fn prototype_instance(&self) -> &Value { &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()); self.prototype = prototype } diff --git a/boa/src/value/display.rs b/boa/src/value/display.rs index a4509be0aa..9fc535faaf 100644 --- a/boa/src/value/display.rs +++ b/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) => { { let object = $obj.borrow(); - if object.prototype().is_object() { + if object.prototype_instance().is_object() { vec![format!( "{:>width$}: {}", "__proto__", - $display_fn(object.prototype(), $encounters, $indent.wrapping_add(4), true), + $display_fn(object.prototype_instance(), $encounters, $indent.wrapping_add(4), true), width = $indent, )] } else { vec![format!( "{:>width$}: {}", "__proto__", - object.prototype().display(), + object.prototype_instance().display(), width = $indent, )] } diff --git a/boa/src/value/mod.rs b/boa/src/value/mod.rs index bbee9fd2df..b243173e8e 100644 --- a/boa/src/value/mod.rs +++ b/boa/src/value/mod.rs @@ -168,7 +168,7 @@ impl Value { pub fn new_object_from_prototype(proto: Value, data: ObjectData) -> Self { let mut object = Object::default(); object.data = data; - object.set_prototype(proto); + object.set_prototype_instance(proto); Self::object(object) } @@ -472,7 +472,7 @@ impl Value { return Some(property); } - object.prototype().get_property(key) + object.prototype_instance().get_property(key) } _ => None, }