Bug 582900: JM: Missing rsh type set. (r=dvander)
[mozilla-central.git] / js / src / jsproxy.h
blobabbad5e0345e55ab255e359f12c70799df2e66e6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=4 sw=4 et tw=99:
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
17 * The Original Code is Mozilla SpiderMonkey JavaScript 1.9 code, released
18 * May 28, 2008.
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.
25 * Contributor(s):
26 * Andreas Gal <gal@mozilla.com>
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.
40 * ***** END LICENSE BLOCK ***** */
42 #ifndef jsproxy_h___
43 #define jsproxy_h___
45 #include "jsapi.h"
46 #include "jscntxt.h"
47 #include "jsobj.h"
49 namespace js {
51 /* Base class for all C++ proxy handlers. */
52 class JSProxyHandler {
53 void *mFamily;
54 public:
55 explicit JSProxyHandler(void *family);
56 virtual ~JSProxyHandler();
58 /* ES5 Harmony fundamental proxy traps. */
59 virtual bool getPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id,
60 PropertyDescriptor *desc) = 0;
61 virtual bool getOwnPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id,
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;
70 /* ES5 Harmony derived proxy traps. */
71 virtual JS_FRIEND_API(bool) has(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
72 virtual JS_FRIEND_API(bool) hasOwn(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
73 virtual JS_FRIEND_API(bool) get(JSContext *cx, JSObject *proxy, JSObject *receiver, jsid id, js::Value *vp);
74 virtual JS_FRIEND_API(bool) set(JSContext *cx, JSObject *proxy, JSObject *receiver, jsid id, js::Value *vp);
75 virtual JS_FRIEND_API(bool) enumerateOwn(JSContext *cx, JSObject *proxy, js::AutoIdVector &props);
76 virtual JS_FRIEND_API(bool) iterate(JSContext *cx, JSObject *proxy, uintN flags, js::Value *vp);
78 /* Spidermonkey extensions. */
79 virtual JS_FRIEND_API(bool) call(JSContext *cx, JSObject *proxy, uintN argc, js::Value *vp);
80 virtual JS_FRIEND_API(bool) construct(JSContext *cx, JSObject *proxy,
81 uintN argc, js::Value *argv, js::Value *rval);
82 virtual JS_FRIEND_API(JSString *) obj_toString(JSContext *cx, JSObject *proxy);
83 virtual JS_FRIEND_API(JSString *) fun_toString(JSContext *cx, JSObject *proxy, uintN indent);
84 virtual JS_FRIEND_API(void) finalize(JSContext *cx, JSObject *proxy);
85 virtual JS_FRIEND_API(void) trace(JSTracer *trc, JSObject *proxy);
87 inline void *family() {
88 return mFamily;
92 /* Dispatch point for handlers that executes the appropriate C++ or scripted traps. */
93 class JSProxy {
94 public:
95 /* ES5 Harmony fundamental proxy traps. */
96 static bool getPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id,
97 PropertyDescriptor *desc);
98 static bool getPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, Value *vp);
99 static bool getOwnPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id,
100 PropertyDescriptor *desc);
101 static bool getOwnPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, Value *vp);
102 static bool defineProperty(JSContext *cx, JSObject *proxy, jsid id, PropertyDescriptor *desc);
103 static bool defineProperty(JSContext *cx, JSObject *proxy, jsid id, const Value &v);
104 static bool getOwnPropertyNames(JSContext *cx, JSObject *proxy, js::AutoIdVector &props);
105 static bool delete_(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
106 static bool enumerate(JSContext *cx, JSObject *proxy, js::AutoIdVector &props);
107 static bool fix(JSContext *cx, JSObject *proxy, Value *vp);
109 /* ES5 Harmony derived proxy traps. */
110 static bool has(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
111 static bool hasOwn(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
112 static bool get(JSContext *cx, JSObject *proxy, JSObject *receiver, jsid id, Value *vp);
113 static bool set(JSContext *cx, JSObject *proxy, JSObject *receiver, jsid id, Value *vp);
114 static bool enumerateOwn(JSContext *cx, JSObject *proxy, js::AutoIdVector &props);
115 static bool iterate(JSContext *cx, JSObject *proxy, uintN flags, Value *vp);
117 /* Spidermonkey extensions. */
118 static bool call(JSContext *cx, JSObject *proxy, uintN argc, js::Value *vp);
119 static bool construct(JSContext *cx, JSObject *proxy, uintN argc, js::Value *argv, js::Value *rval);
120 static JSString *obj_toString(JSContext *cx, JSObject *proxy);
121 static JSString *fun_toString(JSContext *cx, JSObject *proxy, uintN indent);
124 /* Shared between object and function proxies. */
125 const uint32 JSSLOT_PROXY_HANDLER = JSSLOT_PRIVATE + 0;
126 const uint32 JSSLOT_PROXY_PRIVATE = JSSLOT_PRIVATE + 1;
127 /* Function proxies only. */
128 const uint32 JSSLOT_PROXY_CALL = JSSLOT_PRIVATE + 2;
129 const uint32 JSSLOT_PROXY_CONSTRUCT = JSSLOT_PRIVATE + 3;
131 extern JS_FRIEND_API(js::Class) ObjectProxyClass;
132 extern JS_FRIEND_API(js::Class) FunctionProxyClass;
133 extern js::Class CallableObjectClass;
137 inline bool
138 JSObject::isObjectProxy() const
140 return getClass() == &js::ObjectProxyClass;
143 inline bool
144 JSObject::isFunctionProxy() const
146 return getClass() == &js::FunctionProxyClass;
149 inline bool
150 JSObject::isProxy() const
152 return isObjectProxy() || isFunctionProxy();
155 inline js::JSProxyHandler *
156 JSObject::getProxyHandler() const
158 JS_ASSERT(isProxy());
159 return (js::JSProxyHandler *) getSlot(js::JSSLOT_PROXY_HANDLER).toPrivate();
162 inline const js::Value &
163 JSObject::getProxyPrivate() const
165 JS_ASSERT(isProxy());
166 return getSlot(js::JSSLOT_PROXY_PRIVATE);
169 inline void
170 JSObject::setProxyPrivate(const js::Value &priv)
172 JS_ASSERT(isProxy());
173 setSlot(js::JSSLOT_PROXY_PRIVATE, priv);
176 namespace js {
178 JS_FRIEND_API(JSObject *)
179 NewProxyObject(JSContext *cx, JSProxyHandler *handler, const js::Value &priv,
180 JSObject *proto, JSObject *parent,
181 JSObject *call = NULL, JSObject *construct = NULL);
183 JS_FRIEND_API(JSBool)
184 FixProxy(JSContext *cx, JSObject *proxy, JSBool *bp);
188 JS_BEGIN_EXTERN_C
190 extern js::Class js_ProxyClass;
192 extern JS_FRIEND_API(JSObject *)
193 js_InitProxyClass(JSContext *cx, JSObject *obj);
195 JS_END_EXTERN_C
197 #endif