Add proto chain inspection to the object interface.

This commit is contained in:
David Wendt 2019-10-25 18:02:47 -04:00
parent 0e59e1c961
commit 983c0fb200
3 changed files with 13 additions and 11 deletions

View File

@ -105,19 +105,14 @@ fn is_prototype_of<'gc>(
Ok(ob) => ob, Ok(ob) => ob,
Err(_) => return Ok(Value::Bool(false).into()), Err(_) => return Ok(Value::Bool(false).into()),
}; };
let mut proto = ob.read().as_script_object().unwrap().prototype().cloned(); let mut proto = ob.read().proto();
while let Some(proto_ob) = proto { while let Some(proto_ob) = proto {
if GcCell::ptr_eq(this, proto_ob) { if GcCell::ptr_eq(this, proto_ob) {
return Ok(Value::Bool(true).into()); return Ok(Value::Bool(true).into());
} }
proto = proto_ob proto = proto_ob.read().proto();
.read()
.as_script_object()
.unwrap()
.prototype()
.cloned();
} }
Ok(Value::Bool(false).into()) Ok(Value::Bool(false).into())

View File

@ -75,6 +75,13 @@ pub trait Object<'gc>: 'gc + Collect + Debug {
/// Returns false if the property cannot be deleted. /// Returns false if the property cannot be deleted.
fn delete(&mut self, name: &str) -> bool; fn delete(&mut self, name: &str) -> bool;
/// Retrieve the `__proto__` of a given object.
///
/// The proto is another object used to resolve methods across a class of
/// multiple objects. It should also be accessible as `__proto__` from
/// `get`.
fn proto(&self) -> Option<ObjectCell<'gc>>;
/// Checks if the object has a given named property. /// Checks if the object has a given named property.
fn has_property(&self, name: &str) -> bool; fn has_property(&self, name: &str) -> bool;

View File

@ -204,10 +204,6 @@ impl<'gc> ScriptObject<'gc> {
self.prototype = Some(prototype); self.prototype = Some(prototype);
} }
pub fn prototype(&self) -> Option<&ObjectCell<'gc>> {
self.prototype.as_ref()
}
pub fn set_type_of(&mut self, type_of: &'static str) { pub fn set_type_of(&mut self, type_of: &'static str) {
self.type_of = type_of; self.type_of = type_of;
} }
@ -332,6 +328,10 @@ impl<'gc> Object<'gc> for ScriptObject<'gc> {
false false
} }
fn proto(&self) -> Option<ObjectCell<'gc>> {
self.prototype
}
/// Checks if the object has a given named property. /// Checks if the object has a given named property.
fn has_property(&self, name: &str) -> bool { fn has_property(&self, name: &str) -> bool {
self.has_own_property(name) self.has_own_property(name)