1/*
2 * Copyright (C) 1999-2002 Harri Porten ([email protected])
3 * Copyright (C) 2001 Peter Kelly ([email protected])
4 * Copyright (C) 2004-2019 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#include "config.h"
24#include "InternalFunction.h"
25
26#include "FunctionPrototype.h"
27#include "JSGlobalObject.h"
28#include "JSString.h"
29#include "JSCInlines.h"
30
31namespace JSC {
32
33STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(InternalFunction);
34
35const ClassInfo InternalFunction::s_info = { "Function", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(InternalFunction) };
36
37InternalFunction::InternalFunction(VM& vm, Structure* structure, NativeFunction functionForCall, NativeFunction functionForConstruct)
38 : JSDestructibleObject(vm, structure)
39 , m_functionForCall(functionForCall)
40 , m_functionForConstruct(functionForConstruct ? functionForConstruct : callHostFunctionAsConstructor)
41 , m_globalObject(vm, this, structure->globalObject())
42{
43 ASSERT_WITH_MESSAGE(m_functionForCall, "[[Call]] must be implemented");
44 ASSERT(m_functionForConstruct);
45}
46
47void InternalFunction::finishCreation(VM& vm, const String& name, NameAdditionMode nameAdditionMode)
48{
49 Base::finishCreation(vm);
50 ASSERT(jsDynamicCast<InternalFunction*>(vm, this));
51 ASSERT(methodTable(vm)->getCallData == InternalFunction::info()->methodTable.getCallData);
52 ASSERT(methodTable(vm)->getConstructData == InternalFunction::info()->methodTable.getConstructData);
53 ASSERT(type() == InternalFunctionType);
54 JSString* nameString = jsString(vm, name);
55 m_originalName.set(vm, this, nameString);
56 if (nameAdditionMode == NameAdditionMode::WithStructureTransition)
57 putDirect(vm, vm.propertyNames->name, nameString, PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum);
58 else
59 putDirectWithoutTransition(vm, vm.propertyNames->name, nameString, PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum);
60}
61
62void InternalFunction::visitChildren(JSCell* cell, SlotVisitor& visitor)
63{
64 InternalFunction* thisObject = jsCast<InternalFunction*>(cell);
65 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
66 Base::visitChildren(thisObject, visitor);
67
68 visitor.append(thisObject->m_originalName);
69}
70
71const String& InternalFunction::name()
72{
73 const String& name = m_originalName->tryGetValue();
74 ASSERT(name); // m_originalName was built from a String, and hence, there is no rope to resolve.
75 return name;
76}
77
78const String InternalFunction::displayName(VM& vm)
79{
80 JSValue displayName = getDirect(vm, vm.propertyNames->displayName);
81
82 if (displayName && isJSString(displayName))
83 return asString(displayName)->tryGetValue();
84
85 return String();
86}
87
88CallType InternalFunction::getCallData(JSCell* cell, CallData& callData)
89{
90 auto* function = jsCast<InternalFunction*>(cell);
91 ASSERT(function->m_functionForCall);
92 callData.native.function = function->m_functionForCall;
93 return CallType::Host;
94}
95
96ConstructType InternalFunction::getConstructData(JSCell* cell, ConstructData& constructData)
97{
98 auto* function = jsCast<InternalFunction*>(cell);
99 if (function->m_functionForConstruct == callHostFunctionAsConstructor)
100 return ConstructType::None;
101 constructData.native.function = function->m_functionForConstruct;
102 return ConstructType::Host;
103}
104
105const String InternalFunction::calculatedDisplayName(VM& vm)
106{
107 const String explicitName = displayName(vm);
108
109 if (!explicitName.isEmpty())
110 return explicitName;
111
112 return name();
113}
114
115Structure* InternalFunction::createSubclassStructureSlow(JSGlobalObject* globalObject, JSValue newTarget, Structure* baseClass)
116{
117 VM& vm = globalObject->vm();
118 auto scope = DECLARE_THROW_SCOPE(vm);
119
120 ASSERT(baseClass->hasMonoProto());
121
122 // newTarget may be an InternalFunction if we were called from Reflect.construct.
123 JSFunction* targetFunction = jsDynamicCast<JSFunction*>(vm, newTarget);
124 JSGlobalObject* baseGlobalObject = baseClass->globalObject();
125
126 if (LIKELY(targetFunction)) {
127 Structure* structure = targetFunction->rareData(vm)->internalFunctionAllocationStructure();
128 if (LIKELY(structure && structure->classInfo() == baseClass->classInfo() && structure->globalObject() == baseGlobalObject))
129 return structure;
130
131 // Note, Reflect.construct might cause the profile to churn but we don't care.
132 JSValue prototypeValue = targetFunction->get(globalObject, vm.propertyNames->prototype);
133 RETURN_IF_EXCEPTION(scope, nullptr);
134 if (JSObject* prototype = jsDynamicCast<JSObject*>(vm, prototypeValue))
135 return targetFunction->rareData(vm)->createInternalFunctionAllocationStructureFromBase(vm, baseGlobalObject, prototype, baseClass);
136 } else {
137 JSValue prototypeValue = newTarget.get(globalObject, vm.propertyNames->prototype);
138 RETURN_IF_EXCEPTION(scope, nullptr);
139 if (JSObject* prototype = jsDynamicCast<JSObject*>(vm, prototypeValue)) {
140 // This only happens if someone Reflect.constructs our builtin constructor with another builtin constructor as the new.target.
141 // Thus, we don't care about the cost of looking up the structure from our hash table every time.
142 return vm.structureCache.emptyStructureForPrototypeFromBaseStructure(baseGlobalObject, prototype, baseClass);
143 }
144 }
145
146 return baseClass;
147}
148
149
150} // namespace JSC
151