quickjs_rusty/
callback.rs

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
use std::ffi::{c_int, c_void};
use std::{convert::TryFrom, marker::PhantomData, panic::RefUnwindSafe};

use anyhow::Result;
use libquickjs_ng_sys as q;

use crate::utils::create_string;
use crate::utils::create_undefined;
use crate::ExecutionError;
use crate::OwnedJsValue;
use crate::ValueError;

pub trait IntoCallbackResult {
    fn into_callback_res(self, context: *mut q::JSContext) -> Result<OwnedJsValue, String>;
}

impl<T> IntoCallbackResult for T
where
    OwnedJsValue: From<(*mut q::JSContext, T)>,
{
    fn into_callback_res(self, context: *mut q::JSContext) -> Result<OwnedJsValue, String> {
        Ok((context, self).into())
    }
}

impl<T, E: std::fmt::Display> IntoCallbackResult for Result<T, E>
where
    OwnedJsValue: From<(*mut q::JSContext, T)>,
{
    fn into_callback_res(self, context: *mut q::JSContext) -> Result<OwnedJsValue, String> {
        match self {
            Ok(v) => Ok((context, v).into()),
            Err(e) => Err(e.to_string()),
        }
    }
}

/// The Callback trait is implemented for functions/closures that can be
/// used as callbacks in the JS runtime.
pub trait Callback<F>: RefUnwindSafe {
    /// Returns the number of required Javascript arguments.
    fn argument_count(&self) -> usize;

    /// Execute the callback.
    ///
    /// Should return:
    ///   - Err(_) if the JS values could not be converted
    ///   - Ok(Err(_)) if an error ocurred while processing.
    ///       The given error will be raised as a JS exception.
    ///   - Ok(Ok(result)) when execution succeeded.
    fn call(
        &self,
        context: *mut q::JSContext,
        args: Vec<OwnedJsValue>,
    ) -> Result<Result<OwnedJsValue, String>, ValueError>;
}

macro_rules! impl_callback {
    (@call $len:literal $self:ident $args:ident ) => {
        $self()
    };

    (@call $len:literal $self:ident $args:ident $( $arg:ident ),* ) => {
        {
            let mut iter = $args.into_iter();
            $self(
                $(
                    $arg::try_from(iter.next().unwrap())?,
                )*
            )
        }
    };

    [ $(  $len:literal : ( $( $arg:ident, )* ), )* ] => {
        $(

            impl<
                $( $arg, )*
                E,
                R,
                F,
            > Callback<PhantomData<(
                $( &$arg, )*
                &E,
                &R,
                &F,
            )>> for F
            where
                $( $arg: TryFrom<OwnedJsValue, Error = E>, )*
                ValueError: From<E>,
                R: IntoCallbackResult,
                F: Fn( $( $arg, )*  ) -> R + Sized + RefUnwindSafe,
            {
                fn argument_count(&self) -> usize {
                    $len
                }

                fn call(&self, context: *mut q::JSContext, args: Vec<OwnedJsValue>)
                    -> Result<Result<OwnedJsValue, String>, ValueError> {
                    if args.len() != $len {
                        return Ok(Err(format!(
                            "Invalid argument count: Expected {}, got {}",
                            self.argument_count(),
                            args.len()
                        )));
                    }

                    let res = impl_callback!(@call $len self args $($arg),* );
                    Ok(res.into_callback_res(context))
                }
            }
        )*
    };
}

impl<R, F> Callback<PhantomData<(&R, &F)>> for F
where
    R: IntoCallbackResult,
    F: Fn() -> R + Sized + RefUnwindSafe,
{
    fn argument_count(&self) -> usize {
        0
    }

    fn call(
        &self,
        context: *mut q::JSContext,
        args: Vec<OwnedJsValue>,
    ) -> Result<Result<OwnedJsValue, String>, ValueError> {
        if !args.is_empty() {
            return Ok(Err(format!(
                "Invalid argument count: Expected 0, got {}",
                args.len(),
            )));
        }

        let res = self();
        Ok(res.into_callback_res(context))
    }
}

impl_callback![
    1: (A1,),
    2: (A1, A2,),
    3: (A1, A2, A3,),
    4: (A1, A2, A3, A4,),
    5: (A1, A2, A3, A4, A5,),
];

/// A wrapper around Vec<JsValue>, used for vararg callbacks.
///
/// To create a callback with a variable number of arguments, a callback closure
/// must take a single `Arguments` argument.
pub struct Arguments(Vec<OwnedJsValue>);

impl Arguments {
    /// Unpack the arguments into a Vec.
    pub fn into_vec(self) -> Vec<OwnedJsValue> {
        self.0
    }
}

impl<F> Callback<PhantomData<(&Arguments, &F)>> for F
where
    F: Fn(Arguments) + Sized + RefUnwindSafe,
{
    fn argument_count(&self) -> usize {
        0
    }

    fn call(
        &self,
        context: *mut q::JSContext,
        args: Vec<OwnedJsValue>,
    ) -> Result<Result<OwnedJsValue, String>, ValueError> {
        (self)(Arguments(args));
        Ok(Ok(OwnedJsValue::new(context, create_undefined())))
    }
}

impl<F, R> Callback<PhantomData<(&Arguments, &F, &R)>> for F
where
    R: IntoCallbackResult,
    F: Fn(Arguments) -> R + Sized + RefUnwindSafe,
{
    fn argument_count(&self) -> usize {
        0
    }

    fn call(
        &self,
        context: *mut q::JSContext,
        args: Vec<OwnedJsValue>,
    ) -> Result<Result<OwnedJsValue, String>, ValueError> {
        let res = (self)(Arguments(args));
        Ok(res.into_callback_res(context))
    }
}

/// Helper for executing a callback closure.
pub fn exec_callback<F>(
    context: *mut q::JSContext,
    argc: c_int,
    argv: *mut q::JSValue,
    callback: &impl Callback<F>,
) -> Result<q::JSValue, 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| OwnedJsValue::own(context, raw))
            .collect::<Vec<_>>();

        match callback.call(context, args) {
            Ok(Ok(result)) => {
                let serialized = unsafe { result.extract() };
                Ok(serialized)
            }
            // TODO: better error reporting.
            Ok(Err(e)) => Err(ExecutionError::Exception(OwnedJsValue::new(
                context,
                create_string(context, &e).unwrap(),
            ))),
            Err(e) => Err(e.into()),
        }
    });

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

pub type CustomCallback = fn(*mut q::JSContext, &[q::JSValue]) -> Result<Option<q::JSValue>>;
pub 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.
pub 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_Ext_GetPtr(*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_Ext_NewPointer(
        q::JS_TAG_NULL,
        (&*boxed_f) as *const F as *mut c_void,
    ));

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