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 | |
31 | namespace JSC { |
32 | |
33 | STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(InternalFunction); |
34 | |
35 | const ClassInfo InternalFunction::s_info = { "Function" , &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(InternalFunction) }; |
36 | |
37 | InternalFunction::InternalFunction(VM& vm, Structure* structure, NativeFunction functionForCall, NativeFunction functionForConstruct) |
38 | : JSDestructibleObject(vm, structure) |
39 | , m_functionForCall(functionForCall) |
40 | , m_functionForConstruct(functionForConstruct ? functionForConstruct : callHostFunctionAsConstructor) |
41 | { |
42 | // exec->vm() wants callees to not be large allocations. |
43 | RELEASE_ASSERT(!isLargeAllocation()); |
44 | ASSERT_WITH_MESSAGE(m_functionForCall, "[[Call]] must be implemented" ); |
45 | ASSERT(m_functionForConstruct); |
46 | } |
47 | |
48 | void InternalFunction::finishCreation(VM& vm, const String& name, NameVisibility nameVisibility, NameAdditionMode nameAdditionMode) |
49 | { |
50 | Base::finishCreation(vm); |
51 | ASSERT(jsDynamicCast<InternalFunction*>(vm, this)); |
52 | ASSERT(methodTable(vm)->getCallData == InternalFunction::info()->methodTable.getCallData); |
53 | ASSERT(methodTable(vm)->getConstructData == InternalFunction::info()->methodTable.getConstructData); |
54 | ASSERT(type() == InternalFunctionType); |
55 | JSString* nameString = jsString(&vm, name); |
56 | m_originalName.set(vm, this, nameString); |
57 | if (nameVisibility == NameVisibility::Visible) { |
58 | if (nameAdditionMode == NameAdditionMode::WithStructureTransition) |
59 | putDirect(vm, vm.propertyNames->name, nameString, PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum); |
60 | else |
61 | putDirectWithoutTransition(vm, vm.propertyNames->name, nameString, PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum); |
62 | } |
63 | } |
64 | |
65 | void InternalFunction::visitChildren(JSCell* cell, SlotVisitor& visitor) |
66 | { |
67 | InternalFunction* thisObject = jsCast<InternalFunction*>(cell); |
68 | ASSERT_GC_OBJECT_INHERITS(thisObject, info()); |
69 | Base::visitChildren(thisObject, visitor); |
70 | |
71 | visitor.append(thisObject->m_originalName); |
72 | } |
73 | |
74 | const String& InternalFunction::name() |
75 | { |
76 | const String& name = m_originalName->tryGetValue(); |
77 | ASSERT(name); // m_originalName was built from a String, and hence, there is no rope to resolve. |
78 | return name; |
79 | } |
80 | |
81 | const String InternalFunction::displayName(VM& vm) |
82 | { |
83 | JSValue displayName = getDirect(vm, vm.propertyNames->displayName); |
84 | |
85 | if (displayName && isJSString(displayName)) |
86 | return asString(displayName)->tryGetValue(); |
87 | |
88 | return String(); |
89 | } |
90 | |
91 | CallType InternalFunction::getCallData(JSCell* cell, CallData& callData) |
92 | { |
93 | auto* function = jsCast<InternalFunction*>(cell); |
94 | ASSERT(function->m_functionForCall); |
95 | callData.native.function = function->m_functionForCall; |
96 | return CallType::Host; |
97 | } |
98 | |
99 | ConstructType InternalFunction::getConstructData(JSCell* cell, ConstructData& constructData) |
100 | { |
101 | auto* function = jsCast<InternalFunction*>(cell); |
102 | if (function->m_functionForConstruct == callHostFunctionAsConstructor) |
103 | return ConstructType::None; |
104 | constructData.native.function = function->m_functionForConstruct; |
105 | return ConstructType::Host; |
106 | } |
107 | |
108 | const String InternalFunction::calculatedDisplayName(VM& vm) |
109 | { |
110 | const String explicitName = displayName(vm); |
111 | |
112 | if (!explicitName.isEmpty()) |
113 | return explicitName; |
114 | |
115 | return name(); |
116 | } |
117 | |
118 | Structure* InternalFunction::createSubclassStructureSlow(ExecState* exec, JSValue newTarget, Structure* baseClass) |
119 | { |
120 | VM& vm = exec->vm(); |
121 | auto scope = DECLARE_THROW_SCOPE(vm); |
122 | ASSERT(!newTarget || newTarget.isConstructor(vm)); |
123 | ASSERT(newTarget && newTarget != exec->jsCallee()); |
124 | |
125 | ASSERT(baseClass->hasMonoProto()); |
126 | |
127 | // newTarget may be an InternalFunction if we were called from Reflect.construct. |
128 | JSFunction* targetFunction = jsDynamicCast<JSFunction*>(vm, newTarget); |
129 | JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject(); |
130 | |
131 | if (LIKELY(targetFunction)) { |
132 | Structure* structure = targetFunction->rareData(vm)->internalFunctionAllocationStructure(); |
133 | if (LIKELY(structure && structure->classInfo() == baseClass->classInfo())) |
134 | return structure; |
135 | |
136 | // Note, Reflect.construct might cause the profile to churn but we don't care. |
137 | JSValue prototypeValue = newTarget.get(exec, vm.propertyNames->prototype); |
138 | RETURN_IF_EXCEPTION(scope, nullptr); |
139 | if (JSObject* prototype = jsDynamicCast<JSObject*>(vm, prototypeValue)) |
140 | return targetFunction->rareData(vm)->createInternalFunctionAllocationStructureFromBase(vm, lexicalGlobalObject, prototype, baseClass); |
141 | } else { |
142 | JSValue prototypeValue = newTarget.get(exec, vm.propertyNames->prototype); |
143 | RETURN_IF_EXCEPTION(scope, nullptr); |
144 | if (JSObject* prototype = jsDynamicCast<JSObject*>(vm, prototypeValue)) { |
145 | // This only happens if someone Reflect.constructs our builtin constructor with another builtin constructor as the new.target. |
146 | // Thus, we don't care about the cost of looking up the structure from our hash table every time. |
147 | return vm.structureCache.emptyStructureForPrototypeFromBaseStructure(lexicalGlobalObject, prototype, baseClass); |
148 | } |
149 | } |
150 | |
151 | return baseClass; |
152 | } |
153 | |
154 | |
155 | } // namespace JSC |
156 | |