Skip to content

Commit

Permalink
replace as_any with extract functions
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Nov 11, 2024
1 parent ab72e60 commit 504b34a
Show file tree
Hide file tree
Showing 37 changed files with 272 additions and 300 deletions.
Binary file removed .github/img/JitDemo.gif
Binary file not shown.
27 changes: 20 additions & 7 deletions crates/dash_node_impl/src/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::any::Any;
use std::cell::Cell;

use dash_middle::interner::sym;
Expand All @@ -14,8 +13,8 @@ use dash_vm::value::object::{Object, PropertyValue};
use dash_vm::value::ops::conversions::ValueConversion;
use dash_vm::value::primitive::Number;
use dash_vm::value::typedarray::{TypedArray, TypedArrayKind};
use dash_vm::value::{Unpack, Value, ValueKind};
use dash_vm::{delegate, throw, Vm};
use dash_vm::value::{Root, Unpack, Value, ValueKind};
use dash_vm::{delegate, extract, throw};

use crate::state::state_mut;
use crate::symbols::NodeSymbols;
Expand Down Expand Up @@ -136,9 +135,8 @@ impl Object for Buffer {
apply,
own_keys
);
fn as_any(&self, _: &Vm) -> &dyn Any {
self
}

extract!(self, inner);
}

fn from(cx: CallContext) -> Result<Value, Value> {
Expand All @@ -147,7 +145,22 @@ fn from(cx: CallContext) -> Result<Value, Value> {
buffer_ctor,
} = State::from_vm(cx.scope).store[BufferKey];

let arraybuffer = cx.scope.register(ArrayBuffer::new(cx.scope));
let Some(source) = cx.args.first() else {
throw!(cx.scope, Error, "Missing source to `Buffer.from`")
};

let length = source.length_of_array_like(cx.scope)?;
let mut buf = Vec::with_capacity(length);
for i in 0..length {
let i = cx.scope.intern_usize(i);
let item = source
.get_property(cx.scope, i.into())
.root(cx.scope)?
.to_number(cx.scope)? as u8;
buf.push(Cell::new(item));
}

let arraybuffer = cx.scope.register(ArrayBuffer::from_storage(cx.scope, buf));
let instn = Buffer {
inner: TypedArray::new_with_proto_ctor(arraybuffer, TypedArrayKind::Uint8Array, buffer_prototype, buffer_ctor),
};
Expand Down
5 changes: 3 additions & 2 deletions crates/dash_node_impl/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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::{Unpack, Value, ValueKind};
use dash_vm::{delegate, throw};
use dash_vm::{delegate, extract, throw};
use rustc_hash::FxHashMap;

use crate::state::state_mut;
Expand Down Expand Up @@ -107,9 +107,10 @@ impl Object for EventEmitter {
set_prototype,
get_prototype,
apply,
as_any,
own_keys
);

extract!(self);
}

fn with_event_emitter(
Expand Down
5 changes: 3 additions & 2 deletions crates/dash_node_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use dash_vm::localscope::LocalScope;
use dash_vm::value::array::Array;
use dash_vm::value::object::{NamedObject, Object, PropertyValue};
use dash_vm::value::{Root, Unpack, Unrooted, Value, ValueKind};
use dash_vm::{delegate, throw, Vm};
use dash_vm::{delegate, extract, throw, Vm};
use package::Package;
use rustc_hash::FxHashMap;
use state::Nodejs;
Expand Down Expand Up @@ -271,7 +271,6 @@ impl Object for RequireFunction {
delete_property,
set_prototype,
get_prototype,
as_any,
own_keys
);

Expand Down Expand Up @@ -390,4 +389,6 @@ impl Object for RequireFunction {
debug!(%arg, "resolved module");
result
}

extract!(self);
}
5 changes: 3 additions & 2 deletions crates/dash_node_impl/src/stream.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use dash_proc_macro::Trace;
use dash_rt::state::State;
use dash_rt::typemap::Key;
use dash_vm::delegate;
use dash_vm::gc::ObjectId;
use dash_vm::localscope::LocalScope;
use dash_vm::value::function::native::register_native_fn;
use dash_vm::value::function::{Function, FunctionKind};
use dash_vm::value::object::{NamedObject, Object, PropertyValue};
use dash_vm::value::Value;
use dash_vm::{delegate, extract};

use crate::state::state_mut;
use crate::symbols::NodeSymbols;
Expand Down Expand Up @@ -88,7 +88,8 @@ impl Object for Stream {
set_prototype,
get_prototype,
apply,
as_any,
own_keys
);

extract!(self);
}
5 changes: 3 additions & 2 deletions crates/dash_node_impl/src/zlib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use dash_proc_macro::Trace;
use dash_rt::state::State;
use dash_rt::typemap::Key;
use dash_vm::delegate;
use dash_vm::gc::ObjectId;
use dash_vm::localscope::LocalScope;
use dash_vm::value::function::{Function, FunctionKind};
use dash_vm::value::object::{NamedObject, Object, PropertyValue};
use dash_vm::value::Value;
use dash_vm::{delegate, extract};

