1#[cfg(all(feature = "compiler", any(target_arch = "wasm32", feature = "embed")))]
16use crate::compiler::compile;
17#[cfg(feature = "compiler")]
21use crate::errors::BOLD;
22#[cfg(feature = "compiler")]
23use crate::errors::ErrorCtx;
24#[cfg(feature = "compiler")]
25use crate::errors::RED;
26#[cfg(feature = "compiler")]
27use crate::errors::RESET;
28use crate::vm::RegisterFile;
29#[cfg(all(feature = "embed", feature = "compiler"))]
30use std::ffi::{CStr, CString, c_char};
31#[cfg(feature = "compiler")]
32use std::hint::cold_path;
33#[cfg(all(feature = "embed", feature = "compiler"))]
34use std::panic::catch_unwind;
35#[cfg(target_arch = "wasm32")]
36use wasm_bindgen::prelude::*;
37
38pub(crate) use candela_vm::data;
42pub(crate) use candela_vm::errors;
43pub(crate) use candela_vm::instr;
44pub(crate) use candela_vm::rt;
45pub(crate) use candela_vm::vm;
46
47#[cfg(feature = "compiler")]
51#[path = "./compiler/compiler.rs"]
52pub mod compiler;
53#[cfg(feature = "compiler")]
56mod engine;
57#[cfg(feature = "compiler")]
59mod build;
60#[cfg(all(feature = "native", not(target_arch = "wasm32")))]
62pub mod native;
63#[cfg(all(feature = "compiler", not(target_arch = "wasm32")))]
65mod cli;
66#[cfg(all(feature = "compiler", not(target_arch = "wasm32")))]
68mod lpm;
69#[cfg(all(feature = "compiler", not(target_arch = "wasm32")))]
71mod manifest;
72#[cfg(feature = "compiler")]
74pub mod cfg;
75#[cfg(feature = "compiler")]
79pub mod macros;
80#[cfg(feature = "compiler")]
83#[path = "./parser/parser.rs"]
84pub mod parser;
85#[cfg(feature = "compiler")]
86mod repl;
87#[path = "./tests.rs"]
88#[cfg(all(test, feature = "compiler"))]
89mod tests;
90#[cfg(feature = "compiler")]
93mod trampoline;
94#[cfg(feature = "compiler")]
97mod update;
98#[path = "./util/util.rs"]
99mod util;
100#[cfg(feature = "compiler")]
103mod warnings;
104
105pub use candela_vm::Diagnostic;
106pub use candela_vm::collect_diagnostic;
107
108pub use candela_vm::FromHostValue;
109pub use candela_vm::HostError;
110pub use candela_vm::HostType;
111pub use candela_vm::IntoHostFn;
112pub use candela_vm::IntoHostResult;
113pub use candela_vm::IntoHostValue;
114pub use candela_vm::Value;
115#[cfg(feature = "compiler")]
116pub use cfg::Cfg;
117#[cfg(feature = "compiler")]
118pub use engine::Engine;
119#[cfg(feature = "compiler")]
120pub use engine::Program;
121#[cfg(feature = "compiler")]
124pub use compiler::imports::ImportResolver;
125
126pub use candela_vm::CallError;
128pub use candela_vm::HostBindError;
129pub use candela_vm::HostRegistry;
130pub use candela_vm::LoadError;
131pub use candela_vm::RuntimeProgram;
132pub use candela_vm::load_program;
133pub use candela_vm::Attribute;
135pub use candela_vm::AttributeTable;
136pub use candela_vm::AttributeTarget;
137pub use candela_vm::AttributeValue;
138pub use candela_vm::read_attributes;
139pub use candela_vm::rt::DataType;
140pub use candela_vm::GcStats;
142pub use candela_vm::PoolStats;
143pub use candela_vm::dylib_dirs;
147pub use candela_vm::set_dylib_dirs;
148pub use candela_vm::base_dir;
151pub use candela_vm::set_base_dir;
152pub use candela_vm::EventWaker;
154pub use candela_vm::set_event_waker;
155#[cfg(feature = "compiler")]
158pub use build::build_bytecode;
159#[cfg(feature = "compiler")]
161pub use build::BuildOptions;
162#[cfg(feature = "compiler")]
163pub use build::NativeCode;
164#[cfg(feature = "compiler")]
165pub use build::build_artifact;
166#[cfg(feature = "compiler")]
171pub use trampoline::compile_checked;
172#[cfg(feature = "compiler")]
175pub use trampoline::check_only;
176#[cfg(feature = "compiler")]
178pub use warnings::collect_warnings;
179#[cfg(feature = "compiler")]
184pub use compiler::compiler_data::TypeNames;
185
186#[cfg(feature = "compiler")]
190pub(crate) fn execute_compiled(out: compiler::CompileOutput) {
191 out.require_main();
193 let compiler::CompileOutput {
194 instructions,
195 registers,
196 mut pools,
197 instr_src,
198 callsite_registers,
199 dyn_lib_fns,
200 structs,
201 enums,
202 allocated_arg_count,
203 allocated_call_depth,
204 sources,
205 event_entry,
206 ..
207 } = out;
208 let err_ctx = ErrorCtx {
209 instr_src: &instr_src,
210 sources: &sources,
211 };
212 let mut registers = RegisterFile(registers);
213 let mut run = |registers: &mut RegisterFile, start: usize| {
214 if let Err(error) = vm::execute(
215 &instructions,
216 registers,
217 &mut pools,
218 &err_ctx,
219 &callsite_registers,
220 &dyn_lib_fns,
221 &structs,
222 &enums,
223 allocated_arg_count,
224 allocated_call_depth,
225 &[],
226 &[],
227 start,
228 None,
229 ) {
230 error.report(&err_ctx);
231 }
232 };
233 run(&mut registers, 0);
234 #[cfg(not(target_arch = "wasm32"))]
238 if let Some(entry) = event_entry {
239 registers.0[entry.arg_registers[0] as usize] = data::Data::int(-1);
240 registers.0[entry.arg_registers[1] as usize] = data::TRUE;
241 run(&mut registers, entry.entry as usize);
242 }
243 #[cfg(target_arch = "wasm32")]
244 let _ = event_entry;
245}
246
247#[cfg(target_arch = "wasm32")]
248#[wasm_bindgen]
249pub fn get_output() -> String {
250 candela_vm::captured_output::CAPTURED_OUTPUT.with(|o| o.take())
251}
252
253#[cfg(all(target_arch = "wasm32", feature = "compiler"))]
254#[wasm_bindgen]
255pub fn run(code: String) {
256 candela_vm::captured_output::CAPTURED_OUTPUT.with(|o| o.borrow_mut().clear());
257 execute_compiled(compile(
258 code,
259 "playground.cdl",
260 false,
261 &ImportResolver::new(),
262 ));
263}
264
265#[cfg(all(feature = "embed", feature = "compiler"))]
266#[unsafe(no_mangle)]
267#[allow(clippy::missing_safety_doc)] pub unsafe extern "C" fn candela_run(code: *const c_char) -> *mut c_char {
269 std::panic::set_hook(Box::new(|_| {}));
270 let code = unsafe { CStr::from_ptr(code) }
271 .to_string_lossy()
272 .to_string();
273 candela_vm::captured_output::CAPTURED_OUTPUT.with(|o| o.borrow_mut().clear());
274 let was_capturing = candela_vm::captured_output::set_capturing(true);
277 let _ = catch_unwind(|| {
278 execute_compiled(compile(code, "embedded.cdl", false, &ImportResolver::new()));
279 });
280 candela_vm::captured_output::set_capturing(was_capturing);
281 let output = candela_vm::captured_output::CAPTURED_OUTPUT.with(|o| o.take());
282 CString::new(output).unwrap_or_default().into_raw()
283}
284
285#[cfg(all(feature = "embed", feature = "compiler"))]
286#[unsafe(no_mangle)]
287#[allow(clippy::missing_safety_doc)] pub unsafe extern "C" fn candela_free_output(output: *mut c_char) {
289 if !output.is_null() {
290 #[allow(unused_must_use)]
291 unsafe {
292 CString::from_raw(output)
293 };
294 }
295}
296
297#[cfg(all(feature = "compiler", not(target_arch = "wasm32")))]
299#[must_use]
300pub fn main() -> std::process::ExitCode {
301 #[cfg(not(debug_assertions))]
302 std::panic::set_hook(Box::new(|info| {
303 eprintln!("{RED}CANDELA ERROR{RESET}\n{info}");
304 }));
305
306 cli::main()
307}