Fix warnings and split Scope (#1902)

This commit is contained in:
Jonathan Turner
2020-05-27 16:50:26 +12:00
committed by GitHub
parent 9567c1f564
commit fa812849b8
29 changed files with 141 additions and 148 deletions

View File

@ -368,7 +368,7 @@ fn create_default_command_args(context: &RunnableContextWithoutInput) -> RawComm
is_last: true,
},
name_tag: context.name.clone(),
scope: Scope::empty(),
scope: Scope::new(),
},
}
}

View File

@ -6,14 +6,16 @@ use crate::stream::InputStream;
use futures::stream::TryStreamExt;
use nu_errors::ShellError;
use nu_protocol::hir::{Block, ClassifiedCommand, Commands};
use nu_protocol::{ReturnSuccess, Scope, UntaggedValue, Value};
use nu_protocol::{ReturnSuccess, UntaggedValue, Value};
use std::sync::atomic::Ordering;
pub(crate) async fn run_block(
block: &Block,
ctx: &mut Context,
mut input: InputStream,
scope: &Scope,
it: &Value,
vars: &IndexMap<String, Value>,
env: &IndexMap<String, String>,
) -> Result<InputStream, ShellError> {
let mut output: Result<InputStream, ShellError> = Ok(InputStream::empty());
for pipeline in &block.block {
@ -52,7 +54,7 @@ pub(crate) async fn run_block(
return Err(e);
}
}
output = run_pipeline(pipeline, ctx, input, scope).await;
output = run_pipeline(pipeline, ctx, input, it, vars, env).await;
input = InputStream::empty();
}
@ -64,10 +66,11 @@ async fn run_pipeline(
commands: &Commands,
ctx: &mut Context,
mut input: InputStream,
scope: &Scope,
it: &Value,
vars: &IndexMap<String, Value>,
env: &IndexMap<String, String>,
) -> Result<InputStream, ShellError> {
let mut iter = commands.list.clone().into_iter().peekable();
loop {
let item: Option<ClassifiedCommand> = iter.next();
let next: Option<&ClassifiedCommand> = iter.peek();
@ -78,13 +81,13 @@ async fn run_pipeline(
}
(Some(ClassifiedCommand::Expr(expr)), _) => {
run_expression_block(*expr, ctx, input, scope).await?
run_expression_block(*expr, ctx, it, vars, env).await?
}
(Some(ClassifiedCommand::Error(err)), _) => return Err(err.into()),
(_, Some(ClassifiedCommand::Error(err))) => return Err(err.clone().into()),
(Some(ClassifiedCommand::Internal(left)), _) => {
run_internal_command(left, ctx, input, scope)?
run_internal_command(left, ctx, input, it, vars, env)?
}
(None, _) => break,

View File

@ -6,22 +6,22 @@ use log::{log_enabled, trace};
use futures::stream::once;
use nu_errors::ShellError;
use nu_protocol::hir::SpannedExpression;
use nu_protocol::Scope;
use nu_protocol::Value;
pub(crate) async fn run_expression_block(
expr: SpannedExpression,
context: &mut Context,
_input: InputStream,
scope: &Scope,
it: &Value,
vars: &IndexMap<String, Value>,
env: &IndexMap<String, String>,
) -> Result<InputStream, ShellError> {
if log_enabled!(log::Level::Trace) {
trace!(target: "nu::run::expr", "->");
trace!(target: "nu::run::expr", "{:?}", expr);
}
let scope = scope.clone();
let registry = context.registry().clone();
let output = evaluate_baseline_expr(&expr, &registry, &scope).await?;
let output = evaluate_baseline_expr(&expr, &registry, it, vars, env).await?;
Ok(once(async { Ok(output) }).to_input_stream())
}

View File

@ -115,7 +115,9 @@ async fn run_with_stdin(
let mut command_args = vec![];
for arg in command.args.iter() {
let value = evaluate_baseline_expr(arg, &context.registry, scope).await?;
let value =
evaluate_baseline_expr(arg, &context.registry, &scope.it, &scope.vars, &scope.env)
.await?;
// Skip any arguments that don't really exist, treating them as optional
// FIXME: we may want to preserve the gap in the future, though it's hard to say
// what value we would put in its place.
@ -509,7 +511,7 @@ mod tests {
let mut ctx = Context::basic().expect("There was a problem creating a basic context.");
assert!(
run_external_command(cmd, &mut ctx, input, &Scope::empty(), false)
run_external_command(cmd, &mut ctx, input, &Scope::new(), false)
.await
.is_err()
);

View File

@ -11,13 +11,20 @@ pub(crate) fn run_internal_command(
command: InternalCommand,
context: &mut Context,
input: InputStream,
scope: &Scope,
it: &Value,
vars: &IndexMap<String, Value>,
env: &IndexMap<String, String>,
) -> Result<InputStream, ShellError> {
if log_enabled!(log::Level::Trace) {
trace!(target: "nu::run::internal", "->");
trace!(target: "nu::run::internal", "{}", command.name);
}
let scope = Scope {
it: it.clone(),
vars: vars.clone(),
env: env.clone(),
};
let objects: InputStream = trace_stream!(target: "nu::trace_stream::internal", "input" = input);
let internal_command = context.expect_command(&command.name);
@ -26,14 +33,14 @@ pub(crate) fn run_internal_command(
internal_command?,
Tag::unknown_anchor(command.name_span),
command.args.clone(),
scope,
&scope,
objects,
)
};
let mut result = trace_out_stream!(target: "nu::trace_stream::internal", "output" = result);
let mut context = context.clone();
let scope = scope.clone();
// let scope = scope.clone();
let stream = async_stream! {
let mut soft_errs: Vec<ShellError> = vec![];

View File

@ -35,7 +35,7 @@ impl UnevaluatedCallInfo {
it: &Value,
) -> Result<CallInfo, ShellError> {
let mut scope = self.scope.clone();
scope = scope.set_it(it.clone());
scope.it = it.clone();
let args = evaluate_args(&self.args, registry, &scope).await?;
Ok(CallInfo {

View File

@ -84,19 +84,20 @@ fn each(raw_args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStrea
let (each_args, mut input): (EachArgs, _) = raw_args.process(&registry).await?;
let block = each_args.block;
while let Some(input) = input.next().await {
let input_clone = input.clone();
let input_stream = if is_expanded_it_usage(&head) {
InputStream::empty()
} else {
once(async { Ok(input) }).to_input_stream()
once(async { Ok(input_clone) }).to_input_stream()
};
let result = run_block(
&block,
&mut context,
input_stream,
&scope.clone().set_it(input_clone),
&input,
&scope.vars,
&scope.env
).await;
match result {

View File

@ -62,7 +62,6 @@ fn format_command(
let commands = format_pattern;
while let Some(value) = input.next().await {
let scope = scope.clone().set_it(value);
let mut output = String::new();
for command in &commands {
@ -74,7 +73,7 @@ fn format_command(
// FIXME: use the correct spans
let full_column_path = nu_parser::parse_full_column_path(&(c.to_string()).spanned(Span::unknown()), &registry);
let result = evaluate_baseline_expr(&full_column_path.0, &registry, &scope).await;
let result = evaluate_baseline_expr(&full_column_path.0, &registry, &value, &scope.vars, &scope.env).await;
if let Ok(c) = result {
output

View File

@ -89,7 +89,7 @@ impl WholeStreamCommand for KeepUntil {
let condition = condition.clone();
trace!("ITEM = {:?}", item);
let result =
evaluate_baseline_expr(&*condition, &registry, &scope.clone().set_it(item.clone()))
evaluate_baseline_expr(&*condition, &registry, &item, &scope.vars, &scope.env)
.await;
trace!("RESULT = {:?}", result);

View File

@ -89,7 +89,7 @@ impl WholeStreamCommand for KeepWhile {
let condition = condition.clone();
trace!("ITEM = {:?}", item);
let result =
evaluate_baseline_expr(&*condition, &registry, &scope.clone().set_it(item.clone()))
evaluate_baseline_expr(&*condition, &registry, &item, &scope.vars, &scope.env)
.await;
trace!("RESULT = {:?}", result);

View File

@ -50,17 +50,19 @@ impl WholeStreamCommand for Merge {
fn merge(raw_args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let scope = raw_args.call_info.scope.clone();
let stream = async_stream! {
let mut context = Context::from_raw(&raw_args, &registry);
let name_tag = raw_args.call_info.name_tag.clone();
let scope = raw_args.call_info.scope.clone();
let (merge_args, mut input): (MergeArgs, _) = raw_args.process(&registry).await?;
let block = merge_args.block;
let table: Option<Vec<Value>> = match run_block(&block,
&mut context,
InputStream::empty(),
&scope).await {
&scope.it,
&scope.vars,
&scope.env).await {
Ok(mut stream) => Some(stream.drain_vec().await),
Err(err) => {
yield Err(err);

View File

@ -50,7 +50,7 @@ impl WholeStreamCommand for AliasCommand {
let evaluated = call_info.evaluate(&registry).await?;
if let Some(positional) = &evaluated.args.positional {
for (pos, arg) in positional.iter().enumerate() {
scope = scope.set_var(alias_command.args[pos].to_string(), arg.clone());
scope.vars.insert(alias_command.args[pos].to_string(), arg.clone());
}
}
@ -58,7 +58,9 @@ impl WholeStreamCommand for AliasCommand {
&block,
&mut context,
input,
&scope,
&scope.it,
&scope.vars,
&scope.env,
).await;
match result {

View File

@ -90,7 +90,7 @@ impl WholeStreamCommand for SkipUntil {
let condition = condition.clone();
trace!("ITEM = {:?}", item);
let result =
evaluate_baseline_expr(&*condition, &registry, &scope.clone().set_it(item.clone()))
evaluate_baseline_expr(&*condition, &registry, &item, &scope.vars, &scope.env)
.await;
trace!("RESULT = {:?}", result);

View File

@ -90,7 +90,7 @@ impl WholeStreamCommand for SkipWhile {
let condition = condition.clone();
trace!("ITEM = {:?}", item);
let result =
evaluate_baseline_expr(&*condition, &registry, &scope.clone().set_it(item.clone()))
evaluate_baseline_expr(&*condition, &registry, &item, &scope.vars, &scope.env)
.await;
trace!("RESULT = {:?}", result);

View File

@ -62,14 +62,15 @@ fn update(raw_args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStr
tag,
} => {
let for_block = input.clone();
let input_clone = input.clone();
let input_stream = once(async { Ok(for_block) }).to_input_stream();
let result = run_block(
&block,
&mut context,
input_stream,
&scope.clone().set_it(input_clone),
&input,
&scope.vars,
&scope.env
).await;
match result {

View File

@ -67,10 +67,9 @@ fn where_command(
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let scope = raw_args.call_info.scope.clone();
let tag = raw_args.call_info.name_tag.clone();
let stream = async_stream! {
let tag = raw_args.call_info.name_tag.clone();
let scope = raw_args.call_info.scope.clone();
let (WhereArgs { block }, mut input) = raw_args.process(&registry).await?;
let condition = {
if block.block.len() != 1 {
@ -108,7 +107,7 @@ fn where_command(
while let Some(input) = input.next().await {
//FIXME: should we use the scope that's brought in as well?
let condition = evaluate_baseline_expr(&condition, &registry, &scope.clone().set_it(input.clone())).await?;
let condition = evaluate_baseline_expr(&condition, &registry, &input, &scope.vars, &scope.env).await?;
match condition.as_bool() {
Ok(b) => {

View File

@ -57,18 +57,21 @@ fn with_env(raw_args: CommandArgs, registry: &CommandRegistry) -> Result<OutputS
let stream = async_stream! {
let mut context = Context::from_raw(&raw_args, &registry);
let scope = raw_args
let mut scope = raw_args
.call_info
.scope
.clone();
let (WithEnvArgs { variable, block }, mut input) = raw_args.process(&registry).await?;
let scope = scope.set_env_var(variable.0.item, variable.1.item);
scope.env.insert(variable.0.item, variable.1.item);
let result = run_block(
&block,
&mut context,
input,
&scope.clone(),
&scope.it,
&scope.vars,
&scope.env,
).await;
match result {