difftreelog
feat(jrsonnet-evaluator) implement gc
in: master
Some manual Trace/Finalize implementations can be replaced with derives with https://github.com/Manishearth/rust-gc/pull/116 getting merged
17 files changed
crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -30,13 +30,12 @@
jrsonnet-types = { path = "../jrsonnet-types", version = "0.3.7" }
pathdiff = "0.2.0"
-closure = "0.3.0"
-
md5 = "0.7.0"
base64 = "0.13.0"
rustc-hash = "1.1.0"
thiserror = "1.0"
+gc = { version = "0.4.1", features = ["derive"] }
[dependencies.anyhow]
version = "1.0"
crates/jrsonnet-evaluator/src/builtin/format.rsdiffbeforeafterboth1//! faster std.format impl2#![allow(clippy::too_many_arguments)]34use crate::{error::Error::*, throw, LocError, ObjValue, Result, Val};5use jrsonnet_interner::IStr;6use jrsonnet_types::ValType;7use thiserror::Error;89#[derive(Debug, Clone, Error)]10pub enum FormatError {11 #[error("truncated format code")]12 TruncatedFormatCode,13 #[error("unrecognized conversion type: {0}")]14 UnrecognizedConversionType(char),1516 #[error("not enough values")]17 NotEnoughValues,1819 #[error("cannot use * width with object")]20 CannotUseStarWidthWithObject,21 #[error("mapping keys required")]22 MappingKeysRequired,23 #[error("no such format field: {0}")]24 NoSuchFormatField(IStr),25}2627impl From<FormatError> for LocError {28 fn from(e: FormatError) -> Self {29 Self::new(Format(e))30 }31}3233use FormatError::*;3435type ParseResult<'t, T> = std::result::Result<(T, &'t str), FormatError>;3637pub fn try_parse_mapping_key(str: &str) -> ParseResult<&str> {38 if str.is_empty() {39 return Err(TruncatedFormatCode);40 }41 let bytes = str.as_bytes();42 if bytes[0] == b'(' {43 let mut i = 1;44 while i < bytes.len() {45 if bytes[i] == b')' {46 return Ok((&str[1..i as usize], &str[i as usize + 1..]));47 }48 i += 1;49 }50 Err(TruncatedFormatCode)51 } else {52 Ok(("", str))53 }54}5556#[cfg(test)]57pub mod tests_key {58 use super::*;5960 #[test]61 fn parse_key() {62 assert_eq!(63 try_parse_mapping_key("(hello ) world").unwrap(),64 ("hello ", " world")65 );66 assert_eq!(try_parse_mapping_key("() world").unwrap(), ("", " world"));67 assert_eq!(try_parse_mapping_key(" world").unwrap(), ("", " world"));68 assert_eq!(69 try_parse_mapping_key(" () world").unwrap(),70 ("", " () world")71 );72 }7374 #[test]75 #[should_panic]76 fn parse_key_missing_start() {77 try_parse_mapping_key("").unwrap();78 }7980 #[test]81 #[should_panic]82 fn parse_key_missing_end() {83 try_parse_mapping_key("( ").unwrap();84 }85}8687#[derive(Default, Debug)]88pub struct CFlags {89 pub alt: bool,90 pub zero: bool,91 pub left: bool,92 pub blank: bool,93 pub sign: bool,94}9596pub fn try_parse_cflags(str: &str) -> ParseResult<CFlags> {97 if str.is_empty() {98 return Err(TruncatedFormatCode);99 }100 let bytes = str.as_bytes();101 let mut i = 0;102 let mut out = CFlags::default();103 loop {104 if bytes.len() == i {105 return Err(TruncatedFormatCode);106 }107 match bytes[i] {108 b'#' => out.alt = true,109 b'0' => out.zero = true,110 b'-' => out.left = true,111 b' ' => out.blank = true,112 b'+' => out.sign = true,113 _ => break,114 }115 i += 1;116 }117 Ok((out, &str[i..]))118}119120#[derive(Debug, PartialEq)]121pub enum Width {122 Star,123 Fixed(usize),124}125pub fn try_parse_field_width(str: &str) -> ParseResult<Width> {126 if str.is_empty() {127 return Err(TruncatedFormatCode);128 }129 let bytes = str.as_bytes();130 if bytes[0] == b'*' {131 return Ok((Width::Star, &str[1..]));132 }133 let mut out: usize = 0;134 let mut digits = 0;135 while let Some(digit) = (bytes[digits] as char).to_digit(10) {136 out *= 10;137 out += digit as usize;138 digits += 1;139 if digits == bytes.len() {140 return Err(TruncatedFormatCode);141 }142 }143 Ok((Width::Fixed(out), &str[digits..]))144}145146pub fn try_parse_precision(str: &str) -> ParseResult<Option<Width>> {147 if str.is_empty() {148 return Err(TruncatedFormatCode);149 }150 let bytes = str.as_bytes();151 if bytes[0] == b'.' {152 try_parse_field_width(&str[1..]).map(|(r, s)| (Some(r), s))153 } else {154 Ok((None, str))155 }156}157158// Only skips159pub fn try_parse_length_modifier(str: &str) -> ParseResult<()> {160 if str.is_empty() {161 return Err(TruncatedFormatCode);162 }163 let bytes = str.as_bytes();164 let mut idx = 0;165 while bytes[idx] == b'h' || bytes[idx] == b'l' || bytes[idx] == b'L' {166 idx += 1;167 if bytes.len() == idx {168 return Err(TruncatedFormatCode);169 }170 }171 Ok(((), &str[idx..]))172}173174#[derive(Debug, PartialEq)]175pub enum ConvTypeV {176 Decimal,177 Octal,178 Hexadecimal,179 Scientific,180 Float,181 Shorter,182 Char,183 String,184 Percent,185}186pub struct ConvType {187 v: ConvTypeV,188 caps: bool,189}190191pub fn parse_conversion_type(str: &str) -> ParseResult<ConvType> {192 if str.is_empty() {193 return Err(TruncatedFormatCode);194 }195196 let code = str.as_bytes()[0];197 let v: (ConvTypeV, bool) = match code {198 b'd' | b'i' | b'u' => (ConvTypeV::Decimal, false),199 b'o' => (ConvTypeV::Octal, false),200 b'x' => (ConvTypeV::Hexadecimal, false),201 b'X' => (ConvTypeV::Hexadecimal, true),202 b'e' => (ConvTypeV::Scientific, false),203 b'E' => (ConvTypeV::Scientific, true),204 b'f' => (ConvTypeV::Float, false),205 b'F' => (ConvTypeV::Float, true),206 b'g' => (ConvTypeV::Shorter, false),207 b'G' => (ConvTypeV::Shorter, true),208 b'c' => (ConvTypeV::Char, false),209 b's' => (ConvTypeV::String, false),210 b'%' => (ConvTypeV::Percent, false),211 c => return Err(UnrecognizedConversionType(c as char)),212 };213214 Ok((ConvType { v: v.0, caps: v.1 }, &str[1..]))215}216217#[derive(Debug)]218pub struct Code<'s> {219 mkey: &'s str,220 cflags: CFlags,221 width: Width,222 precision: Option<Width>,223 convtype: ConvTypeV,224 caps: bool,225}226pub fn parse_code(str: &str) -> ParseResult<Code> {227 if str.is_empty() {228 return Err(TruncatedFormatCode);229 }230 let (mkey, str) = try_parse_mapping_key(str)?;231 let (cflags, str) = try_parse_cflags(str)?;232 let (width, str) = try_parse_field_width(str)?;233 let (precision, str) = try_parse_precision(str)?;234 let (_, str) = try_parse_length_modifier(str)?;235 let (convtype, str) = parse_conversion_type(str)?;236237 Ok((238 Code {239 mkey,240 cflags,241 width,242 precision,243 convtype: convtype.v,244 caps: convtype.caps,245 },246 str,247 ))248}249250#[derive(Debug)]251pub enum Element<'s> {252 String(&'s str),253 Code(Code<'s>),254}255pub fn parse_codes(mut str: &str) -> Result<Vec<Element>> {256 let mut bytes = str.as_bytes();257 let mut out = vec![];258 let mut offset = 0;259260 loop {261 while offset != bytes.len() && bytes[offset] != b'%' {262 offset += 1;263 }264 if offset != 0 {265 out.push(Element::String(&str[0..offset]));266 }267 if offset == bytes.len() {268 return Ok(out);269 }270 str = &str[offset + 1..];271 let (code, nstr) = parse_code(str)?;272 str = nstr;273 bytes = str.as_bytes();274 offset = 0;275276 out.push(Element::Code(code))277 }278}279280const NUMBERS: &[u8] = b"0123456789abcdefghijklmnopqrstuvwxyz";281282#[inline]283pub fn render_integer(284 out: &mut String,285 iv: i64,286 padding: usize,287 precision: usize,288 blank: bool,289 sign: bool,290 radix: i64,291 prefix: &str,292 caps: bool,293) {294 // Digit char indexes in reverse order, i.e295 // for radix = 16 and n = 12f: [15, 2, 1]296 let digits = if iv == 0 {297 vec![0u8]298 } else {299 let mut v = iv.abs();300 let mut nums = Vec::with_capacity(1);301 while v > 0 {302 nums.push((v % radix) as u8);303 v /= radix;304 }305 nums306 };307 let neg = iv < 0;308 let zp = padding.saturating_sub(if neg || blank || sign { 1 } else { 0 });309 let zp2 = zp310 .max(precision)311 .saturating_sub(prefix.len() + digits.len());312313 if neg {314 out.push('-')315 } else if sign {316 out.push('+');317 } else if blank {318 out.push(' ');319 }320321 out.reserve(zp2);322 for _ in 0..zp2 {323 out.push('0');324 }325 out.push_str(prefix);326327 for digit in digits.into_iter().rev() {328 let ch = NUMBERS[digit as usize] as char;329 out.push(if caps { ch.to_ascii_uppercase() } else { ch });330 }331}332333pub fn render_decimal(334 out: &mut String,335 iv: i64,336 padding: usize,337 precision: usize,338 blank: bool,339 sign: bool,340) {341 render_integer(out, iv, padding, precision, blank, sign, 10, "", false)342}343pub fn render_octal(344 out: &mut String,345 iv: i64,346 padding: usize,347 precision: usize,348 alt: bool,349 blank: bool,350 sign: bool,351) {352 render_integer(353 out,354 iv,355 padding,356 precision,357 blank,358 sign,359 8,360 if alt && iv != 0 { "0" } else { "" },361 false,362 )363}364pub fn render_hexadecimal(365 out: &mut String,366 iv: i64,367 padding: usize,368 precision: usize,369 alt: bool,370 blank: bool,371 sign: bool,372 caps: bool,373) {374 render_integer(375 out,376 iv,377 padding,378 precision,379 blank,380 sign,381 16,382 match (alt, caps) {383 (true, true) => "0X",384 (true, false) => "0x",385 (false, _) => "",386 },387 caps,388 )389}390391pub fn render_float(392 out: &mut String,393 n: f64,394 mut padding: usize,395 precision: usize,396 blank: bool,397 sign: bool,398 ensure_pt: bool,399 trailing: bool,400) {401 let dot_size = if precision == 0 && !ensure_pt { 0 } else { 1 };402 padding = padding.saturating_sub(dot_size + precision);403 render_decimal(out, n.floor() as i64, padding, 0, blank, sign);404 if precision == 0 {405 if ensure_pt {406 out.push('.');407 }408 return;409 }410 let frac = n411 .fract()412 .mul_add(10.0_f64.powf(precision as f64), 0.5)413 .floor();414 if trailing || frac > 0.0 {415 out.push('.');416 let mut frac_str = String::new();417 render_decimal(&mut frac_str, frac as i64, precision, 0, false, false);418 let mut trim = frac_str.len();419 if !trailing {420 for b in frac_str.as_bytes().iter().rev() {421 if *b == b'0' {422 trim -= 1;423 }424 }425 }426 out.push_str(&frac_str[..trim]);427 } else if ensure_pt {428 out.push('.');429 }430}431432pub fn render_float_sci(433 out: &mut String,434 n: f64,435 mut padding: usize,436 precision: usize,437 blank: bool,438 sign: bool,439 ensure_pt: bool,440 trailing: bool,441 caps: bool,442) {443 let exponent = n.log10().floor();444 let mantissa = if exponent as i16 == -324 {445 n * 10.0 / 10.0_f64.powf(exponent + 1.0)446 } else {447 n / 10.0_f64.powf(exponent)448 };449 let mut exponent_str = String::new();450 render_decimal(&mut exponent_str, exponent as i64, 3, 0, false, true);451452 // +1 for e453 padding = padding.saturating_sub(exponent_str.len() + 1);454455 render_float(456 out, mantissa, padding, precision, blank, sign, ensure_pt, trailing,457 );458 out.push(if caps { 'E' } else { 'e' });459 out.push_str(&exponent_str);460}461462pub fn format_code(463 out: &mut String,464 value: &Val,465 code: &Code,466 width: usize,467 precision: Option<usize>,468) -> Result<()> {469 let clfags = &code.cflags;470 let (fpprec, iprec) = match precision {471 Some(v) => (v, v),472 None => (6, 0),473 };474 let padding = if clfags.zero && !clfags.left {475 width476 } else {477 0478 };479480 // TODO: If left padded, can optimize by writing directly to out481 let mut tmp_out = String::new();482483 match code.convtype {484 ConvTypeV::String => tmp_out.push_str(&value.clone().to_string()?),485 ConvTypeV::Decimal => {486 let value = value.clone().try_cast_num("%d/%u/%i requires number")?;487 render_decimal(488 &mut tmp_out,489 value as i64,490 padding,491 iprec,492 clfags.blank,493 clfags.sign,494 );495 }496 ConvTypeV::Octal => {497 let value = value.clone().try_cast_num("%o requires number")?;498 render_octal(499 &mut tmp_out,500 value as i64,501 padding,502 iprec,503 clfags.alt,504 clfags.blank,505 clfags.sign,506 );507 }508 ConvTypeV::Hexadecimal => {509 let value = value.clone().try_cast_num("%x/%X requires number")?;510 render_hexadecimal(511 &mut tmp_out,512 value as i64,513 padding,514 iprec,515 clfags.alt,516 clfags.blank,517 clfags.sign,518 code.caps,519 );520 }521 ConvTypeV::Scientific => {522 let value = value.clone().try_cast_num("%e/%E requires number")?;523 render_float_sci(524 &mut tmp_out,525 value,526 padding,527 fpprec,528 clfags.blank,529 clfags.sign,530 clfags.alt,531 true,532 code.caps,533 );534 }535 ConvTypeV::Float => {536 let value = value.clone().try_cast_num("%e/%E requires number")?;537 render_float(538 &mut tmp_out,539 value,540 padding,541 fpprec,542 clfags.blank,543 clfags.sign,544 clfags.alt,545 true,546 );547 }548 ConvTypeV::Shorter => {549 let value = value.clone().try_cast_num("%g/%G requires number")?;550 let exponent = value.log10().floor();551 if exponent < -4.0 || exponent >= fpprec as f64 {552 render_float_sci(553 &mut tmp_out,554 value,555 padding,556 fpprec - 1,557 clfags.blank,558 clfags.sign,559 clfags.alt,560 clfags.alt,561 code.caps,562 );563 } else {564 let digits_before_pt = 1.max(exponent as usize + 1);565 render_float(566 &mut tmp_out,567 value,568 padding,569 fpprec - digits_before_pt,570 clfags.blank,571 clfags.sign,572 clfags.alt,573 clfags.alt,574 );575 }576 }577 ConvTypeV::Char => match value.clone() {578 Val::Num(n) => tmp_out.push(579 std::char::from_u32(n as u32)580 .ok_or_else(|| InvalidUnicodeCodepointGot(n as u32))?,581 ),582 Val::Str(s) => {583 if s.chars().count() != 1 {584 throw!(RuntimeError(585 format!("%c expected 1 char string, got {}", s.chars().count()).into(),586 ));587 }588 tmp_out.push_str(&s);589 }590 _ => {591 throw!(TypeMismatch(592 "%c requires number/string",593 vec![ValType::Num, ValType::Str],594 value.value_type(),595 ));596 }597 },598 ConvTypeV::Percent => tmp_out.push('%'),599 };600601 let padding = width.saturating_sub(tmp_out.len());602603 if !clfags.left {604 for _ in 0..padding {605 out.push(' ');606 }607 }608 out.push_str(&tmp_out);609 if clfags.left {610 for _ in 0..padding {611 out.push(' ');612 }613 }614615 Ok(())616}617618pub fn format_arr(str: &str, mut values: &[Val]) -> Result<String> {619 let codes = parse_codes(str)?;620 let mut out = String::new();621622 for code in codes {623 match code {624 Element::String(s) => {625 out.push_str(s);626 }627 Element::Code(c) => {628 let width = match c.width {629 Width::Star => {630 if values.is_empty() {631 throw!(NotEnoughValues);632 }633 let value = &values[0];634 values = &values[1..];635 value.clone().try_cast_num("field width")? as usize636 }637 Width::Fixed(n) => n,638 };639 let precision = match c.precision {640 Some(Width::Star) => {641 if values.is_empty() {642 throw!(NotEnoughValues);643 }644 let value = &values[0];645 values = &values[1..];646 Some(value.clone().try_cast_num("field precision")? as usize)647 }648 Some(Width::Fixed(n)) => Some(n),649 None => None,650 };651652 // %% should not consume a value653 let value = if c.convtype == ConvTypeV::Percent {654 &Val::Null655 } else {656 if values.is_empty() {657 throw!(NotEnoughValues);658 }659 let value = &values[0];660 values = &values[1..];661 value662 };663664 format_code(&mut out, value, &c, width, precision)?;665 }666 }667 }668669 Ok(out)670}671672pub fn format_obj(str: &str, values: &ObjValue) -> Result<String> {673 let codes = parse_codes(str)?;674 let mut out = String::new();675676 for code in codes {677 match code {678 Element::String(s) => {679 out.push_str(s);680 }681 Element::Code(c) => {682 // TODO: Operate on ref683 let f: IStr = c.mkey.into();684 let width = match c.width {685 Width::Star => {686 throw!(CannotUseStarWidthWithObject);687 }688 Width::Fixed(n) => n,689 };690 let precision = match c.precision {691 Some(Width::Star) => {692 throw!(CannotUseStarWidthWithObject);693 }694 Some(Width::Fixed(n)) => Some(n),695 None => None,696 };697698 let value = if c.convtype == ConvTypeV::Percent {699 Val::Null700 } else {701 if f.is_empty() {702 throw!(MappingKeysRequired);703 }704 if let Some(v) = values.get(f.clone())? {705 v706 } else {707 throw!(NoSuchFormatField(f));708 }709 };710711 format_code(&mut out, &value, &c, width, precision)?;712 }713 }714 }715716 Ok(out)717}718719#[cfg(test)]720pub mod test_format {721 use super::*;722723 #[test]724 fn parse() {725 assert_eq!(726 parse_codes(727 "How much error budget is left looking at our %.3f%% availability gurantees?"728 )729 .unwrap()730 .len(),731 4732 );733 }734735 #[test]736 fn octals() {737 assert_eq!(format_arr("%#o", &[Val::Num(8.0)]).unwrap(), "010");738 assert_eq!(format_arr("%#4o", &[Val::Num(8.0)]).unwrap(), " 010");739 assert_eq!(format_arr("%4o", &[Val::Num(8.0)]).unwrap(), " 10");740 assert_eq!(format_arr("%04o", &[Val::Num(8.0)]).unwrap(), "0010");741 assert_eq!(format_arr("%+4o", &[Val::Num(8.0)]).unwrap(), " +10");742 assert_eq!(format_arr("%+04o", &[Val::Num(8.0)]).unwrap(), "+010");743 assert_eq!(format_arr("%-4o", &[Val::Num(8.0)]).unwrap(), "10 ");744 assert_eq!(format_arr("%+-4o", &[Val::Num(8.0)]).unwrap(), "+10 ");745 assert_eq!(format_arr("%+-04o", &[Val::Num(8.0)]).unwrap(), "+10 ");746 }747748 #[test]749 fn percent_doesnt_consumes_values() {750 assert_eq!(751 format_arr(752 "How much error budget is left looking at our %.3f%% availability gurantees?",753 &[Val::Num(4.0)]754 )755 .unwrap(),756 "How much error budget is left looking at our 4.000% availability gurantees?"757 );758 }759}1//! faster std.format impl2#![allow(clippy::too_many_arguments)]34use crate::{error::Error::*, throw, LocError, ObjValue, Result, Val};5use gc::{Finalize, Trace};6use jrsonnet_interner::IStr;7use jrsonnet_types::ValType;8use thiserror::Error;910#[derive(Debug, Clone, Error, Trace, Finalize)]11pub enum FormatError {12 #[error("truncated format code")]13 TruncatedFormatCode,14 #[error("unrecognized conversion type: {0}")]15 UnrecognizedConversionType(char),1617 #[error("not enough values")]18 NotEnoughValues,1920 #[error("cannot use * width with object")]21 CannotUseStarWidthWithObject,22 #[error("mapping keys required")]23 MappingKeysRequired,24 #[error("no such format field: {0}")]25 NoSuchFormatField(IStr),26}2728impl From<FormatError> for LocError {29 fn from(e: FormatError) -> Self {30 Self::new(Format(e))31 }32}3334use FormatError::*;3536type ParseResult<'t, T> = std::result::Result<(T, &'t str), FormatError>;3738pub fn try_parse_mapping_key(str: &str) -> ParseResult<&str> {39 if str.is_empty() {40 return Err(TruncatedFormatCode);41 }42 let bytes = str.as_bytes();43 if bytes[0] == b'(' {44 let mut i = 1;45 while i < bytes.len() {46 if bytes[i] == b')' {47 return Ok((&str[1..i as usize], &str[i as usize + 1..]));48 }49 i += 1;50 }51 Err(TruncatedFormatCode)52 } else {53 Ok(("", str))54 }55}5657#[cfg(test)]58pub mod tests_key {59 use super::*;6061 #[test]62 fn parse_key() {63 assert_eq!(64 try_parse_mapping_key("(hello ) world").unwrap(),65 ("hello ", " world")66 );67 assert_eq!(try_parse_mapping_key("() world").unwrap(), ("", " world"));68 assert_eq!(try_parse_mapping_key(" world").unwrap(), ("", " world"));69 assert_eq!(70 try_parse_mapping_key(" () world").unwrap(),71 ("", " () world")72 );73 }7475 #[test]76 #[should_panic]77 fn parse_key_missing_start() {78 try_parse_mapping_key("").unwrap();79 }8081 #[test]82 #[should_panic]83 fn parse_key_missing_end() {84 try_parse_mapping_key("( ").unwrap();85 }86}8788#[derive(Default, Debug)]89pub struct CFlags {90 pub alt: bool,91 pub zero: bool,92 pub left: bool,93 pub blank: bool,94 pub sign: bool,95}9697pub fn try_parse_cflags(str: &str) -> ParseResult<CFlags> {98 if str.is_empty() {99 return Err(TruncatedFormatCode);100 }101 let bytes = str.as_bytes();102 let mut i = 0;103 let mut out = CFlags::default();104 loop {105 if bytes.len() == i {106 return Err(TruncatedFormatCode);107 }108 match bytes[i] {109 b'#' => out.alt = true,110 b'0' => out.zero = true,111 b'-' => out.left = true,112 b' ' => out.blank = true,113 b'+' => out.sign = true,114 _ => break,115 }116 i += 1;117 }118 Ok((out, &str[i..]))119}120121#[derive(Debug, PartialEq)]122pub enum Width {123 Star,124 Fixed(usize),125}126pub fn try_parse_field_width(str: &str) -> ParseResult<Width> {127 if str.is_empty() {128 return Err(TruncatedFormatCode);129 }130 let bytes = str.as_bytes();131 if bytes[0] == b'*' {132 return Ok((Width::Star, &str[1..]));133 }134 let mut out: usize = 0;135 let mut digits = 0;136 while let Some(digit) = (bytes[digits] as char).to_digit(10) {137 out *= 10;138 out += digit as usize;139 digits += 1;140 if digits == bytes.len() {141 return Err(TruncatedFormatCode);142 }143 }144 Ok((Width::Fixed(out), &str[digits..]))145}146147pub fn try_parse_precision(str: &str) -> ParseResult<Option<Width>> {148 if str.is_empty() {149 return Err(TruncatedFormatCode);150 }151 let bytes = str.as_bytes();152 if bytes[0] == b'.' {153 try_parse_field_width(&str[1..]).map(|(r, s)| (Some(r), s))154 } else {155 Ok((None, str))156 }157}158159// Only skips160pub fn try_parse_length_modifier(str: &str) -> ParseResult<()> {161 if str.is_empty() {162 return Err(TruncatedFormatCode);163 }164 let bytes = str.as_bytes();165 let mut idx = 0;166 while bytes[idx] == b'h' || bytes[idx] == b'l' || bytes[idx] == b'L' {167 idx += 1;168 if bytes.len() == idx {169 return Err(TruncatedFormatCode);170 }171 }172 Ok(((), &str[idx..]))173}174175#[derive(Debug, PartialEq)]176pub enum ConvTypeV {177 Decimal,178 Octal,179 Hexadecimal,180 Scientific,181 Float,182 Shorter,183 Char,184 String,185 Percent,186}187pub struct ConvType {188 v: ConvTypeV,189 caps: bool,190}191192pub fn parse_conversion_type(str: &str) -> ParseResult<ConvType> {193 if str.is_empty() {194 return Err(TruncatedFormatCode);195 }196197 let code = str.as_bytes()[0];198 let v: (ConvTypeV, bool) = match code {199 b'd' | b'i' | b'u' => (ConvTypeV::Decimal, false),200 b'o' => (ConvTypeV::Octal, false),201 b'x' => (ConvTypeV::Hexadecimal, false),202 b'X' => (ConvTypeV::Hexadecimal, true),203 b'e' => (ConvTypeV::Scientific, false),204 b'E' => (ConvTypeV::Scientific, true),205 b'f' => (ConvTypeV::Float, false),206 b'F' => (ConvTypeV::Float, true),207 b'g' => (ConvTypeV::Shorter, false),208 b'G' => (ConvTypeV::Shorter, true),209 b'c' => (ConvTypeV::Char, false),210 b's' => (ConvTypeV::String, false),211 b'%' => (ConvTypeV::Percent, false),212 c => return Err(UnrecognizedConversionType(c as char)),213 };214215 Ok((ConvType { v: v.0, caps: v.1 }, &str[1..]))216}217218#[derive(Debug)]219pub struct Code<'s> {220 mkey: &'s str,221 cflags: CFlags,222 width: Width,223 precision: Option<Width>,224 convtype: ConvTypeV,225 caps: bool,226}227pub fn parse_code(str: &str) -> ParseResult<Code> {228 if str.is_empty() {229 return Err(TruncatedFormatCode);230 }231 let (mkey, str) = try_parse_mapping_key(str)?;232 let (cflags, str) = try_parse_cflags(str)?;233 let (width, str) = try_parse_field_width(str)?;234 let (precision, str) = try_parse_precision(str)?;235 let (_, str) = try_parse_length_modifier(str)?;236 let (convtype, str) = parse_conversion_type(str)?;237238 Ok((239 Code {240 mkey,241 cflags,242 width,243 precision,244 convtype: convtype.v,245 caps: convtype.caps,246 },247 str,248 ))249}250251#[derive(Debug)]252pub enum Element<'s> {253 String(&'s str),254 Code(Code<'s>),255}256pub fn parse_codes(mut str: &str) -> Result<Vec<Element>> {257 let mut bytes = str.as_bytes();258 let mut out = vec![];259 let mut offset = 0;260261 loop {262 while offset != bytes.len() && bytes[offset] != b'%' {263 offset += 1;264 }265 if offset != 0 {266 out.push(Element::String(&str[0..offset]));267 }268 if offset == bytes.len() {269 return Ok(out);270 }271 str = &str[offset + 1..];272 let (code, nstr) = parse_code(str)?;273 str = nstr;274 bytes = str.as_bytes();275 offset = 0;276277 out.push(Element::Code(code))278 }279}280281const NUMBERS: &[u8] = b"0123456789abcdefghijklmnopqrstuvwxyz";282283#[inline]284pub fn render_integer(285 out: &mut String,286 iv: i64,287 padding: usize,288 precision: usize,289 blank: bool,290 sign: bool,291 radix: i64,292 prefix: &str,293 caps: bool,294) {295 // Digit char indexes in reverse order, i.e296 // for radix = 16 and n = 12f: [15, 2, 1]297 let digits = if iv == 0 {298 vec![0u8]299 } else {300 let mut v = iv.abs();301 let mut nums = Vec::with_capacity(1);302 while v > 0 {303 nums.push((v % radix) as u8);304 v /= radix;305 }306 nums307 };308 let neg = iv < 0;309 let zp = padding.saturating_sub(if neg || blank || sign { 1 } else { 0 });310 let zp2 = zp311 .max(precision)312 .saturating_sub(prefix.len() + digits.len());313314 if neg {315 out.push('-')316 } else if sign {317 out.push('+');318 } else if blank {319 out.push(' ');320 }321322 out.reserve(zp2);323 for _ in 0..zp2 {324 out.push('0');325 }326 out.push_str(prefix);327328 for digit in digits.into_iter().rev() {329 let ch = NUMBERS[digit as usize] as char;330 out.push(if caps { ch.to_ascii_uppercase() } else { ch });331 }332}333334pub fn render_decimal(335 out: &mut String,336 iv: i64,337 padding: usize,338 precision: usize,339 blank: bool,340 sign: bool,341) {342 render_integer(out, iv, padding, precision, blank, sign, 10, "", false)343}344pub fn render_octal(345 out: &mut String,346 iv: i64,347 padding: usize,348 precision: usize,349 alt: bool,350 blank: bool,351 sign: bool,352) {353 render_integer(354 out,355 iv,356 padding,357 precision,358 blank,359 sign,360 8,361 if alt && iv != 0 { "0" } else { "" },362 false,363 )364}365pub fn render_hexadecimal(366 out: &mut String,367 iv: i64,368 padding: usize,369 precision: usize,370 alt: bool,371 blank: bool,372 sign: bool,373 caps: bool,374) {375 render_integer(376 out,377 iv,378 padding,379 precision,380 blank,381 sign,382 16,383 match (alt, caps) {384 (true, true) => "0X",385 (true, false) => "0x",386 (false, _) => "",387 },388 caps,389 )390}391392pub fn render_float(393 out: &mut String,394 n: f64,395 mut padding: usize,396 precision: usize,397 blank: bool,398 sign: bool,399 ensure_pt: bool,400 trailing: bool,401) {402 let dot_size = if precision == 0 && !ensure_pt { 0 } else { 1 };403 padding = padding.saturating_sub(dot_size + precision);404 render_decimal(out, n.floor() as i64, padding, 0, blank, sign);405 if precision == 0 {406 if ensure_pt {407 out.push('.');408 }409 return;410 }411 let frac = n412 .fract()413 .mul_add(10.0_f64.powf(precision as f64), 0.5)414 .floor();415 if trailing || frac > 0.0 {416 out.push('.');417 let mut frac_str = String::new();418 render_decimal(&mut frac_str, frac as i64, precision, 0, false, false);419 let mut trim = frac_str.len();420 if !trailing {421 for b in frac_str.as_bytes().iter().rev() {422 if *b == b'0' {423 trim -= 1;424 }425 }426 }427 out.push_str(&frac_str[..trim]);428 } else if ensure_pt {429 out.push('.');430 }431}432433pub fn render_float_sci(434 out: &mut String,435 n: f64,436 mut padding: usize,437 precision: usize,438 blank: bool,439 sign: bool,440 ensure_pt: bool,441 trailing: bool,442 caps: bool,443) {444 let exponent = n.log10().floor();445 let mantissa = if exponent as i16 == -324 {446 n * 10.0 / 10.0_f64.powf(exponent + 1.0)447 } else {448 n / 10.0_f64.powf(exponent)449 };450 let mut exponent_str = String::new();451 render_decimal(&mut exponent_str, exponent as i64, 3, 0, false, true);452453 // +1 for e454 padding = padding.saturating_sub(exponent_str.len() + 1);455456 render_float(457 out, mantissa, padding, precision, blank, sign, ensure_pt, trailing,458 );459 out.push(if caps { 'E' } else { 'e' });460 out.push_str(&exponent_str);461}462463pub fn format_code(464 out: &mut String,465 value: &Val,466 code: &Code,467 width: usize,468 precision: Option<usize>,469) -> Result<()> {470 let clfags = &code.cflags;471 let (fpprec, iprec) = match precision {472 Some(v) => (v, v),473 None => (6, 0),474 };475 let padding = if clfags.zero && !clfags.left {476 width477 } else {478 0479 };480481 // TODO: If left padded, can optimize by writing directly to out482 let mut tmp_out = String::new();483484 match code.convtype {485 ConvTypeV::String => tmp_out.push_str(&value.clone().to_string()?),486 ConvTypeV::Decimal => {487 let value = value.clone().try_cast_num("%d/%u/%i requires number")?;488 render_decimal(489 &mut tmp_out,490 value as i64,491 padding,492 iprec,493 clfags.blank,494 clfags.sign,495 );496 }497 ConvTypeV::Octal => {498 let value = value.clone().try_cast_num("%o requires number")?;499 render_octal(500 &mut tmp_out,501 value as i64,502 padding,503 iprec,504 clfags.alt,505 clfags.blank,506 clfags.sign,507 );508 }509 ConvTypeV::Hexadecimal => {510 let value = value.clone().try_cast_num("%x/%X requires number")?;511 render_hexadecimal(512 &mut tmp_out,513 value as i64,514 padding,515 iprec,516 clfags.alt,517 clfags.blank,518 clfags.sign,519 code.caps,520 );521 }522 ConvTypeV::Scientific => {523 let value = value.clone().try_cast_num("%e/%E requires number")?;524 render_float_sci(525 &mut tmp_out,526 value,527 padding,528 fpprec,529 clfags.blank,530 clfags.sign,531 clfags.alt,532 true,533 code.caps,534 );535 }536 ConvTypeV::Float => {537 let value = value.clone().try_cast_num("%e/%E requires number")?;538 render_float(539 &mut tmp_out,540 value,541 padding,542 fpprec,543 clfags.blank,544 clfags.sign,545 clfags.alt,546 true,547 );548 }549 ConvTypeV::Shorter => {550 let value = value.clone().try_cast_num("%g/%G requires number")?;551 let exponent = value.log10().floor();552 if exponent < -4.0 || exponent >= fpprec as f64 {553 render_float_sci(554 &mut tmp_out,555 value,556 padding,557 fpprec - 1,558 clfags.blank,559 clfags.sign,560 clfags.alt,561 clfags.alt,562 code.caps,563 );564 } else {565 let digits_before_pt = 1.max(exponent as usize + 1);566 render_float(567 &mut tmp_out,568 value,569 padding,570 fpprec - digits_before_pt,571 clfags.blank,572 clfags.sign,573 clfags.alt,574 clfags.alt,575 );576 }577 }578 ConvTypeV::Char => match value.clone() {579 Val::Num(n) => tmp_out.push(580 std::char::from_u32(n as u32)581 .ok_or_else(|| InvalidUnicodeCodepointGot(n as u32))?,582 ),583 Val::Str(s) => {584 if s.chars().count() != 1 {585 throw!(RuntimeError(586 format!("%c expected 1 char string, got {}", s.chars().count()).into(),587 ));588 }589 tmp_out.push_str(&s);590 }591 _ => {592 throw!(TypeMismatch(593 "%c requires number/string",594 vec![ValType::Num, ValType::Str],595 value.value_type(),596 ));597 }598 },599 ConvTypeV::Percent => tmp_out.push('%'),600 };601602 let padding = width.saturating_sub(tmp_out.len());603604 if !clfags.left {605 for _ in 0..padding {606 out.push(' ');607 }608 }609 out.push_str(&tmp_out);610 if clfags.left {611 for _ in 0..padding {612 out.push(' ');613 }614 }615616 Ok(())617}618619pub fn format_arr(str: &str, mut values: &[Val]) -> Result<String> {620 let codes = parse_codes(str)?;621 let mut out = String::new();622623 for code in codes {624 match code {625 Element::String(s) => {626 out.push_str(s);627 }628 Element::Code(c) => {629 let width = match c.width {630 Width::Star => {631 if values.is_empty() {632 throw!(NotEnoughValues);633 }634 let value = &values[0];635 values = &values[1..];636 value.clone().try_cast_num("field width")? as usize637 }638 Width::Fixed(n) => n,639 };640 let precision = match c.precision {641 Some(Width::Star) => {642 if values.is_empty() {643 throw!(NotEnoughValues);644 }645 let value = &values[0];646 values = &values[1..];647 Some(value.clone().try_cast_num("field precision")? as usize)648 }649 Some(Width::Fixed(n)) => Some(n),650 None => None,651 };652653 // %% should not consume a value654 let value = if c.convtype == ConvTypeV::Percent {655 &Val::Null656 } else {657 if values.is_empty() {658 throw!(NotEnoughValues);659 }660 let value = &values[0];661 values = &values[1..];662 value663 };664665 format_code(&mut out, value, &c, width, precision)?;666 }667 }668 }669670 Ok(out)671}672673pub fn format_obj(str: &str, values: &ObjValue) -> Result<String> {674 let codes = parse_codes(str)?;675 let mut out = String::new();676677 for code in codes {678 match code {679 Element::String(s) => {680 out.push_str(s);681 }682 Element::Code(c) => {683 // TODO: Operate on ref684 let f: IStr = c.mkey.into();685 let width = match c.width {686 Width::Star => {687 throw!(CannotUseStarWidthWithObject);688 }689 Width::Fixed(n) => n,690 };691 let precision = match c.precision {692 Some(Width::Star) => {693 throw!(CannotUseStarWidthWithObject);694 }695 Some(Width::Fixed(n)) => Some(n),696 None => None,697 };698699 let value = if c.convtype == ConvTypeV::Percent {700 Val::Null701 } else {702 if f.is_empty() {703 throw!(MappingKeysRequired);704 }705 if let Some(v) = values.get(f.clone())? {706 v707 } else {708 throw!(NoSuchFormatField(f));709 }710 };711712 format_code(&mut out, &value, &c, width, precision)?;713 }714 }715 }716717 Ok(out)718}719720#[cfg(test)]721pub mod test_format {722 use super::*;723724 #[test]725 fn parse() {726 assert_eq!(727 parse_codes(728 "How much error budget is left looking at our %.3f%% availability gurantees?"729 )730 .unwrap()731 .len(),732 4733 );734 }735736 #[test]737 fn octals() {738 assert_eq!(format_arr("%#o", &[Val::Num(8.0)]).unwrap(), "010");739 assert_eq!(format_arr("%#4o", &[Val::Num(8.0)]).unwrap(), " 010");740 assert_eq!(format_arr("%4o", &[Val::Num(8.0)]).unwrap(), " 10");741 assert_eq!(format_arr("%04o", &[Val::Num(8.0)]).unwrap(), "0010");742 assert_eq!(format_arr("%+4o", &[Val::Num(8.0)]).unwrap(), " +10");743 assert_eq!(format_arr("%+04o", &[Val::Num(8.0)]).unwrap(), "+010");744 assert_eq!(format_arr("%-4o", &[Val::Num(8.0)]).unwrap(), "10 ");745 assert_eq!(format_arr("%+-4o", &[Val::Num(8.0)]).unwrap(), "+10 ");746 assert_eq!(format_arr("%+-04o", &[Val::Num(8.0)]).unwrap(), "+10 ");747 }748749 #[test]750 fn percent_doesnt_consumes_values() {751 assert_eq!(752 format_arr(753 "How much error budget is left looking at our %.3f%% availability gurantees?",754 &[Val::Num(4.0)]755 )756 .unwrap(),757 "How much error budget is left looking at our 4.000% availability gurantees?"758 );759 }760}crates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/manifest.rs
@@ -126,6 +126,7 @@
buf.push('}');
}
Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),
+ Val::DebugGcTraceValue(v) => manifest_json_ex_buf(&v.value, buf, cur_padding, options)?,
};
Ok(())
}
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -1,10 +1,11 @@
use crate::{
equals,
error::{Error::*, Result},
- parse_args, primitive_equals, push, throw, with_state, ArrValue, Context, EvaluationState,
- FuncVal, LazyVal, Val,
+ parse_args, primitive_equals, push, throw, with_state, ArrValue, Context, DebugGcTraceValue,
+ EvaluationState, FuncVal, LazyVal, Val,
};
use format::{format_arr, format_obj};
+use gc::Gc;
use jrsonnet_interner::IStr;
use jrsonnet_parser::{ArgsDesc, BinaryOpType, ExprLocation};
use jrsonnet_types::ty;
@@ -68,6 +69,8 @@
("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),
@@ -301,7 +304,7 @@
parse_args!(context, "native", args, 1, [
0, x: ty!(string) => Val::Str;
], {
- Ok(with_state(|s| s.settings().ext_natives.get(&x).cloned()).map(|v| Val::Func(Rc::new(FuncVal::NativeExt(x.clone(), v)))).ok_or(UndefinedExternalFunction(x))?)
+ 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))?)
})
}
@@ -446,6 +449,28 @@
})
}
+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");
+ 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::new(name, rest))
+ })
+}
+
fn builtin_base64(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
parse_args!(context, "base64", args, 1, [
0, input: ty!((string | (Array<number>)));
crates/jrsonnet-evaluator/src/builtin/sort.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/sort.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/sort.rs
@@ -2,9 +2,9 @@
error::{Error, LocError, Result},
throw, Context, FuncVal, Val,
};
-use std::rc::Rc;
+use gc::{Finalize, Gc, Trace};
-#[derive(Debug, Clone, thiserror::Error)]
+#[derive(Debug, Clone, thiserror::Error, Trace, Finalize)]
pub enum SortError {
#[error("sort key should be string or number")]
SortKeyShouldBeStringOrNumber,
@@ -59,13 +59,13 @@
Ok(sort_type)
}
-pub fn sort(ctx: Context, mut values: Rc<Vec<Val>>, key_getter: &FuncVal) -> Result<Rc<Vec<Val>>> {
+pub fn sort(ctx: Context, values: Gc<Vec<Val>>, key_getter: &FuncVal) -> Result<Gc<Vec<Val>>> {
if values.len() <= 1 {
return Ok(values);
}
if key_getter.is_ident() {
- let mvalues = Rc::make_mut(&mut values);
- let sort_type = get_sort_type(mvalues, |k| k)?;
+ let mut mvalues = (*values).clone();
+ let sort_type = get_sort_type(&mut mvalues, |k| k)?;
match sort_type {
SortKeyType::Number => mvalues.sort_by_key(|v| match v {
Val::Num(n) => NonNaNf64(*n),
@@ -77,7 +77,7 @@
}),
SortKeyType::Unknown => unreachable!(),
};
- Ok(values)
+ Ok(Gc::new(mvalues))
} else {
let mut vk = Vec::with_capacity(values.len());
for value in values.iter() {
@@ -98,6 +98,6 @@
}),
SortKeyType::Unknown => unreachable!(),
};
- Ok(Rc::new(vk.into_iter().map(|v| v.0).collect()))
+ Ok(Gc::new(vk.into_iter().map(|v| v.0).collect()))
}
}
crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -1,13 +1,14 @@
use crate::{
- error::Error::*, map::LayeredHashMap, resolved_lazy_val, FutureWrapper, LazyBinding, LazyVal,
- ObjValue, Result, Val,
+ error::Error::*, map::LayeredHashMap, FutureWrapper, LazyBinding, LazyVal, ObjValue, Result,
+ Val,
};
+use gc::{Finalize, Gc, Trace};
use jrsonnet_interner::IStr;
use rustc_hash::FxHashMap;
+use std::fmt::Debug;
use std::hash::BuildHasherDefault;
-use std::{fmt::Debug, rc::Rc};
-#[derive(Clone)]
+#[derive(Clone, Trace, Finalize)]
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> {
@@ -20,6 +21,7 @@
}
}
+#[derive(Trace, Finalize)]
struct ContextInternals {
dollar: Option<ObjValue>,
this: Option<ObjValue>,
@@ -28,15 +30,12 @@
}
impl Debug for ContextInternals {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- f.debug_struct("Context")
- .field("this", &self.this.as_ref().map(|e| Rc::as_ptr(&e.0)))
- .field("bindings", &self.bindings)
- .finish()
+ f.debug_struct("Context").finish()
}
}
-#[derive(Debug, Clone)]
-pub struct Context(Rc<ContextInternals>);
+#[derive(Debug, Clone, Trace, Finalize)]
+pub struct Context(Gc<ContextInternals>);
impl Context {
pub fn new_future() -> FutureWrapper<Self> {
FutureWrapper::new()
@@ -55,7 +54,7 @@
}
pub fn new() -> Self {
- Self(Rc::new(ContextInternals {
+ Self(Gc::new(ContextInternals {
dollar: None,
this: None,
super_obj: None,
@@ -81,7 +80,7 @@
pub fn with_var(self, name: IStr, value: Val) -> Self {
let mut new_bindings =
FxHashMap::with_capacity_and_hasher(1, BuildHasherDefault::default());
- new_bindings.insert(name, resolved_lazy_val!(value));
+ new_bindings.insert(name, LazyVal::new_resolved(value));
self.extend(new_bindings, None, None, None)
}
@@ -96,40 +95,21 @@
new_this: Option<ObjValue>,
new_super_obj: Option<ObjValue>,
) -> Self {
- match Rc::try_unwrap(self.0) {
- Ok(mut ctx) => {
- // Extended context aren't used by anything else, we can freely mutate it without cloning
- if let Some(dollar) = new_dollar {
- ctx.dollar = Some(dollar);
- }
- if let Some(this) = new_this {
- ctx.this = Some(this);
- }
- if let Some(super_obj) = new_super_obj {
- ctx.super_obj = Some(super_obj);
- }
- if !new_bindings.is_empty() {
- ctx.bindings = ctx.bindings.extend(new_bindings);
- }
- Self(Rc::new(ctx))
- }
- Err(ctx) => {
- let dollar = new_dollar.or_else(|| ctx.dollar.clone());
- let this = new_this.or_else(|| ctx.this.clone());
- let super_obj = new_super_obj.or_else(|| ctx.super_obj.clone());
- let bindings = if new_bindings.is_empty() {
- ctx.bindings.clone()
- } else {
- ctx.bindings.clone().extend(new_bindings)
- };
- Self(Rc::new(ContextInternals {
- dollar,
- this,
- super_obj,
- bindings,
- }))
- }
- }
+ let ctx = &self.0;
+ let dollar = new_dollar.or_else(|| ctx.dollar.clone());
+ let this = new_this.or_else(|| ctx.this.clone());
+ let super_obj = new_super_obj.or_else(|| ctx.super_obj.clone());
+ let bindings = if new_bindings.is_empty() {
+ ctx.bindings.clone()
+ } else {
+ ctx.bindings.clone().extend(new_bindings)
+ };
+ Self(Gc::new(ContextInternals {
+ dollar,
+ this,
+ super_obj,
+ bindings,
+ }))
}
pub fn extend_bound(self, new_bindings: FxHashMap<IStr, LazyVal>) -> Self {
let new_this = self.0.this.clone();
@@ -166,22 +146,6 @@
impl PartialEq for Context {
fn eq(&self, other: &Self) -> bool {
- Rc::ptr_eq(&self.0, &other.0)
- }
-}
-
-#[cfg(feature = "unstable")]
-#[derive(Debug, Clone)]
-pub struct WeakContext(std::rc::Weak<ContextInternals>);
-#[cfg(feature = "unstable")]
-impl WeakContext {
- pub fn upgrade(&self) -> Context {
- Context(self.0.upgrade().expect("context is removed"))
- }
-}
-#[cfg(feature = "unstable")]
-impl PartialEq for WeakContext {
- fn eq(&self, other: &Self) -> bool {
- self.0.ptr_eq(&other.0)
+ Gc::ptr_eq(&self.0, &other.0)
}
}
crates/jrsonnet-evaluator/src/dynamic.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/dynamic.rs
+++ b/crates/jrsonnet-evaluator/src/dynamic.rs
@@ -1,23 +1,23 @@
-use std::{cell::RefCell, rc::Rc};
+use gc::{Finalize, Gc, GcCell, Trace};
-#[derive(Clone)]
-pub struct FutureWrapper<V>(pub Rc<RefCell<Option<V>>>);
-impl<T> FutureWrapper<T> {
+#[derive(Clone, Trace, Finalize)]
+pub struct FutureWrapper<V: Trace + 'static>(pub Gc<GcCell<Option<V>>>);
+impl<T: Trace + 'static> FutureWrapper<T> {
pub fn new() -> Self {
- Self(Rc::new(RefCell::new(None)))
+ Self(Gc::new(GcCell::new(None)))
}
pub fn fill(self, value: T) {
assert!(self.0.borrow().is_none(), "wrapper is filled already");
self.0.borrow_mut().replace(value);
}
}
-impl<T: Clone> FutureWrapper<T> {
+impl<T: Clone + Trace + 'static> FutureWrapper<T> {
pub fn unwrap(&self) -> T {
self.0.borrow().as_ref().cloned().unwrap()
}
}
-impl<T> Default for FutureWrapper<T> {
+impl<T: Trace + 'static> Default for FutureWrapper<T> {
fn default() -> Self {
Self::new()
}
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -2,13 +2,14 @@
builtin::{format::FormatError, sort::SortError},
typed::TypeLocError,
};
+use gc::{Finalize, Trace};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};
use jrsonnet_types::ValType;
use std::{path::PathBuf, rc::Rc};
use thiserror::Error;
-#[derive(Error, Debug, Clone)]
+#[derive(Error, Debug, Clone, Trace, Finalize)]
pub enum Error {
#[error("intrinsic not found: {0}")]
IntrinsicNotFound(IStr),
@@ -88,6 +89,7 @@
ImportSyntaxError {
path: Rc<PathBuf>,
source_code: IStr,
+ #[unsafe_ignore_trace]
error: Box<jrsonnet_parser::ParseError>,
},
@@ -95,6 +97,8 @@
RuntimeError(IStr),
#[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]
StackOverflow,
+ #[error("infinite recursion detected")]
+ RecursiveLazyValueEvaluation,
#[error("tried to index by fractional value")]
FractionalIndex,
#[error("attempted to divide by zero")]
@@ -142,15 +146,15 @@
}
}
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, Trace, Finalize)]
pub struct StackTraceElement {
pub location: Option<ExprLocation>,
pub desc: String,
}
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Trace, Finalize)]
pub struct StackTrace(pub Vec<StackTraceElement>);
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Trace, Finalize)]
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
@@ -1,8 +1,9 @@
use crate::{
- equals, error::Error::*, lazy_val, push, throw, with_state, ArrValue, Context, ContextCreator,
- FuncDesc, FuncVal, FutureWrapper, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val,
+ equals, error::Error::*, push, throw, with_state, ArrValue, Bindable, Context, ContextCreator,
+ FuncDesc, FuncVal, FutureWrapper, LazyBinding, LazyVal, LazyValValue, ObjMember, ObjValue,
+ ObjectAssertion, Result, Val,
};
-use closure::closure;
+use gc::{custom_trace, Finalize, Gc, Trace};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{
ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, ExprLocation, FieldMember,
@@ -20,17 +21,62 @@
let b = b.clone();
if let Some(params) = &b.params {
let params = params.clone();
- LazyVal::new(Box::new(move || {
- Ok(evaluate_method(
- context_creator.unwrap(),
- b.name.clone(),
- params.clone(),
- b.value.clone(),
- ))
+
+ 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(
+ self.context_creator.unwrap(),
+ self.name,
+ self.params,
+ self.value,
+ ))
+ }
+ }
+
+ LazyVal::new(Box::new(LazyMethodBinding {
+ context_creator,
+ name: b.name.clone(),
+ params,
+ value: b.value.clone(),
}))
} else {
- LazyVal::new(Box::new(move || {
- evaluate_named(context_creator.unwrap(), &b.value, b.name.clone())
+ 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)
+ }
+ }
+ LazyVal::new(Box::new(LazyNamedBinding {
+ context_creator,
+ name: b.name.clone(),
+ value: b.value,
}))
}
}
@@ -39,37 +85,129 @@
let b = b.clone();
if let Some(params) = &b.params {
let params = params.clone();
+
+ struct BindableMethodLazyVal {
+ this: Option<ObjValue>,
+ super_obj: Option<ObjValue>,
+
+ context_creator: ContextCreator,
+ 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> {
+ Ok(evaluate_method(
+ self.context_creator.create(self.this, self.super_obj)?,
+ self.name,
+ self.params,
+ self.value,
+ ))
+ }
+ }
+
+ #[derive(Trace, Finalize)]
+ struct BindableMethod {
+ context_creator: ContextCreator,
+ name: IStr,
+ params: ParamsDesc,
+ value: LocExpr,
+ }
+ impl Bindable for BindableMethod {
+ fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {
+ Ok(LazyVal::new(Box::new(BindableMethodLazyVal {
+ this: this.clone(),
+ super_obj: super_obj.clone(),
+
+ context_creator: self.context_creator.clone(),
+ name: self.name.clone(),
+ params: self.params.clone(),
+ value: self.value.clone(),
+ })))
+ }
+ }
+
(
b.name.clone(),
- LazyBinding::Bindable(Rc::new(move |this, super_obj| {
- Ok(lazy_val!(
- closure!(clone b, clone params, clone context_creator, || Ok(evaluate_method(
- context_creator.create(this.clone(), super_obj.clone())?,
- b.name.clone(),
- params.clone(),
- b.value.clone(),
- )))
- ))
- })),
+ LazyBinding::Bindable(Gc::new(Box::new(BindableMethod {
+ context_creator,
+ name: b.name.clone(),
+ params,
+ value: b.value.clone(),
+ }))),
)
} else {
+ struct BindableNamedLazyVal {
+ this: Option<ObjValue>,
+ super_obj: Option<ObjValue>,
+
+ context_creator: ContextCreator,
+ 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(
+ self.context_creator.create(self.this, self.super_obj)?,
+ &self.value,
+ self.name,
+ )
+ }
+ }
+
+ #[derive(Trace, Finalize)]
+ struct BindableNamed {
+ context_creator: ContextCreator,
+ name: IStr,
+ value: LocExpr,
+ }
+ impl Bindable for BindableNamed {
+ fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {
+ Ok(LazyVal::new(Box::new(BindableNamedLazyVal {
+ this,
+ super_obj,
+
+ context_creator: self.context_creator.clone(),
+ name: self.name.clone(),
+ value: self.value.clone(),
+ })))
+ }
+ }
+
(
b.name.clone(),
- LazyBinding::Bindable(Rc::new(move |this, super_obj| {
- Ok(lazy_val!(closure!(clone context_creator, clone b, ||
- evaluate_named(
- context_creator.create(this.clone(), super_obj.clone())?,
- &b.value,
- b.name.clone()
- )
- )))
- })),
+ LazyBinding::Bindable(Gc::new(Box::new(BindableNamed {
+ context_creator,
+ name: b.name.clone(),
+ value: b.value.clone(),
+ }))),
)
}
}
pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {
- Val::Func(Rc::new(FuncVal::Normal(FuncDesc {
+ Val::Func(Gc::new(FuncVal::Normal(FuncDesc {
name,
ctx,
params,
@@ -105,6 +243,9 @@
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)
@@ -257,7 +398,7 @@
}
let mut new_members = FxHashMap::default();
- let mut assertions = Vec::new();
+ let mut assertions: Vec<Box<dyn ObjectAssertion>> = Vec::new();
for member in members.iter() {
match member {
Member::Field(FieldMember {
@@ -272,20 +413,36 @@
continue;
}
let name = name.unwrap();
+
+ #[derive(Trace, Finalize)]
+ struct ObjMemberBinding {
+ context_creator: ContextCreator,
+ value: LocExpr,
+ name: IStr,
+ }
+ impl Bindable for ObjMemberBinding {
+ fn bind(
+ &self,
+ this: Option<ObjValue>,
+ super_obj: Option<ObjValue>,
+ ) -> Result<LazyVal> {
+ Ok(LazyVal::new_resolved(evaluate_named(
+ self.context_creator.create(this, super_obj)?,
+ &self.value,
+ self.name.clone(),
+ )?))
+ }
+ }
new_members.insert(
name.clone(),
ObjMember {
add: *plus,
visibility: *visibility,
- invoke: LazyBinding::Bindable(Rc::new(
- closure!(clone name, clone value, clone context_creator, |this, super_obj| {
- Ok(LazyVal::new_resolved(evaluate_named(
- context_creator.create(this, super_obj)?,
- &value,
- name.clone(),
- )?))
- }),
- )),
+ invoke: LazyBinding::Bindable(Gc::new(Box::new(ObjMemberBinding {
+ context_creator: context_creator.clone(),
+ value: value.clone(),
+ name,
+ }))),
location: value.1.clone(),
},
);
@@ -301,33 +458,73 @@
continue;
}
let name = name.unwrap();
+ #[derive(Trace, Finalize)]
+ struct ObjMemberBinding {
+ context_creator: ContextCreator,
+ value: LocExpr,
+ params: ParamsDesc,
+ name: IStr,
+ }
+ impl Bindable for ObjMemberBinding {
+ fn bind(
+ &self,
+ this: Option<ObjValue>,
+ super_obj: Option<ObjValue>,
+ ) -> Result<LazyVal> {
+ Ok(LazyVal::new_resolved(evaluate_method(
+ self.context_creator.create(this, super_obj)?,
+ self.name.clone(),
+ self.params.clone(),
+ self.value.clone(),
+ )))
+ }
+ }
new_members.insert(
name.clone(),
ObjMember {
add: false,
visibility: Visibility::Hidden,
- invoke: LazyBinding::Bindable(Rc::new(
- closure!(clone value, clone context_creator, clone params, clone name, |this, super_obj| {
- // TODO: Assert
- Ok(LazyVal::new_resolved(evaluate_method(
- context_creator.create(this, super_obj)?,
- name.clone(),
- params.clone(),
- value.clone(),
- )))
- }),
- )),
+ invoke: LazyBinding::Bindable(Gc::new(Box::new(ObjMemberBinding {
+ context_creator: context_creator.clone(),
+ value: value.clone(),
+ params: params.clone(),
+ name,
+ }))),
location: value.1.clone(),
},
);
}
Member::BindStmt(_) => {}
Member::AssertStmt(stmt) => {
- assertions.push(stmt.clone());
+ 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(
+ &self,
+ this: Option<ObjValue>,
+ super_obj: Option<ObjValue>,
+ ) -> Result<()> {
+ let ctx = self.context_creator.create(this, super_obj)?;
+ evaluate_assert(ctx, &self.assert)
+ }
+ }
+ assertions.push(Box::new(ObjectAssert {
+ context_creator: context_creator.clone(),
+ assert: stmt.clone(),
+ }));
}
}
}
- let this = ObjValue::new(context, None, Rc::new(new_members), Rc::new(assertions));
+ let this = ObjValue::new(None, Gc::new(new_members), Gc::new(assertions));
future_this.fill(this.clone());
Ok(this)
}
@@ -361,16 +558,37 @@
match key {
Val::Null => {}
Val::Str(n) => {
+ #[derive(Trace, Finalize)]
+ struct ObjCompBinding {
+ context: Context,
+ value: LocExpr,
+ }
+ impl Bindable for ObjCompBinding {
+ fn bind(
+ &self,
+ this: Option<ObjValue>,
+ _super_obj: Option<ObjValue>,
+ ) -> Result<LazyVal> {
+ Ok(LazyVal::new_resolved(evaluate(
+ self.context.clone().extend(
+ FxHashMap::default(),
+ None,
+ this,
+ None,
+ ),
+ &self.value,
+ )?))
+ }
+ }
new_members.insert(
n,
ObjMember {
add: false,
visibility: Visibility::Normal,
- invoke: LazyBinding::Bindable(Rc::new(
- closure!(clone ctx, clone obj.value, |this, _super_obj| {
- Ok(LazyVal::new_resolved(evaluate(ctx.clone().extend(FxHashMap::default(), None, this, None), &value)?))
- }),
- )),
+ invoke: LazyBinding::Bindable(Gc::new(Box::new(ObjCompBinding {
+ context: ctx.clone(),
+ value: obj.value.clone(),
+ }))),
location: obj.value.1.clone(),
},
);
@@ -381,7 +599,7 @@
Ok(())
})?;
- let this = ObjValue::new(context, None, Rc::new(new_members), Rc::new(Vec::new()));
+ let this = ObjValue::new(None, Gc::new(new_members), Gc::new(Vec::new()));
future_this.fill(this.clone());
this
}
@@ -486,7 +704,7 @@
if let Some(v) = v.get(s.clone())? {
Ok(v)
} else if v.get("__intrinsic_namespace__".into())?.is_some() {
- Ok(Val::Func(Rc::new(FuncVal::Intrinsic(s))))
+ Ok(Val::Func(Gc::new(FuncVal::Intrinsic(s))))
} else {
throw!(NoSuchField(s))
}
@@ -549,11 +767,27 @@
Arr(items) => {
let mut out = Vec::with_capacity(items.len());
for item in items {
- out.push(LazyVal::new(Box::new(
- closure!(clone context, clone item, || {
- evaluate(context.clone(), &item)
- }),
- )));
+ // TODO: Implement ArrValue::Lazy with same context for every element?
+ 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> {
+ evaluate(self.context, &self.item)
+ }
+ }
+ out.push(LazyVal::new(Box::new(ArrayElement {
+ context: context.clone(),
+ item: item.clone(),
+ })));
}
Val::Arr(out.into())
}
@@ -563,7 +797,7 @@
out.push(evaluate(ctx, expr)?);
Ok(())
})?;
- Val::Arr(ArrValue::Eager(Rc::new(out)))
+ Val::Arr(ArrValue::Eager(Gc::new(out)))
}
Obj(body) => Val::Obj(evaluate_object(context, body)?),
ObjExtend(s, t) => evaluate_add_op(
@@ -576,7 +810,7 @@
Function(params, body) => {
evaluate_method(context, "anonymous".into(), params.clone(), body.clone())
}
- Intrinsic(name) => Val::Func(Rc::new(FuncVal::Intrinsic(name.clone()))),
+ Intrinsic(name) => Val::Func(Gc::new(FuncVal::Intrinsic(name.clone()))),
AssertExpr(assert, returned) => {
evaluate_assert(context.clone(), assert)?;
evaluate(context, returned)?
crates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function.rs
+++ b/crates/jrsonnet-evaluator/src/function.rs
@@ -1,7 +1,7 @@
-use crate::{error::Error::*, evaluate, lazy_val, resolved_lazy_val, throw, Context, Result, Val};
-use closure::closure;
+use crate::{error::Error::*, evaluate, throw, Context, LazyVal, LazyValValue, Result, Val};
+use gc::{custom_trace, Finalize, Trace};
use jrsonnet_interner::IStr;
-use jrsonnet_parser::{ArgsDesc, ParamsDesc};
+use jrsonnet_parser::{ArgsDesc, LocExpr, ParamsDesc};
use rustc_hash::FxHashMap;
use std::{collections::HashMap, hash::BuildHasherDefault};
@@ -53,9 +53,29 @@
throw!(FunctionParameterNotBoundInCall(p.0.clone()));
};
let val = if tailstrict {
- resolved_lazy_val!(evaluate(ctx, expr)?)
+ LazyVal::new_resolved(evaluate(ctx, expr)?)
} else {
- lazy_val!(closure!(clone ctx, clone expr, ||evaluate(ctx.clone(), &expr)))
+ 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> {
+ evaluate(self.context, &self.expr)
+ }
+ }
+
+ LazyVal::new(Box::new(EvaluateLazyVal {
+ context: ctx.clone(),
+ expr: expr.clone(),
+ }))
};
out.insert(p.0.clone(), val);
}
@@ -89,19 +109,30 @@
// Fill defaults
for (id, p) in params.iter().enumerate() {
let val = if let Some(arg) = positioned_args[id].take() {
- resolved_lazy_val!(arg)
+ LazyVal::new_resolved(arg)
} else if let Some(default) = &p.1 {
if tailstrict {
- resolved_lazy_val!(evaluate(
+ LazyVal::new_resolved(evaluate(
body_ctx.clone().expect(NO_DEFAULT_CONTEXT),
- default
+ default,
)?)
} else {
let body_ctx = body_ctx.clone();
let default = default.clone();
- lazy_val!(move || {
- evaluate(body_ctx.clone().expect(NO_DEFAULT_CONTEXT), &default)
- })
+ #[derive(Trace, Finalize)]
+ struct EvaluateLazyVal {
+ body_ctx: Option<Context>,
+ default: LocExpr,
+ }
+ impl LazyValValue for EvaluateLazyVal {
+ fn get(self: Box<Self>) -> Result<Val> {
+ evaluate(
+ self.body_ctx.clone().expect(NO_DEFAULT_CONTEXT),
+ &self.default,
+ )
+ }
+ }
+ LazyVal::new(Box::new(EvaluateLazyVal { body_ctx, default }))
}
} else {
throw!(FunctionParameterNotBoundInCall(p.0.clone()));
@@ -135,7 +166,7 @@
} else {
throw!(FunctionParameterNotBoundInCall(p.0.clone()));
};
- out.insert(p.0.clone(), resolved_lazy_val!(val));
+ out.insert(p.0.clone(), LazyVal::new_resolved(val));
}
Ok(body_ctx.unwrap_or(ctx).extend(out, None, None, None))
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -2,6 +2,7 @@
error::{Error::*, LocError, Result},
throw, Context, LazyBinding, LazyVal, ObjMember, ObjValue, Val,
};
+use gc::Gc;
use jrsonnet_parser::Visibility;
use rustc_hash::FxHasher;
use serde_json::{Map, Number, Value};
@@ -9,7 +10,6 @@
collections::HashMap,
convert::{TryFrom, TryInto},
hash::BuildHasherDefault,
- rc::Rc,
};
impl TryFrom<&Val> for Value {
@@ -42,6 +42,7 @@
Self::Object(out)
}
Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),
+ Val::DebugGcTraceValue(v) => Value::try_from(&*v.value as &Val)?,
})
}
}
@@ -76,12 +77,7 @@
},
);
}
- Self::Obj(ObjValue::new(
- Context::new(),
- None,
- Rc::new(entries),
- Rc::new(Vec::new()),
- ))
+ Self::Obj(ObjValue::new(None, Gc::new(entries), Gc::new(Vec::new())))
}
}
}
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -25,6 +25,7 @@
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_interner::IStr;
use jrsonnet_parser::*;
@@ -42,10 +43,12 @@
use trace::{offset_to_location, CodeLocation, CompactFormat, TraceFormat};
pub use val::*;
-type BindableFn = dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>;
-#[derive(Clone)]
+pub trait Bindable: Trace {
+ fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal>;
+}
+#[derive(Trace, Finalize, Clone)]
pub enum LazyBinding {
- Bindable(Rc<BindableFn>),
+ Bindable(Gc<Box<dyn Bindable>>),
Bound(LazyVal),
}
@@ -57,7 +60,7 @@
impl LazyBinding {
pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {
match self {
- Self::Bindable(v) => v(this, super_obj),
+ Self::Bindable(v) => v.bind(this, super_obj),
Self::Bound(v) => Ok(v.clone()),
}
}
@@ -71,7 +74,7 @@
/// Used for s`td.extVar`
pub ext_vars: HashMap<IStr, Val>,
/// Used for ext.native
- pub ext_natives: HashMap<IStr, Rc<NativeCallback>>,
+ pub ext_natives: HashMap<IStr, Gc<NativeCallback>>,
/// TLA vars
pub tla_vars: HashMap<IStr, Val>,
/// Global variables are inserted in default context
@@ -270,7 +273,7 @@
let mut new_bindings: FxHashMap<IStr, LazyVal> =
FxHashMap::with_capacity_and_hasher(globals.len(), BuildHasherDefault::default());
for (name, value) in globals.iter() {
- new_bindings.insert(name.clone(), resolved_lazy_val!(value.clone()));
+ new_bindings.insert(name.clone(), LazyVal::new_resolved(value.clone()));
}
Context::new().extend_bound(new_bindings)
}
@@ -449,7 +452,7 @@
self.settings_mut().import_resolver = resolver;
}
- pub fn add_native(&self, name: IStr, cb: Rc<NativeCallback>) {
+ pub fn add_native(&self, name: IStr, cb: Gc<NativeCallback>) {
self.settings_mut().ext_natives.insert(name, cb);
}
crates/jrsonnet-evaluator/src/map.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/map.rs
+++ b/crates/jrsonnet-evaluator/src/map.rs
@@ -1,28 +1,29 @@
+use gc::{Finalize, Gc, Trace};
use jrsonnet_interner::IStr;
use rustc_hash::FxHashMap;
-use std::rc::Rc;
-#[derive(Default, Debug)]
-struct LayeredHashMapInternals<V> {
+pub struct LayeredHashMapInternals<V: Trace + Finalize + 'static> {
parent: Option<LayeredHashMap<V>>,
current: FxHashMap<IStr, V>,
}
-#[derive(Debug)]
-pub struct LayeredHashMap<V>(Rc<LayeredHashMapInternals<V>>);
+unsafe impl<V: Trace + Finalize + 'static> Trace for LayeredHashMapInternals<V> {
+ gc::custom_trace!(this, {
+ mark(&this.parent);
+ mark(&this.current);
+ });
+}
+impl<V: Trace + Finalize + 'static> Finalize for LayeredHashMapInternals<V> {}
+
+#[derive(Trace, Finalize)]
+pub struct LayeredHashMap<V: Trace + Finalize + 'static>(Gc<LayeredHashMapInternals<V>>);
-impl<V> LayeredHashMap<V> {
+impl<V: Trace + 'static> LayeredHashMap<V> {
pub fn extend(self, new_layer: FxHashMap<IStr, V>) -> Self {
- match Rc::try_unwrap(self.0) {
- Ok(mut map) => {
- map.current.extend(new_layer);
- Self(Rc::new(map))
- }
- Err(this) => Self(Rc::new(LayeredHashMapInternals {
- parent: Some(Self(this)),
- current: new_layer,
- })),
- }
+ Self(Gc::new(LayeredHashMapInternals {
+ parent: Some(self),
+ current: new_layer,
+ }))
}
pub fn get(&self, key: &IStr) -> Option<&V> {
@@ -33,15 +34,15 @@
}
}
-impl<V> Clone for LayeredHashMap<V> {
+impl<V: Trace + 'static> Clone for LayeredHashMap<V> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
-impl<V> Default for LayeredHashMap<V> {
+impl<V: Trace + 'static> Default for LayeredHashMap<V> {
fn default() -> Self {
- Self(Rc::new(LayeredHashMapInternals {
+ Self(Gc::new(LayeredHashMapInternals {
parent: None,
current: FxHashMap::default(),
}))
crates/jrsonnet-evaluator/src/native.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/native.rs
+++ b/crates/jrsonnet-evaluator/src/native.rs
@@ -1,27 +1,27 @@
#![allow(clippy::type_complexity)]
use crate::{error::Result, Val};
+use gc::{Finalize, Trace};
use jrsonnet_parser::ParamsDesc;
use std::fmt::Debug;
use std::path::PathBuf;
use std::rc::Rc;
+pub trait NativeCallbackHandler: Trace {
+ fn call(&self, from: Option<Rc<PathBuf>>, args: &[Val]) -> Result<Val>;
+}
+
+#[derive(Trace, Finalize)]
pub struct NativeCallback {
pub params: ParamsDesc,
- handler: Box<dyn Fn(Option<Rc<PathBuf>>, &[Val]) -> Result<Val>>,
+ handler: Box<dyn NativeCallbackHandler>,
}
impl NativeCallback {
- pub fn new(
- params: ParamsDesc,
- handler: impl Fn(Option<Rc<PathBuf>>, &[Val]) -> Result<Val> + 'static,
- ) -> Self {
- Self {
- params,
- handler: Box::new(handler),
- }
+ pub fn new(params: ParamsDesc, handler: Box<dyn NativeCallbackHandler>) -> Self {
+ Self { params, handler }
}
pub fn call(&self, caller: Option<Rc<PathBuf>>, args: &[Val]) -> Result<Val> {
- (self.handler)(caller, args)
+ self.handler.call(caller, args)
}
}
impl Debug for NativeCallback {
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -1,11 +1,12 @@
use crate::{evaluate_add_op, evaluate_assert, Context, LazyBinding, Result, Val};
+use gc::{Finalize, Gc, GcCell, Trace};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{AssertStmt, ExprLocation, Visibility};
use rustc_hash::{FxHashMap, FxHashSet};
use std::hash::{Hash, Hasher};
-use std::{cell::RefCell, fmt::Debug, hash::BuildHasherDefault, rc::Rc};
+use std::{fmt::Debug, hash::BuildHasherDefault};
-#[derive(Debug)]
+#[derive(Debug, Trace, Finalize)]
pub struct ObjMember {
pub add: bool,
pub visibility: Visibility,
@@ -13,21 +14,24 @@
pub location: Option<ExprLocation>,
}
+pub trait ObjectAssertion: Trace {
+ fn run(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<()>;
+}
+
// Field => This
type CacheKey = (IStr, ObjValue);
-#[derive(Debug)]
+#[derive(Trace, Finalize)]
pub struct ObjValueInternals {
- context: Context,
super_obj: Option<ObjValue>,
- assertions: Rc<Vec<AssertStmt>>,
- assertions_ran: RefCell<FxHashSet<ObjValue>>,
+ assertions: Gc<Vec<Box<dyn ObjectAssertion>>>,
+ assertions_ran: GcCell<FxHashSet<ObjValue>>,
this_obj: Option<ObjValue>,
- this_entries: Rc<FxHashMap<IStr, ObjMember>>,
- value_cache: RefCell<FxHashMap<CacheKey, Option<Val>>>,
+ this_entries: Gc<FxHashMap<IStr, ObjMember>>,
+ value_cache: GcCell<FxHashMap<CacheKey, Option<Val>>>,
}
-#[derive(Clone)]
-pub struct ObjValue(pub(crate) Rc<ObjValueInternals>);
+#[derive(Clone, Trace, Finalize)]
+pub struct ObjValue(pub(crate) Gc<ObjValueInternals>);
impl Debug for ObjValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(super_obj) = self.0.super_obj.as_ref() {
@@ -55,39 +59,30 @@
impl ObjValue {
pub fn new(
- context: Context,
super_obj: Option<Self>,
- this_entries: Rc<FxHashMap<IStr, ObjMember>>,
- assertions: Rc<Vec<AssertStmt>>,
+ this_entries: Gc<FxHashMap<IStr, ObjMember>>,
+ assertions: Gc<Vec<Box<dyn ObjectAssertion>>>,
) -> Self {
- Self(Rc::new(ObjValueInternals {
- context,
+ Self(Gc::new(ObjValueInternals {
super_obj,
assertions,
- assertions_ran: RefCell::new(FxHashSet::default()),
+ assertions_ran: GcCell::new(FxHashSet::default()),
this_obj: None,
this_entries,
- value_cache: RefCell::new(FxHashMap::default()),
+ value_cache: GcCell::new(FxHashMap::default()),
}))
}
pub fn new_empty() -> Self {
- Self::new(
- Context::new(),
- None,
- Rc::new(FxHashMap::default()),
- Rc::new(Vec::new()),
- )
+ Self::new(None, Gc::new(FxHashMap::default()), Gc::new(Vec::new()))
}
pub fn extend_from(&self, super_obj: Self) -> Self {
match &self.0.super_obj {
None => Self::new(
- self.0.context.clone(),
Some(super_obj),
self.0.this_entries.clone(),
self.0.assertions.clone(),
),
Some(v) => Self::new(
- self.0.context.clone(),
Some(v.extend_from(super_obj)),
self.0.this_entries.clone(),
self.0.assertions.clone(),
@@ -95,14 +90,13 @@
}
}
pub fn with_this(&self, this_obj: Self) -> Self {
- Self(Rc::new(ObjValueInternals {
- context: self.0.context.clone(),
+ Self(Gc::new(ObjValueInternals {
super_obj: self.0.super_obj.clone(),
assertions: self.0.assertions.clone(),
- assertions_ran: RefCell::new(FxHashSet::default()),
+ assertions_ran: GcCell::new(FxHashSet::default()),
this_obj: Some(this_obj),
this_entries: self.0.this_entries.clone(),
- value_cache: RefCell::new(FxHashMap::default()),
+ value_cache: GcCell::new(FxHashMap::default()),
}))
}
@@ -203,12 +197,7 @@
pub fn extend_with_field(self, key: IStr, value: ObjMember) -> Self {
let mut new = FxHashMap::with_capacity_and_hasher(1, BuildHasherDefault::default());
new.insert(key, value);
- Self::new(
- Context::new(),
- Some(self),
- Rc::new(new),
- Rc::new(Vec::new()),
- )
+ Self::new(Some(self), Gc::new(new), Gc::new(Vec::new()))
}
fn get_raw(&self, key: IStr, real_this: Option<&Self>) -> Result<Option<Val>> {
@@ -249,13 +238,7 @@
fn run_assertions_raw(&self, real_this: &Self) -> Result<()> {
if self.0.assertions_ran.borrow_mut().insert(real_this.clone()) {
for assertion in self.0.assertions.iter() {
- if let Err(e) = evaluate_assert(
- self.0
- .context
- .clone()
- .with_this_super(real_this.clone(), self.0.super_obj.clone()),
- assertion,
- ) {
+ if let Err(e) = assertion.run(Some(real_this.clone()), self.0.super_obj.clone()) {
self.0.assertions_ran.borrow_mut().remove(real_this);
return Err(e);
}
@@ -271,19 +254,19 @@
}
pub fn ptr_eq(a: &Self, b: &Self) -> bool {
- Rc::ptr_eq(&a.0, &b.0)
+ Gc::ptr_eq(&a.0, &b.0)
}
}
impl PartialEq for ObjValue {
fn eq(&self, other: &Self) -> bool {
- Rc::ptr_eq(&self.0, &other.0)
+ Gc::ptr_eq(&self.0, &other.0)
}
}
impl Eq for ObjValue {}
impl Hash for ObjValue {
- fn hash<H: Hasher>(&self, state: &mut H) {
- state.write_usize(Rc::as_ptr(&self.0) as usize)
+ fn hash<H: Hasher>(&self, hasher: &mut H) {
+ hasher.write_usize(&*self.0 as *const _ as usize)
}
}
crates/jrsonnet-evaluator/src/typed.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed.rs
+++ b/crates/jrsonnet-evaluator/src/typed.rs
@@ -4,6 +4,7 @@
error::{Error, LocError, Result},
push, Val,
};
+use gc::{Finalize, Trace};
use jrsonnet_parser::ExprLocation;
use jrsonnet_types::{ComplexValType, ValType};
use thiserror::Error;
@@ -20,7 +21,7 @@
}};
}
-#[derive(Debug, Error, Clone)]
+#[derive(Debug, Error, Clone, Trace, Finalize)]
pub enum TypeError {
#[error("expected {0}, got {1}")]
ExpectedGot(ComplexValType, ValType),
@@ -37,7 +38,7 @@
}
}
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Trace, Finalize)]
pub struct TypeLocError(Box<TypeError>, ValuePathStack);
impl From<TypeError> for TypeLocError {
fn from(e: TypeError) -> Self {
@@ -59,7 +60,7 @@
}
}
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Trace, Finalize)]
pub struct TypeLocErrorList(Vec<TypeLocError>);
impl Display for TypeLocErrorList {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -122,7 +123,7 @@
}
}
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, Trace, Finalize)]
enum ValuePathItem {
Field(Rc<str>),
Index(u64),
@@ -137,7 +138,7 @@
}
}
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, Trace, Finalize)]
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
@@ -3,52 +3,75 @@
call_builtin,
manifest::{manifest_json_ex, ManifestJsonOptions, ManifestType},
},
- error::Error::*,
+ error::{Error::*, LocError},
evaluate,
function::{parse_function_call, parse_function_call_map, place_args},
native::NativeCallback,
throw, with_state, Context, ObjValue, Result,
};
+use gc::{custom_trace, Finalize, Gc, GcCell, Trace};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{el, Arg, ArgsDesc, Expr, ExprLocation, LiteralType, LocExpr, ParamsDesc};
use jrsonnet_types::ValType;
-use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
+use std::{collections::HashMap, fmt::Debug, rc::Rc};
+pub trait LazyValValue: Trace {
+ fn get(self: Box<Self>) -> Result<Val>;
+}
+
enum LazyValInternals {
Computed(Val),
- Waiting(Box<dyn Fn() -> Result<Val>>),
+ Errored(LocError),
+ Waiting(Box<dyn LazyValValue>),
+ Pending,
}
-#[derive(Clone)]
-pub struct LazyVal(Rc<RefCell<LazyValInternals>>);
+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)]
+pub struct LazyVal(Gc<GcCell<LazyValInternals>>);
impl LazyVal {
- pub fn new(f: Box<dyn Fn() -> Result<Val>>) -> Self {
- Self(Rc::new(RefCell::new(LazyValInternals::Waiting(f))))
+ pub fn new(f: Box<dyn LazyValValue>) -> Self {
+ Self(Gc::new(GcCell::new(LazyValInternals::Waiting(f))))
}
pub fn new_resolved(val: Val) -> Self {
- Self(Rc::new(RefCell::new(LazyValInternals::Computed(val))))
+ Self(Gc::new(GcCell::new(LazyValInternals::Computed(val))))
}
pub fn evaluate(&self) -> Result<Val> {
- let new_value = match &*self.0.borrow() {
+ match &*self.0.borrow() {
LazyValInternals::Computed(v) => return Ok(v.clone()),
- LazyValInternals::Waiting(f) => f()?,
+ LazyValInternals::Errored(e) => return Err(e.clone().into()),
+ LazyValInternals::Pending => return Err(RecursiveLazyValueEvaluation.into()),
+ _ => (),
+ };
+ let value = if let LazyValInternals::Waiting(value) =
+ std::mem::replace(&mut *self.0.borrow_mut(), LazyValInternals::Pending)
+ {
+ value
+ } else {
+ unreachable!()
};
+ let new_value = match value.get() {
+ Ok(v) => v,
+ Err(e) => {
+ *self.0.borrow_mut() = LazyValInternals::Errored(e.clone());
+ return Err(e);
+ }
+ };
*self.0.borrow_mut() = LazyValInternals::Computed(new_value.clone());
Ok(new_value)
}
}
-#[macro_export]
-macro_rules! lazy_val {
- ($f: expr) => {
- $crate::LazyVal::new(Box::new($f))
- };
-}
-#[macro_export]
-macro_rules! resolved_lazy_val {
- ($f: expr) => {
- $crate::LazyVal::new_resolved($f)
- };
-}
impl Debug for LazyVal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Lazy")
@@ -56,11 +79,11 @@
}
impl PartialEq for LazyVal {
fn eq(&self, other: &Self) -> bool {
- Rc::ptr_eq(&self.0, &other.0)
+ Gc::ptr_eq(&self.0, &other.0)
}
}
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Trace, Finalize)]
pub struct FuncDesc {
pub name: IStr,
pub ctx: Context,
@@ -68,14 +91,14 @@
pub body: LocExpr,
}
-#[derive(Debug)]
+#[derive(Debug, Trace, Finalize)]
pub enum FuncVal {
/// Plain function implemented in jsonnet
Normal(FuncDesc),
/// Standard library function
Intrinsic(IStr),
/// Library functions implemented in native
- NativeExt(IStr, Rc<NativeCallback>),
+ NativeExt(IStr, Gc<NativeCallback>),
}
impl PartialEq for FuncVal {
@@ -174,13 +197,23 @@
#[derive(Debug, Clone)]
pub enum ArrValue {
- Lazy(Rc<Vec<LazyVal>>),
- Eager(Rc<Vec<Val>>),
+ 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(Rc::new(Vec::new()))
+ Self::Eager(Gc::new(Vec::new()))
}
pub fn len(&self) -> usize {
@@ -231,14 +264,14 @@
}
}
- pub fn evaluated(&self) -> Result<Rc<Vec<Val>>> {
+ pub fn evaluated(&self) -> Result<Gc<Vec<Val>>> {
Ok(match self {
Self::Lazy(vec) => {
let mut out = Vec::with_capacity(vec.len());
for item in vec.iter() {
out.push(item.evaluate()?);
}
- Rc::new(out)
+ Gc::new(out)
}
Self::Eager(vec) => vec.clone(),
Self::Extended(_v) => {
@@ -246,7 +279,7 @@
for item in self.iter() {
out.push(item?);
}
- Rc::new(out)
+ Gc::new(out)
}
})
}
@@ -272,12 +305,12 @@
Self::Lazy(vec) => {
let mut out = (&vec as &Vec<_>).clone();
out.reverse();
- Self::Lazy(Rc::new(out))
+ Self::Lazy(Gc::new(out))
}
Self::Eager(vec) => {
let mut out = (&vec as &Vec<_>).clone();
out.reverse();
- Self::Eager(Rc::new(out))
+ Self::Eager(Gc::new(out))
}
Self::Extended(b) => Self::Extended(Box::new((b.1.reversed(), b.0.reversed()))),
}
@@ -290,7 +323,7 @@
out.push(mapper(value?)?);
}
- Ok(Self::Eager(Rc::new(out)))
+ Ok(Self::Eager(Gc::new(out)))
}
pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {
@@ -303,13 +336,13 @@
}
}
- Ok(Self::Eager(Rc::new(out)))
+ Ok(Self::Eager(Gc::new(out)))
}
pub fn ptr_eq(a: &Self, b: &Self) -> bool {
match (a, b) {
- (Self::Lazy(a), Self::Lazy(b)) => Rc::ptr_eq(a, b),
- (Self::Eager(a), Self::Eager(b)) => Rc::ptr_eq(a, b),
+ (Self::Lazy(a), Self::Lazy(b)) => Gc::ptr_eq(a, b),
+ (Self::Eager(a), Self::Eager(b)) => Gc::ptr_eq(a, b),
_ => false,
}
}
@@ -317,13 +350,72 @@
impl From<Vec<LazyVal>> for ArrValue {
fn from(v: Vec<LazyVal>) -> Self {
- Self::Lazy(Rc::new(v))
+ Self::Lazy(Gc::new(v))
}
}
impl From<Vec<Val>> for ArrValue {
fn from(v: Vec<Val>) -> Self {
- Self::Eager(Rc::new(v))
+ Self::Eager(Gc::new(v))
+ }
+}
+
+#[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 = DebugGcTraceValue {
+ name: self.name.clone(),
+ value: self.value.clone(),
+ };
+ value.print("I'm clone");
+ value
+ }
+}
+impl DebugGcTraceValue {
+ pub fn new(name: IStr, value: Val) -> Val {
+ let value = Self {
+ name,
+ value: Box::new(value),
+ };
+ value.print("Constructed");
+ Val::DebugGcTraceValue(value)
}
}
@@ -335,7 +427,23 @@
Num(f64),
Arr(ArrValue),
Obj(ObjValue),
- Func(Rc<FuncVal>),
+ 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 {
@@ -368,7 +476,7 @@
pub fn unwrap_num(self) -> Result<f64> {
Ok(matches_unwrap!(self, Self::Num(v), v))
}
- pub fn unwrap_func(self) -> Result<Rc<FuncVal>> {
+ pub fn unwrap_func(self) -> Result<Gc<FuncVal>> {
Ok(matches_unwrap!(self, Self::Func(v), v))
}
pub fn try_cast_bool(self, context: &'static str) -> Result<bool> {
@@ -392,6 +500,7 @@
Self::Bool(_) => ValType::Bool,
Self::Null => ValType::Null,
Self::Func(..) => ValType::Func,
+ Self::DebugGcTraceValue(v) => v.value.value_type(),
}
}