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 745207 : JSFunction::inStrictMode() const
52 : {
53 745207 : return script()->strictModeCode;
54 : }
55 :
56 : inline JSObject *
57 62210205 : JSFunction::environment() const
58 : {
59 62210205 : JS_ASSERT(isInterpreted());
60 62210205 : return u.i.env_;
61 : }
62 :
63 : inline void
64 884936 : JSFunction::setEnvironment(JSObject *obj)
65 : {
66 884936 : JS_ASSERT(isInterpreted());
67 884936 : *(js::HeapPtrObject *)&u.i.env_ = obj;
68 884936 : }
69 :
70 : inline void
71 3617600 : JSFunction::initEnvironment(JSObject *obj)
72 : {
73 3617600 : JS_ASSERT(isInterpreted());
74 3617600 : ((js::HeapPtrObject *)&u.i.env_)->init(obj);
75 3617600 : }
76 :
77 : inline void
78 3279310 : JSFunction::initializeExtended()
79 : {
80 3279310 : JS_ASSERT(isExtended());
81 :
82 3279310 : JS_ASSERT(js::ArrayLength(toExtended()->extendedSlots) == 2);
83 3279310 : toExtended()->extendedSlots[0].init(js::UndefinedValue());
84 3279310 : toExtended()->extendedSlots[1].init(js::UndefinedValue());
85 3279310 : }
86 :
87 : inline void
88 98477 : JSFunction::setJoinable()
89 : {
90 98477 : JS_ASSERT(isInterpreted());
91 98477 : flags |= JSFUN_JOINABLE;
92 98477 : }
93 :
94 : inline bool
95 27892 : JSFunction::isClonedMethod() const
96 : {
97 27892 : return joinable() && isExtended() && getExtendedSlot(METHOD_OBJECT_SLOT).isObject();
98 : }
99 :
100 : inline JSAtom *
101 19564 : JSFunction::methodAtom() const
102 : {
103 58257 : return (joinable() && isExtended() && getExtendedSlot(METHOD_PROPERTY_SLOT).isString())
104 19248 : ? (JSAtom *) getExtendedSlot(METHOD_PROPERTY_SLOT).toString()
105 77505 : : NULL;
106 : }
107 :
108 : inline void
109 7096 : JSFunction::setMethodAtom(JSAtom *atom)
110 : {
111 7096 : JS_ASSERT(joinable());
112 7096 : setExtendedSlot(METHOD_PROPERTY_SLOT, js::StringValue(atom));
113 7096 : }
114 :
115 : inline JSObject *
116 5 : JSFunction::methodObj() const
117 : {
118 5 : JS_ASSERT(joinable());
119 5 : return isClonedMethod() ? &getExtendedSlot(METHOD_OBJECT_SLOT).toObject() : NULL;
120 : }
121 :
122 : inline void
123 2285 : JSFunction::setMethodObj(JSObject& obj)
124 : {
125 2285 : JS_ASSERT(joinable());
126 2285 : setExtendedSlot(METHOD_OBJECT_SLOT, js::ObjectValue(obj));
127 2285 : }
128 :
129 : inline void
130 4964643 : JSFunction::setExtendedSlot(size_t which, const js::Value &val)
131 : {
132 4964643 : JS_ASSERT(which < js::ArrayLength(toExtended()->extendedSlots));
133 4964643 : toExtended()->extendedSlots[which] = val;
134 4964643 : }
135 :
136 : inline const js::Value &
137 24584410 : JSFunction::getExtendedSlot(size_t which) const
138 : {
139 24584410 : JS_ASSERT(which < js::ArrayLength(toExtended()->extendedSlots));
140 24584410 : return toExtended()->extendedSlots[which];
141 : }
142 :
143 : inline bool
144 6939240 : JSFunction::hasFlatClosureUpvars() const
145 : {
146 6939240 : JS_ASSERT(isFlatClosure());
147 6939240 : return isExtended() && !getExtendedSlot(FLAT_CLOSURE_UPVARS_SLOT).isUndefined();
148 : }
149 :
150 : inline js::HeapValue *
151 3521522 : JSFunction::getFlatClosureUpvars() const
152 : {
153 3521522 : JS_ASSERT(hasFlatClosureUpvars());
154 3521522 : return (js::HeapValue *) getExtendedSlot(FLAT_CLOSURE_UPVARS_SLOT).toPrivate();
155 : }
156 :
157 : inline void
158 706008 : JSFunction::finalizeUpvars()
159 : {
160 : /*
161 : * Cloned function objects may be flat closures with upvars to free.
162 : *
163 : * We must not access JSScript here that is stored in JSFunction. The
164 : * script can be finalized before the function or closure instances. So we
165 : * just check if JSSLOT_FLAT_CLOSURE_UPVARS holds a private value encoded
166 : * as a double. We must also ignore newborn closures that do not have the
167 : * private pointer set.
168 : *
169 : * FIXME bug 648320 - allocate upvars on the GC heap to avoid doing it
170 : * here explicitly.
171 : */
172 706008 : if (hasFlatClosureUpvars()) {
173 592987 : js::HeapValue *upvars = getFlatClosureUpvars();
174 592987 : js::Foreground::free_(upvars);
175 : }
176 706008 : }
177 :
178 : inline js::Value
179 718807 : JSFunction::getFlatClosureUpvar(uint32_t i) const
180 : {
181 718807 : JS_ASSERT(hasFlatClosureUpvars());
182 718807 : JS_ASSERT(script()->bindings.countUpvars() == script()->upvars()->length);
183 718807 : JS_ASSERT(i < script()->bindings.countUpvars());
184 718807 : return getFlatClosureUpvars()[i];
185 : }
186 :
187 : inline void
188 0 : JSFunction::setFlatClosureUpvar(uint32_t i, const js::Value &v)
189 : {
190 0 : JS_ASSERT(isFlatClosure());
191 0 : JS_ASSERT(script()->bindings.countUpvars() == script()->upvars()->length);
192 0 : JS_ASSERT(i < script()->bindings.countUpvars());
193 0 : getFlatClosureUpvars()[i] = v;
194 0 : }
195 :
196 : inline void
197 765453 : JSFunction::initFlatClosureUpvar(uint32_t i, const js::Value &v)
198 : {
199 765453 : JS_ASSERT(isFlatClosure());
200 765453 : JS_ASSERT(script()->bindings.countUpvars() == script()->upvars()->length);
201 765453 : JS_ASSERT(i < script()->bindings.countUpvars());
202 765453 : getFlatClosureUpvars()[i].init(v);
203 765453 : }
204 :
205 : /* static */ inline size_t
206 2117 : JSFunction::getFlatClosureUpvarsOffset()
207 : {
208 2117 : return offsetof(js::FunctionExtended, extendedSlots[FLAT_CLOSURE_UPVARS_SLOT]);
209 : }
210 :
211 : namespace js {
212 :
213 : static JS_ALWAYS_INLINE bool
214 822263 : IsFunctionObject(const js::Value &v)
215 : {
216 822263 : return v.isObject() && v.toObject().isFunction();
217 : }
218 :
219 : static JS_ALWAYS_INLINE bool
220 49193798 : IsFunctionObject(const js::Value &v, JSFunction **fun)
221 : {
222 49193798 : if (v.isObject() && v.toObject().isFunction()) {
223 49067215 : *fun = v.toObject().toFunction();
224 49067215 : return true;
225 : }
226 126583 : return false;
227 : }
228 :
229 : static JS_ALWAYS_INLINE bool
230 4200733 : IsNativeFunction(const js::Value &v)
231 : {
232 : JSFunction *fun;
233 4200733 : return IsFunctionObject(v, &fun) && fun->isNative();
234 : }
235 :
236 : static JS_ALWAYS_INLINE bool
237 : IsNativeFunction(const js::Value &v, JSFunction **fun)
238 : {
239 : return IsFunctionObject(v, fun) && (*fun)->isNative();
240 : }
241 :
242 : static JS_ALWAYS_INLINE bool
243 186014 : IsNativeFunction(const js::Value &v, JSNative native)
244 : {
245 : JSFunction *fun;
246 186014 : return IsFunctionObject(v, &fun) && fun->maybeNative() == native;
247 : }
248 :
249 : /*
250 : * When we have an object of a builtin class, we don't quite know what its
251 : * valueOf/toString methods are, since these methods may have been overwritten
252 : * or shadowed. However, we can still do better than the general case by
253 : * hard-coding the necessary properties for us to find the native we expect.
254 : *
255 : * TODO: a per-thread shape-based cache would be faster and simpler.
256 : */
257 : static JS_ALWAYS_INLINE bool
258 186032 : ClassMethodIsNative(JSContext *cx, JSObject *obj, Class *clasp, jsid methodid, JSNative native)
259 : {
260 186032 : JS_ASSERT(obj->getClass() == clasp);
261 :
262 : Value v;
263 186032 : if (!HasDataProperty(cx, obj, methodid, &v)) {
264 186032 : JSObject *proto = obj->getProto();
265 186032 : if (!proto || proto->getClass() != clasp || !HasDataProperty(cx, proto, methodid, &v))
266 18 : return false;
267 : }
268 :
269 186014 : return js::IsNativeFunction(v, native);
270 : }
271 :
272 : extern JS_ALWAYS_INLINE bool
273 : SameTraceType(const Value &lhs, const Value &rhs)
274 : {
275 : return SameType(lhs, rhs) &&
276 : (lhs.isPrimitive() ||
277 : lhs.toObject().isFunction() == rhs.toObject().isFunction());
278 : }
279 :
280 : /* Valueified JS_IsConstructing. */
281 : static JS_ALWAYS_INLINE bool
282 400129 : IsConstructing(const Value *vp)
283 : {
284 : #ifdef DEBUG
285 400129 : JSObject *callee = &JS_CALLEE(cx, vp).toObject();
286 400129 : if (callee->isFunction()) {
287 400129 : JSFunction *fun = callee->toFunction();
288 400129 : JS_ASSERT((fun->flags & JSFUN_CONSTRUCTOR) != 0);
289 : } else {
290 0 : JS_ASSERT(callee->getClass()->construct != NULL);
291 : }
292 : #endif
293 400129 : return vp[1].isMagic();
294 : }
295 :
296 : inline bool
297 367649 : IsConstructing(CallReceiver call)
298 : {
299 367649 : return IsConstructing(call.base());
300 : }
301 :
302 : inline const char *
303 2276 : GetFunctionNameBytes(JSContext *cx, JSFunction *fun, JSAutoByteString *bytes)
304 : {
305 2276 : if (fun->atom)
306 2276 : return bytes->encode(cx, fun->atom);
307 0 : return js_anonymous_str;
308 : }
309 :
310 : extern JSFunctionSpec function_methods[];
311 :
312 : extern JSBool
313 : Function(JSContext *cx, unsigned argc, Value *vp);
314 :
315 : extern bool
316 : IsBuiltinFunctionConstructor(JSFunction *fun);
317 :
318 : /*
319 : * Preconditions: funobj->isInterpreted() && !funobj->isFunctionPrototype() &&
320 : * !funobj->isBoundFunction(). This is sufficient to establish that funobj has
321 : * a non-configurable non-method .prototype data property, thought it might not
322 : * have been resolved yet, and its value could be anything.
323 : *
324 : * Return the shape of the .prototype property of funobj, resolving it if
325 : * needed. On error, return NULL.
326 : *
327 : * This is not safe to call on trace because it defines properties, which can
328 : * trigger lookups that could reenter.
329 : */
330 : const Shape *
331 : LookupInterpretedFunctionPrototype(JSContext *cx, JSObject *funobj);
332 :
333 : static inline JSObject *
334 13782678 : SkipScopeParent(JSObject *parent)
335 : {
336 13782678 : if (!parent)
337 176868 : return NULL;
338 29129022 : while (parent->isScope())
339 1917402 : parent = &parent->asScope().enclosingScope();
340 13605810 : return parent;
341 : }
342 :
343 : inline JSFunction *
344 2403127 : CloneFunctionObject(JSContext *cx, JSFunction *fun, JSObject *parent,
345 : gc::AllocKind kind = JSFunction::FinalizeKind)
346 : {
347 2403127 : JS_ASSERT(parent);
348 2403127 : JSObject *proto = parent->global().getOrCreateFunctionPrototype(cx);
349 2403127 : if (!proto)
350 0 : return NULL;
351 :
352 2403127 : return js_CloneFunctionObject(cx, fun, parent, proto, kind);
353 : }
354 :
355 : inline JSFunction *
356 1828233 : CloneFunctionObjectIfNotSingleton(JSContext *cx, JSFunction *fun, JSObject *parent)
357 : {
358 : /*
359 : * For attempts to clone functions at a function definition opcode or from
360 : * a method barrier, don't perform the clone if the function has singleton
361 : * type. This was called pessimistically, and we need to preserve the
362 : * type's property that if it is singleton there is only a single object
363 : * with its type in existence.
364 : */
365 1828233 : if (fun->hasSingletonType()) {
366 21261 : if (!fun->setParent(cx, SkipScopeParent(parent)))
367 0 : return NULL;
368 21261 : fun->setEnvironment(parent);
369 21261 : return fun;
370 : }
371 :
372 1806972 : return CloneFunctionObject(cx, fun, parent);
373 : }
374 :
375 : inline JSFunction *
376 6344 : CloneFunctionObject(JSContext *cx, JSFunction *fun)
377 : {
378 : /*
379 : * Variant which makes an exact clone of fun, preserving parent and proto.
380 : * Calling the above version CloneFunctionObject(cx, fun, fun->getParent())
381 : * is not equivalent: API clients, including XPConnect, can reparent
382 : * objects so that fun->global() != fun->getProto()->global().
383 : * See ReparentWrapperIfFound.
384 : */
385 6344 : JS_ASSERT(fun->getParent() && fun->getProto());
386 :
387 6344 : if (fun->hasSingletonType())
388 1110 : return fun;
389 :
390 : return js_CloneFunctionObject(cx, fun, fun->environment(), fun->getProto(),
391 5234 : JSFunction::ExtendedFinalizeKind);
392 : }
393 :
394 : } /* namespace js */
395 :
396 : inline void
397 1165565 : JSFunction::setScript(JSScript *script_)
398 : {
399 1165565 : JS_ASSERT(isInterpreted());
400 1165565 : script() = script_;
401 1165565 : }
402 :
403 : inline void
404 2446913 : JSFunction::initScript(JSScript *script_)
405 : {
406 2446913 : JS_ASSERT(isInterpreted());
407 2446913 : script().init(script_);
408 2446913 : }
409 :
410 : #endif /* jsfuninlines_h___ */
|