servo-script-bindings 0.5.0

A component of the servo web-engine.
Documentation
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* 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 https://mozilla.org/MPL/2.0/. */

use std::cell::Cell;
use std::rc::Rc;

use js::context::JSContext;
use js::jsapi::{AddAssociatedMemory, Heap, JSObject, MemoryUse, RemoveAssociatedMemory};
use js::rust::HandleObject;
use malloc_size_of_derive::MallocSizeOf;

use crate::conversions::DerivedFrom;
use crate::interfaces::GlobalScopeHelpers;
use crate::iterable::{Iterable, IterableIterator};
use crate::root::{Dom, DomRoot, Root};
use crate::{DomTypes, JSTraceable};

pub trait AssociatedMemorySize: Default {
    fn size(&self) -> usize;
}

impl AssociatedMemorySize for () {
    fn size(&self) -> usize {
        0
    }
}

#[derive(Default, MallocSizeOf)]
pub struct AssociatedMemory(Cell<usize>);

impl AssociatedMemorySize for AssociatedMemory {
    fn size(&self) -> usize {
        self.0.get()
    }
}

/// A struct to store a reference to the reflector of a DOM object.
#[derive(MallocSizeOf)]
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
// If you're renaming or moving this field, update the path in plugins::reflector as well
pub struct Reflector<T = ()> {
    #[ignore_malloc_size_of = "defined and measured in rust-mozjs"]
    object: Heap<*mut JSObject>,
    /// Associated memory size (of rust side). Used for memory reporting to SM.
    size: T,
    /// Cached prototype ID for fast type checks.
    proto_id: Cell<u16>,
}

unsafe impl<T> js::gc::Traceable for Reflector<T> {
    unsafe fn trace(&self, _: *mut js::jsapi::JSTracer) {}
}

impl<T> PartialEq for Reflector<T> {
    fn eq(&self, other: &Reflector<T>) -> bool {
        self.object.get() == other.object.get()
    }
}

impl<T> Reflector<T> {
    /// Get the reflector.
    #[inline]
    pub fn get_jsobject(&self) -> HandleObject<'_> {
        // We're rooted, so it's safe to hand out a handle to object in Heap
        unsafe { HandleObject::from_raw(self.object.handle()) }
    }

    /// Get the cached prototype ID.
    #[inline]
    pub fn proto_id(&self) -> u16 {
        self.proto_id.get()
    }

    /// Set the cached prototype ID.
    #[inline]
    pub fn set_proto_id(&self, id: u16) {
        self.proto_id.set(id);
    }

    /// Initialize the reflector. (May be called only once.)
    ///
    /// # Safety
    ///
    /// The provided [`JSObject`] pointer must point to a valid [`JSObject`].
    unsafe fn set_jsobject(&self, object: *mut JSObject) {
        assert!(self.object.get().is_null());
        assert!(!object.is_null());
        self.object.set(object);
    }

    /// Return a pointer to the memory location at which the JS reflector
    /// object is stored. Used to root the reflector, as
    /// required by the JSAPI rooting APIs.
    pub fn rootable(&self) -> &Heap<*mut JSObject> {
        &self.object
    }
}

impl<T: AssociatedMemorySize> Reflector<T> {
    /// Create an uninitialized `Reflector`.
    // These are used by the bindings and do not need `default()` functions.
    #[expect(clippy::new_without_default)]
    pub fn new() -> Reflector<T> {
        Reflector {
            object: Heap::default(),
            proto_id: Cell::new(u16::MAX),
            size: T::default(),
        }
    }

    pub fn rust_size<D>(&self, _: &D) -> usize {
        size_of::<D>() + size_of::<Box<D>>() + self.size.size()
    }

    /// This function should be called from finalize of the DOM objects
    pub fn drop_memory<D>(&self, d: &D) {
        unsafe {
            RemoveAssociatedMemory(self.object.get(), self.rust_size(d), MemoryUse::DOMBinding);
        }
    }
}

impl Reflector<AssociatedMemory> {
    /// Update the associated memory size.
    pub fn update_memory_size<D>(&self, d: &D, new_size: usize) {
        if self.size.size() == new_size {
            return;
        }
        unsafe {
            RemoveAssociatedMemory(self.object.get(), self.rust_size(d), MemoryUse::DOMBinding);
            self.size.0.set(new_size);
            AddAssociatedMemory(self.object.get(), self.rust_size(d), MemoryUse::DOMBinding);
        }
    }
}

/// A trait to provide access to the `Reflector` for a DOM object.
pub trait DomObject: js::gc::Traceable + 'static {
    type ReflectorType: AssociatedMemorySize;
    /// Returns the receiver's reflector.
    fn reflector(&self) -> &Reflector<Self::ReflectorType>;
}

impl DomObject for Reflector<()> {
    type ReflectorType = ();

    fn reflector(&self) -> &Reflector<Self::ReflectorType> {
        self
    }
}

impl DomObject for Reflector<AssociatedMemory> {
    type ReflectorType = AssociatedMemory;

