difftreelog
refactor switch to jrsonnet-gc
in: master
27 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -100,27 +100,6 @@
]
[[package]]
-name = "gc"
-version = "0.4.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3edaac0f5832202ebc99520cb77c932248010c4645d20be1dc62d6579f5b3752"
-dependencies = [
- "gc_derive",
-]
-
-[[package]]
-name = "gc_derive"
-version = "0.4.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "60df8444f094ff7885631d80e78eb7d88c3c2361a98daaabb06256e4500db941"
-dependencies = [
- "proc-macro2",
- "quote",
- "syn",
- "synstructure",
-]
-
-[[package]]
name = "hashbrown"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -190,7 +169,7 @@
"anyhow",
"base64",
"bincode",
- "gc",
+ "jrsonnet-gc",
"jrsonnet-interner",
"jrsonnet-parser",
"jrsonnet-stdlib",
@@ -204,10 +183,31 @@
]
[[package]]
+name = "jrsonnet-gc"
+version = "0.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "68da8bc2f00117b1373bb8877af03b1d391e4c4800e6585d7279e5b99c919dde"
+dependencies = [
+ "jrsonnet-gc-derive",
+]
+
+[[package]]
+name = "jrsonnet-gc-derive"
+version = "0.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "adcba9c387b64b054f06cc4d724905296e21edeeb7506847f3299117a2d92d12"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+ "synstructure",
+]
+
+[[package]]
name = "jrsonnet-interner"
version = "0.3.8"
dependencies = [
- "gc",
+ "jrsonnet-gc",
"rustc-hash",
"serde",
]
@@ -216,7 +216,7 @@
name = "jrsonnet-parser"
version = "0.3.8"
dependencies = [
- "gc",
+ "jrsonnet-gc",
"jrsonnet-interner",
"jrsonnet-stdlib",
"peg",
@@ -232,7 +232,7 @@
name = "jrsonnet-types"
version = "0.3.8"
dependencies = [
- "gc",
+ "jrsonnet-gc",
"peg",
]
@@ -240,8 +240,8 @@
name = "jsonnet"
version = "0.3.8"
dependencies = [
- "gc",
"jrsonnet-evaluator",
+ "jrsonnet-gc",
"jrsonnet-parser",
]
bindings/jsonnet/Cargo.tomldiffbeforeafterboth--- a/bindings/jsonnet/Cargo.toml
+++ b/bindings/jsonnet/Cargo.toml
@@ -10,7 +10,7 @@
[dependencies]
jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "0.3.8" }
jrsonnet-parser = { path = "../../crates/jrsonnet-parser", version = "0.3.8" }
-gc = { version = "0.4.1", features = ["derive"] }
+jrsonnet-gc = { version = "0.4.2", features = ["derive"] }
[lib]
crate-type = ["cdylib"]
bindings/jsonnet/src/native.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/native.rs
+++ b/bindings/jsonnet/src/native.rs
@@ -1,9 +1,9 @@
-use gc::{unsafe_empty_trace, Finalize, Gc, Trace};
use jrsonnet_evaluator::{
error::{Error, LocError},
native::{NativeCallback, NativeCallbackHandler},
EvaluationState, Val,
};
+use jrsonnet_gc::{unsafe_empty_trace, Finalize, Gc, Trace};
use jrsonnet_parser::{Param, ParamsDesc};
use std::{
ffi::{c_void, CStr},
bindings/jsonnet/src/val_make.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/val_make.rs
+++ b/bindings/jsonnet/src/val_make.rs
@@ -1,7 +1,7 @@
//! Create values in VM
-use gc::Gc;
use jrsonnet_evaluator::{ArrValue, EvaluationState, ObjValue, Val};
+use jrsonnet_gc::Gc;
use std::{
ffi::CStr,
os::raw::{c_char, c_double, c_int},
bindings/jsonnet/src/val_modify.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/val_modify.rs
+++ b/bindings/jsonnet/src/val_modify.rs
@@ -2,8 +2,8 @@
//! Only tested with variables, which haven't altered by code before appearing here
//! In jrsonnet every value is immutable, and this code is probally broken
-use gc::Gc;
use jrsonnet_evaluator::{ArrValue, EvaluationState, LazyBinding, LazyVal, ObjMember, Val};
+use jrsonnet_gc::Gc;
use jrsonnet_parser::Visibility;
use std::{ffi::CStr, os::raw::c_char};
crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -35,7 +35,7 @@
rustc-hash = "1.1.0"
thiserror = "1.0"
-gc = { version = "0.4.1", features = ["derive"] }
+jrsonnet-gc = { version = "0.4.2", features = ["derive"] }
[dependencies.anyhow]
version = "1.0"
crates/jrsonnet-evaluator/src/builtin/format.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/format.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/format.rs
@@ -2,12 +2,13 @@
#![allow(clippy::too_many_arguments)]
use crate::{error::Error::*, throw, LocError, ObjValue, Result, Val};
-use gc::{Finalize, Trace};
+use jrsonnet_gc::Trace;
use jrsonnet_interner::IStr;
use jrsonnet_types::ValType;
use thiserror::Error;
-#[derive(Debug, Clone, Error, Trace, Finalize)]
+#[derive(Debug, Clone, Error, Trace)]
+#[trivially_drop]
pub enum FormatError {
#[error("truncated format code")]
TruncatedFormatCode,
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth1use crate::{2 equals,3 error::{Error::*, Result},4 parse_args, primitive_equals, push, throw, with_state, ArrValue, Context, DebugGcTraceValue,5 EvaluationState, FuncVal, LazyVal, Val,6};7use format::{format_arr, format_obj};8use gc::Gc;9use jrsonnet_interner::IStr;10use jrsonnet_parser::{ArgsDesc, BinaryOpType, ExprLocation};11use jrsonnet_types::ty;12use std::{collections::HashMap, path::PathBuf, rc::Rc};1314pub mod stdlib;15pub use stdlib::*;1617use self::manifest::{escape_string_json, manifest_json_ex, ManifestJsonOptions, ManifestType};1819pub mod format;20pub mod manifest;21pub mod sort;2223fn std_format(str: IStr, vals: Val) -> Result<Val> {24 push(25 Some(&ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0)),26 || format!("std.format of {}", str),27 || {28 Ok(match vals {29 Val::Arr(vals) => Val::Str(format_arr(&str, &vals.evaluated()?)?.into()),30 Val::Obj(obj) => Val::Str(format_obj(&str, &obj)?.into()),31 o => Val::Str(format_arr(&str, &[o])?.into()),32 })33 },34 )35}3637type Builtin = fn(context: Context, loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val>;3839type BuiltinsType = HashMap<Box<str>, Builtin>;4041thread_local! {42 static BUILTINS: BuiltinsType = {43 [44 ("length".into(), builtin_length as Builtin),45 ("type".into(), builtin_type),46 ("makeArray".into(), builtin_make_array),47 ("codepoint".into(), builtin_codepoint),48 ("objectFieldsEx".into(), builtin_object_fields_ex),49 ("objectHasEx".into(), builtin_object_has_ex),50 ("slice".into(), builtin_slice),51 ("primitiveEquals".into(), builtin_primitive_equals),52 ("equals".into(), builtin_equals),53 ("modulo".into(), builtin_modulo),54 ("mod".into(), builtin_mod),55 ("floor".into(), builtin_floor),56 ("log".into(), builtin_log),57 ("pow".into(), builtin_pow),58 ("extVar".into(), builtin_ext_var),59 ("native".into(), builtin_native),60 ("filter".into(), builtin_filter),61 ("map".into(), builtin_map),62 ("foldl".into(), builtin_foldl),63 ("foldr".into(), builtin_foldr),64 ("sortImpl".into(), builtin_sort_impl),65 ("format".into(), builtin_format),66 ("range".into(), builtin_range),67 ("char".into(), builtin_char),68 ("encodeUTF8".into(), builtin_encode_utf8),69 ("md5".into(), builtin_md5),70 ("base64".into(), builtin_base64),71 ("trace".into(), builtin_trace),72 ("gc".into(), builtin_gc),73 ("gcTrace".into(), builtin_gc_trace),74 ("join".into(), builtin_join),75 ("escapeStringJson".into(), builtin_escape_string_json),76 ("manifestJsonEx".into(), builtin_manifest_json_ex),77 ("reverse".into(), builtin_reverse),78 ("id".into(), builtin_id),79 ("strReplace".into(), builtin_str_replace),80 ("parseJson".into(), builtin_parse_json),81 ].iter().cloned().collect()82 };83}8485fn builtin_length(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {86 parse_args!(context, "length", args, 1, [87 0, x: ty!((string | object | array));88 ], {89 Ok(match x {90 Val::Str(n) => Val::Num(n.chars().count() as f64),91 Val::Arr(a) => Val::Num(a.len() as f64),92 Val::Obj(o) => Val::Num(93 o.fields_visibility()94 .into_iter()95 .filter(|(_k, v)| *v)96 .count() as f64,97 ),98 _ => unreachable!(),99 })100 })101}102103fn builtin_type(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {104 parse_args!(context, "type", args, 1, [105 0, x: ty!(any);106 ], {107 Ok(Val::Str(x.value_type().name().into()))108 })109}110111fn builtin_make_array(112 context: Context,113 _loc: Option<&ExprLocation>,114 args: &ArgsDesc,115) -> Result<Val> {116 parse_args!(context, "makeArray", args, 2, [117 0, sz: ty!(BoundedNumber<(Some(0.0)), (None)>) => Val::Num;118 1, func: ty!(function) => Val::Func;119 ], {120 let mut out = Vec::with_capacity(sz as usize);121 for i in 0..sz as usize {122 out.push(LazyVal::new_resolved(func.evaluate_values(123 context.clone(),124 &[Val::Num(i as f64)]125 )?))126 }127 Ok(Val::Arr(out.into()))128 })129}130131fn builtin_codepoint(132 context: Context,133 _loc: Option<&ExprLocation>,134 args: &ArgsDesc,135) -> Result<Val> {136 parse_args!(context, "codepoint", args, 1, [137 0, str: ty!(char) => Val::Str;138 ], {139 Ok(Val::Num(str.chars().next().unwrap() as u32 as f64))140 })141}142143fn builtin_object_fields_ex(144 context: Context,145 _loc: Option<&ExprLocation>,146 args: &ArgsDesc,147) -> Result<Val> {148 parse_args!(context, "objectFieldsEx", args, 2, [149 0, obj: ty!(object) => Val::Obj;150 1, inc_hidden: ty!(boolean) => Val::Bool;151 ], {152 let out = obj.fields_ex(inc_hidden);153 Ok(Val::Arr(out.into_iter().map(Val::Str).collect::<Vec<_>>().into()))154 })155}156157fn builtin_object_has_ex(158 context: Context,159 _loc: Option<&ExprLocation>,160 args: &ArgsDesc,161) -> Result<Val> {162 parse_args!(context, "objectHasEx", args, 3, [163 0, obj: ty!(object) => Val::Obj;164 1, f: ty!(string) => Val::Str;165 2, inc_hidden: ty!(boolean) => Val::Bool;166 ], {167 Ok(Val::Bool(obj.has_field_ex(f, inc_hidden)))168 })169}170171fn builtin_parse_json(172 context: Context,173 _loc: Option<&ExprLocation>,174 args: &ArgsDesc,175) -> Result<Val> {176 parse_args!(context, "parseJson", args, 1, [177 0, s: ty!(string) => Val::Str;178 ], {179 let state = EvaluationState::default();180 let path = PathBuf::from("std.parseJson").into();181 state.evaluate_snippet_raw(path ,s)182 })183}184185// faster186fn builtin_slice(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {187 parse_args!(context, "slice", args, 4, [188 0, indexable: ty!((string | array));189 1, index: ty!((number | null));190 2, end: ty!((number | null));191 3, step: ty!((number | null));192 ], {193 let index = match index {194 Val::Num(v) => v as usize,195 Val::Null => 0,196 _ => unreachable!(),197 };198 let end = match end {199 Val::Num(v) => v as usize,200 Val::Null => match &indexable {201 Val::Str(s) => s.chars().count(),202 Val::Arr(v) => v.len(),203 _ => unreachable!()204 },205 _ => unreachable!()206 };207 let step = match step {208 Val::Num(v) => v as usize,209 Val::Null => 1,210 _ => unreachable!()211 };212 match &indexable {213 Val::Str(s) => {214 Ok(Val::Str((s.chars().skip(index).take(end-index).step_by(step).collect::<String>()).into()))215 }216 Val::Arr(arr) => {217 Ok(Val::Arr((arr.iter().skip(index).take(end-index).step_by(step).collect::<Result<Vec<Val>>>()?).into()))218 }219 _ => unreachable!()220 }221 })222}223224// faster225fn builtin_primitive_equals(226 context: Context,227 _loc: Option<&ExprLocation>,228 args: &ArgsDesc,229) -> Result<Val> {230 parse_args!(context, "primitiveEquals", args, 2, [231 0, a: ty!(any);232 1, b: ty!(any);233 ], {234 Ok(Val::Bool(primitive_equals(&a, &b)?))235 })236}237238// faster239fn builtin_equals(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {240 parse_args!(context, "equals", args, 2, [241 0, a: ty!(any);242 1, b: ty!(any);243 ], {244 Ok(Val::Bool(equals(&a, &b)?))245 })246}247248fn builtin_modulo(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {249 parse_args!(context, "modulo", args, 2, [250 0, a: ty!(number) => Val::Num;251 1, b: ty!(number) => Val::Num;252 ], {253 Ok(Val::Num(a % b))254 })255}256257fn builtin_mod(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {258 parse_args!(context, "mod", args, 2, [259 0, a: ty!((number | string));260 1, b: ty!(any);261 ], {262 match (a, b) {263 (Val::Num(a), Val::Num(b)) => Ok(Val::Num(a % b)),264 (Val::Str(str), vals) => std_format(str, vals),265 (a, b) => throw!(BinaryOperatorDoesNotOperateOnValues(BinaryOpType::Mod, a.value_type(), b.value_type()))266 }267 })268}269270fn builtin_floor(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {271 parse_args!(context, "floor", args, 1, [272 0, x: ty!(number) => Val::Num;273 ], {274 Ok(Val::Num(x.floor()))275 })276}277278fn builtin_log(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {279 parse_args!(context, "log", args, 1, [280 0, n: ty!(number) => Val::Num;281 ], {282 Ok(Val::Num(n.ln()))283 })284}285286fn builtin_pow(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {287 parse_args!(context, "pow", args, 2, [288 0, x: ty!(number) => Val::Num;289 1, n: ty!(number) => Val::Num;290 ], {291 Ok(Val::Num(x.powf(n)))292 })293}294295fn builtin_ext_var(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {296 parse_args!(context, "extVar", args, 1, [297 0, x: ty!(string) => Val::Str;298 ], {299 Ok(with_state(|s| s.settings().ext_vars.get(&x).cloned()).ok_or(UndefinedExternalVariable(x))?)300 })301}302303fn builtin_native(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {304 parse_args!(context, "native", args, 1, [305 0, x: ty!(string) => Val::Str;306 ], {307 Ok(with_state(|s| s.settings().ext_natives.get(&x).cloned()).map(|v| Val::Func(Gc::new(FuncVal::NativeExt(x.clone(), v)))).ok_or(UndefinedExternalFunction(x))?)308 })309}310311fn builtin_filter(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {312 parse_args!(context, "filter", args, 2, [313 0, func: ty!(function) => Val::Func;314 1, arr: ty!(array) => Val::Arr;315 ], {316 Ok(Val::Arr(arr.filter(|val| func317 .evaluate_values(context.clone(), &[val.clone()])?318 .try_cast_bool("filter predicate"))?))319 })320}321322fn builtin_map(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {323 parse_args!(context, "map", args, 2, [324 0, func: ty!(function) => Val::Func;325 1, arr: ty!(array) => Val::Arr;326 ], {327 Ok(Val::Arr(arr.map(|val| func328 .evaluate_values(context.clone(), &[val]))?))329 })330}331332fn builtin_foldl(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {333 parse_args!(context, "foldl", args, 3, [334 0, func: ty!(function) => Val::Func;335 1, arr: ty!(array) => Val::Arr;336 2, init: ty!(any);337 ], {338 let mut acc = init;339 for i in arr.iter() {340 acc = func.evaluate_values(context.clone(), &[acc, i?])?;341 }342 Ok(acc)343 })344}345346fn builtin_foldr(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {347 parse_args!(context, "foldr", args, 3, [348 0, func: ty!(function) => Val::Func;349 1, arr: ty!(array) => Val::Arr;350 2, init: ty!(any);351 ], {352 let mut acc = init;353 for i in arr.iter().rev() {354 acc = func.evaluate_values(context.clone(), &[acc, i?])?;355 }356 Ok(acc)357 })358}359360#[allow(non_snake_case)]361fn builtin_sort_impl(362 context: Context,363 _loc: Option<&ExprLocation>,364 args: &ArgsDesc,365) -> Result<Val> {366 parse_args!(context, "sort", args, 2, [367 0, arr: ty!(array) => Val::Arr;368 1, keyF: ty!(function) => Val::Func;369 ], {370 if arr.len() <= 1 {371 return Ok(Val::Arr(arr))372 }373 Ok(Val::Arr(ArrValue::Eager(sort::sort(context, arr.evaluated()?, &keyF)?)))374 })375}376377// faster378fn builtin_format(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {379 parse_args!(context, "format", args, 2, [380 0, str: ty!(string) => Val::Str;381 1, vals: ty!(any)382 ], {383 std_format(str, vals)384 })385}386387fn builtin_range(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {388 parse_args!(context, "range", args, 2, [389 0, from: ty!(number) => Val::Num;390 1, to: ty!(number) => Val::Num;391 ], {392 if to < from {393 return Ok(Val::Arr(ArrValue::new_eager()))394 }395 let mut out = Vec::with_capacity((1+to as usize-from as usize).max(0));396 for i in from as usize..=to as usize {397 out.push(Val::Num(i as f64));398 }399 Ok(Val::Arr(out.into()))400 })401}402403fn builtin_char(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {404 parse_args!(context, "char", args, 1, [405 0, n: ty!(number) => Val::Num;406 ], {407 let mut out = String::new();408 out.push(std::char::from_u32(n as u32).ok_or_else(||409 InvalidUnicodeCodepointGot(n as u32)410 )?);411 Ok(Val::Str(out.into()))412 })413}414415fn builtin_encode_utf8(416 context: Context,417 _loc: Option<&ExprLocation>,418 args: &ArgsDesc,419) -> Result<Val> {420 parse_args!(context, "encodeUTF8", args, 1, [421 0, str: ty!(string) => Val::Str;422 ], {423 Ok(Val::Arr((str.bytes().map(|b| Val::Num(b as f64)).collect::<Vec<Val>>()).into()))424 })425}426427fn builtin_md5(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {428 parse_args!(context, "md5", args, 1, [429 0, str: ty!(string) => Val::Str;430 ], {431 Ok(Val::Str(format!("{:x}", md5::compute(&str.as_bytes())).into()))432 })433}434435fn builtin_trace(context: Context, loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {436 parse_args!(context, "trace", args, 2, [437 0, str: ty!(string) => Val::Str;438 1, rest: ty!(any);439 ], {440 eprint!("TRACE:");441 if let Some(loc) = loc {442 with_state(|s|{443 let locs = s.map_source_locations(&loc.0, &[loc.1]);444 eprint!(" {}:{}", loc.0.file_name().unwrap().to_str().unwrap(), locs[0].line);445 });446 }447 eprintln!(" {}", str);448 Ok(rest)449 })450}451452fn builtin_gc(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {453 parse_args!(context, "gc", args, 1, [454 0, rest: ty!(any);455 ], {456 println!("GC start");457 gc::force_collect();458 println!("GC done");459460 Ok(rest)461 })462}463464fn builtin_gc_trace(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {465 parse_args!(context, "gcTrace", args, 2, [466 0, name: ty!(string) => Val::Str;467 1, rest: ty!(any);468 ], {469 Ok(DebugGcTraceValue::create(name, rest))470 })471}472473fn builtin_base64(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {474 parse_args!(context, "base64", args, 1, [475 0, input: ty!((string | (Array<number>)));476 ], {477 Ok(Val::Str(match input {478 Val::Str(s) => {479 base64::encode(s.bytes().collect::<Vec<_>>()).into()480 },481 Val::Arr(a) => {482 base64::encode(a.iter().map(|v| {483 Ok(v?.unwrap_num()? as u8)484 }).collect::<Result<Vec<_>>>()?).into()485 },486 _ => unreachable!()487 }))488 })489}490491fn builtin_join(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {492 parse_args!(context, "join", args, 2, [493 0, sep: ty!((string | array));494 1, arr: ty!(array) => Val::Arr;495 ], {496 Ok(match sep {497 Val::Arr(joiner_items) => {498 let mut out = Vec::new();499500 let mut first = true;501 for item in arr.iter() {502 let item = item?.clone();503 if let Val::Arr(items) = item {504 if !first {505 out.reserve(joiner_items.len());506 // TODO: extend507 for item in joiner_items.iter() {508 out.push(item?);509 }510 }511 first = false;512 out.reserve(items.len());513 // TODO: extend514 for item in items.iter() {515 out.push(item?);516 }517 } else {518 throw!(RuntimeError("in std.join all items should be arrays".into()));519 }520 }521522 Val::Arr(out.into())523 },524 Val::Str(sep) => {525 let mut out = String::new();526527 let mut first = true;528 for item in arr.iter() {529 let item = item?.clone();530 if let Val::Str(item) = item {531 if !first {532 out += &sep;533 }534 first = false;535 out += &item;536 } else {537 throw!(RuntimeError("in std.join all items should be strings".into()));538 }539 }540541 Val::Str(out.into())542 },543 _ => unreachable!()544 })545 })546}547548// faster549fn builtin_escape_string_json(550 context: Context,551 _loc: Option<&ExprLocation>,552 args: &ArgsDesc,553) -> Result<Val> {554 parse_args!(context, "escapeStringJson", args, 1, [555 0, str_: ty!(string) => Val::Str;556 ], {557 Ok(Val::Str(escape_string_json(&str_).into()))558 })559}560561// faster562fn builtin_manifest_json_ex(563 context: Context,564 _loc: Option<&ExprLocation>,565 args: &ArgsDesc,566) -> Result<Val> {567 parse_args!(context, "manifestJsonEx", args, 2, [568 0, value: ty!(any);569 1, indent: ty!(string) => Val::Str;570 ], {571 Ok(Val::Str(manifest_json_ex(&value, &ManifestJsonOptions {572 padding: &indent,573 mtype: ManifestType::Std,574 })?.into()))575 })576}577578// faster579fn builtin_reverse(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {580 parse_args!(context, "reverse", args, 1, [581 0, value: ty!(array) => Val::Arr;582 ], {583 Ok(Val::Arr(value.reversed()))584 })585}586587fn builtin_id(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {588 parse_args!(context, "id", args, 1, [589 0, v: ty!(any);590 ], {591 Ok(v)592 })593}594595// faster596fn builtin_str_replace(597 context: Context,598 _loc: Option<&ExprLocation>,599 args: &ArgsDesc,600) -> Result<Val> {601 parse_args!(context, "strReplace", args, 3, [602 0, str: ty!(string) => Val::Str;603 1, from: ty!(string) => Val::Str;604 2, to: ty!(string) => Val::Str;605 ], {606 let mut out = String::new();607 let mut last_idx = 0;608 while let Some(idx) = (&str[last_idx..]).find(&from as &str) {609 out.push_str(&str[last_idx..last_idx+idx]);610 out.push_str(&to);611 last_idx += idx + from.len();612 }613 if last_idx == 0 {614 return Ok(Val::Str(str))615 }616 out.push_str(&str[last_idx..]);617 Ok(Val::Str(out.into()))618 })619}620621pub fn call_builtin(622 context: Context,623 loc: Option<&ExprLocation>,624 name: &str,625 args: &ArgsDesc,626) -> Result<Val> {627 BUILTINS628 .with(|builtins| builtins.get(name).copied())629 .ok_or_else(|| IntrinsicNotFound(name.into()))?(context, loc, args)630}1use crate::{2 equals,3 error::{Error::*, Result},4 parse_args, primitive_equals, push, throw, with_state, ArrValue, Context, DebugGcTraceValue,5 EvaluationState, FuncVal, LazyVal, Val,6};7use format::{format_arr, format_obj};8use jrsonnet_gc::Gc;9use jrsonnet_interner::IStr;10use jrsonnet_parser::{ArgsDesc, BinaryOpType, ExprLocation};11use jrsonnet_types::ty;12use std::{collections::HashMap, path::PathBuf, rc::Rc};1314pub mod stdlib;15pub use stdlib::*;1617use self::manifest::{escape_string_json, manifest_json_ex, ManifestJsonOptions, ManifestType};1819pub mod format;20pub mod manifest;21pub mod sort;2223fn std_format(str: IStr, vals: Val) -> Result<Val> {24 push(25 Some(&ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0)),26 || format!("std.format of {}", str),27 || {28 Ok(match vals {29 Val::Arr(vals) => Val::Str(format_arr(&str, &vals.evaluated()?)?.into()),30 Val::Obj(obj) => Val::Str(format_obj(&str, &obj)?.into()),31 o => Val::Str(format_arr(&str, &[o])?.into()),32 })33 },34 )35}3637type Builtin = fn(context: Context, loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val>;3839type BuiltinsType = HashMap<Box<str>, Builtin>;4041thread_local! {42 static BUILTINS: BuiltinsType = {43 [44 ("length".into(), builtin_length as Builtin),45 ("type".into(), builtin_type),46 ("makeArray".into(), builtin_make_array),47 ("codepoint".into(), builtin_codepoint),48 ("objectFieldsEx".into(), builtin_object_fields_ex),49 ("objectHasEx".into(), builtin_object_has_ex),50 ("slice".into(), builtin_slice),51 ("primitiveEquals".into(), builtin_primitive_equals),52 ("equals".into(), builtin_equals),53 ("modulo".into(), builtin_modulo),54 ("mod".into(), builtin_mod),55 ("floor".into(), builtin_floor),56 ("log".into(), builtin_log),57 ("pow".into(), builtin_pow),58 ("extVar".into(), builtin_ext_var),59 ("native".into(), builtin_native),60 ("filter".into(), builtin_filter),61 ("map".into(), builtin_map),62 ("foldl".into(), builtin_foldl),63 ("foldr".into(), builtin_foldr),64 ("sortImpl".into(), builtin_sort_impl),65 ("format".into(), builtin_format),66 ("range".into(), builtin_range),67 ("char".into(), builtin_char),68 ("encodeUTF8".into(), builtin_encode_utf8),69 ("md5".into(), builtin_md5),70 ("base64".into(), builtin_base64),71 ("trace".into(), builtin_trace),72 ("gc".into(), builtin_gc),73 ("gcTrace".into(), builtin_gc_trace),74 ("join".into(), builtin_join),75 ("escapeStringJson".into(), builtin_escape_string_json),76 ("manifestJsonEx".into(), builtin_manifest_json_ex),77 ("reverse".into(), builtin_reverse),78 ("id".into(), builtin_id),79 ("strReplace".into(), builtin_str_replace),80 ("parseJson".into(), builtin_parse_json),81 ].iter().cloned().collect()82 };83}8485fn builtin_length(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {86 parse_args!(context, "length", args, 1, [87 0, x: ty!((string | object | array));88 ], {89 Ok(match x {90 Val::Str(n) => Val::Num(n.chars().count() as f64),91 Val::Arr(a) => Val::Num(a.len() as f64),92 Val::Obj(o) => Val::Num(93 o.fields_visibility()94 .into_iter()95 .filter(|(_k, v)| *v)96 .count() as f64,97 ),98 _ => unreachable!(),99 })100 })101}102103fn builtin_type(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {104 parse_args!(context, "type", args, 1, [105 0, x: ty!(any);106 ], {107 Ok(Val::Str(x.value_type().name().into()))108 })109}110111fn builtin_make_array(112 context: Context,113 _loc: Option<&ExprLocation>,114 args: &ArgsDesc,115) -> Result<Val> {116 parse_args!(context, "makeArray", args, 2, [117 0, sz: ty!(BoundedNumber<(Some(0.0)), (None)>) => Val::Num;118 1, func: ty!(function) => Val::Func;119 ], {120 let mut out = Vec::with_capacity(sz as usize);121 for i in 0..sz as usize {122 out.push(LazyVal::new_resolved(func.evaluate_values(123 context.clone(),124 &[Val::Num(i as f64)]125 )?))126 }127 Ok(Val::Arr(out.into()))128 })129}130131fn builtin_codepoint(132 context: Context,133 _loc: Option<&ExprLocation>,134 args: &ArgsDesc,135) -> Result<Val> {136 parse_args!(context, "codepoint", args, 1, [137 0, str: ty!(char) => Val::Str;138 ], {139 Ok(Val::Num(str.chars().next().unwrap() as u32 as f64))140 })141}142143fn builtin_object_fields_ex(144 context: Context,145 _loc: Option<&ExprLocation>,146 args: &ArgsDesc,147) -> Result<Val> {148 parse_args!(context, "objectFieldsEx", args, 2, [149 0, obj: ty!(object) => Val::Obj;150 1, inc_hidden: ty!(boolean) => Val::Bool;151 ], {152 let out = obj.fields_ex(inc_hidden);153 Ok(Val::Arr(out.into_iter().map(Val::Str).collect::<Vec<_>>().into()))154 })155}156157fn builtin_object_has_ex(158 context: Context,159 _loc: Option<&ExprLocation>,160 args: &ArgsDesc,161) -> Result<Val> {162 parse_args!(context, "objectHasEx", args, 3, [163 0, obj: ty!(object) => Val::Obj;164 1, f: ty!(string) => Val::Str;165 2, inc_hidden: ty!(boolean) => Val::Bool;166 ], {167 Ok(Val::Bool(obj.has_field_ex(f, inc_hidden)))168 })169}170171fn builtin_parse_json(172 context: Context,173 _loc: Option<&ExprLocation>,174 args: &ArgsDesc,175) -> Result<Val> {176 parse_args!(context, "parseJson", args, 1, [177 0, s: ty!(string) => Val::Str;178 ], {179 let state = EvaluationState::default();180 let path = PathBuf::from("std.parseJson").into();181 state.evaluate_snippet_raw(path ,s)182 })183}184185// faster186fn builtin_slice(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {187 parse_args!(context, "slice", args, 4, [188 0, indexable: ty!((string | array));189 1, index: ty!((number | null));190 2, end: ty!((number | null));191 3, step: ty!((number | null));192 ], {193 let index = match index {194 Val::Num(v) => v as usize,195 Val::Null => 0,196 _ => unreachable!(),197 };198 let end = match end {199 Val::Num(v) => v as usize,200 Val::Null => match &indexable {201 Val::Str(s) => s.chars().count(),202 Val::Arr(v) => v.len(),203 _ => unreachable!()204 },205 _ => unreachable!()206 };207 let step = match step {208 Val::Num(v) => v as usize,209 Val::Null => 1,210 _ => unreachable!()211 };212 match &indexable {213 Val::Str(s) => {214 Ok(Val::Str((s.chars().skip(index).take(end-index).step_by(step).collect::<String>()).into()))215 }216 Val::Arr(arr) => {217 Ok(Val::Arr((arr.iter().skip(index).take(end-index).step_by(step).collect::<Result<Vec<Val>>>()?).into()))218 }219 _ => unreachable!()220 }221 })222}223224// faster225fn builtin_primitive_equals(226 context: Context,227 _loc: Option<&ExprLocation>,228 args: &ArgsDesc,229) -> Result<Val> {230 parse_args!(context, "primitiveEquals", args, 2, [231 0, a: ty!(any);232 1, b: ty!(any);233 ], {234 Ok(Val::Bool(primitive_equals(&a, &b)?))235 })236}237238// faster239fn builtin_equals(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {240 parse_args!(context, "equals", args, 2, [241 0, a: ty!(any);242 1, b: ty!(any);243 ], {244 Ok(Val::Bool(equals(&a, &b)?))245 })246}247248fn builtin_modulo(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {249 parse_args!(context, "modulo", args, 2, [250 0, a: ty!(number) => Val::Num;251 1, b: ty!(number) => Val::Num;252 ], {253 Ok(Val::Num(a % b))254 })255}256257fn builtin_mod(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {258 parse_args!(context, "mod", args, 2, [259 0, a: ty!((number | string));260 1, b: ty!(any);261 ], {262 match (a, b) {263 (Val::Num(a), Val::Num(b)) => Ok(Val::Num(a % b)),264 (Val::Str(str), vals) => std_format(str, vals),265 (a, b) => throw!(BinaryOperatorDoesNotOperateOnValues(BinaryOpType::Mod, a.value_type(), b.value_type()))266 }267 })268}269270fn builtin_floor(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {271 parse_args!(context, "floor", args, 1, [272 0, x: ty!(number) => Val::Num;273 ], {274 Ok(Val::Num(x.floor()))275 })276}277278fn builtin_log(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {279 parse_args!(context, "log", args, 1, [280 0, n: ty!(number) => Val::Num;281 ], {282 Ok(Val::Num(n.ln()))283 })284}285286fn builtin_pow(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {287 parse_args!(context, "pow", args, 2, [288 0, x: ty!(number) => Val::Num;289 1, n: ty!(number) => Val::Num;290 ], {291 Ok(Val::Num(x.powf(n)))292 })293}294295fn builtin_ext_var(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {296 parse_args!(context, "extVar", args, 1, [297 0, x: ty!(string) => Val::Str;298 ], {299 Ok(with_state(|s| s.settings().ext_vars.get(&x).cloned()).ok_or(UndefinedExternalVariable(x))?)300 })301}302303fn builtin_native(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {304 parse_args!(context, "native", args, 1, [305 0, x: ty!(string) => Val::Str;306 ], {307 Ok(with_state(|s| s.settings().ext_natives.get(&x).cloned()).map(|v| Val::Func(Gc::new(FuncVal::NativeExt(x.clone(), v)))).ok_or(UndefinedExternalFunction(x))?)308 })309}310311fn builtin_filter(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {312 parse_args!(context, "filter", args, 2, [313 0, func: ty!(function) => Val::Func;314 1, arr: ty!(array) => Val::Arr;315 ], {316 Ok(Val::Arr(arr.filter(|val| func317 .evaluate_values(context.clone(), &[val.clone()])?318 .try_cast_bool("filter predicate"))?))319 })320}321322fn builtin_map(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {323 parse_args!(context, "map", args, 2, [324 0, func: ty!(function) => Val::Func;325 1, arr: ty!(array) => Val::Arr;326 ], {327 Ok(Val::Arr(arr.map(|val| func328 .evaluate_values(context.clone(), &[val]))?))329 })330}331332fn builtin_foldl(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {333 parse_args!(context, "foldl", args, 3, [334 0, func: ty!(function) => Val::Func;335 1, arr: ty!(array) => Val::Arr;336 2, init: ty!(any);337 ], {338 let mut acc = init;339 for i in arr.iter() {340 acc = func.evaluate_values(context.clone(), &[acc, i?])?;341 }342 Ok(acc)343 })344}345346fn builtin_foldr(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {347 parse_args!(context, "foldr", args, 3, [348 0, func: ty!(function) => Val::Func;349 1, arr: ty!(array) => Val::Arr;350 2, init: ty!(any);351 ], {352 let mut acc = init;353 for i in arr.iter().rev() {354 acc = func.evaluate_values(context.clone(), &[acc, i?])?;355 }356 Ok(acc)357 })358}359360#[allow(non_snake_case)]361fn builtin_sort_impl(362 context: Context,363 _loc: Option<&ExprLocation>,364 args: &ArgsDesc,365) -> Result<Val> {366 parse_args!(context, "sort", args, 2, [367 0, arr: ty!(array) => Val::Arr;368 1, keyF: ty!(function) => Val::Func;369 ], {370 if arr.len() <= 1 {371 return Ok(Val::Arr(arr))372 }373 Ok(Val::Arr(ArrValue::Eager(sort::sort(context, arr.evaluated()?, &keyF)?)))374 })375}376377// faster378fn builtin_format(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {379 parse_args!(context, "format", args, 2, [380 0, str: ty!(string) => Val::Str;381 1, vals: ty!(any)382 ], {383 std_format(str, vals)384 })385}386387fn builtin_range(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {388 parse_args!(context, "range", args, 2, [389 0, from: ty!(number) => Val::Num;390 1, to: ty!(number) => Val::Num;391 ], {392 if to < from {393 return Ok(Val::Arr(ArrValue::new_eager()))394 }395 let mut out = Vec::with_capacity((1+to as usize-from as usize).max(0));396 for i in from as usize..=to as usize {397 out.push(Val::Num(i as f64));398 }399 Ok(Val::Arr(out.into()))400 })401}402403fn builtin_char(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {404 parse_args!(context, "char", args, 1, [405 0, n: ty!(number) => Val::Num;406 ], {407 let mut out = String::new();408 out.push(std::char::from_u32(n as u32).ok_or_else(||409 InvalidUnicodeCodepointGot(n as u32)410 )?);411 Ok(Val::Str(out.into()))412 })413}414415fn builtin_encode_utf8(416 context: Context,417 _loc: Option<&ExprLocation>,418 args: &ArgsDesc,419) -> Result<Val> {420 parse_args!(context, "encodeUTF8", args, 1, [421 0, str: ty!(string) => Val::Str;422 ], {423 Ok(Val::Arr((str.bytes().map(|b| Val::Num(b as f64)).collect::<Vec<Val>>()).into()))424 })425}426427fn builtin_md5(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {428 parse_args!(context, "md5", args, 1, [429 0, str: ty!(string) => Val::Str;430 ], {431 Ok(Val::Str(format!("{:x}", md5::compute(&str.as_bytes())).into()))432 })433}434435fn builtin_trace(context: Context, loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {436 parse_args!(context, "trace", args, 2, [437 0, str: ty!(string) => Val::Str;438 1, rest: ty!(any);439 ], {440 eprint!("TRACE:");441 if let Some(loc) = loc {442 with_state(|s|{443 let locs = s.map_source_locations(&loc.0, &[loc.1]);444 eprint!(" {}:{}", loc.0.file_name().unwrap().to_str().unwrap(), locs[0].line);445 });446 }447 eprintln!(" {}", str);448 Ok(rest)449 })450}451452fn builtin_gc(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {453 parse_args!(context, "gc", args, 1, [454 0, rest: ty!(any);455 ], {456 println!("GC start");457 jrsonnet_gc::force_collect();458 println!("GC done");459460 Ok(rest)461 })462}463464fn builtin_gc_trace(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {465 parse_args!(context, "gcTrace", args, 2, [466 0, name: ty!(string) => Val::Str;467 1, rest: ty!(any);468 ], {469 Ok(DebugGcTraceValue::create(name, rest))470 })471}472473fn builtin_base64(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {474 parse_args!(context, "base64", args, 1, [475 0, input: ty!((string | (Array<number>)));476 ], {477 Ok(Val::Str(match input {478 Val::Str(s) => {479 base64::encode(s.bytes().collect::<Vec<_>>()).into()480 },481 Val::Arr(a) => {482 base64::encode(a.iter().map(|v| {483 Ok(v?.unwrap_num()? as u8)484 }).collect::<Result<Vec<_>>>()?).into()485 },486 _ => unreachable!()487 }))488 })489}490491fn builtin_join(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {492 parse_args!(context, "join", args, 2, [493 0, sep: ty!((string | array));494 1, arr: ty!(array) => Val::Arr;495 ], {496 Ok(match sep {497 Val::Arr(joiner_items) => {498 let mut out = Vec::new();499500 let mut first = true;501 for item in arr.iter() {502 let item = item?.clone();503 if let Val::Arr(items) = item {504 if !first {505 out.reserve(joiner_items.len());506 // TODO: extend507 for item in joiner_items.iter() {508 out.push(item?);509 }510 }511 first = false;512 out.reserve(items.len());513 // TODO: extend514 for item in items.iter() {515 out.push(item?);516 }517 } else {518 throw!(RuntimeError("in std.join all items should be arrays".into()));519 }520 }521522 Val::Arr(out.into())523 },524 Val::Str(sep) => {525 let mut out = String::new();526527 let mut first = true;528 for item in arr.iter() {529 let item = item?.clone();530 if let Val::Str(item) = item {531 if !first {532 out += &sep;533 }534 first = false;535 out += &item;536 } else {537 throw!(RuntimeError("in std.join all items should be strings".into()));538 }539 }540541 Val::Str(out.into())542 },543 _ => unreachable!()544 })545 })546}547548// faster549fn builtin_escape_string_json(550 context: Context,551 _loc: Option<&ExprLocation>,552 args: &ArgsDesc,553) -> Result<Val> {554 parse_args!(context, "escapeStringJson", args, 1, [555 0, str_: ty!(string) => Val::Str;556 ], {557 Ok(Val::Str(escape_string_json(&str_).into()))558 })559}560561// faster562fn builtin_manifest_json_ex(563 context: Context,564 _loc: Option<&ExprLocation>,565 args: &ArgsDesc,566) -> Result<Val> {567 parse_args!(context, "manifestJsonEx", args, 2, [568 0, value: ty!(any);569 1, indent: ty!(string) => Val::Str;570 ], {571 Ok(Val::Str(manifest_json_ex(&value, &ManifestJsonOptions {572 padding: &indent,573 mtype: ManifestType::Std,574 })?.into()))575 })576}577578// faster579fn builtin_reverse(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {580 parse_args!(context, "reverse", args, 1, [581 0, value: ty!(array) => Val::Arr;582 ], {583 Ok(Val::Arr(value.reversed()))584 })585}586587fn builtin_id(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {588 parse_args!(context, "id", args, 1, [589 0, v: ty!(any);590 ], {591 Ok(v)592 })593}594595// faster596fn builtin_str_replace(597 context: Context,598 _loc: Option<&ExprLocation>,599 args: &ArgsDesc,600) -> Result<Val> {601 parse_args!(context, "strReplace", args, 3, [602 0, str: ty!(string) => Val::Str;603 1, from: ty!(string) => Val::Str;604 2, to: ty!(string) => Val::Str;605 ], {606 let mut out = String::new();607 let mut last_idx = 0;608 while let Some(idx) = (&str[last_idx..]).find(&from as &str) {609 out.push_str(&str[last_idx..last_idx+idx]);610 out.push_str(&to);611 last_idx += idx + from.len();612 }613 if last_idx == 0 {614 return Ok(Val::Str(str))615 }616 out.push_str(&str[last_idx..]);617 Ok(Val::Str(out.into()))618 })619}620621pub fn call_builtin(622 context: Context,623 loc: Option<&ExprLocation>,624 name: &str,625 args: &ArgsDesc,626) -> Result<Val> {627 BUILTINS628 .with(|builtins| builtins.get(name).copied())629 .ok_or_else(|| IntrinsicNotFound(name.into()))?(context, loc, args)630}crates/jrsonnet-evaluator/src/builtin/sort.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/sort.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/sort.rs
@@ -2,7 +2,7 @@
error::{Error, LocError, Result},
throw, Context, FuncVal, Val,
};
-use gc::{Finalize, Gc, Trace};
+use jrsonnet_gc::{Finalize, Gc, Trace};
#[derive(Debug, Clone, thiserror::Error, Trace, Finalize)]
pub enum SortError {
crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -2,13 +2,14 @@
error::Error::*, map::LayeredHashMap, FutureWrapper, LazyBinding, LazyVal, ObjValue, Result,
Val,
};
-use gc::{Finalize, Gc, Trace};
+use jrsonnet_gc::{Gc, Trace};
use jrsonnet_interner::IStr;
use rustc_hash::FxHashMap;
use std::fmt::Debug;
use std::hash::BuildHasherDefault;
-#[derive(Clone, Trace, Finalize)]
+#[derive(Clone, Trace)]
+#[trivially_drop]
pub struct ContextCreator(pub Context, pub FutureWrapper<FxHashMap<IStr, LazyBinding>>);
impl ContextCreator {
pub fn create(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<Context> {
@@ -21,12 +22,13 @@
}
}
-#[derive(Trace, Finalize)]
+#[derive(Trace)]
+#[trivially_drop]
struct ContextInternals {
dollar: Option<ObjValue>,
this: Option<ObjValue>,
super_obj: Option<ObjValue>,
- bindings: LayeredHashMap<LazyVal>,
+ bindings: LayeredHashMap,
}
impl Debug for ContextInternals {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -34,7 +36,8 @@
}
}
-#[derive(Debug, Clone, Trace, Finalize)]
+#[derive(Debug, Clone, Trace)]
+#[trivially_drop]
pub struct Context(Gc<ContextInternals>);
impl Context {
pub fn new_future() -> FutureWrapper<Self> {
crates/jrsonnet-evaluator/src/dynamic.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/dynamic.rs
+++ b/crates/jrsonnet-evaluator/src/dynamic.rs
@@ -1,6 +1,7 @@
-use gc::{Finalize, Gc, GcCell, Trace};
+use jrsonnet_gc::{Gc, GcCell, Trace};
-#[derive(Clone, Trace, Finalize)]
+#[derive(Clone, Trace)]
+#[trivially_drop]
pub struct FutureWrapper<V: Trace + 'static>(pub Gc<GcCell<Option<V>>>);
impl<T: Trace + 'static> FutureWrapper<T> {
pub fn new() -> Self {
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -2,7 +2,7 @@
builtin::{format::FormatError, sort::SortError},
typed::TypeLocError,
};
-use gc::{Finalize, Trace};
+use jrsonnet_gc::Trace;
use jrsonnet_interner::IStr;
use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};
use jrsonnet_types::ValType;
@@ -12,7 +12,8 @@
};
use thiserror::Error;
-#[derive(Error, Debug, Clone, Trace, Finalize)]
+#[derive(Error, Debug, Clone, Trace)]
+#[trivially_drop]
pub enum Error {
#[error("intrinsic not found: {0}")]
IntrinsicNotFound(IStr),
@@ -149,15 +150,18 @@
}
}
-#[derive(Clone, Debug, Trace, Finalize)]
+#[derive(Clone, Debug, Trace)]
+#[trivially_drop]
pub struct StackTraceElement {
pub location: Option<ExprLocation>,
pub desc: String,
}
-#[derive(Debug, Clone, Trace, Finalize)]
+#[derive(Debug, Clone, Trace)]
+#[trivially_drop]
pub struct StackTrace(pub Vec<StackTraceElement>);
-#[derive(Debug, Clone, Trace, Finalize)]
+#[derive(Debug, Clone, Trace)]
+#[trivially_drop]
pub struct LocError(Box<(Error, StackTrace)>);
impl LocError {
pub fn new(e: Error) -> Self {
crates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate.rs
@@ -3,7 +3,7 @@
FuncDesc, FuncVal, FutureWrapper, LazyBinding, LazyVal, LazyValValue, ObjMember, ObjValue,
ObjectAssertion, Result, Val,
};
-use gc::{custom_trace, Finalize, Gc, Trace};
+use jrsonnet_gc::{Gc, Trace};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{
ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, ExprLocation, FieldMember,
@@ -22,21 +22,14 @@
if let Some(params) = &b.params {
let params = params.clone();
+ #[derive(Trace)]
+ #[trivially_drop]
struct LazyMethodBinding {
context_creator: FutureWrapper<Context>,
name: IStr,
params: ParamsDesc,
value: LocExpr,
}
- impl Finalize for LazyMethodBinding {}
- unsafe impl Trace for LazyMethodBinding {
- custom_trace!(this, {
- mark(&this.context_creator);
- mark(&this.name);
- mark(&this.params);
- mark(&this.value);
- });
- }
impl LazyValValue for LazyMethodBinding {
fn get(self: Box<Self>) -> Result<Val> {
Ok(evaluate_method(
@@ -55,19 +48,13 @@
value: b.value.clone(),
}))
} else {
+ #[derive(Trace)]
+ #[trivially_drop]
struct LazyNamedBinding {
context_creator: FutureWrapper<Context>,
name: IStr,
value: LocExpr,
}
- impl Finalize for LazyNamedBinding {}
- unsafe impl Trace for LazyNamedBinding {
- custom_trace!(this, {
- mark(&this.context_creator);
- mark(&this.name);
- mark(&this.value);
- });
- }
impl LazyValValue for LazyNamedBinding {
fn get(self: Box<Self>) -> Result<Val> {
evaluate_named(self.context_creator.unwrap(), &self.value, self.name)
@@ -86,6 +73,8 @@
if let Some(params) = &b.params {
let params = params.clone();
+ #[derive(Trace)]
+ #[trivially_drop]
struct BindableMethodLazyVal {
this: Option<ObjValue>,
super_obj: Option<ObjValue>,
@@ -94,17 +83,6 @@
name: IStr,
params: ParamsDesc,
value: LocExpr,
- }
- impl Finalize for BindableMethodLazyVal {}
- unsafe impl Trace for BindableMethodLazyVal {
- custom_trace!(this, {
- mark(&this.this);
- mark(&this.super_obj);
- mark(&this.context_creator);
- mark(&this.name);
- mark(&this.params);
- mark(&this.value);
- });
}
impl LazyValValue for BindableMethodLazyVal {
fn get(self: Box<Self>) -> Result<Val> {
@@ -117,7 +95,8 @@
}
}
- #[derive(Trace, Finalize)]
+ #[derive(Trace)]
+ #[trivially_drop]
struct BindableMethod {
context_creator: ContextCreator,
name: IStr,
@@ -148,6 +127,8 @@
}))),
)
} else {
+ #[derive(Trace)]
+ #[trivially_drop]
struct BindableNamedLazyVal {
this: Option<ObjValue>,
super_obj: Option<ObjValue>,
@@ -156,16 +137,6 @@
name: IStr,
value: LocExpr,
}
- impl Finalize for BindableNamedLazyVal {}
- unsafe impl Trace for BindableNamedLazyVal {
- custom_trace!(this, {
- mark(&this.this);
- mark(&this.super_obj);
- mark(&this.context_creator);
- mark(&this.name);
- mark(&this.value);
- });
- }
impl LazyValValue for BindableNamedLazyVal {
fn get(self: Box<Self>) -> Result<Val> {
evaluate_named(
@@ -176,7 +147,8 @@
}
}
- #[derive(Trace, Finalize)]
+ #[derive(Trace)]
+ #[trivially_drop]
struct BindableNamed {
context_creator: ContextCreator,
name: IStr,
@@ -414,7 +386,8 @@
}
let name = name.unwrap();
- #[derive(Trace, Finalize)]
+ #[derive(Trace)]
+ #[trivially_drop]
struct ObjMemberBinding {
context_creator: ContextCreator,
value: LocExpr,
@@ -458,7 +431,8 @@
continue;
}
let name = name.unwrap();
- #[derive(Trace, Finalize)]
+ #[derive(Trace)]
+ #[trivially_drop]
struct ObjMemberBinding {
context_creator: ContextCreator,
value: LocExpr,
@@ -496,16 +470,11 @@
}
Member::BindStmt(_) => {}
Member::AssertStmt(stmt) => {
+ #[derive(Trace)]
+ #[trivially_drop]
struct ObjectAssert {
context_creator: ContextCreator,
assert: AssertStmt,
- }
- impl Finalize for ObjectAssert {}
- unsafe impl Trace for ObjectAssert {
- custom_trace!(this, {
- mark(&this.context_creator);
- mark(&this.assert);
- });
}
impl ObjectAssertion for ObjectAssert {
fn run(
@@ -558,7 +527,8 @@
match key {
Val::Null => {}
Val::Str(n) => {
- #[derive(Trace, Finalize)]
+ #[derive(Trace)]
+ #[trivially_drop]
struct ObjCompBinding {
context: Context,
value: LocExpr,
@@ -768,16 +738,11 @@
let mut out = Vec::with_capacity(items.len());
for item in items {
// TODO: Implement ArrValue::Lazy with same context for every element?
+ #[derive(Trace)]
+ #[trivially_drop]
struct ArrayElement {
context: Context,
item: LocExpr,
- }
- impl Finalize for ArrayElement {}
- unsafe impl Trace for ArrayElement {
- custom_trace!(this, {
- mark(&this.context);
- mark(&this.item);
- });
}
impl LazyValValue for ArrayElement {
fn get(self: Box<Self>) -> Result<Val> {
crates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function.rs
+++ b/crates/jrsonnet-evaluator/src/function.rs
@@ -1,5 +1,5 @@
use crate::{error::Error::*, evaluate, throw, Context, LazyVal, LazyValValue, Result, Val};
-use gc::{custom_trace, Finalize, Trace};
+use jrsonnet_gc::Trace;
use jrsonnet_interner::IStr;
use jrsonnet_parser::{ArgsDesc, LocExpr, ParamsDesc};
use rustc_hash::FxHashMap;
@@ -55,16 +55,11 @@
let val = if tailstrict {
LazyVal::new_resolved(evaluate(ctx, expr)?)
} else {
+ #[derive(Trace)]
+ #[trivially_drop]
struct EvaluateLazyVal {
context: Context,
expr: LocExpr,
- }
- impl Finalize for EvaluateLazyVal {}
- unsafe impl Trace for EvaluateLazyVal {
- custom_trace!(this, {
- mark(&this.context);
- mark(&this.expr);
- });
}
impl LazyValValue for EvaluateLazyVal {
fn get(self: Box<Self>) -> Result<Val> {
@@ -119,7 +114,8 @@
} else {
let body_ctx = body_ctx.clone();
let default = default.clone();
- #[derive(Trace, Finalize)]
+ #[derive(Trace)]
+ #[trivially_drop]
struct EvaluateLazyVal {
body_ctx: Option<Context>,
default: LocExpr,
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -2,7 +2,7 @@
error::{Error::*, LocError, Result},
throw, LazyBinding, LazyVal, ObjMember, ObjValue, Val,
};
-use gc::Gc;
+use jrsonnet_gc::Gc;
use jrsonnet_parser::Visibility;
use rustc_hash::FxHasher;
use serde_json::{Map, Number, Value};
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -25,8 +25,8 @@
use error::{Error::*, LocError, Result, StackTraceElement};
pub use evaluate::*;
pub use function::parse_function_call;
-use gc::{Finalize, Gc, Trace};
pub use import::*;
+use jrsonnet_gc::{Finalize, Gc, Trace};
pub use jrsonnet_interner::IStr;
use jrsonnet_parser::*;
use native::NativeCallback;
crates/jrsonnet-evaluator/src/map.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/map.rs
+++ b/crates/jrsonnet-evaluator/src/map.rs
@@ -1,32 +1,29 @@
-use gc::{Finalize, Gc, Trace};
+use jrsonnet_gc::{Gc, Trace};
use jrsonnet_interner::IStr;
use rustc_hash::FxHashMap;
-pub struct LayeredHashMapInternals<V: Trace + Finalize + 'static> {
- parent: Option<LayeredHashMap<V>>,
- current: FxHashMap<IStr, V>,
-}
+use crate::LazyVal;
-unsafe impl<V: Trace + Finalize + 'static> Trace for LayeredHashMapInternals<V> {
- gc::custom_trace!(this, {
- mark(&this.parent);
- mark(&this.current);
- });
+#[derive(Trace)]
+#[trivially_drop]
+pub struct LayeredHashMapInternals {
+ parent: Option<LayeredHashMap>,
+ current: FxHashMap<IStr, LazyVal>,
}
-impl<V: Trace + Finalize + 'static> Finalize for LayeredHashMapInternals<V> {}
-#[derive(Trace, Finalize)]
-pub struct LayeredHashMap<V: Trace + Finalize + 'static>(Gc<LayeredHashMapInternals<V>>);
+#[derive(Trace)]
+#[trivially_drop]
+pub struct LayeredHashMap(Gc<LayeredHashMapInternals>);
-impl<V: Trace + 'static> LayeredHashMap<V> {
- pub fn extend(self, new_layer: FxHashMap<IStr, V>) -> Self {
+impl LayeredHashMap {
+ pub fn extend(self, new_layer: FxHashMap<IStr, LazyVal>) -> Self {
Self(Gc::new(LayeredHashMapInternals {
parent: Some(self),
current: new_layer,
}))
}
- pub fn get(&self, key: &IStr) -> Option<&V> {
+ pub fn get(&self, key: &IStr) -> Option<&LazyVal> {
(self.0)
.current
.get(key)
@@ -34,13 +31,13 @@
}
}
-impl<V: Trace + 'static> Clone for LayeredHashMap<V> {
+impl Clone for LayeredHashMap {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
-impl<V: Trace + 'static> Default for LayeredHashMap<V> {
+impl Default for LayeredHashMap {
fn default() -> Self {
Self(Gc::new(LayeredHashMapInternals {
parent: None,
crates/jrsonnet-evaluator/src/native.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/native.rs
+++ b/crates/jrsonnet-evaluator/src/native.rs
@@ -1,7 +1,7 @@
#![allow(clippy::type_complexity)]
use crate::{error::Result, Val};
-use gc::{Finalize, Trace};
+use jrsonnet_gc::Trace;
use jrsonnet_parser::ParamsDesc;
use std::fmt::Debug;
use std::path::Path;
@@ -11,7 +11,8 @@
fn call(&self, from: Option<Rc<Path>>, args: &[Val]) -> Result<Val>;
}
-#[derive(Trace, Finalize)]
+#[derive(Trace)]
+#[trivially_drop]
pub struct NativeCallback {
pub params: ParamsDesc,
handler: Box<dyn NativeCallbackHandler>,
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -1,12 +1,13 @@
use crate::{evaluate_add_op, LazyBinding, Result, Val};
-use gc::{Finalize, Gc, GcCell, Trace};
+use jrsonnet_gc::{Gc, GcCell, Trace};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{ExprLocation, Visibility};
use rustc_hash::{FxHashMap, FxHashSet};
use std::hash::{Hash, Hasher};
use std::{fmt::Debug, hash::BuildHasherDefault};
-#[derive(Debug, Trace, Finalize)]
+#[derive(Debug, Trace)]
+#[trivially_drop]
pub struct ObjMember {
pub add: bool,
pub visibility: Visibility,
@@ -20,7 +21,8 @@
// Field => This
type CacheKey = (IStr, ObjValue);
-#[derive(Trace, Finalize)]
+#[derive(Trace)]
+#[trivially_drop]
pub struct ObjValueInternals {
super_obj: Option<ObjValue>,
assertions: Gc<Vec<Box<dyn ObjectAssertion>>>,
@@ -30,7 +32,8 @@
value_cache: GcCell<FxHashMap<CacheKey, Option<Val>>>,
}
-#[derive(Clone, Trace, Finalize)]
+#[derive(Clone, Trace)]
+#[trivially_drop]
pub struct ObjValue(pub(crate) Gc<ObjValueInternals>);
impl Debug for ObjValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
crates/jrsonnet-evaluator/src/typed.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed.rs
+++ b/crates/jrsonnet-evaluator/src/typed.rs
@@ -4,7 +4,7 @@
error::{Error, LocError, Result},
push, Val,
};
-use gc::{Finalize, Trace};
+use jrsonnet_gc::Trace;
use jrsonnet_parser::ExprLocation;
use jrsonnet_types::{ComplexValType, ValType};
use thiserror::Error;
@@ -21,7 +21,8 @@
}};
}
-#[derive(Debug, Error, Clone, Trace, Finalize)]
+#[derive(Debug, Error, Clone, Trace)]
+#[trivially_drop]
pub enum TypeError {
#[error("expected {0}, got {1}")]
ExpectedGot(ComplexValType, ValType),
@@ -38,7 +39,8 @@
}
}
-#[derive(Debug, Clone, Trace, Finalize)]
+#[derive(Debug, Clone, Trace)]
+#[trivially_drop]
pub struct TypeLocError(Box<TypeError>, ValuePathStack);
impl From<TypeError> for TypeLocError {
fn from(e: TypeError) -> Self {
@@ -60,7 +62,8 @@
}
}
-#[derive(Debug, Clone, Trace, Finalize)]
+#[derive(Debug, Clone, Trace)]
+#[trivially_drop]
pub struct TypeLocErrorList(Vec<TypeLocError>);
impl Display for TypeLocErrorList {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -123,7 +126,8 @@
}
}
-#[derive(Clone, Debug, Trace, Finalize)]
+#[derive(Clone, Debug, Trace)]
+#[trivially_drop]
enum ValuePathItem {
Field(Rc<str>),
Index(u64),
@@ -138,7 +142,8 @@
}
}
-#[derive(Clone, Debug, Trace, Finalize)]
+#[derive(Clone, Debug, Trace)]
+#[trivially_drop]
struct ValuePathStack(Vec<ValuePathItem>);
impl Display for ValuePathStack {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
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 gc::{custom_trace, Finalize, Gc, GcCell, Trace};
+use jrsonnet_gc::{Finalize, Gc, GcCell, Trace};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{el, Arg, ArgsDesc, Expr, ExprLocation, LiteralType, LocExpr, ParamsDesc};
use jrsonnet_types::ValType;
@@ -19,25 +19,17 @@
fn get(self: Box<Self>) -> Result<Val>;
}
+#[derive(Trace)]
+#[trivially_drop]
enum LazyValInternals {
Computed(Val),
Errored(LocError),
Waiting(Box<dyn LazyValValue>),
Pending,
-}
-impl Finalize for LazyValInternals {}
-unsafe impl Trace for LazyValInternals {
- custom_trace!(this, {
- match &this {
- LazyValInternals::Computed(v) => mark(v),
- LazyValInternals::Errored(e) => mark(e),
- LazyValInternals::Waiting(w) => mark(w),
- LazyValInternals::Pending => {}
- }
- });
}
-#[derive(Clone, Trace, Finalize)]
+#[derive(Clone, Trace)]
+#[trivially_drop]
pub struct LazyVal(Gc<GcCell<LazyValInternals>>);
impl LazyVal {
pub fn new(f: Box<dyn LazyValValue>) -> Self {
@@ -83,7 +75,8 @@
}
}
-#[derive(Debug, PartialEq, Trace, Finalize)]
+#[derive(Debug, PartialEq, Trace)]
+#[trivially_drop]
pub struct FuncDesc {
pub name: IStr,
pub ctx: Context,
@@ -91,7 +84,8 @@
pub body: LocExpr,
}
-#[derive(Debug, Trace, Finalize)]
+#[derive(Debug, Trace)]
+#[trivially_drop]
pub enum FuncVal {
/// Plain function implemented in jsonnet
Normal(FuncDesc),
@@ -195,22 +189,13 @@
String,
}
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Trace)]
+#[trivially_drop]
pub enum ArrValue {
Lazy(Gc<Vec<LazyVal>>),
Eager(Gc<Vec<Val>>),
Extended(Box<(Self, Self)>),
}
-impl Finalize for ArrValue {}
-unsafe impl Trace for ArrValue {
- custom_trace!(this, {
- match &this {
- ArrValue::Lazy(l) => mark(l),
- ArrValue::Eager(e) => mark(e),
- ArrValue::Extended(x) => mark(x),
- }
- });
-}
impl ArrValue {
pub fn new_eager() -> Self {
Self::Eager(Gc::new(Vec::new()))
@@ -419,7 +404,8 @@
}
}
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Trace)]
+#[trivially_drop]
pub enum Val {
Bool(bool),
Null,
@@ -429,21 +415,6 @@
Obj(ObjValue),
Func(Gc<FuncVal>),
DebugGcTraceValue(DebugGcTraceValue),
-}
-impl Finalize for Val {}
-unsafe impl Trace for Val {
- custom_trace!(this, {
- match &this {
- Val::Bool(_) => {}
- Val::Null => {}
- Val::Str(_) => {}
- Val::Num(_) => {}
- Val::Arr(a) => mark(a),
- Val::Obj(o) => mark(o),
- Val::Func(f) => mark(f),
- Val::DebugGcTraceValue(v) => mark(v),
- }
- });
}
macro_rules! matches_unwrap {
crates/jrsonnet-interner/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-interner/Cargo.toml
+++ b/crates/jrsonnet-interner/Cargo.toml
@@ -9,4 +9,4 @@
[dependencies]
serde = { version = "1.0" }
rustc-hash = "1.1.0"
-gc = { version = "0.4.1", features = ["derive"] }
+jrsonnet-gc = { version = "0.4.2", features = ["derive"] }
crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -1,4 +1,4 @@
-use gc::{unsafe_empty_trace, Finalize, Trace};
+use jrsonnet_gc::{unsafe_empty_trace, Finalize, Trace};
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use std::{
crates/jrsonnet-parser/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-parser/Cargo.toml
+++ b/crates/jrsonnet-parser/Cargo.toml
@@ -18,7 +18,7 @@
unescape = "0.1.0"
serde = { version = "1.0", features = ["derive", "rc"], optional = true }
-gc = { version = "0.4.1", features = ["derive"] }
+jrsonnet-gc = { version = "0.4.2", features = ["derive"] }
[dev-dependencies]
jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.3.8" }
crates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -1,4 +1,4 @@
-use gc::{unsafe_empty_trace, Finalize, Trace};
+use jrsonnet_gc::{unsafe_empty_trace, Finalize, Trace};
use jrsonnet_interner::IStr;
#[cfg(feature = "deserialize")]
use serde::Deserialize;
@@ -13,21 +13,19 @@
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Trace)]
+#[trivially_drop]
pub enum FieldName {
/// {fixed: 2}
Fixed(IStr),
/// {["dyn"+"amic"]: 3}
Dyn(LocExpr),
}
-impl Finalize for FieldName {}
-unsafe impl Trace for FieldName {
- unsafe_empty_trace!();
-}
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
-#[derive(Debug, Clone, Copy, PartialEq)]
+#[derive(Debug, Clone, Copy, PartialEq, Trace)]
+#[trivially_drop]
pub enum Visibility {
/// :
Normal,
@@ -36,10 +34,6 @@
/// :::
Unhide,
}
-impl Finalize for Visibility {}
-unsafe impl Trace for Visibility {
- unsafe_empty_trace!();
-}
impl Visibility {
pub fn is_visible(&self) -> bool {
@@ -49,16 +43,14 @@
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
-#[derive(Clone, Debug, PartialEq)]
+#[derive(Clone, Debug, PartialEq, Trace)]
+#[trivially_drop]
pub struct AssertStmt(pub LocExpr, pub Option<LocExpr>);
-impl Finalize for AssertStmt {}
-unsafe impl Trace for AssertStmt {
- unsafe_empty_trace!();
-}
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Trace)]
+#[trivially_drop]
pub struct FieldMember {
pub name: FieldName,
pub plus: bool,
@@ -66,36 +58,26 @@
pub visibility: Visibility,
pub value: LocExpr,
}
-impl Finalize for FieldMember {}
-unsafe impl Trace for FieldMember {
- unsafe_empty_trace!();
-}
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Trace)]
+#[trivially_drop]
pub enum Member {
Field(FieldMember),
BindStmt(BindSpec),
AssertStmt(AssertStmt),
}
-impl Finalize for Member {}
-unsafe impl Trace for Member {
- unsafe_empty_trace!();
-}
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
-#[derive(Debug, Clone, Copy, PartialEq)]
+#[derive(Debug, Clone, Copy, PartialEq, Trace)]
+#[trivially_drop]
pub enum UnaryOpType {
Plus,
Minus,
BitNot,
Not,
-}
-impl Finalize for UnaryOpType {}
-unsafe impl Trace for UnaryOpType {
- unsafe_empty_trace!();
}
impl Display for UnaryOpType {
@@ -116,7 +98,8 @@
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
-#[derive(Debug, Clone, Copy, PartialEq)]
+#[derive(Debug, Clone, Copy, PartialEq, Trace)]
+#[trivially_drop]
pub enum BinaryOpType {
Mul,
Div,
@@ -145,10 +128,6 @@
And,
Or,
}
-impl Finalize for BinaryOpType {}
-unsafe impl Trace for BinaryOpType {
- unsafe_empty_trace!();
-}
impl Display for BinaryOpType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -183,22 +162,22 @@
/// name, default value
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Trace)]
+#[trivially_drop]
pub struct Param(pub IStr, pub Option<LocExpr>);
-impl Finalize for Param {}
-unsafe impl Trace for Param {
- unsafe_empty_trace!();
-}
/// Defined function parameters
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct ParamsDesc(pub Rc<Vec<Param>>);
-impl Finalize for ParamsDesc {}
+
+/// Safety:
+/// AST is acyclic, and there should be no gc pointers
unsafe impl Trace for ParamsDesc {
unsafe_empty_trace!();
}
+impl Finalize for ParamsDesc {}
impl Deref for ParamsDesc {
type Target = Vec<Param>;
@@ -209,21 +188,15 @@
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Trace)]
+#[trivially_drop]
pub struct Arg(pub Option<String>, pub LocExpr);
-impl Finalize for Arg {}
-unsafe impl Trace for Arg {
- unsafe_empty_trace!();
-}
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Trace)]
+#[trivially_drop]
pub struct ArgsDesc(pub Vec<Arg>);
-impl Finalize for ArgsDesc {}
-unsafe impl Trace for ArgsDesc {
- unsafe_empty_trace!();
-}
impl Deref for ArgsDesc {
type Target = Vec<Arg>;
@@ -234,50 +207,39 @@
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
-#[derive(Debug, Clone, PartialEq)]
+#[derive(Debug, Clone, PartialEq, Trace)]
+#[trivially_drop]
pub struct BindSpec {
pub name: IStr,
pub params: Option<ParamsDesc>,
pub value: LocExpr,
-}
-impl Finalize for BindSpec {}
-unsafe impl Trace for BindSpec {
- unsafe_empty_trace!();
}
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Trace)]
+#[trivially_drop]
pub struct IfSpecData(pub LocExpr);
-impl Finalize for IfSpecData {}
-unsafe impl Trace for IfSpecData {
- unsafe_empty_trace!();
-}
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Trace)]
+#[trivially_drop]
pub struct ForSpecData(pub IStr, pub LocExpr);
-impl Finalize for ForSpecData {}
-unsafe impl Trace for ForSpecData {
- unsafe_empty_trace!();
-}
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Trace)]
+#[trivially_drop]
pub enum CompSpec {
IfSpec(IfSpecData),
ForSpec(ForSpecData),
-}
-impl Finalize for CompSpec {}
-unsafe impl Trace for CompSpec {
- unsafe_empty_trace!();
}
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Trace)]
+#[trivially_drop]
pub struct ObjComp {
pub pre_locals: Vec<BindSpec>,
pub key: LocExpr,
@@ -285,26 +247,20 @@
pub post_locals: Vec<BindSpec>,
pub compspecs: Vec<CompSpec>,
}
-impl Finalize for ObjComp {}
-unsafe impl Trace for ObjComp {
- unsafe_empty_trace!();
-}
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Trace)]
+#[trivially_drop]
pub enum ObjBody {
MemberList(Vec<Member>),
ObjComp(ObjComp),
-}
-impl Finalize for ObjBody {}
-unsafe impl Trace for ObjBody {
- unsafe_empty_trace!();
}
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
-#[derive(Debug, PartialEq, Clone, Copy)]
+#[derive(Debug, PartialEq, Clone, Copy, Trace)]
+#[trivially_drop]
pub enum LiteralType {
This,
Super,
@@ -313,26 +269,20 @@
True,
False,
}
-impl Finalize for LiteralType {}
-unsafe impl Trace for LiteralType {
- unsafe_empty_trace!();
-}
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Trace)]
+#[trivially_drop]
pub struct SliceDesc {
pub start: Option<LocExpr>,
pub end: Option<LocExpr>,
pub step: Option<LocExpr>,
-}
-impl Finalize for SliceDesc {}
-unsafe impl Trace for SliceDesc {
- unsafe_empty_trace!();
}
/// Syntax base
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Trace)]
+#[trivially_drop]
pub enum Expr {
Literal(LiteralType),
@@ -396,20 +346,13 @@
cond_else: Option<LocExpr>,
},
}
-impl Finalize for Expr {}
-unsafe impl Trace for Expr {
- unsafe_empty_trace!();
-}
/// file, begin offset, end offset
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
-#[derive(Clone, PartialEq)]
+#[derive(Clone, PartialEq, Trace)]
+#[trivially_drop]
pub struct ExprLocation(pub Rc<Path>, pub usize, pub usize);
-impl Finalize for ExprLocation {}
-unsafe impl Trace for ExprLocation {
- unsafe_empty_trace!();
-}
impl Debug for ExprLocation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -422,10 +365,12 @@
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Clone, PartialEq)]
pub struct LocExpr(pub Rc<Expr>, pub Option<ExprLocation>);
-impl Finalize for LocExpr {}
+/// Safety:
+/// AST is acyclic, and there should be no gc pointers
unsafe impl Trace for LocExpr {
unsafe_empty_trace!();
}
+impl Finalize for LocExpr {}
impl Debug for LocExpr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
crates/jrsonnet-types/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-types/Cargo.toml
+++ b/crates/jrsonnet-types/Cargo.toml
@@ -8,4 +8,4 @@
[dependencies]
peg = "0.7.0"
-gc = { version = "0.4.1", features = ["derive"] }
+jrsonnet-gc = { version = "0.4.2", features = ["derive"] }
crates/jrsonnet-types/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-types/src/lib.rs
+++ b/crates/jrsonnet-types/src/lib.rs
@@ -1,6 +1,6 @@
#![allow(clippy::redundant_closure_call)]
-use gc::{unsafe_empty_trace, Finalize, Trace};
+use jrsonnet_gc::Trace;
use std::fmt::Display;
#[macro_export]
@@ -78,7 +78,8 @@
);
}
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Trace)]
+#[trivially_drop]
pub enum ValType {
Bool,
Null,
@@ -87,10 +88,6 @@
Arr,
Obj,
Func,
-}
-impl Finalize for ValType {}
-unsafe impl Trace for ValType {
- unsafe_empty_trace!();
}
impl ValType {
@@ -114,7 +111,8 @@
}
}
-#[derive(Debug, Clone, PartialEq)]
+#[derive(Debug, Clone, PartialEq, Trace)]
+#[trivially_drop]
pub enum ComplexValType {
Any,
Char,
@@ -127,10 +125,6 @@
UnionRef(&'static [ComplexValType]),
Sum(Vec<ComplexValType>),
SumRef(&'static [ComplexValType]),
-}
-impl Finalize for ComplexValType {}
-unsafe impl Trace for ComplexValType {
- unsafe_empty_trace!();
}
impl From<ValType> for ComplexValType {