Skip to content

Commit

Permalink
implement Object.setPrototypeOf and add global
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed May 5, 2024
1 parent c581dce commit 44a0255
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/dash_middle/src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ pub mod sym {
zero: "0",
one: "1",
getPrototypeOf,
setPrototypeOf,
isPrototypeOf,
arguments,
propertyIsEnumerable,
Expand Down
11 changes: 11 additions & 0 deletions crates/dash_node_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ async fn run_inner_fallible(path: &str, opt: OptLevel, initial_gc_threshold: Opt
State::from_vm_mut(rt.vm_mut()).store.insert(Nodejs, state);

rt.vm_mut().with_scope(|scope| {
let global = scope.global();
let global_k = scope.intern("global");
global
.clone()
.set_property(
scope,
global_k.into(),
PropertyValue::static_default(Value::Object(global)),
)
.unwrap();

anyhow::Ok(
execute_node_module(scope, path, path, &entry, opt, global_state, Rc::new(package_state)).map_err(
|err| match err {
Expand Down
7 changes: 7 additions & 0 deletions crates/dash_vm/src/js_std/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ pub fn get_prototype_of(cx: CallContext) -> Result<Value, Value> {
obj.get_prototype(cx.scope)
}

pub fn set_prototype_of(cx: CallContext) -> Result<Value, Value> {
let obj = cx.args.first().unwrap_or_undefined().to_object(cx.scope)?;
let target = cx.args.get(1).unwrap_or_undefined();
obj.set_prototype(cx.scope, target)?;
Ok(Value::Object(obj))
}

pub fn is_prototype_of(cx: CallContext) -> Result<Value, Value> {
let target_proto = Value::Object(cx.this.to_object(cx.scope)?);
let this_proto = cx.args.first().unwrap_or_undefined();
Expand Down
1 change: 1 addition & 0 deletions crates/dash_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ impl Vm {
(sym::entries, scope.statics.object_entries.clone()),
(sym::assign, scope.statics.object_assign.clone()),
(sym::getPrototypeOf, scope.statics.object_get_prototype_of.clone()),
(sym::setPrototypeOf, scope.statics.object_set_prototype_of.clone()),
],
[],
[],
Expand Down
2 changes: 2 additions & 0 deletions crates/dash_vm/src/statics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub struct Statics {
pub object_assign: Handle,
pub object_entries: Handle,
pub object_get_prototype_of: Handle,
pub object_set_prototype_of: Handle,
pub object_is_prototype_of: Handle,
pub object_property_is_enumerable: Handle,
pub number_ctor: Handle,
Expand Down Expand Up @@ -298,6 +299,7 @@ impl Statics {
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_set_prototype_of: function(gc, sym::setPrototypeOf, js_std::object::set_prototype_of),
object_is_prototype_of: function(gc, sym::isPrototypeOf, js_std::object::is_prototype_of),
object_property_is_enumerable: function(
gc,
Expand Down

0 comments on commit 44a0255

Please sign in to comment.