difftreelog
refactor move push_frame out of State struct
in: master
14 files changed
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -16,10 +16,11 @@
error::{suggest_object_fields, ErrorKind::*},
evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
function::{CallLocation, FuncDesc, FuncVal},
+ in_frame,
typed::Typed,
val::{CachedUnbound, IndexableVal, NumValue, StrValue, Thunk, ThunkValue},
Context, Error, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result,
- ResultExt, State, Unbound, Val,
+ ResultExt, Unbound, Val,
};
pub mod destructure;
pub mod operator;
@@ -71,7 +72,7 @@
pub fn evaluate_field_name(ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {
Ok(match field_name {
FieldName::Fixed(n) => Some(n.clone()),
- FieldName::Dyn(expr) => State::push(
+ FieldName::Dyn(expr) => in_frame(
CallLocation::new(&expr.span()),
|| "evaluating field name".to_string(),
|| {
@@ -374,7 +375,7 @@
if tailstrict {
body()?
} else {
- State::push(loc, || format!("function <{}> call", f.name()), body)?
+ in_frame(loc, || format!("function <{}> call", f.name()), body)?
}
}
v => bail!(OnlyFunctionsCanBeCalledGot(v.value_type())),
@@ -384,13 +385,13 @@
pub fn evaluate_assert(ctx: Context, assertion: &AssertStmt) -> Result<()> {
let value = &assertion.0;
let msg = &assertion.1;
- let assertion_result = State::push(
+ let assertion_result = in_frame(
CallLocation::new(&value.span()),
|| "assertion condition".to_owned(),
|| bool::from_untyped(evaluate(ctx.clone(), value)?),
)?;
if !assertion_result {
- State::push(
+ in_frame(
CallLocation::new(&value.span()),
|| "assertion failure".to_owned(),
|| {
@@ -457,7 +458,7 @@
}
BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,
UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,
- Var(name) => State::push(
+ Var(name) => in_frame(
CallLocation::new(&loc),
|| format!("variable <{name}> access"),
|| ctx.binding(name.clone())?.evaluate(),
@@ -645,7 +646,7 @@
evaluate_assert(ctx.clone(), assert)?;
evaluate(ctx, returned)?
}
- ErrorStmt(e) => State::push(
+ ErrorStmt(e) => in_frame(
CallLocation::new(&loc),
|| "error statement".to_owned(),
|| bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),
@@ -655,7 +656,7 @@
cond_then,
cond_else,
} => {
- if State::push(
+ if in_frame(
CallLocation::new(&loc),
|| "if condition".to_owned(),
|| bool::from_untyped(evaluate(ctx.clone(), &cond.0)?),
@@ -676,7 +677,7 @@
desc: &'static str,
) -> Result<Option<T>> {
if let Some(value) = expr {
- Ok(Some(State::push(
+ Ok(Some(in_frame(
loc,
|| format!("slice {desc}"),
|| T::from_untyped(evaluate(ctx.clone(), value)?),
@@ -703,7 +704,7 @@
let s = ctx.state();
let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;
match i {
- Import(_) => State::push(
+ Import(_) => in_frame(
CallLocation::new(&loc),
|| format!("import {:?}", path.clone()),
|| s.import_resolved(resolved_path),
crates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -1,6 +1,5 @@
use std::{
any::Any,
- cell::RefCell,
env::current_dir,
fs,
io::{ErrorKind, Read},
@@ -41,8 +40,10 @@
/// this cannot be resolved using associated type, as evaluator uses object instead of generic for [`ImportResolver`]
fn load_file_contents(&self, resolved: &SourcePath) -> Result<Vec<u8>>;
- /// For downcasts
+ // For downcasts, will be removed after trait_upcasting_coercion
+ // stabilization.
fn as_any(&self) -> &dyn Any;
+ fn as_any_mut(&mut self) -> &mut dyn Any;
}
/// Dummy resolver, can't resolve/load any file
@@ -56,6 +57,9 @@
fn as_any(&self) -> &dyn Any {
self
}
+ fn as_any_mut(&mut self) -> &mut dyn Any {
+ self
+ }
}
#[allow(clippy::use_self)]
impl Default for Box<dyn ImportResolver> {
@@ -69,17 +73,15 @@
pub struct FileImportResolver {
/// Library directories to search for file.
/// Referred to as `jpath` in original jsonnet implementation.
- library_paths: RefCell<Vec<PathBuf>>,
+ library_paths: Vec<PathBuf>,
}
impl FileImportResolver {
- pub fn new(jpath: Vec<PathBuf>) -> Self {
- Self {
- library_paths: RefCell::new(jpath),
- }
+ pub fn new(library_paths: Vec<PathBuf>) -> Self {
+ Self { library_paths }
}
/// Dynamically add new jpath, used by bindings
- pub fn add_jpath(&self, path: PathBuf) {
- self.library_paths.borrow_mut().push(path);
+ pub fn add_jpath(&mut self, path: PathBuf) {
+ self.library_paths.push(path);
}
}
@@ -132,7 +134,7 @@
if let Some(direct) = check_path(&direct)? {
return Ok(direct);
}
- for library_path in self.library_paths.borrow().iter() {
+ for library_path in &self.library_paths {
let mut cloned = library_path.clone();
cloned.push(path);
if let Some(cloned) = check_path(&cloned)? {
@@ -165,11 +167,15 @@
Ok(out)
}
+ fn resolve_from_default(&self, path: &str) -> Result<SourcePath> {
+ self.resolve_from(&SourcePath::default(), path)
+ }
+
fn as_any(&self) -> &dyn Any {
self
}
- fn resolve_from_default(&self, path: &str) -> Result<SourcePath> {
- self.resolve_from(&SourcePath::default(), path)
+ fn as_any_mut(&mut self) -> &mut dyn Any {
+ self
}
}
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -11,8 +11,8 @@
};
use crate::{
- arr::ArrValue, runtime_error, val::NumValue, Error as JrError, ObjValue, ObjValueBuilder,
- Result, State, Val,
+ arr::ArrValue, in_description_frame, runtime_error, val::NumValue, Error as JrError, ObjValue,
+ ObjValueBuilder, Result, Val,
};
impl<'de> Deserialize<'de> for Val {
@@ -173,8 +173,7 @@
let mut seq = serializer.serialize_seq(Some(arr.len()))?;
for (i, element) in arr.iter().enumerate() {
let mut serde_error = None;
- // TODO: rewrite using try{} after stabilization
- State::push_description(
+ in_description_frame(
|| format!("array index [{i}]"),
|| {
let e = element?;
@@ -199,7 +198,7 @@
) {
let mut serde_error = None;
// TODO: rewrite using try{} after stabilization
- State::push_description(
+ in_description_frame(
|| format!("object field {field:?}"),
|| {
let v = value?;
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -45,7 +45,7 @@
#[doc(hidden)]
pub use jrsonnet_macros;
pub use jrsonnet_parser as parser;
-use jrsonnet_parser::{LocExpr, ParserSettings, Source, SourcePath, Span};
+use jrsonnet_parser::{LocExpr, ParserSettings, Source, SourcePath};
pub use obj::*;
use stack::check_depth;
pub use tla::apply_tla;
@@ -376,38 +376,6 @@
context_initializer.populate(source, &mut builder);
builder.build()
- }
-
- /// Executes code creating a new stack frame
- pub fn push<T>(
- e: CallLocation<'_>,
- frame_desc: impl FnOnce() -> String,
- f: impl FnOnce() -> Result<T>,
- ) -> Result<T> {
- let _guard = check_depth()?;
-
- f().with_description_src(e, frame_desc)
- }
-
- /// Executes code creating a new stack frame
- pub fn push_val(
- &self,
- e: &Span,
- frame_desc: impl FnOnce() -> String,
- f: impl FnOnce() -> Result<Val>,
- ) -> Result<Val> {
- let _guard = check_depth()?;
-
- f().with_description_src(e, frame_desc)
- }
- /// Executes code creating a new stack frame
- pub fn push_description<T>(
- frame_desc: impl FnOnce() -> String,
- f: impl FnOnce() -> Result<T>,
- ) -> Result<T> {
- let _guard = check_depth()?;
-
- f().with_description(frame_desc)
}
}
@@ -417,6 +385,26 @@
self.0.file_cache.borrow_mut()
}
}
+/// Executes code creating a new stack frame, to be replaced with try{}
+pub fn in_frame<T>(
+ e: CallLocation<'_>,
+ frame_desc: impl FnOnce() -> String,
+ f: impl FnOnce() -> Result<T>,
+) -> Result<T> {
+ let _guard = check_depth()?;
+
+ f().with_description_src(e, frame_desc)
+}
+
+/// Executes code creating a new stack frame, to be replaced with try{}
+pub fn in_description_frame<T>(
+ frame_desc: impl FnOnce() -> String,
+ f: impl FnOnce() -> Result<T>,
+) -> Result<T> {
+ let _guard = check_depth()?;
+
+ f().with_description(frame_desc)
+}
#[derive(Trace)]
pub struct InitialUnderscore(pub Thunk<Val>);
crates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/manifest.rs
@@ -1,6 +1,6 @@
use std::{borrow::Cow, fmt::Write, ptr};
-use crate::{bail, Result, ResultExt, State, Val};
+use crate::{bail, in_description_frame, Result, ResultExt, Val};
pub trait ManifestFormat {
fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()>;
@@ -242,7 +242,7 @@
Minify | ToString => {}
};
- State::push_description(
+ in_description_frame(
|| format!("elem <{i}> manifestification"),
|| manifest_json_ex_buf(&item, buf, cur_padding, options),
)?;
@@ -304,7 +304,7 @@
escape_string_json_buf(&key, buf);
buf.push_str(options.key_val_sep);
- State::push_description(
+ in_description_frame(
|| format!("field <{key}> manifestification"),
|| manifest_json_ex_buf(&value, buf, cur_padding, options),
)?;
@@ -412,7 +412,7 @@
for (i, v) in arr.iter().enumerate() {
let v = v.with_description(|| format!("elem <{i}> evaluation"))?;
out.push_str("---\n");
- State::push_description(
+ in_description_frame(
|| format!("elem <{i}> manifestification"),
|| self.inner.manifest_buf(v, out),
)?;
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -17,10 +17,11 @@
error::{suggest_object_fields, Error, ErrorKind::*},
function::{CallLocation, FuncVal},
gc::{GcHashMap, GcHashSet, TraceBox},
+ in_frame,
operator::evaluate_add_op,
tb,
val::{ArrValue, ThunkValue},
- MaybeUnbound, Result, State, Thunk, Unbound, Val,
+ MaybeUnbound, Result, Thunk, Unbound, Val,
};
#[cfg(not(feature = "exp-preserve-order"))]
@@ -969,7 +970,7 @@
let location = member.location.clone();
let old = receiver.0.map.insert(name.clone(), member);
if old.is_some() {
- State::push(
+ in_frame(
CallLocation(location.as_ref()),
|| format!("field <{}> initializtion", name.clone()),
|| bail!(DuplicateFieldName(name.clone())),
crates/jrsonnet-evaluator/src/stdlib/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/mod.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/mod.rs
@@ -3,12 +3,12 @@
use format::{format_arr, format_obj};
-use crate::{function::CallLocation, Result, State, Val};
+use crate::{function::CallLocation, in_frame, Result, Val};
pub mod format;
pub fn std_format(str: &str, vals: Val) -> Result<String> {
- State::push(
+ in_frame(
CallLocation::native(),
|| format!("std.format of {str}"),
|| {
crates/jrsonnet-evaluator/src/tla.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/tla.rs
+++ b/crates/jrsonnet-evaluator/src/tla.rs
@@ -3,12 +3,12 @@
use crate::{
function::{ArgsLike, CallLocation},
- Result, State, Val,
+ in_description_frame, Result, State, Val,
};
pub fn apply_tla<A: ArgsLike>(s: State, args: &A, val: Val) -> Result<Val> {
Ok(if let Val::Func(func) = val {
- State::push_description(
+ in_description_frame(
|| "during TLA call".to_owned(),
|| {
func.evaluate(
crates/jrsonnet-evaluator/src/typed/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/mod.rs
+++ b/crates/jrsonnet-evaluator/src/typed/mod.rs
@@ -8,7 +8,7 @@
use crate::{
error::{Error, ErrorKind, Result},
- State, Val,
+ in_description_frame, Val,
};
#[derive(Debug, Error, Clone, Trace)]
@@ -89,7 +89,7 @@
path: impl Fn() -> ValuePathItem,
item: impl Fn() -> Result<()>,
) -> Result<()> {
- State::push_description(error_reason, || match item() {
+ in_description_frame(error_reason, || match item() {
Ok(()) => Ok(()),
Err(mut e) => {
if let ErrorKind::TypeError(e) = &mut e.error_mut() {
crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth1#![deny(2 unsafe_op_in_unsafe_fn,3 clippy::missing_safety_doc,4 clippy::undocumented_unsafe_blocks5)]6#![warn(clippy::pedantic, clippy::nursery)]7#![allow(clippy::missing_const_for_fn)]8use std::{9 borrow::Cow,10 cell::RefCell,11 fmt::{self, Display},12 hash::{BuildHasherDefault, Hash, Hasher},13 ops::Deref,14 str,15};1617use hashbrown::{hash_map::RawEntryMut, HashMap};18use jrsonnet_gcmodule::Trace;19use rustc_hash::FxHasher;2021mod inner;22use inner::Inner;2324/// Interned string25///26/// Provides O(1) comparsions and hashing, cheap copy, and cheap conversion to [`IBytes`]27#[derive(Clone, PartialOrd, Ord, Eq)]28pub struct IStr(Inner);29impl Trace for IStr {30 fn is_type_tracked() -> bool {31 false32 }33}3435impl IStr {36 #[must_use]37 pub fn empty() -> Self {38 "".into()39 }40 #[must_use]41 pub fn as_str(&self) -> &str {42 self as &str43 }4445 #[must_use]46 pub fn cast_bytes(self) -> IBytes {47 IBytes(self.0.clone())48 }49}5051impl Deref for IStr {52 type Target = str;5354 fn deref(&self) -> &Self::Target {55 // SAFETY: Inner::check_utf8 is called on IStr construction, data is utf-856 unsafe { self.0.as_str_unchecked() }57 }58}5960impl PartialEq for IStr {61 fn eq(&self, other: &Self) -> bool {62 // all IStr should be inlined into same pool63 Inner::ptr_eq(&self.0, &other.0)64 }65}6667impl PartialEq<str> for IStr {68 fn eq(&self, other: &str) -> bool {69 self as &str == other70 }71}7273impl Hash for IStr {74 fn hash<H: Hasher>(&self, state: &mut H) {75 // IStr is always obtained from pool, where no string have duplicate, thus every unique string has unique address76 state.write_usize(Inner::as_ptr(&self.0).cast::<()>() as usize);77 }78}7980impl Drop for IStr {81 fn drop(&mut self) {82 maybe_unpool(&self.0);83 }84}8586impl fmt::Debug for IStr {87 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {88 fmt::Debug::fmt(self as &str, f)89 }90}9192impl Display for IStr {93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {94 fmt::Display::fmt(self as &str, f)95 }96}9798/// Interned byte array99#[derive(Clone, PartialOrd, Ord, Eq)]100pub struct IBytes(Inner);101impl Trace for IBytes {102 fn is_type_tracked() -> bool {103 false104 }105}106107impl IBytes {108 #[must_use]109 pub fn cast_str(self) -> Option<IStr> {110 if Inner::check_utf8(&self.0) {111 Some(IStr(self.0.clone()))112 } else {113 None114 }115 }116 /// # Safety117 /// data should be valid utf8118 unsafe fn cast_str_unchecked(self) -> IStr {119 // SAFETY: data is utf8120 unsafe { Inner::assume_utf8(&self.0) };121 IStr(self.0.clone())122 }123124 #[must_use]125 pub fn as_slice(&self) -> &[u8] {126 self.0.as_slice()127 }128}129130impl Deref for IBytes {131 type Target = [u8];132133 fn deref(&self) -> &Self::Target {134 self.0.as_slice()135 }136}137138impl PartialEq for IBytes {139 fn eq(&self, other: &Self) -> bool {140 // all IStr should be inlined into same pool141 Inner::ptr_eq(&self.0, &other.0)142 }143}144145impl Hash for IBytes {146 fn hash<H: Hasher>(&self, state: &mut H) {147 // IBytes is always obtained from pool, where no string have duplicate, thus every unique string has unique address148 state.write_usize(Inner::as_ptr(&self.0).cast::<()>() as usize);149 }150}151152impl Drop for IBytes {153 fn drop(&mut self) {154 maybe_unpool(&self.0);155 }156}157158fn maybe_unpool(inner: &Inner) {159 #[cold]160 #[inline(never)]161 fn unpool(inner: &Inner) {162 // May fail on program termination163 let _ = POOL.try_with(|pool| {164 let mut pool = pool.borrow_mut();165166 if pool.remove(inner).is_none() {167 // On some platforms (i.e i686-windows), try_with will not fail after TLS168 // destructor is called, but instead re-initialize the TLS with the empty pool.169 // Allow non-pooled Drop in this case.170 // https://github.com/CertainLach/jrsonnet/issues/98#issuecomment-1591624016171 //172 // However, if pool is not empty, most likely this is issue #113, and then I don't173 // have any explainations for now.174 assert!(pool.is_empty(), "received an unpooled string not during the program termination, please write any info regarding this crash to https://github.com/CertainLach/jrsonnet/issues/113, thanks!");175 }176 });177 }178 // First reference - current object, second - POOL179 if Inner::strong_count(inner) <= 2 {180 unpool(inner);181 }182}183184impl fmt::Debug for IBytes {185 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {186 fmt::Debug::fmt(self as &[u8], f)187 }188}189190impl<'c> From<Cow<'c, str>> for IStr {191 fn from(v: Cow<'c, str>) -> Self {192 intern_str(&v)193 }194}195impl From<&str> for IStr {196 fn from(v: &str) -> Self {197 intern_str(v)198 }199}200impl From<String> for IStr {201 fn from(s: String) -> Self {202 s.as_str().into()203 }204}205impl From<&String> for IStr {206 fn from(s: &String) -> Self {207 s.as_str().into()208 }209}210impl From<char> for IStr {211 fn from(value: char) -> Self {212 let mut buf = [0; 5];213 Self::from(&*value.encode_utf8(&mut buf))214 }215}216impl From<&[u8]> for IBytes {217 fn from(v: &[u8]) -> Self {218 intern_bytes(v)219 }220}221222type PoolMap = HashMap<Inner, (), BuildHasherDefault<FxHasher>>;223224thread_local! {225 static POOL: RefCell<PoolMap> = RefCell::new(HashMap::with_capacity_and_hasher(200, BuildHasherDefault::default()));226}227228/// Jrsonnet golang bindings require that it is possible to move jsonnet229/// VM between OS threads, and this is not possible due to usage of230/// `thread_local`. Instead, there is two methods added, one should be231/// called at the end of current thread work, and one that should be232/// used when using other thread.233pub mod interop {234 use std::mem;235236 use crate::{PoolMap, POOL};237238 pub enum PoolState {}239240 /// Dump current interned string pool, to be restored by241 /// `reenter_thread`242 pub fn exit_thread() -> *mut PoolState {243 Box::into_raw(Box::new(POOL.with_borrow_mut(mem::take))).cast()244 }245246 /// Reenter thread, using state dumped by `exit_thread`.247 ///248 /// # Safety249 ///250 /// `state` should be acquired from `exit_thread`, it is not allowed251 /// to reuse state to reenter multiple threads.252 pub unsafe fn reenter_thread(state: *mut PoolState) {253 let ptr: *mut PoolMap = state.cast();254 // SAFETY: ptr is an unique state per method safety requirements.255 let ptr: Box<PoolMap> = unsafe { Box::from_raw(ptr) };256 let ptr: PoolMap = *ptr;257 POOL.with_borrow_mut(|pool| {258 let _ = mem::replace(pool, ptr);259 });260 }261}262263#[must_use]264pub fn intern_bytes(bytes: &[u8]) -> IBytes {265 POOL.with(|pool| {266 let mut pool = pool.borrow_mut();267 let entry = pool.raw_entry_mut().from_key(bytes);268 match entry {269 RawEntryMut::Occupied(i) => IBytes(i.get_key_value().0.clone()),270 RawEntryMut::Vacant(e) => {271 let (k, ()) = e.insert(Inner::new_bytes(bytes), ());272 IBytes(k.clone())273 }274 }275 })276}277278#[must_use]279pub fn intern_str(str: &str) -> IStr {280 // SAFETY: Rust strings always utf8281 unsafe { intern_bytes(str.as_bytes()).cast_str_unchecked() }282}283284#[cfg(test)]285mod tests {286 use crate::IStr;287288 #[test]289 fn simple() {290 let a = IStr::from("a");291 let b = IStr::from("a");292293 assert_eq!(a.as_ptr(), b.as_ptr());294 }295}1#![deny(2 unsafe_op_in_unsafe_fn,3 clippy::missing_safety_doc,4 clippy::undocumented_unsafe_blocks5)]6#![warn(clippy::pedantic, clippy::nursery)]7#![allow(clippy::missing_const_for_fn)]8use std::{9 borrow::Cow,10 cell::RefCell,11 fmt::{self, Display},12 hash::{BuildHasherDefault, Hash, Hasher},13 ops::Deref,14 str,15};1617use hashbrown::{hash_map::RawEntryMut, HashMap};18use jrsonnet_gcmodule::Trace;19use rustc_hash::FxHasher;2021mod inner;22use inner::Inner;2324/// Interned string25///26/// Provides O(1) comparsions and hashing, cheap copy, and cheap conversion to [`IBytes`]27#[derive(Clone, PartialOrd, Ord, Eq)]28pub struct IStr(Inner);29impl Trace for IStr {30 fn is_type_tracked() -> bool {31 false32 }33}3435impl IStr {36 #[must_use]37 pub fn empty() -> Self {38 "".into()39 }40 #[must_use]41 pub fn as_str(&self) -> &str {42 self as &str43 }4445 #[must_use]46 pub fn cast_bytes(self) -> IBytes {47 IBytes(self.0.clone())48 }49}5051impl Deref for IStr {52 type Target = str;5354 fn deref(&self) -> &Self::Target {55 // SAFETY: Inner::check_utf8 is called on IStr construction, data is utf-856 unsafe { self.0.as_str_unchecked() }57 }58}5960impl PartialEq for IStr {61 fn eq(&self, other: &Self) -> bool {62 // all IStr should be inlined into same pool63 Inner::ptr_eq(&self.0, &other.0)64 }65}6667impl PartialEq<str> for IStr {68 fn eq(&self, other: &str) -> bool {69 self as &str == other70 }71}7273impl Hash for IStr {74 fn hash<H: Hasher>(&self, state: &mut H) {75 // IStr is always obtained from pool, where no string have duplicate, thus every unique string has unique address76 state.write_usize(Inner::as_ptr(&self.0).cast::<()>() as usize);77 }78}7980impl Drop for IStr {81 fn drop(&mut self) {82 maybe_unpool(&self.0);83 }84}8586impl fmt::Debug for IStr {87 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {88 fmt::Debug::fmt(self as &str, f)89 }90}9192impl Display for IStr {93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {94 fmt::Display::fmt(self as &str, f)95 }96}9798/// Interned byte array99#[derive(Clone, PartialOrd, Ord, Eq)]100pub struct IBytes(Inner);101impl Trace for IBytes {102 fn is_type_tracked() -> bool {103 false104 }105}106107impl IBytes {108 #[must_use]109 pub fn cast_str(self) -> Option<IStr> {110 if Inner::check_utf8(&self.0) {111 Some(IStr(self.0.clone()))112 } else {113 None114 }115 }116 /// # Safety117 /// data should be valid utf8118 unsafe fn cast_str_unchecked(self) -> IStr {119 // SAFETY: data is utf8120 unsafe { Inner::assume_utf8(&self.0) };121 IStr(self.0.clone())122 }123124 #[must_use]125 pub fn as_slice(&self) -> &[u8] {126 self.0.as_slice()127 }128}129130impl Deref for IBytes {131 type Target = [u8];132133 fn deref(&self) -> &Self::Target {134 self.0.as_slice()135 }136}137138impl PartialEq for IBytes {139 fn eq(&self, other: &Self) -> bool {140 // all IStr should be inlined into same pool141 Inner::ptr_eq(&self.0, &other.0)142 }143}144145impl Hash for IBytes {146 fn hash<H: Hasher>(&self, state: &mut H) {147 // IBytes is always obtained from pool, where no string have duplicate, thus every unique string has unique address148 state.write_usize(Inner::as_ptr(&self.0).cast::<()>() as usize);149 }150}151152impl Drop for IBytes {153 fn drop(&mut self) {154 maybe_unpool(&self.0);155 }156}157158fn maybe_unpool(inner: &Inner) {159 #[cold]160 #[inline(never)]161 fn unpool(inner: &Inner) {162 // May fail on program termination163 let _ = POOL.try_with(|pool| {164 let mut pool = pool.borrow_mut();165166 if pool.remove(inner).is_none() {167 // On some platforms (i.e i686-windows), try_with will not fail after TLS168 // destructor is called, but instead re-initialize the TLS with the empty pool.169 // Allow non-pooled Drop in this case.170 // https://github.com/CertainLach/jrsonnet/issues/98#issuecomment-1591624016171 //172 // However, if pool is not empty, most likely this is issue #113, and then I don't173 // have any explainations for now.174 assert!(pool.is_empty(), "received an unpooled string not during the program termination, please write any info regarding this crash to https://github.com/CertainLach/jrsonnet/issues/113, thanks!");175 }176 });177 }178 // First reference - current object, second - POOL179 if Inner::strong_count(inner) <= 2 {180 unpool(inner);181 }182}183184impl fmt::Debug for IBytes {185 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {186 fmt::Debug::fmt(self as &[u8], f)187 }188}189190impl<'c> From<Cow<'c, str>> for IStr {191 fn from(v: Cow<'c, str>) -> Self {192 intern_str(&v)193 }194}195impl From<&str> for IStr {196 fn from(v: &str) -> Self {197 intern_str(v)198 }199}200impl From<String> for IStr {201 fn from(s: String) -> Self {202 s.as_str().into()203 }204}205impl From<&String> for IStr {206 fn from(s: &String) -> Self {207 s.as_str().into()208 }209}210impl From<char> for IStr {211 fn from(value: char) -> Self {212 let mut buf = [0; 5];213 Self::from(&*value.encode_utf8(&mut buf))214 }215}216impl From<&[u8]> for IBytes {217 fn from(v: &[u8]) -> Self {218 intern_bytes(v)219 }220}221222type PoolMap = HashMap<Inner, (), BuildHasherDefault<FxHasher>>;223224thread_local! {225 static POOL: RefCell<PoolMap> = RefCell::new(HashMap::with_capacity_and_hasher(200, BuildHasherDefault::default()));226}227228/// Jrsonnet golang bindings require that it is possible to move jsonnet229/// VM between OS threads, and this is not possible due to usage of230/// `thread_local`. Instead, there is two methods added, one should be231/// called at the end of current thread work, and one that should be232/// used when using other thread.233pub mod interop {234 use std::mem;235236 use crate::{PoolMap, POOL};237238 /// Type-erased interned string pool239 pub enum PoolState {}240241 /// Dump current interned string pool, to be restored by242 /// `reenter_thread`243 pub fn exit_thread() -> *mut PoolState {244 Box::into_raw(Box::new(POOL.with_borrow_mut(mem::take))).cast()245 }246247 /// Reenter thread, using state dumped by `exit_thread`.248 ///249 /// # Safety250 ///251 /// `state` should be acquired from `exit_thread`, it is not allowed252 /// to reuse state to reenter multiple threads.253 pub unsafe fn reenter_thread(state: *mut PoolState) {254 let ptr: *mut PoolMap = state.cast();255 // SAFETY: ptr is an unique state per method safety requirements.256 let ptr: Box<PoolMap> = unsafe { Box::from_raw(ptr) };257 let ptr: PoolMap = *ptr;258 POOL.with_borrow_mut(|pool| {259 let _ = mem::replace(pool, ptr);260 });261 }262}263264#[must_use]265pub fn intern_bytes(bytes: &[u8]) -> IBytes {266 POOL.with(|pool| {267 let mut pool = pool.borrow_mut();268 let entry = pool.raw_entry_mut().from_key(bytes);269 match entry {270 RawEntryMut::Occupied(i) => IBytes(i.get_key_value().0.clone()),271 RawEntryMut::Vacant(e) => {272 let (k, ()) = e.insert(Inner::new_bytes(bytes), ());273 IBytes(k.clone())274 }275 }276 })277}278279#[must_use]280pub fn intern_str(str: &str) -> IStr {281 // SAFETY: Rust strings always utf8282 unsafe { intern_bytes(str.as_bytes()).cast_str_unchecked() }283}284285#[cfg(test)]286mod tests {287 use crate::IStr;288289 #[test]290 fn simple() {291 let a = IStr::from("a");292 let b = IStr::from("a");293294 assert_eq!(a.as_ptr(), b.as_ptr());295 }296}crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -290,7 +290,7 @@
cfg_attrs,
} => {
let name = name.as_ref().map_or("<unnamed>", String::as_str);
- let eval = quote! {jrsonnet_evaluator::State::push_description(
+ let eval = quote! {jrsonnet_evaluator::in_description_frame(
|| format!("argument <{}> evaluation", #name),
|| <#ty>::from_untyped(value.evaluate()?),
)?};
crates/jrsonnet-stdlib/src/manifest/toml.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/manifest/toml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/toml.rs
@@ -1,10 +1,10 @@
use std::borrow::Cow;
use jrsonnet_evaluator::{
- bail,
+ bail, in_description_frame,
manifest::{escape_string_json_buf, ManifestFormat},
val::ArrValue,
- IStr, ObjValue, Result, ResultExt, State, Val,
+ IStr, ObjValue, Result, ResultExt, Val,
};
pub struct TomlFormat<'s> {
@@ -124,7 +124,7 @@
buf.push_str(&options.padding);
}
- State::push_description(
+ in_description_frame(
|| format!("elem <{i}> manifestification"),
|| manifest_value(&e, true, buf, "", options),
)?;
@@ -161,7 +161,7 @@
escape_key_toml_buf(&k, buf);
buf.push_str(" = ");
- State::push_description(
+ in_description_frame(
|| format!("field <{k}> manifestification"),
|| manifest_value(&v, true, buf, "", options),
)?;
crates/jrsonnet-stdlib/src/manifest/xml.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/manifest/xml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/xml.rs
@@ -1,9 +1,9 @@
use jrsonnet_evaluator::{
- bail,
+ bail, in_description_frame,
manifest::{ManifestFormat, ToStringFormat},
typed::{ComplexValType, Either2, Typed, ValType},
val::ArrValue,
- Either, ObjValue, Result, ResultExt, State, Val,
+ Either, ObjValue, Result, ResultExt, Val,
};
pub struct XmlJsonmlFormat {
@@ -70,7 +70,7 @@
Ok(Self::Tag {
tag,
attrs,
- children: State::push_description(
+ children: in_description_frame(
|| "parsing children".to_owned(),
|| {
Typed::from_untyped(Val::Arr(arr.slice(
crates/jrsonnet-stdlib/src/manifest/yaml.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/manifest/yaml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/yaml.rs
@@ -1,9 +1,9 @@
use std::{borrow::Cow, fmt::Write};
use jrsonnet_evaluator::{
- bail,
+ bail, in_description_frame,
manifest::{escape_string_json_buf, ManifestFormat},
- Result, ResultExt, State, Val,
+ Result, ResultExt, Val,
};
pub struct YamlFormat<'s> {
@@ -178,7 +178,7 @@
if extra_padding {
cur_padding.push_str(&options.padding);
}
- State::push_description(
+ in_description_frame(
|| format!("elem <{i}> manifestification"),
|| manifest_yaml_ex_buf(&item, buf, cur_padding, options),
)?;
@@ -225,7 +225,7 @@
}
_ => buf.push(' '),
}
- State::push_description(
+ in_description_frame(
|| format!("field <{key}> manifestification"),
|| manifest_yaml_ex_buf(&value, buf, cur_padding, options),
)?;