1pub mod analyzer;
4pub mod host;
5pub mod parser;
6pub mod project;
7pub mod sourcemap;
8pub mod testgen;
9
10pub mod native_proto;
13
14pub mod pipeline;
16
17pub(crate) mod codegen;
20pub(crate) mod infer;
21pub(crate) mod jsdoc;
22pub(crate) mod native_builtins;
23#[cfg(test)]
24pub(crate) mod tests;
25
26use std::path::PathBuf;
27
28#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub enum HostType {
31 Void,
32 Bool,
33 I32,
34 I64,
35 F64,
36 Str, }
38
39#[derive(Debug, Clone)]
41pub struct HostFunction {
42 pub name: String,
43 pub params: Vec<HostType>,
44 pub return_type: Option<HostType>,
45 pub is_async: bool,
47 pub async_return_fields: Vec<(String, HostType)>,
50}
51
52impl HostFunction {
53 pub fn struct_zig_name(&self) -> String {
56 let pascal: String = self
57 .name
58 .split('_')
59 .map(|word| {
60 let mut chars = word.chars();
61 match chars.next() {
62 Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
63 None => String::new(),
64 }
65 })
66 .collect();
67 format!("{}Result", pascal)
68 }
69
70 pub fn struct_c_name(&self) -> String {
73 format!("Host{}", self.struct_zig_name())
74 }
75}
76
77impl HostType {
78 pub fn to_zig_field_type(self) -> &'static str {
80 match self {
81 HostType::Void => "void",
82 HostType::Bool => "bool",
83 HostType::I32 => "i32",
84 HostType::I64 => "i64",
85 HostType::F64 => "f64",
86 HostType::Str => "[]const u8",
87 }
88 }
89
90 pub fn to_c_field_type(self) -> &'static str {
92 match self {
93 HostType::Void => "void",
94 HostType::Bool => "bool",
95 HostType::I32 => "i32",
96 HostType::I64 => "i64",
97 HostType::F64 => "f64",
98 HostType::Str => "[256]u8",
99 }
100 }
101}
102
103#[derive(Debug, Clone)]
105pub struct HostConfig {
106 pub functions: Vec<HostFunction>,
107}
108
109#[derive(Debug, Clone)]
111pub struct ProjectConfig {
112 pub name: String,
114 pub js_file: PathBuf,
116 pub additional_js_files: Vec<PathBuf>,
118 pub out_dir: PathBuf,
120 pub host_config: Option<HostConfig>,
122 pub force_rebuild: bool,
124 pub run_zig_build: bool,
126}
127
128impl Default for ProjectConfig {
129 fn default() -> Self {
130 Self {
131 name: "js2zig_lib".into(),
132 js_file: PathBuf::from("main.js"),
133 additional_js_files: Vec::new(),
134 out_dir: PathBuf::from("out"),
135 host_config: None,
136 force_rebuild: false,
137 run_zig_build: false,
138 }
139 }
140}
141
142#[derive(Debug, Clone)]
144pub struct GroupResult {
145 pub name: String,
147 pub is_test: bool,
149 pub cabi_exports_json: String,
151 pub diagnostics: Vec<String>,
153 pub output_files: Vec<PathBuf>,
155}
156
157#[derive(Debug, Default)]
159pub struct ProjectResult {
160 pub groups: Vec<GroupResult>,
162 pub diagnostics: Vec<String>,
164}
165
166pub fn transpile_project(config: &ProjectConfig) -> Result<ProjectResult, String> {
173 pipeline::transpile_project(config)
174}
175
176pub fn write_cabi_metadata(
179 out_dir: &std::path::Path,
180 group_name: &str,
181 cabi_exports: &[native_proto::NativeCabiExport],
182 host_fns: &host::HostFnRegistry,
183 include_init: bool,
184) {
185 pipeline::write_cabi_metadata(out_dir, group_name, cabi_exports, host_fns, include_init)
186}