1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#![allow(missing_docs)]

mod callback;
mod compile;
mod convert;
mod droppable_value;
mod module;
mod utils;
mod value;

use std::{
    ffi::CString,
    os::raw::{c_int, c_void},
    ptr::null_mut,
    sync::{Arc, Mutex},
};

use libquickjspp_sys as q;

use crate::{
    callback::{Arguments, Callback},
    console::ConsoleBackend,
    ContextError, ExecutionError, JsValue, ValueError,
};

pub use value::*;

pub use self::callback::*;
pub use self::module::{JSModuleLoaderFunc, JSModuleNormalizeFunc};
pub use self::utils::*;
use self::{
    module::{js_module_loader, js_module_normalize, ModuleLoader},
    utils::{ensure_no_excpetion, get_exception},
};

// JS_TAG_* constants from quickjs.
// For some reason bindgen does not pick them up.
#[cfg(feature = "bigint")]
const TAG_BIG_INT: u32 = q::JS_TAG_BIG_INT;
const TAG_STRING: u32 = q::JS_TAG_STRING;
const TAG_FUNCTION_BYTECODE: u32 = q::JS_TAG_FUNCTION_BYTECODE;
const TAG_OBJECT: u32 = q::JS_TAG_OBJECT;
const TAG_INT: u32 = q::JS_TAG_INT;
const TAG_BOOL: u32 = q::JS_TAG_BOOL;
const TAG_NULL: u32 = q::JS_TAG_NULL;
const TAG_UNDEFINED: u32 = q::JS_TAG_UNDEFINED;
const TAG_EXCEPTION: u32 = q::JS_TAG_EXCEPTION;
const TAG_FLOAT64: u32 = q::JS_TAG_FLOAT64;
const TAG_SYMBOL: u32 = q::JS_TAG_SYMBOL;

/// Helper for creating CStrings.
fn make_cstring(value: impl Into<Vec<u8>>) -> Result<CString, ValueError> {
    CString::new(value).map_err(ValueError::StringWithZeroBytes)
}

type WrappedCallback = dyn Fn(c_int, *mut q::JSValue) -> q::JSValue;

/// Taken from: https://s3.amazonaws.com/temp.michaelfbryan.com/callbacks/index.html
///
/// Create a C wrapper function for a Rust closure to enable using it as a
/// callback function in the Quickjs runtime.
///
/// Both the boxed closure and the boxed data are returned and must be stored
/// by the caller to guarantee they stay alive.
unsafe fn build_closure_trampoline<F>(
    closure: F,
) -> ((Box<WrappedCallback>, Box<q::JSValue>), q::JSCFunctionData)
where
    F: Fn(c_int, *mut q::JSValue) -> q::JSValue + 'static,
{
    unsafe extern "C" fn trampoline<F>(
        _ctx: *mut q::JSContext,
        _this: q::JSValue,
        argc: c_int,
        argv: *mut q::JSValue,
        _magic: c_int,
        data: *mut q::JSValue,
    ) -> q::JSValue
    where
        F: Fn(c_int, *mut q::JSValue) -> q::JSValue,
    {
        let closure_ptr = q::JS_VALUE_GET_PTR(*data);
        let closure: &mut F = &mut *(closure_ptr as *mut F);
        (*closure)(argc, argv)
    }

    let boxed_f = Box::new(closure);

    let data = Box::new(q::JS_NewPointer(
        TAG_NULL,
        (&*boxed_f) as *const F as *mut c_void,
    ));

    ((boxed_f, data), Some(trampoline::<F>))
}

/*
type ModuleInit = dyn Fn(*mut q::JSContext, *mut q::JSModuleDef);

thread_local! {
    static NATIVE_MODULE_INIT: RefCell<Option<Box<ModuleInit>>> = RefCell::new(None);
}

unsafe extern "C" fn native_module_init(
    ctx: *mut q::JSContext,
    m: *mut q::JSModuleDef,
) -> ::std::os::raw::c_int {
    NATIVE_MODULE_INIT.with(|init| {
        let init = init.replace(None).unwrap();
        init(ctx, m);
    });
    0
}
*/

/// Wraps a quickjs context.
///
/// Cleanup of the context happens in drop.
pub struct ContextWrapper {
    runtime: *mut q::JSRuntime,
    pub(crate) context: *mut q::JSContext,
    pub(crate) loop_context: Arc<Mutex<*mut q::JSContext>>,
    /// Stores callback closures and quickjs data pointers.
    /// This array is write-only and only exists to ensure the lifetime of
    /// the closure.
    // A Mutex is used over a RefCell because it needs to be unwind-safe.
    callbacks: Mutex<Vec<(Box<WrappedCallback>, Box<q::JSValue>)>>,
}

