Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Sep 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions boa/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion boa/src/builtins/boolean/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
));
}
4 changes: 2 additions & 2 deletions boa/src/builtins/json/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions boa/src/builtins/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@ impl Object {
/// Get the `prototype` of an object.
pub fn get_prototype_of(_: &Value, args: &[Value], _: &mut Context) -> Result<Value> {
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<Value> {
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)
}

Expand Down
17 changes: 10 additions & 7 deletions boa/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion boa/src/exec/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
}
}
Expand Down
4 changes: 2 additions & 2 deletions boa/src/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions boa/src/value/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)]
}
Expand Down
4 changes: 2 additions & 2 deletions boa/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -472,7 +472,7 @@ impl Value {
return Some(property);
}

object.prototype().get_property(key)
object.prototype_instance().get_property(key)
}
_ => None,
}
Expand Down