Skip to content

Commit

Permalink
Propagate current path through the rust code
Browse files Browse the repository at this point in the history
Propagating current path (workdir) through the rust code.
  • Loading branch information
arduano@localhost authored and arduano committed May 27, 2024
1 parent 411b1b2 commit d038af2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
5 changes: 4 additions & 1 deletion src/cmd/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ pub fn handle_cmd(parsed_args: &ArgMatches) -> Result<(), NixError> {
let expr = parsed_args
.get_one::<String>("expr")
.ok_or("You must use the '--expr' option. Nothing else is implemented :)")?;
print_value(&execution::evaluate(expr)?);

let current_dir = std::env::current_dir().map_err(|_| "Couldn't get the current directory")?;

print_value(&execution::evaluate(expr, &current_dir)?);
println!();
Ok(())
}
Expand Down
18 changes: 6 additions & 12 deletions src/eval/execution.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::env::current_dir;
use std::path::Path;
use std::sync::Once;

Expand All @@ -13,7 +12,7 @@ use super::types::js_value_to_nix;

static INIT_V8: Once = Once::new();

pub fn evaluate(nix_expr: &str) -> EvalResult {
pub fn evaluate(nix_expr: &str, workdir: &Path) -> EvalResult {
initialize_v8();
// Declare the V8 execution context
let isolate = &mut v8::Isolate::new(Default::default());
Expand Down Expand Up @@ -53,7 +52,7 @@ pub fn evaluate(nix_expr: &str) -> EvalResult {

let root_nix_fn = nix_expr_to_js_function(scope, nix_expr)?;

nix_value_from_module(scope, root_nix_fn, nixjs_rt_obj)
nix_value_from_module(scope, root_nix_fn, nixjs_rt_obj, workdir)
}

fn nix_expr_to_js_function<'s>(
Expand Down Expand Up @@ -170,20 +169,15 @@ fn initialize_v8() {

fn nix_value_from_module(
scope: &mut v8::ContextScope<v8::HandleScope>,
nix_value: v8::Local<v8::Function>,
nix_module_fn: v8::Local<v8::Function>,
nixjs_rt_obj: v8::Local<v8::Object>,
workdir: &Path,
) -> EvalResult {
let nixrt: v8::Local<v8::Value> = nixjs_rt_obj.into();

let eval_ctx = create_eval_ctx(
scope,
&nixrt,
&current_dir().map_err(|err| {
format!("Failed to determine the current working directory. Error: {err}")
})?,
)?;
let eval_ctx = create_eval_ctx(scope, &nixrt, workdir)?;

let nix_value = call_js_function(scope, &nix_value, nixjs_rt_obj, &[eval_ctx.into()])?;
let nix_value = call_js_function(scope, &nix_module_fn, nixjs_rt_obj, &[eval_ctx.into()])?;

let to_strict_fn: v8::Local<v8::Function> =
try_get_js_object_key(scope, &nixrt, "recursiveStrict")?
Expand Down
8 changes: 5 additions & 3 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ mod literals;
mod operators;

fn eval_ok(nix_expr: &str) -> Value {
match evaluate(nix_expr) {
let workdir = std::env::current_dir().unwrap();
match evaluate(nix_expr, &workdir) {
Ok(val) => val,
Err(err) => panic!(
"eval '{}' shouldn't fail.\nError message: {}",
"eval '{}' shouldn't fail.\nError message: {:?}",
nix_expr, err
),
}
}

fn eval_err(nix_expr: &str) -> NixErrorKind {
evaluate(nix_expr)
let workdir = std::env::current_dir().unwrap();
evaluate(nix_expr, &workdir)
.expect_err(&format!("eval '{}' expected an error", nix_expr))
.kind
}
Expand Down

0 comments on commit d038af2

Please sign in to comment.