michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef jsfuninlines_h michael@0: #define jsfuninlines_h michael@0: michael@0: #include "jsfun.h" michael@0: michael@0: #include "vm/ScopeObject.h" michael@0: michael@0: namespace js { michael@0: michael@0: inline const char * michael@0: GetFunctionNameBytes(JSContext *cx, JSFunction *fun, JSAutoByteString *bytes) michael@0: { michael@0: JSAtom *atom = fun->atom(); michael@0: if (atom) michael@0: return bytes->encodeLatin1(cx, atom); michael@0: return js_anonymous_str; michael@0: } michael@0: michael@0: static inline JSObject * michael@0: SkipScopeParent(JSObject *parent) michael@0: { michael@0: if (!parent) michael@0: return nullptr; michael@0: while (parent->is()) michael@0: parent = &parent->as().enclosingScope(); michael@0: return parent; michael@0: } michael@0: michael@0: inline bool michael@0: CanReuseFunctionForClone(JSContext *cx, HandleFunction fun) michael@0: { michael@0: if (!fun->hasSingletonType()) michael@0: return false; michael@0: if (fun->isInterpretedLazy()) { michael@0: LazyScript *lazy = fun->lazyScript(); michael@0: if (lazy->hasBeenCloned()) michael@0: return false; michael@0: lazy->setHasBeenCloned(); michael@0: } else { michael@0: JSScript *script = fun->nonLazyScript(); michael@0: if (script->hasBeenCloned()) michael@0: return false; michael@0: script->setHasBeenCloned(); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: inline JSFunction * michael@0: CloneFunctionObjectIfNotSingleton(JSContext *cx, HandleFunction fun, HandleObject parent, michael@0: NewObjectKind newKind = GenericObject) michael@0: { michael@0: /* michael@0: * For attempts to clone functions at a function definition opcode, michael@0: * try to avoid the the clone if the function has singleton type. This michael@0: * was called pessimistically, and we need to preserve the type's michael@0: * property that if it is singleton there is only a single object michael@0: * with its type in existence. michael@0: * michael@0: * For functions inner to run once lambda, it may be possible that michael@0: * the lambda runs multiple times and we repeatedly clone it. In these michael@0: * cases, fall through to CloneFunctionObject, which will deep clone michael@0: * the function's script. michael@0: */ michael@0: if (CanReuseFunctionForClone(cx, fun)) { michael@0: RootedObject obj(cx, SkipScopeParent(parent)); michael@0: if (!JSObject::setParent(cx, fun, obj)) michael@0: return nullptr; michael@0: fun->setEnvironment(parent); michael@0: return fun; michael@0: } michael@0: michael@0: // These intermediate variables are needed to avoid link errors on some michael@0: // platforms. Sigh. michael@0: gc::AllocKind finalizeKind = JSFunction::FinalizeKind; michael@0: gc::AllocKind extendedFinalizeKind = JSFunction::ExtendedFinalizeKind; michael@0: gc::AllocKind kind = fun->isExtended() michael@0: ? extendedFinalizeKind michael@0: : finalizeKind; michael@0: return CloneFunctionObject(cx, fun, parent, kind, newKind); michael@0: } michael@0: michael@0: } /* namespace js */ michael@0: michael@0: #endif /* jsfuninlines_h */