Skip to content

Commit

Permalink
handle js-await in wrapped let/if code; tag 0.8.44
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Mar 20, 2024
1 parent 8832b58 commit f6f5b24
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "calcit"
version = "0.8.43"
version = "0.8.44"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down
9 changes: 7 additions & 2 deletions calcit/test-js.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@
f1 $ fn () (hint-fn async)
new js/Promise $ fn (resolve reject)
js/setTimeout
fn () (println "|async code finished after 1s") (resolve true)
, 1000
fn () (println "|async code finished after 200ms") (resolve true)
, 200
f2 $ fn () (hint-fn async)
js-await $ f1
assert= true $ if true
js-await $ f1
let
a $ js-await $ f1
assert= true a
f2
|test-collection $ %{} :CodeEntry (:doc |)
:code $ quote
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@calcit/procs",
"version": "0.8.43",
"version": "0.8.44",
"main": "./lib/calcit.procs.mjs",
"devDependencies": {
"@types/node": "^20.11.28",
Expand Down
36 changes: 31 additions & 5 deletions src/codegen/emit_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,12 @@ fn make_let_with_wrapper(left: &str, right: &str, body: &str) -> String {
format!("(function __let__(){{ \nlet {left} = {right};\n {body} }})()")
}

fn make_fn_wrapper(body: &str) -> String {
format!("(function __fn__(){{\n{body}\n}})()")
fn make_fn_wrapper(body: &str, is_async: bool) -> String {
if is_async {
format!("await (async function _async_fn_(){{\n{body}\n}})()")
} else {
format!("(function _fn_(){{\n{body}\n}})()")
}
}

fn to_js_code(
Expand Down Expand Up @@ -421,6 +425,7 @@ fn gen_call_code(
match body.first() {
Some(m) => {
let message: String = to_js_code(m, ns, local_defs, file_imports, tags, None)?;
let has_await = detect_await(&body);
let data_code = match body.get(1) {
Some(d) => to_js_code(d, ns, local_defs, file_imports, tags, None)?,
None => String::from("null"),
Expand All @@ -433,7 +438,7 @@ fn gen_call_code(
// println!("inside raise: {:?} {}", return_label, xs);
match return_label {
Some(_) => Ok(ret),
_ => Ok(make_fn_wrapper(&ret)),
_ => Ok(make_fn_wrapper(&ret, has_await)),
}
}
None => Err(format!("raise expected 1~2 arguments, got: {}", body)),
Expand Down Expand Up @@ -651,6 +656,25 @@ fn gen_symbol_code(s: &str, def_ns: &str, at_def: &str, xs: &Calcit, passed_defs
}
}

fn detect_await(xs: &CalcitList) -> bool {
for x in xs {
match x {
Calcit::List(al) => {
if detect_await(al) {
return true;
}
}
Calcit::Symbol { sym, .. } => {
if &**sym == "js-await" {
return true;
}
}
_ => {}
}
}
false
}

fn gen_let_code(
body: &CalcitList,
local_defs: &HashSet<Arc<str>>,
Expand All @@ -662,6 +686,7 @@ fn gen_let_code(
) -> Result<String, String> {
let mut let_def_body = body.to_owned();
let return_label = base_return_label.unwrap_or("return ");
let has_await = detect_await(body);

// defined new local variable
let mut scoped_defs = local_defs.to_owned();
Expand Down Expand Up @@ -784,7 +809,7 @@ fn gen_let_code(
if base_return_label.is_some() {
Ok(format!("{defs_code}{body_part}"))
} else {
Ok(make_fn_wrapper(&format!("{defs_code}{body_part}")))
Ok(make_fn_wrapper(&format!("{defs_code}{body_part}"), has_await))
}
}

Expand All @@ -805,6 +830,7 @@ fn gen_if_code(
let mut true_node = body[1].to_owned();
let mut some_false_node = body.get(2);
let mut need_else = false;
let has_await = detect_await(body);

let return_label = base_return_label.unwrap_or("return ");

Expand Down Expand Up @@ -842,7 +868,7 @@ fn gen_if_code(
if base_return_label.is_some() {
Ok(chunk)
} else {
Ok(make_fn_wrapper(&chunk))
Ok(make_fn_wrapper(&chunk, has_await))
}
}
}
Expand Down

0 comments on commit f6f5b24

Please sign in to comment.