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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/// Defines a macro `native_fn!` to create a JavaScript function from a Rust function pointer.
/// # Example
/// ```
/// let js_function: Rc<Function> = native_fn!(my_rust_function, c"myFunction", 2, 0);
/// ```
#[macro_export]
macro_rules! native_fn {
    ($call:expr, $name:expr, $nargs:expr, $flags:expr) => {{
        let cx = $crate::dom::types::GlobalScope::get_cx();
        let fun_obj = $crate::native_raw_obj_fn!(cx, $call, $name, $nargs, $flags);
        #[allow(unsafe_code)]
        unsafe {
            Function::new(cx, fun_obj)
        }
    }};
}

/// Defines a macro `native_raw_obj_fn!` to create a raw JavaScript function object.
/// # Example
/// ```
/// let raw_function_obj: *mut JSObject = native_raw_obj_fn!(cx, my_rust_function, c"myFunction", 2, 0);
/// ```
#[macro_export]
macro_rules! native_raw_obj_fn {
    ($cx:expr, $call:expr, $name:expr, $nargs:expr, $flags:expr) => {{
        #[allow(unsafe_code)]
        #[allow(clippy::macro_metavars_in_unsafe)]
        unsafe extern "C" fn wrapper(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
            $call(cx, argc, vp)
        }
        #[allow(unsafe_code)]
        #[allow(clippy::macro_metavars_in_unsafe)]
        unsafe {
            let name: &std::ffi::CStr = $name;
            let raw_fun = $crate::dom::bindings::import::module::jsapi::JS_NewFunction(
                *$cx,
                Some(wrapper),
                $nargs,
                $flags,
                name.as_ptr() as *const std::ffi::c_char,
            );
            assert!(!raw_fun.is_null());
            $crate::dom::bindings::import::module::jsapi::JS_GetFunctionObject(raw_fun)
        }
    }};
}