2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2018-11-30 18:39:55 +03:00
|
|
|
* vim: set ts=8 sts=2 et sw=2 tw=80:
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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/. */
|
2010-05-19 06:21:43 +04:00
|
|
|
|
2015-02-10 21:58:18 +03:00
|
|
|
#ifndef js_Proxy_h
|
|
|
|
#define js_Proxy_h
|
2010-05-19 06:21:43 +04:00
|
|
|
|
2013-08-28 02:10:28 +04:00
|
|
|
#include "mozilla/Maybe.h"
|
|
|
|
|
2011-10-04 18:06:54 +04:00
|
|
|
#include "jsfriendapi.h"
|
2010-05-19 06:21:43 +04:00
|
|
|
|
2019-12-11 09:17:44 +03:00
|
|
|
#include "js/Array.h" // JS::IsArrayAnswer
|
2013-08-27 08:39:38 +04:00
|
|
|
#include "js/CallNonGenericMethod.h"
|
2013-08-29 04:20:24 +04:00
|
|
|
#include "js/Class.h"
|
2020-09-09 01:55:38 +03:00
|
|
|
#include "js/Object.h" // JS::GetClass
|
2020-08-20 22:21:53 +03:00
|
|
|
#include "js/shadow/Object.h" // JS::shadow::Object
|
2013-08-27 08:39:38 +04:00
|
|
|
|
2010-05-25 01:33:03 +04:00
|
|
|
namespace js {
|
|
|
|
|
2017-03-27 12:38:29 +03:00
|
|
|
class RegExpShared;
|
2017-03-27 12:38:29 +03:00
|
|
|
|
2018-11-19 20:02:47 +03:00
|
|
|
class JS_FRIEND_API Wrapper;
|
2012-06-28 06:10:37 +04:00
|
|
|
|
2012-07-08 21:04:14 +04:00
|
|
|
/*
|
2018-06-26 19:02:59 +03:00
|
|
|
* [SMDOC] Proxy Objects
|
|
|
|
*
|
2014-10-08 21:09:08 +04:00
|
|
|
* A proxy is a JSObject with highly customizable behavior. ES6 specifies a
|
|
|
|
* single kind of proxy, but the customization mechanisms we use to implement
|
|
|
|
* ES6 Proxy objects are also useful wherever an object with weird behavior is
|
|
|
|
* wanted. Proxies are used to implement:
|
2012-07-08 21:04:14 +04:00
|
|
|
*
|
2014-10-08 21:09:08 +04:00
|
|
|
* - the scope objects used by the Debugger's frame.eval() method
|
2018-11-05 12:09:45 +03:00
|
|
|
* (see js::GetDebugEnvironment)
|
2012-07-08 21:04:14 +04:00
|
|
|
*
|
2014-10-08 21:09:08 +04:00
|
|
|
* - the khuey hack, whereby a whole compartment can be blown away
|
|
|
|
* even if other compartments hold references to objects in it
|
|
|
|
* (see js::NukeCrossCompartmentWrappers)
|
2012-07-08 21:04:14 +04:00
|
|
|
*
|
2014-10-08 21:09:08 +04:00
|
|
|
* - XPConnect security wrappers, which protect chrome from malicious content
|
|
|
|
* (js/xpconnect/wrappers)
|
2013-12-14 00:01:30 +04:00
|
|
|
*
|
2014-10-08 21:09:08 +04:00
|
|
|
* - DOM objects with special property behavior, like named getters
|
|
|
|
* (dom/bindings/Codegen.py generates these proxies from WebIDL)
|
|
|
|
*
|
|
|
|
* ### Proxies and internal methods
|
|
|
|
*
|
2018-11-05 12:09:45 +03:00
|
|
|
* ES2019 specifies 13 internal methods. The runtime semantics of just about
|
|
|
|
* everything a script can do to an object is specified in terms of these
|
|
|
|
* internal methods. For example:
|
2014-10-08 21:09:08 +04:00
|
|
|
*
|
|
|
|
* JS code ES6 internal method that gets called
|
|
|
|
* --------------------------- --------------------------------
|
|
|
|
* obj.prop obj.[[Get]](obj, "prop")
|
|
|
|
* "prop" in obj obj.[[HasProperty]]("prop")
|
|
|
|
* new obj() obj.[[Construct]](<empty argument List>)
|
|
|
|
*
|
|
|
|
* With regard to the implementation of these internal methods, there are three
|
|
|
|
* very different kinds of object in SpiderMonkey.
|
|
|
|
*
|
2018-11-05 12:09:45 +03:00
|
|
|
* 1. Native objects cover most objects and contain both internal slots and
|
2019-08-14 13:00:05 +03:00
|
|
|
* properties. JSClassOps and ObjectOps may be used to override certain
|
2018-11-05 12:09:45 +03:00
|
|
|
* default behaviors.
|
2014-10-08 21:09:08 +04:00
|
|
|
*
|
2018-11-05 12:09:45 +03:00
|
|
|
* 2. Proxy objects are composed of internal slots and a ProxyHandler. The
|
|
|
|
* handler contains C++ methods that can implement these standard (and
|
2019-08-14 13:00:05 +03:00
|
|
|
* non-standard) internal methods. JSClassOps and ObjectOps for the base
|
2018-11-05 12:09:45 +03:00
|
|
|
* ProxyObject invoke the handler methods as appropriate.
|
2014-12-18 00:55:32 +03:00
|
|
|
*
|
2019-08-14 13:00:05 +03:00
|
|
|
* 3. Objects with custom layouts like TypedObjects. These rely on JSClassOps
|
2018-11-05 12:09:45 +03:00
|
|
|
* and ObjectOps to implement internal methods.
|
2014-10-08 21:09:08 +04:00
|
|
|
*
|
2019-08-14 13:00:05 +03:00
|
|
|
* Native objects with custom JSClassOps / ObjectOps are used when the object
|
2018-11-05 12:09:45 +03:00
|
|
|
* behaves very similar to a normal object such as the ArrayObject and it's
|
|
|
|
* length property. Most usages wrapping a C++ or other type should prefer
|
|
|
|
* using a Proxy. Using the proxy approach makes it much easier to create an
|
|
|
|
* ECMAScript and JIT compatible object, particularly if using an appropriate
|
|
|
|
* base class.
|
|
|
|
*
|
|
|
|
* Just about anything you do to a proxy will end up going through a C++
|
|
|
|
* virtual method call. Possibly several. There's no reason the JITs and ICs
|
|
|
|
* can't specialize for particular proxies, based on the handler; but currently
|
|
|
|
* we don't do much of this, so the virtual method overhead typically is
|
|
|
|
* actually incurred.
|
2014-10-08 21:09:08 +04:00
|
|
|
*
|
|
|
|
* ### The proxy handler hierarchy
|
|
|
|
*
|
|
|
|
* A major use case for proxies is to forward each internal method call to
|
|
|
|
* another object, known as its target. The target can be an arbitrary JS
|
|
|
|
* object. Not every proxy has the notion of a target, however.
|
2013-12-14 00:01:30 +04:00
|
|
|
*
|
2012-07-08 21:04:14 +04:00
|
|
|
* To minimize code duplication, a set of abstract proxy handler classes is
|
2014-10-08 21:09:08 +04:00
|
|
|
* provided, from which other handlers may inherit. These abstract classes are
|
|
|
|
* organized in the following hierarchy:
|
2012-07-08 21:04:14 +04:00
|
|
|
*
|
2014-10-08 21:09:08 +04:00
|
|
|
* BaseProxyHandler
|
|
|
|
* |
|
2018-11-05 12:09:45 +03:00
|
|
|
* ForwardingProxyHandler // has a target and forwards internal methods
|
|
|
|
* |
|
|
|
|
* Wrapper // can be unwrapped to reveal target
|
|
|
|
* | // (see js::CheckedUnwrap)
|
2014-10-08 21:09:08 +04:00
|
|
|
* |
|
|
|
|
* CrossCompartmentWrapper // target is in another compartment;
|
|
|
|
* // implements membrane between compartments
|
|
|
|
*
|
|
|
|
* Example: Some DOM objects (including all the arraylike DOM objects) are
|
|
|
|
* implemented as proxies. Since these objects don't need to forward operations
|
2018-11-05 12:09:45 +03:00
|
|
|
* to any underlying JS object, BaseDOMProxyHandler directly subclasses
|
2014-10-08 21:09:08 +04:00
|
|
|
* BaseProxyHandler.
|
|
|
|
*
|
|
|
|
* Gecko's security wrappers are examples of cross-compartment wrappers.
|
|
|
|
*
|
|
|
|
* ### Proxy prototype chains
|
|
|
|
*
|
2018-11-05 12:09:45 +03:00
|
|
|
* While most ECMAScript internal methods are handled by simply calling the
|
|
|
|
* handler method, the [[GetPrototypeOf]] / [[SetPrototypeOf]] behaviors may
|
|
|
|
* follow one of two models:
|
|
|
|
*
|
|
|
|
* 1. A concrete prototype object (or null) is passed to object construction
|
|
|
|
* and ordinary prototype read and write applies. The prototype-related
|
|
|
|
* handler hooks are never called in this case. The [[Prototype]] slot is
|
|
|
|
* used to store the current prototype value.
|
|
|
|
*
|
|
|
|
* 2. TaggedProto::LazyProto is passed to NewProxyObject (or the
|
|
|
|
* ProxyOptions::lazyProto flag is set). Each read or write of the
|
|
|
|
* prototype will invoke the handler. This dynamic prototype behavior may
|
|
|
|
* be useful for wrapper-like objects. If this mode is used the
|
|
|
|
* getPrototype handler at a minimum must be implemented.
|
|
|
|
*
|
|
|
|
* NOTE: In this mode the [[Prototype]] internal slot is unavailable and
|
|
|
|
* must be simulated if needed. This is non-standard, but an
|
|
|
|
* appropriate handler can hide this implementation detail.
|
|
|
|
*
|
|
|
|
* One subtlety here is that ECMAScript has a notion of "ordinary" prototypes.
|
|
|
|
* An object that doesn't override [[GetPrototypeOf]] is considered to have an
|
|
|
|
* ordinary prototype. The getPrototypeIfOrdinary handler must be implemented
|
|
|
|
* by you or your base class. Typically model 1 will be considered "ordinary"
|
|
|
|
* and model 2 will not.
|
2012-07-08 21:04:14 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BaseProxyHandler is the most generic kind of proxy handler. It does not make
|
|
|
|
* any assumptions about the target. Consequently, it does not provide any
|
2014-10-08 21:09:08 +04:00
|
|
|
* default implementation for most methods. As a convenience, a few high-level
|
|
|
|
* methods, like get() and set(), are given default implementations that work by
|
|
|
|
* calling the low-level methods, like getOwnPropertyDescriptor().
|
2013-12-06 00:58:20 +04:00
|
|
|
*
|
2014-10-08 21:09:08 +04:00
|
|
|
* Important: If you add a method here, you should probably also add a
|
|
|
|
* Proxy::foo entry point with an AutoEnterPolicy. If you don't, you need an
|
|
|
|
* explicit override for the method in SecurityWrapper. See bug 945826 comment
|
|
|
|
* 0.
|
2012-07-08 21:04:14 +04:00
|
|
|
*/
|
2018-11-19 20:02:47 +03:00
|
|
|
class JS_FRIEND_API BaseProxyHandler {
|
2014-08-28 04:09:06 +04:00
|
|
|
/*
|
|
|
|
* Sometimes it's desirable to designate groups of proxy handlers as
|
|
|
|
* "similar". For this, we use the notion of a "family": A consumer-provided
|
|
|
|
* opaque pointer that designates the larger group to which this proxy
|
|
|
|
* belongs.
|
|
|
|
*
|
|
|
|
* If it will never be important to differentiate this proxy from others as
|
|
|
|
* part of a distinct group, nullptr may be used instead.
|
|
|
|
*/
|
2015-03-29 01:22:11 +03:00
|
|
|
const void* mFamily;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-04-26 00:07:18 +04:00
|
|
|
/*
|
|
|
|
* Proxy handlers can use mHasPrototype to request the following special
|
|
|
|
* treatment from the JS engine:
|
|
|
|
*
|
|
|
|
* - When mHasPrototype is true, the engine never calls these methods:
|
2019-01-25 19:41:34 +03:00
|
|
|
* has, set, enumerate, iterate. Instead, for these operations,
|
|
|
|
* it calls the "own" methods like getOwnPropertyDescriptor, hasOwn,
|
|
|
|
* defineProperty, getOwnEnumerablePropertyKeys, etc.,
|
|
|
|
* and consults the prototype chain if needed.
|
2014-04-26 00:07:18 +04:00
|
|
|
*
|
|
|
|
* - When mHasPrototype is true, the engine calls handler->get() only if
|
|
|
|
* handler->hasOwn() says an own property exists on the proxy. If not,
|
|
|
|
* it consults the prototype chain.
|
|
|
|
*
|
|
|
|
* This is useful because it frees the ProxyHandler from having to implement
|
|
|
|
* any behavior having to do with the prototype chain.
|
|
|
|
*/
|
2012-08-10 15:55:55 +04:00
|
|
|
bool mHasPrototype;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-30 01:07:13 +04:00
|
|
|
/*
|
|
|
|
* All proxies indicate whether they have any sort of interesting security
|
|
|
|
* policy that might prevent the caller from doing something it wants to
|
|
|
|
* the object. In the case of wrappers, this distinction is used to
|
|
|
|
* determine whether the caller may strip off the wrapper if it so desires.
|
|
|
|
*/
|
|
|
|
bool mHasSecurityPolicy;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-05-19 06:21:43 +04:00
|
|
|
public:
|
2016-07-09 00:39:53 +03:00
|
|
|
explicit constexpr BaseProxyHandler(const void* aFamily,
|
|
|
|
bool aHasPrototype = false,
|
2014-08-29 00:47:16 +04:00
|
|
|
bool aHasSecurityPolicy = false)
|
|
|
|
: mFamily(aFamily),
|
|
|
|
mHasPrototype(aHasPrototype),
|
|
|
|
mHasSecurityPolicy(aHasSecurityPolicy) {}
|
2010-05-19 06:21:43 +04:00
|
|
|
|
2017-02-02 20:48:49 +03:00
|
|
|
bool hasPrototype() const { return mHasPrototype; }
|
2010-05-19 06:21:43 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
bool hasSecurityPolicy() const { return mHasSecurityPolicy; }
|
2014-09-24 00:03:40 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
inline const void* family() const { return mFamily; }
|
|
|
|
static size_t offsetOfFamily() { return offsetof(BaseProxyHandler, mFamily); }
|
2014-10-21 22:40:04 +04:00
|
|
|
|
2020-10-02 15:51:03 +03:00
|
|
|
virtual bool finalizeInBackground(const JS::Value& priv) const {
|
2014-10-08 21:09:08 +04:00
|
|
|
/*
|
|
|
|
* Called on creation of a proxy to determine whether its finalize
|
|
|
|
* method can be finalized on the background thread.
|
|
|
|
*/
|
2015-09-18 01:14:33 +03:00
|
|
|
return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-05-19 06:21:43 +04:00
|
|
|
|
2016-04-04 21:50:12 +03:00
|
|
|
virtual bool canNurseryAllocate() const {
|
2014-10-08 21:09:08 +04:00
|
|
|
/*
|
|
|
|
* Nursery allocation is allowed if and only if it is safe to not
|
|
|
|
* run |finalize| when the ProxyObject dies.
|
|
|
|
*/
|
2015-03-29 01:22:11 +03:00
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
/* Policy enforcement methods.
|
2018-11-30 13:46:48 +03:00
|
|
|
*
|
2015-03-29 01:22:11 +03:00
|
|
|
* enter() allows the policy to specify whether the caller may perform |act|
|
|
|
|
* on the proxy's |id| property. In the case when |act| is CALL, |id| is
|
|
|
|
* generally JSID_VOID. The |mayThrow| parameter indicates whether a
|
|
|
|
* handler that wants to throw custom exceptions when denying should do so
|
2017-02-02 20:48:49 +03:00
|
|
|
* or not.
|
2018-11-30 13:46:48 +03:00
|
|
|
*
|
2015-03-29 01:22:11 +03:00
|
|
|
* The |act| parameter to enter() specifies the action being performed.
|
|
|
|
* If |bp| is false, the method suggests that the caller throw (though it
|
|
|
|
* may still decide to squelch the error).
|
2018-11-30 13:46:48 +03:00
|
|
|
*
|
2015-03-29 01:22:11 +03:00
|
|
|
* We make these OR-able so that assertEnteredPolicy can pass a union of them.
|
|
|
|
* For example, get{,Own}PropertyDescriptor is invoked by calls to ::get()
|
|
|
|
* ::set(), in addition to being invoked on its own, so there are several
|
|
|
|
* valid Actions that could have been entered.
|
2018-11-30 13:46:48 +03:00
|
|
|
*/
|
2015-03-29 01:22:11 +03:00
|
|
|
typedef uint32_t Action;
|
2018-11-30 13:46:48 +03:00
|
|
|
enum {
|
2014-02-18 00:59:03 +04:00
|
|
|
NONE = 0x00,
|
|
|
|
GET = 0x01,
|
|
|
|
SET = 0x02,
|
2015-03-29 01:22:11 +03:00
|
|
|
CALL = 0x04,
|
2014-07-30 23:23:03 +04:00
|
|
|
ENUMERATE = 0x08,
|
|
|
|
GET_PROPERTY_DESCRIPTOR = 0x10
|
2018-11-30 13:46:48 +03:00
|
|
|
};
|
|
|
|
|
2020-10-02 15:51:03 +03:00
|
|
|
virtual bool enter(JSContext* cx, JS::HandleObject wrapper, JS::HandleId id,
|
2017-06-29 07:00:43 +03:00
|
|
|
Action act, bool mayThrow, bool* bp) const;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-06-29 07:00:43 +03:00
|
|
|
/* Standard internal methods. */
|
|
|
|
virtual bool getOwnPropertyDescriptor(
|
2020-10-02 15:51:03 +03:00
|
|
|
JSContext* cx, JS::HandleObject proxy, JS::HandleId id,
|
|
|
|
JS::MutableHandle<JS::PropertyDescriptor> desc) const = 0;
|
|
|
|
virtual bool defineProperty(JSContext* cx, JS::HandleObject proxy,
|
|
|
|
JS::HandleId id,
|
|
|
|
JS::Handle<JS::PropertyDescriptor> desc,
|
|
|
|
JS::ObjectOpResult& result) const = 0;
|
|
|
|
virtual bool ownPropertyKeys(JSContext* cx, JS::HandleObject proxy,
|
|
|
|
JS::MutableHandleIdVector props) const = 0;
|
|
|
|
virtual bool delete_(JSContext* cx, JS::HandleObject proxy, JS::HandleId id,
|
|
|
|
JS::ObjectOpResult& result) const = 0;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
|
|
|
/*
|
2015-08-27 22:18:37 +03:00
|
|
|
* These methods are standard, but the engine does not normally call them.
|
2015-03-29 01:22:11 +03:00
|
|
|
* They're opt-in. See "Proxy prototype chains" above.
|
2018-11-30 13:46:48 +03:00
|
|
|
*
|
2015-03-29 01:22:11 +03:00
|
|
|
* getPrototype() crashes if called. setPrototype() throws a TypeError.
|
2018-11-30 13:46:48 +03:00
|
|
|
*/
|
2020-10-02 15:51:03 +03:00
|
|
|
virtual bool getPrototype(JSContext* cx, JS::HandleObject proxy,
|
|
|
|
JS::MutableHandleObject protop) const;
|
|
|
|
virtual bool setPrototype(JSContext* cx, JS::HandleObject proxy,
|
|
|
|
JS::HandleObject proto,
|
|
|
|
JS::ObjectOpResult& result) const;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
/* Non-standard but conceptual kin to {g,s}etPrototype, so these live here. */
|
2020-10-02 15:51:03 +03:00
|
|
|
virtual bool getPrototypeIfOrdinary(JSContext* cx, JS::HandleObject proxy,
|
2016-02-24 00:42:30 +03:00
|
|
|
bool* isOrdinary,
|
2020-10-02 15:51:03 +03:00
|
|
|
JS::MutableHandleObject protop) const = 0;
|
|
|
|
virtual bool setImmutablePrototype(JSContext* cx, JS::HandleObject proxy,
|
2015-03-29 01:22:11 +03:00
|
|
|
bool* succeeded) const;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-10-02 15:51:03 +03:00
|
|
|
virtual bool preventExtensions(JSContext* cx, JS::HandleObject proxy,
|
|
|
|
JS::ObjectOpResult& result) const = 0;
|
|
|
|
virtual bool isExtensible(JSContext* cx, JS::HandleObject proxy,
|
2015-03-29 01:22:11 +03:00
|
|
|
bool* extensible) const = 0;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
|
|
|
/*
|
2014-10-08 21:09:08 +04:00
|
|
|
* These standard internal methods are implemented, as a convenience, so
|
|
|
|
* that ProxyHandler subclasses don't have to provide every single method.
|
2018-11-30 13:46:48 +03:00
|
|
|
*
|
2019-01-25 19:41:34 +03:00
|
|
|
* The base-class implementations work by calling getOwnPropertyDescriptor()
|
|
|
|
* and going up the [[Prototype]] chain if necessary. The algorithm for this
|
|
|
|
* follows what is defined for Ordinary Objects in the ES spec.
|
2014-10-08 21:09:08 +04:00
|
|
|
* They do not follow any standard. When in doubt, override them.
|
2018-11-30 13:46:48 +03:00
|
|
|
*/
|
2020-10-02 15:51:03 +03:00
|
|
|
virtual bool has(JSContext* cx, JS::HandleObject proxy, JS::HandleId id,
|
2015-03-29 01:22:11 +03:00
|
|
|
bool* bp) const;
|
2020-10-02 15:51:03 +03:00
|
|
|
virtual bool get(JSContext* cx, JS::HandleObject proxy,
|
|
|
|
JS::HandleValue receiver, JS::HandleId id,
|
|
|
|
JS::MutableHandleValue vp) const;
|
|
|
|
virtual bool set(JSContext* cx, JS::HandleObject proxy, JS::HandleId id,
|
|
|
|
JS::HandleValue v, JS::HandleValue receiver,
|
|
|
|
JS::ObjectOpResult& result) const;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-07-28 00:08:42 +03:00
|
|
|
// Use the ProxyExpando object for private fields, rather than taking the
|
|
|
|
// normal get/set/defineField paths.
|
|
|
|
virtual bool useProxyExpandoObjectForPrivateFields() const { return true; }
|
2020-07-20 16:49:12 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
/*
|
2014-10-08 21:09:08 +04:00
|
|
|
* [[Call]] and [[Construct]] are standard internal methods but according
|
2016-06-30 01:04:56 +03:00
|
|
|
* to the spec, they are not present on every object.
|
2018-11-30 13:46:48 +03:00
|
|
|
*
|
2016-06-30 01:04:56 +03:00
|
|
|
* SpiderMonkey never calls a proxy's call()/construct() internal method
|
|
|
|
* unless isCallable()/isConstructor() returns true for that proxy.
|
2018-11-30 13:46:48 +03:00
|
|
|
*
|
2015-08-23 11:10:24 +03:00
|
|
|
* BaseProxyHandler::isCallable()/isConstructor() always return false, and
|
|
|
|
* BaseProxyHandler::call()/construct() crash if called. So if you're
|
|
|
|
* creating a kind of that is never callable, you don't have to override
|
|
|
|
* anything, but otherwise you probably want to override all four.
|
2018-11-30 13:46:48 +03:00
|
|
|
*/
|
2020-10-02 15:51:03 +03:00
|
|
|
virtual bool call(JSContext* cx, JS::HandleObject proxy,
|
|
|
|
const JS::CallArgs& args) const;
|
|
|
|
virtual bool construct(JSContext* cx, JS::HandleObject proxy,
|
|
|
|
const JS::CallArgs& args) const;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-10-08 21:09:08 +04:00
|
|
|
/* SpiderMonkey extensions. */
|
2020-10-02 15:51:03 +03:00
|
|
|
virtual bool enumerate(JSContext* cx, JS::HandleObject proxy,
|
|
|
|
JS::MutableHandleIdVector props) const;
|
|
|
|
virtual bool hasOwn(JSContext* cx, JS::HandleObject proxy, JS::HandleId id,
|
2017-07-11 12:31:12 +03:00
|
|
|
bool* bp) const;
|
2020-10-02 15:51:03 +03:00
|
|
|
virtual bool getOwnEnumerablePropertyKeys(
|
|
|
|
JSContext* cx, JS::HandleObject proxy,
|
|
|
|
JS::MutableHandleIdVector props) const;
|
|
|
|
virtual bool nativeCall(JSContext* cx, JS::IsAcceptableThis test,
|
|
|
|
JS::NativeImpl impl, const JS::CallArgs& args) const;
|
|
|
|
virtual bool hasInstance(JSContext* cx, JS::HandleObject proxy,
|
|
|
|
JS::MutableHandleValue v, bool* bp) const;
|
|
|
|
virtual bool getBuiltinClass(JSContext* cx, JS::HandleObject proxy,
|
2015-03-29 01:22:11 +03:00
|
|
|
ESClass* cls) const;
|
2020-10-02 15:51:03 +03:00
|
|
|
virtual bool isArray(JSContext* cx, JS::HandleObject proxy,
|
2015-03-29 01:22:11 +03:00
|
|
|
JS::IsArrayAnswer* answer) const;
|
2020-10-02 15:51:03 +03:00
|
|
|
virtual const char* className(JSContext* cx, JS::HandleObject proxy) const;
|
|
|
|
virtual JSString* fun_toString(JSContext* cx, JS::HandleObject proxy,
|
2015-03-29 01:22:11 +03:00
|
|
|
bool isToSource) const;
|
|
|
|
virtual RegExpShared* regexp_toShared(JSContext* cx,
|
2020-10-02 15:51:03 +03:00
|
|
|
JS::HandleObject proxy) const;
|
|
|
|
virtual bool boxedValue_unbox(JSContext* cx, JS::HandleObject proxy,
|
|
|
|
JS::MutableHandleValue vp) const;
|
2017-09-19 14:31:30 +03:00
|
|
|
virtual void trace(JSTracer* trc, JSObject* proxy) const;
|
|
|
|
virtual void finalize(JSFreeOp* fop, JSObject* proxy) const;
|
2015-03-29 01:22:11 +03:00
|
|
|
virtual size_t objectMoved(JSObject* proxy, JSObject* old) const;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-09-11 02:52:36 +04:00
|
|
|
// Allow proxies, wrappers in particular, to specify callability at runtime.
|
2015-03-29 01:22:11 +03:00
|
|
|
// Note: These do not take const JSObject*, but they do in spirit.
|
2014-09-11 02:52:36 +04:00
|
|
|
// We are not prepared to do this, as there's little const correctness
|
|
|
|
// in the external APIs that handle proxies.
|
2015-03-29 01:22:11 +03:00
|
|
|
virtual bool isCallable(JSObject* obj) const;
|
|
|
|
virtual bool isConstructor(JSObject* obj) const;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-10-02 15:51:03 +03:00
|
|
|
virtual bool getElements(JSContext* cx, JS::HandleObject proxy,
|
|
|
|
uint32_t begin, uint32_t end,
|
|
|
|
ElementAdder* adder) const;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-06-27 15:44:06 +04:00
|
|
|
virtual bool isScripted() const { return false; }
|
2010-05-19 06:21:43 +04:00
|
|
|
};
|
|
|
|
|
2019-08-14 22:13:12 +03:00
|
|
|
extern JS_FRIEND_DATA const JSClass ProxyClass;
|
2013-08-27 08:39:37 +04:00
|
|
|
|
2015-03-12 02:54:11 +03:00
|
|
|
inline bool IsProxy(const JSObject* obj) {
|
2021-02-22 16:12:29 +03:00
|
|
|
return JS::GetClass(obj)->isProxyObject();
|
2011-10-04 18:06:54 +04:00
|
|
|
}
|
|
|
|
|
2016-03-24 23:24:51 +03:00
|
|
|
namespace detail {
|
2014-10-18 03:57:52 +04:00
|
|
|
|
2017-04-28 15:12:28 +03:00
|
|
|
// Proxy slot layout
|
|
|
|
// -----------------
|
|
|
|
//
|
|
|
|
// Every proxy has a ProxyValueArray that contains the following Values:
|
|
|
|
//
|
2020-07-20 16:49:12 +03:00
|
|
|
// - The expando slot. This is used to hold private fields should they be
|
|
|
|
// stamped into a non-forwarding proxy type.
|
2017-04-28 15:12:28 +03:00
|
|
|
// - The private slot.
|
|
|
|
// - The reserved slots. The number of slots is determined by the proxy's Class.
|
|
|
|
//
|
|
|
|
// Proxy objects store a pointer to the reserved slots (ProxyReservedSlots*).
|
|
|
|
// The ProxyValueArray and the private slot can be accessed using
|
|
|
|
// ProxyValueArray::fromReservedSlots or ProxyDataLayout::values.
|
|
|
|
//
|
|
|
|
// Storing a pointer to ProxyReservedSlots instead of ProxyValueArray has a
|
2020-09-09 01:55:38 +03:00
|
|
|
// number of advantages. In particular, it means JS::GetReservedSlot and
|
|
|
|
// JS::SetReservedSlot can be used with both proxies and native objects. This
|
2017-04-28 15:12:28 +03:00
|
|
|
// works because the ProxyReservedSlots* pointer is stored where native objects
|
|
|
|
// store their dynamic slots pointer.
|
|
|
|
|
|
|
|
struct ProxyReservedSlots {
|
2020-10-02 15:51:03 +03:00
|
|
|
JS::Value slots[1];
|
2017-04-28 15:12:28 +03:00
|
|
|
|
|
|
|
static inline int offsetOfPrivateSlot();
|
|
|
|
|
2018-01-12 14:47:19 +03:00
|
|
|
static inline int offsetOfSlot(size_t slot) {
|
2020-10-02 15:51:03 +03:00
|
|
|
return offsetof(ProxyReservedSlots, slots[0]) + slot * sizeof(JS::Value);
|
2018-01-12 14:47:19 +03:00
|
|
|
}
|
|
|
|
|
2017-04-28 15:12:28 +03:00
|
|
|
void init(size_t nreserved) {
|
|
|
|
for (size_t i = 0; i < nreserved; i++) {
|
|
|
|
slots[i] = JS::UndefinedValue();
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2017-04-28 15:12:28 +03:00
|
|
|
|
|
|
|
ProxyReservedSlots(const ProxyReservedSlots&) = delete;
|
|
|
|
void operator=(const ProxyReservedSlots&) = delete;
|
|
|
|
};
|
2014-10-18 03:57:52 +04:00
|
|
|
|
|
|
|
struct ProxyValueArray {
|
2020-10-02 15:51:03 +03:00
|
|
|
JS::Value expandoSlot;
|
|
|
|
JS::Value privateSlot;
|
2017-04-28 15:12:28 +03:00
|
|
|
ProxyReservedSlots reservedSlots;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-04-28 15:12:28 +03:00
|
|
|
void init(size_t nreserved) {
|
2020-07-20 16:49:12 +03:00
|
|
|
expandoSlot = JS::ObjectOrNullValue(nullptr);
|
2017-04-28 15:12:28 +03:00
|
|
|
privateSlot = JS::UndefinedValue();
|
|
|
|
reservedSlots.init(nreserved);
|
2014-10-18 03:57:52 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-04-28 15:12:28 +03:00
|
|
|
static size_t sizeOf(size_t nreserved) {
|
2020-10-02 15:51:03 +03:00
|
|
|
return offsetOfReservedSlots() + nreserved * sizeof(JS::Value);
|
2017-04-28 15:12:28 +03:00
|
|
|
}
|
|
|
|
static MOZ_ALWAYS_INLINE ProxyValueArray* fromReservedSlots(
|
|
|
|
ProxyReservedSlots* slots) {
|
|
|
|
uintptr_t p = reinterpret_cast<uintptr_t>(slots);
|
|
|
|
return reinterpret_cast<ProxyValueArray*>(p - offsetOfReservedSlots());
|
|
|
|
}
|
|
|
|
static size_t offsetOfReservedSlots() {
|
|
|
|
return offsetof(ProxyValueArray, reservedSlots);
|
2017-02-22 23:16:04 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-04-28 15:12:28 +03:00
|
|
|
ProxyValueArray(const ProxyValueArray&) = delete;
|
|
|
|
void operator=(const ProxyValueArray&) = delete;
|
2014-10-18 03:57:52 +04:00
|
|
|
};
|
|
|
|
|
2017-04-28 15:12:28 +03:00
|
|
|
/* static */ inline int ProxyReservedSlots::offsetOfPrivateSlot() {
|
|
|
|
return -int(ProxyValueArray::offsetOfReservedSlots()) +
|
|
|
|
offsetof(ProxyValueArray, privateSlot);
|
|
|
|
}
|
|
|
|
|
2014-10-18 03:57:52 +04:00
|
|
|
// All proxies share the same data layout. Following the object's shape and
|
|
|
|
// type, the proxy has a ProxyDataLayout structure with a pointer to an array
|
|
|
|
// of values and the proxy's handler. This is designed both so that proxies can
|
|
|
|
// be easily swapped with other objects (via RemapWrapper) and to mimic the
|
|
|
|
// layout of other objects (proxies and other objects have the same size) so
|
|
|
|
// that common code can access either type of object.
|
|
|
|
//
|
|
|
|
// See GetReservedOrProxyPrivateSlot below.
|
|
|
|
struct ProxyDataLayout {
|
2017-04-28 15:12:28 +03:00
|
|
|
ProxyReservedSlots* reservedSlots;
|
2015-03-29 01:22:11 +03:00
|
|
|
const BaseProxyHandler* handler;
|
2017-04-28 15:12:28 +03:00
|
|
|
|
|
|
|
MOZ_ALWAYS_INLINE ProxyValueArray* values() const {
|
|
|
|
return ProxyValueArray::fromReservedSlots(reservedSlots);
|
|
|
|
}
|
2014-10-18 03:57:52 +04:00
|
|
|
};
|
|
|
|
|
2021-03-06 02:11:29 +03:00
|
|
|
const uint32_t ProxyDataOffset = 2 * sizeof(void*);
|
2014-10-18 03:57:52 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
inline ProxyDataLayout* GetProxyDataLayout(JSObject* obj) {
|
2014-10-18 03:57:52 +04:00
|
|
|
MOZ_ASSERT(IsProxy(obj));
|
2015-03-29 01:22:11 +03:00
|
|
|
return reinterpret_cast<ProxyDataLayout*>(reinterpret_cast<uint8_t*>(obj) +
|
|
|
|
ProxyDataOffset);
|
2014-10-18 03:57:52 +04:00
|
|
|
}
|
2010-05-19 06:21:43 +04:00
|
|
|
|
2016-03-24 23:24:51 +03:00
|
|
|
inline const ProxyDataLayout* GetProxyDataLayout(const JSObject* obj) {
|
|
|
|
MOZ_ASSERT(IsProxy(obj));
|
|
|
|
return reinterpret_cast<const ProxyDataLayout*>(
|
|
|
|
reinterpret_cast<const uint8_t*>(obj) + ProxyDataOffset);
|
|
|
|
}
|
2018-04-20 11:09:52 +03:00
|
|
|
|
2020-10-02 15:51:03 +03:00
|
|
|
JS_FRIEND_API void SetValueInProxy(JS::Value* slot, const JS::Value& value);
|
2018-04-20 11:09:52 +03:00
|
|
|
|
|
|
|
inline void SetProxyReservedSlotUnchecked(JSObject* obj, size_t n,
|
2020-10-02 15:51:03 +03:00
|
|
|
const JS::Value& extra) {
|
2020-09-09 01:55:38 +03:00
|
|
|
MOZ_ASSERT(n < JSCLASS_RESERVED_SLOTS(JS::GetClass(obj)));
|
2018-04-20 11:09:52 +03:00
|
|
|
|
2020-10-02 15:51:03 +03:00
|
|
|
JS::Value* vp = &GetProxyDataLayout(obj)->reservedSlots->slots[n];
|
2018-04-20 11:09:52 +03:00
|
|
|
|
|
|
|
// Trigger a barrier before writing the slot.
|
|
|
|
if (vp->isGCThing() || extra.isGCThing()) {
|
|
|
|
SetValueInProxy(vp, extra);
|
|
|
|
} else {
|
|
|
|
*vp = extra;
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2018-04-20 11:09:52 +03:00
|
|
|
}
|
|
|
|
|
2016-03-24 23:24:51 +03:00
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
inline const BaseProxyHandler* GetProxyHandler(const JSObject* obj) {
|
|
|
|
return detail::GetProxyDataLayout(obj)->handler;
|
2010-05-19 06:21:43 +04:00
|
|
|
}
|
|
|
|
|
2020-10-02 15:51:03 +03:00
|
|
|
inline const JS::Value& GetProxyPrivate(const JSObject* obj) {
|
2017-04-28 15:12:28 +03:00
|
|
|
return detail::GetProxyDataLayout(obj)->values()->privateSlot;
|
2010-05-19 06:21:43 +04:00
|
|
|
}
|
|
|
|
|
2020-10-02 15:51:03 +03:00
|
|
|
inline const JS::Value& GetProxyExpando(const JSObject* obj) {
|
2020-07-20 16:49:12 +03:00
|
|
|
return detail::GetProxyDataLayout(obj)->values()->expandoSlot;
|
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
inline JSObject* GetProxyTargetObject(JSObject* obj) {
|
2012-05-17 15:19:37 +04:00
|
|
|
return GetProxyPrivate(obj).toObjectOrNull();
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:51:03 +03:00
|
|
|
inline const JS::Value& GetProxyReservedSlot(const JSObject* obj, size_t n) {
|
2020-09-09 01:55:38 +03:00
|
|
|
MOZ_ASSERT(n < JSCLASS_RESERVED_SLOTS(JS::GetClass(obj)));
|
2017-04-28 15:12:28 +03:00
|
|
|
return detail::GetProxyDataLayout(obj)->reservedSlots->slots[n];
|
2010-09-21 01:48:01 +04:00
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
inline void SetProxyHandler(JSObject* obj, const BaseProxyHandler* handler) {
|
2016-03-24 23:24:51 +03:00
|
|
|
detail::GetProxyDataLayout(obj)->handler = handler;
|
2012-04-26 08:03:53 +04:00
|
|
|
}
|
|
|
|
|
2020-10-02 15:51:03 +03:00
|
|
|
inline void SetProxyReservedSlot(JSObject* obj, size_t n,
|
|
|
|
const JS::Value& extra) {
|
2018-12-07 00:28:10 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (gc::detail::ObjectIsMarkedBlack(obj)) {
|
|
|
|
JS::AssertValueIsNotGray(extra);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-04-20 11:09:52 +03:00
|
|
|
detail::SetProxyReservedSlotUnchecked(obj, n, extra);
|
2010-09-21 01:48:01 +04:00
|
|
|
}
|
|
|
|
|
2020-10-02 15:51:03 +03:00
|
|
|
inline void SetProxyPrivate(JSObject* obj, const JS::Value& value) {
|
2018-12-07 00:28:10 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (gc::detail::ObjectIsMarkedBlack(obj)) {
|
|
|
|
JS::AssertValueIsNotGray(value);
|
|
|
|
}
|
|
|
|
#endif
|
2018-02-16 14:40:04 +03:00
|
|
|
|
2020-10-02 15:51:03 +03:00
|
|
|
JS::Value* vp = &detail::GetProxyDataLayout(obj)->values()->privateSlot;
|
2014-04-26 00:07:18 +04:00
|
|
|
|
2017-04-28 15:12:28 +03:00
|
|
|
// Trigger a barrier before writing the slot.
|
|
|
|
if (vp->isGCThing() || value.isGCThing()) {
|
2018-04-20 11:09:52 +03:00
|
|
|
detail::SetValueInProxy(vp, value);
|
2017-04-28 15:12:28 +03:00
|
|
|
} else {
|
|
|
|
*vp = value;
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2014-10-18 03:57:52 +04:00
|
|
|
}
|
|
|
|
|
2017-04-28 15:12:28 +03:00
|
|
|
inline bool IsScriptedProxy(const JSObject* obj) {
|
|
|
|
return IsProxy(obj) && GetProxyHandler(obj)->isScripted();
|
2014-10-18 03:57:52 +04:00
|
|
|
}
|
|
|
|
|
2013-10-04 15:29:35 +04:00
|
|
|
class MOZ_STACK_CLASS ProxyOptions {
|
2014-01-30 05:20:16 +04:00
|
|
|
protected:
|
|
|
|
/* protected constructor for subclass */
|
2020-04-14 19:57:15 +03:00
|
|
|
explicit ProxyOptions(bool lazyProtoArg)
|
|
|
|
: lazyProto_(lazyProtoArg), clasp_(&ProxyClass) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-10-04 15:29:35 +04:00
|
|
|
public:
|
2020-04-14 19:57:15 +03:00
|
|
|
ProxyOptions() : ProxyOptions(false) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-02-10 18:29:28 +03:00
|
|
|
bool lazyProto() const { return lazyProto_; }
|
2015-03-29 01:22:11 +03:00
|
|
|
ProxyOptions& setLazyProto(bool flag) {
|
2015-02-10 18:29:28 +03:00
|
|
|
lazyProto_ = flag;
|
|
|
|
return *this;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-08-14 22:13:12 +03:00
|
|
|
const JSClass* clasp() const { return clasp_; }
|
|
|
|
ProxyOptions& setClass(const JSClass* claspArg) {
|
2014-01-30 05:20:16 +04:00
|
|
|
clasp_ = claspArg;
|
|
|
|
return *this;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-10-04 15:29:35 +04:00
|
|
|
private:
|
2015-02-10 18:29:28 +03:00
|
|
|
bool lazyProto_;
|
2019-08-14 22:13:12 +03:00
|
|
|
const JSClass* clasp_;
|
2013-03-27 04:51:55 +04:00
|
|
|
};
|
|
|
|
|
2018-11-19 20:02:47 +03:00
|
|
|
JS_FRIEND_API JSObject* NewProxyObject(
|
2020-10-02 15:51:03 +03:00
|
|
|
JSContext* cx, const BaseProxyHandler* handler, JS::HandleValue priv,
|
2015-03-29 01:22:11 +03:00
|
|
|
JSObject* proto, const ProxyOptions& options = ProxyOptions());
|
2013-03-27 04:51:55 +04:00
|
|
|
|
2016-09-11 12:15:22 +03:00
|
|
|
JSObject* RenewProxyObject(JSContext* cx, JSObject* obj,
|
2020-10-02 15:51:03 +03:00
|
|
|
BaseProxyHandler* handler, const JS::Value& priv);
|
2012-09-12 04:14:24 +04:00
|
|
|
|
2018-11-19 20:02:47 +03:00
|
|
|
class JS_FRIEND_API AutoEnterPolicy {
|
2013-02-26 01:54:18 +04:00
|
|
|
public:
|
|
|
|
typedef BaseProxyHandler::Action Action;
|
2018-04-12 22:15:26 +03:00
|
|
|
AutoEnterPolicy(JSContext* cx, const BaseProxyHandler* handler,
|
2020-10-02 15:51:03 +03:00
|
|
|
JS::HandleObject wrapper, JS::HandleId id, Action act,
|
|
|
|
bool mayThrow)
|
2013-12-07 03:03:08 +04:00
|
|
|
#ifdef JS_DEBUG
|
2018-04-12 22:15:26 +03:00
|
|
|
: context(nullptr)
|
2013-02-26 01:54:18 +04:00
|
|
|
#endif
|
2013-02-26 01:54:18 +04:00
|
|
|
{
|
2017-02-02 20:48:49 +03:00
|
|
|
allow = handler->hasSecurityPolicy()
|
|
|
|
? handler->enter(cx, wrapper, id, act, mayThrow, &rv)
|
2014-01-30 01:07:13 +04:00
|
|
|
: true;
|
2014-02-13 22:54:07 +04:00
|
|
|
recordEnter(cx, wrapper, id, act);
|
2013-03-18 08:44:41 +04:00
|
|
|
// We want to throw an exception if all of the following are true:
|
|
|
|
// * The policy disallowed access.
|
|
|
|
// * The policy set rv to false, indicating that we should throw.
|
|
|
|
// * The caller did not instruct us to ignore exceptions.
|
|
|
|
// * The policy did not throw itself.
|
2013-08-29 04:20:24 +04:00
|
|
|
if (!allow && !rv && mayThrow) {
|
|
|
|
reportErrorIfExceptionIsNotPending(cx, id);
|
2013-02-26 01:54:18 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2013-02-26 01:54:18 +04:00
|
|
|
virtual ~AutoEnterPolicy() { recordLeave(); }
|
2013-02-26 01:54:18 +04:00
|
|
|
inline bool allowed() { return allow; }
|
2014-10-01 21:17:51 +04:00
|
|
|
inline bool returnValue() {
|
|
|
|
MOZ_ASSERT(!allowed());
|
|
|
|
return rv;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-02-26 01:54:18 +04:00
|
|
|
protected:
|
2013-02-26 01:54:18 +04:00
|
|
|
// no-op constructor for subclass
|
|
|
|
AutoEnterPolicy()
|
2013-12-07 03:03:08 +04:00
|
|
|
#ifdef JS_DEBUG
|
2018-04-12 22:15:26 +03:00
|
|
|
: context(nullptr),
|
|
|
|
enteredAction(BaseProxyHandler::NONE)
|
2013-02-26 01:54:18 +04:00
|
|
|
#endif
|
2018-04-12 22:15:26 +03:00
|
|
|
{
|
|
|
|
}
|
2020-10-02 15:51:03 +03:00
|
|
|
void reportErrorIfExceptionIsNotPending(JSContext* cx, JS::HandleId id);
|
2013-02-26 01:54:18 +04:00
|
|
|
bool allow;
|
|
|
|
bool rv;
|
2013-02-26 01:54:18 +04:00
|
|
|
|
2013-12-07 03:03:08 +04:00
|
|
|
#ifdef JS_DEBUG
|
2015-03-29 01:22:11 +03:00
|
|
|
JSContext* context;
|
2020-10-02 15:51:03 +03:00
|
|
|
mozilla::Maybe<JS::HandleObject> enteredProxy;
|
|
|
|
mozilla::Maybe<JS::HandleId> enteredId;
|
2014-02-13 22:54:07 +04:00
|
|
|
Action enteredAction;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-02-26 01:54:18 +04:00
|
|
|
// NB: We explicitly don't track the entered action here, because sometimes
|
2014-10-08 21:09:08 +04:00
|
|
|
// set() methods do an implicit get() during their implementation, leading
|
|
|
|
// to spurious assertions.
|
2015-03-29 01:22:11 +03:00
|
|
|
AutoEnterPolicy* prev;
|
2020-10-02 15:51:03 +03:00
|
|
|
void recordEnter(JSContext* cx, JS::HandleObject proxy, JS::HandleId id,
|
|
|
|
Action act);
|
2013-02-26 01:54:18 +04:00
|
|
|
void recordLeave();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-11-19 20:02:47 +03:00
|
|
|
friend JS_FRIEND_API void assertEnteredPolicy(JSContext* cx, JSObject* proxy,
|
|
|
|
jsid id, Action act);
|
2013-02-26 01:54:18 +04:00
|
|
|
#else
|
2015-03-29 01:22:11 +03:00
|
|
|
inline void recordEnter(JSContext* cx, JSObject* proxy, jsid id, Action act) {
|
|
|
|
}
|
2013-02-26 01:54:18 +04:00
|
|
|
inline void recordLeave() {}
|
|
|
|
#endif
|
|
|
|
|
2016-12-29 02:58:01 +03:00
|
|
|
private:
|
|
|
|
// This operator needs to be deleted explicitly, otherwise Visual C++ will
|
|
|
|
// create it automatically when it is part of the export JS API. In that
|
|
|
|
// case, compile would fail because HandleId is not allowed to be assigned
|
|
|
|
// and consequently instantiation of assign operator of mozilla::Maybe
|
|
|
|
// would fail. See bug 1325351 comment 16. Copy constructor is removed at
|
|
|
|
// the same time for consistency.
|
|
|
|
AutoEnterPolicy(const AutoEnterPolicy&) = delete;
|
|
|
|
AutoEnterPolicy& operator=(const AutoEnterPolicy&) = delete;
|
2013-02-26 01:54:18 +04:00
|
|
|
};
|
|
|
|
|
2013-12-07 03:03:08 +04:00
|
|
|
#ifdef JS_DEBUG
|
2018-11-19 20:02:47 +03:00
|
|
|
class JS_FRIEND_API AutoWaivePolicy : public AutoEnterPolicy {
|
2013-02-26 01:54:18 +04:00
|
|
|
public:
|
2020-10-02 15:51:03 +03:00
|
|
|
AutoWaivePolicy(JSContext* cx, JS::HandleObject proxy, JS::HandleId id,
|
2014-02-13 22:54:07 +04:00
|
|
|
BaseProxyHandler::Action act) {
|
2013-02-26 01:54:18 +04:00
|
|
|
allow = true;
|
2014-02-13 22:54:07 +04:00
|
|
|
recordEnter(cx, proxy, id, act);
|
2013-02-26 01:54:18 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
#else
|
2018-11-19 20:02:47 +03:00
|
|
|
class JS_FRIEND_API AutoWaivePolicy {
|
2014-02-13 22:54:07 +04:00
|
|
|
public:
|
2020-10-02 15:51:03 +03:00
|
|
|
AutoWaivePolicy(JSContext* cx, JS::HandleObject proxy, JS::HandleId id,
|
2014-02-13 22:54:07 +04:00
|
|
|
BaseProxyHandler::Action act) {}
|
2013-02-26 01:54:18 +04:00
|
|
|
};
|
2013-02-26 01:54:18 +04:00
|
|
|
#endif
|
2013-02-26 01:54:18 +04:00
|
|
|
|
2014-02-13 22:54:07 +04:00
|
|
|
#ifdef JS_DEBUG
|
2015-03-29 01:22:11 +03:00
|
|
|
extern JS_FRIEND_API void assertEnteredPolicy(JSContext* cx, JSObject* obj,
|
|
|
|
jsid id,
|
2014-02-13 22:54:07 +04:00
|
|
|
BaseProxyHandler::Action act);
|
|
|
|
#else
|
2015-03-29 01:22:11 +03:00
|
|
|
inline void assertEnteredPolicy(JSContext* cx, JSObject* obj, jsid id,
|
2014-02-13 22:54:07 +04:00
|
|
|
BaseProxyHandler::Action act) {}
|
|
|
|
#endif
|
|
|
|
|
2019-08-14 13:00:05 +03:00
|
|
|
extern JS_FRIEND_DATA const JSClassOps ProxyClassOps;
|
2018-11-19 20:02:47 +03:00
|
|
|
extern JS_FRIEND_DATA const js::ClassExtension ProxyClassExtension;
|
|
|
|
extern JS_FRIEND_DATA const js::ObjectOps ProxyObjectOps;
|
2017-04-29 15:41:49 +03:00
|
|
|
|
|
|
|
template <unsigned Flags>
|
|
|
|
constexpr unsigned CheckProxyFlags() {
|
2020-10-02 15:51:03 +03:00
|
|
|
constexpr size_t reservedSlots =
|
|
|
|
(Flags >> JSCLASS_RESERVED_SLOTS_SHIFT) & JSCLASS_RESERVED_SLOTS_MASK;
|
|
|
|
|
2017-04-29 15:41:49 +03:00
|
|
|
// For now assert each Proxy Class has at least 1 reserved slot. This is
|
|
|
|
// not a hard requirement, but helps catch Classes that need an explicit
|
|
|
|
// JSCLASS_HAS_RESERVED_SLOTS since bug 1360523.
|
2020-10-02 15:51:03 +03:00
|
|
|
static_assert(reservedSlots > 0,
|
2017-04-29 15:41:49 +03:00
|
|
|
"Proxy Classes must have at least 1 reserved slot");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-10-02 15:51:03 +03:00
|
|
|
constexpr size_t numSlots =
|
|
|
|
offsetof(js::detail::ProxyValueArray, reservedSlots) / sizeof(JS::Value);
|
|
|
|
|
2017-04-29 15:41:49 +03:00
|
|
|
// ProxyValueArray must fit inline in the object, so assert the number of
|
|
|
|
// slots does not exceed MAX_FIXED_SLOTS.
|
2020-10-02 15:51:03 +03:00
|
|
|
static_assert(numSlots + reservedSlots <= JS::shadow::Object::MAX_FIXED_SLOTS,
|
|
|
|
"ProxyValueArray size must not exceed max JSObject size");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-07-01 03:31:41 +03:00
|
|
|
// Proxies must not have the JSCLASS_SKIP_NURSERY_FINALIZE flag set: they
|
|
|
|
// always have finalizers, and whether they can be nursery allocated is
|
|
|
|
// controlled by the canNurseryAllocate() method on the proxy handler.
|
|
|
|
static_assert(!(Flags & JSCLASS_SKIP_NURSERY_FINALIZE),
|
|
|
|
"Proxies must not use JSCLASS_SKIP_NURSERY_FINALIZE; use "
|
|
|
|
"the canNurseryAllocate() proxy handler method instead.");
|
2017-04-29 15:41:49 +03:00
|
|
|
return Flags;
|
|
|
|
}
|
|
|
|
|
2019-11-15 20:42:55 +03:00
|
|
|
#define PROXY_CLASS_DEF_WITH_CLASS_SPEC(name, flags, classSpec) \
|
2017-04-29 15:41:49 +03:00
|
|
|
{ \
|
|
|
|
name, \
|
2019-08-14 22:13:12 +03:00
|
|
|
JSClass::NON_NATIVE | JSCLASS_IS_PROXY | \
|
2017-04-29 15:41:49 +03:00
|
|
|
JSCLASS_DELAY_METADATA_BUILDER | js::CheckProxyFlags<flags>(), \
|
2019-11-15 20:42:55 +03:00
|
|
|
&js::ProxyClassOps, classSpec, &js::ProxyClassExtension, \
|
2017-04-29 15:41:49 +03:00
|
|
|
&js::ProxyObjectOps \
|
|
|
|
}
|
|
|
|
|
2019-11-15 20:42:55 +03:00
|
|
|
#define PROXY_CLASS_DEF(name, flags) \
|
|
|
|
PROXY_CLASS_DEF_WITH_CLASS_SPEC(name, flags, JS_NULL_CLASS_SPEC)
|
|
|
|
|
Bug 1570484 - Nuke Xray waivers for remote outer window proxies. r=bzbarsky,tcampbell,jonco
Remote outer window proxies can't be the target of a CCW, because if
you attempt to wrap them we just create a new outer window proxy.
Therefore, they can't be the target of an Xray wrapper, so they can't
have Xray waivers that do anything useful. However, if we do a
navigation from a local iframe to a remote iframe, we'll transplant a
remote outer window proxy onto a local outer window proxy, which might
have an Xray. This can cause some issues, particularly if we later
navigate back to a different local window.
To work around this, this patch nukes Xray waivers on navigation to a
remote outer window proxy. This makes Xray waiver behavior
inconsistent with the non-Fission behavior, but it is safer to leave
the non-Fission behavior alone for now, for fear of breaking addons.
Differential Revision: https://phabricator.services.mozilla.com/D40116
--HG--
extra : moz-landing-system : lando
2019-08-05 19:32:31 +03:00
|
|
|
// Converts a proxy into a DeadObjectProxy that will throw exceptions on all
|
|
|
|
// access. This will run the proxy's finalizer to perform clean-up before the
|
|
|
|
// conversion happens.
|
2020-10-02 15:51:03 +03:00
|
|
|
JS_FRIEND_API void NukeNonCCWProxy(JSContext* cx, JS::HandleObject proxy);
|
Bug 1570484 - Nuke Xray waivers for remote outer window proxies. r=bzbarsky,tcampbell,jonco
Remote outer window proxies can't be the target of a CCW, because if
you attempt to wrap them we just create a new outer window proxy.
Therefore, they can't be the target of an Xray wrapper, so they can't
have Xray waivers that do anything useful. However, if we do a
navigation from a local iframe to a remote iframe, we'll transplant a
remote outer window proxy onto a local outer window proxy, which might
have an Xray. This can cause some issues, particularly if we later
navigate back to a different local window.
To work around this, this patch nukes Xray waivers on navigation to a
remote outer window proxy. This makes Xray waiver behavior
inconsistent with the non-Fission behavior, but it is safer to leave
the non-Fission behavior alone for now, for fear of breaking addons.
Differential Revision: https://phabricator.services.mozilla.com/D40116
--HG--
extra : moz-landing-system : lando
2019-08-05 19:32:31 +03:00
|
|
|
|
|
|
|
// This is a variant of js::NukeNonCCWProxy() for CCWs. It should only be called
|
|
|
|
// on CCWs that have been removed from CCW tables.
|
|
|
|
JS_FRIEND_API void NukeRemovedCrossCompartmentWrapper(JSContext* cx,
|
|
|
|
JSObject* wrapper);
|
|
|
|
|
2015-02-27 18:08:15 +03:00
|
|
|
} /* namespace js */
|
2010-05-19 06:21:43 +04:00
|
|
|
|
2015-02-10 21:58:18 +03:00
|
|
|
#endif /* js_Proxy_h */
|