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::jsapi::{JSClass, JSFunctionSpec};
11use js::rust::{HandleObject, MutableHandleObject};
12
13use crate::DomTypes;
14use crate::constant::ConstantSpec;
15use crate::guard::Guard;
16use crate::interface::{create_object, define_on_global_object};
17use crate::script_runtime::JSContext;
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: 0 as *mut _,
32            spec: ptr::null(),
33            ext: ptr::null(),
34            oOps: ptr::null(),
35        })
36    }
37}
38
39/// Create a new namespace object.
40#[allow(clippy::too_many_arguments)]
41pub(crate) fn create_namespace_object<D: DomTypes>(
42    cx: JSContext,
43    global: HandleObject,
44    proto: HandleObject,
45    class: &'static NamespaceObjectClass,
46    methods: &[Guard<&'static [JSFunctionSpec]>],
47    constants: &[Guard<&'static [ConstantSpec]>],
48    name: &CStr,
49    mut rval: MutableHandleObject,
50) {
51    create_object::<D>(
52        cx,
53        global,
54        proto,
55        &class.0,
56        methods,
57        &[],
58        constants,
59        rval.reborrow(),
60    );
61    define_on_global_object(cx, global, name, rval.handle());
62}