Skip to main content

v8/
property_handler_flags.rs

1// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license.
2
3#[repr(C)]
4#[derive(Debug, Eq, PartialEq)]
5pub struct PropertyHandlerFlags(u32);
6
7impl PropertyHandlerFlags {
8  /// None.
9  pub const NONE: Self = Self(0);
10
11  /// Will not call into interceptor for properties on the receiver or prototype
12  /// chain, i.e., only call into interceptor for properties that do not exist.
13  /// Currently only valid for named interceptors.
14  pub const NON_MASKING: Self = Self(1 << 0);
15
16  /// Will not call into interceptor for symbol lookup.  Only meaningful for
17  /// named interceptors.
18  pub const ONLY_INTERCEPT_STRINGS: Self = Self(1 << 1);
19
20  /// The getter, query, enumerator callbacks do not produce side effects.
21  pub const HAS_NO_SIDE_EFFECT: Self = Self(1 << 2);
22
23  /// Test if no property handler flags are set.
24  #[inline(always)]
25  pub fn is_none(&self) -> bool {
26    *self == Self::NONE
27  }
28
29  /// Test if the non-masking property handler flag is set.
30  #[inline(always)]
31  pub fn is_non_masking(&self) -> bool {
32    self.has(Self::NON_MASKING)
33  }
34
35  /// Test if the only-intercept-strings property handler flag is set.
36  #[inline(always)]
37  pub fn is_only_intercept_strings(&self) -> bool {
38    self.has(Self::ONLY_INTERCEPT_STRINGS)
39  }
40
41  /// Test if the has-no-side-effect property handler flag is set.
42  #[inline(always)]
43  pub fn is_has_no_side_effect(&self) -> bool {
44    self.has(Self::HAS_NO_SIDE_EFFECT)
45  }
46
47  #[inline(always)]
48  fn has(&self, that: Self) -> bool {
49    let Self(lhs) = self;
50    let Self(rhs) = that;
51    0 != lhs & rhs
52  }
53}
54
55// Identical to #[derive(Default)] but arguably clearer when made explicit.
56impl Default for PropertyHandlerFlags {
57  fn default() -> Self {
58    Self::NONE
59  }
60}
61
62impl std::ops::BitOr for PropertyHandlerFlags {
63  type Output = Self;
64
65  fn bitor(self, Self(rhs): Self) -> Self {
66    let Self(lhs) = self;
67    Self(lhs | rhs)
68  }
69}
70
71#[test]
72fn test_attr() {
73  assert!(PropertyHandlerFlags::NONE.is_none());
74  assert!(!PropertyHandlerFlags::NONE.is_non_masking());
75  assert!(!PropertyHandlerFlags::NONE.is_only_intercept_strings());
76  assert!(!PropertyHandlerFlags::NONE.is_has_no_side_effect());
77
78  assert!(!PropertyHandlerFlags::NON_MASKING.is_none());
79  assert!(PropertyHandlerFlags::NON_MASKING.is_non_masking());
80  assert!(!PropertyHandlerFlags::NON_MASKING.is_only_intercept_strings());
81  assert!(!PropertyHandlerFlags::NON_MASKING.is_has_no_side_effect());
82
83  assert!(!PropertyHandlerFlags::ONLY_INTERCEPT_STRINGS.is_none());
84  assert!(!PropertyHandlerFlags::ONLY_INTERCEPT_STRINGS.is_non_masking());
85  assert!(
86    PropertyHandlerFlags::ONLY_INTERCEPT_STRINGS.is_only_intercept_strings()
87  );
88  assert!(
89    !PropertyHandlerFlags::ONLY_INTERCEPT_STRINGS.is_has_no_side_effect()
90  );
91
92  assert!(!PropertyHandlerFlags::HAS_NO_SIDE_EFFECT.is_none());
93  assert!(!PropertyHandlerFlags::HAS_NO_SIDE_EFFECT.is_non_masking());
94  assert!(
95    !PropertyHandlerFlags::HAS_NO_SIDE_EFFECT.is_only_intercept_strings()
96  );
97  assert!(PropertyHandlerFlags::HAS_NO_SIDE_EFFECT.is_has_no_side_effect());
98
99  assert_eq!(PropertyHandlerFlags::NONE, Default::default());
100
101  let attr = PropertyHandlerFlags::ONLY_INTERCEPT_STRINGS
102    | PropertyHandlerFlags::HAS_NO_SIDE_EFFECT
103    | PropertyHandlerFlags::NON_MASKING;
104  assert!(!attr.is_none());
105  assert!(attr.is_non_masking());
106  assert!(attr.is_only_intercept_strings());
107  assert!(attr.is_has_no_side_effect());
108}