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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
use std::{
    convert::TryFrom,
    ffi::CString,
    marker::PhantomData,
    os::raw::{c_int, c_void},
    panic::RefUnwindSafe,
    sync::Mutex,
};

use libquickjs_sys as q;

use crate::{ContextError, ExecutionError, JsValue, ValueError};

// JS_TAG_* constants from quickjs.
// For some reason bindgen does not pick them up.
const TAG_STRING: i64 = -7;
const TAG_OBJECT: i64 = -1;
const TAG_INT: i64 = 0;
const TAG_BOOL: i64 = 1;
const TAG_NULL: i64 = 2;
const TAG_UNDEFINED: i64 = 3;
const TAG_EXCEPTION: i64 = 6;
const TAG_FLOAT64: i64 = 7;

/// Free a JSValue.
/// This function is the equivalent of JS_FreeValue from quickjs, which can not
/// be used due to being `static inline`.
unsafe fn free_value(context: *mut q::JSContext, value: q::JSValue) {
    // All tags < 0 are garbage collected and need to be freed.
    if value.tag < 0 {
        // This transmute is OK since if tag < 0, the union will be a refcount
        // pointer.
        let ptr = std::mem::transmute::<_, *mut q::JSRefCountHeader>(value.u.ptr);
        let pref: &mut q::JSRefCountHeader = &mut *ptr;
        pref.ref_count -= 1;
        if pref.ref_count <= 0 {
            q::__JS_FreeValue(context, value);
        }
    }
}

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

/// The Callback trait is implemented for functions/closures that can be
/// used as callbacks in the JS runtime.
pub trait Callback<F>: RefUnwindSafe {
    fn argument_count(&self) -> usize;
    fn call(&self, args: Vec<JsValue>) -> Result<Result<JsValue, String>, ValueError>;
}

// Implement Callback for Fn(A) -> R functions.
impl<R, F> Callback<PhantomData<(&R, &F)>> for F
where
    R: Into<JsValue>,
    F: Fn() -> R + Sized + RefUnwindSafe,
{
    fn argument_count(&self) -> usize {
        0
    }

    fn call(&self, args: Vec<JsValue>) -> Result<Result<JsValue, String>, ValueError> {
        if args.len() != 0 {
            return Ok(Err(format!(
                "Invalid argument count: Expected 0, got {}",
                args.len()
            )));
        }

        let res = self().into();
        Ok(Ok(res))
    }
}

// Implement Callback for Fn(A) -> R functions.
impl<A1, R, F> Callback<PhantomData<(&A1, &R, &F)>> for F
where
    A1: TryFrom<JsValue, Error = ValueError>,
    R: Into<JsValue>,
    F: Fn(A1) -> R + Sized + RefUnwindSafe,
{
    fn argument_count(&self) -> usize {
        1
    }
    fn call(&self, args: Vec<JsValue>) -> Result<Result<JsValue, String>, ValueError> {
        if args.len() != 1 {
            return Ok(Err(format!(
                "Invalid argument count: Expected 1, got {}",
                args.len()
            )));
        }

        let arg_raw = args.into_iter().next().expect("Invalid argument count");
        let arg = A1::try_from(arg_raw)?;
        let res = self(arg).into();
        Ok(Ok(res))
    }
}

// Implement Callback for Fn(A1, A2) -> R functions.
impl<A1, A2, R, F> Callback<PhantomData<(&A1, &A2, &R, &F)>> for F
where
    A1: TryFrom<JsValue, Error = ValueError>,
    A2: TryFrom<JsValue, Error = ValueError>,
    R: Into<JsValue>,
    F: Fn(A1, A2) -> R + Sized + RefUnwindSafe,
{
    fn argument_count(&self) -> usize {
        1
    }

    fn call(&self, args: Vec<JsValue>) -> Result<Result<JsValue, String>, ValueError> {
        if args.len() != 2 {
            return Ok(Err(format!(
                "Invalid argument count: Expected 2, got {}",
                args.len()
            )));
        }

        let mut iter = args.into_iter();
        let arg1_raw = iter.next().expect("Invalid argument count");
        let arg1 = A1::try_from(arg1_raw)?;

        let arg2_raw = iter.next().expect("Invalid argument count");
        let arg2 = A2::try_from(arg2_raw)?;

        let res = self(arg1, arg2).into();
        Ok(Ok(res))
    }
}

