script/dom/bindings/
function.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5/// Defines a macro `native_fn!` to create a JavaScript function from a Rust function pointer.
6/// # Example
7/// ```
8/// let js_function: Rc<Function> = native_fn!(my_rust_function, c"myFunction", 2, 0);
9/// ```
10#[macro_export]
11macro_rules! native_fn {
12    ($cx:expr, $call:expr, $name:expr, $nargs:expr, $flags:expr) => {{
13        let fun_obj = $crate::native_raw_obj_fn!($cx, $call, $name, $nargs, $flags);
14        let cx = $cx.into();
15        #[expect(unsafe_code)]
16        unsafe {
17            Function::new(cx, fun_obj)
18        }
19    }};
20}
21
22/// Defines a macro `native_raw_obj_fn!` to create a raw JavaScript function object.
23/// # Example
24/// ```
25/// let raw_function_obj: *mut JSObject = native_raw_obj_fn!(cx, my_rust_function, c"myFunction", 2, 0);
26/// ```
27#[macro_export]
28macro_rules! native_raw_obj_fn {
29    ($cx:expr, $call:expr, $name:expr, $nargs:expr, $flags:expr) => {{
30        #[expect(unsafe_code)]
31        #[allow(clippy::macro_metavars_in_unsafe)]
32        unsafe extern "C" fn wrapper(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
33            let mut cx = unsafe {
34                // SAFETY: We are in SM hook
35                js::context::JSContext::from_ptr(
36                    std::ptr::NonNull::new(cx).expect("JSContext is not null in SM hook"),
37                )
38            };
39            let call_args = unsafe { CallArgs::from_vp(vp, argc) };
40            $call(&mut cx, call_args)
41        }
42        #[expect(unsafe_code)]
43        #[allow(clippy::macro_metavars_in_unsafe)]
44        unsafe {
45            let name: &std::ffi::CStr = $name;
46            let raw_fun = js::jsapi::JS_NewFunction(
47                $cx.raw_cx(),
48                Some(wrapper),
49                $nargs,
50                $flags,
51                name.as_ptr() as *const std::ffi::c_char,
52            );
53            assert!(!raw_fun.is_null());
54            js::jsapi::JS_GetFunctionObject(raw_fun)
55        }
56    }};
57}