1mod coll;
7mod conc;
8pub use conc::report_unjoined_panics;
9mod core;
10mod data;
11pub mod fiber;
12mod net;
13mod script;
14mod sys;
15
16use std::io::Write;
17use std::sync::OnceLock;
18
19use catallaxy_runtime::class::class_of;
20use catallaxy_runtime::object::str_of;
21use catallaxy_runtime::ops;
22use catallaxy_runtime::ownership;
23use catallaxy_runtime::{panic, try_push, try_vec, RtResult, Trap, Value};
24use catallaxy_vm::{NativeFn, Vm};
25use parking_lot::Mutex;
26
27pub static ARGS: OnceLock<Vec<String>> = OnceLock::new();
29
30static STDOUT: Mutex<Option<std::io::BufWriter<std::io::Stdout>>> = Mutex::new(None);
31
32pub fn write_out(s: &str) {
34 let mut g = STDOUT.lock();
35 let w = g.get_or_insert_with(|| std::io::BufWriter::with_capacity(1 << 16, std::io::stdout()));
36 let _ = w.write_all(s.as_bytes());
37}
38
39pub fn flush() {
41 if let Some(w) = STDOUT.lock().as_mut() {
42 let _ = w.flush();
43 }
44}
45
46pub fn internal_error_message(payload: &(dyn std::any::Any + Send)) -> Option<String> {
50 let msg = if let Some(s) = payload.downcast_ref::<&'static str>() {
51 (*s).to_string()
52 } else {
53 payload.downcast_ref::<String>()?.clone()
54 };
55 Some(format!("internal error: {msg}"))
56}
57
58fn with_trace(mut msg: String, trace: &[String]) -> String {
60 for line in trace.iter().take(8) {
61 msg.push_str(&format!("\n at {line}"));
62 }
63 msg
64}
65
66static EXTENSIONS: std::sync::LazyLock<parking_lot::RwLock<rustc_hash::FxHashMap<String, NativeFn>>> =
67 std::sync::LazyLock::new(Default::default);
68
69pub fn register_native(name: &str, f: NativeFn) {
73 EXTENSIONS.write().insert(name.to_string(), f);
74}
75
76pub fn lookup(name: &str) -> Option<NativeFn> {
78 sys::init_clock();
81 core::lookup(name)
82 .or_else(|| coll::lookup(name))
83 .or_else(|| conc::lookup(name))
84 .or_else(|| sys::lookup(name))
85 .or_else(|| script::lookup(name))
86 .or_else(|| net::lookup(name))
87 .or_else(|| data::lookup(name))
88 .or_else(|| EXTENSIONS.read().get(name).copied())
89}
90
91pub(crate) fn opt(v: Value) -> Option<Value> {
94 if v.is_empty_marker() {
95 None
96 } else {
97 Some(v)
98 }
99}
100
101pub(crate) fn arg(args: &[Value], i: usize) -> Option<Value> {
102 args.get(i).copied().and_then(opt)
103}
104
105pub(crate) fn s<'a>(v: Value) -> RtResult<&'a str> {
106 match str_of(v) {
107 Some(s) => Ok(s),
108 None => panic(format!("TypeError: expected String, got {}", class_of(v).name)),
109 }
110}
111
112pub(crate) fn str_obj<'a>(v: Value) -> RtResult<&'a catallaxy_runtime::object::StrObj> {
114 catallaxy_runtime::object::str_obj_of(v)
115}
116
117pub(crate) fn int(v: Value) -> RtResult<i64> {
118 if v.is_small_int() {
119 return Ok(v.as_small_int());
120 }
121 if ops::is_int(v) {
122 return match ops::int_as_i64(v) {
123 Some(i) => Ok(i),
124 None => panic("RangeError: integer too large"),
125 };
126 }
127 panic(format!("TypeError: expected Int, got {}", class_of(v).name))
128}
129
130pub(crate) fn int_value(v: Value) -> RtResult<Value> {
132 if ops::is_int(v) {
133 Ok(v)
134 } else {
135 catallaxy_runtime::object::type_error("Int", v)
136 }
137}
138
139pub(crate) fn bool_value(v: Value) -> RtResult<Value> {
141 if v.is_bool() {
142 Ok(v)
143 } else {
144 catallaxy_runtime::object::type_error("Bool", v)
145 }
146}
147
148pub(crate) fn float(v: Value) -> RtResult<f64> {
149 if v.is_float() {
150 Ok(v.as_f64())
151 } else if ops::is_int(v) {
152 Ok(ops::int_to_f64(v))
153 } else {
154 panic(format!("TypeError: expected Float, got {}", class_of(v).name))
155 }
156}
157
158pub(crate) fn new_str(vm: &mut Vm, s: &str) -> Value {
159 vm.m.alloc_str(s)
160}
161
162pub(crate) fn new_list(vm: &mut Vm, items: Vec<Value>) -> RtResult<Value> {
165 vm.m.try_alloc_list(items)
166}
167
168pub(crate) fn new_str_list<'a>(vm: &mut Vm, parts: impl IntoIterator<Item = &'a str>) -> RtResult<Value> {
172 let parts = parts.into_iter();
173 let mut items = try_vec(parts.size_hint().0)?;
174 for p in parts {
175 let v = vm.m.try_alloc_str_nogc(p)?;
176 try_push(&mut items, v)?;
177 }
178 new_list(vm, items)
179}
180
181pub(crate) fn list_items(vm: &Vm, l: Value) -> RtResult<Vec<Value>> {
183 ownership::read_list(&vm.m, l)?.try_to_vec()
184}
185
186pub(crate) fn to_s(vm: &mut Vm, v: Value) -> RtResult<String> {
187 ops::to_s(vm, v)
188}
189
190pub(crate) fn ok(vm: &mut Vm, v: Value) -> Value {
191 catallaxy_vm::slow::make_result(vm, true, v)
192}
193
194pub(crate) fn err(vm: &mut Vm, msg: &str) -> Value {
195 let s = new_str(vm, msg);
196 catallaxy_vm::slow::make_result(vm, false, s)
197}
198
199pub(crate) fn io_err(vm: &mut Vm, path: &str, e: std::io::Error) -> RtResult<Value> {
202 if e.kind() == std::io::ErrorKind::OutOfMemory {
203 return catallaxy_runtime::refused(format!("cannot read {path} into memory"));
204 }
205 Ok(err(vm, &format!("{path}: {e}")))
206}
207
208pub(crate) fn exit_trap(code: i32) -> Trap {
209 Trap::Exit(code)
210}
211
212pub(crate) fn blocking<R>(vm: &Vm, f: impl FnOnce() -> R) -> R {
214 flush();
215 catallaxy_gc::blocking(&vm.m.rec, f)
216}