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