// Implement Callback for Fn(A1, A2, A3) -> R functions.
impl<A1, A2, A3, R, F> Callback<PhantomData<(&A1, &A2, &A3, &R, &F)>> for F
where
    A1: TryFrom<JsValue, Error = ValueError>,
    A2: TryFrom<JsValue, Error = ValueError>,
    A3: TryFrom<JsValue, Error = ValueError>,
    R: Into<JsValue>,
    F: Fn(A1, A2, A3) -> R + Sized + RefUnwindSafe,
{
    fn argument_count(&self) -> usize {
        1
    }

    fn call(&self, args: Vec<JsValue>) -> Result<Result<JsValue, String>, ValueError> {
        if args.len() != 3 {
            return Ok(Err(format!(
                "Invalid argument count: Expected 3, got {}",
                args.len()
            )));
        }

        let mut iter = args.into_iter();
        let arg1_raw = iter.next().expect("Invalid argument count");
        let arg1 = A1::try_from(arg1_raw)?;

        let arg2_raw = iter.next().expect("Invalid argument count");
        let arg2 = A2::try_from(arg2_raw)?;

        let arg3_raw = iter.next().expect("Invalid argument count");
        let arg3 = A3::try_from(arg3_raw)?;

        let res = self(arg1, arg2, arg3).into();
        Ok(Ok(res))
    }
}

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.
///
/// TODO: use catch_unwind to prevent pancis.
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 = (*data).u.ptr;
        let closure: &mut F = &mut *(closure_ptr as *mut F);
        (*closure)(argc, argv)
    }

    let boxed_f = Box::new(closure);

    let data = Box::new(q::JSValue {
        u: q::JSValueUnion {
            ptr: (&*boxed_f) as *const F as *mut c_void,
        },
        tag: TAG_NULL,
    });

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

/// OwnedValueRef wraps a Javascript value from the quickjs runtime.
/// It prevents leaks by ensuring that the inner value is deallocated on drop.
pub struct OwnedValueRef<'a> {
    context: &'a ContextWrapper,
    value: q::JSValue,
}

impl<'a> Drop for OwnedValueRef<'a> {
    fn drop(&mut self) {
        unsafe {
            free_value(self.context.context, self.value);
        }
    }
}

impl<'a> OwnedValueRef<'a> {
    pub fn new(context: &'a ContextWrapper, value: q::JSValue) -> Self {
        Self { context, value }
    }

    /// Get the inner JSValue without freeing in drop.
    ///
    /// Unsafe because the caller is responsible for freeing the value.
    unsafe fn into_inner(mut self) -> q::JSValue {
        let v = self.value;
        self.value = q::JSValue {
            u: q::JSValueUnion { int32: 0 },
            tag: TAG_NULL,
        };
        v
    }

    pub fn is_exception(&self) -> bool {
        self.value.tag == TAG_EXCEPTION
    }

    pub fn is_object(&self) -> bool {
        self.value.tag == TAG_OBJECT
    }

    pub fn is_string(&self) -> bool {
        self.value.tag == TAG_STRING
    }

    pub fn to_string(&self) -> Result<String, ExecutionError> {
        let value = if self.is_string() {
            self.to_value()?
        } else {
            let raw = unsafe { q::JS_ToString(self.context.context, self.value) };
            let value = OwnedValueRef::new(self.context, raw);

            if value.value.tag != TAG_STRING {
                return Err(ExecutionError::Exception(
                    "Could not convert value to string".into(),
                ));
            }
            value.to_value()?
        };

        Ok(value.into_string().unwrap())
    }

    pub fn to_value(&self) -> Result<JsValue, ValueError> {
        self.context.to_value(&self.value)
    }
}

/// Wraps an object from the quickjs runtime.
/// Provides convenience property accessors.
pub struct OwnedObjectRef<'a> {
    value: OwnedValueRef<'a>,
}

impl<'a> OwnedObjectRef<'a> {
    pub fn new(value: OwnedValueRef<'a>) -> Result<Self, ValueError> {
        if value.value.tag != TAG_OBJECT {
            Err(ValueError::Internal("Expected an object".into()))
        } else {
            Ok(Self { value })
        }
    }

