Bug 617935: Check string lengths using StringBuffer. (r=lw)
[mozilla-central.git] / js / src / jsproxy.h
blob0ef0ede2cf0d80e3a8ed405c00cb517b3f7cc8b1
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 JS_FRIEND_API(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, 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;
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, js::Value *vp);
75 virtual bool keys(JSContext *cx, JSObject *proxy, js::AutoIdVector &props);
76 virtual bool iterate(JSContext *cx, JSObject *proxy, uintN flags, js::Value *vp);
78 /* Spidermonkey extensions. */
79 virtual bool call(JSContext *cx, JSObject *proxy, uintN argc, js::Value *vp);
80 virtual bool construct(JSContext *cx, JSObject *proxy,
81 uintN argc, js::Value *argv, js::Value *rval);
82 virtual bool hasInstance(JSContext *cx, JSObject *proxy, const js::Value *vp, bool *bp);
83 virtual JSType typeOf(JSContext *cx, JSObject *proxy);
84 virtual JSString *obj_toString(JSContext *cx, JSObject *proxy);
85 virtual JSString *fun_toString(JSContext *cx, JSObject *proxy, uintN indent);
86 virtual void finalize(JSContext *cx, JSObject *proxy);
87 virtual void trace(JSTracer *trc, JSObject *proxy);
89 virtual bool isOuterWindow() {
90 return false;
93 inline void *family() {
94 return mFamily;
98 /* Dispatch point for handlers that executes the appropriate C++ or scripted traps. */
99 class JSProxy {
100 public:
101 /* ES5 Harmony fundamental proxy traps. */
102 static bool getPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set,
103 PropertyDescriptor *desc);
104 static bool getPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set, Value *vp);
105 static bool getOwnPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set,
106 PropertyDescriptor *desc);
107 static bool getOwnPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set,
108 Value *vp);
109 static bool defineProperty(JSContext *cx, JSObject *proxy, jsid id, PropertyDescriptor *desc);
110 static bool defineProperty(JSContext *cx, JSObject *proxy, jsid id, const Value &v);
111 static bool getOwnPropertyNames(JSContext *cx, JSObject *proxy, js::AutoIdVector &props);
112 static bool delete_(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
113 static bool enumerate(JSContext *cx, JSObject *proxy, js::AutoIdVector &props);
114 static bool fix(JSContext *cx, JSObject *proxy, Value *vp);
116 /* ES5 Harmony derived proxy traps. */
117 static bool has(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
118 static bool hasOwn(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
119 static bool get(JSContext *cx, JSObject *proxy, JSObject *receiver, jsid id, Value *vp);
120 static bool set(JSContext *cx, JSObject *proxy, JSObject *receiver, jsid id, Value *vp);
121 static bool keys(JSContext *cx, JSObject *proxy, js::AutoIdVector &props);
122 static bool iterate(JSContext *cx, JSObject *proxy, uintN flags, Value *vp);
124 /* Spidermonkey extensions. */
125 static bool call(JSContext *cx, JSObject *proxy, uintN argc, js::Value *vp);
126 static bool construct(JSContext *cx, JSObject *proxy, uintN argc, js::Value *argv, js::Value *rval);
127 static bool hasInstance(JSContext *cx, JSObject *proxy, const js::Value *vp, bool *bp);
128 static JSType typeOf(JSContext *cx, JSObject *proxy);
129 static JSString *obj_toString(JSContext *cx, JSObject *proxy);
130 static JSString *fun_toString(JSContext *cx, JSObject *proxy, uintN indent);
133 /* Shared between object and function proxies. */
134 const uint32 JSSLOT_PROXY_HANDLER = 0;
135 const uint32 JSSLOT_PROXY_PRIVATE = 1;
136 const uint32 JSSLOT_PROXY_EXTRA = 2;
137 /* Function proxies only. */
138 const uint32 JSSLOT_PROXY_CALL = 3;
139 const uint32 JSSLOT_PROXY_CONSTRUCT = 4;
141 extern JS_FRIEND_API(js::Class) ObjectProxyClass;
142 extern JS_FRIEND_API(js::Class) FunctionProxyClass;
143 extern JS_FRIEND_API(js::Class) OuterWindowProxyClass;
144 extern js::Class CallableObjectClass;
148 inline bool
149 JSObject::isObjectProxy() const
151 return getClass() == &js::ObjectProxyClass ||
152 getClass() == &js::OuterWindowProxyClass;
155 inline bool
156 JSObject::isFunctionProxy() const
158 return getClass() == &js::FunctionProxyClass;
161 inline bool
162 JSObject::isProxy() const
164 return isObjectProxy() || isFunctionProxy();
167 inline js::JSProxyHandler *
168 JSObject::getProxyHandler() const
170 JS_ASSERT(isProxy());
171 return (js::JSProxyHandler *) getSlot(js::JSSLOT_PROXY_HANDLER).toPrivate();
174 inline const js::Value &
175 JSObject::getProxyPrivate() const
177 JS_ASSERT(isProxy());
178 return getSlot(js::JSSLOT_PROXY_PRIVATE);
181 inline void
182 JSObject::setProxyPrivate(const js::Value &priv)
184 JS_ASSERT(isProxy());
185 setSlot(js::JSSLOT_PROXY_PRIVATE, priv);
188 inline const js::Value &
189 JSObject::getProxyExtra() const
191 JS_ASSERT(isProxy());
192 return getSlot(js::JSSLOT_PROXY_EXTRA);
195 inline void
196 JSObject::setProxyExtra(const js::Value &extra)
198 JS_ASSERT(isProxy());
199 setSlot(js::JSSLOT_PROXY_EXTRA, extra);
202 namespace js {
204 JS_FRIEND_API(JSObject *)
205 NewProxyObject(JSContext *cx, JSProxyHandler *handler, const js::Value &priv,
206 JSObject *proto, JSObject *parent,
207 JSObject *call = NULL, JSObject *construct = NULL);
209 JS_FRIEND_API(JSBool)
210 FixProxy(JSContext *cx, JSObject *proxy, JSBool *bp);
214 JS_BEGIN_EXTERN_C
216 extern js::Class js_ProxyClass;
218 extern JS_FRIEND_API(JSObject *)
219 js_InitProxyClass(JSContext *cx, JSObject *obj);
221 JS_END_EXTERN_C
223 #endif