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