    pub fn property(&'a self, name: &str) -> Result<OwnedValueRef<'a>, ExecutionError> {
        let cname = make_cstring(name)?;
        let raw = unsafe {
            q::JS_GetPropertyStr(self.value.context.context, self.value.value, cname.as_ptr())
        };

        if raw.tag == TAG_EXCEPTION {
            Err(ExecutionError::Internal(format!(
                "Exception while getting property '{}'",
                name
            )))
        } else if raw.tag == TAG_UNDEFINED {
            Err(ExecutionError::Internal(format!(
                "Property '{}' not found",
                name
            )))
        } else {
            Ok(OwnedValueRef::new(self.value.context, raw))
        }
    }

    unsafe fn set_property_raw(&self, name: &str, value: q::JSValue) -> Result<(), ExecutionError> {
        let cname = make_cstring(name)?;
        let ret = q::JS_SetPropertyStr(
            self.value.context.context,
            self.value.value,
            cname.as_ptr(),
            value,
        );
        if ret < 0 {
            Err(ExecutionError::Exception("Could not set property".into()))
        } else {
            Ok(())
        }
    }

    // pub fn set_property(&self, name: &str, value: JsValue) -> Result<(), ExecutionError> {
    //     let qval = self.value.context.serialize_value(value)?;
    //     unsafe { self.set_property_raw(name, qval.value) }
    // }
}

/// Wraps a quickjs context.
///
/// Cleanup of the context happens in drop.
pub struct ContextWrapper {
    context: *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 {
            let rt = q::JS_GetRuntime(self.context);
            q::JS_FreeContext(self.context);
            q::JS_FreeRuntime(rt);
        }
    }
}

impl ContextWrapper {
    pub fn new() -> Result<Self, ContextError> {
        let rt = unsafe { q::JS_NewRuntime() };
        if rt.is_null() {
            return Err(ContextError::RuntimeCreationFailed);
        }
        let context = unsafe { q::JS_NewContext(rt) };
        if context.is_null() {
            return Err(ContextError::ContextCreationFailed);
        }

        Ok(Self {
            context,
            callbacks: Mutex::new(Vec::new()),
        })
    }

