From 672dd5a868a28c1fe2a8aa9bea077942e88e38f7 Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Sun, 19 Sep 2021 22:48:33 +0100 Subject: [PATCH 1/3] external with input --- crates/nu-engine/src/eval.rs | 73 ++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 32 deletions(-) diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index 705fa19213..d4c90fc0a4 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -68,6 +68,40 @@ fn eval_call(context: &EvaluationContext, call: &Call, input: Value) -> Result Result { + let engine_state = context.engine_state.borrow(); + + let decl_id = engine_state + .find_decl("run_external".as_bytes()) + .ok_or_else(|| ShellError::ExternalNotSupported(*name))?; + + let command = engine_state.get_decl(decl_id); + let new_context = context.enter_scope(); + + let mut call = Call::new(); + call.positional = [*name] + .iter() + .chain(args.iter()) + .map(|span| { + let contents = engine_state.get_span_contents(span); + let val = String::from_utf8_lossy(contents); + Expression { + expr: Expr::String(val.into()), + span: *span, + ty: Type::String, + custom_completion: None, + } + }) + .collect(); + + command.run(&new_context, &call, input) +} + pub fn eval_expression( context: &EvaluationContext, expr: &Expression, @@ -125,38 +159,7 @@ pub fn eval_expression( } Expr::RowCondition(_, expr) => eval_expression(context, expr), Expr::Call(call) => eval_call(context, call, Value::nothing()), - Expr::ExternalCall(name, args) => { - let engine_state = context.engine_state.borrow(); - - let decl_id = engine_state - .find_decl("run_external".as_bytes()) - .ok_or_else(|| ShellError::ExternalNotSupported(*name))?; - - let command = engine_state.get_decl(decl_id); - let new_context = context.enter_scope(); - - let mut call = Call::new(); - call.positional = [*name] - .iter() - .chain(args.iter()) - .map(|span| { - let contents = engine_state.get_span_contents(span); - let val = String::from_utf8_lossy(contents); - Expression { - expr: Expr::String(val.into()), - span: *span, - ty: Type::String, - custom_completion: None, - } - }) - .collect(); - - let value = Value::Nothing { - span: Span::new(0, 1), - }; - - command.run(&new_context, &call, value) - } + Expr::ExternalCall(name, args) => eval_external(context, name, args, Value::nothing()), Expr::Operator(_) => Ok(Value::Nothing { span: expr.span }), Expr::BinaryOp(lhs, op, rhs) => { let op_span = op.span; @@ -247,6 +250,12 @@ pub fn eval_block( } => { input = eval_call(context, call, input)?; } + Expression { + expr: Expr::ExternalCall(name, args), + .. + } => { + input = eval_external(context, name, args, input)?; + } elem => { input = eval_expression(context, elem)?; From cb0914ecb080f5bf274a382be2093eb07fe7adb2 Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Mon, 20 Sep 2021 10:32:55 +0100 Subject: [PATCH 2/3] remove enter scope --- crates/nu-engine/src/eval.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index d4c90fc0a4..a054a1b059 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -81,7 +81,6 @@ fn eval_external( .ok_or_else(|| ShellError::ExternalNotSupported(*name))?; let command = engine_state.get_decl(decl_id); - let new_context = context.enter_scope(); let mut call = Call::new(); call.positional = [*name] @@ -99,7 +98,7 @@ fn eval_external( }) .collect(); - command.run(&new_context, &call, input) + command.run(&context, &call, input) } pub fn eval_expression( From 29771c7d233b308f06d71bf56efbd823f792952a Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Mon, 20 Sep 2021 10:42:03 +0100 Subject: [PATCH 3/3] clippy errors --- crates/nu-engine/src/eval.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index a054a1b059..30db23220d 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -98,7 +98,7 @@ fn eval_external( }) .collect(); - command.run(&context, &call, input) + command.run(context, &call, input) } pub fn eval_expression(