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::isHostOrBuiltinFunction() const
72{
73 return isHostFunction() || isBuiltinFunction();
74}
75
76inline bool JSFunction::isClassConstructorFunction() const
77{
78 return !isHostFunction() && jsExecutable()->isClassConstructorFunction();
79}
80
81inline TaggedNativeFunction JSFunction::nativeFunction()
82{
83 ASSERT(isHostFunctionNonInline());
84 return static_cast<NativeExecutable*>(m_executable.get())->function();
85}
86
87inline TaggedNativeFunction JSFunction::nativeConstructor()
88{
89 ASSERT(isHostFunctionNonInline());
90 return static_cast<NativeExecutable*>(m_executable.get())->constructor();
91}
92
93inline bool isHostFunction(JSValue value, TaggedNativeFunction nativeFunction)
94{
95 JSFunction* function = jsCast<JSFunction*>(getJSFunction(value));
96 if (!function || !function->isHostFunction())
97 return false;
98 return function->nativeFunction() == nativeFunction;
99}
100
101inline bool JSFunction::hasReifiedLength() const
102{
103 return m_rareData ? m_rareData->hasReifiedLength() : false;
104}
105
106inline bool JSFunction::hasReifiedName() const
107{
108 return m_rareData ? m_rareData->hasReifiedName() : false;
109}
110
111inline bool JSFunction::canUseAllocationProfile()
112{
113 if (isHostOrBuiltinFunction()) {
114 if (isHostFunction())
115 return false;
116
117 VM& vm = globalObject()->vm();
118 unsigned attributes;
119 JSValue prototype = getDirect(vm, vm.propertyNames->prototype, attributes);
120 if (!prototype || (attributes & PropertyAttribute::AccessorOrCustomAccessorOrValue))
121 return false;
122 }
123
124 // If we don't have a prototype property, we're not guaranteed it's
125 // non-configurable. For example, user code can define the prototype
126 // as a getter. JS semantics require that the getter is called every
127 // time |construct| occurs with this function as new.target.
128 return jsExecutable()->hasPrototypeProperty();
129}
130
131inline FunctionRareData* JSFunction::ensureRareDataAndAllocationProfile(ExecState* exec, unsigned inlineCapacity)
132{
133 ASSERT(canUseAllocationProfile());
134 if (UNLIKELY(!m_rareData))
135 return allocateAndInitializeRareData(exec, inlineCapacity);
136 if (UNLIKELY(!m_rareData->isObjectAllocationProfileInitialized()))
137 return initializeRareData(exec, inlineCapacity);
138 return m_rareData.get();
139}
140
141} // namespace JSC
142