1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=4 sw=4 et tw=99:
3 : *
4 : * ***** BEGIN LICENSE BLOCK *****
5 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 : *
7 : * The contents of this file are subject to the Mozilla Public License Version
8 : * 1.1 (the "License"); you may not use this file except in compliance with
9 : * the License. You may obtain a copy of the License at
10 : * http://www.mozilla.org/MPL/
11 : *
12 : * Software distributed under the License is distributed on an "AS IS" basis,
13 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 : * for the specific language governing rights and limitations under the
15 : * License.
16 : *
17 : * The Original Code is Mozilla SpiderMonkey JavaScript 1.9 code, released
18 : * May 28, 2008.
19 : *
20 : * The Initial Developer of the Original Code is
21 : * Mozilla Foundation
22 : * Portions created by the Initial Developer are Copyright (C) 2009
23 : * the Initial Developer. All Rights Reserved.
24 : *
25 : * Contributor(s):
26 : * Andreas Gal <gal@mozilla.com>
27 : *
28 : * Alternatively, the contents of this file may be used under the terms of
29 : * either of the GNU General Public License Version 2 or later (the "GPL"),
30 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 : * in which case the provisions of the GPL or the LGPL are applicable instead
32 : * of those above. If you wish to allow use of your version of this file only
33 : * under the terms of either the GPL or the LGPL, and not to allow others to
34 : * use your version of this file under the terms of the MPL, indicate your
35 : * decision by deleting the provisions above and replace them with the notice
36 : * and other provisions required by the GPL or the LGPL. If you do not delete
37 : * the provisions above, a recipient may use your version of this file under
38 : * the terms of any one of the MPL, the GPL or the LGPL.
39 : *
40 : * ***** END LICENSE BLOCK ***** */
41 :
42 : #ifndef jsproxy_h___
43 : #define jsproxy_h___
44 :
45 : #include "jsapi.h"
46 : #include "jsfriendapi.h"
47 :
48 : namespace js {
49 :
50 : /* Base class for all C++ proxy handlers. */
51 : class JS_FRIEND_API(ProxyHandler) {
52 : void *mFamily;
53 : public:
54 : explicit ProxyHandler(void *family);
55 : virtual ~ProxyHandler();
56 :
57 : /* ES5 Harmony fundamental proxy traps. */
58 : virtual bool getPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set,
59 : PropertyDescriptor *desc) = 0;
60 : virtual bool getOwnPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set,
61 : PropertyDescriptor *desc) = 0;
62 : virtual bool defineProperty(JSContext *cx, JSObject *proxy, jsid id,
63 : PropertyDescriptor *desc) = 0;
64 : virtual bool getOwnPropertyNames(JSContext *cx, JSObject *proxy, AutoIdVector &props) = 0;
65 : virtual bool delete_(JSContext *cx, JSObject *proxy, jsid id, bool *bp) = 0;
66 : virtual bool enumerate(JSContext *cx, JSObject *proxy, AutoIdVector &props) = 0;
67 : virtual bool fix(JSContext *cx, JSObject *proxy, Value *vp) = 0;
68 :
69 : /* ES5 Harmony derived proxy traps. */
70 : virtual bool has(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
71 : virtual bool hasOwn(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
72 : virtual bool get(JSContext *cx, JSObject *proxy, JSObject *receiver, jsid id, Value *vp);
73 : virtual bool set(JSContext *cx, JSObject *proxy, JSObject *receiver, jsid id, bool strict,
74 : Value *vp);
75 : virtual bool keys(JSContext *cx, JSObject *proxy, AutoIdVector &props);
76 : virtual bool iterate(JSContext *cx, JSObject *proxy, unsigned flags, Value *vp);
77 :
78 : /* Spidermonkey extensions. */
79 : virtual bool call(JSContext *cx, JSObject *proxy, unsigned argc, Value *vp);
80 : virtual bool construct(JSContext *cx, JSObject *proxy, unsigned argc, Value *argv, Value *rval);
81 : virtual bool nativeCall(JSContext *cx, JSObject *proxy, Class *clasp, Native native, CallArgs args);
82 : virtual bool hasInstance(JSContext *cx, JSObject *proxy, const Value *vp, bool *bp);
83 : virtual JSType typeOf(JSContext *cx, JSObject *proxy);
84 : virtual bool objectClassIs(JSObject *obj, ESClassValue classValue, JSContext *cx);
85 : virtual JSString *obj_toString(JSContext *cx, JSObject *proxy);
86 : virtual JSString *fun_toString(JSContext *cx, JSObject *proxy, unsigned indent);
87 : virtual bool regexp_toShared(JSContext *cx, JSObject *proxy, RegExpGuard *g);
88 : virtual bool defaultValue(JSContext *cx, JSObject *obj, JSType hint, Value *vp);
89 : virtual bool iteratorNext(JSContext *cx, JSObject *proxy, Value *vp);
90 : virtual void finalize(JSContext *cx, JSObject *proxy);
91 : virtual void trace(JSTracer *trc, JSObject *proxy);
92 : virtual bool getElementIfPresent(JSContext *cx, JSObject *obj, JSObject *receiver,
93 : uint32_t index, Value *vp, bool *present);
94 :
95 17839 : virtual bool isOuterWindow() {
96 17839 : return false;
97 : }
98 :
99 109982 : inline void *family() {
100 109982 : return mFamily;
101 : }
102 : };
103 :
104 : /* Dispatch point for handlers that executes the appropriate C++ or scripted traps. */
105 : class Proxy {
106 : public:
107 : /* ES5 Harmony fundamental proxy traps. */
108 : static bool getPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set,
109 : PropertyDescriptor *desc);
110 : static bool getPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set, Value *vp);
111 : static bool getOwnPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set,
112 : PropertyDescriptor *desc);
113 : static bool getOwnPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set,
114 : Value *vp);
115 : static bool defineProperty(JSContext *cx, JSObject *proxy, jsid id, PropertyDescriptor *desc);
116 : static bool defineProperty(JSContext *cx, JSObject *proxy, jsid id, const Value &v);
117 : static bool getOwnPropertyNames(JSContext *cx, JSObject *proxy, AutoIdVector &props);
118 : static bool delete_(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
119 : static bool enumerate(JSContext *cx, JSObject *proxy, AutoIdVector &props);
120 : static bool fix(JSContext *cx, JSObject *proxy, Value *vp);
121 :
122 : /* ES5 Harmony derived proxy traps. */
123 : static bool has(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
124 : static bool hasOwn(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
125 : static bool get(JSContext *cx, JSObject *proxy, JSObject *receiver, jsid id, Value *vp);
126 : static bool getElementIfPresent(JSContext *cx, JSObject *proxy, JSObject *receiver,
127 : uint32_t index, Value *vp, bool *present);
128 : static bool set(JSContext *cx, JSObject *proxy, JSObject *receiver, jsid id, bool strict,
129 : Value *vp);
130 : static bool keys(JSContext *cx, JSObject *proxy, AutoIdVector &props);
131 : static bool iterate(JSContext *cx, JSObject *proxy, unsigned flags, Value *vp);
132 :
133 : /* Spidermonkey extensions. */
134 : static bool call(JSContext *cx, JSObject *proxy, unsigned argc, Value *vp);
135 : static bool construct(JSContext *cx, JSObject *proxy, unsigned argc, Value *argv, Value *rval);
136 : static bool nativeCall(JSContext *cx, JSObject *proxy, Class *clasp, Native native, CallArgs args);
137 : static bool hasInstance(JSContext *cx, JSObject *proxy, const Value *vp, bool *bp);
138 : static JSType typeOf(JSContext *cx, JSObject *proxy);
139 : static bool objectClassIs(JSObject *obj, ESClassValue classValue, JSContext *cx);
140 : static JSString *obj_toString(JSContext *cx, JSObject *proxy);
141 : static JSString *fun_toString(JSContext *cx, JSObject *proxy, unsigned indent);
142 : static bool regexp_toShared(JSContext *cx, JSObject *proxy, RegExpGuard *g);
143 : static bool defaultValue(JSContext *cx, JSObject *obj, JSType hint, Value *vp);
144 : static bool iteratorNext(JSContext *cx, JSObject *proxy, Value *vp);
145 : };
146 :
147 50266144 : inline bool IsObjectProxyClass(const Class *clasp)
148 : {
149 50266144 : return clasp == &js::ObjectProxyClass || clasp == &js::OuterWindowProxyClass;
150 : }
151 :
152 49806596 : inline bool IsFunctionProxyClass(const Class *clasp)
153 : {
154 49806596 : return clasp == &js::FunctionProxyClass;
155 : }
156 :
157 : inline bool IsObjectProxy(const JSObject *obj)
158 : {
159 : return IsObjectProxyClass(GetObjectClass(obj));
160 : }
161 :
162 46369 : inline bool IsFunctionProxy(const JSObject *obj)
163 : {
164 46369 : return IsFunctionProxyClass(GetObjectClass(obj));
165 : }
166 :
167 50266144 : inline bool IsProxy(const JSObject *obj)
168 : {
169 50266144 : Class *clasp = GetObjectClass(obj);
170 50266144 : return IsObjectProxyClass(clasp) || IsFunctionProxyClass(clasp);
171 : }
172 :
173 : /* Shared between object and function proxies. */
174 : const uint32_t JSSLOT_PROXY_HANDLER = 0;
175 : const uint32_t JSSLOT_PROXY_PRIVATE = 1;
176 : const uint32_t JSSLOT_PROXY_EXTRA = 2;
177 : /* Function proxies only. */
178 : const uint32_t JSSLOT_PROXY_CALL = 4;
179 : const uint32_t JSSLOT_PROXY_CONSTRUCT = 5;
180 :
181 : inline ProxyHandler *
182 338300 : GetProxyHandler(const JSObject *obj)
183 : {
184 338300 : JS_ASSERT(IsProxy(obj));
185 338300 : return (ProxyHandler *) GetReservedSlot(obj, JSSLOT_PROXY_HANDLER).toPrivate();
186 : }
187 :
188 : inline const Value &
189 208260 : GetProxyPrivate(const JSObject *obj)
190 : {
191 208260 : JS_ASSERT(IsProxy(obj));
192 208260 : return GetReservedSlot(obj, JSSLOT_PROXY_PRIVATE);
193 : }
194 :
195 : inline const Value &
196 : GetProxyExtra(const JSObject *obj, size_t n)
197 : {
198 : JS_ASSERT(IsProxy(obj));
199 : return GetReservedSlot(obj, JSSLOT_PROXY_EXTRA + n);
200 : }
201 :
202 : inline void
203 3 : SetProxyExtra(JSObject *obj, size_t n, const Value &extra)
204 : {
205 3 : JS_ASSERT(IsProxy(obj));
206 3 : JS_ASSERT(n <= 1);
207 3 : SetReservedSlot(obj, JSSLOT_PROXY_EXTRA + n, extra);
208 3 : }
209 :
210 : JS_FRIEND_API(JSObject *)
211 : NewProxyObject(JSContext *cx, ProxyHandler *handler, const Value &priv,
212 : JSObject *proto, JSObject *parent,
213 : JSObject *call = NULL, JSObject *construct = NULL);
214 :
215 : } /* namespace js */
216 :
217 : JS_BEGIN_EXTERN_C
218 :
219 : extern JS_FRIEND_API(JSObject *)
220 : js_InitProxyClass(JSContext *cx, JSObject *obj);
221 :
222 : JS_END_EXTERN_C
223 :
224 : #endif
|