impl Drop for ContextWrapper {
    fn drop(&mut self) {
        unsafe {
            q::JS_FreeContext(self.context);
            q::JS_FreeRuntime(self.runtime);
        }
    }
}

impl ContextWrapper {
    /// Initialize a wrapper by creating a JSRuntime and JSContext.
    pub fn new(memory_limit: Option<usize>) -> Result<Self, ContextError> {
        let runtime = unsafe { q::JS_NewRuntime() };
        if runtime.is_null() {
            return Err(ContextError::RuntimeCreationFailed);
        }

        // Configure memory limit if specified.
        if let Some(limit) = memory_limit {
            unsafe {
                q::JS_SetMemoryLimit(runtime, limit as _);
            }
        }

        let context = unsafe { q::JS_NewContext(runtime) };
        if context.is_null() {
            unsafe {
                q::JS_FreeRuntime(runtime);
            }
            return Err(ContextError::ContextCreationFailed);
        }

        // Initialize the promise resolver helper code.
        // This code is needed by Self::resolve_value
        let wrapper = Self {
            runtime,
            context,
            loop_context: Arc::new(Mutex::new(null_mut())),
            callbacks: Mutex::new(Vec::new()),
        };

        Ok(wrapper)
    }

    // See console standard: https://console.spec.whatwg.org
    pub fn set_console(&self, backend: Box<dyn ConsoleBackend>) -> Result<(), ExecutionError> {
        use crate::console::Level;

        self.add_callback("__console_write", move |args: Arguments| {
            let mut args = args.into_vec();

            if args.len() > 1 {
                let level_raw = args.remove(0);

                let level_opt = level_raw.as_str().and_then(|v| match v {
                    "trace" => Some(Level::Trace),
                    "debug" => Some(Level::Debug),
                    "log" => Some(Level::Log),
                    "info" => Some(Level::Info),
                    "warn" => Some(Level::Warn),
                    "error" => Some(Level::Error),
                    _ => None,
                });

                if let Some(level) = level_opt {
                    backend.log(level, args);
                }
            }
        })?;

        self.eval(
            r#"
            globalThis.console = {
                trace: (...args) => {
                    globalThis.__console_write("trace", ...args);
                },
                debug: (...args) => {
                    globalThis.__console_write("debug", ...args);
                },
                log: (...args) => {
                    globalThis.__console_write("log", ...args);
                },
                info: (...args) => {
                    globalThis.__console_write("info", ...args);
                },
                warn: (...args) => {
                    globalThis.__console_write("warn", ...args);
                },
                error: (...args) => {
                    globalThis.__console_write("error", ...args);
                },
            };
        "#,
        )?;

        Ok(())
    }

    /// Reset the wrapper by creating a new context.
    pub fn reset(self) -> Result<Self, ContextError> {
        unsafe {
            q::JS_FreeContext(self.context);
        };
        self.callbacks.lock().unwrap().clear();
        let context = unsafe { q::JS_NewContext(self.runtime) };
        if context.is_null() {
            return Err(ContextError::ContextCreationFailed);
        }

        let mut s = self;
        s.context = context;
        Ok(s)
    }

    /// Get the global object.
    pub fn global(&self) -> Result<OwnedJsObject, ExecutionError> {
        let global_raw = unsafe { q::JS_GetGlobalObject(self.context) };
        let global_ref = OwnedJsValue::new(self.context, global_raw);
        let global = global_ref.try_into_object()?;
        Ok(global)
    }

    /// Execute the pending job in the event loop.
    pub fn execute_pending_job(&self) -> Result<(), ExecutionError> {
        let ctx = &mut *self.loop_context.lock().unwrap();
        unsafe {
            loop {
                let err = q::JS_ExecutePendingJob(self.runtime, ctx as *mut _);

                if err <= 0 {
                    if err < 0 {
                        ensure_no_excpetion(*ctx)?
                    }
                    break;
                }
            }
        }
        Ok(())
    }