    fn reflector(&self) -> &Reflector<Self::ReflectorType> {
        self
    }
}

/// A trait to initialize the `Reflector` for a DOM object.
pub trait MutDomObject: DomObject {
    /// Initializes the Reflector
    ///
    /// # Safety
    ///
    /// The provided [`JSObject`] pointer must point to a valid [`JSObject`].
    /// The provided [`JSObject`] pointer must not be allocated in the nursery.
    unsafe fn init_reflector<D>(&self, obj: *mut JSObject);

    /// Initializes the Reflector without recording any associated memory usage.
    ///
    /// # Safety
    ///
    /// The provided [`JSObject`] pointer must point to a valid [`JSObject`].
    unsafe fn init_reflector_without_associated_memory(&self, obj: *mut JSObject);
}

impl MutDomObject for Reflector<()> {
    unsafe fn init_reflector<D>(&self, obj: *mut JSObject) {
        unsafe {
            js::jsapi::AddAssociatedMemory(
                obj,
                size_of::<D>() + size_of::<Box<D>>(),
                MemoryUse::DOMBinding,
            );
            self.init_reflector_without_associated_memory(obj);
        }
    }

    unsafe fn init_reflector_without_associated_memory(&self, obj: *mut JSObject) {
        unsafe {
            self.set_jsobject(obj);
        }
    }
}

impl MutDomObject for Reflector<AssociatedMemory> {
    unsafe fn init_reflector<D>(&self, obj: *mut JSObject) {
        unsafe {
            js::jsapi::AddAssociatedMemory(
                obj,
                size_of::<D>() + size_of::<Box<D>>(),
                MemoryUse::DOMBinding,
            );
            self.init_reflector_without_associated_memory(obj);
        }
    }

    unsafe fn init_reflector_without_associated_memory(&self, obj: *mut JSObject) {
        unsafe {
            self.set_jsobject(obj);
        }
    }
}

pub trait DomGlobalGeneric<D: DomTypes>: DomObject {
    /// Returns the [`GlobalScope`] of the realm that the [`DomObject`] was created in.  If this
    /// object is a `Node`, this will be different from it's owning `Document` if adopted by. For
    /// `Node`s it's almost always better to use `NodeTraits::owning_global`.
    fn global_from_reflector(&self) -> DomRoot<D::GlobalScope>
    where
        Self: Sized,
    {
        D::GlobalScope::from_reflector(self)
    }
}

impl<D: DomTypes, T: DomObject> DomGlobalGeneric<D> for T {}

/// A trait to provide a function pointer to wrap function for DOM objects.
pub trait DomObjectWrap<D: DomTypes>: Sized + DomObject + DomGlobalGeneric<D> {
    /// Function pointer to the general wrap function type
    #[expect(clippy::type_complexity)]
    const WRAP: unsafe fn(
        &mut JSContext,
        &D::GlobalScope,
        Option<HandleObject>,
        Box<Self>,
    ) -> Root<Dom<Self>>;
}

/// A trait to provide a function pointer to wrap function for DOM objects.
pub trait WeakReferenceableDomObjectWrap<D: DomTypes>:
    Sized + DomObject + DomGlobalGeneric<D>
{
    /// Function pointer to the general wrap function type
    #[expect(clippy::type_complexity)]
    const WRAP: unsafe fn(
        &mut js::context::JSContext,
        &D::GlobalScope,
        Option<HandleObject>,
        Rc<Self>,
    ) -> Root<Dom<Self>>;
}

/// A trait to provide a function pointer to wrap function for
/// DOM iterator interfaces.
pub trait DomObjectIteratorWrap<D: DomTypes>: DomObjectWrap<D> + JSTraceable + Iterable {
    /// Function pointer to the wrap function for `IterableIterator<T>`
    #[expect(clippy::type_complexity)]
    const ITER_WRAP: unsafe fn(
        &mut JSContext,
        &D::GlobalScope,
        Option<HandleObject>,
        Box<IterableIterator<D, Self>>,
    ) -> Root<Dom<IterableIterator<D, Self>>>;
}

/// Create the reflector for a new DOM object and yield ownership to the
/// reflector.
pub fn reflect_dom_object<D, T, U>(cx: &mut JSContext, obj: Box<T>, global: &U) -> DomRoot<T>
where
    D: DomTypes,
    T: DomObject + DomObjectWrap<D>,
    U: DerivedFrom<D::GlobalScope>,
{
    let global_scope = global.upcast();
    unsafe { T::WRAP(cx, global_scope, None, obj) }
}

pub fn reflect_dom_object_with_proto<D, T, U>(
    cx: &mut JSContext,
    obj: Box<T>,
    global: &U,
    proto: Option<HandleObject>,
) -> DomRoot<T>
where
    D: DomTypes,
    T: DomObject + DomObjectWrap<D>,
    U: DerivedFrom<D::GlobalScope>,
{
    let global_scope = global.upcast();
    unsafe { T::WRAP(cx, global_scope, proto, obj) }
}

