Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / jsproxy.h
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 "jscntxt.h"
47 #include "jsobj.h"
48
49 namespace js {
50
51 /* Base class for all C++ proxy handlers. */
52 class JS_FRIEND_API(JSProxyHandler) {
53     void *mFamily;
54   public:
55     explicit JSProxyHandler(void *family);
56     virtual ~JSProxyHandler();
57
58     /* ES5 Harmony fundamental proxy traps. */
59     virtual bool getPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set,
60                                        PropertyDescriptor *desc) = 0;
61     virtual bool getOwnPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set,
62                                           PropertyDescriptor *desc) = 0;
63     virtual bool defineProperty(JSContext *cx, JSObject *proxy, jsid id,
64                                 PropertyDescriptor *desc) = 0;
65     virtual bool getOwnPropertyNames(JSContext *cx, JSObject *proxy, js::AutoIdVector &props) = 0;
66     virtual bool delete_(JSContext *cx, JSObject *proxy, jsid id, bool *bp) = 0;
67     virtual bool enumerate(JSContext *cx, JSObject *proxy, js::AutoIdVector &props) = 0;
68     virtual bool fix(JSContext *cx, JSObject *proxy, Value *vp) = 0;
69
70     /* ES5 Harmony derived proxy traps. */
71     virtual bool has(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
72     virtual bool hasOwn(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
73     virtual bool get(JSContext *cx, JSObject *proxy, JSObject *receiver, jsid id, js::Value *vp);
74     virtual bool set(JSContext *cx, JSObject *proxy, JSObject *receiver, jsid id, bool strict,
75                      js::Value *vp);
76     virtual bool keys(JSContext *cx, JSObject *proxy, js::AutoIdVector &props);
77     virtual bool iterate(JSContext *cx, JSObject *proxy, uintN flags, js::Value *vp);
78
79     /* Spidermonkey extensions. */
80     virtual bool call(JSContext *cx, JSObject *proxy, uintN argc, js::Value *vp);
81     virtual bool construct(JSContext *cx, JSObject *proxy,
82                                           uintN argc, js::Value *argv, js::Value *rval);
83     virtual bool hasInstance(JSContext *cx, JSObject *proxy, const js::Value *vp, bool *bp);
84     virtual JSType typeOf(JSContext *cx, JSObject *proxy);
85     virtual JSString *obj_toString(JSContext *cx, JSObject *proxy);
86     virtual JSString *fun_toString(JSContext *cx, JSObject *proxy, uintN indent);
87     virtual void finalize(JSContext *cx, JSObject *proxy);
88     virtual void trace(JSTracer *trc, JSObject *proxy);
89
90     virtual bool isOuterWindow() {
91         return false;
92     }
93
94     inline void *family() {
95         return mFamily;
96     }
97 };
98
99 /* Dispatch point for handlers that executes the appropriate C++ or scripted traps. */
100 class JSProxy {
101   public:
102     /* ES5 Harmony fundamental proxy traps. */
103     static bool getPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set,
104                                       PropertyDescriptor *desc);
105     static bool getPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set, Value *vp);
106     static bool getOwnPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set,
107                                          PropertyDescriptor *desc);
108     static bool getOwnPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set,
109                                          Value *vp);
110     static bool defineProperty(JSContext *cx, JSObject *proxy, jsid id, PropertyDescriptor *desc);
111     static bool defineProperty(JSContext *cx, JSObject *proxy, jsid id, const Value &v);
112     static bool getOwnPropertyNames(JSContext *cx, JSObject *proxy, js::AutoIdVector &props);
113     static bool delete_(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
114     static bool enumerate(JSContext *cx, JSObject *proxy, js::AutoIdVector &props);
115     static bool fix(JSContext *cx, JSObject *proxy, Value *vp);
116
117     /* ES5 Harmony derived proxy traps. */
118     static bool has(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
119     static bool hasOwn(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
120     static bool get(JSContext *cx, JSObject *proxy, JSObject *receiver, jsid id, Value *vp);
121     static bool set(JSContext *cx, JSObject *proxy, JSObject *receiver, jsid id, bool strict,
122                     Value *vp);
123     static bool keys(JSContext *cx, JSObject *proxy, js::AutoIdVector &props);
124     static bool iterate(JSContext *cx, JSObject *proxy, uintN flags, Value *vp);
125
126     /* Spidermonkey extensions. */
127     static bool call(JSContext *cx, JSObject *proxy, uintN argc, js::Value *vp);
128     static bool construct(JSContext *cx, JSObject *proxy, uintN argc, js::Value *argv, js::Value *rval);
129     static bool hasInstance(JSContext *cx, JSObject *proxy, const js::Value *vp, bool *bp);
130     static JSType typeOf(JSContext *cx, JSObject *proxy);
131     static JSString *obj_toString(JSContext *cx, JSObject *proxy);
132     static JSString *fun_toString(JSContext *cx, JSObject *proxy, uintN indent);
133 };
134
135 /* Shared between object and function proxies. */
136 const uint32 JSSLOT_PROXY_HANDLER = 0;
137 const uint32 JSSLOT_PROXY_PRIVATE = 1;
138 const uint32 JSSLOT_PROXY_EXTRA   = 2;
139 /* Function proxies only. */
140 const uint32 JSSLOT_PROXY_CALL = 3;
141 const uint32 JSSLOT_PROXY_CONSTRUCT = 4;
142
143 extern JS_FRIEND_API(js::Class) ObjectProxyClass;
144 extern JS_FRIEND_API(js::Class) FunctionProxyClass;
145 extern JS_FRIEND_API(js::Class) OuterWindowProxyClass;
146 extern js::Class CallableObjectClass;
147
148 }
149
150 inline bool
151 JSObject::isObjectProxy() const
152 {
153     return getClass() == &js::ObjectProxyClass ||
154            getClass() == &js::OuterWindowProxyClass;
155 }
156
157 inline bool
158 JSObject::isFunctionProxy() const
159 {
160     return getClass() == &js::FunctionProxyClass;
161 }
162
163 inline bool
164 JSObject::isProxy() const
165 {
166     return isObjectProxy() || isFunctionProxy();
167 }
168
169 inline js::JSProxyHandler *
170 JSObject::getProxyHandler() const
171 {
172     JS_ASSERT(isProxy());
173     return (js::JSProxyHandler *) getSlot(js::JSSLOT_PROXY_HANDLER).toPrivate();
174 }
175
176 inline const js::Value &
177 JSObject::getProxyPrivate() const
178 {
179     JS_ASSERT(isProxy());
180     return getSlot(js::JSSLOT_PROXY_PRIVATE);
181 }
182
183 inline void
184 JSObject::setProxyPrivate(const js::Value &priv)
185 {
186     JS_ASSERT(isProxy());
187     setSlot(js::JSSLOT_PROXY_PRIVATE, priv);
188 }
189
190 inline const js::Value &
191 JSObject::getProxyExtra() const
192 {
193     JS_ASSERT(isProxy());
194     return getSlot(js::JSSLOT_PROXY_EXTRA);
195 }
196
197 inline void
198 JSObject::setProxyExtra(const js::Value &extra)
199 {
200     JS_ASSERT(isProxy());
201     setSlot(js::JSSLOT_PROXY_EXTRA, extra);
202 }
203
204 namespace js {
205
206 JS_FRIEND_API(JSObject *)
207 NewProxyObject(JSContext *cx, JSProxyHandler *handler, const js::Value &priv,
208                JSObject *proto, JSObject *parent,
209                JSObject *call = NULL, JSObject *construct = NULL);
210
211 JS_FRIEND_API(JSBool)
212 FixProxy(JSContext *cx, JSObject *proxy, JSBool *bp);
213
214 }
215
216 JS_BEGIN_EXTERN_C
217
218 extern js::Class js_ProxyClass;
219
220 extern JS_FRIEND_API(JSObject *)
221 js_InitProxyClass(JSContext *cx, JSObject *obj);
222
223 JS_END_EXTERN_C
224
225 #endif