Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into lf94/tauri-to-electron
Browse files Browse the repository at this point in the history
  • Loading branch information
Irev-Dev committed Aug 14, 2024
2 parents e82b39d + b2b62ec commit 338f46f
Show file tree
Hide file tree
Showing 13 changed files with 2,421 additions and 84 deletions.
1 change: 1 addition & 0 deletions src/editor/plugins/lsp/kcl/highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const klcHighlight = styleTags({
'true false': t.bool,
nil: t.null,
'AddOp MultOp ExpOp': t.arithmeticOperator,
BangOp: t.logicOperator,
CompOp: t.logicOperator,
'Equals Arrow': t.definitionOperator,
PipeOperator: t.controlOperator,
Expand Down
5 changes: 4 additions & 1 deletion src/editor/plugins/lsp/kcl/kcl.grammar
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ expression[@isGroup=Expression] {
expression !exp ExpOp expression |
expression !comp CompOp expression
} |
UnaryExpression { AddOp expression } |
UnaryExpression { UnaryOp expression } |
ParenthesizedExpression { "(" expression ")" } |
CallExpression { expression !call ArgumentList } |
ArrayExpression { "[" commaSep<expression | IntegerRange { expression !range ".." expression }> "]" } |
Expand All @@ -48,6 +48,8 @@ expression[@isGroup=Expression] {
PipeExpression { expression (!pipe PipeOperator expression)+ }
}

UnaryOp { AddOp | BangOp }

ObjectProperty { PropertyName ":" expression }

ArgumentList { "(" commaSep<expression> ")" }
Expand Down Expand Up @@ -80,6 +82,7 @@ commaSep<term> { (term ("," term)*)? ","? }
AddOp { "+" | "-" }
MultOp { "/" | "*" | "\\" }
ExpOp { "^" }
BangOp { "!" }
CompOp { $[<>] "="? | "!=" | "==" }
Equals { "=" }
Arrow { "=>" }
Expand Down
8 changes: 8 additions & 0 deletions src/wasm-lib/kcl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,11 @@ required-features = ["lsp-test-util"]
name = "lsp_semantic_tokens_benchmark_iai"
harness = false
required-features = ["lsp-test-util"]

[[bench]]
name = "executor_benchmark_criterion"
harness = false

[[bench]]
name = "executor_benchmark_iai"
harness = false
36 changes: 36 additions & 0 deletions src/wasm-lib/kcl/benches/executor_benchmark_criterion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use kcl_lib::test_server;
use tokio::runtime::Runtime;

pub fn bench_execute(c: &mut Criterion) {
for (name, code) in [
("big_kitt", KITT_PROGRAM),
("cube", CUBE_PROGRAM),
("server_rack_heavy", SERVER_RACK_HEAVY_PROGRAM),
] {
let mut group = c.benchmark_group("executor");
// Configure Criterion.rs to detect smaller differences and increase sample size to improve
// precision and counteract the resulting noise.
group.sample_size(10);
group.bench_with_input(BenchmarkId::new("execute_", name), &code, |b, &s| {
let rt = Runtime::new().unwrap();

// Spawn a future onto the runtime
b.iter(|| {
rt.block_on(test_server::execute_and_snapshot(
s,
kcl_lib::settings::types::UnitLength::Mm,
))
.unwrap();
});
});
group.finish();
}
}

criterion_group!(benches, bench_execute);
criterion_main!(benches);

const KITT_PROGRAM: &str = include_str!("../../tests/executor/inputs/kittycad_svg.kcl");
const CUBE_PROGRAM: &str = include_str!("../../tests/executor/inputs/cube.kcl");
const SERVER_RACK_HEAVY_PROGRAM: &str = include_str!("../../tests/executor/inputs/server-rack-heavy.kcl");
16 changes: 16 additions & 0 deletions src/wasm-lib/kcl/benches/executor_benchmark_iai.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use iai::black_box;

async fn execute_server_rack_heavy() {
let code = SERVER_RACK_HEAVY_PROGRAM;
black_box(
kcl_lib::test_server::execute_and_snapshot(code, kcl_lib::settings::types::UnitLength::Mm)
.await
.unwrap(),
);
}

iai::main! {
execute_server_rack_heavy,
}

const SERVER_RACK_HEAVY_PROGRAM: &str = include_str!("../../tests/executor/inputs/server-rack-heavy.kcl");
78 changes: 62 additions & 16 deletions src/wasm-lib/kcl/src/ast/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,7 @@ impl CallExpression {
source_range: SourceRange([arg.start(), arg.end()]),
};
let result = ctx
.arg_into_mem_item(
.execute_expr(
arg,
memory,
dynamic_state,
Expand Down Expand Up @@ -3196,6 +3196,18 @@ pub fn parse_json_value_as_string(j: &serde_json::Value) -> Option<String> {
}
}

/// JSON value as bool. If it isn't a bool, returns None.
pub fn json_as_bool(j: &serde_json::Value) -> Option<bool> {
match j {
JValue::Null => None,
JValue::Bool(b) => Some(*b),
JValue::Number(_) => None,
JValue::String(_) => None,
JValue::Array(_) => None,
JValue::Object(_) => None,
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, FromStr, Display, Bake)]
#[databake(path = kcl_lib::ast::types)]
#[ts(export)]
Expand Down Expand Up @@ -3336,6 +3348,27 @@ impl UnaryExpression {
pipe_info: &PipeInfo,
ctx: &ExecutorContext,
) -> Result<KclValue, KclError> {
if self.operator == UnaryOperator::Not {
let value = self
.argument
.get_result(memory, dynamic_state, pipe_info, ctx)
.await?
.get_json_value()?;
let Some(bool_value) = json_as_bool(&value) else {
return Err(KclError::Semantic(KclErrorDetails {
message: format!("Cannot apply unary operator ! to non-boolean value: {}", value),
source_ranges: vec![self.into()],
}));
};
let negated = !bool_value;
return Ok(KclValue::UserVal(UserVal {
value: serde_json::Value::Bool(negated),
meta: vec![Metadata {
source_range: self.into(),
}],
}));
}

let num = parse_json_number_as_f64(
&self
.argument
Expand Down Expand Up @@ -3549,7 +3582,7 @@ async fn execute_pipe_body(
source_range: SourceRange([first.start(), first.end()]),
};
let output = ctx
.arg_into_mem_item(
.execute_expr(
first,
memory,
dynamic_state,
Expand All @@ -3565,26 +3598,39 @@ async fn execute_pipe_body(
new_pipe_info.previous_results = Some(output);
// Evaluate remaining elements.
for expression in body {
let output = match expression {
Expr::BinaryExpression(binary_expression) => {
binary_expression
.get_result(memory, dynamic_state, &new_pipe_info, ctx)
.await?
}
Expr::CallExpression(call_expression) => {
call_expression
.execute(memory, dynamic_state, &new_pipe_info, ctx)
.await?
}
Expr::Identifier(identifier) => memory.get(&identifier.name, identifier.into())?.clone(),
_ => {
// Return an error this should not happen.
match expression {
Expr::TagDeclarator(_) => {
return Err(KclError::Semantic(KclErrorDetails {
message: format!("This cannot be in a PipeExpression: {:?}", expression),
source_ranges: vec![expression.into()],
}));
}
Expr::Literal(_)
| Expr::Identifier(_)
| Expr::BinaryExpression(_)
| Expr::FunctionExpression(_)
| Expr::CallExpression(_)
| Expr::PipeExpression(_)
| Expr::PipeSubstitution(_)
| Expr::ArrayExpression(_)
| Expr::ObjectExpression(_)
| Expr::MemberExpression(_)
| Expr::UnaryExpression(_)
| Expr::None(_) => {}
};
let metadata = Metadata {
source_range: SourceRange([expression.start(), expression.end()]),
};
let output = ctx
.execute_expr(
expression,
memory,
dynamic_state,
&new_pipe_info,
&metadata,
StatementKind::Expression,
)
.await?;
new_pipe_info.previous_results = Some(output);
}
// Safe to unwrap here, because `newpipe_info` always has something pushed in when the `match first` executes.
Expand Down
Loading

0 comments on commit 338f46f

Please sign in to comment.