Skip to content

Commit

Permalink
value packing
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Oct 13, 2024
1 parent 9cb0fd5 commit dc67edf
Show file tree
Hide file tree
Showing 70 changed files with 2,225 additions and 1,392 deletions.
4 changes: 2 additions & 2 deletions crates/dash_dlloader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ impl ModuleLoader for DllModule {
let load = sc.intern("load");
let load_sync = Function::new(sc, Some(load.into()), FunctionKind::Native(load_sync));
let load_sync = sc.register(load_sync);
object.set_property(sc, load.into(), PropertyValue::static_default(Value::Object(load_sync)))?;
object.set_property(sc, load.into(), PropertyValue::static_default(Value::object(load_sync)))?;

Ok(Some(Value::Object(sc.register(object))))
Ok(Some(Value::object(sc.register(object))))
}
}

Expand Down
4 changes: 4 additions & 0 deletions crates/dash_middle/src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,10 @@ impl Symbol {
self.0
}

pub fn from_raw(v: u32) -> Self {
Self(v)
}

pub fn is_keyword(self) -> bool {
#![allow(clippy::absurd_extreme_comparisons)]

Expand Down
20 changes: 10 additions & 10 deletions crates/dash_node_impl/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use dash_middle::interner::Symbol;
use dash_proc_macro::Trace;
use dash_rt::state::State;
use dash_rt::typemap::Key;
use dash_vm::gc::handle::Handle;
use dash_vm::gc::ObjectId;
use dash_vm::localscope::LocalScope;
use dash_vm::value::function::native::{register_native_fn, CallContext};
use dash_vm::value::function::{Function, FunctionKind};
use dash_vm::value::object::{NamedObject, Object, PropertyValue};
use dash_vm::value::ops::conversions::ValueConversion;
use dash_vm::value::root_ext::RootErrExt;
use dash_vm::value::Value;
use dash_vm::value::{Unpack, Value, ValueKind};
use dash_vm::{delegate, throw};
use rustc_hash::FxHashMap;

Expand Down Expand Up @@ -76,13 +76,13 @@ pub fn init_module(sc: &mut LocalScope<'_>) -> Result<Value, Value> {
PropertyValue::static_default(event_emitter_ctor.clone().into()),
)?;

Ok(Value::Object(event_emitter_ctor))
Ok(Value::object(event_emitter_ctor))
}

#[derive(Debug, Trace)]
pub struct EventEmitter {
object: NamedObject,
handlers: RefCell<FxHashMap<Symbol, Vec<Handle>>>,
handlers: RefCell<FxHashMap<Symbol, Vec<ObjectId>>>,
}

