use crate::types::{JSDocData, NativeCabiExport, TranspileResult};
use oxc_ast::ast::Program;
use crate::zigir::types::{IrDecl, IrExpr, IrStmt};
pub fn transpile_js(
program: &Program<'_>,
js_source: &str,
exported_functions: Option<std::collections::HashSet<String>>,
host_fns: Option<&crate::host::HostFnRegistry>,
module_name: &str,
) -> Result<TranspileResult, String> {
let (typedefs, type_annotations, return_types, param_types) =
crate::jsdoc::extract_all_jsdoc(js_source);
let jsdoc_data = JSDocData {
typedefs,
type_annotations,
return_types,
param_types,
};
let mut inferrer = crate::infer::TypeInferrer::new();
inferrer.set_jsdoc_data(jsdoc_data.clone());
if let Some(hf) = host_fns {
inferrer.set_host_fn_types(hf);
}
let type_info = inferrer.infer_all(program, exported_functions.clone());
let infer_errors = type_info.errors.clone();
let var_types = type_info.var_types.clone();
let async_host_fns: std::collections::HashSet<String> = if let Some(hf) = host_fns {
hf.async_fn_names().into_iter().collect()
} else {
std::collections::HashSet::new()
};
use crate::zigir::lower::Lowerer;
let mut lowerer = Lowerer::new(
type_info,
jsdoc_data,
exported_functions,
async_host_fns,
js_source.to_string(),
module_name.to_string(),
);
let mut ir_module = lowerer.lower(program);
let mut pipeline = crate::zigir::passes::PassPipeline::default_pipeline();
let pass_result = pipeline.run(&mut ir_module);
if !pass_result.diagnostics.is_empty() {
ir_module.diagnostics.extend(pass_result.diagnostics);
}
let compile_errors = collect_compile_errors(&ir_module);
use crate::zigir::emit::Emitter;
let zig_code = Emitter::emit_module(&ir_module);
let mut errors: Vec<String> = infer_errors;
let mut warnings: Vec<String> = Vec::new();
for diag in &ir_module.diagnostics {
match diag.level {
crate::zigir::source_span::DiagnosticLevel::Error => {
errors.push(diag.message.clone());
}
crate::zigir::source_span::DiagnosticLevel::Warning => {
warnings.push(diag.message.clone());
}
}
}
let cabi_exports: Vec<NativeCabiExport> = ir_module
.cabi_exports
.iter()
.map(|ce| NativeCabiExport {
name: ce.name.clone(),
params: ce
.params
.iter()
.enumerate()
.map(|(i, p)| (format!("arg{}", i), p.zig_type.clone()))
.collect(),
ret_type: ce.return_type.clone(),
is_async: ce.is_async,
can_throw: ce.can_throw,
ret_struct_name: ce.ret_struct_name.clone(),
})
.collect();
Ok(TranspileResult {
zig_code,
errors,
warnings,
compile_errors,
var_types,
cabi_exports,
})
}
fn collect_compile_errors(module: &crate::zigir::types::IrModule) -> Vec<String> {
use crate::zigir::passes::walk;
use std::cell::RefCell;
let results = RefCell::new(Vec::new());
fn push_msg(msg: &str, results: &RefCell<Vec<String>>) {
for part in msg.split("\n\nAlso: ") {
results.borrow_mut().push(part.to_string());
}
}
fn collect_from_block(block: &crate::zigir::types::IrBlock, results: &RefCell<Vec<String>>) {
for stmt in &block.stmts {
collect_from_stmt(stmt, results);
}
}
fn collect_from_stmt(stmt: &IrStmt, results: &RefCell<Vec<String>>) {
match stmt {
IrStmt::CompileError { msg, .. } => push_msg(msg, results),
_ => walk::for_each_stmt_child(
stmt,
&mut |block| collect_from_block(block, results),
&mut |s| collect_from_stmt(s, results),
&mut |expr| collect_from_expr(expr, results),
&mut |_| {},
),
}
}
fn collect_from_expr(expr: &IrExpr, results: &RefCell<Vec<String>>) {
match expr {
IrExpr::CompileError { msg, .. } => push_msg(msg, results),
_ => walk::for_each_expr_child(
expr,
&mut |block| collect_from_block(block, results),
&mut |s| collect_from_stmt(s, results),
&mut |expr| collect_from_expr(expr, results),
&mut |_| {},
),
}
}
for decl in &module.declarations {
match decl {
IrDecl::CompileError { msg, .. } => push_msg(msg, &results),
_ => walk::for_each_decl_child(
decl,
&mut |block| collect_from_block(block, &results),
&mut |expr| collect_from_expr(expr, &results),
),
}
}
for cs in &module.closure_structs {
collect_from_block(&cs.body, &results);
}
results.into_inner()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_collect_compile_errors_from_ir() {
use crate::types::ZigType;
use crate::zigir::ident::IrIdent;
use crate::zigir::source_span::SourceSpan;
use crate::zigir::types::{IrBlock, IrDecl, IrFnDecl, IrModule, IrStmt};
let mut module = IrModule::new("test".to_string());
module.declarations.push(IrDecl::CompileError {
span: SourceSpan::new(1, 1),
msg: "unsupported import".to_string(),
});
module.declarations.push(IrDecl::Fn(IrFnDecl {
name: IrIdent::new("testFn"),
params: vec![],
return_type: ZigType::Void,
body: IrBlock::new(vec![IrStmt::CompileError {
span: SourceSpan::new(5, 10),
msg: "nested class not supported".to_string(),
}]),
is_export: false,
is_async: false,
can_throw: false,
is_cabi: false,
typeof_return_body: None,
}));
let results = collect_compile_errors(&module);
assert_eq!(results.len(), 2);
assert!(results.iter().any(|m| m.contains("unsupported import")));
assert!(
results
.iter()
.any(|m| m.contains("nested class not supported"))
);
}
#[test]
fn test_collect_compile_errors_splits_merged() {
use crate::types::ZigType;
use crate::zigir::ident::IrIdent;
use crate::zigir::source_span::SourceSpan;
use crate::zigir::types::{IrBlock, IrDecl, IrFnDecl, IrModule, IrStmt};
let mut module = IrModule::new("test".to_string());
module.declarations.push(IrDecl::Fn(IrFnDecl {
name: IrIdent::new("testFn"),
params: vec![],
return_type: ZigType::Void,
body: IrBlock::new(vec![IrStmt::CompileError {
span: SourceSpan::new(1, 1),
msg: "error 1\n\nAlso: error 2\n\nAlso: error 3".to_string(),
}]),
is_export: false,
is_async: false,
can_throw: false,
is_cabi: false,
typeof_return_body: None,
}));
let results = collect_compile_errors(&module);
assert_eq!(results.len(), 3);
assert_eq!(results[0], "error 1");
assert_eq!(results[1], "error 2");
assert_eq!(results[2], "error 3");
}
}