Skip to content

Commit

Permalink
SSO function call arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Dec 29, 2024
1 parent be617d6 commit 39e42fe
Show file tree
Hide file tree
Showing 35 changed files with 261 additions and 146 deletions.
18 changes: 9 additions & 9 deletions crates/dash_node_impl/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use dash_rt::typemap::Key;
use dash_vm::frame::This;
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::args::CallArgs;
use dash_vm::value::function::native::{CallContext, register_native_fn};
use dash_vm::value::function::{Function, FunctionKind};
use dash_vm::value::object::{NamedObject, Object, PropertyValue};
use dash_vm::value::ops::conversions::ValueConversion;
Expand Down Expand Up @@ -65,13 +66,10 @@ pub fn init_module(sc: &mut LocalScope<'_>) -> Result<Value, Value> {
sc.register(event_emitter_ctor)
};

State::from_vm_mut(sc).store.insert(
EventsKey,
EventsState {
event_emitter_constructor: event_emitter_ctor,
event_emitter_prototype,
},
);
State::from_vm_mut(sc).store.insert(EventsKey, EventsState {
event_emitter_constructor: event_emitter_ctor,
event_emitter_prototype,
});

event_emitter_ctor.set_property(
sc,
Expand Down Expand Up @@ -159,7 +157,9 @@ fn emit(cx: CallContext) -> Result<Value, Value> {
let mut did_emit = false;
if let Some(handlers) = this.handlers.borrow().get(&name.sym()) {
for handler in handlers {
handler.apply(sc, This::Bound(cx.this), args.to_owned()).root_err(sc)?;
handler
.apply(sc, This::Bound(cx.this), CallArgs::from(args))
.root_err(sc)?;
did_emit = true;
}
}
Expand Down
11 changes: 8 additions & 3 deletions crates/dash_node_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use dash_vm::frame::This;
use dash_vm::gc::ObjectId;
use dash_vm::localscope::LocalScope;
use dash_vm::value::array::Array;
use dash_vm::value::function::args::CallArgs;
use dash_vm::value::object::{NamedObject, Object, PropertyValue};
use dash_vm::value::{Root, Unpack, Unrooted, Value, ValueKind};
use dash_vm::{Vm, delegate, extract, throw};
Expand Down Expand Up @@ -230,8 +231,12 @@ fn execute_node_module(

let dirname = Value::string(scope.intern(dir_path.to_str().expect("invalid utf-8 path")).into());
let filename = Value::string(scope.intern(file_path.to_str().expect("invalid utf-8 path")).into());
fun.apply(scope, This::Default, vec![exports, module, require, dirname, filename])
.map_err(|err| (EvalError::Exception(err), code))?;
fun.apply(
scope,
This::Default,
[exports, module, require, dirname, filename].into(),
)
.map_err(|err| (EvalError::Exception(err), code))?;

Ok(module)
}
Expand Down Expand Up @@ -278,7 +283,7 @@ impl Object for RequireFunction {
scope: &mut LocalScope,
_callee: dash_vm::gc::ObjectId,
_this: This,
args: Vec<Value>,
args: CallArgs,
) -> Result<Unrooted, Unrooted> {
let Some(ValueKind::String(raw_arg)) = args.first().unpack() else {
throw!(scope, Error, "require() expects a string argument");
Expand Down
17 changes: 7 additions & 10 deletions crates/dash_node_impl/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use dash_middle::interner::sym;
use dash_vm::frame::This;
use dash_vm::localscope::LocalScope;
use dash_vm::throw;
use dash_vm::value::function::native::{register_native_fn, CallContext};
use dash_vm::value::function::args::CallArgs;
use dash_vm::value::function::native::{CallContext, register_native_fn};
use dash_vm::value::object::{NamedObject, Object, PropertyDataDescriptor, PropertyValue, PropertyValueKind};
use dash_vm::value::{Root, Typeof, Value, ValueContext};

Expand Down Expand Up @@ -39,17 +40,13 @@ fn inherits(cx: CallContext) -> Result<Value, Value> {
}

let super_inst = super_ctor
.construct(cx.scope, This::Default, Vec::new())
.construct(cx.scope, This::Default, CallArgs::empty())
.root(cx.scope)?;

super_inst.set_property(
cx.scope,
sym::constructor.into(),
PropertyValue {
kind: PropertyValueKind::Static(ctor),
descriptor: PropertyDataDescriptor::WRITABLE | PropertyDataDescriptor::CONFIGURABLE,
},
)?;
super_inst.set_property(cx.scope, sym::constructor.into(), PropertyValue {
kind: PropertyValueKind::Static(ctor),
descriptor: PropertyDataDescriptor::WRITABLE | PropertyDataDescriptor::CONFIGURABLE,
})?;

ctor.set_property(
cx.scope,
Expand Down
2 changes: 1 addition & 1 deletion crates/dash_rt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ where
Ok(ok) => (ok, PromiseAction::Resolve),
Err(err) => (err, PromiseAction::Reject),
};
scope.drive_promise(action, promise, vec![arg]);
scope.drive_promise(action, promise, [arg].into());
scope.process_async_tasks();
})));
});
Expand Down
4 changes: 2 additions & 2 deletions crates/dash_rt_fetch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fn fetch(cx: CallContext) -> Result<Value, Value> {
}
};

