Skip to content

Commit ed0455a

Browse files
committed
refactor: in-progress preparsed calls
1 parent 4a7a964 commit ed0455a

File tree

36 files changed

+1801
-892
lines changed

36 files changed

+1801
-892
lines changed

README.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ There already are multiple implementations of this standard implemented in diffe
7272
* [NEW] https://github.com/eduardosm/rsjsonnet[Another Rust impl appears].
7373

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

7777
Also, I wanted to experiment on new syntax features, and jrsonnet implements some of them.
7878
For more information see link:./docs/features.adoc[features]

bindings/jsonnet/src/vars_tlas.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::{ffi::CStr, os::raw::c_char};
44

55
use jrsonnet_evaluator::{function::TlaArg, IStr};
6-
use jrsonnet_parser::{ParserSettings, Source};
6+
use jrsonnet_parser::Source;
77

88
use crate::VM;
99

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

cmds/jrsonnet/src/main.rs

+9-37
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,6 @@ enum SubOpts {
2525
},
2626
}
2727

28-
#[derive(Parser)]
29-
#[clap(next_help_heading = "DEBUG")]
30-
struct DebugOpts {
31-
/// Required OS stack size.
32-
/// This shouldn't be changed unless jrsonnet is failing with stack overflow error.
33-
#[clap(long, name = "size")]
34-
pub os_stack: Option<usize>,
35-
}
36-
3728
#[derive(Parser)]
3829
#[clap(next_help_heading = "INPUT")]
3930
struct InputOpts {
@@ -83,8 +74,6 @@ struct Opts {
8374
manifest: ManifestOpts,
8475
#[clap(flatten)]
8576
output: OutputOpts,
86-
#[clap(flatten)]
87-
debug: DebugOpts,
8877
}
8978

9079
// TODO: Add unix_sigpipe = "sig_dfl"
@@ -108,17 +97,15 @@ fn main() {
10897
}
10998
}
11099

111-
let success = if let Some(size) = opts.debug.os_stack {
112-
std::thread::Builder::new()
113-
.stack_size(size * 1024 * 1024)
114-
.spawn(|| main_catch(opts))
115-
.expect("new thread spawned")
116-
.join()
117-
.expect("thread finished successfully")
118-
} else {
119-
main_catch(opts)
120-
};
121-
if !success {
100+
let trace = opts.trace.trace_format();
101+
if let Err(e) = main_real(opts) {
102+
if let Error::Evaluation(e) = e {
103+
let mut out = String::new();
104+
trace.write_trace(&mut out, &e).expect("format error");
105+
eprintln!("{out}");
106+
} else {
107+
eprintln!("{e}");
108+
}
122109
std::process::exit(1);
123110
}
124111
}
@@ -146,21 +133,6 @@ impl From<ErrorKind> for Error {
146133
}
147134
}
148135

149-
fn main_catch(opts: Opts) -> bool {
150-
let trace = opts.trace.trace_format();
151-
if let Err(e) = main_real(opts) {
152-
if let Error::Evaluation(e) = e {
153-
let mut out = String::new();
154-
trace.write_trace(&mut out, &e).expect("format error");
155-
eprintln!("{out}");
156-
} else {
157-
eprintln!("{e}");
158-
}
159-
return false;
160-
}
161-
true
162-
}
163-
164136
fn main_real(opts: Opts) -> Result<(), Error> {
165137
let _gc_leak_guard = opts.gc.leak_on_exit();
166138
let _gc_print_stats = opts.gc.stats_printer();

crates/jrsonnet-cli/src/tla.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
use std::rc::Rc;
2+
13
use clap::Parser;
24
use jrsonnet_evaluator::{
35
error::{ErrorKind, Result},
46
function::TlaArg,
57
gc::GcHashMap,
68
IStr,
79
};
8-
use jrsonnet_parser::{ParserSettings, Source};
10+
use jrsonnet_parser::Source;
911

1012
use crate::{ExtFile, ExtStr};
1113

@@ -51,18 +53,14 @@ impl TlaOpts {
5153
let source = Source::new_virtual(format!("<top-level-arg:{name}>").into(), code.into());
5254
out.insert(
5355
(name as &str).into(),
54-
TlaArg::Code(
55-
jrsonnet_parser::parse(
56-
code,
57-
&ParserSettings {
58-
source: source.clone(),
59-
},
60-
)
61-
.map_err(|e| ErrorKind::ImportSyntaxError {
62-
path: source,
63-
error: Box::new(e),
56+
TlaArg::Code(Rc::new(
57+
jrsonnet_parser::parse(code, source.clone()).map_err(|e| {
58+
ErrorKind::ImportSyntaxError {
59+
path: source,
60+
error: Box::new(e),
61+
}
6462
})?,
65-
),
63+
)),
6664
);
6765
}
6866
Ok(out)

crates/jrsonnet-evaluator/src/arr/mod.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use std::{any::Any, num::NonZeroU32};
1+
use std::{any::Any, num::NonZeroU32, rc::Rc};
22

33
use jrsonnet_gcmodule::{Cc, Trace};
44
use jrsonnet_interner::IBytes;
5-
use jrsonnet_parser::LocExpr;
5+
use jrsonnet_parser::Expr;
66

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

@@ -29,7 +29,7 @@ impl ArrValue {
2929
Self::new(RangeArray::empty())
3030
}
3131

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

@@ -44,6 +44,9 @@ impl ArrValue {
4444
pub fn repeated(data: Self, repeats: usize) -> Option<Self> {
4545
Some(Self::new(RepeatedArray::new(data, repeats)?))
4646
}
47+
pub fn repeated_element(data: Thunk<Val>, repeats: usize) -> Self {
48+
Self::new(RepeatedElement::new(data, repeats))
49+
}
4750

4851
pub fn bytes(bytes: IBytes) -> Self {
4952
Self::new(BytesArray(bytes))
@@ -52,14 +55,22 @@ impl ArrValue {
5255
Self::new(CharArray(chars.collect()))
5356
}
5457

58+
#[must_use]
59+
fn map_inner<const WITH_INDEX: bool>(self, mapper: FuncVal) -> Self {
60+
let len = self.len();
61+
match <MappedArray<WITH_INDEX>>::new(self, mapper) {
62+
Ok(v) => Self::new(v),
63+
Err(e) => Self::repeated_element(Thunk::errored(e), len),
64+
}
65+
}
5566
#[must_use]
5667
pub fn map(self, mapper: FuncVal) -> Self {
57-
Self::new(<MappedArray<false>>::new(self, mapper))
68+
self.map_inner::<false>(mapper)
5869
}
5970

6071
#[must_use]
6172
pub fn map_with_index(self, mapper: FuncVal) -> Self {
62-
Self::new(<MappedArray<true>>::new(self, mapper))
73+
self.map_inner::<true>(mapper)
6374
}
6475

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

0 commit comments

Comments
 (0)