script_bindings/
constructor.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
5use std::ptr;
6
7use js::jsapi::{CallArgs, JSObject};
8use js::rust::HandleObject;
9
10use crate::codegen::PrototypeList;
11use crate::error::throw_constructor_without_new;
12use crate::interface::get_desired_proto;
13use crate::utils::ProtoOrIfaceArray;
14
15pub(crate) unsafe fn call_default_constructor<D: crate::DomTypes>(
16    cx: &mut js::context::JSContext,
17    args: &CallArgs,
18    global: &D::GlobalScope,
19    proto_id: PrototypeList::ID,
20    ctor_name: &str,
21    creator: unsafe fn(&mut js::context::JSContext, HandleObject, *mut ProtoOrIfaceArray),
22    constructor: impl FnOnce(
23        &mut js::context::JSContext,
24        &CallArgs,
25        &D::GlobalScope,
26        HandleObject,
27    ) -> bool,
28) -> bool {
29    if !args.is_constructing() {
30        throw_constructor_without_new(cx.into(), ctor_name);
31        return false;
32    }
33
34    rooted!(&in(cx) let mut desired_proto = ptr::null_mut::<JSObject>());
35    let proto_result = get_desired_proto(cx, args, proto_id, creator, desired_proto.handle_mut());
36    if proto_result.is_err() {
37        return false;
38    }
39
40    constructor(cx, args, global, desired_proto.handle())
41}