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::script_runtime::JSContext;
14use crate::utils::ProtoOrIfaceArray;
15
16pub(crate) unsafe fn call_default_constructor<D: crate::DomTypes>(
17    cx: JSContext,
18    args: &CallArgs,
19    global: &D::GlobalScope,
20    proto_id: PrototypeList::ID,
21    ctor_name: &str,
22    creator: unsafe fn(JSContext, HandleObject, *mut ProtoOrIfaceArray),
23    constructor: impl FnOnce(JSContext, &CallArgs, &D::GlobalScope, HandleObject) -> bool,
24) -> bool {
25    if !args.is_constructing() {
26        throw_constructor_without_new(cx, ctor_name);
27        return false;
28    }
29
30    rooted!(in(*cx) let mut desired_proto = ptr::null_mut::<JSObject>());
31    let proto_result = get_desired_proto(cx, args, proto_id, creator, desired_proto.handle_mut());
32    if proto_result.is_err() {
33        return false;
34    }
35
36    constructor(cx, args, global, desired_proto.handle())
37}