ion/object/
mod.rs

1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 */
6
7use std::ptr;
8
9pub use array::Array;
10pub use date::Date;
11pub use descriptor::PropertyDescriptor;
12pub use iterator::{Iterator, JSIterator};
13pub use key::{OwnedKey, PropertyKey};
14pub use map::Map;
15use mozjs::jsapi::{
16	JS_NewGlobalObject, JSCLASS_RESERVED_SLOTS_MASK, JSCLASS_RESERVED_SLOTS_SHIFT, JSClass, JSPrincipals,
17	OnNewGlobalHookOption,
18};
19use mozjs::rust::{RealmOptions, SIMPLE_GLOBAL_CLASS};
20pub use object::Object;
21pub use promise::Promise;
22pub use regexp::RegExp;
23pub use set::Set;
24
25use crate::Context;
26
27mod array;
28mod date;
29mod descriptor;
30mod iterator;
31mod key;
32mod map;
33mod object;
34mod promise;
35mod regexp;
36mod set;
37pub mod typedarray;
38
39/// Returns the bit-masked representation of reserved slots for a class.
40pub const fn class_reserved_slots(slots: u32) -> u32 {
41	(slots & JSCLASS_RESERVED_SLOTS_MASK) << JSCLASS_RESERVED_SLOTS_SHIFT
42}
43
44pub const unsafe fn class_num_reserved_slots(class: *const JSClass) -> u32 {
45	unsafe { ((*class).flags >> JSCLASS_RESERVED_SLOTS_SHIFT) & JSCLASS_RESERVED_SLOTS_MASK }
46}
47
48pub fn new_global<'cx>(
49	cx: &'cx Context, class: &JSClass, principals: Option<*mut JSPrincipals>, hook_option: OnNewGlobalHookOption,
50	realm_options: Option<RealmOptions>,
51) -> Object<'cx> {
52	let realm_options = realm_options.unwrap_or_default();
53	let global = unsafe {
54		JS_NewGlobalObject(
55			cx.as_ptr(),
56			class,
57			principals.unwrap_or_else(ptr::null_mut),
58			hook_option,
59			&raw const *realm_options,
60		)
61	};
62	Object::from(cx.root(global))
63}
64
65pub fn default_new_global(cx: &Context) -> Object<'_> {
66	let mut options = RealmOptions::default();
67	options.creationOptions_.sharedMemoryAndAtomics_ = true;
68	options.creationOptions_.defineSharedArrayBufferConstructor_ = true;
69
70	new_global(
71		cx,
72		&SIMPLE_GLOBAL_CLASS,
73		None,
74		OnNewGlobalHookOption::FireOnNewGlobalHook,
75		Some(options),
76	)
77}