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