    /// If the given value is a promise, run the event loop until it is
    /// resolved, and return the final value.
    fn resolve_value(&self, value: OwnedJsValue) -> Result<OwnedJsValue, ExecutionError> {
        if value.is_exception() {
            let err = get_exception(self.context)
                .unwrap_or_else(|| ExecutionError::Exception("Unknown exception".into()));
            Err(err)
        } else if value.is_object() {
            let obj = value.try_into_object()?;
            if obj.is_promise()? {
                self.eval(
                    r#"
                    // Values:
                    //   - undefined: promise not finished
                    //   - false: error ocurred, __promiseError is set.
                    //   - true: finished, __promiseSuccess is set.
                    var __promiseResult = 0;
                    var __promiseValue = 0;

                    var __resolvePromise = function(p) {
                        p
                            .then(value => {
                                __promiseResult = true;
                                __promiseValue = value;
                            })
                            .catch(e => {
                                __promiseResult = false;
                                __promiseValue = e;
                            });
                    }
                "#,
                )?;

                let global = self.global()?;
                let resolver = global
                    .property_require("__resolvePromise")?
                    .try_into_function()?;

                // Call the resolver code that sets the result values once
                // the promise resolves.
                resolver.call(vec![obj.into_value()])?;

                loop {
                    let flag = unsafe {
                        let wrapper_mut = self as *const Self as *mut Self;
                        let ctx_mut = &mut (*wrapper_mut).context;
                        q::JS_ExecutePendingJob(self.runtime, ctx_mut)
                    };
                    if flag < 0 {
                        let e = get_exception(self.context).unwrap_or_else(|| {
                            ExecutionError::Exception("Unknown exception".into())
                        });
                        return Err(e);
                    }

                    // Check if promise is finished.
                    let res_val = global.property_require("__promiseResult")?;
                    if res_val.is_bool() {
                        let ok = res_val.to_bool()?;
                        let value = global.property_require("__promiseValue")?;

                        if ok {
                            return self.resolve_value(value);
                        } else {
                            let err_msg = value.js_to_string()?;
                            return Err(ExecutionError::Exception(JsValue::String(err_msg)));
                        }
                    }
                }
            } else {
                Ok(obj.into_value())
            }
        } else {
            Ok(value)
        }
    }

    /// Evaluate javascript code.
    pub fn eval(&self, code: &str) -> Result<OwnedJsValue, ExecutionError> {
        let filename = "script.js";
        let filename_c = make_cstring(filename)?;
        let code_c = make_cstring(code)?;

        let value_raw = unsafe {
            q::JS_Eval(
                self.context,
                code_c.as_ptr(),
                code.len(),
                filename_c.as_ptr(),
                q::JS_EVAL_TYPE_GLOBAL as i32,
            )
        };
        let value = OwnedJsValue::new(self.context, value_raw);
        self.resolve_value(value)
    }

    /// Evaluate javascript module code.
    pub fn eval_module(&self, code: &str) -> Result<OwnedJsValue, ExecutionError> {
        let filename = "module.js";
        let filename_c = make_cstring(filename)?;
        let code_c = make_cstring(code)?;

        let value_raw = unsafe {
            q::JS_Eval(
                self.context,
                code_c.as_ptr(),
                code.len(),
                filename_c.as_ptr(),
                q::JS_EVAL_TYPE_MODULE as i32,
            )
        };
        let value = OwnedJsValue::new(self.context, value_raw);
        self.resolve_value(value)
    }

    /// Evaluate Javascript module
    pub fn run_module(&self, filename: &str) -> Result<(), ExecutionError> {
        let filename_c = make_cstring(filename)?;

        unsafe {
            q::JS_RunModule(
                self.context,
                ".\0".as_ptr() as *const i8,
                filename_c.as_ptr(),
            )
        };

        ensure_no_excpetion(self.context)?;

        Ok(())
    }

    pub fn set_module_loader(
        &self,
        module_loader_func: JSModuleLoaderFunc,
        module_normalize: Option<JSModuleNormalizeFunc>,
        opaque: *mut c_void,
    ) {
        let has_module_normalize = module_normalize.is_some();

        let module_loader = ModuleLoader {
            loader: module_loader_func,
            normalize: module_normalize,
            opaque,
        };

        let module_loader = Box::new(module_loader);
        let module_loader_ptr = Box::into_raw(module_loader);

        unsafe {
            if has_module_normalize {
                q::JS_SetModuleLoaderFunc(
                    self.runtime,
                    Some(js_module_normalize),
                    Some(js_module_loader),
                    module_loader_ptr as *mut c_void,
                );
            } else {
                q::JS_SetModuleLoaderFunc(
                    self.runtime,
                    None,
                    Some(js_module_loader),
                    module_loader_ptr as *mut c_void,
                );
            }
        }
    }

