…reak servo <!-- Please describe your changes on the following line: --> --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [x] These changes fix https://github.com/servo/rust-mozjs/issues/153 <!-- Either: --> - [ ] There are tests for these changes OR - [x] These changes do not require tests because this is just migration to a lifetimed API <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> Source-Repo: https://github.com/servo/servo Source-Revision: 914952487bcbba0a31db8aefc4a9487903721959
57 lines
1.6 KiB
Rust
57 lines
1.6 KiB
Rust
/* 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 http://mozilla.org/MPL/2.0/. */
|
|
|
|
//! Machinery to conditionally expose things.
|
|
|
|
use js::jsapi::JSContext;
|
|
use js::rust::HandleObject;
|
|
use servo_config::prefs::PREFS;
|
|
|
|
/// A container with a condition.
|
|
pub struct Guard<T: Clone + Copy> {
|
|
condition: Condition,
|
|
value: T,
|
|
}
|
|
|
|
impl<T: Clone + Copy> Guard<T> {
|
|
/// Construct a new guarded value.
|
|
pub const fn new(condition: Condition, value: T) -> Self {
|
|
Guard {
|
|
condition: condition,
|
|
value: value,
|
|
}
|
|
}
|
|
|
|
/// Expose the value if the condition is satisfied.
|
|
///
|
|
/// The passed handle is the object on which the value may be exposed.
|
|
pub unsafe fn expose(&self, cx: *mut JSContext, obj: HandleObject) -> Option<T> {
|
|
if self.condition.is_satisfied(cx, obj) {
|
|
Some(self.value)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A condition to expose things.
|
|
pub enum Condition {
|
|
/// The condition is satisfied if the function returns true.
|
|
Func(unsafe fn(*mut JSContext, HandleObject) -> bool),
|
|
/// The condition is satisfied if the preference is set.
|
|
Pref(&'static str),
|
|
/// The condition is always satisfied.
|
|
Satisfied,
|
|
}
|
|
|
|
impl Condition {
|
|
unsafe fn is_satisfied(&self, cx: *mut JSContext, obj: HandleObject) -> bool {
|
|
match *self {
|
|
Condition::Pref(name) => PREFS.get(name).as_boolean().unwrap_or(false),
|
|
Condition::Func(f) => f(cx, obj),
|
|
Condition::Satisfied => true,
|
|
}
|
|
}
|
|
}
|