Proxy.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  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. #ifndef js_Proxy_h
  6. #define js_Proxy_h
  7. #include "mozilla/Maybe.h"
  8. #include "jsfriendapi.h"
  9. #include "js/CallNonGenericMethod.h"
  10. #include "js/Class.h"
  11. namespace js {
  12. using JS::AutoIdVector;
  13. using JS::CallArgs;
  14. using JS::Handle;
  15. using JS::HandleId;
  16. using JS::HandleObject;
  17. using JS::HandleValue;
  18. using JS::IsAcceptableThis;
  19. using JS::MutableHandle;
  20. using JS::MutableHandleObject;
  21. using JS::MutableHandleValue;
  22. using JS::NativeImpl;
  23. using JS::ObjectOpResult;
  24. using JS::PrivateValue;
  25. using JS::PropertyDescriptor;
  26. using JS::Value;
  27. class RegExpGuard;
  28. class JS_FRIEND_API(Wrapper);
  29. /*
  30. * A proxy is a JSObject with highly customizable behavior. ES6 specifies a
  31. * single kind of proxy, but the customization mechanisms we use to implement
  32. * ES6 Proxy objects are also useful wherever an object with weird behavior is
  33. * wanted. Proxies are used to implement:
  34. *
  35. * - the scope objects used by the Debugger's frame.eval() method
  36. * (see js::GetDebugScopeForFunction)
  37. *
  38. * - the khuey hack, whereby a whole compartment can be blown away
  39. * even if other compartments hold references to objects in it
  40. * (see js::NukeCrossCompartmentWrappers)
  41. *
  42. * - XPConnect security wrappers, which protect chrome from malicious content
  43. * (js/xpconnect/wrappers)
  44. *
  45. * - DOM objects with special property behavior, like named getters
  46. * (dom/bindings/Codegen.py generates these proxies from WebIDL)
  47. *
  48. * - semi-transparent use of objects that live in other processes
  49. * (CPOWs, implemented in js/ipc)
  50. *
  51. * ### Proxies and internal methods
  52. *
  53. * ES2016 specifies 13 internal methods. The runtime semantics of just
  54. * about everything a script can do to an object is specified in terms
  55. * of these internal methods. For example:
  56. *
  57. * JS code ES6 internal method that gets called
  58. * --------------------------- --------------------------------
  59. * obj.prop obj.[[Get]](obj, "prop")
  60. * "prop" in obj obj.[[HasProperty]]("prop")
  61. * new obj() obj.[[Construct]](<empty argument List>)
  62. *
  63. * With regard to the implementation of these internal methods, there are three
  64. * very different kinds of object in SpiderMonkey.
  65. *
  66. * 1. Native objects' internal methods are implemented in vm/NativeObject.cpp,
  67. * with duplicate (but functionally identical) implementations scattered
  68. * through the ICs and JITs.
  69. *
  70. * 2. Certain non-native objects have internal methods that are implemented as
  71. * magical js::ObjectOps hooks. We're trying to get rid of these.
  72. *
  73. * 3. All other objects are proxies. A proxy's internal methods are
  74. * implemented in C++, as the virtual methods of a C++ object stored on the
  75. * proxy, known as its handler.
  76. *
  77. * This means that just about anything you do to a proxy will end up going
  78. * through a C++ virtual method call. Possibly several. There's no reason the
  79. * JITs and ICs can't specialize for particular proxies, based on the handler;
  80. * but currently we don't do much of this, so the virtual method overhead
  81. * typically is actually incurred.
  82. *
  83. * ### The proxy handler hierarchy
  84. *
  85. * A major use case for proxies is to forward each internal method call to
  86. * another object, known as its target. The target can be an arbitrary JS
  87. * object. Not every proxy has the notion of a target, however.
  88. *
  89. * To minimize code duplication, a set of abstract proxy handler classes is
  90. * provided, from which other handlers may inherit. These abstract classes are
  91. * organized in the following hierarchy:
  92. *
  93. * BaseProxyHandler
  94. * |
  95. * Wrapper // has a target, can be unwrapped to reveal
  96. * | // target (see js::CheckedUnwrap)
  97. * |
  98. * CrossCompartmentWrapper // target is in another compartment;
  99. * // implements membrane between compartments
  100. *
  101. * Example: Some DOM objects (including all the arraylike DOM objects) are
  102. * implemented as proxies. Since these objects don't need to forward operations
  103. * to any underlying JS object, DOMJSProxyHandler directly subclasses
  104. * BaseProxyHandler.
  105. *
  106. * Gecko's security wrappers are examples of cross-compartment wrappers.
  107. *
  108. * ### Proxy prototype chains
  109. *
  110. * In addition to the normal methods, there are two models for proxy prototype
  111. * chains.
  112. *
  113. * 1. Proxies can use the standard prototype mechanism used throughout the
  114. * engine. To do so, simply pass a prototype to NewProxyObject() at
  115. * creation time. All prototype accesses will then "just work" to treat the
  116. * proxy as a "normal" object.
  117. *
  118. * 2. A proxy can implement more complicated prototype semantics (if, for
  119. * example, it wants to delegate the prototype lookup to a wrapped object)
  120. * by passing Proxy::LazyProto as the prototype at create time. This
  121. * guarantees that the getPrototype() handler method will be called every
  122. * time the object's prototype chain is accessed.
  123. *
  124. * This system is implemented with two methods: {get,set}Prototype. The
  125. * default implementation of setPrototype throws a TypeError. Since it is
  126. * not possible to create an object without a sense of prototype chain,
  127. * handlers must implement getPrototype if opting in to the dynamic
  128. * prototype system.
  129. */
  130. /*
  131. * BaseProxyHandler is the most generic kind of proxy handler. It does not make
  132. * any assumptions about the target. Consequently, it does not provide any
  133. * default implementation for most methods. As a convenience, a few high-level
  134. * methods, like get() and set(), are given default implementations that work by
  135. * calling the low-level methods, like getOwnPropertyDescriptor().
  136. *
  137. * Important: If you add a method here, you should probably also add a
  138. * Proxy::foo entry point with an AutoEnterPolicy. If you don't, you need an
  139. * explicit override for the method in SecurityWrapper. See bug 945826 comment 0.
  140. */
  141. class JS_FRIEND_API(BaseProxyHandler)
  142. {
  143. /*
  144. * Sometimes it's desirable to designate groups of proxy handlers as "similar".
  145. * For this, we use the notion of a "family": A consumer-provided opaque pointer
  146. * that designates the larger group to which this proxy belongs.
  147. *
  148. * If it will never be important to differentiate this proxy from others as
  149. * part of a distinct group, nullptr may be used instead.
  150. */
  151. const void* mFamily;
  152. /*
  153. * Proxy handlers can use mHasPrototype to request the following special
  154. * treatment from the JS engine:
  155. *
  156. * - When mHasPrototype is true, the engine never calls these methods:
  157. * getPropertyDescriptor, has, set, enumerate, iterate. Instead, for
  158. * these operations, it calls the "own" methods like
  159. * getOwnPropertyDescriptor, hasOwn, defineProperty,
  160. * getOwnEnumerablePropertyKeys, etc., and consults the prototype chain
  161. * if needed.
  162. *
  163. * - When mHasPrototype is true, the engine calls handler->get() only if
  164. * handler->hasOwn() says an own property exists on the proxy. If not,
  165. * it consults the prototype chain.
  166. *
  167. * This is useful because it frees the ProxyHandler from having to implement
  168. * any behavior having to do with the prototype chain.
  169. */
  170. bool mHasPrototype;
  171. /*
  172. * All proxies indicate whether they have any sort of interesting security
  173. * policy that might prevent the caller from doing something it wants to
  174. * the object. In the case of wrappers, this distinction is used to
  175. * determine whether the caller may strip off the wrapper if it so desires.
  176. */
  177. bool mHasSecurityPolicy;
  178. public:
  179. explicit constexpr BaseProxyHandler(const void* aFamily, bool aHasPrototype = false,
  180. bool aHasSecurityPolicy = false)
  181. : mFamily(aFamily),
  182. mHasPrototype(aHasPrototype),
  183. mHasSecurityPolicy(aHasSecurityPolicy)
  184. { }
  185. bool hasPrototype() const {
  186. return mHasPrototype;
  187. }
  188. bool hasSecurityPolicy() const {
  189. return mHasSecurityPolicy;
  190. }
  191. inline const void* family() const {
  192. return mFamily;
  193. }
  194. static size_t offsetOfFamily() {
  195. return offsetof(BaseProxyHandler, mFamily);
  196. }
  197. virtual bool finalizeInBackground(const Value& priv) const {
  198. /*
  199. * Called on creation of a proxy to determine whether its finalize
  200. * method can be finalized on the background thread.
  201. */
  202. return true;
  203. }
  204. virtual bool canNurseryAllocate() const {
  205. /*
  206. * Nursery allocation is allowed if and only if it is safe to not
  207. * run |finalize| when the ProxyObject dies.
  208. */
  209. return false;
  210. }
  211. /* Policy enforcement methods.
  212. *
  213. * enter() allows the policy to specify whether the caller may perform |act|
  214. * on the proxy's |id| property. In the case when |act| is CALL, |id| is
  215. * generally JSID_VOID.
  216. *
  217. * The |act| parameter to enter() specifies the action being performed.
  218. * If |bp| is false, the method suggests that the caller throw (though it
  219. * may still decide to squelch the error).
  220. *
  221. * We make these OR-able so that assertEnteredPolicy can pass a union of them.
  222. * For example, get{,Own}PropertyDescriptor is invoked by calls to ::get()
  223. * ::set(), in addition to being invoked on its own, so there are several
  224. * valid Actions that could have been entered.
  225. */
  226. typedef uint32_t Action;
  227. enum {
  228. NONE = 0x00,
  229. GET = 0x01,
  230. SET = 0x02,
  231. CALL = 0x04,
  232. ENUMERATE = 0x08,
  233. GET_PROPERTY_DESCRIPTOR = 0x10
  234. };
  235. virtual bool enter(JSContext* cx, HandleObject wrapper, HandleId id, Action act,
  236. bool* bp) const;
  237. /* Standard internal methods. */
  238. virtual bool getOwnPropertyDescriptor(JSContext* cx, HandleObject proxy, HandleId id,
  239. MutableHandle<PropertyDescriptor> desc) const = 0;
  240. virtual bool defineProperty(JSContext* cx, HandleObject proxy, HandleId id,
  241. Handle<PropertyDescriptor> desc,
  242. ObjectOpResult& result) const = 0;
  243. virtual bool ownPropertyKeys(JSContext* cx, HandleObject proxy,
  244. AutoIdVector& props) const = 0;
  245. virtual bool delete_(JSContext* cx, HandleObject proxy, HandleId id,
  246. ObjectOpResult& result) const = 0;
  247. /*
  248. * These methods are standard, but the engine does not normally call them.
  249. * They're opt-in. See "Proxy prototype chains" above.
  250. *
  251. * getPrototype() crashes if called. setPrototype() throws a TypeError.
  252. */
  253. virtual bool getPrototype(JSContext* cx, HandleObject proxy, MutableHandleObject protop) const;
  254. virtual bool setPrototype(JSContext* cx, HandleObject proxy, HandleObject proto,
  255. ObjectOpResult& result) const;
  256. /* Non-standard but conceptual kin to {g,s}etPrototype, so these live here. */
  257. virtual bool getPrototypeIfOrdinary(JSContext* cx, HandleObject proxy, bool* isOrdinary,
  258. MutableHandleObject protop) const = 0;
  259. virtual bool setImmutablePrototype(JSContext* cx, HandleObject proxy, bool* succeeded) const;
  260. virtual bool preventExtensions(JSContext* cx, HandleObject proxy,
  261. ObjectOpResult& result) const = 0;
  262. virtual bool isExtensible(JSContext* cx, HandleObject proxy, bool* extensible) const = 0;
  263. /*
  264. * These standard internal methods are implemented, as a convenience, so
  265. * that ProxyHandler subclasses don't have to provide every single method.
  266. *
  267. * The base-class implementations work by calling getPropertyDescriptor().
  268. * They do not follow any standard. When in doubt, override them.
  269. */
  270. virtual bool has(JSContext* cx, HandleObject proxy, HandleId id, bool* bp) const;
  271. virtual bool get(JSContext* cx, HandleObject proxy, HandleValue receiver,
  272. HandleId id, MutableHandleValue vp) const;
  273. virtual bool set(JSContext* cx, HandleObject proxy, HandleId id, HandleValue v,
  274. HandleValue receiver, ObjectOpResult& result) const;
  275. /*
  276. * [[Call]] and [[Construct]] are standard internal methods but according
  277. * to the spec, they are not present on every object.
  278. *
  279. * SpiderMonkey never calls a proxy's call()/construct() internal method
  280. * unless isCallable()/isConstructor() returns true for that proxy.
  281. *
  282. * BaseProxyHandler::isCallable()/isConstructor() always return false, and
  283. * BaseProxyHandler::call()/construct() crash if called. So if you're
  284. * creating a kind of that is never callable, you don't have to override
  285. * anything, but otherwise you probably want to override all four.
  286. */
  287. virtual bool call(JSContext* cx, HandleObject proxy, const CallArgs& args) const;
  288. virtual bool construct(JSContext* cx, HandleObject proxy, const CallArgs& args) const;
  289. /* SpiderMonkey extensions. */
  290. virtual bool enumerate(JSContext* cx, HandleObject proxy, MutableHandleObject objp) const;
  291. virtual bool getPropertyDescriptor(JSContext* cx, HandleObject proxy, HandleId id,
  292. MutableHandle<PropertyDescriptor> desc) const;
  293. virtual bool hasOwn(JSContext* cx, HandleObject proxy, HandleId id, bool* bp) const;
  294. virtual bool getOwnEnumerablePropertyKeys(JSContext* cx, HandleObject proxy,
  295. AutoIdVector& props) const;
  296. virtual bool nativeCall(JSContext* cx, IsAcceptableThis test, NativeImpl impl,
  297. const CallArgs& args) const;
  298. virtual bool hasInstance(JSContext* cx, HandleObject proxy, MutableHandleValue v, bool* bp) const;
  299. virtual bool getBuiltinClass(JSContext* cx, HandleObject proxy,
  300. ESClass* cls) const;
  301. virtual bool isArray(JSContext* cx, HandleObject proxy, JS::IsArrayAnswer* answer) const;
  302. virtual const char* className(JSContext* cx, HandleObject proxy) const;
  303. virtual JSString* fun_toString(JSContext* cx, HandleObject proxy, unsigned indent) const;
  304. virtual bool regexp_toShared(JSContext* cx, HandleObject proxy, RegExpGuard* g) const;
  305. virtual bool boxedValue_unbox(JSContext* cx, HandleObject proxy, MutableHandleValue vp) const;
  306. virtual void trace(JSTracer* trc, JSObject* proxy) const;
  307. virtual void finalize(JSFreeOp* fop, JSObject* proxy) const;
  308. virtual void objectMoved(JSObject* proxy, const JSObject* old) const;
  309. // Allow proxies, wrappers in particular, to specify callability at runtime.
  310. // Note: These do not take const JSObject*, but they do in spirit.
  311. // We are not prepared to do this, as there's little const correctness
  312. // in the external APIs that handle proxies.
  313. virtual bool isCallable(JSObject* obj) const;
  314. virtual bool isConstructor(JSObject* obj) const;
  315. virtual bool getElements(JSContext* cx, HandleObject proxy, uint32_t begin, uint32_t end,
  316. ElementAdder* adder) const;
  317. /* See comment for weakmapKeyDelegateOp in js/Class.h. */
  318. virtual JSObject* weakmapKeyDelegate(JSObject* proxy) const;
  319. virtual bool isScripted() const { return false; }
  320. };
  321. extern JS_FRIEND_DATA(const js::Class* const) ProxyClassPtr;
  322. inline bool IsProxy(const JSObject* obj)
  323. {
  324. return GetObjectClass(obj)->isProxy();
  325. }
  326. namespace detail {
  327. const uint32_t PROXY_EXTRA_SLOTS = 2;
  328. // Layout of the values stored by a proxy. Note that API clients require the
  329. // private slot to be the first slot in the proxy's values, so that the private
  330. // slot can be accessed in the same fashion as the first reserved slot, via
  331. // {Get,Set}ReservedOrProxyPrivateSlot.
  332. struct ProxyValueArray
  333. {
  334. Value privateSlot;
  335. Value extraSlots[PROXY_EXTRA_SLOTS];
  336. ProxyValueArray()
  337. : privateSlot(JS::UndefinedValue())
  338. {
  339. for (size_t i = 0; i < PROXY_EXTRA_SLOTS; i++)
  340. extraSlots[i] = JS::UndefinedValue();
  341. }
  342. };
  343. // All proxies share the same data layout. Following the object's shape and
  344. // type, the proxy has a ProxyDataLayout structure with a pointer to an array
  345. // of values and the proxy's handler. This is designed both so that proxies can
  346. // be easily swapped with other objects (via RemapWrapper) and to mimic the
  347. // layout of other objects (proxies and other objects have the same size) so
  348. // that common code can access either type of object.
  349. //
  350. // See GetReservedOrProxyPrivateSlot below.
  351. struct ProxyDataLayout
  352. {
  353. ProxyValueArray* values;
  354. const BaseProxyHandler* handler;
  355. };
  356. const uint32_t ProxyDataOffset = 2 * sizeof(void*);
  357. inline ProxyDataLayout*
  358. GetProxyDataLayout(JSObject* obj)
  359. {
  360. MOZ_ASSERT(IsProxy(obj));
  361. return reinterpret_cast<ProxyDataLayout*>(reinterpret_cast<uint8_t*>(obj) + ProxyDataOffset);
  362. }
  363. inline const ProxyDataLayout*
  364. GetProxyDataLayout(const JSObject* obj)
  365. {
  366. MOZ_ASSERT(IsProxy(obj));
  367. return reinterpret_cast<const ProxyDataLayout*>(reinterpret_cast<const uint8_t*>(obj) +
  368. ProxyDataOffset);
  369. }
  370. } // namespace detail
  371. inline const BaseProxyHandler*
  372. GetProxyHandler(const JSObject* obj)
  373. {
  374. return detail::GetProxyDataLayout(obj)->handler;
  375. }
  376. inline const Value&
  377. GetProxyPrivate(const JSObject* obj)
  378. {
  379. return detail::GetProxyDataLayout(obj)->values->privateSlot;
  380. }
  381. inline JSObject*
  382. GetProxyTargetObject(JSObject* obj)
  383. {
  384. return GetProxyPrivate(obj).toObjectOrNull();
  385. }
  386. inline const Value&
  387. GetProxyExtra(const JSObject* obj, size_t n)
  388. {
  389. MOZ_ASSERT(n < detail::PROXY_EXTRA_SLOTS);
  390. return detail::GetProxyDataLayout(obj)->values->extraSlots[n];
  391. }
  392. inline void
  393. SetProxyHandler(JSObject* obj, const BaseProxyHandler* handler)
  394. {
  395. detail::GetProxyDataLayout(obj)->handler = handler;
  396. }
  397. JS_FRIEND_API(void)
  398. SetValueInProxy(Value* slot, const Value& value);
  399. inline void
  400. SetProxyExtra(JSObject* obj, size_t n, const Value& extra)
  401. {
  402. MOZ_ASSERT(n < detail::PROXY_EXTRA_SLOTS);
  403. Value* vp = &detail::GetProxyDataLayout(obj)->values->extraSlots[n];
  404. // Trigger a barrier before writing the slot.
  405. if (vp->isGCThing() || extra.isGCThing())
  406. SetValueInProxy(vp, extra);
  407. else
  408. *vp = extra;
  409. }
  410. inline bool
  411. IsScriptedProxy(const JSObject* obj)
  412. {
  413. return IsProxy(obj) && GetProxyHandler(obj)->isScripted();
  414. }
  415. inline const Value&
  416. GetReservedOrProxyPrivateSlot(const JSObject* obj, size_t slot)
  417. {
  418. MOZ_ASSERT(slot == 0);
  419. MOZ_ASSERT(slot < JSCLASS_RESERVED_SLOTS(GetObjectClass(obj)) || IsProxy(obj));
  420. return reinterpret_cast<const shadow::Object*>(obj)->slotRef(slot);
  421. }
  422. inline void
  423. SetReservedOrProxyPrivateSlot(JSObject* obj, size_t slot, const Value& value)
  424. {
  425. MOZ_ASSERT(slot == 0);
  426. MOZ_ASSERT(slot < JSCLASS_RESERVED_SLOTS(GetObjectClass(obj)) || IsProxy(obj));
  427. shadow::Object* sobj = reinterpret_cast<shadow::Object*>(obj);
  428. if (sobj->slotRef(slot).isGCThing() || value.isGCThing())
  429. SetReservedOrProxyPrivateSlotWithBarrier(obj, slot, value);
  430. else
  431. sobj->slotRef(slot) = value;
  432. }
  433. class MOZ_STACK_CLASS ProxyOptions {
  434. protected:
  435. /* protected constructor for subclass */
  436. explicit ProxyOptions(bool singletonArg, bool lazyProtoArg = false)
  437. : singleton_(singletonArg),
  438. lazyProto_(lazyProtoArg),
  439. clasp_(ProxyClassPtr)
  440. {}
  441. public:
  442. ProxyOptions() : singleton_(false),
  443. lazyProto_(false),
  444. clasp_(ProxyClassPtr)
  445. {}
  446. bool singleton() const { return singleton_; }
  447. ProxyOptions& setSingleton(bool flag) {
  448. singleton_ = flag;
  449. return *this;
  450. }
  451. bool lazyProto() const { return lazyProto_; }
  452. ProxyOptions& setLazyProto(bool flag) {
  453. lazyProto_ = flag;
  454. return *this;
  455. }
  456. const Class* clasp() const {
  457. return clasp_;
  458. }
  459. ProxyOptions& setClass(const Class* claspArg) {
  460. clasp_ = claspArg;
  461. return *this;
  462. }
  463. private:
  464. bool singleton_;
  465. bool lazyProto_;
  466. const Class* clasp_;
  467. };
  468. JS_FRIEND_API(JSObject*)
  469. NewProxyObject(JSContext* cx, const BaseProxyHandler* handler, HandleValue priv,
  470. JSObject* proto, const ProxyOptions& options = ProxyOptions());
  471. JSObject*
  472. RenewProxyObject(JSContext* cx, JSObject* obj, BaseProxyHandler* handler, const Value& priv);
  473. class JS_FRIEND_API(AutoEnterPolicy)
  474. {
  475. public:
  476. typedef BaseProxyHandler::Action Action;
  477. AutoEnterPolicy(JSContext* cx, const BaseProxyHandler* handler,
  478. HandleObject wrapper, HandleId id, Action act, bool mayThrow)
  479. #ifdef JS_DEBUG
  480. : context(nullptr)
  481. #endif
  482. {
  483. allow = handler->hasSecurityPolicy() ? handler->enter(cx, wrapper, id, act, &rv)
  484. : true;
  485. recordEnter(cx, wrapper, id, act);
  486. // We want to throw an exception if all of the following are true:
  487. // * The policy disallowed access.
  488. // * The policy set rv to false, indicating that we should throw.
  489. // * The caller did not instruct us to ignore exceptions.
  490. // * The policy did not throw itself.
  491. if (!allow && !rv && mayThrow)
  492. reportErrorIfExceptionIsNotPending(cx, id);
  493. }
  494. virtual ~AutoEnterPolicy() { recordLeave(); }
  495. inline bool allowed() { return allow; }
  496. inline bool returnValue() { MOZ_ASSERT(!allowed()); return rv; }
  497. protected:
  498. // no-op constructor for subclass
  499. AutoEnterPolicy()
  500. #ifdef JS_DEBUG
  501. : context(nullptr)
  502. , enteredAction(BaseProxyHandler::NONE)
  503. #endif
  504. {}
  505. void reportErrorIfExceptionIsNotPending(JSContext* cx, jsid id);
  506. bool allow;
  507. bool rv;
  508. #ifdef JS_DEBUG
  509. JSContext* context;
  510. mozilla::Maybe<HandleObject> enteredProxy;
  511. mozilla::Maybe<HandleId> enteredId;
  512. Action enteredAction;
  513. // NB: We explicitly don't track the entered action here, because sometimes
  514. // set() methods do an implicit get() during their implementation, leading
  515. // to spurious assertions.
  516. AutoEnterPolicy* prev;
  517. void recordEnter(JSContext* cx, HandleObject proxy, HandleId id, Action act);
  518. void recordLeave();
  519. friend JS_FRIEND_API(void) assertEnteredPolicy(JSContext* cx, JSObject* proxy, jsid id, Action act);
  520. #else
  521. inline void recordEnter(JSContext* cx, JSObject* proxy, jsid id, Action act) {}
  522. inline void recordLeave() {}
  523. #endif
  524. };
  525. #ifdef JS_DEBUG
  526. class JS_FRIEND_API(AutoWaivePolicy) : public AutoEnterPolicy {
  527. public:
  528. AutoWaivePolicy(JSContext* cx, HandleObject proxy, HandleId id,
  529. BaseProxyHandler::Action act)
  530. {
  531. allow = true;
  532. recordEnter(cx, proxy, id, act);
  533. }
  534. };
  535. #else
  536. class JS_FRIEND_API(AutoWaivePolicy) {
  537. public:
  538. AutoWaivePolicy(JSContext* cx, HandleObject proxy, HandleId id,
  539. BaseProxyHandler::Action act)
  540. {}
  541. };
  542. #endif
  543. #ifdef JS_DEBUG
  544. extern JS_FRIEND_API(void)
  545. assertEnteredPolicy(JSContext* cx, JSObject* obj, jsid id,
  546. BaseProxyHandler::Action act);
  547. #else
  548. inline void assertEnteredPolicy(JSContext* cx, JSObject* obj, jsid id,
  549. BaseProxyHandler::Action act)
  550. {}
  551. #endif
  552. extern JS_FRIEND_API(JSObject*)
  553. InitProxyClass(JSContext* cx, JS::HandleObject obj);
  554. } /* namespace js */
  555. #endif /* js_Proxy_h */