    /*
    /// Call a constructor function.
    fn call_constructor(
        & self,
        function: OwnedJsValue,
        args: Vec<OwnedJsValue>,
    ) -> Result<OwnedJsValue, ExecutionError> {
        let mut qargs = args.iter().map(|arg| arg.value).collect::<Vec<_>>();

        let value_raw = unsafe {
            q::JS_CallConstructor(
                self.context,
                function.value,
                qargs.len() as i32,
                qargs.as_mut_ptr(),
            )
        };
        let value = OwnedJsValue::new(self, value_raw);
        if value.is_exception() {
            let err = self
                .get_exception()
                .unwrap_or_else(|| ExecutionError::Exception("Unknown exception".into()));
            Err(err)
        } else {
            Ok(value)
        }
    }
    */

    /// Call a JS function with the given arguments.
    pub fn call_function(
        &self,
        function: JsFunction,
        args: Vec<OwnedJsValue>,
    ) -> Result<OwnedJsValue, ExecutionError> {
        let ret = function.call(args)?;
        self.resolve_value(ret)
    }

    /// Create a wrapped callback function.
    pub fn create_callback<'a, F>(
        &self,
        callback: impl Callback<F> + 'static,
    ) -> Result<JsFunction, ExecutionError> {
        let argcount = callback.argument_count() as i32;

        let context = self.context;
        let wrapper = move |argc: c_int, argv: *mut q::JSValue| -> q::JSValue {
            match exec_callback(context, argc, argv, &callback) {
                Ok(value) => value,
                // TODO: better error reporting.
                Err(e) => {
                    let js_exception_value = match e {
                        ExecutionError::Exception(e) => e,
                        other => other.to_string().into(),
                    };
                    let js_exception =
                        convert::serialize_value(context, js_exception_value).unwrap();
                    unsafe {
                        q::JS_Throw(context, js_exception);
                    }

                    unsafe { q::JS_NewSpecialValue(TAG_EXCEPTION, 0) }
                }
            }
        };

        let (pair, trampoline) = unsafe { build_closure_trampoline(wrapper) };
        let data = (&*pair.1) as *const q::JSValue as *mut q::JSValue;
        self.callbacks.lock().unwrap().push(pair);

        let obj = unsafe {
            let f = q::JS_NewCFunctionData(self.context, trampoline, argcount, 0, 1, data);
            OwnedJsValue::new(self.context, f)
        };

        let f = obj.try_into_function()?;
        Ok(f)
    }

    /// Add a global JS function that is backed by a Rust function or closure.
    pub fn add_callback<'a, F>(
        &self,
        name: &str,
        callback: impl Callback<F> + 'static,
    ) -> Result<(), ExecutionError> {
        let cfunc = self.create_callback(callback)?;
        let global = self.global()?;
        global.set_property(name, cfunc.into_value())?;
        Ok(())
    }

    /// Create a raw callback function
    pub fn create_custom_callback(
        &self,
        callback: CustomCallback,
    ) -> Result<JsFunction, ExecutionError> {
        let context = self.context;
        let wrapper = move |argc: c_int, argv: *mut q::JSValue| -> q::JSValue {
            let result = std::panic::catch_unwind(|| {
                let arg_slice = unsafe { std::slice::from_raw_parts(argv, argc as usize) };
                match callback(context, arg_slice) {
                    Ok(Some(value)) => value,
                    Ok(None) => unsafe { q::JS_NewSpecialValue(TAG_UNDEFINED, 0) },
                    // TODO: better error reporting.
                    Err(e) => {
                        // TODO: should create an Error type.
                        let js_exception_value = e.to_string().into();
                        let js_exception =
                            convert::serialize_value(context, js_exception_value).unwrap();
                        unsafe {
                            q::JS_Throw(context, js_exception);
                        }

                        unsafe { q::JS_NewSpecialValue(TAG_EXCEPTION, 0) }
                    }
                }
            });

            match result {
                Ok(v) => v,
                Err(_) => {
                    // TODO: should create an Error type.
                    let js_exception_value =
                        ExecutionError::Internal("Callback panicked!".to_string())
                            .to_string()
                            .into();
                    let js_exception =
                        convert::serialize_value(context, js_exception_value).unwrap();
                    unsafe {
                        q::JS_Throw(context, js_exception);
                    }

                    unsafe { q::JS_NewSpecialValue(TAG_EXCEPTION, 0) }
                }
            }
        };

        let (pair, trampoline) = unsafe { build_closure_trampoline(wrapper) };
        let data = (&*pair.1) as *const q::JSValue as *mut q::JSValue;
        self.callbacks.lock().unwrap().push(pair);

        let obj = unsafe {
            let f = q::JS_NewCFunctionData(self.context, trampoline, 0, 0, 1, data);
            OwnedJsValue::new(self.context, f)
        };

        let f = obj.try_into_function()?;
        Ok(f)
    }
}