Skip to content

Commit 9cef408

Browse files
committed
move LocalScope out of CallContext
1 parent 0b2c4e3 commit 9cef408

44 files changed

Lines changed: 967 additions & 1023 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

crates/dash_dlloader/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use dash_vm::throw;
77
use dash_vm::value::Value;
88
use dash_vm::value::function::native::CallContext;
99
use dash_vm::value::function::{Function, FunctionKind};
10-
use dash_vm::value::object::{OrdObject, Object, PropertyValue};
10+
use dash_vm::value::object::{Object, OrdObject, PropertyValue};
1111
use dash_vm::value::ops::conversions::ValueConversion;
1212
use dash_vm::value::propertykey::ToPropertyKey;
1313
use dash_vm::value::string::JsString;
@@ -51,24 +51,24 @@ macro_rules! dashdl {
5151
};
5252
}
5353

54-
pub fn load_sync(mut cx: CallContext) -> Result<Value, Value> {
54+
pub fn load_sync(mut cx: CallContext, scope: &mut LocalScope<'_>) -> Result<Value, Value> {
5555
let path = match cx.args.first() {
5656
Some(first) => first,
57-
None => throw!(cx.scope, ReferenceError, "Missing path to dynamic library"),
57+
None => throw!(scope, ReferenceError, "Missing path to dynamic library"),
5858
};
5959

60-
let path = ValueConversion::to_js_string(path, cx.scope)?;
60+
let path = ValueConversion::to_js_string(path, scope)?;
6161

6262
unsafe {
63-
let lib = match Library::new(path.res(cx.scope)) {
63+
let lib = match Library::new(path.res(scope)) {
6464
// TODO: Currently we (intentionally) leak all dlopen'd handles, because we don't know exactly when we should close it
6565
Ok(lib) => ManuallyDrop::new(lib),
66-
Err(err) => throw!(cx.scope, Error, "{}", err),
66+
Err(err) => throw!(scope, Error, "{}", err),
6767
};
6868

6969
let init: libloading::Symbol<InitFunction> = match lib.get(b"dashjs_init_module\0") {
7070
Ok(sym) => sym,
71-
Err(err) => throw!(cx.scope, Error, "{}", err),
71+
Err(err) => throw!(scope, Error, "{}", err),
7272
};
7373

7474
let mut ret = Ok(Value::undefined());

crates/dash_node_impl/src/assert.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ pub fn init_module(sc: &mut LocalScope<'_>) -> Result<Value, Value> {
1313
Ok(Value::object(js_assert))
1414
}
1515

16-
fn js_assert(cx: CallContext) -> Result<Value, Value> {
17-
let value = cx.args.first().or_type_err(cx.scope, "Missing value to assert")?;
16+
fn js_assert(cx: CallContext, scope: &mut LocalScope<'_>) -> Result<Value, Value> {
17+
let value = cx.args.first().or_type_err(scope, "Missing value to assert")?;
1818
let message = cx.args.get(1);
1919

2020
// TODO: throw AssertionError
21-
if !value.is_truthy(cx.scope) {
21+
if !value.is_truthy(scope) {
2222
match message {
2323
Some(message) => {
24-
let message = message.to_js_string(cx.scope)?.res(cx.scope).to_owned();
25-
throw!(cx.scope, Error, "Assertion failed: {}", message)
24+
let message = message.to_js_string(scope)?.res(scope).to_owned();
25+
throw!(scope, Error, "Assertion failed: {}", message)
2626
}
27-
None => throw!(cx.scope, Error, "Assertion failed"),
27+
None => throw!(scope, Error, "Assertion failed"),
2828
}
2929
}
3030

crates/dash_node_impl/src/buffer.rs

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ pub fn init_module(sc: &mut LocalScope<'_>) -> Result<Value, Value> {
3636
inner: TypedArray::new(sc, arraybuffer, TypedArrayKind::Uint8Array),
3737
})
3838
};
39-
let wu32be = register_native_fn(sc, wu32be_sym, |cx| write_byte(cx, Endianness::Big, 4));
40-
let wu32le = register_native_fn(sc, wu32le_sym, |cx| write_byte(cx, Endianness::Little, 4));
39+
let wu32be = register_native_fn(sc, wu32be_sym, |cx, scope| write_byte(cx, scope, Endianness::Big, 4));
40+
let wu32le = register_native_fn(sc, wu32le_sym, |cx, scope| write_byte(cx, scope, Endianness::Little, 4));
4141
buffer_prototype.set_property(wu32be_sym.to_key(sc), PropertyValue::static_default(wu32be.into()), sc)?;
4242
buffer_prototype.set_property(wu32le_sym.to_key(sc), PropertyValue::static_default(wu32le.into()), sc)?;
4343

4444
let buffer_ctor = Function::new(
4545
sc,
4646
Some(buffer_sym.into()),
47-
FunctionKind::Native(|cx| throw!(cx.scope, Error, "Buffer() constructor unsupported")),
47+
FunctionKind::Native(|_, scope| throw!(scope, Error, "Buffer() constructor unsupported")),
4848
);
4949
buffer_ctor.set_fn_prototype(buffer_prototype);
5050
let buffer_ctor = sc.register(buffer_ctor);
@@ -77,22 +77,27 @@ enum Endianness {
7777
Big,
7878
}
7979

80-
fn write_byte(cx: CallContext, endianness: Endianness, size: usize) -> Result<Value, Value> {
80+
fn write_byte(
81+
cx: CallContext,
82+
scope: &mut LocalScope<'_>,
83+
endianness: Endianness,
84+
size: usize,
85+
) -> Result<Value, Value> {
8186
// TODO: can we just merge this with the TypedArray builtin logic?
8287
let Some(ValueKind::Number(Number(value))) = cx.args.first().map(|s| s.unpack()) else {
83-
throw!(cx.scope, Error, "Invalid 'value' argument type")
88+
throw!(scope, Error, "Invalid 'value' argument type")
8489
};
8590
let offset = match cx.args.get(1).map(|v| v.unpack()) {
8691
Some(ValueKind::Number(Number(n))) => n as usize,
87-
Some(_) => throw!(cx.scope, TypeError, "Invalid 'offset' argument type"),
92+
Some(_) => throw!(scope, TypeError, "Invalid 'offset' argument type"),
8893
None => 0,
8994
};
9095

91-
let buf = receiver_t::<Buffer>(cx.scope, &cx.this, "Buffer.prototype.write*")?;
92-
let buf = if let Some(buf) = buf.inner.arraybuffer(cx.scope).storage().get(offset..) {
96+
let buf = receiver_t::<Buffer>(scope, &cx.this, "Buffer.prototype.write*")?;
97+
let buf = if let Some(buf) = buf.inner.arraybuffer(scope).storage().get(offset..) {
9398
buf
9499
} else {
95-
throw!(cx.scope, Error, "out of range")
100+
throw!(scope, Error, "out of range")
96101
};
97102
let bytes = match (size, endianness) {
98103
(1, Endianness::Little) => &u8::to_le_bytes(value as u8) as &[_],
@@ -140,25 +145,22 @@ impl Object for Buffer {
140145
extract!(self, inner);
141146
}
142147

143-
fn from(cx: CallContext) -> Result<Value, Value> {
144-
let BufferState { buffer_prototype } = State::from_vm(cx.scope).store[BufferKey];
148+
fn from(cx: CallContext, scope: &mut LocalScope<'_>) -> Result<Value, Value> {
149+
let BufferState { buffer_prototype } = State::from_vm(scope).store[BufferKey];
145150

146-
let source = cx
147-
.args
148-
.first()
149-
.or_type_err(cx.scope, "Missing source to `Buffer.from`")?;
151+
let source = cx.args.first().or_type_err(scope, "Missing source to `Buffer.from`")?;
150152

151-
let length = source.length_of_array_like(cx.scope)?;
153+
let length = source.length_of_array_like(scope)?;
152154
let mut buf = Vec::with_capacity(length);
153155
for i in 0..length {
154156
let item = source
155-
.get_property(i.to_key(cx.scope), cx.scope)
156-
.root(cx.scope)?
157-
.to_number(cx.scope)? as u8;
157+
.get_property(i.to_key(scope), scope)
158+
.root(scope)?
159+
.to_number(scope)? as u8;
158160
buf.push(Cell::new(item));
159161
}
160162

161-
let arraybuffer = cx.scope.register(ArrayBuffer::from_storage(cx.scope, buf));
163+
let arraybuffer = scope.register(ArrayBuffer::from_storage(scope, buf));
162164
let instn = Buffer {
163165
inner: TypedArray::with_obj(
164166
arraybuffer,
@@ -167,15 +169,15 @@ fn from(cx: CallContext) -> Result<Value, Value> {
167169
),
168170
};
169171

170-
Ok(Value::object(cx.scope.register(instn)))
172+
Ok(Value::object(scope.register(instn)))
171173
}
172174

173-
fn alloc(cx: CallContext) -> Result<Value, Value> {
175+
fn alloc(cx: CallContext, scope: &mut LocalScope<'_>) -> Result<Value, Value> {
174176
let size = cx
175177
.args
176178
.first()
177-
.or_type_err(cx.scope, "Missing size argument to `Buffer.alloc`")?;
178-
let size = size.to_number(cx.scope)? as usize;
179+
.or_type_err(scope, "Missing size argument to `Buffer.alloc`")?;
180+
let size = size.to_number(scope)? as usize;
179181

180182
let fill = cx.args.get(1).copied();
181183

@@ -184,14 +186,14 @@ fn alloc(cx: CallContext) -> Result<Value, Value> {
184186
if let ValueKind::Number(Number(num)) = unpacked {
185187
vec![Cell::new(num as u8); size]
186188
} else {
187-
throw!(cx.scope, Error, "invalid fill argument to Buffer.alloc: {:?}", unpacked)
189+
throw!(scope, Error, "invalid fill argument to Buffer.alloc: {:?}", unpacked)
188190
}
189191
} else {
190192
vec![Cell::new(0); size]
191193
};
192194

193-
let BufferState { buffer_prototype, .. } = State::from_vm(cx.scope).store[BufferKey];
194-
let arraybuffer = cx.scope.register(ArrayBuffer::from_storage(cx.scope, buf));
195+
let BufferState { buffer_prototype, .. } = State::from_vm(scope).store[BufferKey];
196+
let arraybuffer = scope.register(ArrayBuffer::from_storage(scope, buf));
195197
let instn = Buffer {
196198
inner: TypedArray::with_obj(
197199
arraybuffer,
@@ -200,5 +202,5 @@ fn alloc(cx: CallContext) -> Result<Value, Value> {
200202
),
201203
};
202204

203-
Ok(Value::object(cx.scope.register(instn)))
205+
Ok(Value::object(scope.register(instn)))
204206
}

crates/dash_node_impl/src/events.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,16 @@ pub fn init_module(sc: &mut LocalScope<'_>) -> Result<Value, Value> {
4646
let event_emitter_ctor = Function::new(
4747
sc,
4848
Some(event_emitter_sym.into()),
49-
FunctionKind::Native(|cx| {
49+
FunctionKind::Native(|_, scope| {
5050
let EventsState {
5151
event_emitter_prototype,
52-
} = State::from_vm(cx.scope).store[EventsKey];
52+
} = State::from_vm(scope).store[EventsKey];
5353

5454
let emitter = EventEmitter {
5555
object: OrdObject::with_prototype(event_emitter_prototype),
5656
handlers: RefCell::new(FxHashMap::default()),
5757
};
58-
Ok(cx.scope.register(emitter).into())
58+
Ok(scope.register(emitter).into())
5959
}),
6060
);
6161
event_emitter_ctor.set_fn_prototype(event_emitter_prototype);
@@ -133,15 +133,15 @@ fn with_event_emitter(
133133
}
134134
}
135135

136-
fn on(cx: CallContext) -> Result<Value, Value> {
136+
fn on(cx: CallContext, scope: &mut LocalScope<'_>) -> Result<Value, Value> {
137137
let [name, cb] = *cx.args else {
138-
throw!(cx.scope, Error, "expected an event name and callback function");
138+
throw!(scope, Error, "expected an event name and callback function");
139139
};
140-
let name = name.to_js_string(cx.scope)?;
140+
let name = name.to_js_string(scope)?;
141141
let ValueKind::Object(cb) = cb.unpack() else {
142-
throw!(cx.scope, Error, "expected callback to be a function")
142+
throw!(scope, Error, "expected callback to be a function")
143143
};
144-
with_event_emitter(cx.scope, cx.this, |_, this| {
144+
with_event_emitter(scope, cx.this, |_, this| {
145145
match this.handlers.borrow_mut().entry(name.sym()) {
146146
Entry::Occupied(mut entry) => entry.get_mut().push(cb),
147147
Entry::Vacant(entry) => drop(entry.insert(vec![cb])),
@@ -150,12 +150,12 @@ fn on(cx: CallContext) -> Result<Value, Value> {
150150
})
151151
}
152152

153-
fn emit(cx: CallContext) -> Result<Value, Value> {
153+
fn emit(cx: CallContext, scope: &mut LocalScope<'_>) -> Result<Value, Value> {
154154
let [name, args @ ..] = &*cx.args else {
155-
throw!(cx.scope, Error, "expected an event name");
155+
throw!(scope, Error, "expected an event name");
156156
};
157-
let name = name.to_js_string(cx.scope)?;
158-
with_event_emitter(cx.scope, cx.this, |sc, this| {
157+
let name = name.to_js_string(scope)?;
158+
with_event_emitter(scope, cx.this, |sc, this| {
159159
let mut did_emit = false;
160160
if let Some(handlers) = this.handlers.borrow().get(&name.sym()) {
161161
for handler in handlers {

crates/dash_node_impl/src/path.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use dash_middle::interner::sym;
44
use dash_vm::localscope::LocalScope;
55
use dash_vm::throw;
66
use dash_vm::value::function::native::{CallContext, register_native_fn};
7-
use dash_vm::value::object::{OrdObject, Object, PropertyValue};
7+
use dash_vm::value::object::{Object, OrdObject, PropertyValue};
88
use dash_vm::value::ops::conversions::ValueConversion;
99
use dash_vm::value::propertykey::ToPropertyKey;
1010
use dash_vm::value::{ExceptionContext, Unpack, Value, ValueKind};
@@ -30,38 +30,38 @@ pub fn init_module(sc: &mut LocalScope<'_>) -> Result<Value, Value> {
3030
Ok(sc.register(exports).into())
3131
}
3232

33-
fn parse_path(cx: CallContext) -> Result<Value, Value> {
34-
let path = cx.args.first().or_type_err(cx.scope, "Missing path to path")?;
35-
let path = path.to_js_string(cx.scope)?;
36-
let path = Path::new(path.res(cx.scope));
33+
fn parse_path(cx: CallContext, scope: &mut LocalScope<'_>) -> Result<Value, Value> {
34+
let path = cx.args.first().or_type_err(scope, "Missing path to path")?;
35+
let path = path.to_js_string(scope)?;
36+
let path = Path::new(path.res(scope));
3737
let dir = if path.is_dir() {
3838
path.to_str()
3939
} else {
4040
path.parent().and_then(Path::to_str)
4141
};
4242
let dir = match dir {
43-
Some(path) => cx.scope.intern(path.to_owned()),
44-
None => throw!(cx.scope, Error, "malformed path"),
43+
Some(path) => scope.intern(path.to_owned()),
44+
None => throw!(scope, Error, "malformed path"),
4545
};
46-
let object = OrdObject::new(cx.scope);
47-
let object = cx.scope.register(object);
48-
let dir_sym = state_mut(cx.scope).sym.dir;
46+
let object = OrdObject::new(scope);
47+
let object = scope.register(object);
48+
let dir_sym = state_mut(scope).sym.dir;
4949
object.set_property(
50-
dir_sym.to_key(cx.scope),
50+
dir_sym.to_key(scope),
5151
PropertyValue::static_default(Value::string(dir.into())),
52-
cx.scope,
52+
scope,
5353
)?;
54-
Ok(cx.scope.register(object).into())
54+
Ok(scope.register(object).into())
5555
}
5656

57-
fn join_path(cx: CallContext) -> Result<Value, Value> {
57+
fn join_path(cx: CallContext, scope: &mut LocalScope<'_>) -> Result<Value, Value> {
5858
let mut path = PathBuf::new();
5959

6060
for arg in &cx.args {
6161
let value = match arg.unpack() {
62-
ValueKind::String(s) => s.res(cx.scope),
62+
ValueKind::String(s) => s.res(scope),
6363
other => throw!(
64-
cx.scope,
64+
scope,
6565
TypeError,
6666
"expected string argument to path.join, got {:?}",
6767
other
@@ -77,7 +77,5 @@ fn join_path(cx: CallContext) -> Result<Value, Value> {
7777
}
7878
}
7979

80-
Ok(Value::string(
81-
cx.scope.intern(path.display().to_string().as_str()).into(),
82-
))
80+
Ok(Value::string(scope.intern(path.display().to_string().as_str()).into()))
8381
}

crates/dash_node_impl/src/stream.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ pub fn init_module(sc: &mut LocalScope<'_>) -> Result<Value, Value> {
2727
let stream_ctor = Function::new(
2828
sc,
2929
Some(stream_sym.into()),
30-
FunctionKind::Native(|cx| {
31-
let StreamState { stream_prototype } = State::from_vm(cx.scope).store[StreamKey];
30+
FunctionKind::Native(|_, scope| {
31+
let StreamState { stream_prototype } = State::from_vm(scope).store[StreamKey];
3232

33-
Ok(cx
34-
.scope
33+
Ok(scope
3534
.register(Stream {
3635
object: OrdObject::with_prototype(stream_prototype),
3736
})
@@ -46,7 +45,7 @@ pub fn init_module(sc: &mut LocalScope<'_>) -> Result<Value, Value> {
4645
sc,
4746
)?;
4847

49-
let readable_fn = register_native_fn(sc, readable_sym, |_sc| Ok(Value::undefined()));
48+
let readable_fn = register_native_fn(sc, readable_sym, |_, _| Ok(Value::undefined()));
5049
stream_ctor.set_property(
5150
readable_sym.to_key(sc),
5251
PropertyValue::static_default(readable_fn.into()),

0 commit comments

Comments
 (0)