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
17 * The Original Code is Mozilla SpiderMonkey JavaScript 1.9 code, released
20 * The Initial Developer of the Original Code is
22 * Portions created by the Initial Developer are Copyright (C) 2009
23 * the Initial Developer. All Rights Reserved.
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 ***** */
51 /* Base class for all C++ proxy handlers. */
52 class JS_FRIEND_API(JSProxyHandler
) {
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
, bool strict
,
76 virtual bool keys(JSContext
*cx
, JSObject
*proxy
, js::AutoIdVector
&props
);
77 virtual bool iterate(JSContext
*cx
, JSObject
*proxy
, uintN flags
, js::Value
*vp
);
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
);
90 virtual bool isOuterWindow() {
94 inline void *family() {
99 /* Dispatch point for handlers that executes the appropriate C++ or scripted traps. */
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
,
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
);
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
,
123 static bool keys(JSContext
*cx
, JSObject
*proxy
, js::AutoIdVector
&props
);
124 static bool iterate(JSContext
*cx
, JSObject
*proxy
, uintN flags
, Value
*vp
);
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
);
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;
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
;
151 JSObject::isObjectProxy() const
153 return getClass() == &js::ObjectProxyClass
||
154 getClass() == &js::OuterWindowProxyClass
;
158 JSObject::isFunctionProxy() const
160 return getClass() == &js::FunctionProxyClass
;
164 JSObject::isProxy() const
166 return isObjectProxy() || isFunctionProxy();
169 inline js::JSProxyHandler
*
170 JSObject::getProxyHandler() const
172 JS_ASSERT(isProxy());
173 return (js::JSProxyHandler
*) getSlot(js::JSSLOT_PROXY_HANDLER
).toPrivate();
176 inline const js::Value
&
177 JSObject::getProxyPrivate() const
179 JS_ASSERT(isProxy());
180 return getSlot(js::JSSLOT_PROXY_PRIVATE
);
184 JSObject::setProxyPrivate(const js::Value
&priv
)
186 JS_ASSERT(isProxy());
187 setSlot(js::JSSLOT_PROXY_PRIVATE
, priv
);
190 inline const js::Value
&
191 JSObject::getProxyExtra() const
193 JS_ASSERT(isProxy());
194 return getSlot(js::JSSLOT_PROXY_EXTRA
);
198 JSObject::setProxyExtra(const js::Value
&extra
)
200 JS_ASSERT(isProxy());
201 setSlot(js::JSSLOT_PROXY_EXTRA
, extra
);
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
);
211 JS_FRIEND_API(JSBool
)
212 FixProxy(JSContext
*cx
, JSObject
*proxy
, JSBool
*bp
);
218 extern js::Class js_ProxyClass
;
220 extern JS_FRIEND_API(JSObject
*)
221 js_InitProxyClass(JSContext
*cx
, JSObject
*obj
);