2013-04-16 13:47:10 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=8 sts=4 et sw=4 tw=99:
|
2012-05-21 04:12:37 -07: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-18 19:21:43 -07:00
|
|
|
|
2014-09-09 14:31:20 -07:00
|
|
|
#ifndef jsproxy_h
|
|
|
|
#define jsproxy_h
|
2010-05-18 19:21:43 -07:00
|
|
|
|
2013-08-27 15:10:28 -07:00
|
|
|
#include "mozilla/Maybe.h"
|
|
|
|
|
2011-10-04 07:06:54 -07:00
|
|
|
#include "jsfriendapi.h"
|
2010-05-18 19:21:43 -07:00
|
|
|
|
2013-08-26 21:39:38 -07:00
|
|
|
#include "js/CallNonGenericMethod.h"
|
2013-08-28 17:20:24 -07:00
|
|
|
#include "js/Class.h"
|
2013-08-26 21:39:38 -07:00
|
|
|
|
2010-05-24 14:33:03 -07:00
|
|
|
namespace js {
|
|
|
|
|
2013-08-28 17:20:24 -07:00
|
|
|
using JS::AutoIdVector;
|
|
|
|
using JS::CallArgs;
|
|
|
|
using JS::HandleId;
|
|
|
|
using JS::HandleObject;
|
|
|
|
using JS::HandleValue;
|
|
|
|
using JS::IsAcceptableThis;
|
|
|
|
using JS::MutableHandle;
|
|
|
|
using JS::MutableHandleObject;
|
|
|
|
using JS::MutableHandleValue;
|
|
|
|
using JS::NativeImpl;
|
|
|
|
using JS::PrivateValue;
|
|
|
|
using JS::Value;
|
|
|
|
|
2013-07-25 21:23:14 -07:00
|
|
|
class RegExpGuard;
|
2012-07-17 09:54:41 -07:00
|
|
|
class JS_FRIEND_API(Wrapper);
|
2012-06-27 19:10:37 -07:00
|
|
|
|
2012-07-08 10:04:14 -07:00
|
|
|
/*
|
2014-10-08 10:09:08 -07: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 10:04:14 -07:00
|
|
|
*
|
2014-10-08 10:09:08 -07:00
|
|
|
* - the scope objects used by the Debugger's frame.eval() method
|
|
|
|
* (see js::GetDebugScopeForFunction)
|
2012-07-08 10:04:14 -07:00
|
|
|
*
|
2014-10-08 10:09:08 -07: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 10:04:14 -07:00
|
|
|
*
|
2014-10-08 10:09:08 -07:00
|
|
|
* - XPConnect security wrappers, which protect chrome from malicious content
|
|
|
|
* (js/xpconnect/wrappers)
|
2013-12-13 12:01:30 -08:00
|
|
|
*
|
2014-10-08 10:09:08 -07:00
|
|
|
* - DOM objects with special property behavior, like named getters
|
|
|
|
* (dom/bindings/Codegen.py generates these proxies from WebIDL)
|
|
|
|
*
|
|
|
|
* - semi-transparent use of objects that live in other processes
|
|
|
|
* (CPOWs, implemented in js/ipc)
|
|
|
|
*
|
|
|
|
* ### Proxies and internal methods
|
|
|
|
*
|
|
|
|
* ES6 draft rev 27 (24 August 2014) specifies 14 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:
|
|
|
|
*
|
|
|
|
* 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>)
|
|
|
|
* for (k in obj) {} obj.[[Enumerate]]()
|
|
|
|
*
|
|
|
|
* With regard to the implementation of these internal methods, there are three
|
|
|
|
* very different kinds of object in SpiderMonkey.
|
|
|
|
*
|
|
|
|
* 1. Native objects' internal methods are implemented in js::baseops in
|
|
|
|
* vm/NativeObject.cpp, with duplicate (but functionally identical)
|
|
|
|
* implementations scattered through the ICs and JITs.
|
|
|
|
*
|
|
|
|
* 2. Certain non-native objects have internal methods that are implemented as
|
|
|
|
* magical js::ObjectOps hooks. We're trying to get rid of these.
|
|
|
|
*
|
|
|
|
* 3. All other objects are proxies. A proxy's internal methods are
|
|
|
|
* implemented in C++, as the virtual methods of a C++ object stored on the
|
|
|
|
* proxy, known as its handler.
|
|
|
|
*
|
|
|
|
* This means that 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.
|
|
|
|
*
|
|
|
|
* ### 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-13 12:01:30 -08:00
|
|
|
*
|
2012-07-08 10:04:14 -07:00
|
|
|
* To minimize code duplication, a set of abstract proxy handler classes is
|
2014-10-08 10:09:08 -07:00
|
|
|
* provided, from which other handlers may inherit. These abstract classes are
|
|
|
|
* organized in the following hierarchy:
|
2012-07-08 10:04:14 -07:00
|
|
|
*
|
2014-10-08 10:09:08 -07:00
|
|
|
* BaseProxyHandler
|
|
|
|
* |
|
|
|
|
* DirectProxyHandler // has a target
|
|
|
|
* |
|
|
|
|
* Wrapper // can be unwrapped, revealing target
|
|
|
|
* | // (see js::CheckedUnwrap)
|
|
|
|
* |
|
|
|
|
* 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
|
|
|
|
* to any underlying JS object, DOMJSProxyHandler directly subclasses
|
|
|
|
* BaseProxyHandler.
|
|
|
|
*
|
|
|
|
* Gecko's security wrappers are examples of cross-compartment wrappers.
|
|
|
|
*
|
|
|
|
* ### Proxy prototype chains
|
|
|
|
*
|
|
|
|
* In addition to the normal methods, there are two models for proxy prototype
|
|
|
|
* chains.
|
|
|
|
*
|
|
|
|
* 1. Proxies can use the standard prototype mechanism used throughout the
|
|
|
|
* engine. To do so, simply pass a prototype to NewProxyObject() at
|
|
|
|
* creation time. All prototype accesses will then "just work" to treat the
|
|
|
|
* proxy as a "normal" object.
|
|
|
|
*
|
|
|
|
* 2. A proxy can implement more complicated prototype semantics (if, for
|
|
|
|
* example, it wants to delegate the prototype lookup to a wrapped object)
|
|
|
|
* by passing Proxy::LazyProto as the prototype at create time. This
|
|
|
|
* guarantees that the getPrototypeOf() handler method will be called every
|
|
|
|
* time the object's prototype chain is accessed.
|
|
|
|
*
|
|
|
|
* This system is implemented with two methods: {get,set}PrototypeOf. The
|
|
|
|
* default implementation of setPrototypeOf throws a TypeError. Since it is
|
|
|
|
* not possible to create an object without a sense of prototype chain,
|
|
|
|
* handlers must implement getPrototypeOf if opting in to the dynamic
|
|
|
|
* prototype system.
|
2012-07-08 10:04:14 -07: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 10:09:08 -07: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-05 12:58:20 -08:00
|
|
|
*
|
2014-10-08 10:09:08 -07: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 10:04:14 -07:00
|
|
|
*/
|
2013-03-22 19:43:03 -07:00
|
|
|
class JS_FRIEND_API(BaseProxyHandler)
|
|
|
|
{
|
2014-08-27 17:09:06 -07: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.
|
|
|
|
*/
|
2013-09-24 08:21:22 -07:00
|
|
|
const void *mFamily;
|
2014-04-25 13:07:18 -07: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:
|
|
|
|
* getPropertyDescriptor, has, set, enumerate, iterate. Instead, for
|
2014-10-08 10:09:08 -07:00
|
|
|
* these operations, it calls the "own" methods like
|
2014-10-08 20:01:55 -07:00
|
|
|
* getOwnPropertyDescriptor, hasOwn, defineProperty,
|
|
|
|
* getOwnEnumerablePropertyKeys, etc., and consults the prototype chain
|
|
|
|
* if needed.
|
2014-04-25 13:07:18 -07: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 04:55:55 -07:00
|
|
|
bool mHasPrototype;
|
2014-01-29 13:07:13 -08: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;
|
|
|
|
|
2010-05-18 19:21:43 -07:00
|
|
|
public:
|
2014-08-28 13:47:16 -07:00
|
|
|
explicit MOZ_CONSTEXPR BaseProxyHandler(const void *aFamily, bool aHasPrototype = false,
|
|
|
|
bool aHasSecurityPolicy = false)
|
|
|
|
: mFamily(aFamily),
|
|
|
|
mHasPrototype(aHasPrototype),
|
|
|
|
mHasSecurityPolicy(aHasSecurityPolicy)
|
|
|
|
{ }
|
2010-05-18 19:21:43 -07:00
|
|
|
|
2014-06-27 04:44:04 -07:00
|
|
|
bool hasPrototype() const {
|
2012-08-10 04:55:55 -07:00
|
|
|
return mHasPrototype;
|
|
|
|
}
|
|
|
|
|
2014-06-27 04:44:04 -07:00
|
|
|
bool hasSecurityPolicy() const {
|
2014-01-29 13:07:13 -08:00
|
|
|
return mHasSecurityPolicy;
|
2013-02-25 13:54:18 -08:00
|
|
|
}
|
|
|
|
|
2014-06-27 04:44:04 -07:00
|
|
|
inline const void *family() const {
|
2012-06-27 19:10:37 -07:00
|
|
|
return mFamily;
|
2012-06-29 15:59:42 -07:00
|
|
|
}
|
2013-08-10 22:20:36 -07:00
|
|
|
static size_t offsetOfFamily() {
|
|
|
|
return offsetof(BaseProxyHandler, mFamily);
|
|
|
|
}
|
2012-06-27 19:10:37 -07:00
|
|
|
|
2014-06-27 04:44:06 -07:00
|
|
|
virtual bool finalizeInBackground(Value priv) const {
|
2013-02-21 02:19:17 -08:00
|
|
|
/*
|
|
|
|
* Called on creation of a proxy to determine whether its finalize
|
|
|
|
* method can be finalized on the background thread.
|
|
|
|
*/
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-08 10:09:08 -07:00
|
|
|
/* Policy enforcement methods.
|
2013-02-25 13:54:18 -08: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
|
2013-12-16 18:27:43 -08:00
|
|
|
* generally JSID_VOID.
|
2013-02-25 13:54:18 -08:00
|
|
|
*
|
|
|
|
* The |act| parameter to enter() specifies the action being performed.
|
2014-10-08 10:09:08 -07:00
|
|
|
* If |bp| is false, the method suggests that the caller throw (though it
|
2013-02-25 13:54:18 -08:00
|
|
|
* may still decide to squelch the error).
|
2014-02-13 10:54:07 -08:00
|
|
|
*
|
|
|
|
* We make these OR-able so that assertEnteredPolicy can pass a union of them.
|
2014-07-30 12:23:03 -07:00
|
|
|
* 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.
|
2013-02-25 13:54:18 -08:00
|
|
|
*/
|
2014-02-13 10:54:07 -08:00
|
|
|
typedef uint32_t Action;
|
2014-02-17 12:59:03 -08:00
|
|
|
enum {
|
|
|
|
NONE = 0x00,
|
|
|
|
GET = 0x01,
|
|
|
|
SET = 0x02,
|
|
|
|
CALL = 0x04,
|
2014-07-30 12:23:03 -07:00
|
|
|
ENUMERATE = 0x08,
|
|
|
|
GET_PROPERTY_DESCRIPTOR = 0x10
|
2014-02-17 12:59:03 -08:00
|
|
|
};
|
2014-02-13 10:54:07 -08:00
|
|
|
|
2013-03-21 15:23:48 -07:00
|
|
|
virtual bool enter(JSContext *cx, HandleObject wrapper, HandleId id, Action act,
|
2014-06-27 04:44:06 -07:00
|
|
|
bool *bp) const;
|
2013-02-25 13:54:18 -08:00
|
|
|
|
2014-10-08 10:09:08 -07:00
|
|
|
/* Standard internal methods. */
|
|
|
|
virtual bool getOwnPropertyDescriptor(JSContext *cx, HandleObject proxy, HandleId id,
|
|
|
|
MutableHandle<JSPropertyDescriptor> desc) const = 0;
|
2013-03-21 15:23:47 -07:00
|
|
|
virtual bool defineProperty(JSContext *cx, HandleObject proxy, HandleId id,
|
2014-06-27 04:44:06 -07:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc) const = 0;
|
2014-09-26 13:16:36 -07:00
|
|
|
virtual bool ownPropertyKeys(JSContext *cx, HandleObject proxy,
|
|
|
|
AutoIdVector &props) const = 0;
|
2014-06-27 04:44:06 -07:00
|
|
|
virtual bool delete_(JSContext *cx, HandleObject proxy, HandleId id, bool *bp) const = 0;
|
|
|
|
virtual bool enumerate(JSContext *cx, HandleObject proxy, AutoIdVector &props) const = 0;
|
2014-10-08 10:09:08 -07:00
|
|
|
virtual bool isExtensible(JSContext *cx, HandleObject proxy, bool *extensible) const = 0;
|
|
|
|
virtual bool preventExtensions(JSContext *cx, HandleObject proxy) const = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These methods are standard, but the engine does not normally call them.
|
|
|
|
* They're opt-in. See "Proxy prototype chains" above.
|
|
|
|
*
|
|
|
|
* getPrototypeOf() crashes if called. setPrototypeOf() throws a TypeError.
|
|
|
|
*/
|
|
|
|
virtual bool getPrototypeOf(JSContext *cx, HandleObject proxy, MutableHandleObject protop) const;
|
|
|
|
virtual bool setPrototypeOf(JSContext *cx, HandleObject proxy, HandleObject proto, bool *bp) const;
|
2010-05-18 19:21:43 -07:00
|
|
|
|
2014-10-08 10:09:08 -07:00
|
|
|
/*
|
|
|
|
* These standard internal methods are implemented, as a convenience, so
|
|
|
|
* that ProxyHandler subclasses don't have to provide every single method.
|
|
|
|
*
|
|
|
|
* The base-class implementations work by calling getPropertyDescriptor().
|
|
|
|
* They do not follow any standard. When in doubt, override them.
|
|
|
|
*/
|
2014-06-27 04:44:06 -07:00
|
|
|
virtual bool has(JSContext *cx, HandleObject proxy, HandleId id, bool *bp) const;
|
2013-03-21 15:23:48 -07:00
|
|
|
virtual bool get(JSContext *cx, HandleObject proxy, HandleObject receiver,
|
2014-06-27 04:44:06 -07:00
|
|
|
HandleId id, MutableHandleValue vp) const;
|
2013-03-21 15:23:48 -07:00
|
|
|
virtual bool set(JSContext *cx, HandleObject proxy, HandleObject receiver,
|
2014-06-27 04:44:06 -07:00
|
|
|
HandleId id, bool strict, MutableHandleValue vp) const;
|
2010-05-18 19:21:43 -07:00
|
|
|
|
2014-10-08 10:09:08 -07:00
|
|
|
/*
|
|
|
|
* [[Call]] and [[Construct]] are standard internal methods but according
|
|
|
|
* to the spec, they are not present on every object.
|
|
|
|
*
|
|
|
|
* SpiderMonkey never calls a proxy's call()/construct() internal method
|
|
|
|
* unless isCallable()/isConstructor() returns true for that proxy.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-06-27 04:44:06 -07:00
|
|
|
virtual bool call(JSContext *cx, HandleObject proxy, const CallArgs &args) const;
|
|
|
|
virtual bool construct(JSContext *cx, HandleObject proxy, const CallArgs &args) const;
|
2014-10-08 10:09:08 -07:00
|
|
|
|
|
|
|
/* SpiderMonkey extensions. */
|
|
|
|
virtual bool getPropertyDescriptor(JSContext *cx, HandleObject proxy, HandleId id,
|
|
|
|
MutableHandle<JSPropertyDescriptor> desc) const = 0;
|
|
|
|
virtual bool hasOwn(JSContext *cx, HandleObject proxy, HandleId id, bool *bp) const;
|
2014-10-08 20:01:55 -07:00
|
|
|
virtual bool getOwnEnumerablePropertyKeys(JSContext *cx, HandleObject proxy,
|
|
|
|
AutoIdVector &props) const;
|
2014-10-08 10:09:08 -07:00
|
|
|
virtual bool iterate(JSContext *cx, HandleObject proxy, unsigned flags,
|
|
|
|
MutableHandleValue vp) const;
|
2014-06-27 04:44:06 -07:00
|
|
|
virtual bool nativeCall(JSContext *cx, IsAcceptableThis test, NativeImpl impl, CallArgs args) const;
|
|
|
|
virtual bool hasInstance(JSContext *cx, HandleObject proxy, MutableHandleValue v, bool *bp) const;
|
|
|
|
virtual bool objectClassIs(HandleObject obj, ESClassValue classValue, JSContext *cx) const;
|
|
|
|
virtual const char *className(JSContext *cx, HandleObject proxy) const;
|
|
|
|
virtual JSString *fun_toString(JSContext *cx, HandleObject proxy, unsigned indent) const;
|
|
|
|
virtual bool regexp_toShared(JSContext *cx, HandleObject proxy, RegExpGuard *g) const;
|
2014-08-18 14:18:39 -07:00
|
|
|
virtual bool boxedValue_unbox(JSContext *cx, HandleObject proxy, MutableHandleValue vp) const;
|
2014-06-27 04:44:06 -07:00
|
|
|
virtual bool defaultValue(JSContext *cx, HandleObject obj, JSType hint, MutableHandleValue vp) const;
|
|
|
|
virtual void finalize(JSFreeOp *fop, JSObject *proxy) const;
|
2014-09-02 02:07:22 -07:00
|
|
|
virtual void objectMoved(JSObject *proxy, const JSObject *old) const;
|
2012-10-08 18:22:47 -07:00
|
|
|
|
2014-09-10 15:52:36 -07:00
|
|
|
// Allow proxies, wrappers in particular, to specify callability at runtime.
|
|
|
|
// Note: These do not take const JSObject *, but they do in spirit.
|
|
|
|
// We are not prepared to do this, as there's little const correctness
|
|
|
|
// in the external APIs that handle proxies.
|
|
|
|
virtual bool isCallable(JSObject *obj) const;
|
|
|
|
virtual bool isConstructor(JSObject *obj) const;
|
|
|
|
|
2013-10-29 16:39:09 -07:00
|
|
|
// These two hooks must be overridden, or not overridden, in tandem -- no
|
|
|
|
// overriding just one!
|
|
|
|
virtual bool watch(JSContext *cx, JS::HandleObject proxy, JS::HandleId id,
|
2014-06-27 04:44:06 -07:00
|
|
|
JS::HandleObject callable) const;
|
|
|
|
virtual bool unwatch(JSContext *cx, JS::HandleObject proxy, JS::HandleId id) const;
|
2013-10-29 16:39:09 -07:00
|
|
|
|
2013-12-05 11:07:24 -08:00
|
|
|
virtual bool slice(JSContext *cx, HandleObject proxy, uint32_t begin, uint32_t end,
|
2014-06-27 04:44:06 -07:00
|
|
|
HandleObject result) const;
|
2013-12-05 11:07:24 -08:00
|
|
|
|
2013-08-21 22:26:57 -07:00
|
|
|
/* See comment for weakmapKeyDelegateOp in js/Class.h. */
|
2014-06-27 04:44:06 -07:00
|
|
|
virtual JSObject *weakmapKeyDelegate(JSObject *proxy) const;
|
|
|
|
virtual bool isScripted() const { return false; }
|
2010-05-18 19:21:43 -07:00
|
|
|
};
|
|
|
|
|
2012-07-08 10:04:14 -07:00
|
|
|
/*
|
2014-10-08 10:09:08 -07:00
|
|
|
* DirectProxyHandler includes a notion of a target object. All methods are
|
2012-10-29 08:52:53 -07:00
|
|
|
* reimplemented such that they forward their behavior to the target. This
|
|
|
|
* allows consumers of this class to forward to another object as transparently
|
|
|
|
* and efficiently as possible.
|
2013-12-05 12:58:20 -08:00
|
|
|
*
|
2014-10-08 10:09:08 -07:00
|
|
|
* Important: If you add a method implementation here, you probably also need
|
|
|
|
* to add an override in CrossCompartmentWrapper. If you don't, you risk
|
2013-12-05 12:58:20 -08:00
|
|
|
* compartment mismatches. See bug 945826 comment 0.
|
2012-07-08 10:04:14 -07:00
|
|
|
*/
|
2013-03-22 19:43:03 -07:00
|
|
|
class JS_PUBLIC_API(DirectProxyHandler) : public BaseProxyHandler
|
|
|
|
{
|
|
|
|
public:
|
2014-08-28 13:47:16 -07:00
|
|
|
explicit MOZ_CONSTEXPR DirectProxyHandler(const void *aFamily, bool aHasPrototype = false,
|
|
|
|
bool aHasSecurityPolicy = false)
|
|
|
|
: BaseProxyHandler(aFamily, aHasPrototype, aHasSecurityPolicy)
|
|
|
|
{ }
|
2012-05-17 04:19:37 -07:00
|
|
|
|
2014-10-08 10:09:08 -07:00
|
|
|
/* Standard internal methods. */
|
2014-04-25 14:11:01 -07:00
|
|
|
virtual bool getOwnPropertyDescriptor(JSContext *cx, HandleObject proxy, HandleId id,
|
2014-06-27 04:44:06 -07:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc) const MOZ_OVERRIDE;
|
2013-03-21 15:23:47 -07:00
|
|
|
virtual bool defineProperty(JSContext *cx, HandleObject proxy, HandleId id,
|
2014-06-27 04:44:06 -07:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc) const MOZ_OVERRIDE;
|
2014-09-26 13:16:36 -07:00
|
|
|
virtual bool ownPropertyKeys(JSContext *cx, HandleObject proxy,
|
|
|
|
AutoIdVector &props) const MOZ_OVERRIDE;
|
2013-03-21 15:23:48 -07:00
|
|
|
virtual bool delete_(JSContext *cx, HandleObject proxy, HandleId id,
|
2014-06-27 04:44:06 -07:00
|
|
|
bool *bp) const MOZ_OVERRIDE;
|
2013-03-21 15:23:48 -07:00
|
|
|
virtual bool enumerate(JSContext *cx, HandleObject proxy,
|
2014-06-27 04:44:06 -07:00
|
|
|
AutoIdVector &props) const MOZ_OVERRIDE;
|
2014-10-08 10:09:08 -07:00
|
|
|
virtual bool isExtensible(JSContext *cx, HandleObject proxy, bool *extensible) const MOZ_OVERRIDE;
|
|
|
|
virtual bool preventExtensions(JSContext *cx, HandleObject proxy) const MOZ_OVERRIDE;
|
|
|
|
virtual bool getPrototypeOf(JSContext *cx, HandleObject proxy,
|
|
|
|
MutableHandleObject protop) const MOZ_OVERRIDE;
|
|
|
|
virtual bool setPrototypeOf(JSContext *cx, HandleObject proxy, HandleObject proto,
|
|
|
|
bool *bp) const MOZ_OVERRIDE;
|
2013-03-21 15:23:48 -07:00
|
|
|
virtual bool has(JSContext *cx, HandleObject proxy, HandleId id,
|
2014-06-27 04:44:06 -07:00
|
|
|
bool *bp) const MOZ_OVERRIDE;
|
2013-03-21 15:23:48 -07:00
|
|
|
virtual bool get(JSContext *cx, HandleObject proxy, HandleObject receiver,
|
2014-06-27 04:44:06 -07:00
|
|
|
HandleId id, MutableHandleValue vp) const MOZ_OVERRIDE;
|
2013-03-21 15:23:48 -07:00
|
|
|
virtual bool set(JSContext *cx, HandleObject proxy, HandleObject receiver,
|
2014-06-27 04:44:06 -07:00
|
|
|
HandleId id, bool strict, MutableHandleValue vp) const MOZ_OVERRIDE;
|
2014-10-08 10:09:08 -07:00
|
|
|
virtual bool call(JSContext *cx, HandleObject proxy, const CallArgs &args) const MOZ_OVERRIDE;
|
|
|
|
virtual bool construct(JSContext *cx, HandleObject proxy, const CallArgs &args) const MOZ_OVERRIDE;
|
|
|
|
|
|
|
|
/* SpiderMonkey extensions. */
|
|
|
|
virtual bool getPropertyDescriptor(JSContext *cx, HandleObject proxy, HandleId id,
|
|
|
|
MutableHandle<JSPropertyDescriptor> desc) const MOZ_OVERRIDE;
|
|
|
|
virtual bool hasOwn(JSContext *cx, HandleObject proxy, HandleId id,
|
|
|
|
bool *bp) const MOZ_OVERRIDE;
|
2014-10-08 20:01:55 -07:00
|
|
|
virtual bool getOwnEnumerablePropertyKeys(JSContext *cx, HandleObject proxy,
|
|
|
|
AutoIdVector &props) const MOZ_OVERRIDE;
|
2013-03-21 15:23:48 -07:00
|
|
|
virtual bool iterate(JSContext *cx, HandleObject proxy, unsigned flags,
|
2014-06-27 04:44:06 -07:00
|
|
|
MutableHandleValue vp) const MOZ_OVERRIDE;
|
2012-07-03 17:44:22 -07:00
|
|
|
virtual bool nativeCall(JSContext *cx, IsAcceptableThis test, NativeImpl impl,
|
2014-06-27 04:44:06 -07:00
|
|
|
CallArgs args) const MOZ_OVERRIDE;
|
2012-09-04 16:40:12 -07:00
|
|
|
virtual bool hasInstance(JSContext *cx, HandleObject proxy, MutableHandleValue v,
|
2014-06-27 04:44:06 -07:00
|
|
|
bool *bp) const MOZ_OVERRIDE;
|
2013-03-21 15:23:48 -07:00
|
|
|
virtual bool objectClassIs(HandleObject obj, ESClassValue classValue,
|
2014-06-27 04:44:06 -07:00
|
|
|
JSContext *cx) const MOZ_OVERRIDE;
|
|
|
|
virtual const char *className(JSContext *cx, HandleObject proxy) const MOZ_OVERRIDE;
|
2013-03-21 15:23:48 -07:00
|
|
|
virtual JSString *fun_toString(JSContext *cx, HandleObject proxy,
|
2014-06-27 04:44:06 -07:00
|
|
|
unsigned indent) const MOZ_OVERRIDE;
|
2013-03-21 15:23:48 -07:00
|
|
|
virtual bool regexp_toShared(JSContext *cx, HandleObject proxy,
|
2014-06-27 04:44:06 -07:00
|
|
|
RegExpGuard *g) const MOZ_OVERRIDE;
|
2014-08-18 14:18:39 -07:00
|
|
|
virtual bool boxedValue_unbox(JSContext *cx, HandleObject proxy, MutableHandleValue vp) const;
|
2014-09-10 15:52:36 -07:00
|
|
|
virtual bool isCallable(JSObject *obj) const MOZ_OVERRIDE;
|
2014-10-08 10:09:08 -07:00
|
|
|
virtual JSObject *weakmapKeyDelegate(JSObject *proxy) const MOZ_OVERRIDE;
|
2012-05-17 04:19:37 -07:00
|
|
|
};
|
|
|
|
|
2014-09-10 15:52:36 -07:00
|
|
|
extern JS_FRIEND_DATA(const js::Class* const) ProxyClassPtr;
|
2013-08-26 21:39:37 -07:00
|
|
|
|
2013-04-30 15:41:12 -07:00
|
|
|
inline bool IsProxy(JSObject *obj)
|
2011-10-04 07:06:54 -07:00
|
|
|
{
|
2014-02-01 00:30:44 -08:00
|
|
|
return GetObjectClass(obj)->isProxy();
|
2011-10-04 07:06:54 -07:00
|
|
|
}
|
|
|
|
|
2012-12-06 12:21:19 -08:00
|
|
|
/*
|
2013-06-20 22:39:22 -07:00
|
|
|
* These are part of the API.
|
|
|
|
*
|
|
|
|
* NOTE: PROXY_PRIVATE_SLOT is 0 because that way slot 0 is usable by API
|
2012-12-06 12:21:19 -08:00
|
|
|
* clients for both proxy and non-proxy objects. So an API client that only
|
|
|
|
* needs to store one slot's worth of data doesn't need to branch on what sort
|
|
|
|
* of object it has.
|
|
|
|
*/
|
2014-01-29 17:20:16 -08:00
|
|
|
const uint32_t PROXY_PRIVATE_SLOT = 0;
|
|
|
|
const uint32_t PROXY_HANDLER_SLOT = 1;
|
|
|
|
const uint32_t PROXY_EXTRA_SLOT = 2;
|
|
|
|
const uint32_t PROXY_MINIMUM_SLOTS = 4;
|
2010-05-18 19:21:43 -07:00
|
|
|
|
2014-06-27 04:44:08 -07:00
|
|
|
inline const BaseProxyHandler *
|
2013-04-30 15:41:12 -07:00
|
|
|
GetProxyHandler(JSObject *obj)
|
2010-05-18 19:21:43 -07:00
|
|
|
{
|
2014-10-01 10:17:51 -07:00
|
|
|
MOZ_ASSERT(IsProxy(obj));
|
2014-06-27 04:44:08 -07:00
|
|
|
return (const BaseProxyHandler *) GetReservedSlot(obj, PROXY_HANDLER_SLOT).toPrivate();
|
2010-05-18 19:21:43 -07:00
|
|
|
}
|
|
|
|
|
2011-10-04 07:06:54 -07:00
|
|
|
inline const Value &
|
2013-04-30 15:41:12 -07:00
|
|
|
GetProxyPrivate(JSObject *obj)
|
2010-05-18 19:21:43 -07:00
|
|
|
{
|
2014-10-01 10:17:51 -07:00
|
|
|
MOZ_ASSERT(IsProxy(obj));
|
2013-06-20 22:39:22 -07:00
|
|
|
return GetReservedSlot(obj, PROXY_PRIVATE_SLOT);
|
2010-05-18 19:21:43 -07:00
|
|
|
}
|
|
|
|
|
2012-05-17 04:19:37 -07:00
|
|
|
inline JSObject *
|
2013-04-30 15:41:12 -07:00
|
|
|
GetProxyTargetObject(JSObject *obj)
|
2012-05-17 04:19:37 -07:00
|
|
|
{
|
2014-10-01 10:17:51 -07:00
|
|
|
MOZ_ASSERT(IsProxy(obj));
|
2012-05-17 04:19:37 -07:00
|
|
|
return GetProxyPrivate(obj).toObjectOrNull();
|
|
|
|
}
|
|
|
|
|
2012-05-23 16:31:26 -07:00
|
|
|
inline const Value &
|
2013-04-30 15:41:12 -07:00
|
|
|
GetProxyExtra(JSObject *obj, size_t n)
|
2010-09-20 14:48:01 -07:00
|
|
|
{
|
2014-10-01 10:17:51 -07:00
|
|
|
MOZ_ASSERT(IsProxy(obj));
|
2013-06-20 22:39:22 -07:00
|
|
|
return GetReservedSlot(obj, PROXY_EXTRA_SLOT + n);
|
2010-09-20 14:48:01 -07:00
|
|
|
}
|
|
|
|
|
2012-04-25 21:03:53 -07:00
|
|
|
inline void
|
2013-04-30 15:41:12 -07:00
|
|
|
SetProxyHandler(JSObject *obj, BaseProxyHandler *handler)
|
2012-04-25 21:03:53 -07:00
|
|
|
{
|
2014-10-01 10:17:51 -07:00
|
|
|
MOZ_ASSERT(IsProxy(obj));
|
2013-06-20 22:39:22 -07:00
|
|
|
SetReservedSlot(obj, PROXY_HANDLER_SLOT, PrivateValue(handler));
|
2012-04-25 21:03:53 -07:00
|
|
|
}
|
|
|
|
|
2010-09-20 14:48:01 -07:00
|
|
|
inline void
|
2013-04-30 15:41:12 -07:00
|
|
|
SetProxyExtra(JSObject *obj, size_t n, const Value &extra)
|
2010-09-20 14:48:01 -07:00
|
|
|
{
|
2014-10-01 10:17:51 -07:00
|
|
|
MOZ_ASSERT(IsProxy(obj));
|
|
|
|
MOZ_ASSERT(n <= 1);
|
2013-06-20 22:39:22 -07:00
|
|
|
SetReservedSlot(obj, PROXY_EXTRA_SLOT + n, extra);
|
2010-09-20 14:48:01 -07:00
|
|
|
}
|
|
|
|
|
2014-04-25 13:07:18 -07:00
|
|
|
inline bool
|
|
|
|
IsScriptedProxy(JSObject *obj)
|
|
|
|
{
|
|
|
|
return IsProxy(obj) && GetProxyHandler(obj)->isScripted();
|
|
|
|
}
|
|
|
|
|
2013-10-04 04:29:35 -07:00
|
|
|
class MOZ_STACK_CLASS ProxyOptions {
|
2014-01-29 17:20:16 -08:00
|
|
|
protected:
|
|
|
|
/* protected constructor for subclass */
|
2014-09-23 19:04:52 -07:00
|
|
|
explicit ProxyOptions(bool singletonArg)
|
2014-01-29 17:20:16 -08:00
|
|
|
: singleton_(singletonArg),
|
2014-09-10 15:52:36 -07:00
|
|
|
clasp_(ProxyClassPtr)
|
2014-01-29 17:20:16 -08:00
|
|
|
{}
|
|
|
|
|
2013-10-04 04:29:35 -07:00
|
|
|
public:
|
2014-01-29 17:20:16 -08:00
|
|
|
ProxyOptions() : singleton_(false),
|
2014-09-10 15:52:36 -07:00
|
|
|
clasp_(ProxyClassPtr)
|
2013-10-04 04:29:35 -07:00
|
|
|
{}
|
|
|
|
|
|
|
|
bool singleton() const { return singleton_; }
|
|
|
|
ProxyOptions &setSingleton(bool flag) {
|
|
|
|
singleton_ = flag;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-01-29 17:20:16 -08:00
|
|
|
const Class *clasp() const {
|
|
|
|
return clasp_;
|
|
|
|
}
|
|
|
|
ProxyOptions &setClass(const Class *claspArg) {
|
|
|
|
clasp_ = claspArg;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-10-04 04:29:35 -07:00
|
|
|
private:
|
|
|
|
bool singleton_;
|
2014-01-29 17:20:16 -08:00
|
|
|
const Class *clasp_;
|
2013-03-26 17:51:55 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
JS_FRIEND_API(JSObject *)
|
2014-06-27 04:44:04 -07:00
|
|
|
NewProxyObject(JSContext *cx, const BaseProxyHandler *handler, HandleValue priv,
|
2013-10-04 04:29:35 -07:00
|
|
|
JSObject *proto, JSObject *parent, const ProxyOptions &options = ProxyOptions());
|
2013-03-26 17:51:55 -07:00
|
|
|
|
2012-09-11 17:14:24 -07:00
|
|
|
JSObject *
|
|
|
|
RenewProxyObject(JSContext *cx, JSObject *obj, BaseProxyHandler *handler, Value priv);
|
|
|
|
|
2013-02-25 13:54:18 -08:00
|
|
|
class JS_FRIEND_API(AutoEnterPolicy)
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef BaseProxyHandler::Action Action;
|
2014-06-27 04:44:08 -07:00
|
|
|
AutoEnterPolicy(JSContext *cx, const BaseProxyHandler *handler,
|
2013-03-21 05:56:58 -07:00
|
|
|
HandleObject wrapper, HandleId id, Action act, bool mayThrow)
|
2013-12-06 15:03:08 -08:00
|
|
|
#ifdef JS_DEBUG
|
2013-10-07 09:44:15 -07:00
|
|
|
: context(nullptr)
|
2013-02-25 13:54:18 -08:00
|
|
|
#endif
|
2013-02-25 13:54:18 -08:00
|
|
|
{
|
2014-01-29 13:07:13 -08:00
|
|
|
allow = handler->hasSecurityPolicy() ? handler->enter(cx, wrapper, id, act, &rv)
|
|
|
|
: true;
|
2014-02-13 10:54:07 -08:00
|
|
|
recordEnter(cx, wrapper, id, act);
|
2013-03-17 21:44:41 -07: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-28 17:20:24 -07:00
|
|
|
if (!allow && !rv && mayThrow)
|
|
|
|
reportErrorIfExceptionIsNotPending(cx, id);
|
2013-02-25 13:54:18 -08:00
|
|
|
}
|
|
|
|
|
2013-02-25 13:54:18 -08:00
|
|
|
virtual ~AutoEnterPolicy() { recordLeave(); }
|
2013-02-25 13:54:18 -08:00
|
|
|
inline bool allowed() { return allow; }
|
2014-10-01 10:17:51 -07:00
|
|
|
inline bool returnValue() { MOZ_ASSERT(!allowed()); return rv; }
|
2013-02-25 13:54:18 -08:00
|
|
|
|
|
|
|
protected:
|
2013-02-25 13:54:18 -08:00
|
|
|
// no-op constructor for subclass
|
|
|
|
AutoEnterPolicy()
|
2013-12-06 15:03:08 -08:00
|
|
|
#ifdef JS_DEBUG
|
2013-10-07 09:44:15 -07:00
|
|
|
: context(nullptr)
|
2014-02-13 11:55:11 -08:00
|
|
|
, enteredAction(BaseProxyHandler::NONE)
|
2013-02-25 13:54:18 -08:00
|
|
|
#endif
|
2014-08-18 12:20:39 -07:00
|
|
|
{}
|
2013-08-28 17:20:24 -07:00
|
|
|
void reportErrorIfExceptionIsNotPending(JSContext *cx, jsid id);
|
2013-02-25 13:54:18 -08:00
|
|
|
bool allow;
|
|
|
|
bool rv;
|
2013-02-25 13:54:18 -08:00
|
|
|
|
2013-12-06 15:03:08 -08:00
|
|
|
#ifdef JS_DEBUG
|
2013-02-25 13:54:18 -08:00
|
|
|
JSContext *context;
|
2013-03-21 05:56:58 -07:00
|
|
|
mozilla::Maybe<HandleObject> enteredProxy;
|
|
|
|
mozilla::Maybe<HandleId> enteredId;
|
2014-02-13 10:54:07 -08:00
|
|
|
Action enteredAction;
|
|
|
|
|
2013-02-25 13:54:18 -08:00
|
|
|
// NB: We explicitly don't track the entered action here, because sometimes
|
2014-10-08 10:09:08 -07:00
|
|
|
// set() methods do an implicit get() during their implementation, leading
|
|
|
|
// to spurious assertions.
|
2013-02-25 13:54:18 -08:00
|
|
|
AutoEnterPolicy *prev;
|
2014-02-13 10:54:07 -08:00
|
|
|
void recordEnter(JSContext *cx, HandleObject proxy, HandleId id, Action act);
|
2013-02-25 13:54:18 -08:00
|
|
|
void recordLeave();
|
|
|
|
|
2014-02-13 10:54:07 -08:00
|
|
|
friend JS_FRIEND_API(void) assertEnteredPolicy(JSContext *cx, JSObject *proxy, jsid id, Action act);
|
2013-02-25 13:54:18 -08:00
|
|
|
#else
|
2014-02-13 10:54:07 -08:00
|
|
|
inline void recordEnter(JSContext *cx, JSObject *proxy, jsid id, Action act) {}
|
2013-02-25 13:54:18 -08:00
|
|
|
inline void recordLeave() {}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-12-06 15:03:08 -08:00
|
|
|
#ifdef JS_DEBUG
|
2013-02-25 13:54:18 -08:00
|
|
|
class JS_FRIEND_API(AutoWaivePolicy) : public AutoEnterPolicy {
|
|
|
|
public:
|
2014-02-13 10:54:07 -08:00
|
|
|
AutoWaivePolicy(JSContext *cx, HandleObject proxy, HandleId id,
|
|
|
|
BaseProxyHandler::Action act)
|
2013-02-25 13:54:18 -08:00
|
|
|
{
|
|
|
|
allow = true;
|
2014-02-13 10:54:07 -08:00
|
|
|
recordEnter(cx, proxy, id, act);
|
2013-02-25 13:54:18 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
class JS_FRIEND_API(AutoWaivePolicy) {
|
2014-02-13 10:54:07 -08:00
|
|
|
public:
|
|
|
|
AutoWaivePolicy(JSContext *cx, HandleObject proxy, HandleId id,
|
|
|
|
BaseProxyHandler::Action act)
|
|
|
|
{}
|
2013-02-25 13:54:18 -08:00
|
|
|
};
|
2013-02-25 13:54:18 -08:00
|
|
|
#endif
|
2013-02-25 13:54:18 -08:00
|
|
|
|
2014-02-13 10:54:07 -08:00
|
|
|
#ifdef JS_DEBUG
|
|
|
|
extern JS_FRIEND_API(void)
|
|
|
|
assertEnteredPolicy(JSContext *cx, JSObject *obj, jsid id,
|
|
|
|
BaseProxyHandler::Action act);
|
|
|
|
#else
|
|
|
|
inline void assertEnteredPolicy(JSContext *cx, JSObject *obj, jsid id,
|
|
|
|
BaseProxyHandler::Action act)
|
2014-08-18 12:20:39 -07:00
|
|
|
{}
|
2014-02-13 10:54:07 -08:00
|
|
|
#endif
|
|
|
|
|
2011-10-04 07:06:54 -07:00
|
|
|
} /* namespace js */
|
2010-05-24 14:33:03 -07:00
|
|
|
|
2010-05-18 19:21:43 -07:00
|
|
|
extern JS_FRIEND_API(JSObject *)
|
2013-06-21 06:12:46 -07:00
|
|
|
js_InitProxyClass(JSContext *cx, JS::HandleObject obj);
|
2010-05-18 19:21:43 -07:00
|
|
|
|
2013-06-19 17:59:46 -07:00
|
|
|
#endif /* jsproxy_h */
|