Skip to content

Commit

Permalink
fix: Prevent printing out the contents of functions to the repl when …
Browse files Browse the repository at this point in the history
…debug status is not set
  • Loading branch information
oooooba committed Oct 21, 2018
1 parent 9279778 commit 3824695
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
3 changes: 2 additions & 1 deletion repl/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,10 @@ fn eval_line_(
.map(move |ExecuteValue { value, typ, .. }| {
let vm = value.vm();
let env = vm.global_env().get_env();
let debug_level = vm.global_env().get_debug_level();
println!(
"{}",
ValuePrinter::new(&*env, &typ, value.get_variant())
ValuePrinter::new(&*env, &typ, value.get_variant(), &*debug_level)
.width(80)
.max_level(5)
);
Expand Down
3 changes: 2 additions & 1 deletion src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,9 @@ fn run_expr(
Ok(execute_value) => {
let env = vm.global_env().get_env();
let typ = execute_value.typ;
let debug_level = vm.global_env().get_debug_level();
IO::Value(record_no_decl!{
value => ValuePrinter::new(&*env, &typ, execute_value.value.get_variant()).width(80).to_string(),
value => ValuePrinter::new(&*env, &typ, execute_value.value.get_variant(), &*debug_level).width(80).to_string(),
typ => typ.to_string()
})
}
Expand Down
30 changes: 24 additions & 6 deletions vm/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use base::fnv::FnvMap;
use base::symbol::Symbol;
use base::types::pretty_print::ident as pretty_ident;
use base::types::{ArcType, Type, TypeEnv};
use base::DebugLevel;
use types::*;

use array::Array;
Expand Down Expand Up @@ -539,16 +540,23 @@ pub struct ValuePrinter<'a> {
pub value: Variants<'a>,
pub max_level: i32,
pub width: usize,
pub debug_level: &'a DebugLevel,
}

impl<'t> ValuePrinter<'t> {
pub fn new(env: &'t TypeEnv, typ: &'t ArcType, value: Variants<'t>) -> ValuePrinter<'t> {
pub fn new(
env: &'t TypeEnv,
typ: &'t ArcType,
value: Variants<'t>,
debug_level: &'t DebugLevel,
) -> ValuePrinter<'t> {
ValuePrinter {
typ,
env,
value,
max_level: 50,
width: 80,
debug_level,
}
}

Expand All @@ -571,6 +579,7 @@ struct InternalPrinter<'a, 't> {
arena: &'a Arena<'a>,
prec: Prec,
level: i32,
debug_level: &'t DebugLevel,
}

impl<'a> fmt::Display for ValuePrinter<'a> {
Expand All @@ -583,6 +592,7 @@ impl<'a> fmt::Display for ValuePrinter<'a> {
arena: &arena,
prec: Top,
level: self.max_level,
debug_level: self.debug_level,
}.pretty(self.value)
.group()
.1
Expand All @@ -607,7 +617,13 @@ impl<'a, 't> InternalPrinter<'a, 't> {
function.id.declared_name().to_string(),
">"
],
ValueRepr::Closure(ref closure) => chain![arena;
ValueRepr::Closure(ref closure) => match self.debug_level {
&DebugLevel::None => chain![arena;
"<",
arena.text(closure.function.name.declared_name().to_string()),
">"
],
&DebugLevel::Low | &DebugLevel::High => chain![arena;
"<",
arena.text(closure.function.name.declared_name().to_string()),
arena.concat(variant_iter(&closure.upvars).zip(&closure.function.debug_info.upvars)
Expand All @@ -622,6 +638,7 @@ impl<'a, 't> InternalPrinter<'a, 't> {
}).intersperse(arena.text(","))).nest(INDENT),
">"
],
},
ValueRepr::Array(ref array) => chain![arena;
"[",
arena.concat(array.iter().map(|field| {
Expand Down Expand Up @@ -752,6 +769,7 @@ impl<'a, 't> InternalPrinter<'a, 't> {
arena: self.arena,
prec: prec,
level: self.level - 1,
debug_level: self.debug_level,
}
}
}
Expand Down Expand Up @@ -1600,7 +1618,7 @@ mod tests {
assert_eq!(
format!(
"{}",
ValuePrinter::new(&env, &typ, unsafe { Variants::new(&nil) })
ValuePrinter::new(&env, &typ, unsafe { Variants::new(&nil) }, &DebugLevel::None)
),
"Nil"
);
Expand All @@ -1613,7 +1631,7 @@ mod tests {
assert_eq!(
format!(
"{}",
ValuePrinter::new(&env, &typ, unsafe { Variants::new(&list1) })
ValuePrinter::new(&env, &typ, unsafe { Variants::new(&list1) }, &DebugLevel::None)
),
"Cons 123 Nil"
);
Expand All @@ -1626,7 +1644,7 @@ mod tests {
assert_eq!(
format!(
"{}",
ValuePrinter::new(&env, &typ, unsafe { Variants::new(&list2) })
ValuePrinter::new(&env, &typ, unsafe { Variants::new(&list2) }, &DebugLevel::None)
),
"Cons 0 (Cons 123 Nil)"
);
Expand All @@ -1644,7 +1662,7 @@ mod tests {
assert_eq!(
format!(
"{}",
ValuePrinter::new(&env, &typ, unsafe { Variants::new(&nil) })
ValuePrinter::new(&env, &typ, unsafe { Variants::new(&nil) }, &DebugLevel::None)
),
"[1, 2, 3]"
);
Expand Down

0 comments on commit 3824695

Please sign in to comment.