1/*
2 * Copyright (C) 2013-2018 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "FunctionExecutable.h"
29#include "JSFunction.h"
30#include "NativeExecutable.h"
31
32namespace JSC {
33
34inline JSFunction* JSFunction::createWithInvalidatedReallocationWatchpoint(
35 VM& vm, FunctionExecutable* executable, JSScope* scope)
36{
37 ASSERT(executable->singleton().hasBeenInvalidated());
38 return createImpl(vm, executable, scope, selectStructureForNewFuncExp(scope->globalObject(vm), executable));
39}
40
41inline JSFunction::JSFunction(VM& vm, FunctionExecutable* executable, JSScope* scope, Structure* structure)
42 : Base(vm, scope, structure)
43 , m_executable(vm, this, executable)
44 , m_rareData()
45{
46 assertTypeInfoFlagInvariants();
47}
48
49inline FunctionExecutable* JSFunction::jsExecutable() const
50{
51 ASSERT(!isHostFunctionNonInline());
52 return static_cast<FunctionExecutable*>(m_executable.get());
53}
54
55inline bool JSFunction::isHostFunction() const
56{
57 ASSERT(m_executable);
58 return m_executable->isHostFunction();
59}
60
61inline Intrinsic JSFunction::intrinsic() const
62{
63 return executable()->intrinsic();
64}
65
66inline bool JSFunction::isBuiltinFunction() const
67{
68 return !isHostFunction() && jsExecutable()->isBuiltinFunction();
69}
70
71inline bool JSFunction::isAnonymousBuiltinFunction() const
72{
73 return !isHostFunction() && jsExecutable()->isAnonymousBuiltinFunction();
74}
75
76inline bool JSFunction::isHostOrBuiltinFunction() const
77{
78 return isHostFunction() || isBuiltinFunction();
79}
80
81inline bool JSFunction::isClassConstructorFunction() const
82{
83 return !isHostFunction() && jsExecutable()->isClassConstructorFunction();
84}
85
86inline TaggedNativeFunction JSFunction::nativeFunction()
87{
88 ASSERT(isHostFunctionNonInline());
89 return static_cast<NativeExecutable*>(m_executable.get())->function();
90}
91
92inline TaggedNativeFunction JSFunction::nativeConstructor()
93{
94 ASSERT(isHostFunctionNonInline());
95 return static_cast<NativeExecutable*>(m_executable.get())->constructor();
96}
97
98inline bool isHostFunction(JSValue value, TaggedNativeFunction nativeFunction)
99{
100 JSFunction* function = jsCast<JSFunction*>(getJSFunction(value));
101 if (!function || !function->isHostFunction())
102 return false;
103 return function->nativeFunction() == nativeFunction;
104}
105
106inline bool JSFunction::hasReifiedLength() const
107{
108 return m_rareData ? m_rareData->hasReifiedLength() : false;
109}
110
111inline bool JSFunction::hasReifiedName() const
112{
113 return m_rareData ? m_rareData->hasReifiedName() : false;
114}
115
116inline bool JSFunction::canUseAllocationProfile()
117{
118 if (isHostOrBuiltinFunction()) {
119 if (isHostFunction())
120 return false;
121
122 VM& vm = globalObject()->vm();
123 unsigned attributes;
124 JSValue prototype = getDirect(vm, vm.propertyNames->prototype, attributes);
125 if (!prototype || (attributes & PropertyAttribute::AccessorOrCustomAccessorOrValue))
126 return false;
127 }
128
129 // If we don't have a prototype property, we're not guaranteed it's
130 // non-configurable. For example, user code can define the prototype
131 // as a getter. JS semantics require that the getter is called every
132 // time |construct| occurs with this function as new.target.
133 return jsExecutable()->hasPrototypeProperty();
134}
135
136inline FunctionRareData* JSFunction::ensureRareDataAndAllocationProfile(ExecState* exec, unsigned inlineCapacity)
137{
138 ASSERT(canUseAllocationProfile());
139 if (UNLIKELY(!m_rareData))
140 return allocateAndInitializeRareData(exec, inlineCapacity);
141 if (UNLIKELY(!m_rareData->isObjectAllocationProfileInitialized()))
142 return initializeRareData(exec, inlineCapacity);
143 return m_rareData.get();
144}
145
146} // namespace JSC
147