    /// Serialize a Rust value into a quickjs runtime value.
    pub fn serialize_value<'a>(&'a self, value: JsValue) -> Result<OwnedValueRef<'a>, ValueError> {
        let context = self.context;
        let v = match value {
            JsValue::Null => q::JSValue {
                u: q::JSValueUnion { int32: 0 },
                tag: TAG_NULL,
            },
            JsValue::Bool(flag) => q::JSValue {
                u: q::JSValueUnion {
                    int32: if flag { 1 } else { 0 },
                },
                tag: TAG_BOOL,
            },
            JsValue::Int(val) => q::JSValue {
                u: q::JSValueUnion { int32: val },
                tag: TAG_INT,
            },
            JsValue::Float(val) => q::JSValue {
                u: q::JSValueUnion { float64: val },
                tag: TAG_FLOAT64,
            },
            JsValue::String(val) => {
                let cstr = make_cstring(val)?;
                let qval = unsafe { q::JS_NewString(context, cstr.as_ptr()) };

                if qval.tag == TAG_EXCEPTION {
                    return Err(ValueError::Internal(
                        "Could not create string in runtime".into(),
                    ));
                }

                qval
            }
            JsValue::Array(values) => {
                // Allocate a new array in the runtime.
                let arr = unsafe { q::JS_NewArray(context) };
                if arr.tag == TAG_EXCEPTION {
                    return Err(ValueError::Internal(
                        "Could not create array in runtime".into(),
                    ));
                }

                for (index, value) in values.into_iter().enumerate() {
                    let qvalue = match self.serialize_value(value) {
                        Ok(qval) => qval,
                        Err(e) => {
                            // Make sure to free the array if a individual
                            // element fails.
                            unsafe {
                                free_value(context, arr);
                            }
                            return Err(e);
                        }
                    };

                    let ret = unsafe {
                        q::JS_DefinePropertyValueUint32(
                            context,
                            arr,
                            index as u32,
                            qvalue.value,
                            q::JS_PROP_C_W_E as i32,
                        )
                    };
                    if ret < 0 {
                        // Make sure to free the array if a individual
                        // element fails.
                        unsafe {
                            free_value(context, arr);
                        }
                        return Err(ValueError::Internal(
                            "Could not append element to array".into(),
                        ));
                    }
                }
                arr
            }
            JsValue::Object(map) => {
                let obj = unsafe { q::JS_NewObject(context) };
                if obj.tag == TAG_EXCEPTION {
                    return Err(ValueError::Internal("Could not create object".into()));
                }

                for (key, value) in map {
                    let ckey = make_cstring(key)?;

                    let qvalue = self.serialize_value(value).map_err(|e| {
                        // Free the object if a property failed.
                        unsafe {
                            free_value(context, obj);
                        }
                        e
                    })?;

                    let ret =
                        unsafe { q::JS_SetPropertyStr(context, obj, ckey.as_ptr(), qvalue.value) };
                    if ret < 0 {
                        // Free the object if a property failed.
                        unsafe {
                            free_value(context, obj);
                        }
                        return Err(ValueError::Internal(
                            "Could not add add property to object".into(),
                        ));
                    }
                }

                obj
            }
        };
        Ok(OwnedValueRef::new(self, v))
    }

    // Deserialize a quickjs runtime value into a Rust value.
    fn to_value(&self, value: &q::JSValue) -> Result<JsValue, ValueError> {
        let context = self.context;
        let r = value;

        match r.tag {
            // Int.
            TAG_INT => {
                let val = unsafe { r.u.int32 };
                Ok(JsValue::Int(val))
            }
            // Bool.
            TAG_BOOL => {
                let raw = unsafe { r.u.int32 };
                let val = raw > 0;
                Ok(JsValue::Bool(val))
            }
            // Null.
            TAG_NULL => Ok(JsValue::Null),
            // Undefined.
            TAG_UNDEFINED => Ok(JsValue::Null),
            // Float.
            TAG_FLOAT64 => {
                let val = unsafe { r.u.float64 };
                Ok(JsValue::Float(val))
            }
            // String.
            TAG_STRING => {
                let ptr = unsafe {
                    q::JS_ToCStringLen(context, std::ptr::null::<i32>() as *mut i32, *r, 0)
                };

                if ptr == std::ptr::null() {
                    return Err(ValueError::Internal(
                        "Could not convert string: got a null pointer".into(),
                    ));
                }

                let cstr = unsafe { std::ffi::CStr::from_ptr(ptr) };

                let s = cstr
                    .to_str()
                    .map_err(|e| ValueError::InvalidString(e))?
                    .to_string();

                // Free the c string.
                unsafe { q::JS_FreeCString(context, ptr) };

                Ok(JsValue::String(s))
            }
            // Object.
            TAG_OBJECT => {
                let is_array = unsafe { q::JS_IsArray(context, *r) } > 0;
                if is_array {
                    let length_name = make_cstring("length")?;

                    let len_value = unsafe {
                        let raw = q::JS_GetPropertyStr(context, *r, length_name.as_ptr());
                        let wrapped = OwnedValueRef::new(self, raw);
                        let len = wrapped.to_value()?;
                        len
                    };
                    let len = if let JsValue::Int(x) = len_value {
                        x
                    } else {
                        return Err(ValueError::Internal(
                            "Could not determine arrya length".into(),
                        ));
                    };

                    let mut values = Vec::new();
                    for index in 0..(len as usize) {
                        let value_raw =
                            unsafe { q::JS_GetPropertyUint32(context, *r, index as u32) };
                        let value_ref = OwnedValueRef::new(self, value_raw);
                        if value_ref.value.tag == TAG_EXCEPTION {
                            return Err(ValueError::Internal("Could not build array".into()));
                        }
                        let value = value_ref.to_value()?;
                        values.push(value);
                    }

                    Ok(JsValue::Array(values))
                } else {
                    Err(ValueError::Internal("Unsupported JS type: Object".into()))
                }
            }
            x => Err(ValueError::Internal(format!(
                "Unhandled JS_TAG value: {}",
                x
            ))),
        }
    }

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

    /// Get the last exception.
    /// NOTE: currently this is useless in eval, since the exception is
    /// swallowed.
    fn get_exception<'a>(&'a self) -> Result<OwnedValueRef<'a>, ExecutionError> {
        let raw = unsafe { q::JS_GetException(self.context) };
        let value = OwnedValueRef::new(self, raw);
        if value.is_exception() {
            Err(ExecutionError::Exception(
                "Could not get last exception".into(),
            ))
        } else {
            Ok(value)
        }
    }

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

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

        if value.is_exception() {
            let exception = self.get_exception().and_then(|e| match e.to_value() {
                Ok(e) => Ok(e),
                Err(_e) => e
                    .to_string()
                    .map(JsValue::String)
                    .map_err(|_| ExecutionError::Internal("Unknown exception".into())),
            })?;
            Err(ExecutionError::Exception(exception))
        } else {
            Ok(value)
        }
    }

    /// Call a JS function with the given arguments.
    pub fn call_function<'a>(
        &'a self,
        function: OwnedValueRef<'a>,
        args: Vec<OwnedValueRef<'a>>,
    ) -> Result<OwnedValueRef<'a>, ExecutionError> {
        let mut qargs = args.iter().map(|arg| arg.value).collect::<Vec<_>>();

        let n = q::JSValue {
            u: q::JSValueUnion { int32: 0 },
            tag: TAG_NULL,
        };

        let qres_raw = unsafe {
            q::JS_Call(
                self.context,
                function.value,
                n,
                qargs.len() as i32,
                qargs.as_mut_ptr(),
            )
        };
        let qres = OwnedValueRef::new(self, qres_raw);

        if qres.is_exception() {
            let exception = self
                .get_exception()
                .and_then(|e| e.to_value().map_err(ExecutionError::Conversion))
                .map_err(|_| ExecutionError::Internal("Unknown Exception".to_string()))?;
            Err(ExecutionError::Exception(exception))
        } else {
            Ok(qres)
        }
    }

    /// Helper for executing a callback closure.
    fn exec_callback<'a, F>(
        &'a self,
        argc: c_int,
        argv: *mut q::JSValue,
        callback: &impl Callback<F>,
    ) -> Result<OwnedValueRef<'a>, ExecutionError> {
        let result = std::panic::catch_unwind(|| {
            let arg_slice = unsafe { std::slice::from_raw_parts(argv, argc as usize) };

            let args = arg_slice
                .iter()
                .map(|raw| self.to_value(raw))
                .collect::<Result<Vec<_>, _>>()?;

            match callback.call(args) {
                Ok(Ok(result)) => {
                    let serialized = self.serialize_value(result)?;
                    Ok(serialized)
                }
                // TODO: better error reporting.
                Ok(Err(e)) => Err(ExecutionError::Internal(e.to_string())),
                Err(e) => Err(e.into()),
            }
        });

        match result {
            Ok(r) => r,
            Err(_e) => Err(ExecutionError::Internal(format!("Callback panicked!"))),
        }
    }

    /// Add a global JS function that is backed by a Rust function or closure.
    pub fn add_callback<'a, F>(
        &'a self,
        name: &str,
        callback: impl Callback<F> + 'static,
    ) -> Result<(), ExecutionError> {
        let self_ptr = self as *const ContextWrapper;

        let argcount = callback.argument_count() as i32;

        let wrapper = move |argc: c_int, argv: *mut q::JSValue| -> q::JSValue {
            let ctx: &ContextWrapper = unsafe { &*self_ptr };

            match ctx.exec_callback(argc, argv, &callback) {
                Ok(value) => unsafe { value.into_inner() },
                // TODO: better error reporting.
                Err(e) => {
                    let js_exception = ctx.serialize_value(e.to_string().into()).unwrap();
                    unsafe {
                        q::JS_Throw(ctx.context, js_exception.into_inner());
                    }

                    q::JSValue {
                        u: q::JSValueUnion { int32: 0 },
                        tag: TAG_EXCEPTION,
                    }
                }
            }
        };

        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 cfunc =
            unsafe { q::JS_NewCFunctionData(self.context, trampoline, argcount, 0, 1, data) };
        if cfunc.tag != TAG_OBJECT {
            return Err(ExecutionError::Internal("Could not create callback".into()));
        }

        let global = self.global()?;
        unsafe {
            global.set_property_raw(name, cfunc)?;
        }

        Ok(())
    }
}