use crate::state::state_mut;
use crate::symbols::NodeSymbols;
Expand Down Expand Up @@ -83,7 +83,8 @@ impl Object for Inflate {
set_prototype,
get_prototype,
apply,
as_any,
own_keys
);

extract!(self);
}
3 changes: 1 addition & 2 deletions crates/dash_rt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use dash_compiler::FunctionCompiler;
use dash_vm::frame::{Exports, Frame};
use dash_vm::localscope::LocalScope;
use dash_vm::value::function::native::CallContext;
use dash_vm::value::object::Object;
use dash_vm::value::ops::conversions::ValueConversion;
use dash_vm::value::promise::Promise;
use dash_vm::value::{Root, Value};
Expand Down Expand Up @@ -47,7 +46,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(&scope).downcast_ref::<Promise>().unwrap();
let promise = promise.extract::<Promise>(&scope).unwrap();

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

Expand Down
11 changes: 6 additions & 5 deletions crates/dash_rt_fetch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use dash_vm::value::object::{NamedObject, Object, PropertyValue};
use dash_vm::value::promise::Promise;
use dash_vm::value::string::JsString;
use dash_vm::value::{Unpack, Value, ValueKind};
use dash_vm::{delegate, throw, PromiseAction, Vm};
use dash_vm::{delegate, extract, throw, PromiseAction, Vm};
use once_cell::sync::Lazy;
use reqwest::{Client, Method};

