2013-08-07 09:43:25 +08: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-10-19 11:48:42 +08: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/. */
|
2012-08-28 02:41:49 +08:00
|
|
|
|
2013-10-30 21:55:19 +08:00
|
|
|
#ifndef jsproxy_h
|
|
|
|
#define jsproxy_h
|
2012-08-28 02:41:49 +08:00
|
|
|
|
2014-02-07 14:05:03 +08:00
|
|
|
#include "mozilla/Maybe.h"
|
|
|
|
|
2012-08-28 02:41:49 +08:00
|
|
|
#include "jsfriendapi.h"
|
|
|
|
|
2014-02-07 14:05:03 +08:00
|
|
|
#include "js/CallNonGenericMethod.h"
|
|
|
|
#include "js/Class.h"
|
|
|
|
|
2012-08-28 02:41:49 +08:00
|
|
|
namespace js {
|
|
|
|
|
2014-02-07 14:05:03 +08: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;
|
|
|
|
|
|
|
|
class RegExpGuard;
|
2012-10-31 10:33:43 +08:00
|
|
|
class JS_FRIEND_API(Wrapper);
|
2012-10-19 11:48:42 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A proxy is a JSObject that implements generic behavior by providing custom
|
|
|
|
* implementations for each object trap. The implementation for each trap is
|
|
|
|
* provided by a C++ object stored on the proxy, known as its handler.
|
|
|
|
*
|
|
|
|
* A major use case for proxies is to forward each trap to another object,
|
|
|
|
* known as its target. The target can be an arbitrary C++ object. Not every
|
|
|
|
* proxy has the notion of a target, however.
|
|
|
|
*
|
|
|
|
* Proxy traps are grouped into fundamental and derived traps. Every proxy has
|
|
|
|
* to at least provide implementations for the fundamental traps, but the
|
2013-02-27 16:57:36 +08:00
|
|
|
* derived traps can be implemented in terms of the fundamental ones
|
|
|
|
* BaseProxyHandler provides implementations of the derived traps in terms of
|
|
|
|
* the (pure virtual) fundamental traps.
|
2012-10-19 11:48:42 +08:00
|
|
|
*
|
|
|
|
* To minimize code duplication, a set of abstract proxy handler classes is
|
|
|
|
* provided, from which other handlers may inherit. These abstract classes
|
|
|
|
* are organized in the following hierarchy:
|
|
|
|
*
|
|
|
|
* BaseProxyHandler
|
|
|
|
* |
|
|
|
|
* DirectProxyHandler
|
2013-02-27 16:57:36 +08:00
|
|
|
* |
|
|
|
|
* Wrapper
|
2012-10-19 11:48:42 +08: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
|
|
|
|
* default implementation for the fundamental traps. It does, however, implement
|
|
|
|
* the derived traps in terms of the fundamental ones. This allows consumers of
|
|
|
|
* this class to define any custom behavior they want.
|
|
|
|
*/
|
2013-06-27 16:06:05 +08:00
|
|
|
class JS_FRIEND_API(BaseProxyHandler)
|
|
|
|
{
|
2014-02-07 14:05:03 +08:00
|
|
|
const void *mFamily;
|
2012-10-31 10:33:43 +08:00
|
|
|
bool mHasPrototype;
|
2013-06-27 16:06:05 +08:00
|
|
|
bool mHasPolicy;
|
2012-10-31 10:33:43 +08:00
|
|
|
protected:
|
|
|
|
// Subclasses may set this in their constructor.
|
2013-05-22 17:31:07 +08:00
|
|
|
void setHasPrototype(bool aHasPrototype) { mHasPrototype = aHasPrototype; }
|
2013-06-27 16:06:05 +08:00
|
|
|
void setHasPolicy(bool aHasPolicy) { mHasPolicy = aHasPolicy; }
|
2012-10-31 10:33:43 +08:00
|
|
|
|
2012-08-28 02:41:49 +08:00
|
|
|
public:
|
2014-02-07 14:05:03 +08:00
|
|
|
explicit BaseProxyHandler(const void *family);
|
2012-10-19 11:48:42 +08:00
|
|
|
virtual ~BaseProxyHandler();
|
|
|
|
|
2012-10-31 10:33:43 +08:00
|
|
|
bool hasPrototype() {
|
|
|
|
return mHasPrototype;
|
|
|
|
}
|
|
|
|
|
2013-06-27 16:06:05 +08:00
|
|
|
bool hasPolicy() {
|
|
|
|
return mHasPolicy;
|
|
|
|
}
|
|
|
|
|
2014-02-07 14:05:03 +08:00
|
|
|
inline const void *family() {
|
2012-10-19 11:48:42 +08:00
|
|
|
return mFamily;
|
|
|
|
}
|
2014-02-07 14:05:03 +08:00
|
|
|
static size_t offsetOfFamily() {
|
|
|
|
return offsetof(BaseProxyHandler, mFamily);
|
|
|
|
}
|
2012-10-19 11:48:42 +08:00
|
|
|
|
|
|
|
virtual bool isOuterWindow() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-27 16:06:05 +08:00
|
|
|
virtual bool finalizeInBackground(Value priv) {
|
|
|
|
/*
|
|
|
|
* Called on creation of a proxy to determine whether its finalize
|
|
|
|
* method can be finalized on the background thread.
|
|
|
|
*/
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Policy enforcement traps.
|
|
|
|
*
|
|
|
|
* 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 |act| parameter to enter() specifies the action being performed.
|
|
|
|
* If |bp| is false, the trap suggests that the caller throw (though it
|
|
|
|
* may still decide to squelch the error).
|
|
|
|
*/
|
|
|
|
enum Action {
|
|
|
|
GET,
|
|
|
|
SET,
|
|
|
|
CALL
|
|
|
|
};
|
|
|
|
virtual bool enter(JSContext *cx, HandleObject wrapper, HandleId id, Action act,
|
|
|
|
bool *bp);
|
|
|
|
|
2012-08-28 02:41:49 +08:00
|
|
|
/* ES5 Harmony fundamental proxy traps. */
|
2013-06-27 16:06:05 +08:00
|
|
|
virtual bool preventExtensions(JSContext *cx, HandleObject proxy) = 0;
|
|
|
|
virtual bool getPropertyDescriptor(JSContext *cx, HandleObject proxy, HandleId id,
|
2014-02-07 14:05:03 +08:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
unsigned flags) = 0;
|
2013-06-27 16:06:05 +08:00
|
|
|
virtual bool getOwnPropertyDescriptor(JSContext *cx, HandleObject proxy,
|
2014-02-07 14:05:03 +08:00
|
|
|
HandleId id, MutableHandle<JSPropertyDescriptor> desc,
|
2013-06-27 16:06:05 +08:00
|
|
|
unsigned flags) = 0;
|
|
|
|
virtual bool defineProperty(JSContext *cx, HandleObject proxy, HandleId id,
|
2014-02-07 14:05:03 +08:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc) = 0;
|
2013-06-27 16:06:05 +08:00
|
|
|
virtual bool getOwnPropertyNames(JSContext *cx, HandleObject proxy,
|
2012-10-19 11:48:42 +08:00
|
|
|
AutoIdVector &props) = 0;
|
2013-06-27 16:06:05 +08:00
|
|
|
virtual bool delete_(JSContext *cx, HandleObject proxy, HandleId id, bool *bp) = 0;
|
|
|
|
virtual bool enumerate(JSContext *cx, HandleObject proxy, AutoIdVector &props) = 0;
|
2012-08-28 02:41:49 +08:00
|
|
|
|
|
|
|
/* ES5 Harmony derived proxy traps. */
|
2013-06-27 16:06:05 +08:00
|
|
|
virtual bool has(JSContext *cx, HandleObject proxy, HandleId id, bool *bp);
|
|
|
|
virtual bool hasOwn(JSContext *cx, HandleObject proxy, HandleId id, bool *bp);
|
|
|
|
virtual bool get(JSContext *cx, HandleObject proxy, HandleObject receiver,
|
|
|
|
HandleId id, MutableHandleValue vp);
|
|
|
|
virtual bool set(JSContext *cx, HandleObject proxy, HandleObject receiver,
|
|
|
|
HandleId id, bool strict, MutableHandleValue vp);
|
|
|
|
virtual bool keys(JSContext *cx, HandleObject proxy, AutoIdVector &props);
|
|
|
|
virtual bool iterate(JSContext *cx, HandleObject proxy, unsigned flags,
|
|
|
|
MutableHandleValue vp);
|
2012-08-28 02:41:49 +08:00
|
|
|
|
|
|
|
/* Spidermonkey extensions. */
|
2013-10-30 21:55:19 +08:00
|
|
|
virtual bool isExtensible(JSContext *cx, HandleObject proxy, bool *extensible) = 0;
|
2013-08-07 09:43:25 +08:00
|
|
|
virtual bool call(JSContext *cx, HandleObject proxy, const CallArgs &args);
|
|
|
|
virtual bool construct(JSContext *cx, HandleObject proxy, const CallArgs &args);
|
2012-10-31 10:33:43 +08:00
|
|
|
virtual bool nativeCall(JSContext *cx, IsAcceptableThis test, NativeImpl impl, CallArgs args);
|
2013-01-09 13:42:21 +08:00
|
|
|
virtual bool hasInstance(JSContext *cx, HandleObject proxy, MutableHandleValue v, bool *bp);
|
2013-06-27 16:06:05 +08:00
|
|
|
virtual bool objectClassIs(HandleObject obj, ESClassValue classValue, JSContext *cx);
|
2013-08-07 09:43:25 +08:00
|
|
|
virtual const char *className(JSContext *cx, HandleObject proxy);
|
2013-06-27 16:06:05 +08:00
|
|
|
virtual JSString *fun_toString(JSContext *cx, HandleObject proxy, unsigned indent);
|
|
|
|
virtual bool regexp_toShared(JSContext *cx, HandleObject proxy, RegExpGuard *g);
|
|
|
|
virtual bool defaultValue(JSContext *cx, HandleObject obj, JSType hint, MutableHandleValue vp);
|
2012-08-28 02:41:49 +08:00
|
|
|
virtual void finalize(JSFreeOp *fop, JSObject *proxy);
|
2013-06-27 16:06:05 +08:00
|
|
|
virtual bool getElementIfPresent(JSContext *cx, HandleObject obj, HandleObject receiver,
|
|
|
|
uint32_t index, MutableHandleValue vp, bool *present);
|
|
|
|
virtual bool getPrototypeOf(JSContext *cx, HandleObject proxy, MutableHandleObject protop);
|
2012-10-31 10:33:43 +08:00
|
|
|
|
2014-02-07 14:05:03 +08: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,
|
|
|
|
JS::HandleObject callable);
|
|
|
|
virtual bool unwatch(JSContext *cx, JS::HandleObject proxy, JS::HandleId id);
|
|
|
|
|
|
|
|
/* See comment for weakmapKeyDelegateOp in js/Class.h. */
|
2012-10-31 10:33:43 +08:00
|
|
|
virtual JSObject *weakmapKeyDelegate(JSObject *proxy);
|
2012-10-19 11:48:42 +08:00
|
|
|
};
|
2012-08-28 02:41:49 +08:00
|
|
|
|
2012-10-19 11:48:42 +08:00
|
|
|
/*
|
2013-02-27 16:57:36 +08:00
|
|
|
* DirectProxyHandler includes a notion of a target object. All traps are
|
|
|
|
* 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.
|
2012-10-19 11:48:42 +08:00
|
|
|
*/
|
2013-06-27 16:06:05 +08:00
|
|
|
class JS_PUBLIC_API(DirectProxyHandler) : public BaseProxyHandler
|
|
|
|
{
|
|
|
|
public:
|
2014-02-07 14:05:03 +08:00
|
|
|
explicit DirectProxyHandler(const void *family);
|
2012-08-28 02:41:49 +08:00
|
|
|
|
2012-10-19 11:48:42 +08:00
|
|
|
/* ES5 Harmony fundamental proxy traps. */
|
2013-06-27 16:06:05 +08:00
|
|
|
virtual bool preventExtensions(JSContext *cx, HandleObject proxy) MOZ_OVERRIDE;
|
|
|
|
virtual bool getPropertyDescriptor(JSContext *cx, HandleObject proxy, HandleId id,
|
2014-02-07 14:05:03 +08:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc, unsigned flags) MOZ_OVERRIDE;
|
2013-06-27 16:06:05 +08:00
|
|
|
virtual bool getOwnPropertyDescriptor(JSContext *cx, HandleObject proxy,
|
2014-02-07 14:05:03 +08:00
|
|
|
HandleId id, MutableHandle<JSPropertyDescriptor> desc,
|
2013-04-10 11:19:43 +08:00
|
|
|
unsigned flags) MOZ_OVERRIDE;
|
2013-06-27 16:06:05 +08:00
|
|
|
virtual bool defineProperty(JSContext *cx, HandleObject proxy, HandleId id,
|
2014-02-07 14:05:03 +08:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc) MOZ_OVERRIDE;
|
2013-06-27 16:06:05 +08:00
|
|
|
virtual bool getOwnPropertyNames(JSContext *cx, HandleObject proxy,
|
2012-10-19 11:48:42 +08:00
|
|
|
AutoIdVector &props) MOZ_OVERRIDE;
|
2013-06-27 16:06:05 +08:00
|
|
|
virtual bool delete_(JSContext *cx, HandleObject proxy, HandleId id,
|
2012-10-19 11:48:42 +08:00
|
|
|
bool *bp) MOZ_OVERRIDE;
|
2013-06-27 16:06:05 +08:00
|
|
|
virtual bool enumerate(JSContext *cx, HandleObject proxy,
|
2012-10-19 11:48:42 +08:00
|
|
|
AutoIdVector &props) MOZ_OVERRIDE;
|
|
|
|
|
2013-02-27 16:57:36 +08:00
|
|
|
/* ES5 Harmony derived proxy traps. */
|
2013-06-27 16:06:05 +08:00
|
|
|
virtual bool has(JSContext *cx, HandleObject proxy, HandleId id,
|
2013-02-27 16:57:36 +08:00
|
|
|
bool *bp) MOZ_OVERRIDE;
|
2013-06-27 16:06:05 +08:00
|
|
|
virtual bool hasOwn(JSContext *cx, HandleObject proxy, HandleId id,
|
2013-02-27 16:57:36 +08:00
|
|
|
bool *bp) MOZ_OVERRIDE;
|
2013-06-27 16:06:05 +08:00
|
|
|
virtual bool get(JSContext *cx, HandleObject proxy, HandleObject receiver,
|
|
|
|
HandleId id, MutableHandleValue vp) MOZ_OVERRIDE;
|
|
|
|
virtual bool set(JSContext *cx, HandleObject proxy, HandleObject receiver,
|
|
|
|
HandleId id, bool strict, MutableHandleValue vp) MOZ_OVERRIDE;
|
|
|
|
virtual bool keys(JSContext *cx, HandleObject proxy,
|
2013-02-27 16:57:36 +08:00
|
|
|
AutoIdVector &props) MOZ_OVERRIDE;
|
2013-06-27 16:06:05 +08:00
|
|
|
virtual bool iterate(JSContext *cx, HandleObject proxy, unsigned flags,
|
|
|
|
MutableHandleValue vp) MOZ_OVERRIDE;
|
2013-02-27 16:57:36 +08:00
|
|
|
|
2012-10-19 11:48:42 +08:00
|
|
|
/* Spidermonkey extensions. */
|
2013-10-30 21:55:19 +08:00
|
|
|
virtual bool isExtensible(JSContext *cx, HandleObject proxy, bool *extensible) MOZ_OVERRIDE;
|
2013-08-07 09:43:25 +08:00
|
|
|
virtual bool call(JSContext *cx, HandleObject proxy, const CallArgs &args) MOZ_OVERRIDE;
|
|
|
|
virtual bool construct(JSContext *cx, HandleObject proxy, const CallArgs &args) MOZ_OVERRIDE;
|
2012-10-31 10:33:43 +08:00
|
|
|
virtual bool nativeCall(JSContext *cx, IsAcceptableThis test, NativeImpl impl,
|
|
|
|
CallArgs args) MOZ_OVERRIDE;
|
2013-01-09 13:42:21 +08:00
|
|
|
virtual bool hasInstance(JSContext *cx, HandleObject proxy, MutableHandleValue v,
|
2012-10-19 11:48:42 +08:00
|
|
|
bool *bp) MOZ_OVERRIDE;
|
2013-06-27 16:06:05 +08:00
|
|
|
virtual bool objectClassIs(HandleObject obj, ESClassValue classValue,
|
2012-10-19 11:48:42 +08:00
|
|
|
JSContext *cx) MOZ_OVERRIDE;
|
2013-08-07 09:43:25 +08:00
|
|
|
virtual const char *className(JSContext *cx, HandleObject proxy) MOZ_OVERRIDE;
|
2013-06-27 16:06:05 +08:00
|
|
|
virtual JSString *fun_toString(JSContext *cx, HandleObject proxy,
|
2012-10-19 11:48:42 +08:00
|
|
|
unsigned indent) MOZ_OVERRIDE;
|
2013-06-27 16:06:05 +08:00
|
|
|
virtual bool regexp_toShared(JSContext *cx, HandleObject proxy,
|
2012-10-19 11:48:42 +08:00
|
|
|
RegExpGuard *g) MOZ_OVERRIDE;
|
2012-10-31 10:33:43 +08:00
|
|
|
virtual JSObject *weakmapKeyDelegate(JSObject *proxy);
|
2012-10-19 11:48:42 +08:00
|
|
|
};
|
|
|
|
|
2012-08-28 02:41:49 +08:00
|
|
|
/* Dispatch point for handlers that executes the appropriate C++ or scripted traps. */
|
2013-06-27 16:06:05 +08:00
|
|
|
class Proxy
|
|
|
|
{
|
2012-08-28 02:41:49 +08:00
|
|
|
public:
|
|
|
|
/* ES5 Harmony fundamental proxy traps. */
|
2013-06-27 16:06:05 +08:00
|
|
|
static bool preventExtensions(JSContext *cx, HandleObject proxy);
|
|
|
|
static bool getPropertyDescriptor(JSContext *cx, HandleObject proxy, HandleId id,
|
2014-02-07 14:05:03 +08:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc, unsigned flags);
|
2013-06-27 16:06:05 +08:00
|
|
|
static bool getPropertyDescriptor(JSContext *cx, HandleObject proxy, unsigned flags, HandleId id,
|
|
|
|
MutableHandleValue vp);
|
|
|
|
static bool getOwnPropertyDescriptor(JSContext *cx, HandleObject proxy, HandleId id,
|
2014-02-07 14:05:03 +08:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc, unsigned flags);
|
2013-06-27 16:06:05 +08:00
|
|
|
static bool getOwnPropertyDescriptor(JSContext *cx, HandleObject proxy, unsigned flags, HandleId id,
|
|
|
|
MutableHandleValue vp);
|
2014-02-07 14:05:03 +08:00
|
|
|
static bool defineProperty(JSContext *cx, HandleObject proxy, HandleId id,
|
|
|
|
MutableHandle<JSPropertyDescriptor> desc);
|
2013-06-27 16:06:05 +08:00
|
|
|
static bool defineProperty(JSContext *cx, HandleObject proxy, HandleId id, HandleValue v);
|
|
|
|
static bool getOwnPropertyNames(JSContext *cx, HandleObject proxy, AutoIdVector &props);
|
|
|
|
static bool delete_(JSContext *cx, HandleObject proxy, HandleId id, bool *bp);
|
|
|
|
static bool enumerate(JSContext *cx, HandleObject proxy, AutoIdVector &props);
|
2012-08-28 02:41:49 +08:00
|
|
|
|
|
|
|
/* ES5 Harmony derived proxy traps. */
|
2013-06-27 16:06:05 +08:00
|
|
|
static bool has(JSContext *cx, HandleObject proxy, HandleId id, bool *bp);
|
|
|
|
static bool hasOwn(JSContext *cx, HandleObject proxy, HandleId id, bool *bp);
|
|
|
|
static bool get(JSContext *cx, HandleObject proxy, HandleObject receiver, HandleId id,
|
|
|
|
MutableHandleValue vp);
|
2012-10-31 10:33:43 +08:00
|
|
|
static bool getElementIfPresent(JSContext *cx, HandleObject proxy, HandleObject receiver,
|
|
|
|
uint32_t index, MutableHandleValue vp, bool *present);
|
2013-06-27 16:06:05 +08:00
|
|
|
static bool set(JSContext *cx, HandleObject proxy, HandleObject receiver, HandleId id,
|
|
|
|
bool strict, MutableHandleValue vp);
|
|
|
|
static bool keys(JSContext *cx, HandleObject proxy, AutoIdVector &props);
|
2012-10-31 10:33:43 +08:00
|
|
|
static bool iterate(JSContext *cx, HandleObject proxy, unsigned flags, MutableHandleValue vp);
|
2012-08-28 02:41:49 +08:00
|
|
|
|
|
|
|
/* Spidermonkey extensions. */
|
2013-10-30 21:55:19 +08:00
|
|
|
static bool isExtensible(JSContext *cx, HandleObject proxy, bool *extensible);
|
2013-08-07 09:43:25 +08:00
|
|
|
static bool call(JSContext *cx, HandleObject proxy, const CallArgs &args);
|
|
|
|
static bool construct(JSContext *cx, HandleObject proxy, const CallArgs &args);
|
2012-10-31 10:33:43 +08:00
|
|
|
static bool nativeCall(JSContext *cx, IsAcceptableThis test, NativeImpl impl, CallArgs args);
|
2013-01-09 13:42:21 +08:00
|
|
|
static bool hasInstance(JSContext *cx, HandleObject proxy, MutableHandleValue v, bool *bp);
|
2013-06-27 16:06:05 +08:00
|
|
|
static bool objectClassIs(HandleObject obj, ESClassValue classValue, JSContext *cx);
|
2013-08-07 09:43:25 +08:00
|
|
|
static const char *className(JSContext *cx, HandleObject proxy);
|
2013-06-27 16:06:05 +08:00
|
|
|
static JSString *fun_toString(JSContext *cx, HandleObject proxy, unsigned indent);
|
|
|
|
static bool regexp_toShared(JSContext *cx, HandleObject proxy, RegExpGuard *g);
|
|
|
|
static bool defaultValue(JSContext *cx, HandleObject obj, JSType hint, MutableHandleValue vp);
|
|
|
|
static bool getPrototypeOf(JSContext *cx, HandleObject proxy, MutableHandleObject protop);
|
2013-01-09 13:42:21 +08:00
|
|
|
|
2014-02-07 14:05:03 +08:00
|
|
|
static bool watch(JSContext *cx, JS::HandleObject proxy, JS::HandleId id,
|
|
|
|
JS::HandleObject callable);
|
|
|
|
static bool unwatch(JSContext *cx, JS::HandleObject proxy, JS::HandleId id);
|
|
|
|
|
|
|
|
/* IC entry path for handling __noSuchMethod__ on access. */
|
|
|
|
static bool callProp(JSContext *cx, HandleObject proxy, HandleObject reveiver, HandleId id,
|
|
|
|
MutableHandleValue vp);
|
|
|
|
|
2013-01-09 13:42:21 +08:00
|
|
|
static JSObject * const LazyProto;
|
2012-08-28 02:41:49 +08:00
|
|
|
};
|
|
|
|
|
2014-02-07 14:05:03 +08:00
|
|
|
// Use these in places where you don't want to #include vm/ProxyObject.h.
|
|
|
|
extern JS_FRIEND_DATA(const js::Class* const) CallableProxyClassPtr;
|
|
|
|
extern JS_FRIEND_DATA(const js::Class* const) UncallableProxyClassPtr;
|
|
|
|
extern JS_FRIEND_DATA(const js::Class* const) OuterWindowProxyClassPtr;
|
2013-10-30 21:55:19 +08:00
|
|
|
|
|
|
|
inline bool IsProxyClass(const Class *clasp)
|
|
|
|
{
|
2014-02-07 14:05:03 +08:00
|
|
|
return clasp == CallableProxyClassPtr ||
|
|
|
|
clasp == UncallableProxyClassPtr ||
|
|
|
|
clasp == OuterWindowProxyClassPtr;
|
2012-08-28 02:41:49 +08:00
|
|
|
}
|
|
|
|
|
2013-08-07 09:43:25 +08:00
|
|
|
inline bool IsProxy(JSObject *obj)
|
2012-08-28 02:41:49 +08:00
|
|
|
{
|
2013-10-30 21:55:19 +08:00
|
|
|
return IsProxyClass(GetObjectClass(obj));
|
2012-08-28 02:41:49 +08:00
|
|
|
}
|
|
|
|
|
2013-04-10 11:19:43 +08:00
|
|
|
/*
|
2013-10-30 21:55:19 +08:00
|
|
|
* These are part of the API.
|
|
|
|
*
|
|
|
|
* NOTE: PROXY_PRIVATE_SLOT is 0 because that way slot 0 is usable by API
|
2013-04-10 11:19:43 +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.
|
|
|
|
*/
|
2013-10-30 21:55:19 +08:00
|
|
|
const uint32_t PROXY_PRIVATE_SLOT = 0;
|
|
|
|
const uint32_t PROXY_HANDLER_SLOT = 1;
|
|
|
|
const uint32_t PROXY_EXTRA_SLOT = 2;
|
2012-08-28 02:41:49 +08:00
|
|
|
|
2012-10-19 11:48:42 +08:00
|
|
|
inline BaseProxyHandler *
|
2013-08-07 09:43:25 +08:00
|
|
|
GetProxyHandler(JSObject *obj)
|
2012-08-28 02:41:49 +08:00
|
|
|
{
|
|
|
|
JS_ASSERT(IsProxy(obj));
|
2013-10-30 21:55:19 +08:00
|
|
|
return (BaseProxyHandler *) GetReservedSlot(obj, PROXY_HANDLER_SLOT).toPrivate();
|
2012-08-28 02:41:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline const Value &
|
2013-08-07 09:43:25 +08:00
|
|
|
GetProxyPrivate(JSObject *obj)
|
2012-08-28 02:41:49 +08:00
|
|
|
{
|
|
|
|
JS_ASSERT(IsProxy(obj));
|
2013-10-30 21:55:19 +08:00
|
|
|
return GetReservedSlot(obj, PROXY_PRIVATE_SLOT);
|
2012-08-28 02:41:49 +08:00
|
|
|
}
|
|
|
|
|
2012-10-19 11:48:42 +08:00
|
|
|
inline JSObject *
|
2013-08-07 09:43:25 +08:00
|
|
|
GetProxyTargetObject(JSObject *obj)
|
2012-10-19 11:48:42 +08:00
|
|
|
{
|
|
|
|
JS_ASSERT(IsProxy(obj));
|
|
|
|
return GetProxyPrivate(obj).toObjectOrNull();
|
|
|
|
}
|
|
|
|
|
2012-08-28 02:41:49 +08:00
|
|
|
inline const Value &
|
2013-08-07 09:43:25 +08:00
|
|
|
GetProxyExtra(JSObject *obj, size_t n)
|
2012-08-28 02:41:49 +08:00
|
|
|
{
|
|
|
|
JS_ASSERT(IsProxy(obj));
|
2013-10-30 21:55:19 +08:00
|
|
|
return GetReservedSlot(obj, PROXY_EXTRA_SLOT + n);
|
2012-08-28 02:41:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
2013-08-07 09:43:25 +08:00
|
|
|
SetProxyHandler(JSObject *obj, BaseProxyHandler *handler)
|
2012-08-28 02:41:49 +08:00
|
|
|
{
|
|
|
|
JS_ASSERT(IsProxy(obj));
|
2013-10-30 21:55:19 +08:00
|
|
|
SetReservedSlot(obj, PROXY_HANDLER_SLOT, PrivateValue(handler));
|
2012-08-28 02:41:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
2013-08-07 09:43:25 +08:00
|
|
|
SetProxyExtra(JSObject *obj, size_t n, const Value &extra)
|
2012-08-28 02:41:49 +08:00
|
|
|
{
|
|
|
|
JS_ASSERT(IsProxy(obj));
|
2012-10-19 11:48:42 +08:00
|
|
|
JS_ASSERT(n <= 1);
|
2013-10-30 21:55:19 +08:00
|
|
|
SetReservedSlot(obj, PROXY_EXTRA_SLOT + n, extra);
|
2012-08-28 02:41:49 +08:00
|
|
|
}
|
|
|
|
|
2014-02-07 14:05:03 +08:00
|
|
|
class MOZ_STACK_CLASS ProxyOptions {
|
|
|
|
public:
|
|
|
|
ProxyOptions() : callable_(false),
|
|
|
|
singleton_(false),
|
|
|
|
forceForegroundFinalization_(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool callable() const { return callable_; }
|
|
|
|
ProxyOptions &setCallable(bool flag) {
|
|
|
|
callable_ = flag;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool singleton() const { return singleton_; }
|
|
|
|
ProxyOptions &setSingleton(bool flag) {
|
|
|
|
singleton_ = flag;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool forceForegroundFinalization() const {
|
|
|
|
return forceForegroundFinalization_;
|
|
|
|
}
|
|
|
|
ProxyOptions &setForceForegroundFinalization(bool flag) {
|
|
|
|
forceForegroundFinalization_ = true;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool callable_;
|
|
|
|
bool singleton_;
|
|
|
|
bool forceForegroundFinalization_;
|
2013-06-27 16:06:05 +08:00
|
|
|
};
|
|
|
|
|
2012-08-28 02:41:49 +08:00
|
|
|
JS_FRIEND_API(JSObject *)
|
2013-10-30 21:55:19 +08:00
|
|
|
NewProxyObject(JSContext *cx, BaseProxyHandler *handler, HandleValue priv,
|
2014-02-07 14:05:03 +08:00
|
|
|
JSObject *proto, JSObject *parent, const ProxyOptions &options = ProxyOptions());
|
2012-08-28 02:41:49 +08:00
|
|
|
|
2013-02-27 16:57:36 +08:00
|
|
|
JSObject *
|
|
|
|
RenewProxyObject(JSContext *cx, JSObject *obj, BaseProxyHandler *handler, Value priv);
|
2012-08-28 02:41:49 +08:00
|
|
|
|
2013-06-27 16:06:05 +08:00
|
|
|
class JS_FRIEND_API(AutoEnterPolicy)
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef BaseProxyHandler::Action Action;
|
|
|
|
AutoEnterPolicy(JSContext *cx, BaseProxyHandler *handler,
|
|
|
|
HandleObject wrapper, HandleId id, Action act, bool mayThrow)
|
|
|
|
#ifdef DEBUG
|
2014-02-07 14:05:03 +08:00
|
|
|
: context(nullptr)
|
2013-06-27 16:06:05 +08:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
allow = handler->hasPolicy() ? handler->enter(cx, wrapper, id, act, &rv)
|
|
|
|
: true;
|
|
|
|
recordEnter(cx, wrapper, id);
|
|
|
|
// 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.
|
2014-02-07 14:05:03 +08:00
|
|
|
if (!allow && !rv && mayThrow)
|
|
|
|
reportErrorIfExceptionIsNotPending(cx, id);
|
2013-06-27 16:06:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~AutoEnterPolicy() { recordLeave(); }
|
|
|
|
inline bool allowed() { return allow; }
|
|
|
|
inline bool returnValue() { JS_ASSERT(!allowed()); return rv; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// no-op constructor for subclass
|
|
|
|
AutoEnterPolicy()
|
|
|
|
#ifdef DEBUG
|
2014-02-07 14:05:03 +08:00
|
|
|
: context(nullptr)
|
2013-06-27 16:06:05 +08:00
|
|
|
#endif
|
|
|
|
{};
|
2014-02-07 14:05:03 +08:00
|
|
|
void reportErrorIfExceptionIsNotPending(JSContext *cx, jsid id);
|
2013-06-27 16:06:05 +08:00
|
|
|
bool allow;
|
|
|
|
bool rv;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
JSContext *context;
|
|
|
|
mozilla::Maybe<HandleObject> enteredProxy;
|
|
|
|
mozilla::Maybe<HandleId> enteredId;
|
|
|
|
// NB: We explicitly don't track the entered action here, because sometimes
|
|
|
|
// SET traps do an implicit GET during their implementation, leading to
|
|
|
|
// spurious assertions.
|
|
|
|
AutoEnterPolicy *prev;
|
|
|
|
void recordEnter(JSContext *cx, HandleObject proxy, HandleId id);
|
|
|
|
void recordLeave();
|
|
|
|
|
|
|
|
friend JS_FRIEND_API(void) assertEnteredPolicy(JSContext *cx, JSObject *proxy, jsid id);
|
|
|
|
#else
|
|
|
|
inline void recordEnter(JSContext *cx, JSObject *proxy, jsid id) {}
|
|
|
|
inline void recordLeave() {}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
class JS_FRIEND_API(AutoWaivePolicy) : public AutoEnterPolicy {
|
|
|
|
public:
|
|
|
|
AutoWaivePolicy(JSContext *cx, HandleObject proxy, HandleId id)
|
|
|
|
{
|
|
|
|
allow = true;
|
|
|
|
recordEnter(cx, proxy, id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
class JS_FRIEND_API(AutoWaivePolicy) {
|
|
|
|
public: AutoWaivePolicy(JSContext *cx, HandleObject proxy, HandleId id) {};
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2013-02-27 16:57:36 +08:00
|
|
|
} /* namespace js */
|
2012-08-28 02:41:49 +08:00
|
|
|
|
|
|
|
extern JS_FRIEND_API(JSObject *)
|
2013-10-30 21:55:19 +08:00
|
|
|
js_InitProxyClass(JSContext *cx, JS::HandleObject obj);
|
2012-08-28 02:41:49 +08:00
|
|
|
|
2013-10-30 21:55:19 +08:00
|
|
|
#endif /* jsproxy_h */
|