Skip to main content

script_bindings/
namespace.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 https://mozilla.org/MPL/2.0/. */
4
5//! Machinery to initialise namespace objects.
6
7use std::ffi::CStr;
8use std::ptr;
9
10use js::context::JSContext;
11use js::jsapi::{JSClass, JSFunctionSpec, JSPropertySpec};
12use js::rust::{HandleObject, MutableHandleObject};
13
14use crate::DomTypes;
15use crate::constant::ConstantSpec;
16use crate::guard::Guard;
17use crate::interface::{create_object, define_on_global_object};
18
19/// The class of a namespace object.
20#[derive(Clone, Copy)]
21pub(crate) struct NamespaceObjectClass(JSClass);
22
23unsafe impl Sync for NamespaceObjectClass {}
24
25impl NamespaceObjectClass {
26    /// Create a new `NamespaceObjectClass` structure.
27    pub(crate) const unsafe fn new(name: &'static CStr) -> Self {
28        NamespaceObjectClass(JSClass {
29            name: name.as_ptr(),
30            flags: 0,
31            cOps: ptr::null(),
32            spec: ptr::null(),
33            ext: ptr::null(),
34            oOps: ptr::null(),
35        })
36    }
37}
38
39/// Create a new namespace object.
40#[expect(clippy::too_many_arguments)]
41pub(crate) fn create_namespace_object<D: DomTypes>(
42    cx: &mut JSContext,
43    global: HandleObject,
44    proto: HandleObject,
45    class: &'static NamespaceObjectClass,
46    methods: &[Guard<&'static [JSFunctionSpec]>],
47    attributes: &[Guard<&'static [JSPropertySpec]>],
48    constants: &[Guard<&'static [ConstantSpec]>],
49    name: &CStr,
50    mut rval: MutableHandleObject,
51) {
52    create_object::<D>(
53        cx,
54        global,
55        proto,
56        &class.0,
57        methods,
58        attributes,
59        constants,
60        rval.reborrow(),
61    );
62    define_on_global_object(cx, global, name, rval.handle());
63}