Expand Down Expand Up @@ -72,7 +72,7 @@ fn fetch(cx: CallContext) -> Result<Value, Value> {
event_tx.send(EventMessage::ScheduleCallback(Box::new(move |rt| {
let mut sc = rt.vm_mut().scope();
let promise = State::from_vm_mut(&mut sc).take_promise(promise_id);
let promise = promise.as_any(&sc).downcast_ref::<Promise>().unwrap();
let promise = promise.extract::<Promise>(&sc).unwrap();

let (req, action) = match req {
Ok(resp) => {
Expand Down Expand Up @@ -105,7 +105,7 @@ fn http_response_text(cx: CallContext) -> Result<Value, Value> {
ValueKind::Object(obj) => obj,
_ => throw!(cx.scope, TypeError, "Expected a this value"),
};
let this = match this.as_any(cx.scope).downcast_ref::<HttpResponse>() {
let this = match this.extract::<HttpResponse>(cx.scope) {
Some(resp) => resp,
None => throw!(cx.scope, TypeError, "Invalid receiver, expected HttpResponse"),
};
Expand Down Expand Up @@ -133,7 +133,7 @@ fn http_response_text(cx: CallContext) -> Result<Value, Value> {
event_tx.send(EventMessage::ScheduleCallback(Box::new(move |rt| {
let mut sc = rt.vm_mut().scope();
let promise = State::from_vm_mut(&mut sc).take_promise(promise_id);
let promise = promise.as_any(&sc).downcast_ref::<Promise>().unwrap();
let promise = promise.extract::<Promise>(&sc).unwrap();

let (value, action) = match text {
Ok(text) => {
Expand Down Expand Up @@ -186,8 +186,9 @@ impl Object for HttpResponse {
delete_property,
set_prototype,
get_prototype,
as_any,
apply,
own_keys
);

extract!(self);
}
52 changes: 45 additions & 7 deletions crates/dash_rt_fs/src/sync.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
use std::path::Path;
use std::slice;

use dash_vm::localscope::LocalScope;
use dash_vm::throw;
use dash_vm::value::error::Error;
use dash_vm::value::function::native::CallContext;
use dash_vm::value::function::{Function, FunctionKind};
use dash_vm::value::function::native::{register_native_fn, CallContext};
use dash_vm::value::object::{NamedObject, Object, PropertyValue};
use dash_vm::value::ops::conversions::ValueConversion;
use dash_vm::value::typedarray::TypedArray;
use dash_vm::value::{Value, ValueContext};

pub fn init_module(sc: &mut LocalScope) -> Result<Value, Value> {
let name = sc.intern("readFileSync");
let read_file_value = Function::new(sc, Some(name.into()), FunctionKind::Native(read_file_sync));
let read_file_value = sc.register(read_file_value);
let read_file_sync_sym = sc.intern("readFileSync");
let write_file_sync_sym = sc.intern("writeFileSync");
let read_file_sync_value = register_native_fn(sc, read_file_sync_sym, read_file_sync);
let write_file_sync_value = register_native_fn(sc, write_file_sync_sym, write_file_sync);

let module = NamedObject::new(sc);
module.set_property(
sc,
name.into(),
PropertyValue::static_default(Value::object(read_file_value)),
read_file_sync_sym.into(),
PropertyValue::static_default(Value::object(read_file_sync_value)),
)?;
module.set_property(
sc,
write_file_sync_sym.into(),
PropertyValue::static_default(Value::object(write_file_sync_value)),
)?;

Ok(Value::object(sc.register(module)))
Expand All @@ -38,3 +48,31 @@ fn read_file_sync(cx: CallContext) -> Result<Value, Value> {
}
}
}

fn write_file_sync(cx: CallContext) -> Result<Value, Value> {
let [path, buf] = *cx.args else {
throw!(
cx.scope,
Error,
"Invalid arguments passed to fs.writeFileSync(path, buf)"
)
};
let path = path.to_js_string(cx.scope)?;
if let Some(array) = buf.extract::<TypedArray>(cx.scope) {
let path = Path::new(path.res(cx.scope));

let storage = array.arraybuffer(cx.scope).storage();
// SAFETY: Cell<u8> has the same layout as u8
let view = unsafe { slice::from_raw_parts(storage.as_ptr().cast::<u8>(), storage.len()) };

match std::fs::write(path, view) {
Ok(()) => Ok(Value::undefined()),
Err(err) => {
let err = Error::new(cx.scope, err.to_string());
Err(Value::object(cx.scope.register(err)))
}
}
} else {
throw!(cx.scope, TypeError, "Invalid source passed to fs.writeFileSync")
}
}
5 changes: 3 additions & 2 deletions crates/dash_rt_http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use dash_vm::value::ops::conversions::ValueConversion;
use dash_vm::value::root_ext::RootErrExt;
use dash_vm::value::string::JsString;
use dash_vm::value::{Unpack, Value, ValueContext, ValueKind};
use dash_vm::{delegate, throw};
use dash_vm::{delegate, extract, throw};
use hyper::Body;
use tokio::sync::oneshot;
use tokio::sync::oneshot::Sender;
Expand Down Expand Up @@ -159,10 +159,11 @@ impl Object for HttpContext {
delete_property,
set_prototype,
get_prototype,
as_any,
apply,
own_keys
);

extract!(self);
}

fn ctx_respond(cx: CallContext) -> Result<Value, Value> {
Expand Down
16 changes: 8 additions & 8 deletions crates/dash_rt_net/src/listener/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use dash_vm::value::object::{NamedObject, Object, PropertyValue};
use dash_vm::value::ops::conversions::ValueConversion;
use dash_vm::value::promise::Promise;
use dash_vm::value::{Unpack, Unrooted, Value};
use dash_vm::{delegate, throw, PromiseAction, Vm};
use dash_vm::{delegate, extract, throw, PromiseAction};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
use tokio::sync::{mpsc, oneshot};
Expand Down Expand Up @@ -120,7 +120,7 @@ impl Object for TcpListenerConstructor {
event_tx.send(EventMessage::ScheduleCallback(Box::new(move |rt| {
let mut scope = rt.vm_mut().scope();
let promise = State::from_vm_mut(&mut scope).take_promise(promise_id);
let promise = promise.as_any(&scope).downcast_ref::<Promise>().unwrap();
let promise = promise.extract::<Promise>(&scope).unwrap();

let stream_handle = TcpStreamHandle::new(&mut scope, writer_tx, reader_tx).unwrap();
let stream_handle = scope.register(stream_handle);
Expand All @@ -137,13 +137,11 @@ impl Object for TcpListenerConstructor {
Ok(Value::object(scope.register(handle)).into())
}

fn as_any(&self, _: &Vm) -> &dyn std::any::Any {
self
}

fn own_keys(&self, _: &mut LocalScope<'_>) -> Result<Vec<dash_vm::value::Value>, dash_vm::value::Value> {
Ok(Vec::new())
}

extract!(self);
}

enum TcpListenerBridgeMessage {
Expand Down Expand Up @@ -184,9 +182,10 @@ impl Object for TcpListenerHandle {
set_prototype,
get_prototype,
apply,
as_any,
own_keys
);

extract!(self);
}

fn tcplistener_accept(cx: CallContext) -> Result<Value, Value> {
Expand Down Expand Up @@ -272,9 +271,10 @@ impl Object for TcpStreamHandle {
set_prototype,
get_prototype,
apply,
as_any,
own_keys
);

extract!(self);
}

fn tcpstream_write(cx: CallContext) -> Result<Value, Value> {
Expand Down
2 changes: 1 addition & 1 deletion crates/dash_vm/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ mod handlers {
let id = cx.fetchw_and_inc_ip();
let name = JsString::from(cx.constants().symbols[SymbolConstant(id)]);

let value = match cx.global.as_any(&cx.scope).downcast_ref::<NamedObject>() {
let value = match cx.global.extract::<NamedObject>(&cx.scope) {
Some(value) => match value.get_raw_property(name.into()) {
Some(value) => value.kind().get_or_apply(&mut cx, Value::undefined())?,
None => {
Expand Down
Loading

0 comments on commit 504b34a

Please sign in to comment.