/// Create the reflector for a new DOM object and yield ownership to the
/// reflector.
/// Deprecated, use `reflect_dom_object` instead.
pub fn reflect_dom_object_with_cx<D, T, U>(
    obj: Box<T>,
    global: &U,
    cx: &mut JSContext,
) -> DomRoot<T>
where
    D: DomTypes,
    T: DomObject + DomObjectWrap<D>,
    U: DerivedFrom<D::GlobalScope>,
{
    let global_scope = global.upcast();
    unsafe { T::WRAP(cx, global_scope, None, obj) }
}

/// Create the reflector for a new DOM object and yield ownership to the
/// reflector.
/// Deprecated, use `reflect_dom_object_with_proto` instead.
pub fn reflect_dom_object_with_proto_and_cx<D, T, U>(
    obj: Box<T>,
    global: &U,
    proto: Option<HandleObject>,
    cx: &mut JSContext,
) -> DomRoot<T>
where
    D: DomTypes,
    T: DomObject + DomObjectWrap<D>,
    U: DerivedFrom<D::GlobalScope>,
{
    let global_scope = global.upcast();
    unsafe { T::WRAP(cx, global_scope, proto, obj) }
}

/// Create the reflector for a new DOM object and yield ownership to the
/// reflector.
pub fn reflect_weak_referenceable_dom_object<D, T, U>(
    cx: &mut JSContext,
    obj: Rc<T>,
    global: &U,
) -> DomRoot<T>
where
    D: DomTypes,
    T: DomObject + WeakReferenceableDomObjectWrap<D>,
    U: DerivedFrom<D::GlobalScope>,
{
    let global_scope = global.upcast();
    unsafe { T::WRAP(cx, global_scope, None, obj) }
}

pub fn reflect_weak_referenceable_dom_object_with_proto<D, T, U>(
    cx: &mut JSContext,
    obj: Rc<T>,
    global: &U,
    proto: Option<HandleObject>,
) -> DomRoot<T>
where
    D: DomTypes,
    T: DomObject + WeakReferenceableDomObjectWrap<D>,
    U: DerivedFrom<D::GlobalScope>,
{
    let global_scope = global.upcast();
    unsafe { T::WRAP(cx, global_scope, proto, obj) }
}

type WrapFn<D, AbstractType> = unsafe fn(
    &mut js::context::JSContext,
    &<D as DomTypes>::GlobalScope,
    Option<HandleObject>,
    Box<AbstractType>,
) -> DomRoot<AbstractType>;

/// Create the reflector for a new DOM object and yield ownership to the
/// reflector.
pub fn reflect_dom_object_with_proto_and_wrap<D, AbstractType, GlobalType>(
    obj: Box<AbstractType>,
    global: &GlobalType,
    proto: Option<HandleObject>,
    cx: &mut js::context::JSContext,
    wrap: WrapFn<D, AbstractType>,
) -> DomRoot<AbstractType>
where
    D: DomTypes,
    AbstractType: DomObject,
    GlobalType: DerivedFrom<D::GlobalScope>,
    Box<AbstractType>: From<Box<AbstractType>>,
    DomRoot<AbstractType>: From<DomRoot<AbstractType>>,
{
    let global_scope = global.upcast();
    unsafe { wrap(cx, global_scope, proto, obj) }
}

/// Create the reflector for a new DOM object and yield ownership to the
/// reflector.
pub fn reflect_dom_object_with_wrap<D, AbstractType, GlobalType>(
    obj: Box<AbstractType>,
    global: &GlobalType,
    cx: &mut js::context::JSContext,
    wrap: WrapFn<D, AbstractType>,
) -> DomRoot<AbstractType>
where
    D: DomTypes,
    AbstractType: DomObject,
    GlobalType: DerivedFrom<D::GlobalScope>,
    Box<AbstractType>: From<Box<AbstractType>>,
    DomRoot<AbstractType>: From<DomRoot<AbstractType>>,
{
    let global_scope = global.upcast();
    unsafe { wrap(cx, global_scope, None, obj) }
}

type WrapFnRc<D, AbstractType> = unsafe fn(
    &mut js::context::JSContext,
    &<D as DomTypes>::GlobalScope,
    Option<HandleObject>,
    Rc<AbstractType>,
) -> DomRoot<AbstractType>;

/// Create the reflector for a new DOM object and yield ownership to the
/// reflector.
pub fn reflect_weak_referenceable_dom_object_with_cx_and_wrap<D, AbstractType, GlobalType>(
    cx: &mut JSContext,
    obj: Rc<AbstractType>,
    global: &GlobalType,
    wrap: WrapFnRc<D, AbstractType>,
) -> DomRoot<AbstractType>
where
    D: DomTypes,
    AbstractType: DomObject,
    GlobalType: DerivedFrom<D::GlobalScope>,
    Rc<AbstractType>: From<Rc<AbstractType>>,
    DomRoot<AbstractType>: From<DomRoot<AbstractType>>,
{
    let global_scope = global.upcast();
    unsafe { wrap(cx, global_scope, None, obj) }
}