Skip to content

Commit 7b76079

Browse files
committed
abi-optimize This enum
1 parent 81f4a70 commit 7b76079

34 files changed

Lines changed: 213 additions & 160 deletions

File tree

crates/dash_node_impl/src/events.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ use dash_middle::interner::Symbol;
66
use dash_proc_macro::Trace;
77
use dash_rt::state::State;
88
use dash_rt::typemap::Key;
9-
use dash_vm::frame::This;
109
use dash_vm::gc::ObjectId;
1110
use dash_vm::localscope::LocalScope;
1211
use dash_vm::value::function::args::CallArgs;
1312
use dash_vm::value::function::native::{CallContext, register_native_fn};
1413
use dash_vm::value::function::{Function, FunctionKind};
15-
use dash_vm::value::object::{Object, OrdObject, PropertyValue};
14+
use dash_vm::value::object::{Object, OrdObject, PropertyValue, This};
1615
use dash_vm::value::ops::conversions::ValueConversion;
1716
use dash_vm::value::propertykey::{PropertyKey, ToPropertyKey};
1817
use dash_vm::value::root_ext::RootErrExt;
@@ -161,7 +160,7 @@ fn emit(cx: CallContext) -> Result<Value, Value> {
161160
if let Some(handlers) = this.handlers.borrow().get(&name.sym()) {
162161
for handler in handlers {
163162
handler
164-
.apply(This::Bound(cx.this), CallArgs::from(args), sc)
163+
.apply(This::bound(cx.this), CallArgs::from(args), sc)
165164
.root_err(sc)?;
166165
did_emit = true;
167166
}

crates/dash_node_impl/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ use dash_rt::format_value;
1313
use dash_rt::runtime::Runtime;
1414
use dash_rt::state::State;
1515
use dash_vm::eval::EvalError;
16-
use dash_vm::frame::This;
1716
use dash_vm::gc::ObjectId;
1817
use dash_vm::localscope::LocalScope;
1918
use dash_vm::value::array::Array;
2019
use dash_vm::value::function::args::CallArgs;
21-
use dash_vm::value::object::{OrdObject, Object, PropertyValue};
20+
use dash_vm::value::object::{Object, OrdObject, PropertyValue, This};
2221
use dash_vm::value::propertykey::ToPropertyKey;
2322
use dash_vm::value::{Root, Unpack, Unrooted, Value, ValueKind};
2423
use dash_vm::{Vm, delegate, extract, throw};
@@ -244,7 +243,7 @@ fn execute_node_module(
244243
let dirname = Value::string(scope.intern(dir_path.to_str().expect("invalid utf-8 path")).into());
245244
let filename = Value::string(scope.intern(file_path.to_str().expect("invalid utf-8 path")).into());
246245
fun.apply(
247-
This::Default,
246+
This::default(),
248247
[exports, module, require, dirname, filename].into(),
249248
scope,
250249
)

crates/dash_node_impl/src/util.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use dash_middle::interner::sym;
2-
use dash_vm::frame::This;
32
use dash_vm::localscope::LocalScope;
43
use dash_vm::throw;
54
use dash_vm::value::function::args::CallArgs;
65
use dash_vm::value::function::native::{CallContext, register_native_fn};
7-
use dash_vm::value::object::{OrdObject, Object, PropertyDataDescriptor, PropertyValue, PropertyValueKind};
6+
use dash_vm::value::object::{Object, OrdObject, PropertyDataDescriptor, PropertyValue, PropertyValueKind, This};
87
use dash_vm::value::propertykey::ToPropertyKey;
98
use dash_vm::value::{Root, Typeof, Value, ValueContext};
109

@@ -49,7 +48,7 @@ fn inherits(cx: CallContext) -> Result<Value, Value> {
4948
}
5049

5150
let super_inst = super_ctor
52-
.construct(This::Default, CallArgs::empty(), cx.scope)
51+
.construct(This::default(), CallArgs::empty(), cx.scope)
5352
.root(cx.scope)?;
5453

5554
super_inst.set_property(

crates/dash_rt/src/inspect.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::fmt::Write;
22

33
use dash_middle::interner::{Symbol, sym};
44

5-
use dash_vm::frame::This;
65
use dash_vm::localscope::LocalScope;
76
use dash_vm::util::intern_f64;
87
use dash_vm::value::array::{Array, ArrayIterator};
@@ -12,7 +11,7 @@ use dash_vm::value::propertykey::{PropertyKey, ToPropertyKey};
1211
use dash_vm::value::typedarray::TypedArray;
1312
use dash_vm::value::{Typeof, Unpack, ValueKind};
1413

15-
use dash_vm::value::object::{Object, PropertyDataDescriptor, PropertyValueKind};
14+
use dash_vm::value::object::{Object, PropertyDataDescriptor, PropertyValueKind, This};
1615
use dash_vm::value::ops::conversions::ValueConversion;
1716
use dash_vm::value::primitive::Number;
1817
use dash_vm::value::root_ext::RootErrExt;
@@ -218,7 +217,7 @@ fn inspect_inner_into(
218217
if options.invoke_getters {
219218
colored(out, options, GREY, |s| *s += "(computed) ");
220219
inspect_inner_into(
221-
property_value.get_or_apply(scope, This::Bound(value)).root(scope)?,
220+
property_value.get_or_apply(scope, This::bound(value)).root(scope)?,
222221
scope,
223222
options,
224223
depth + 1,

crates/dash_rt_http/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ use dash_middle::util::{SharedOnce, ThreadSafeStorage};
77
use dash_rt::event::EventMessage;
88
use dash_rt::module::ModuleLoader;
99
use dash_rt::state::State;
10-
use dash_vm::frame::This;
1110
use dash_vm::gc::persistent::Persistent;
1211
use dash_vm::gc::trace::{Trace, TraceCtxt};
1312
use dash_vm::js_std::receiver_t;
1413
use dash_vm::localscope::LocalScope;
1514
use dash_vm::value::function::native::CallContext;
1615
use dash_vm::value::function::{Function, FunctionKind};
17-
use dash_vm::value::object::{Object, OrdObject, PropertyValue};
16+
use dash_vm::value::object::{Object, OrdObject, PropertyValue, This};
1817
use dash_vm::value::ops::conversions::ValueConversion;
1918
use dash_vm::value::propertykey::ToPropertyKey;
2019
use dash_vm::value::root_ext::RootErrExt;
@@ -105,7 +104,7 @@ pub fn listen(cx: CallContext) -> Result<Value, Value> {
105104

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

108-
if let Err(err) = cb.apply(This::Default, [ctx].into(), &mut scope).root_err(&mut scope) {
107+
if let Err(err) = cb.apply(This::default(), [ctx].into(), &mut scope).root_err(&mut scope) {
109108
match err.to_js_string(&mut scope) {
110109
Ok(err) => eprintln!("Unhandled exception in HTTP handler! {}", err.res(&scope)),
111110
Err(..) => eprintln!("Unhandled exception in exception toString method in HTTP handler!"),

crates/dash_rt_net/src/listener/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use dash_proc_macro::Trace;
44
use dash_rt::event::EventMessage;
55
use dash_rt::state::State;
66
use dash_rt::wrap_async;
7-
use dash_vm::frame::This;
87
use dash_vm::gc::ObjectId;
98
use dash_vm::gc::trace::{Trace, TraceCtxt};
109
use dash_vm::js_std::receiver_t;
@@ -13,7 +12,7 @@ use dash_vm::value::arraybuffer::ArrayBuffer;
1312
use dash_vm::value::function::args::CallArgs;
1413
use dash_vm::value::function::native::CallContext;
1514
use dash_vm::value::function::{Function, FunctionKind};
16-
use dash_vm::value::object::{Object, OrdObject, PropertyValue};
15+
use dash_vm::value::object::{Object, OrdObject, PropertyValue, This};
1716
use dash_vm::value::ops::conversions::ValueConversion;
1817
use dash_vm::value::promise::Promise;
1918
use dash_vm::value::propertykey::{PropertyKey, ToPropertyKey};

crates/dash_rt_timers/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ use dash_middle::util::ThreadSafeStorage;
66
use dash_rt::event::EventMessage;
77
use dash_rt::module::ModuleLoader;
88
use dash_rt::state::State;
9-
use dash_vm::frame::This;
109
use dash_vm::gc::persistent::Persistent;
1110
use dash_vm::localscope::LocalScope;
1211
use dash_vm::throw;
1312
use dash_vm::value::function::args::CallArgs;
1413
use dash_vm::value::function::native::{CallContext, register_native_fn};
15-
use dash_vm::value::object::{OrdObject, Object, PropertyValue};
14+
use dash_vm::value::object::{Object, OrdObject, PropertyValue, This};
1615
use dash_vm::value::ops::conversions::ValueConversion;
1716
use dash_vm::value::propertykey::ToPropertyKey;
1817
use dash_vm::value::string::JsString;
@@ -84,7 +83,7 @@ fn set_timeout(cx: CallContext) -> Result<Value, Value> {
8483
let mut sc = rt.vm_mut().scope();
8584
let callback = callback.get();
8685

87-
if let Err(err) = callback.apply(This::Default, CallArgs::empty(), &mut sc) {
86+
if let Err(err) = callback.apply(This::default(), CallArgs::empty(), &mut sc) {
8887
eprintln!("Unhandled error in timer callback: {err:?}");
8988
}
9089

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

114-
if let Err(err) = callback.apply(This::Default, CallArgs::empty(), &mut sc) {
113+
if let Err(err) = callback.apply(This::default(), CallArgs::empty(), &mut sc) {
115114
eprintln!("Unhandled error in timer callback: {err:?}");
116115
}
117116
})));

crates/dash_vm/src/dispatch.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ mod handlers {
571571
use std::ops::{Add, ControlFlow, Div, Mul, Rem, Sub};
572572
use std::rc::Rc;
573573

574-
use crate::frame::{FrameState, This, TryBlock};
574+
use crate::frame::{FrameState, TryBlock};
575575
use crate::throw;
576576
use crate::util::unlikely;
577577
use crate::value::array::table::ArrayTable;
@@ -582,7 +582,7 @@ mod handlers {
582582
use crate::value::function::generator::GeneratorFunction;
583583
use crate::value::function::user::UserFunction;
584584
use crate::value::function::{Function, FunctionKind, adjust_stack_from_flat_call, this_for_new_target};
585-
use crate::value::object::{Object, OrdObject, PropertyValue, PropertyValueKind};
585+
use crate::value::object::{Object, OrdObject, PropertyValue, PropertyValueKind, This, ThisKind};
586586
use crate::value::ops::conversions::ValueConversion;
587587
use crate::value::ops::equality;
588588
use crate::value::primitive::Number;
@@ -930,7 +930,7 @@ mod handlers {
930930

931931
let value = match cx.global.clone().extract::<OrdObject>(&cx.scope) {
932932
Some(value) => match value.get_own_property_descriptor(name.to_key(&mut cx.scope), &mut cx.scope)? {
933-
Some(value) => value.kind().get_or_apply(&mut cx, This::Default)?,
933+
Some(value) => value.kind().get_or_apply(&mut cx, This::default())?,
934934
None => {
935935
let name = name.res(&cx.scope).to_owned();
936936
throw!(&mut cx, ReferenceError, "{} is not defined", name)
@@ -1062,7 +1062,7 @@ mod handlers {
10621062
throw!(cx.scope, TypeError, "supertype constructor must be an object")
10631063
};
10641064

1065-
This::BeforeSuper { super_constructor }
1065+
This::before_super(super_constructor)
10661066
} else {
10671067
this_for_new_target(&mut cx.scope, callee)?
10681068
};
@@ -1076,7 +1076,7 @@ mod handlers {
10761076
throw!(cx.scope, TypeError, "supertype constructor must be an object")
10771077
};
10781078

1079-
This::BeforeSuper { super_constructor }
1079+
This::before_super(super_constructor)
10801080
} else {
10811081
let new_target = cx.active_frame().new_target().unwrap();
10821082
this_for_new_target(&mut cx.scope, new_target)?
@@ -1207,23 +1207,23 @@ mod handlers {
12071207

12081208
let stack_len = cx.stack.len();
12091209
let (callee, this) = if function_call_kind == FunctionCallKind::Super {
1210-
let callee = match cx.active_frame().this {
1211-
This::BeforeSuper { super_constructor } => Value::object(super_constructor),
1210+
let callee = match cx.active_frame().this.kind() {
1211+
ThisKind::BeforeSuper { super_constructor } => Value::object(super_constructor),
12121212
_ => throw!(
12131213
cx.scope,
12141214
TypeError,
12151215
"super() must be called exactly once in a subclass constructor"
12161216
),
12171217
};
1218-
(callee, This::Default)
1218+
(callee, This::default())
12191219
} else if has_this {
12201220
cx.stack[stack_len - argc - 2..].rotate_left(2);
12211221
let (this, callee) = cx.pop_stack2_rooted();
1222-
(callee, This::Bound(this))
1222+
(callee, This::bound(this))
12231223
} else {
12241224
cx.stack[stack_len - argc - 1..].rotate_left(1);
12251225
let callee = cx.pop_stack_rooted();
1226-
(callee, This::Default)
1226+
(callee, This::default())
12271227
};
12281228

12291229
if let Some(function) = callee.unpack().downcast_ref::<Function>(&cx.scope) {
@@ -2050,7 +2050,7 @@ mod handlers {
20502050

20512051
pub fn bindthis(mut cx: DispatchContext<'_>) -> Result<Option<HandleResult>, Unrooted> {
20522052
let value = cx.pop_stack_rooted();
2053-
cx.active_frame_mut().this = This::Bound(value);
2053+
cx.active_frame_mut().this = This::bound(value);
20542054
Ok(None)
20552055
}
20562056

@@ -2085,7 +2085,7 @@ mod handlers {
20852085
let iterable = value
20862086
.get_property(symbol_iterator.to_key(&mut cx.scope), &mut cx.scope)?
20872087
.root(&mut cx.scope);
2088-
let iterator = iterable.apply(This::Bound(value), CallArgs::empty(), &mut cx.scope)?;
2088+
let iterator = iterable.apply(This::bound(value), CallArgs::empty(), &mut cx.scope)?;
20892089
cx.push_stack(iterator);
20902090
Ok(None)
20912091
}
@@ -2335,12 +2335,12 @@ mod handlers {
23352335
let fun = k
23362336
.get_property($v.to_key(&mut cx.scope), &mut cx.scope)?
23372337
.root(&mut cx.scope);
2338-
let result = fun.apply(This::Default, args, &mut cx.scope)?;
2338+
let result = fun.apply(This::default(), args, &mut cx.scope)?;
23392339
cx.push_stack(result);
23402340
} else {
23412341
// Fastpath: call builtin directly
23422342
// TODO: should we add to externals?
2343-
let result = fun.apply(This::Default, args, &mut cx.scope)?;
2343+
let result = fun.apply(This::default(), args, &mut cx.scope)?;
23442344
cx.push_stack(result);
23452345
}
23462346
}};

crates/dash_vm/src/frame.rs

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ use dash_proc_macro::Trace;
99

1010
use crate::gc::ObjectId;
1111
use crate::gc::trace::{Trace, TraceCtxt};
12-
use crate::localscope::LocalScope;
13-
use crate::throw;
12+
use crate::value::object::This;
1413
use crate::value::string::JsString;
1514
use crate::value::{ExternalValue, Unrooted};
1615

17-
use super::value::Value;
1816
use super::value::function::user::UserFunction;
1917

2018
#[derive(Debug, Clone, Copy, Trace)]
@@ -97,41 +95,6 @@ unsafe impl Trace for LoopCounterMap {
9795
fn trace(&self, _: &mut TraceCtxt<'_>) {}
9896
}
9997

100-
#[derive(Debug, Clone, Copy)]
101-
pub enum This {
102-
/// No `this` binding. Evaluates to the global object in non-strict mode, or undefined in strict mode
103-
Default,
104-
/// Initial state of `this` in subclass constructors. Throws an error if attempted to evaluate as a value.
105-
/// Gets changed to `Bound` by the call to super().
106-
BeforeSuper { super_constructor: ObjectId },
107-
/// Bound as a value.
108-
Bound(Value),
109-
}
110-
111-
impl This {
112-
pub fn to_value(self, scope: &mut LocalScope<'_>) -> Result<Value, Value> {
113-
match self {
114-
// TODO: once we have strict mode, eval to undefined
115-
This::Default => Ok(Value::object(scope.global)),
116-
This::Bound(value) => Ok(value),
117-
This::BeforeSuper { .. } => {
118-
// panic!();
119-
throw!(scope, Error, "`super()` must be called before accessing `this`")
120-
}
121-
}
122-
}
123-
}
124-
125-
unsafe impl Trace for This {
126-
fn trace(&self, cx: &mut TraceCtxt<'_>) {
127-
match *self {
128-
This::Default => {}
129-
This::BeforeSuper { super_constructor } => super_constructor.trace(cx),
130-
This::Bound(value) => value.trace(cx),
131-
}
132-
}
133-
}
134-
13598
#[derive(Debug, Clone, Trace)]
13699
pub struct Frame {
137100
pub function: Rc<Function>,
@@ -225,7 +188,7 @@ impl Frame {
225188
};
226189

227190
Self {
228-
this: This::Default,
191+
this: This::default(),
229192
function: Rc::new(fun),
230193
externals: Vec::new().into(),
231194
ip: 0,

crates/dash_vm/src/gc/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ use smallvec::SmallVec;
1111
use trace::TraceCtxt;
1212

1313
use crate::Vm;
14-
use crate::frame::This;
1514
use crate::localscope::LocalScope;
1615
use crate::value::function::args::CallArgs;
17-
use crate::value::object::PropertyValue;
16+
use crate::value::object::{PropertyValue, This};
1817
use crate::value::primitive::InternalSlots;
1918
use crate::value::propertykey::PropertyKey;
2019
use crate::value::{Typeof, Unrooted, Value};
@@ -119,6 +118,7 @@ pub struct AllocId<M> {
119118
}
120119

121120
impl<M> AllocId<M> {
121+
#[inline]
122122
pub(crate) fn from_raw(id_and_chunk: PackedInnerAllocId) -> Self {
123123
Self {
124124
id_and_chunk,

0 commit comments

Comments
 (0)