Skip to content

Commit

Permalink
refactor: in-progress preparsed calls
Browse files Browse the repository at this point in the history
  • Loading branch information
CertainLach committed Nov 24, 2024
1 parent 4a7a964 commit ed0455a
Show file tree
Hide file tree
Showing 36 changed files with 1,801 additions and 892 deletions.
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ There already are multiple implementations of this standard implemented in diffe
* [NEW] https://github.com/eduardosm/rsjsonnet[Another Rust impl appears].

This implementation shows performance better than all existing implementations.
For more information see link:./docs/benchmarks.md[benchmarks]
For more information see link:./docs/benchmarks.adoc[benchmarks]

Also, I wanted to experiment on new syntax features, and jrsonnet implements some of them.
For more information see link:./docs/features.adoc[features]
Expand Down
6 changes: 2 additions & 4 deletions bindings/jsonnet/src/vars_tlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::{ffi::CStr, os::raw::c_char};

use jrsonnet_evaluator::{function::TlaArg, IStr};
use jrsonnet_parser::{ParserSettings, Source};
use jrsonnet_parser::Source;

use crate::VM;

Expand Down Expand Up @@ -87,9 +87,7 @@ pub unsafe extern "C" fn jsonnet_tla_code(vm: &mut VM, name: *const c_char, code
let code: IStr = code.to_str().expect("code is not utf-8").into();
let code = jrsonnet_parser::parse(
&code,
&ParserSettings {
source: Source::new_virtual(format!("<top-level-arg:{name}>").into(), code.clone()),
},
Source::new_virtual(format!("<top-level-arg:{name}>").into(), code.clone()),
)
.expect("can't parse TLA code");

Expand Down
46 changes: 9 additions & 37 deletions cmds/jrsonnet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ enum SubOpts {
},
}

#[derive(Parser)]
#[clap(next_help_heading = "DEBUG")]
struct DebugOpts {
/// Required OS stack size.
/// This shouldn't be changed unless jrsonnet is failing with stack overflow error.
#[clap(long, name = "size")]
pub os_stack: Option<usize>,
}

#[derive(Parser)]
#[clap(next_help_heading = "INPUT")]
struct InputOpts {
Expand Down Expand Up @@ -83,8 +74,6 @@ struct Opts {
manifest: ManifestOpts,
#[clap(flatten)]
output: OutputOpts,
#[clap(flatten)]
debug: DebugOpts,
}

// TODO: Add unix_sigpipe = "sig_dfl"
Expand All @@ -108,17 +97,15 @@ fn main() {
}
}

let success = if let Some(size) = opts.debug.os_stack {
std::thread::Builder::new()
.stack_size(size * 1024 * 1024)
.spawn(|| main_catch(opts))
.expect("new thread spawned")
.join()
.expect("thread finished successfully")
} else {
main_catch(opts)
};
if !success {
let trace = opts.trace.trace_format();
if let Err(e) = main_real(opts) {
if let Error::Evaluation(e) = e {
let mut out = String::new();
trace.write_trace(&mut out, &e).expect("format error");
eprintln!("{out}");
} else {
eprintln!("{e}");
}
std::process::exit(1);
}
}
Expand Down Expand Up @@ -146,21 +133,6 @@ impl From<ErrorKind> for Error {
}
}

fn main_catch(opts: Opts) -> bool {
let trace = opts.trace.trace_format();
if let Err(e) = main_real(opts) {
if let Error::Evaluation(e) = e {
let mut out = String::new();
trace.write_trace(&mut out, &e).expect("format error");
eprintln!("{out}");
} else {
eprintln!("{e}");
}
return false;
}
true
}

fn main_real(opts: Opts) -> Result<(), Error> {
let _gc_leak_guard = opts.gc.leak_on_exit();
let _gc_print_stats = opts.gc.stats_printer();
Expand Down
22 changes: 10 additions & 12 deletions crates/jrsonnet-cli/src/tla.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::rc::Rc;

use clap::Parser;
use jrsonnet_evaluator::{
error::{ErrorKind, Result},
function::TlaArg,
gc::GcHashMap,
IStr,
};
use jrsonnet_parser::{ParserSettings, Source};
use jrsonnet_parser::Source;

use crate::{ExtFile, ExtStr};

Expand Down Expand Up @@ -51,18 +53,14 @@ impl TlaOpts {
let source = Source::new_virtual(format!("<top-level-arg:{name}>").into(), code.into());
out.insert(
(name as &str).into(),
TlaArg::Code(
jrsonnet_parser::parse(
code,
&ParserSettings {
source: source.clone(),
},
)
.map_err(|e| ErrorKind::ImportSyntaxError {
path: source,
error: Box::new(e),
TlaArg::Code(Rc::new(
jrsonnet_parser::parse(code, source.clone()).map_err(|e| {
ErrorKind::ImportSyntaxError {
path: source,
error: Box::new(e),
}
})?,
),
)),
);
}
Ok(out)
Expand Down
21 changes: 16 additions & 5 deletions crates/jrsonnet-evaluator/src/arr/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{any::Any, num::NonZeroU32};
use std::{any::Any, num::NonZeroU32, rc::Rc};

use jrsonnet_gcmodule::{Cc, Trace};
use jrsonnet_interner::IBytes;
use jrsonnet_parser::LocExpr;
use jrsonnet_parser::Expr;

use crate::{function::FuncVal, gc::TraceBox, tb, Context, Result, Thunk, Val};

Expand All @@ -29,7 +29,7 @@ impl ArrValue {
Self::new(RangeArray::empty())
}

pub fn expr(ctx: Context, exprs: impl IntoIterator<Item = LocExpr>) -> Self {
pub(crate) fn expr(ctx: Context, exprs: Rc<Vec<Expr>>) -> Self {
Self::new(ExprArray::new(ctx, exprs))
}

Expand All @@ -44,6 +44,9 @@ impl ArrValue {
pub fn repeated(data: Self, repeats: usize) -> Option<Self> {
Some(Self::new(RepeatedArray::new(data, repeats)?))
}
pub fn repeated_element(data: Thunk<Val>, repeats: usize) -> Self {
Self::new(RepeatedElement::new(data, repeats))
}

pub fn bytes(bytes: IBytes) -> Self {
Self::new(BytesArray(bytes))
Expand All @@ -52,14 +55,22 @@ impl ArrValue {
Self::new(CharArray(chars.collect()))
}

#[must_use]
fn map_inner<const WITH_INDEX: bool>(self, mapper: FuncVal) -> Self {
let len = self.len();
match <MappedArray<WITH_INDEX>>::new(self, mapper) {
Ok(v) => Self::new(v),
Err(e) => Self::repeated_element(Thunk::errored(e), len),
}
}
#[must_use]
pub fn map(self, mapper: FuncVal) -> Self {
Self::new(<MappedArray<false>>::new(self, mapper))
self.map_inner::<false>(mapper)
}

#[must_use]
pub fn map_with_index(self, mapper: FuncVal) -> Self {
Self::new(<MappedArray<true>>::new(self, mapper))
self.map_inner::<true>(mapper)
}

pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {
Expand Down
Loading

0 comments on commit ed0455a

Please sign in to comment.