Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor execution module #5162

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
748 changes: 349 additions & 399 deletions src/wasm-lib/Cargo.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/wasm-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ name = "wasm-lib"
version = "0.1.0"
edition = "2021"
repository = "https://github.com/KittyCAD/modeling-app"
rust-version = "1.73"
rust-version = "1.83"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["cdylib"]

Expand Down
2 changes: 1 addition & 1 deletion src/wasm-lib/derive-docs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ fn generate_code_block_test(fn_name: &str, code_block: &str, index: usize) -> pr
context_type: crate::execution::ContextType::Mock,
};

if let Err(e) = ctx.run(program.into(), &mut crate::ExecState::new(&ctx.settings)).await {
if let Err(e) = ctx.run(&program, &mut crate::ExecState::new(&ctx.settings)).await {
return Err(miette::Report::new(crate::errors::Report {
error: e,
filename: format!("{}{}", #fn_name, #index),
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-lib/derive-docs/tests/args_with_lifetime.gen
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod test_examples_someFn {
context_type: crate::execution::ContextType::Mock,
};
if let Err(e) = ctx
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
.run(&program, &mut crate::ExecState::new(&ctx.settings))
.await
{
return Err(miette::Report::new(crate::errors::Report {
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-lib/derive-docs/tests/args_with_refs.gen
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod test_examples_someFn {
context_type: crate::execution::ContextType::Mock,
};
if let Err(e) = ctx
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
.run(&program, &mut crate::ExecState::new(&ctx.settings))
.await
{
return Err(miette::Report::new(crate::errors::Report {
Expand Down
4 changes: 2 additions & 2 deletions src/wasm-lib/derive-docs/tests/array.gen
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod test_examples_show {
context_type: crate::execution::ContextType::Mock,
};
if let Err(e) = ctx
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
.run(&program, &mut crate::ExecState::new(&ctx.settings))
.await
{
return Err(miette::Report::new(crate::errors::Report {
Expand Down Expand Up @@ -73,7 +73,7 @@ mod test_examples_show {
context_type: crate::execution::ContextType::Mock,
};
if let Err(e) = ctx
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
.run(&program, &mut crate::ExecState::new(&ctx.settings))
.await
{
return Err(miette::Report::new(crate::errors::Report {
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-lib/derive-docs/tests/box.gen
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod test_examples_show {
context_type: crate::execution::ContextType::Mock,
};
if let Err(e) = ctx
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
.run(&program, &mut crate::ExecState::new(&ctx.settings))
.await
{
return Err(miette::Report::new(crate::errors::Report {
Expand Down
4 changes: 2 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 @@ -17,7 +17,7 @@ mod test_examples_my_func {
context_type: crate::execution::ContextType::Mock,
};
if let Err(e) = ctx
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
.run(&program, &mut crate::ExecState::new(&ctx.settings))
.await
{
return Err(miette::Report::new(crate::errors::Report {
Expand Down Expand Up @@ -74,7 +74,7 @@ mod test_examples_my_func {
context_type: crate::execution::ContextType::Mock,
};
if let Err(e) = ctx
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
.run(&program, &mut crate::ExecState::new(&ctx.settings))
.await
{
return Err(miette::Report::new(crate::errors::Report {
Expand Down
4 changes: 2 additions & 2 deletions src/wasm-lib/derive-docs/tests/lineTo.gen
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod test_examples_line_to {
context_type: crate::execution::ContextType::Mock,
};
if let Err(e) = ctx
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
.run(&program, &mut crate::ExecState::new(&ctx.settings))
.await
{
return Err(miette::Report::new(crate::errors::Report {
Expand Down Expand Up @@ -74,7 +74,7 @@ mod test_examples_line_to {
context_type: crate::execution::ContextType::Mock,
};
if let Err(e) = ctx
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
.run(&program, &mut crate::ExecState::new(&ctx.settings))
.await
{
return Err(miette::Report::new(crate::errors::Report {
Expand Down
4 changes: 2 additions & 2 deletions src/wasm-lib/derive-docs/tests/min.gen
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod test_examples_min {
context_type: crate::execution::ContextType::Mock,
};
if let Err(e) = ctx
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
.run(&program, &mut crate::ExecState::new(&ctx.settings))
.await
{
return Err(miette::Report::new(crate::errors::Report {
Expand Down Expand Up @@ -73,7 +73,7 @@ mod test_examples_min {
context_type: crate::execution::ContextType::Mock,
};
if let Err(e) = ctx
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
.run(&program, &mut crate::ExecState::new(&ctx.settings))
.await
{
return Err(miette::Report::new(crate::errors::Report {
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-lib/derive-docs/tests/option.gen
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod test_examples_show {
context_type: crate::execution::ContextType::Mock,
};
if let Err(e) = ctx
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
.run(&program, &mut crate::ExecState::new(&ctx.settings))
.await
{
return Err(miette::Report::new(crate::errors::Report {
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-lib/derive-docs/tests/option_input_format.gen
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod test_examples_import {
context_type: crate::execution::ContextType::Mock,
};
if let Err(e) = ctx
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
.run(&program, &mut crate::ExecState::new(&ctx.settings))
.await
{
return Err(miette::Report::new(crate::errors::Report {
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-lib/derive-docs/tests/return_vec_box_sketch.gen
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod test_examples_import {
context_type: crate::execution::ContextType::Mock,
};
if let Err(e) = ctx
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
.run(&program, &mut crate::ExecState::new(&ctx.settings))
.await
{
return Err(miette::Report::new(crate::errors::Report {
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-lib/derive-docs/tests/return_vec_sketch.gen
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod test_examples_import {
context_type: crate::execution::ContextType::Mock,
};
if let Err(e) = ctx
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
.run(&program, &mut crate::ExecState::new(&ctx.settings))
.await
{
return Err(miette::Report::new(crate::errors::Report {
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-lib/derive-docs/tests/show.gen
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod test_examples_show {
context_type: crate::execution::ContextType::Mock,
};
if let Err(e) = ctx
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
.run(&program, &mut crate::ExecState::new(&ctx.settings))
.await
{
return Err(miette::Report::new(crate::errors::Report {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod test_examples_some_function {
context_type: crate::execution::ContextType::Mock,
};
if let Err(e) = ctx
.run(program.into(), &mut crate::ExecState::new(&ctx.settings))
.run(&program, &mut crate::ExecState::new(&ctx.settings))
.await
{
return Err(miette::Report::new(crate::errors::Report {
Expand Down
15 changes: 9 additions & 6 deletions src/wasm-lib/kcl-test-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
/// KCL errors (from engine or the executor) respond with HTTP Bad Gateway.
/// Malformed requests are HTTP Bad Request.
/// Successful requests contain a PNG as the body.
async fn snapshot_endpoint(body: Bytes, state: ExecutorContext) -> Response<Body> {
async fn snapshot_endpoint(body: Bytes, ctxt: ExecutorContext) -> Response<Body> {

Check warning on line 154 in src/wasm-lib/kcl-test-server/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl-test-server/src/lib.rs#L154

Added line #L154 was not covered by tests
let body = match serde_json::from_slice::<RequestBody>(body.as_ref()) {
Ok(bd) => bd,
Err(e) => return bad_request(format!("Invalid request JSON: {e}")),
Expand All @@ -164,20 +164,23 @@
};

eprintln!("Executing {test_name}");
let mut exec_state = ExecState::new(&state.settings);
let mut exec_state = ExecState::new(&ctxt.settings);

Check warning on line 167 in src/wasm-lib/kcl-test-server/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl-test-server/src/lib.rs#L167

Added line #L167 was not covered by tests
// This is a shitty source range, I don't know what else to use for it though.
// There's no actual KCL associated with this reset_scene call.
if let Err(e) = state
.reset_scene(&mut exec_state, kcl_lib::SourceRange::default())
if let Err(e) = ctxt
.send_clear_scene(&mut exec_state, kcl_lib::SourceRange::default())

Check warning on line 171 in src/wasm-lib/kcl-test-server/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl-test-server/src/lib.rs#L170-L171

Added lines #L170 - L171 were not covered by tests
.await
{
return kcl_err(e);
}
// Let users know if the test is taking a long time.
let (done_tx, done_rx) = oneshot::channel::<()>();
let timer = time_until(done_rx);
let snapshot = match state.execute_and_prepare_snapshot(&program, &mut exec_state).await {
Ok(sn) => sn,
if let Err(e) = ctxt.run(&program, &mut exec_state).await {
return kcl_err(e);
}
let snapshot = match ctxt.prepare_snapshot().await {
Ok(s) => s,

Check warning on line 183 in src/wasm-lib/kcl-test-server/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl-test-server/src/lib.rs#L179-L183

Added lines #L179 - L183 were not covered by tests
Err(e) => return kcl_err(e),
};
let _ = done_tx.send(());
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-lib/kcl-to-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub async fn kcl_to_engine_core(code: &str) -> Result<String> {
let ctx = ExecutorContext::new_forwarded_mock(Arc::new(Box::new(
crate::conn_mock_core::EngineConnection::new(ref_result).await?,
)));
ctx.run(program.into(), &mut ExecState::new(&ctx.settings)).await?;
ctx.run(&program, &mut ExecState::new(&ctx.settings)).await?;

let result = result.lock().expect("mutex lock").clone();
Ok(result)
Expand Down
1 change: 1 addition & 0 deletions src/wasm-lib/kcl/src/engine/conn_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ unsafe impl Sync for EngineConnection {}

impl EngineConnection {
pub async fn new(manager: EngineCommandManager) -> Result<EngineConnection, JsValue> {
#[allow(clippy::arc_with_non_send_sync)]
Ok(EngineConnection {
manager: Arc::new(manager),
batch: Arc::new(Mutex::new(Vec::new())),
Expand Down
65 changes: 65 additions & 0 deletions src/wasm-lib/kcl/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,3 +621,68 @@
/// The opposite side of the YZ plane.
NegYz,
}

/// Create a new zoo api client.
#[cfg(not(target_arch = "wasm32"))]
pub fn new_zoo_client(token: Option<String>, engine_addr: Option<String>) -> anyhow::Result<kittycad::Client> {
let user_agent = concat!(env!("CARGO_PKG_NAME"), ".rs/", env!("CARGO_PKG_VERSION"),);
let http_client = reqwest::Client::builder()
.user_agent(user_agent)
// For file conversions we need this to be long.
.timeout(std::time::Duration::from_secs(600))
.connect_timeout(std::time::Duration::from_secs(60));
let ws_client = reqwest::Client::builder()
.user_agent(user_agent)
// For file conversions we need this to be long.
.timeout(std::time::Duration::from_secs(600))
.connect_timeout(std::time::Duration::from_secs(60))
.connection_verbose(true)
.tcp_keepalive(std::time::Duration::from_secs(600))
.http1_only();

let zoo_token_env = std::env::var("ZOO_API_TOKEN");

let token = if let Some(token) = token {
token
} else if let Ok(token) = std::env::var("KITTYCAD_API_TOKEN") {
if let Ok(zoo_token) = zoo_token_env {
if zoo_token != token {
return Err(anyhow::anyhow!(
"Both environment variables KITTYCAD_API_TOKEN=`{}` and ZOO_API_TOKEN=`{}` are set. Use only one.",
token,
zoo_token
));
}

Check warning on line 655 in src/wasm-lib/kcl/src/engine/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/engine/mod.rs#L649-L655

Added lines #L649 - L655 were not covered by tests
}
token
} else if let Ok(token) = zoo_token_env {
token

Check warning on line 659 in src/wasm-lib/kcl/src/engine/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/engine/mod.rs#L658-L659

Added lines #L658 - L659 were not covered by tests
} else {
return Err(anyhow::anyhow!(
"No API token found in environment variables. Use KITTYCAD_API_TOKEN or ZOO_API_TOKEN"
));

Check warning on line 663 in src/wasm-lib/kcl/src/engine/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/engine/mod.rs#L661-L663

Added lines #L661 - L663 were not covered by tests
};

// Create the client.
let mut client = kittycad::Client::new_from_reqwest(token, http_client, ws_client);
// Set an engine address if it's set.
let kittycad_host_env = std::env::var("KITTYCAD_HOST");
if let Some(addr) = engine_addr {
client.set_base_url(addr);

Check warning on line 671 in src/wasm-lib/kcl/src/engine/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/engine/mod.rs#L671

Added line #L671 was not covered by tests
} else if let Ok(addr) = std::env::var("ZOO_HOST") {
if let Ok(kittycad_host) = kittycad_host_env {
if kittycad_host != addr {
return Err(anyhow::anyhow!(
"Both environment variables KITTYCAD_HOST=`{}` and ZOO_HOST=`{}` are set. Use only one.",
kittycad_host,
addr
));
}
}
client.set_base_url(addr);

Check warning on line 682 in src/wasm-lib/kcl/src/engine/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/engine/mod.rs#L673-L682

Added lines #L673 - L682 were not covered by tests
} else if let Ok(addr) = kittycad_host_env {
client.set_base_url(addr);

Check warning on line 684 in src/wasm-lib/kcl/src/engine/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/engine/mod.rs#L684

Added line #L684 was not covered by tests
}

Ok(client)
}
Loading
Loading