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

reuse args checking code from procs #230

Merged
merged 1 commit into from
Jan 17, 2024
Merged
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
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.15"
version = "0.8.16"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2021"
license = "MIT"
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.15",
"version": "0.8.16",
"main": "./lib/calcit.procs.mjs",
"devDependencies": {
"@types/node": "^20.10.2",
Expand Down
17 changes: 5 additions & 12 deletions src/codegen/emit_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ fn gen_call_code(
local_defs,
file_imports,
};
let ret = gen_js_func(sym, &get_raw_args(ys)?, &func_body, &passed_defs, false, tags);
let ret = gen_js_func(sym, &get_raw_args(ys)?, &func_body, &passed_defs, false, tags, ns);
gen_stack::pop_call_stack();
match ret {
Ok(code) => Ok(format!("{return_code}{code}")),
Expand Down Expand Up @@ -957,6 +957,7 @@ fn gen_js_func(
passed_defs: &PassedDefs,
exported: bool,
tags: &RefCell<HashSet<EdnTag>>,
at_ns: &str,
) -> Result<String, String> {
let var_prefix = if passed_defs.ns == "calcit.core" { "" } else { "$calcit." };
let mut local_defs = passed_defs.local_defs.to_owned();
Expand Down Expand Up @@ -1008,7 +1009,7 @@ fn gen_js_func(
} else if has_optional {
snippets::tmpl_args_between(args_count, args_count + optional_count)
} else {
snippets::tmpl_args_exact(name, args_count)
snippets::tmpl_args_exact(name, args_count, at_ns)
};

let mut body: CalcitItems = TernaryTreeList::Empty;
Expand Down Expand Up @@ -1265,7 +1266,7 @@ pub fn emit_js(entry_ns: &str, emit_path: &str) -> Result<(), String> {
local_defs: &def_names,
file_imports: &file_imports,
};
defs_code.push_str(&gen_js_func(&def, args, code, &passed_defs, true, &collected_tags)?);
defs_code.push_str(&gen_js_func(&def, args, code, &passed_defs, true, &collected_tags, &ns)?);
gen_stack::pop_call_stack();
}
Calcit::Thunk(code, _) => {
Expand Down Expand Up @@ -1354,15 +1355,7 @@ pub fn emit_js(entry_ns: &str, emit_path: &str) -> Result<(), String> {
let js_file_path = code_emit_path.join(to_mjs_filename(&ns));
let wrote_new = write_file_if_changed(
&js_file_path,
&format!(
"{}{}{}\n{}\n\n{}\n{}",
import_code,
tags_code,
snippets::tmpl_errors_init(),
defs_code,
vals_code,
direct_code
),
&format!("{}{}\n{}\n\n{}\n{}", import_code, tags_code, defs_code, vals_code, direct_code),
)?;
if wrote_new {
println!("emitted: {}", js_file_path.to_str().expect("exptract path"));
Expand Down
19 changes: 5 additions & 14 deletions src/codegen/emit_js/snippets.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::builtins::meta::js_gensym;
use crate::{builtins::meta::js_gensym, codegen::emit_js::get_proc_prefix};

pub const CALCIT_VERSION: &str = env!("CARGO_PKG_VERSION");

Expand Down Expand Up @@ -39,11 +39,12 @@ if (arguments.length > {}) throw new Error('too many arguments');",
)
}

pub fn tmpl_args_exact(name: &str, args_count: usize) -> String {
pub fn tmpl_args_exact(name: &str, args_count: usize, at_ns: &str) -> String {
let proc_ns = get_proc_prefix(at_ns);
format!(
"
if (arguments.length !== {}) throw _calcit_args_mismatch('{}', {}, arguments.length);",
args_count, name, args_count
if (arguments.length !== {}) throw {}_calcit_args_mismatch('{}', {}, arguments.length);",
args_count, proc_ns, name, args_count
)
}

Expand Down Expand Up @@ -149,13 +150,3 @@ pub fn tmpl_tags_init(arr: &str, prefix: &str) -> String {
arr, prefix
)
}

pub fn tmpl_errors_init() -> String {
String::from(
"
let _calcit_args_mismatch = (name, expected, got) => {
return new Error(`\\`${name}\\` expected ${expected} params, got ${got}`);
};
",
)
}
4 changes: 4 additions & 0 deletions ts-src/calcit.procs.mts
Original file line number Diff line number Diff line change
Expand Up @@ -1522,5 +1522,9 @@ export let macroexpand = unavailableProc;
export let macroexpand_all = unavailableProc;
export let _$n_get_calcit_running_mode = unavailableProc;

export let _calcit_args_mismatch = (name: string, expected: number, got: number) => {
return new Error(`\`${name}\` expected ${expected} params, got ${got}`);
};

// already handled in code emitter
export let raise = unavailableProc;