sc.drive_promise(action, promise, vec![req]);
sc.drive_promise(action, promise, [req].into());
sc.process_async_tasks();
})));
});
Expand Down Expand Up @@ -139,7 +139,7 @@ fn http_response_text(cx: CallContext) -> Result<Value, Value> {
}
};

sc.drive_promise(action, promise, vec![value]);
sc.drive_promise(action, promise, [value].into());
sc.process_async_tasks();
})));
});
Expand Down
2 changes: 1 addition & 1 deletion crates/dash_rt_http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub fn listen(cx: CallContext) -> Result<Value, Value> {

let ctx = Value::object(scope.register(ctx));

if let Err(err) = cb.apply(&mut scope, This::Default, vec![ctx]).root_err(&mut scope) {
if let Err(err) = cb.apply(&mut scope, This::Default, [ctx].into()).root_err(&mut scope) {
match err.to_js_string(&mut scope) {
Ok(err) => eprintln!("Unhandled exception in HTTP handler! {}", err.res(&scope)),
Err(..) => eprintln!("Unhandled exception in exception toString method in HTTP handler!"),
Expand Down
7 changes: 4 additions & 3 deletions crates/dash_rt_net/src/listener/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use dash_vm::gc::trace::{Trace, TraceCtxt};
use dash_vm::js_std::receiver_t;
use dash_vm::localscope::LocalScope;
use dash_vm::value::arraybuffer::ArrayBuffer;
use dash_vm::value::function::args::CallArgs;
use dash_vm::value::function::native::CallContext;
use dash_vm::value::function::{Function, FunctionKind};
use dash_vm::value::object::{NamedObject, Object, PropertyValue};
Expand Down Expand Up @@ -70,7 +71,7 @@ impl Object for TcpListenerConstructor {
scope: &mut dash_vm::localscope::LocalScope,
_callee: dash_vm::gc::ObjectId,
_this: This,
_args: Vec<dash_vm::value::Value>,
_args: CallArgs,
) -> Result<dash_vm::value::Unrooted, dash_vm::value::Unrooted> {
throw!(scope, Error, "TcpListener should be called as a constructor")
}
Expand All @@ -80,7 +81,7 @@ impl Object for TcpListenerConstructor {
scope: &mut dash_vm::localscope::LocalScope,
_callee: dash_vm::gc::ObjectId,
_this: This,
args: Vec<Value>,
args: CallArgs,
new_target: ObjectId,
) -> Result<Unrooted, Unrooted> {
let Some(value) = args.first() else {
Expand Down Expand Up @@ -129,7 +130,7 @@ impl Object for TcpListenerConstructor {
let stream_handle = TcpStreamHandle::new(&mut scope, writer_tx, reader_tx).unwrap();
let stream_handle = scope.register(stream_handle);

scope.drive_promise(PromiseAction::Resolve, promise, vec![Value::object(stream_handle)]);
scope.drive_promise(PromiseAction::Resolve, promise, [Value::object(stream_handle)].into());
scope.process_async_tasks();
})));
}
Expand Down
7 changes: 4 additions & 3 deletions crates/dash_rt_timers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use dash_vm::frame::This;
use dash_vm::gc::persistent::Persistent;
use dash_vm::localscope::LocalScope;
use dash_vm::throw;
use dash_vm::value::function::native::{register_native_fn, CallContext};
use dash_vm::value::function::args::CallArgs;
use dash_vm::value::function::native::{CallContext, register_native_fn};
use dash_vm::value::object::{NamedObject, Object, PropertyValue};
use dash_vm::value::ops::conversions::ValueConversion;
use dash_vm::value::string::JsString;
Expand Down Expand Up @@ -82,7 +83,7 @@ fn set_timeout(cx: CallContext) -> Result<Value, Value> {
let mut sc = rt.vm_mut().scope();
let callback = callback.get();

if let Err(err) = callback.apply(&mut sc, This::Default, Vec::new()) {
if let Err(err) = callback.apply(&mut sc, This::Default, CallArgs::empty()) {
eprintln!("Unhandled error in timer callback: {err:?}");
}

Expand All @@ -109,7 +110,7 @@ fn set_immediate(cx: CallContext) -> Result<Value, Value> {
let callback = callback.get();
let mut sc = rt.vm_mut().scope();

if let Err(err) = callback.apply(&mut sc, This::Default, Vec::new()) {
if let Err(err) = callback.apply(&mut sc, This::Default, CallArgs::empty()) {
eprintln!("Unhandled error in timer callback: {err:?}");
}
})));
Expand Down
13 changes: 7 additions & 6 deletions crates/dash_vm/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ mod handlers {
use crate::util::unlikely;
use crate::value::array::table::ArrayTable;
use crate::value::array::{Array, ArrayIterator};
use crate::value::function::args::CallArgs;
use crate::value::function::r#async::AsyncFunction;
use crate::value::function::closure::Closure;
use crate::value::function::generator::GeneratorFunction;
Expand Down Expand Up @@ -1129,7 +1130,7 @@ mod handlers {
call_ip: u16,
) -> Result<Option<HandleResult>, Unrooted> {
let args = {
let mut args = Vec::with_capacity(argc);
let mut args = SmallVec::with_capacity(argc);

let len = cx.fetch_and_inc_ip();
let spread_indices: SmallVec<[_; 4]> = (0..len).map(|_| cx.fetch_and_inc_ip()).collect();
Expand Down Expand Up @@ -1167,12 +1168,12 @@ mod handlers {
cx.scope.add_many(&args);

let ret = match function_call_kind {
FunctionCallKind::Constructor => callee.construct(&mut cx.scope, this, args)?,
FunctionCallKind::Constructor => callee.construct(&mut cx.scope, this, args.into())?,
FunctionCallKind::Super => {
let new_target = cx.active_frame().new_target().unwrap();
callee.construct_with_target(&mut cx.scope, this, args, new_target)?
callee.construct_with_target(&mut cx.scope, this, args.into(), new_target)?
}
FunctionCallKind::Function => callee.apply_with_debug(&mut cx.scope, this, args, call_ip)?,
FunctionCallKind::Function => callee.apply_with_debug(&mut cx.scope, this, args.into(), call_ip)?,
};

// SAFETY: no need to root, we're directly pushing into the value stack which itself is a root
Expand Down Expand Up @@ -2034,7 +2035,7 @@ mod handlers {
let iterable = value
.get_property(&mut cx, PropertyKey::Symbol(symbol_iterator))?
.root(&mut cx.scope);
let iterator = iterable.apply(&mut cx, This::Bound(value), Vec::new())?;
let iterator = iterable.apply(&mut cx, This::Bound(value), CallArgs::empty())?;
cx.push_stack(iterator);
Ok(None)
}
Expand Down Expand Up @@ -2258,7 +2259,7 @@ mod handlers {
macro_rules! fn_call {
($fun:ident, $k:expr, $v:expr) => {{
let argc = cx.fetch_and_inc_ip();
let args = cx.pop_stack_many(argc.into()).collect::<Vec<_>>();
let args = cx.pop_stack_many(argc.into()).collect::<CallArgs>();
let fun = cx.statics.$fun.clone();

if unlikely(!cx.builtins_purity()) {
Expand Down
5 changes: 3 additions & 2 deletions crates/dash_vm/src/gc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use trace::TraceCtxt;
use crate::Vm;
use crate::frame::This;
use crate::localscope::LocalScope;
use crate::value::function::args::CallArgs;
use crate::value::object::{PropertyKey, PropertyValue};
use crate::value::primitive::InternalSlots;
use crate::value::{Typeof, Unrooted, Value};
Expand Down Expand Up @@ -40,9 +41,9 @@ pub struct ObjectVTable {
pub(crate) js_set_prototype: unsafe fn(*const (), &mut LocalScope<'_>, Value) -> Result<(), Value>,
pub(crate) js_get_prototype: unsafe fn(*const (), &mut LocalScope<'_>) -> Result<Value, Value>,
pub(crate) js_apply:
unsafe fn(*const (), &mut LocalScope<'_>, ObjectId, This, Vec<Value>) -> Result<Unrooted, Unrooted>,
unsafe fn(*const (), &mut LocalScope<'_>, ObjectId, This, CallArgs) -> Result<Unrooted, Unrooted>,
pub(crate) js_construct:
unsafe fn(*const (), &mut LocalScope<'_>, ObjectId, This, Vec<Value>, ObjectId) -> Result<Unrooted, Unrooted>,
unsafe fn(*const (), &mut LocalScope<'_>, ObjectId, This, CallArgs, ObjectId) -> Result<Unrooted, Unrooted>,
pub(crate) js_internal_slots: unsafe fn(*const (), &Vm) -> Option<*const dyn InternalSlots>,
pub(crate) js_extract_type_raw: unsafe fn(*const (), &Vm, TypeId) -> Option<NonNull<()>>,
pub(crate) js_own_keys: unsafe fn(*const (), sc: &mut LocalScope<'_>) -> Result<Vec<Value>, Value>,
Expand Down
Loading

0 comments on commit 39e42fe

Please sign in to comment.