Skip to content

Commit

Permalink
avoid tracing preinterned symbols and instead don't check them during…
Browse files Browse the repository at this point in the history
… sweep
  • Loading branch information
y21 committed Dec 27, 2023
1 parent f61eeaa commit cf2954d
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 17 deletions.
3 changes: 2 additions & 1 deletion crates/dash_middle/src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,8 @@ impl StringInterner {
/// You must mark all reachable symbols before calling this.
/// It won't cause undefined behavior if you don't (hence not unsafe), but it can lead to oddities such as panics.
pub fn sweep(&mut self) {
for i in 0..self.store.len() {
// Preinterned symbols are always kept, since they can be referred to statically.
for i in sym::PREINTERNED.len()..self.store.len() {
if let Some(data) = self.store[i].as_ref() {
if !data.visited.get() {
self.mapping.remove(&data.value);
Expand Down
10 changes: 5 additions & 5 deletions crates/dash_rt/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,29 @@ impl Runtime {
}

#[cfg(feature = "random")]
fn random_callback(_: &mut Vm) -> Result<f64, Value> {
fn random_callback(_: &mut Vm) -> Result<f64, Unrooted> {
use rand::Rng;
let mut rng = rand::thread_rng();
Ok(rng.gen())
}

fn time_callback(_: &mut Vm) -> Result<u64, Value> {
fn time_callback(_: &mut Vm) -> Result<u64, Unrooted> {
Ok(SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("time() < UNIX_EPOCH")
.as_millis() as u64)
}

fn import_callback(vm: &mut Vm, import_ty: StaticImportKind, path: JsString) -> Result<Value, Value> {
fn import_callback(vm: &mut Vm, import_ty: StaticImportKind, path: JsString) -> Result<Unrooted, Unrooted> {
let mut sc = vm.scope();

let root = State::from_vm(&sc).root_module().clone();

if let Some(module) = &*root.borrow() {
match module.import(&mut sc, import_ty, path) {
Ok(Some(module)) => return Ok(module),
Ok(Some(module)) => return Ok(module.into()),
Ok(None) => {}
Err(err) => return Err(err),
Err(err) => return Err(err.into()),
}
}

Expand Down
3 changes: 2 additions & 1 deletion crates/dash_vm/src/js_std/date.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::throw;
use crate::value::function::native::CallContext;
use crate::value::root_ext::RootErrExt;
use crate::value::Value;

pub fn time_millis(cx: &mut CallContext) -> Result<u64, Value> {
Expand All @@ -8,7 +9,7 @@ pub fn time_millis(cx: &mut CallContext) -> Result<u64, Value> {
None => throw!(&mut cx.scope, Error, "Failed to get the current time"),
};

callback(cx.scope)
callback(cx.scope).root_err(cx.scope)
}

pub fn constructor(cx: CallContext) -> Result<Value, Value> {
Expand Down
3 changes: 2 additions & 1 deletion crates/dash_vm/src/js_std/math.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::throw;
use crate::value::function::native::CallContext;
use crate::value::ops::conversions::ValueConversion;
use crate::value::root_ext::RootErrExt;
use crate::value::{Value, ValueContext};

pub fn abs(cx: CallContext) -> Result<Value, Value> {
Expand Down Expand Up @@ -210,7 +211,7 @@ pub fn floor(cx: CallContext) -> Result<Value, Value> {

pub fn random(cx: CallContext) -> Result<Value, Value> {
let num = match cx.scope.params().math_random_callback() {
Some(cb) => cb(cx.scope)?,
Some(cb) => cb(cx.scope).root_err(cx.scope)?,
None => throw!(cx.scope, Error, "Math.random is disabled for this context"),
};

Expand Down
5 changes: 0 additions & 5 deletions crates/dash_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1419,11 +1419,6 @@ impl Vm {

fn trace_roots(&mut self) {
let mut cx = TraceCtxt::new(&mut self.interner);

debug!("trace preinterned symbols");
for (_, sym) in sym::PREINTERNED {
sym.trace(&mut cx);
}

debug!("trace frames");
self.frames.trace(&mut cx);
Expand Down
9 changes: 5 additions & 4 deletions crates/dash_vm/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use dash_middle::compiler::StaticImportKind;

use crate::localscope::LocalScope;
use crate::value::string::JsString;
use crate::value::Unrooted;

use super::value::Value;
use super::Vm;

pub type MathRandomCallback = fn(vm: &mut Vm) -> Result<f64, Value>;
pub type TimeMillisCallback = fn(vm: &mut Vm) -> Result<u64, Value>;
pub type StaticImportCallback = fn(vm: &mut Vm, ty: StaticImportKind, path: JsString) -> Result<Value, Value>;
pub type DynamicImportCallback = fn(vm: &mut Vm, val: Value) -> Result<Value, Value>;
pub type MathRandomCallback = fn(vm: &mut Vm) -> Result<f64, Unrooted>;
pub type TimeMillisCallback = fn(vm: &mut Vm) -> Result<u64, Unrooted>;
pub type StaticImportCallback = fn(vm: &mut Vm, ty: StaticImportKind, path: JsString) -> Result<Unrooted, Unrooted>;
pub type DynamicImportCallback = fn(vm: &mut Vm, val: Value) -> Result<Unrooted, Unrooted>;
pub type DebuggerCallback = fn(vm: &mut Vm) -> Result<(), Value>;
pub type UnhandledTaskException = fn(vm: &mut LocalScope, exception: Value);

Expand Down

0 comments on commit cf2954d

Please sign in to comment.