Skip to content

Commit

Permalink
add Object.prototype.isPrototypeOf and Object.getPrototypeOf
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Dec 27, 2023
1 parent 5f16bec commit f61eeaa
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 3 deletions.
2 changes: 2 additions & 0 deletions crates/dash_middle/src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ pub mod sym {
isConcatSpreadable,
zero: "0",
one: "1",
getPrototypeOf,
isPrototypeOf
}
]
}
Expand Down
27 changes: 26 additions & 1 deletion crates/dash_vm/src/js_std/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::value::function::native::CallContext;
use crate::value::object::{NamedObject, Object, PropertyKey, PropertyValue};
use crate::value::ops::conversions::ValueConversion;
use crate::value::root_ext::RootErrExt;
use crate::value::{Root, Value, ValueContext};
use crate::value::{Root, Typeof, Value, ValueContext};

pub fn constructor(cx: CallContext) -> Result<Value, Value> {
match cx.args.first() {
Expand Down Expand Up @@ -190,3 +190,28 @@ pub fn entries(cx: CallContext) -> Result<Value, Value> {
let entries = Array::from_vec(cx.scope, entries);
Ok(Value::Object(cx.scope.register(entries)))
}

pub fn get_prototype_of(cx: CallContext) -> Result<Value, Value> {
let obj = cx.args.first().unwrap_or_undefined().to_object(cx.scope)?;
obj.get_prototype(cx.scope)
}

pub fn is_prototype_of(cx: CallContext) -> Result<Value, Value> {
let target_proto = Value::Object(cx.this.to_object(cx.scope)?);
let mut this_proto = cx.args.first().unwrap_or_undefined();
if this_proto.type_of() != Typeof::Object {
return Ok(Value::Boolean(false));
}

loop {
if this_proto == target_proto {
return Ok(Value::Boolean(true));
}

this_proto = match this_proto {
Value::Object(obj) => obj.get_prototype(cx.scope)?,
Value::External(obj) => obj.inner.get_prototype(cx.scope)?,
_ => return Ok(Value::Boolean(false)),
};
}
}
2 changes: 2 additions & 0 deletions crates/dash_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ impl Vm {
(sym::defineProperty, scope.statics.object_define_property.clone()),
(sym::entries, scope.statics.object_entries.clone()),
(sym::assign, scope.statics.object_assign.clone()),
(sym::getPrototypeOf, scope.statics.object_get_prototype_of.clone()),
],
[],
[],
Expand All @@ -249,6 +250,7 @@ impl Vm {
[
(sym::toString, scope.statics.object_to_string.clone()),
(sym::hasOwnProperty, scope.statics.object_has_own_property.clone()),
(sym::isPrototypeOf, scope.statics.object_is_prototype_of.clone()),
],
[],
[],
Expand Down
4 changes: 4 additions & 0 deletions crates/dash_vm/src/statics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ pub struct Statics {
pub object_define_property: Handle<dyn Object>,
pub object_assign: Handle<dyn Object>,
pub object_entries: Handle<dyn Object>,
pub object_get_prototype_of: Handle<dyn Object>,
pub object_is_prototype_of: Handle<dyn Object>,
pub number_ctor: Handle<dyn Object>,
pub number_prototype: Handle<dyn Object>,
pub number_tostring: Handle<dyn Object>,
Expand Down Expand Up @@ -288,6 +290,8 @@ impl Statics {
object_define_property: function(gc, sym::defineProperty, js_std::object::define_property),
object_assign: function(gc, sym::assign, js_std::object::assign),
object_entries: function(gc, sym::entries, js_std::object::entries),
object_get_prototype_of: function(gc, sym::getPrototypeOf, js_std::object::get_prototype_of),
object_is_prototype_of: function(gc, sym::isPrototypeOf, js_std::object::is_prototype_of),
number_ctor: function(gc, sym::Number, js_std::number::constructor),
number_prototype: builtin_object(gc, BoxedNumber::with_obj(0.0, NamedObject::null())),
number_tostring: function(gc, sym::toString, js_std::number::to_string),
Expand Down
1 change: 0 additions & 1 deletion crates/dash_vm/src/test/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::ptr;
use std::rc::Rc;

use dash_middle::interner::sym;
use dash_optimizer::OptLevel;
Expand Down
2 changes: 1 addition & 1 deletion crates/dash_vm/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ impl Value {
}
}

#[derive(Debug)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Typeof {
Undefined,
Object,
Expand Down

0 comments on commit f61eeaa

Please sign in to comment.