Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / jswrapper.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) 2010
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 jswrapper_h___
43 #define jswrapper_h___
44
45 #include "jsapi.h"
46 #include "jsproxy.h"
47
48 JS_BEGIN_EXTERN_C
49
50 /* No-op wrapper handler base class. */
51 class JS_FRIEND_API(JSWrapper) : public js::JSProxyHandler {
52     uintN mFlags;
53   public:
54     uintN flags() const { return mFlags; }
55
56     explicit JSWrapper(uintN flags);
57
58     typedef enum { PermitObjectAccess, PermitPropertyAccess, DenyAccess } Permission;
59
60     virtual ~JSWrapper();
61
62     /* ES5 Harmony fundamental wrapper traps. */
63     virtual bool getPropertyDescriptor(JSContext *cx, JSObject *wrapper, jsid id, bool set,
64                                        js::PropertyDescriptor *desc);
65     virtual bool getOwnPropertyDescriptor(JSContext *cx, JSObject *wrapper, jsid id, bool set,
66                                           js::PropertyDescriptor *desc);
67     virtual bool defineProperty(JSContext *cx, JSObject *wrapper, jsid id,
68                                 js::PropertyDescriptor *desc);
69     virtual bool getOwnPropertyNames(JSContext *cx, JSObject *wrapper, js::AutoIdVector &props);
70     virtual bool delete_(JSContext *cx, JSObject *wrapper, jsid id, bool *bp);
71     virtual bool enumerate(JSContext *cx, JSObject *wrapper, js::AutoIdVector &props);
72     virtual bool fix(JSContext *cx, JSObject *wrapper, js::Value *vp);
73
74     /* ES5 Harmony derived wrapper traps. */
75     virtual bool has(JSContext *cx, JSObject *wrapper, jsid id, bool *bp);
76     virtual bool hasOwn(JSContext *cx, JSObject *wrapper, jsid id, bool *bp);
77     virtual bool get(JSContext *cx, JSObject *wrapper, JSObject *receiver, jsid id, js::Value *vp);
78     virtual bool set(JSContext *cx, JSObject *wrapper, JSObject *receiver, jsid id, bool strict,
79                      js::Value *vp);
80     virtual bool keys(JSContext *cx, JSObject *wrapper, js::AutoIdVector &props);
81     virtual bool iterate(JSContext *cx, JSObject *wrapper, uintN flags, js::Value *vp);
82
83     /* Spidermonkey extensions. */
84     virtual bool call(JSContext *cx, JSObject *wrapper, uintN argc, js::Value *vp);
85     virtual bool construct(JSContext *cx, JSObject *wrapper, uintN argc, js::Value *argv,
86                            js::Value *rval);
87     virtual bool hasInstance(JSContext *cx, JSObject *wrapper, const js::Value *vp, bool *bp);
88     virtual JSType typeOf(JSContext *cx, JSObject *proxy);
89     virtual JSString *obj_toString(JSContext *cx, JSObject *wrapper);
90     virtual JSString *fun_toString(JSContext *cx, JSObject *wrapper, uintN indent);
91
92     virtual void trace(JSTracer *trc, JSObject *wrapper);
93
94     /* Policy enforcement traps. */
95     enum Action { GET, SET, CALL };
96     virtual bool enter(JSContext *cx, JSObject *wrapper, jsid id, Action act, bool *bp);
97     virtual void leave(JSContext *cx, JSObject *wrapper);
98
99     static JSWrapper singleton;
100
101     static JSObject *New(JSContext *cx, JSObject *obj, JSObject *proto, JSObject *parent,
102                          JSWrapper *handler);
103
104     static inline JSObject *wrappedObject(const JSObject *wrapper) {
105         return wrapper->getProxyPrivate().toObjectOrNull();
106     }
107     static inline JSWrapper *wrapperHandler(const JSObject *wrapper) {
108         return static_cast<JSWrapper *>(wrapper->getProxyHandler());
109     }
110
111     enum {
112         CROSS_COMPARTMENT = 1 << 0,
113         LAST_USED_FLAG = CROSS_COMPARTMENT
114     };
115
116     static void *getWrapperFamily();
117 };
118
119 /* Base class for all cross compartment wrapper handlers. */
120 class JS_FRIEND_API(JSCrossCompartmentWrapper) : public JSWrapper {
121   public:
122     JSCrossCompartmentWrapper(uintN flags);
123
124     virtual ~JSCrossCompartmentWrapper();
125
126     /* ES5 Harmony fundamental wrapper traps. */
127     virtual bool getPropertyDescriptor(JSContext *cx, JSObject *wrapper, jsid id, bool set,
128                                        js::PropertyDescriptor *desc);
129     virtual bool getOwnPropertyDescriptor(JSContext *cx, JSObject *wrapper, jsid id, bool set,
130                                           js::PropertyDescriptor *desc);
131     virtual bool defineProperty(JSContext *cx, JSObject *wrapper, jsid id,
132                                 js::PropertyDescriptor *desc);
133     virtual bool getOwnPropertyNames(JSContext *cx, JSObject *wrapper, js::AutoIdVector &props);
134     virtual bool delete_(JSContext *cx, JSObject *wrapper, jsid id, bool *bp);
135     virtual bool enumerate(JSContext *cx, JSObject *wrapper, js::AutoIdVector &props);
136
137     /* ES5 Harmony derived wrapper traps. */
138     virtual bool has(JSContext *cx, JSObject *wrapper, jsid id, bool *bp);
139     virtual bool hasOwn(JSContext *cx, JSObject *wrapper, jsid id, bool *bp);
140     virtual bool get(JSContext *cx, JSObject *wrapper, JSObject *receiver, jsid id, js::Value *vp);
141     virtual bool set(JSContext *cx, JSObject *wrapper, JSObject *receiver, jsid id, bool strict,
142                      js::Value *vp);
143     virtual bool keys(JSContext *cx, JSObject *wrapper, js::AutoIdVector &props);
144     virtual bool iterate(JSContext *cx, JSObject *wrapper, uintN flags, js::Value *vp);
145
146     /* Spidermonkey extensions. */
147     virtual bool call(JSContext *cx, JSObject *wrapper, uintN argc, js::Value *vp);
148     virtual bool construct(JSContext *cx, JSObject *wrapper,
149                            uintN argc, js::Value *argv, js::Value *rval);
150     virtual bool hasInstance(JSContext *cx, JSObject *wrapper, const js::Value *vp, bool *bp);
151     virtual JSString *obj_toString(JSContext *cx, JSObject *wrapper);
152     virtual JSString *fun_toString(JSContext *cx, JSObject *wrapper, uintN indent);
153
154     static JSCrossCompartmentWrapper singleton;
155 };
156
157 namespace js {
158
159 class AutoCompartment
160 {
161   public:
162     JSContext * const context;
163     JSCompartment * const origin;
164     JSObject * const target;
165     JSCompartment * const destination;
166   private:
167     LazilyConstructed<DummyFrameGuard> frame;
168     JSFrameRegs regs;
169     AutoStringRooter input;
170     bool entered;
171
172   public:
173     AutoCompartment(JSContext *cx, JSObject *target);
174     ~AutoCompartment();
175
176     bool enter();
177     void leave();
178
179   private:
180     // Prohibit copying.
181     AutoCompartment(const AutoCompartment &);
182     AutoCompartment & operator=(const AutoCompartment &);
183 };
184
185 extern JSObject *
186 TransparentObjectWrapper(JSContext *cx, JSObject *obj, JSObject *wrappedProto, JSObject *parent,
187                          uintN flags);
188
189 }
190
191 JS_END_EXTERN_C
192
193 #endif