Skip to content

Commit

Permalink
Unify execution state into a single struct (#3877)
Browse files Browse the repository at this point in the history
* Add ExecState that combines ProgramMemory and DynamicState

* Remove unneeded clones

* Add exec_state parameter to all KCL stdlib functions

* Move pipe value into ExecState

* Add test for pipe substitution not leaking into function calls

* KCL: Better message on assertEqual function

Also add a new no-visual test for performance testing.

* Fix new array module to use ExecState

---------

Co-authored-by: Adam Chalmers <[email protected]>
  • Loading branch information
jtran and adamchalmers committed Sep 16, 2024
1 parent c4ff1c2 commit 0ff820d
Show file tree
Hide file tree
Showing 44 changed files with 764 additions and 708 deletions.
10 changes: 7 additions & 3 deletions src/wasm-lib/derive-docs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ fn do_stdlib_inner(
let ty_string = rust_type_to_openapi_type(&ty_string);
let required = !ty_ident.to_string().starts_with("Option <");

if ty_string != "Args" {
if ty_string != "ExecState" && ty_string != "Args" {
let schema = if ty_ident.to_string().starts_with("Vec < ")
|| ty_ident.to_string().starts_with("Option <")
|| ty_ident.to_string().starts_with('[')
Expand Down Expand Up @@ -387,11 +387,12 @@ fn do_stdlib_inner(
#const_struct

fn #boxed_fn_name_ident(
exec_state: &mut crate::executor::ExecState,
args: crate::std::Args,
) -> std::pin::Pin<
Box<dyn std::future::Future<Output = anyhow::Result<crate::executor::KclValue, crate::errors::KclError>> + Send>,
Box<dyn std::future::Future<Output = anyhow::Result<crate::executor::KclValue, crate::errors::KclError>> + Send + '_>,
> {
Box::pin(#fn_name_ident(args))
Box::pin(#fn_name_ident(exec_state, args))
}

impl #docs_crate::StdLibFn for #name_ident
Expand Down Expand Up @@ -662,6 +663,9 @@ fn clean_ty_string(t: &str) -> (String, proc_macro2::TokenStream) {
.replace("mut", "")
.replace("< 'a >", "")
.replace(' ', "");
if ty_string.starts_with("ExecState") {
ty_string = "ExecState".to_string();
}
if ty_string.starts_with("Args") {
ty_string = "Args".to_string();
}
Expand Down
26 changes: 26 additions & 0 deletions src/wasm-lib/derive-docs/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,32 @@ fn test_args_with_lifetime() {
expectorate::assert_contents("tests/args_with_lifetime.gen", &get_text_fmt(&item).unwrap());
}

#[test]
fn test_args_with_exec_state() {
let (item, mut errors) = do_stdlib(
quote! {
name = "someFunction",
},
quote! {
/// Docs
/// ```
/// someFunction()
/// ```
fn inner_some_function<'a>(
exec_state: &mut ExecState,
args: &Args,
) -> i32 {
3
}
},
)
.unwrap();
if let Some(e) = errors.pop() {
panic!("{e}");
}
expectorate::assert_contents("tests/test_args_with_exec_state.gen", &get_text_fmt(&item).unwrap());
}

#[test]
fn test_stdlib_line_to() {
let (item, errors) = do_stdlib(
Expand Down
6 changes: 4 additions & 2 deletions src/wasm-lib/derive-docs/tests/args_with_lifetime.gen
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ pub(crate) struct SomeFn {}
#[doc = "Std lib function: someFn\nDocs"]
pub(crate) const SomeFn: SomeFn = SomeFn {};
fn boxed_someFn(
exec_state: &mut crate::executor::ExecState,
args: crate::std::Args,
) -> std::pin::Pin<
Box<
dyn std::future::Future<
Output = anyhow::Result<crate::executor::KclValue, crate::errors::KclError>,
> + Send,
> + Send
+ '_,
>,
> {
Box::pin(someFn(args))
Box::pin(someFn(exec_state, args))
}

impl crate::docs::StdLibFn for SomeFn {
Expand Down
6 changes: 4 additions & 2 deletions src/wasm-lib/derive-docs/tests/args_with_refs.gen
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ pub(crate) struct SomeFn {}
#[doc = "Std lib function: someFn\nDocs"]
pub(crate) const SomeFn: SomeFn = SomeFn {};
fn boxed_someFn(
exec_state: &mut crate::executor::ExecState,
args: crate::std::Args,
) -> std::pin::Pin<
Box<
dyn std::future::Future<
Output = anyhow::Result<crate::executor::KclValue, crate::errors::KclError>,
> + Send,
> + Send
+ '_,
>,
> {
Box::pin(someFn(args))
Box::pin(someFn(exec_state, args))
}

impl crate::docs::StdLibFn for SomeFn {
Expand Down
6 changes: 4 additions & 2 deletions src/wasm-lib/derive-docs/tests/array.gen
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,17 @@ pub(crate) struct Show {}
#[doc = "Std lib function: show\nThis is some function.\nIt does shit."]
pub(crate) const Show: Show = Show {};
fn boxed_show(
exec_state: &mut crate::executor::ExecState,
args: crate::std::Args,
) -> std::pin::Pin<
Box<
dyn std::future::Future<
Output = anyhow::Result<crate::executor::KclValue, crate::errors::KclError>,
> + Send,
> + Send
+ '_,
>,
> {
Box::pin(show(args))
Box::pin(show(exec_state, args))
}

impl crate::docs::StdLibFn for Show {
Expand Down
6 changes: 4 additions & 2 deletions src/wasm-lib/derive-docs/tests/box.gen
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ pub(crate) struct Show {}
#[doc = "Std lib function: show\nThis is some function.\nIt does shit."]
pub(crate) const Show: Show = Show {};
fn boxed_show(
exec_state: &mut crate::executor::ExecState,
args: crate::std::Args,
) -> std::pin::Pin<
Box<
dyn std::future::Future<
Output = anyhow::Result<crate::executor::KclValue, crate::errors::KclError>,
> + Send,
> + Send
+ '_,
>,
> {
Box::pin(show(args))
Box::pin(show(exec_state, args))
}

impl crate::docs::StdLibFn for Show {
Expand Down
6 changes: 4 additions & 2 deletions src/wasm-lib/derive-docs/tests/doc_comment_with_code.gen
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,17 @@ pub(crate) struct MyFunc {}
#[doc = "Std lib function: myFunc\nThis is some function.\nIt does shit."]
pub(crate) const MyFunc: MyFunc = MyFunc {};
fn boxed_my_func(
exec_state: &mut crate::executor::ExecState,
args: crate::std::Args,
) -> std::pin::Pin<
Box<
dyn std::future::Future<
Output = anyhow::Result<crate::executor::KclValue, crate::errors::KclError>,
> + Send,
> + Send
+ '_,
>,
> {
Box::pin(my_func(args))
Box::pin(my_func(exec_state, args))
}

impl crate::docs::StdLibFn for MyFunc {
Expand Down
6 changes: 4 additions & 2 deletions src/wasm-lib/derive-docs/tests/lineTo.gen
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,17 @@ pub(crate) struct LineTo {}
#[doc = "Std lib function: lineTo\nThis is some function.\nIt does shit."]
pub(crate) const LineTo: LineTo = LineTo {};
fn boxed_line_to(
exec_state: &mut crate::executor::ExecState,
args: crate::std::Args,
) -> std::pin::Pin<
Box<
dyn std::future::Future<
Output = anyhow::Result<crate::executor::KclValue, crate::errors::KclError>,
> + Send,
> + Send
+ '_,
>,
> {
Box::pin(line_to(args))
Box::pin(line_to(exec_state, args))
}

impl crate::docs::StdLibFn for LineTo {
Expand Down
6 changes: 4 additions & 2 deletions src/wasm-lib/derive-docs/tests/min.gen
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,17 @@ pub(crate) struct Min {}
#[doc = "Std lib function: min\nThis is some function.\nIt does shit."]
pub(crate) const Min: Min = Min {};
fn boxed_min(
exec_state: &mut crate::executor::ExecState,
args: crate::std::Args,
) -> std::pin::Pin<
Box<
dyn std::future::Future<
Output = anyhow::Result<crate::executor::KclValue, crate::errors::KclError>,
> + Send,
> + Send
+ '_,
>,
> {
Box::pin(min(args))
Box::pin(min(exec_state, args))
}

impl crate::docs::StdLibFn for Min {
Expand Down
6 changes: 4 additions & 2 deletions src/wasm-lib/derive-docs/tests/option.gen
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ pub(crate) struct Show {}
#[doc = "Std lib function: show\nThis is some function.\nIt does shit."]
pub(crate) const Show: Show = Show {};
fn boxed_show(
exec_state: &mut crate::executor::ExecState,
args: crate::std::Args,
) -> std::pin::Pin<
Box<
dyn std::future::Future<
Output = anyhow::Result<crate::executor::KclValue, crate::errors::KclError>,
> + Send,
> + Send
+ '_,
>,
> {
Box::pin(show(args))
Box::pin(show(exec_state, args))
}

impl crate::docs::StdLibFn for Show {
Expand Down
6 changes: 4 additions & 2 deletions src/wasm-lib/derive-docs/tests/option_input_format.gen
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ pub(crate) struct Import {}
#[doc = "Std lib function: import\nThis is some function.\nIt does shit."]
pub(crate) const Import: Import = Import {};
fn boxed_import(
exec_state: &mut crate::executor::ExecState,
args: crate::std::Args,
) -> std::pin::Pin<
Box<
dyn std::future::Future<
Output = anyhow::Result<crate::executor::KclValue, crate::errors::KclError>,
> + Send,
> + Send
+ '_,
>,
> {
Box::pin(import(args))
Box::pin(import(exec_state, args))
}

impl crate::docs::StdLibFn for Import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ pub(crate) struct Import {}
#[doc = "Std lib function: import\nThis is some function.\nIt does shit."]
pub(crate) const Import: Import = Import {};
fn boxed_import(
exec_state: &mut crate::executor::ExecState,
args: crate::std::Args,
) -> std::pin::Pin<
Box<
dyn std::future::Future<
Output = anyhow::Result<crate::executor::KclValue, crate::errors::KclError>,
> + Send,
> + Send
+ '_,
>,
> {
Box::pin(import(args))
Box::pin(import(exec_state, args))
}

impl crate::docs::StdLibFn for Import {
Expand Down
6 changes: 4 additions & 2 deletions src/wasm-lib/derive-docs/tests/return_vec_sketch_group.gen
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ pub(crate) struct Import {}
#[doc = "Std lib function: import\nThis is some function.\nIt does shit."]
pub(crate) const Import: Import = Import {};
fn boxed_import(
exec_state: &mut crate::executor::ExecState,
args: crate::std::Args,
) -> std::pin::Pin<
Box<
dyn std::future::Future<
Output = anyhow::Result<crate::executor::KclValue, crate::errors::KclError>,
> + Send,
> + Send
+ '_,
>,
> {
Box::pin(import(args))
Box::pin(import(exec_state, args))
}

impl crate::docs::StdLibFn for Import {
Expand Down
6 changes: 4 additions & 2 deletions src/wasm-lib/derive-docs/tests/show.gen
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ pub(crate) struct Show {}
#[doc = "Std lib function: show\nThis is some function.\nIt does shit."]
pub(crate) const Show: Show = Show {};
fn boxed_show(
exec_state: &mut crate::executor::ExecState,
args: crate::std::Args,
) -> std::pin::Pin<
Box<
dyn std::future::Future<
Output = anyhow::Result<crate::executor::KclValue, crate::errors::KclError>,
> + Send,
> + Send
+ '_,
>,
> {
Box::pin(show(args))
Box::pin(show(exec_state, args))
}

impl crate::docs::StdLibFn for Show {
Expand Down
Loading

0 comments on commit 0ff820d

Please sign in to comment.