1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sts=4 et sw=4 tw=99:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
12 #include "vm/ScopeObject.h"
17 GetFunctionNameBytes(JSContext
* cx
, JSFunction
* fun
, JSAutoByteString
* bytes
)
19 JSAtom
* atom
= fun
->atom();
21 return bytes
->encodeLatin1(cx
, atom
);
22 return js_anonymous_str
;
25 static inline JSObject
*
26 SkipScopeParent(JSObject
* parent
)
30 while (parent
->is
<ScopeObject
>())
31 parent
= &parent
->as
<ScopeObject
>().enclosingScope();
36 CanReuseFunctionForClone(JSContext
* cx
, HandleFunction fun
)
38 if (!fun
->hasSingletonType())
40 if (fun
->isInterpretedLazy()) {
41 LazyScript
* lazy
= fun
->lazyScript();
42 if (lazy
->hasBeenCloned())
44 lazy
->setHasBeenCloned();
46 JSScript
* script
= fun
->nonLazyScript();
47 if (script
->hasBeenCloned())
49 script
->setHasBeenCloned();
55 CloneFunctionObjectIfNotSingleton(JSContext
* cx
, HandleFunction fun
, HandleObject parent
,
56 NewObjectKind newKind
= GenericObject
)
59 * For attempts to clone functions at a function definition opcode,
60 * try to avoid the the clone if the function has singleton type. This
61 * was called pessimistically, and we need to preserve the type's
62 * property that if it is singleton there is only a single object
63 * with its type in existence.
65 * For functions inner to run once lambda, it may be possible that
66 * the lambda runs multiple times and we repeatedly clone it. In these
67 * cases, fall through to CloneFunctionObject, which will deep clone
68 * the function's script.
70 if (CanReuseFunctionForClone(cx
, fun
)) {
71 RootedObject
obj(cx
, SkipScopeParent(parent
));
72 if (!JSObject::setParent(cx
, fun
, obj
))
74 fun
->setEnvironment(parent
);
78 // These intermediate variables are needed to avoid link errors on some
80 gc::AllocKind finalizeKind
= JSFunction::FinalizeKind
;
81 gc::AllocKind extendedFinalizeKind
= JSFunction::ExtendedFinalizeKind
;
82 gc::AllocKind kind
= fun
->isExtended()
83 ? extendedFinalizeKind
85 return CloneFunctionObject(cx
, fun
, parent
, kind
, newKind
);
90 #endif /* jsfuninlines_h */