aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/compartments.rs
diff options
context:
space:
mode:
Diffstat (limited to 'components/script/compartments.rs')
-rw-r--r--components/script/compartments.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/components/script/compartments.rs b/components/script/compartments.rs
new file mode 100644
index 00000000000..aedabca773e
--- /dev/null
+++ b/components/script/compartments.rs
@@ -0,0 +1,34 @@
+/* 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 crate::dom::globalscope::GlobalScope;
+use js::jsapi::{GetCurrentRealmOrNull, JSAutoCompartment};
+
+pub struct AlreadyInCompartment(());
+
+impl AlreadyInCompartment {
+ #![allow(unsafe_code)]
+ pub fn assert(global: &GlobalScope) -> AlreadyInCompartment {
+ unsafe {
+ assert!(!GetCurrentRealmOrNull(global.get_cx()).is_null());
+ }
+ AlreadyInCompartment(())
+ }
+}
+
+#[derive(Clone, Copy)]
+pub enum InCompartment<'a> {
+ Already(&'a AlreadyInCompartment),
+ Entered(&'a JSAutoCompartment),
+}
+
+impl<'a> InCompartment<'a> {
+ pub fn in_compartment(token: &AlreadyInCompartment) -> InCompartment {
+ InCompartment::Already(token)
+ }
+
+ pub fn entered(token: &JSAutoCompartment) -> InCompartment {
+ InCompartment::Entered(token)
+ }
+}