difftreelog
refactor remove debug gc trace
in: master
5 files changed
crates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth1use crate::error::Error::*;2use crate::error::Result;3use crate::{throw, Val};45#[derive(PartialEq, Clone, Copy)]6pub enum ManifestType {7 // Applied in manifestification8 Manifest,9 /// Used for std.manifestJson10 /// Empty array/objects extends to "[\n\n]" instead of "[ ]" as in manifest11 Std,12 /// No line breaks, used in `obj+''`13 ToString,14 /// Minified json15 Minify,16}1718pub struct ManifestJsonOptions<'s> {19 pub padding: &'s str,20 pub mtype: ManifestType,21}2223pub fn manifest_json_ex(val: &Val, options: &ManifestJsonOptions<'_>) -> Result<String> {24 let mut out = String::new();25 manifest_json_ex_buf(val, &mut out, &mut String::new(), options)?;26 Ok(out)27}28fn manifest_json_ex_buf(29 val: &Val,30 buf: &mut String,31 cur_padding: &mut String,32 options: &ManifestJsonOptions<'_>,33) -> Result<()> {34 use std::fmt::Write;35 let mtype = options.mtype;36 match val {37 Val::Bool(v) => {38 if *v {39 buf.push_str("true");40 } else {41 buf.push_str("false");42 }43 }44 Val::Null => buf.push_str("null"),45 Val::Str(s) => buf.push_str(&escape_string_json(s)),46 Val::Num(n) => write!(buf, "{}", n).unwrap(),47 Val::Arr(items) => {48 buf.push('[');49 if !items.is_empty() {50 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {51 buf.push('\n');52 }5354 let old_len = cur_padding.len();55 cur_padding.push_str(options.padding);56 for (i, item) in items.iter().enumerate() {57 if i != 0 {58 buf.push(',');59 if mtype == ManifestType::ToString {60 buf.push(' ');61 } else if mtype != ManifestType::Minify {62 buf.push('\n');63 }64 }65 buf.push_str(cur_padding);66 manifest_json_ex_buf(&item?, buf, cur_padding, options)?;67 }68 cur_padding.truncate(old_len);6970 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {71 buf.push('\n');72 buf.push_str(cur_padding);73 }74 } else if mtype == ManifestType::Std {75 buf.push_str("\n\n");76 buf.push_str(cur_padding);77 } else if mtype == ManifestType::ToString || mtype == ManifestType::Manifest {78 buf.push(' ');79 }80 buf.push(']');81 }82 Val::Obj(obj) => {83 obj.run_assertions()?;84 buf.push('{');85 let fields = obj.fields();86 if !fields.is_empty() {87 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {88 buf.push('\n');89 }9091 let old_len = cur_padding.len();92 cur_padding.push_str(options.padding);93 for (i, field) in fields.into_iter().enumerate() {94 if i != 0 {95 buf.push(',');96 if mtype == ManifestType::ToString {97 buf.push(' ');98 } else if mtype != ManifestType::Minify {99 buf.push('\n');100 }101 }102 buf.push_str(cur_padding);103 buf.push_str(&escape_string_json(&field));104 buf.push_str(": ");105 crate::push(106 None,107 || format!("field <{}> manifestification", field.clone()),108 || {109 let value = obj.get(field.clone())?.unwrap();110 manifest_json_ex_buf(&value, buf, cur_padding, options)111 },112 )?;113 }114 cur_padding.truncate(old_len);115116 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {117 buf.push('\n');118 buf.push_str(cur_padding);119 }120 } else if mtype == ManifestType::Std {121 buf.push_str("\n\n");122 buf.push_str(cur_padding);123 } else if mtype == ManifestType::ToString || mtype == ManifestType::Manifest {124 buf.push(' ');125 }126 buf.push('}');127 }128 Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),129 Val::DebugGcTraceValue(v) => manifest_json_ex_buf(&v.value, buf, cur_padding, options)?,130 };131 Ok(())132}133pub fn escape_string_json(s: &str) -> String {134 use std::fmt::Write;135 let mut out = String::new();136 out.push('"');137 for c in s.chars() {138 match c {139 '"' => out.push_str("\\\""),140 '\\' => out.push_str("\\\\"),141 '\u{0008}' => out.push_str("\\b"),142 '\u{000c}' => out.push_str("\\f"),143 '\n' => out.push_str("\\n"),144 '\r' => out.push_str("\\r"),145 '\t' => out.push_str("\\t"),146 c if c < 32 as char || (c >= 127 as char && c <= 159 as char) => {147 write!(out, "\\u{:04x}", c as u32).unwrap()148 }149 c => out.push(c),150 }151 }152 out.push('"');153 out154}155156#[test]157fn json_test() {158 assert_eq!(escape_string_json("\u{001f}"), "\"\\u001f\"")159}crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -1,8 +1,8 @@
use crate::{
equals,
error::{Error::*, Result},
- parse_args, primitive_equals, push, throw, with_state, ArrValue, Context, DebugGcTraceValue,
- EvaluationState, FuncVal, LazyVal, Val,
+ parse_args, primitive_equals, push, throw, with_state, ArrValue, Context, EvaluationState,
+ FuncVal, LazyVal, Val,
};
use format::{format_arr, format_obj};
use jrsonnet_gc::Gc;
@@ -69,8 +69,6 @@
("md5".into(), builtin_md5),
("base64".into(), builtin_base64),
("trace".into(), builtin_trace),
- ("gc".into(), builtin_gc),
- ("gcTrace".into(), builtin_gc_trace),
("join".into(), builtin_join),
("escapeStringJson".into(), builtin_escape_string_json),
("manifestJsonEx".into(), builtin_manifest_json_ex),
@@ -446,27 +444,6 @@
}
eprintln!(" {}", str);
Ok(rest)
- })
-}
-
-fn builtin_gc(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
- parse_args!(context, "gc", args, 1, [
- 0, rest: ty!(any);
- ], {
- println!("GC start");
- jrsonnet_gc::force_collect();
- println!("GC done");
-
- Ok(rest)
- })
-}
-
-fn builtin_gc_trace(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
- parse_args!(context, "gcTrace", args, 2, [
- 0, name: ty!(string) => Val::Str;
- 1, rest: ty!(any);
- ], {
- Ok(DebugGcTraceValue::create(name, rest))
})
}
crates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate.rs
@@ -215,9 +215,6 @@
pub fn evaluate_add_op(a: &Val, b: &Val) -> Result<Val> {
Ok(match (a, b) {
- (Val::DebugGcTraceValue(v1), Val::DebugGcTraceValue(v2)) => {
- evaluate_add_op(&v1.value, &v2.value)?
- }
(Val::Str(v1), Val::Str(v2)) => Val::Str(((**v1).to_owned() + v2).into()),
// Can't use generic json serialization way, because it depends on number to string concatenation (std.jsonnet:890)
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -42,7 +42,6 @@
Self::Object(out)
}
Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),
- Val::DebugGcTraceValue(v) => Self::try_from(&*v.value as &Val)?,
})
}
}
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -9,7 +9,7 @@
native::NativeCallback,
throw, with_state, Context, ObjValue, Result,
};
-use jrsonnet_gc::{Finalize, Gc, GcCell, Trace};
+use jrsonnet_gc::{Gc, GcCell, Trace};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{el, Arg, ArgsDesc, Expr, ExprLocation, LiteralType, LocExpr, ParamsDesc};
use jrsonnet_types::ValType;
@@ -345,65 +345,6 @@
}
}
-#[derive(Debug)]
-pub struct DebugGcTraceValue {
- name: IStr,
- pub value: Box<Val>,
-}
-impl DebugGcTraceValue {
- fn print(&self, action: &str) {
- println!("{} {}#{:?}", action, self.name, &*self.value as *const _)
- }
-}
-impl Finalize for DebugGcTraceValue {
- fn finalize(&self) {
- self.print("Garbage-collecting")
- }
-}
-impl Drop for DebugGcTraceValue {
- fn drop(&mut self) {
- self.print("Garbage-collected")
- }
-}
-unsafe impl Trace for DebugGcTraceValue {
- unsafe fn trace(&self) {
- self.print("Traced");
- self.value.trace()
- }
- unsafe fn root(&self) {
- self.print("Rooted");
- self.value.root()
- }
- unsafe fn unroot(&self) {
- self.print("Unrooted");
- self.value.unroot()
- }
- fn finalize_glue(&self) {
- Finalize::finalize(self)
- }
-}
-impl Clone for DebugGcTraceValue {
- fn clone(&self) -> Self {
- self.print("Cloned");
- let value = Self {
- name: self.name.clone(),
- value: self.value.clone(),
- };
- value.print("I'm clone");
- value
- }
-}
-impl DebugGcTraceValue {
- pub fn create(name: IStr, value: Val) -> Val {
- let value = Self {
- name,
- value: Box::new(value),
- };
- value.print("Constructed");
- Val::DebugGcTraceValue(value)
- }
-}
-
#[derive(Debug, Clone, Trace)]
#[trivially_drop]
pub enum Val {
@@ -414,7 +355,6 @@
Arr(ArrValue),
Obj(ObjValue),
Func(Gc<FuncVal>),
- DebugGcTraceValue(DebugGcTraceValue),
}
macro_rules! matches_unwrap {
@@ -471,7 +411,6 @@
Self::Bool(_) => ValType::Bool,
Self::Null => ValType::Null,
Self::Func(..) => ValType::Func,
- Self::DebugGcTraceValue(v) => v.value.value_type(),
}
}