1 : /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 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 SpiderMonkey.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * the Mozilla Foundation.
21 : * Portions created by the Initial Developer are Copyright (C) 2010
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef jsfuninlines_h___
41 : #define jsfuninlines_h___
42 :
43 : #include "jsfun.h"
44 : #include "jsscript.h"
45 :
46 : #include "vm/GlobalObject.h"
47 :
48 : #include "vm/ScopeObject-inl.h"
49 :
50 : inline bool
51 484970 : JSFunction::inStrictMode() const
52 : {
53 484970 : return script()->strictModeCode;
54 : }
55 :
56 : inline JSObject *
57 46738238 : JSFunction::environment() const
58 : {
59 46738238 : JS_ASSERT(isInterpreted());
60 46738238 : return u.i.env_;
61 : }
62 :
63 : inline void
64 26282 : JSFunction::setEnvironment(JSObject *obj)
65 : {
66 26282 : JS_ASSERT(isInterpreted());
67 26282 : *(js::HeapPtrObject *)&u.i.env_ = obj;
68 26282 : }
69 :
70 : inline void
71 1162560 : JSFunction::initEnvironment(JSObject *obj)
72 : {
73 1162560 : JS_ASSERT(isInterpreted());
74 1162560 : ((js::HeapPtrObject *)&u.i.env_)->init(obj);
75 1162560 : }
76 :
77 : inline void
78 623184 : JSFunction::initializeExtended()
79 : {
80 623184 : JS_ASSERT(isExtended());
81 :
82 623184 : JS_ASSERT(js::ArrayLength(toExtended()->extendedSlots) == 2);
83 623184 : toExtended()->extendedSlots[0].init(js::UndefinedValue());
84 623184 : toExtended()->extendedSlots[1].init(js::UndefinedValue());
85 623184 : }
86 :
87 : inline void
88 623184 : JSFunction::setExtendedSlot(size_t which, const js::Value &val)
89 : {
90 623184 : JS_ASSERT(which < js::ArrayLength(toExtended()->extendedSlots));
91 623184 : toExtended()->extendedSlots[which] = val;
92 623184 : }
93 :
94 : inline const js::Value &
95 5253 : JSFunction::getExtendedSlot(size_t which) const
96 : {
97 5253 : JS_ASSERT(which < js::ArrayLength(toExtended()->extendedSlots));
98 5253 : return toExtended()->extendedSlots[which];
99 : }
100 :
101 : namespace js {
102 :
103 : static JS_ALWAYS_INLINE bool
104 736700 : IsFunctionObject(const js::Value &v)
105 : {
106 736700 : return v.isObject() && v.toObject().isFunction();
107 : }
108 :
109 : static JS_ALWAYS_INLINE bool
110 39149406 : IsFunctionObject(const js::Value &v, JSFunction **fun)
111 : {
112 39149406 : if (v.isObject() && v.toObject().isFunction()) {
113 39107483 : *fun = v.toObject().toFunction();
114 39107483 : return true;
115 : }
116 41923 : return false;
117 : }
118 :
119 : static JS_ALWAYS_INLINE bool
120 5610111 : IsNativeFunction(const js::Value &v)
121 : {
122 : JSFunction *fun;
123 5610111 : return IsFunctionObject(v, &fun) && fun->isNative();
124 : }
125 :
126 : static JS_ALWAYS_INLINE bool
127 : IsNativeFunction(const js::Value &v, JSFunction **fun)
128 : {
129 : return IsFunctionObject(v, fun) && (*fun)->isNative();
130 : }
131 :
132 : static JS_ALWAYS_INLINE bool
133 328683 : IsNativeFunction(const js::Value &v, JSNative native)
134 : {
135 : JSFunction *fun;
136 328683 : return IsFunctionObject(v, &fun) && fun->maybeNative() == native;
137 : }
138 :
139 : /*
140 : * When we have an object of a builtin class, we don't quite know what its
141 : * valueOf/toString methods are, since these methods may have been overwritten
142 : * or shadowed. However, we can still do better than the general case by
143 : * hard-coding the necessary properties for us to find the native we expect.
144 : *
145 : * TODO: a per-thread shape-based cache would be faster and simpler.
146 : */
147 : static JS_ALWAYS_INLINE bool
148 184635 : ClassMethodIsNative(JSContext *cx, JSObject *obj, Class *clasp, jsid methodid, JSNative native)
149 : {
150 184635 : JS_ASSERT(obj->getClass() == clasp);
151 :
152 : Value v;
153 184635 : if (!HasDataProperty(cx, obj, methodid, &v)) {
154 184635 : JSObject *proto = obj->getProto();
155 184635 : if (!proto || proto->getClass() != clasp || !HasDataProperty(cx, proto, methodid, &v))
156 18 : return false;
157 : }
158 :
159 184617 : return js::IsNativeFunction(v, native);
160 : }
161 :
162 : extern JS_ALWAYS_INLINE bool
163 : SameTraceType(const Value &lhs, const Value &rhs)
164 : {
165 : return SameType(lhs, rhs) &&
166 : (lhs.isPrimitive() ||
167 : lhs.toObject().isFunction() == rhs.toObject().isFunction());
168 : }
169 :
170 : /* Valueified JS_IsConstructing. */
171 : static JS_ALWAYS_INLINE bool
172 378531 : IsConstructing(const Value *vp)
173 : {
174 : #ifdef DEBUG
175 378531 : JSObject *callee = &JS_CALLEE(cx, vp).toObject();
176 378531 : if (callee->isFunction()) {
177 378531 : JSFunction *fun = callee->toFunction();
178 378531 : JS_ASSERT((fun->flags & JSFUN_CONSTRUCTOR) != 0);
179 : } else {
180 0 : JS_ASSERT(callee->getClass()->construct != NULL);
181 : }
182 : #endif
183 378531 : return vp[1].isMagic();
184 : }
185 :
186 : inline bool
187 360508 : IsConstructing(CallReceiver call)
188 : {
189 360508 : return IsConstructing(call.base());
190 : }
191 :
192 : inline const char *
193 2276 : GetFunctionNameBytes(JSContext *cx, JSFunction *fun, JSAutoByteString *bytes)
194 : {
195 2276 : if (fun->atom)
196 2276 : return bytes->encode(cx, fun->atom);
197 0 : return js_anonymous_str;
198 : }
199 :
200 : extern JSFunctionSpec function_methods[];
201 :
202 : extern JSBool
203 : Function(JSContext *cx, unsigned argc, Value *vp);
204 :
205 : extern bool
206 : IsBuiltinFunctionConstructor(JSFunction *fun);
207 :
208 : /*
209 : * Preconditions: funobj->isInterpreted() && !funobj->isFunctionPrototype() &&
210 : * !funobj->isBoundFunction(). This is sufficient to establish that funobj has
211 : * a non-configurable non-method .prototype data property, thought it might not
212 : * have been resolved yet, and its value could be anything.
213 : *
214 : * Return the shape of the .prototype property of funobj, resolving it if
215 : * needed. On error, return NULL.
216 : *
217 : * This is not safe to call on trace because it defines properties, which can
218 : * trigger lookups that could reenter.
219 : */
220 : const Shape *
221 : LookupInterpretedFunctionPrototype(JSContext *cx, JSObject *funobj);
222 :
223 : static inline JSObject *
224 7999268 : SkipScopeParent(JSObject *parent)
225 : {
226 7999268 : if (!parent)
227 2923 : return NULL;
228 17388249 : while (parent->isScope())
229 1395559 : parent = &parent->asScope().enclosingScope();
230 7996345 : return parent;
231 : }
232 :
233 : inline JSFunction *
234 971430 : CloneFunctionObject(JSContext *cx, JSFunction *fun, JSObject *parent,
235 : gc::AllocKind kind = JSFunction::FinalizeKind)
236 : {
237 971430 : JS_ASSERT(parent);
238 971430 : JSObject *proto = parent->global().getOrCreateFunctionPrototype(cx);
239 971430 : if (!proto)
240 0 : return NULL;
241 :
242 971430 : return js_CloneFunctionObject(cx, fun, parent, proto, kind);
243 : }
244 :
245 : inline JSFunction *
246 992506 : CloneFunctionObjectIfNotSingleton(JSContext *cx, JSFunction *fun, JSObject *parent)
247 : {
248 : /*
249 : * For attempts to clone functions at a function definition opcode,
250 : * don't perform the clone if the function has singleton type. This
251 : * was called pessimistically, and we need to preserve the type's
252 : * property that if it is singleton there is only a single object
253 : * with its type in existence.
254 : */
255 992506 : if (fun->hasSingletonType()) {
256 23445 : if (!fun->setParent(cx, SkipScopeParent(parent)))
257 0 : return NULL;
258 23445 : fun->setEnvironment(parent);
259 23445 : return fun;
260 : }
261 :
262 969061 : return CloneFunctionObject(cx, fun, parent);
263 : }
264 :
265 : inline JSFunction *
266 : CloneFunctionObject(JSContext *cx, JSFunction *fun)
267 : {
268 : /*
269 : * Variant which makes an exact clone of fun, preserving parent and proto.
270 : * Calling the above version CloneFunctionObject(cx, fun, fun->getParent())
271 : * is not equivalent: API clients, including XPConnect, can reparent
272 : * objects so that fun->global() != fun->getProto()->global().
273 : * See ReparentWrapperIfFound.
274 : */
275 : JS_ASSERT(fun->getParent() && fun->getProto());
276 :
277 : if (fun->hasSingletonType())
278 : return fun;
279 :
280 : return js_CloneFunctionObject(cx, fun, fun->environment(), fun->getProto(),
281 : JSFunction::ExtendedFinalizeKind);
282 : }
283 :
284 : } /* namespace js */
285 :
286 : inline void
287 166822 : JSFunction::setScript(JSScript *script_)
288 : {
289 166822 : JS_ASSERT(isInterpreted());
290 166822 : script() = script_;
291 166822 : }
292 :
293 : inline void
294 995171 : JSFunction::initScript(JSScript *script_)
295 : {
296 995171 : JS_ASSERT(isInterpreted());
297 995171 : script().init(script_);
298 995171 : }
299 :
300 : #endif /* jsfuninlines_h___ */
|