Skip to content

Commit

Permalink
reimplement inspect code in rust
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Dec 25, 2024
1 parent 9abf963 commit 5c9c487
Show file tree
Hide file tree
Showing 12 changed files with 345 additions and 195 deletions.
25 changes: 14 additions & 11 deletions cli/src/cmd/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,29 @@ use clap::ArgMatches;
use dash_middle::parser::error::IntoFormattableErrors;
use dash_optimizer::OptLevel;
use dash_rt::format_value;
use dash_rt::runtime::Runtime;
use dash_vm::eval::EvalError;
use dash_vm::value::Root;
use dash_vm::Vm;

pub fn eval(args: &ArgMatches) -> anyhow::Result<()> {
let source = args.value_of("source").context("Missing source")?;
let opt = *args.get_one::<OptLevel>("opt").unwrap();

let mut vm = Vm::new(Default::default());
let mut scope = vm.scope();
tokio::runtime::Runtime::new()?.block_on(async move {
let mut runtime = Runtime::new(None);

match scope.eval(source, opt) {
Ok(value) => println!("{}", format_value(value.root(&mut scope), &mut scope).unwrap()),
Err(EvalError::Exception(value)) => {
println!("{}", format_value(value.root(&mut scope), &mut scope).unwrap())
}
Err(EvalError::Middle(errs)) => println!("{}", errs.formattable(source, true)),
};
runtime.vm_mut().with_scope(|scope| {
match scope.eval(source, opt) {
Ok(value) => println!("{}", format_value(value.root(scope), scope).unwrap()),
Err(EvalError::Exception(value)) => {
println!("{}", format_value(value.root(scope), scope).unwrap())
}
Err(EvalError::Middle(errs)) => println!("{}", errs.formattable(source, true)),
};
});

scope.process_async_tasks();
runtime.run_event_loop().await;
});

Ok(())
}
37 changes: 20 additions & 17 deletions cli/src/cmd/repl.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
use dash_middle::parser::error::IntoFormattableErrors;
use dash_optimizer::OptLevel;
use dash_rt::format_value;
use dash_rt::runtime::Runtime;
use dash_vm::eval::EvalError;
use dash_vm::value::Root;
use dash_vm::Vm;
use rustyline::Editor;

pub fn repl() -> anyhow::Result<()> {
let mut rl = Editor::<()>::new();

let mut vm = Vm::new(Default::default());
let mut scope = vm.scope();
tokio::runtime::Runtime::new()?.block_on(async move {
let mut rt = Runtime::new(None);

while let Ok(input) = rl.readline("> ") {
if input.is_empty() {
continue;
}
while let Ok(input) = rl.readline("> ") {
if input.is_empty() {
continue;
}

rl.add_history_entry(&input);
rl.add_history_entry(&input);

match scope.eval(&input, OptLevel::Aggressive) {
Ok(value) => println!("{}", format_value(value.root(&mut scope), &mut scope).unwrap()),
Err(EvalError::Exception(value)) => {
println!("{}", format_value(value.root(&mut scope), &mut scope).unwrap())
}
Err(EvalError::Middle(errs)) => println!("{}", errs.formattable(&input, true)),
}
rt.vm_mut().with_scope(|scope| {
match scope.eval(&input, OptLevel::Aggressive) {
Ok(value) => println!("{}", format_value(value.root(scope), scope).unwrap()),
Err(EvalError::Exception(value)) => {
println!("{}", format_value(value.root(scope), scope).unwrap())
}
Err(EvalError::Middle(errs)) => println!("{}", errs.formattable(&input, true)),
}

scope.process_async_tasks();
}
scope.process_async_tasks();
});
}
});

Ok(())
}
10 changes: 3 additions & 7 deletions cli/src/cmd/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use dash_middle::parser::error::IntoFormattableErrors;
use dash_optimizer::OptLevel;
use dash_rt::format_value;
use dash_rt::runtime::Runtime;
use dash_rt::state::State;
use dash_vm::eval::EvalError;
use dash_vm::value::Root;
use std::fs;
Expand Down Expand Up @@ -53,7 +52,7 @@ fn run_normal_mode(path: &str, opt: OptLevel, quiet: bool, initial_gc_threshold:
}

async fn inner(source: String, opt: OptLevel, quiet: bool, initial_gc_threshold: Option<usize>) -> anyhow::Result<()> {
let mut rt = Runtime::new(initial_gc_threshold).await;
let mut rt = Runtime::new(initial_gc_threshold);

let module = dash_rt_modules::init_modules();
rt.set_module_manager(module);
Expand All @@ -76,11 +75,8 @@ async fn inner(source: String, opt: OptLevel, quiet: bool, initial_gc_threshold:
println!("{}", format_value(value, &mut scope).unwrap());
}

let state = State::from_vm_mut(&mut scope);
if state.needs_event_loop() {
drop(scope);
rt.run_event_loop().await;
}
drop(scope);
rt.run_event_loop().await;

Ok(())
}
10 changes: 4 additions & 6 deletions crates/dash_node_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::rc::Rc;

use anyhow::{anyhow, Context};
use anyhow::{Context, anyhow};
use dash_log::debug;
use dash_middle::parser::error::IntoFormattableErrors;
use dash_optimizer::OptLevel;
Expand All @@ -19,7 +19,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, extract, throw, Vm};
use dash_vm::{Vm, delegate, extract, throw};
use package::Package;
use rustc_hash::FxHashMap;
use state::Nodejs;
Expand Down Expand Up @@ -76,7 +76,7 @@ async fn run_inner_fallible(path: &str, opt: OptLevel, initial_gc_threshold: Opt
ongoing_requires: RefCell::new(FxHashMap::default()),
});

let mut rt = Runtime::new(initial_gc_threshold).await;
let mut rt = Runtime::new(initial_gc_threshold);
let state @ state::State {
sym:
NodeSymbols {
Expand Down Expand Up @@ -137,9 +137,7 @@ async fn run_inner_fallible(path: &str, opt: OptLevel, initial_gc_threshold: Opt
)
})?;

if rt.state().needs_event_loop() {
rt.run_event_loop().await;
}
rt.run_event_loop().await;

Ok(())
}
Expand Down
105 changes: 0 additions & 105 deletions crates/dash_rt/js/inspect.js

This file was deleted.

Loading

0 comments on commit 5c9c487

Please sign in to comment.