aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/compartments.rs
diff options
context:
space:
mode:
authorAron Zwaan <aronzwaan@gmail.com>2019-04-24 12:44:33 +0200
committerAron Zwaan <aronzwaan@gmail.com>2019-04-24 12:44:33 +0200
commit7b293ee8cba64937c591c36a9a7e1d6c727d247c (patch)
tree0610f07fe0a59da4d26a3ad9a06683f582da40b6 /components/script/compartments.rs
parent54f54d194df1911d1dd35a82dd87331853f985a3 (diff)
downloadservo-7b293ee8cba64937c591c36a9a7e1d6c727d247c.tar.gz
servo-7b293ee8cba64937c591c36a9a7e1d6c727d247c.zip
Add proof that code is executed in compartment
Diffstat (limited to 'components/script/compartments.rs')
-rw-r--r--components/script/compartments.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/components/script/compartments.rs b/components/script/compartments.rs
new file mode 100644
index 00000000000..e0560c83710
--- /dev/null
+++ b/components/script/compartments.rs
@@ -0,0 +1,33 @@
+/* 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(())
+ }
+}
+
+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)
+ }
+}