quickjs_runtime/quickjs_utils/
mod.rs

1//! low level contains utils for calling the quickjs api
2
3use crate::quickjsruntimeadapter::QuickJsRuntimeAdapter;
4
5#[cfg(feature = "bellard")]
6pub mod class_ids {
7    pub const JS_CLASS_OBJECT: u32 = 1;
8    pub const JS_CLASS_ARRAY: u32 = 2;
9    pub const JS_CLASS_ERROR: u32 = 3;
10    pub const JS_CLASS_NUMBER: u32 = 4;
11    pub const JS_CLASS_STRING: u32 = 5;
12    pub const JS_CLASS_BOOLEAN: u32 = 6;
13    pub const JS_CLASS_SYMBOL: u32 = 7;
14    pub const JS_CLASS_ARGUMENTS: u32 = 8;
15    pub const JS_CLASS_MAPPED_ARGUMENTS: u32 = 9;
16    pub const JS_CLASS_DATE: u32 = 10;
17    pub const JS_CLASS_MODULE_NS: u32 = 11;
18    pub const JS_CLASS_C_FUNCTION: u32 = 12;
19    pub const JS_CLASS_BYTECODE_FUNCTION: u32 = 13;
20    pub const JS_CLASS_BOUND_FUNCTION: u32 = 14;
21    pub const JS_CLASS_C_FUNCTION_DATA: u32 = 15;
22    pub const JS_CLASS_GENERATOR_FUNCTION: u32 = 16;
23    pub const JS_CLASS_FOR_IN_ITERATOR: u32 = 17;
24    pub const JS_CLASS_REGEXP: u32 = 18;
25    pub const JS_CLASS_ARRAY_BUFFER: u32 = 19;
26    pub const JS_CLASS_SHARED_ARRAY_BUFFER: u32 = 20;
27    pub const JS_CLASS_UINT8C_ARRAY: u32 = 21;
28    pub const JS_CLASS_INT8_ARRAY: u32 = 22;
29    pub const JS_CLASS_UINT8_ARRAY: u32 = 23;
30    pub const JS_CLASS_INT16_ARRAY: u32 = 24;
31    pub const JS_CLASS_UINT16_ARRAY: u32 = 25;
32    pub const JS_CLASS_INT32_ARRAY: u32 = 26;
33    pub const JS_CLASS_UINT32_ARRAY: u32 = 27;
34    pub const JS_CLASS_BIG_INT64_ARRAY: u32 = 28;
35    pub const JS_CLASS_BIG_UINT64_ARRAY: u32 = 29;
36    pub const JS_CLASS_FLOAT16_ARRAY: u32 = 30;
37    pub const JS_CLASS_FLOAT32_ARRAY: u32 = 31;
38    pub const JS_CLASS_FLOAT64_ARRAY: u32 = 32;
39    pub const JS_CLASS_DATAVIEW: u32 = 33;
40    pub const JS_CLASS_BIG_INT: u32 = 34;
41    pub const JS_CLASS_MAP: u32 = 35;
42    pub const JS_CLASS_SET: u32 = 36;
43    pub const JS_CLASS_WEAKMAP: u32 = 37;
44    pub const JS_CLASS_WEAKSET: u32 = 38;
45    pub const JS_CLASS_MAP_ITERATOR: u32 = 39;
46    pub const JS_CLASS_SET_ITERATOR: u32 = 40;
47    pub const JS_CLASS_ARRAY_ITERATOR: u32 = 41;
48    pub const JS_CLASS_STRING_ITERATOR: u32 = 42;
49    pub const JS_CLASS_REGEXP_STRING_ITERATOR: u32 = 43;
50    pub const JS_CLASS_GENERATOR: u32 = 44;
51    pub const JS_CLASS_PROXY: u32 = 45;
52    pub const JS_CLASS_PROMISE: u32 = 46;
53    pub const JS_CLASS_PROMISE_RESOLVE_FUNCTION: u32 = 47;
54    pub const JS_CLASS_PROMISE_REJECT_FUNCTION: u32 = 48;
55    pub const JS_CLASS_ASYNC_FUNCTION: u32 = 49;
56    pub const JS_CLASS_ASYNC_FUNCTION_RESOLVE: u32 = 50;
57    pub const JS_CLASS_ASYNC_FUNCTION_REJECT: u32 = 51;
58    pub const JS_CLASS_ASYNC_FROM_SYNC_ITERATOR: u32 = 52;
59    pub const JS_CLASS_ASYNC_GENERATOR_FUNCTION: u32 = 53;
60    pub const JS_CLASS_ASYNC_GENERATOR: u32 = 54;
61    pub const JS_CLASS_WEAK_REF: u32 = 55;
62    pub const JS_CLASS_FINALIZATION_REGISTRY: u32 = 56;
63    pub const JS_CLASS_INIT_COUNT: u32 = 57;
64}
65#[cfg(feature = "quickjs-ng")]
66pub mod class_ids {
67    pub const JS_CLASS_OBJECT: u32 = 1;
68    pub const JS_CLASS_ARRAY: u32 = 2;
69    pub const JS_CLASS_ERROR: u32 = 3;
70    pub const JS_CLASS_NUMBER: u32 = 4;
71    pub const JS_CLASS_STRING: u32 = 5;
72    pub const JS_CLASS_BOOLEAN: u32 = 6;
73    pub const JS_CLASS_SYMBOL: u32 = 7;
74    pub const JS_CLASS_ARGUMENTS: u32 = 8;
75    pub const JS_CLASS_MAPPED_ARGUMENTS: u32 = 9;
76    pub const JS_CLASS_DATE: u32 = 10;
77    pub const JS_CLASS_MODULE_NS: u32 = 11;
78    pub const JS_CLASS_C_FUNCTION: u32 = 12;
79    pub const JS_CLASS_BYTECODE_FUNCTION: u32 = 13;
80    pub const JS_CLASS_BOUND_FUNCTION: u32 = 14;
81    pub const JS_CLASS_C_FUNCTION_DATA: u32 = 15;
82    pub const JS_CLASS_C_CLOSURE: u32 = 16;
83    pub const JS_CLASS_GENERATOR_FUNCTION: u32 = 17;
84    pub const JS_CLASS_FOR_IN_ITERATOR: u32 = 18;
85    pub const JS_CLASS_REGEXP: u32 = 19;
86    pub const JS_CLASS_ARRAY_BUFFER: u32 = 20;
87    pub const JS_CLASS_SHARED_ARRAY_BUFFER: u32 = 21;
88    pub const JS_CLASS_UINT8C_ARRAY: u32 = 22;
89    pub const JS_CLASS_INT8_ARRAY: u32 = 23;
90    pub const JS_CLASS_UINT8_ARRAY: u32 = 24;
91    pub const JS_CLASS_INT16_ARRAY: u32 = 25;
92    pub const JS_CLASS_UINT16_ARRAY: u32 = 26;
93    pub const JS_CLASS_INT32_ARRAY: u32 = 27;
94    pub const JS_CLASS_UINT32_ARRAY: u32 = 28;
95    pub const JS_CLASS_BIG_INT64_ARRAY: u32 = 29;
96    pub const JS_CLASS_BIG_UINT64_ARRAY: u32 = 30;
97    pub const JS_CLASS_FLOAT16_ARRAY: u32 = 31;
98    pub const JS_CLASS_FLOAT32_ARRAY: u32 = 32;
99    pub const JS_CLASS_FLOAT64_ARRAY: u32 = 33;
100    pub const JS_CLASS_DATAVIEW: u32 = 34;
101    pub const JS_CLASS_BIG_INT: u32 = 35;
102    pub const JS_CLASS_MAP: u32 = 36;
103    pub const JS_CLASS_SET: u32 = 37;
104    pub const JS_CLASS_WEAKMAP: u32 = 38;
105    pub const JS_CLASS_WEAKSET: u32 = 39;
106    pub const JS_CLASS_ITERATOR: u32 = 40;
107    pub const JS_CLASS_ITERATOR_CONCAT: u32 = 41;
108    pub const JS_CLASS_ITERATOR_HELPER: u32 = 42;
109    pub const JS_CLASS_ITERATOR_WRAP: u32 = 43;
110    pub const JS_CLASS_MAP_ITERATOR: u32 = 44;
111    pub const JS_CLASS_SET_ITERATOR: u32 = 45;
112    pub const JS_CLASS_ARRAY_ITERATOR: u32 = 46;
113    pub const JS_CLASS_STRING_ITERATOR: u32 = 47;
114    pub const JS_CLASS_REGEXP_STRING_ITERATOR: u32 = 48;
115    pub const JS_CLASS_GENERATOR: u32 = 49;
116    pub const JS_CLASS_PROXY: u32 = 50;
117    pub const JS_CLASS_PROMISE: u32 = 51;
118    pub const JS_CLASS_PROMISE_RESOLVE_FUNCTION: u32 = 52;
119    pub const JS_CLASS_PROMISE_REJECT_FUNCTION: u32 = 53;
120    pub const JS_CLASS_ASYNC_FUNCTION: u32 = 54;
121    pub const JS_CLASS_ASYNC_FUNCTION_RESOLVE: u32 = 55;
122    pub const JS_CLASS_ASYNC_FUNCTION_REJECT: u32 = 56;
123    pub const JS_CLASS_ASYNC_FROM_SYNC_ITERATOR: u32 = 57;
124    pub const JS_CLASS_ASYNC_GENERATOR_FUNCTION: u32 = 58;
125    pub const JS_CLASS_ASYNC_GENERATOR: u32 = 59;
126    pub const JS_CLASS_WEAK_REF: u32 = 60;
127    pub const JS_CLASS_FINALIZATION_REGISTRY: u32 = 61;
128    pub const JS_CLASS_DOM_EXCEPTION: u32 = 62;
129    pub const JS_CLASS_CALL_SITE: u32 = 63;
130    pub const JS_CLASS_INIT_COUNT: u32 = 64;
131}
132
133pub mod arrays;
134pub mod atoms;
135pub mod bigints;
136pub mod compile;
137pub mod dates;
138pub mod errors;
139pub mod functions;
140pub mod interrupthandler;
141pub mod iterators;
142pub mod json;
143pub mod maps;
144pub mod modules;
145pub mod objects;
146pub mod primitives;
147pub mod promises;
148pub mod properties;
149pub mod runtime;
150pub mod sets;
151pub mod typedarrays;
152
153use crate::jsutils::JsError;
154use crate::quickjs_utils::atoms::JSAtomRef;
155use crate::quickjs_utils::objects::get_property;
156use crate::quickjsrealmadapter::QuickJsRealmAdapter;
157use crate::quickjsvalueadapter::{QuickJsValueAdapter, TAG_NULL, TAG_UNDEFINED};
158use libquickjs_sys as q;
159
160// todo
161// runtime and context in thread_local here
162// all function (where applicable) get an Option<QuickJSRuntime> which if None will be gotten from the thread_local
163// every function which returns a q::JSValue will return a OwnedValueRef to ensure values are freed on drop
164
165pub fn gc(q_js_rt: &QuickJsRuntimeAdapter) {
166    log::trace!("GC called");
167    unsafe { q::JS_RunGC(q_js_rt.runtime) }
168    log::trace!("GC done");
169}
170
171pub fn new_undefined_ref() -> QuickJsValueAdapter {
172    QuickJsValueAdapter::new_no_context(new_undefined(), "new_undefined_ref")
173}
174
175pub fn new_null() -> q::JSValue {
176    q::JSValue {
177        u: q::JSValueUnion { int32: 0 },
178        tag: TAG_NULL,
179    }
180}
181
182pub fn new_undefined() -> q::JSValue {
183    q::JSValue {
184        u: q::JSValueUnion { int32: 0 },
185        tag: TAG_UNDEFINED,
186    }
187}
188
189pub fn new_null_ref() -> QuickJsValueAdapter {
190    QuickJsValueAdapter::new_no_context(new_null(), "null_ref")
191}
192
193/// get the current filename
194pub fn get_script_or_module_name_q(ctx: &QuickJsRealmAdapter) -> Result<String, JsError> {
195    unsafe { get_script_or_module_name(ctx.context) }
196}
197
198/// get the current filename
199/// # Safety
200/// ensure the QuickJsContext has not been dropped
201pub unsafe fn get_script_or_module_name(context: *mut q::JSContext) -> Result<String, JsError> {
202    for x in 0..100 {
203        let atom = q::JS_GetScriptOrModuleName(context, x);
204        let atom_ref = JSAtomRef::new(context, atom);
205        let r = atoms::to_string(context, &atom_ref)?;
206        if !r.is_empty() {
207            return Ok(r);
208        }
209    }
210    Ok("".to_string())
211}
212
213pub fn get_global_q(context: &QuickJsRealmAdapter) -> QuickJsValueAdapter {
214    unsafe { get_global(context.context) }
215}
216/// # Safety
217/// When passing a context pointer please make sure the corresponding QuickJsContext is still valid
218pub unsafe fn get_global(context: *mut q::JSContext) -> QuickJsValueAdapter {
219    let global = q::JS_GetGlobalObject(context);
220    QuickJsValueAdapter::new(context, global, false, true, "global")
221}
222/// # Safety
223/// When passing a context pointer please make sure the corresponding QuickJsContext is still valid
224pub unsafe fn get_constructor(
225    context: *mut q::JSContext,
226    constructor_name: &str,
227) -> Result<QuickJsValueAdapter, JsError> {
228    let global_ref = get_global(context);
229
230    let constructor_ref = get_property(context, &global_ref, constructor_name)?;
231
232    if constructor_ref.is_null_or_undefined() {
233        Err(JsError::new_string(format!(
234            "not found: {constructor_name}"
235        )))
236    } else {
237        Ok(constructor_ref)
238    }
239}
240/// Calculate a runtimes memory usage
241/// # Safety
242/// runtime ref should be a valid existing runtime
243pub unsafe fn get_memory_usage(runtime: *mut q::JSRuntime) -> q::JSMemoryUsage {
244    let mut mu = q::JSMemoryUsage {
245        malloc_size: 0,
246        malloc_limit: 0,
247        memory_used_size: 0,
248        malloc_count: 0,
249        memory_used_count: 0,
250        atom_count: 0,
251        atom_size: 0,
252        str_count: 0,
253        str_size: 0,
254        obj_count: 0,
255        obj_size: 0,
256        prop_count: 0,
257        prop_size: 0,
258        shape_count: 0,
259        shape_size: 0,
260        js_func_count: 0,
261        js_func_size: 0,
262        js_func_code_size: 0,
263        js_func_pc2line_count: 0,
264        js_func_pc2line_size: 0,
265        c_func_count: 0,
266        array_count: 0,
267        fast_array_count: 0,
268        fast_array_elements: 0,
269        binary_object_count: 0,
270        binary_object_size: 0,
271    };
272    q::JS_ComputeMemoryUsage(runtime, &mut mu);
273
274    mu
275}
276
277/// # Safety
278/// When passing a context pointer please make sure the corresponding QuickJsContext is still valid
279pub unsafe fn parse_args(
280    context: *mut q::JSContext,
281    argc: ::std::os::raw::c_int,
282    argv: *mut q::JSValue,
283) -> Vec<QuickJsValueAdapter> {
284    let arg_slice = std::slice::from_raw_parts(argv, argc as usize);
285    arg_slice
286        .iter()
287        .map(|raw| QuickJsValueAdapter::new(context, *raw, true, true, "quickjs_utils::parse_args"))
288        .collect::<Vec<_>>()
289}
290
291#[cfg(test)]
292pub mod tests {
293    use crate::facades::tests::init_test_rt;
294    use crate::jsutils::Script;
295    use crate::quickjs_utils::{get_global_q, get_script_or_module_name_q};
296    use crate::values::JsValueConvertable;
297
298    #[test]
299    fn test_global() {
300        let rt = init_test_rt();
301        rt.exe_rt_task_in_event_loop(|q_js_rt| {
302            let q_ctx = q_js_rt.get_main_realm();
303
304            #[cfg(feature = "bellard")]
305            let ct = get_global_q(q_ctx).get_ref_count();
306            for _ in 0..5 {
307                let _global = get_global_q(q_ctx);
308                #[cfg(feature = "bellard")]
309                assert_eq!(_global.get_ref_count(), ct);
310            }
311        });
312    }
313
314    #[test]
315    fn test_script_name() {
316        let rt = init_test_rt();
317        rt.set_function(&[], "testName", |q_ctx, _args| {
318            let res = get_script_or_module_name_q(q_ctx)?.to_js_value_facade();
319            Ok(res)
320        })
321        .ok()
322        .expect("func set failed");
323        let name_esvf = rt
324            .eval_sync(
325                None,
326                Script::new("the_name.es", "(function(){return(testName());}())"),
327            )
328            .ok()
329            .expect("script failed");
330        assert_eq!(name_esvf.get_str(), "the_name.es");
331        let name_esvf = rt
332            .eval_sync(
333                None,
334                Script::new("https://githubstuff.org/tes.js", "(testName())"),
335            )
336            .ok()
337            .expect("script failed");
338        assert_eq!(name_esvf.get_str(), "https://githubstuff.org/tes.js");
339    }
340}