Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef jsfuninlines_h |
michael@0 | 8 | #define jsfuninlines_h |
michael@0 | 9 | |
michael@0 | 10 | #include "jsfun.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "vm/ScopeObject.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace js { |
michael@0 | 15 | |
michael@0 | 16 | inline const char * |
michael@0 | 17 | GetFunctionNameBytes(JSContext *cx, JSFunction *fun, JSAutoByteString *bytes) |
michael@0 | 18 | { |
michael@0 | 19 | JSAtom *atom = fun->atom(); |
michael@0 | 20 | if (atom) |
michael@0 | 21 | return bytes->encodeLatin1(cx, atom); |
michael@0 | 22 | return js_anonymous_str; |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | static inline JSObject * |
michael@0 | 26 | SkipScopeParent(JSObject *parent) |
michael@0 | 27 | { |
michael@0 | 28 | if (!parent) |
michael@0 | 29 | return nullptr; |
michael@0 | 30 | while (parent->is<ScopeObject>()) |
michael@0 | 31 | parent = &parent->as<ScopeObject>().enclosingScope(); |
michael@0 | 32 | return parent; |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | inline bool |
michael@0 | 36 | CanReuseFunctionForClone(JSContext *cx, HandleFunction fun) |
michael@0 | 37 | { |
michael@0 | 38 | if (!fun->hasSingletonType()) |
michael@0 | 39 | return false; |
michael@0 | 40 | if (fun->isInterpretedLazy()) { |
michael@0 | 41 | LazyScript *lazy = fun->lazyScript(); |
michael@0 | 42 | if (lazy->hasBeenCloned()) |
michael@0 | 43 | return false; |
michael@0 | 44 | lazy->setHasBeenCloned(); |
michael@0 | 45 | } else { |
michael@0 | 46 | JSScript *script = fun->nonLazyScript(); |
michael@0 | 47 | if (script->hasBeenCloned()) |
michael@0 | 48 | return false; |
michael@0 | 49 | script->setHasBeenCloned(); |
michael@0 | 50 | } |
michael@0 | 51 | return true; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | inline JSFunction * |
michael@0 | 55 | CloneFunctionObjectIfNotSingleton(JSContext *cx, HandleFunction fun, HandleObject parent, |
michael@0 | 56 | NewObjectKind newKind = GenericObject) |
michael@0 | 57 | { |
michael@0 | 58 | /* |
michael@0 | 59 | * For attempts to clone functions at a function definition opcode, |
michael@0 | 60 | * try to avoid the the clone if the function has singleton type. This |
michael@0 | 61 | * was called pessimistically, and we need to preserve the type's |
michael@0 | 62 | * property that if it is singleton there is only a single object |
michael@0 | 63 | * with its type in existence. |
michael@0 | 64 | * |
michael@0 | 65 | * For functions inner to run once lambda, it may be possible that |
michael@0 | 66 | * the lambda runs multiple times and we repeatedly clone it. In these |
michael@0 | 67 | * cases, fall through to CloneFunctionObject, which will deep clone |
michael@0 | 68 | * the function's script. |
michael@0 | 69 | */ |
michael@0 | 70 | if (CanReuseFunctionForClone(cx, fun)) { |
michael@0 | 71 | RootedObject obj(cx, SkipScopeParent(parent)); |
michael@0 | 72 | if (!JSObject::setParent(cx, fun, obj)) |
michael@0 | 73 | return nullptr; |
michael@0 | 74 | fun->setEnvironment(parent); |
michael@0 | 75 | return fun; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | // These intermediate variables are needed to avoid link errors on some |
michael@0 | 79 | // platforms. Sigh. |
michael@0 | 80 | gc::AllocKind finalizeKind = JSFunction::FinalizeKind; |
michael@0 | 81 | gc::AllocKind extendedFinalizeKind = JSFunction::ExtendedFinalizeKind; |
michael@0 | 82 | gc::AllocKind kind = fun->isExtended() |
michael@0 | 83 | ? extendedFinalizeKind |
michael@0 | 84 | : finalizeKind; |
michael@0 | 85 | return CloneFunctionObject(cx, fun, parent, kind, newKind); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | } /* namespace js */ |
michael@0 | 89 | |
michael@0 | 90 | #endif /* jsfuninlines_h */ |