struct EventsKey;
Expand All @@ -92,8 +92,8 @@ impl Key for EventsKey {

#[derive(Debug, Trace)]
struct EventsState {
event_emitter_prototype: Handle,
event_emitter_constructor: Handle,
event_emitter_prototype: ObjectId,
event_emitter_constructor: ObjectId,
}

impl Object for EventEmitter {
Expand All @@ -115,10 +115,10 @@ fn on(cx: CallContext) -> Result<Value, Value> {
throw!(cx.scope, Error, "expected an event name and callback function");
};
let name = name.to_js_string(cx.scope)?;
let Value::Object(cb) = cb else {
let ValueKind::Object(cb) = cb.unpack() else {
throw!(cx.scope, Error, "expected callback to be a function")
};
let Some(this) = cx.this.downcast_ref::<EventEmitter>() else {
let Some(this) = cx.this.downcast_ref::<EventEmitter>(cx.scope) else {
throw!(cx.scope, TypeError, "on can only be called on EventEmitter instances")
};
match this.handlers.borrow_mut().entry(name.sym()) {
Expand All @@ -133,7 +133,7 @@ fn emit(cx: CallContext) -> Result<Value, Value> {
throw!(cx.scope, Error, "expected an event name");
};
let name = name.to_js_string(cx.scope)?;
let Some(this) = cx.this.downcast_ref::<EventEmitter>() else {
let Some(this) = cx.this.downcast_ref::<EventEmitter>(cx.scope) else {
throw!(cx.scope, TypeError, "on can only be called on EventEmitter instances")
};
let mut did_emit = false;
Expand All @@ -146,5 +146,5 @@ fn emit(cx: CallContext) -> Result<Value, Value> {
}
}

Ok(Value::Boolean(did_emit))
Ok(Value::boolean(did_emit))
}
31 changes: 15 additions & 16 deletions crates/dash_node_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ use dash_rt::format_value;
use dash_rt::runtime::Runtime;
use dash_rt::state::State;
use dash_vm::eval::EvalError;
use dash_vm::gc::handle::Handle;
use dash_vm::gc::ObjectId;
use dash_vm::localscope::LocalScope;
use dash_vm::value::array::Array;
use dash_vm::value::object::{NamedObject, Object, PropertyValue};
use dash_vm::value::{Root, Unrooted, Value};
use dash_vm::{delegate, throw};
use dash_vm::value::{Root, Unpack, Unrooted, Value, ValueKind};
use dash_vm::{delegate, throw, Vm};
use package::Package;
use rustc_hash::FxHashMap;
use state::Nodejs;
Expand Down Expand Up @@ -80,7 +80,7 @@ async fn run_inner_fallible(path: &str, opt: OptLevel, initial_gc_threshold: Opt
.set_property(
scope,
global_k.into(),
PropertyValue::static_default(Value::Object(global.clone())),
PropertyValue::static_default(Value::object(global)),
)
.unwrap();
let process = create_process_object(scope);
Expand Down Expand Up @@ -112,7 +112,7 @@ async fn run_inner_fallible(path: &str, opt: OptLevel, initial_gc_threshold: Opt
Ok(())
}

fn create_process_object(sc: &mut LocalScope<'_>) -> Handle {
fn create_process_object(sc: &mut LocalScope<'_>) -> ObjectId {
let obj = NamedObject::new(sc);
let env = NamedObject::new(sc);
let env = sc.register(env);
Expand All @@ -122,7 +122,7 @@ fn create_process_object(sc: &mut LocalScope<'_>) -> Handle {

let argv_k = sc.intern("argv");
let argv = env::args()
.map(|arg| PropertyValue::static_default(Value::String(sc.intern(arg).into())))
.map(|arg| PropertyValue::static_default(Value::string(sc.intern(arg).into())))
.collect::<Vec<_>>();
let argv = Array::from_vec(sc, argv);
let argv = sc.register(argv);
Expand All @@ -137,7 +137,7 @@ fn create_process_object(sc: &mut LocalScope<'_>) -> Handle {
.set_property(
sc,
dash_k.into(),
PropertyValue::static_default(Value::String(version.into())),
PropertyValue::static_default(Value::string(version.into())),
)
.unwrap();
let versions = sc.register(versions);
Expand Down Expand Up @@ -168,9 +168,9 @@ fn execute_node_module(
package: Rc<PackageState>,
) -> Result<Value, (EvalError, String)> {
debug!(?dir_path, ?file_path);
let exports = Value::Object(scope.register(NamedObject::new(scope)));
let module = Value::Object(scope.register(NamedObject::new(scope)));
let require = Value::Object(scope.register(RequireFunction {
let exports = Value::object(scope.register(NamedObject::new(scope)));
let module = Value::object(scope.register(NamedObject::new(scope)));
let require = Value::object(scope.register(RequireFunction {
current_dir: dir_path.to_owned(),
state: global_state.clone(),
package,
Expand Down Expand Up @@ -235,23 +235,22 @@ impl Object for RequireFunction {
own_keys
);

fn type_of(&self) -> dash_vm::value::Typeof {
fn type_of(&self, _: &Vm) -> dash_vm::value::Typeof {
dash_vm::value::Typeof::Function
}

fn apply(
&self,
scope: &mut LocalScope,
_callee: dash_vm::gc::handle::Handle,
_callee: dash_vm::gc::ObjectId,
_this: Value,
args: Vec<Value>,
) -> Result<Unrooted, Unrooted> {
let Some(Value::String(arg)) = args.first() else {
let Some(ValueKind::String(raw_arg)) = args.first().unpack() else {
throw!(scope, Error, "require() expects a string argument");
};
let exports = scope.intern("exports");
let raw_arg = arg;
let mut arg = arg.res(scope).to_owned();
let mut arg = raw_arg.res(scope).to_owned();
debug!(%arg, "require node module");

let is_path = matches!(arg.chars().next(), Some('.' | '/' | '~'));
Expand Down Expand Up @@ -295,7 +294,7 @@ impl Object for RequireFunction {
};

module.get_property(scope, exports.into())
} else if let Some(o) = native::load_native_module(scope, *raw_arg)? {
} else if let Some(o) = native::load_native_module(scope, raw_arg)? {
Ok(o.into())
} else {
// Resolve dependency in node_modules
Expand Down
4 changes: 2 additions & 2 deletions crates/dash_node_impl/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ pub fn load_native_module(sc: &mut LocalScope<'_>, arg: JsString) -> Result<Opti

fn init_dummy_empty_module(sc: &mut LocalScope<'_>) -> Result<Value, Value> {
let exports = NamedObject::new(sc);
Ok(Value::Object(sc.register(exports)))
Ok(Value::object(sc.register(exports)))
}

fn init_stream(sc: &mut LocalScope<'_>) -> Result<Value, Value> {
let exports = NamedObject::new(sc);
let readable = sc.intern("Readable");
let readable_fn = register_native_fn(sc, readable, |_sc| Ok(Value::undefined()));
exports.set_property(sc, readable.into(), PropertyValue::static_default(readable_fn.into()))?;
Ok(Value::Object(sc.register(exports)))
Ok(Value::object(sc.register(exports)))
}
2 changes: 1 addition & 1 deletion crates/dash_node_impl/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn parse_path(cx: CallContext) -> Result<Value, Value> {
object.set_property(
cx.scope,
dir_sym.into(),
PropertyValue::static_default(Value::String(dir.into())),
PropertyValue::static_default(Value::string(dir.into())),
)?;
Ok(cx.scope.register(object).into())
}
5 changes: 2 additions & 3 deletions crates/dash_rt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ where
};

let (promise_id, rt) = {
let promise = promise.clone();
let state = State::from_vm_mut(cx.scope);
let pid = state.add_pending_promise(promise);
let rt = state.rt_handle();
Expand All @@ -50,7 +49,7 @@ where
event_tx.send(EventMessage::ScheduleCallback(Box::new(move |rt| {
let promise = State::from_vm_mut(rt.vm_mut()).take_promise(promise_id);
let mut scope = rt.vm_mut().scope();
let promise = promise.as_any().downcast_ref::<Promise>().unwrap();
let promise = promise.as_any(&scope).downcast_ref::<Promise>().unwrap();

let data = convert(&mut scope, data);

Expand All @@ -63,7 +62,7 @@ where
})));
});

Ok(Value::Object(promise))
Ok(Value::object(promise))
}

pub fn format_value<'s>(value: Value, scope: &'s mut LocalScope) -> Result<&'s str, Value> {
Expand Down
10 changes: 5 additions & 5 deletions crates/dash_rt/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::cell::RefCell;
use std::rc::Rc;
use std::sync::atomic::{AtomicU64, Ordering};

use dash_vm::gc::handle::Handle;
use dash_vm::gc::trace::Trace;
use dash_vm::gc::ObjectId;
use dash_vm::Vm;
use rustc_hash::FxHashMap;

Expand All @@ -17,7 +17,7 @@ pub struct State {
tx: EventSender,
root_module: Rc<RefCell<Option<Box<dyn ModuleLoader>>>>,
pub tasks: TaskIds,
promises: FxHashMap<u64, Handle>,
promises: FxHashMap<u64, ObjectId>,
pub store: TypeMap,
}
unsafe impl Trace for State {
Expand Down Expand Up @@ -86,19 +86,19 @@ impl State {
self.rt.clone()
}

pub fn add_pending_promise(&mut self, promise: Handle) -> u64 {
pub fn add_pending_promise(&mut self, promise: ObjectId) -> u64 {
static NEXT_PROMISE_ID: AtomicU64 = AtomicU64::new(0);
let id = NEXT_PROMISE_ID.fetch_add(1, Ordering::Relaxed);
self.promises.insert(id, promise);
id
}

pub fn take_promise(&mut self, id: u64) -> Handle {
pub fn take_promise(&mut self, id: u64) -> ObjectId {
self.try_take_promise(id)
.expect("Attempted to take a promise that was already taken")
}

pub fn try_take_promise(&mut self, id: u64) -> Option<Handle> {
pub fn try_take_promise(&mut self, id: u64) -> Option<ObjectId> {
self.promises.remove(&id)
}
}
Loading

0 comments on commit dc67edf

Please sign in to comment.