Unreviewed, rolling out r221327.
[WebKit-https.git] / Source / JavaScriptCore / runtime / JSFunction.cpp
1 /*
2  *  Copyright (C) 1999-2002 Harri Porten (porten@kde.org)
3  *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
4  *  Copyright (C) 2003-2009, 2015-2017 Apple Inc. All rights reserved.
5  *  Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
6  *  Copyright (C) 2007 Maks Orlovich
7  *  Copyright (C) 2015 Canon Inc. All rights reserved.
8  *
9  *  This library is free software; you can redistribute it and/or
10  *  modify it under the terms of the GNU Library General Public
11  *  License as published by the Free Software Foundation; either
12  *  version 2 of the License, or (at your option) any later version.
13  *
14  *  This library is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  Library General Public License for more details.
18  *
19  *  You should have received a copy of the GNU Library General Public License
20  *  along with this library; see the file COPYING.LIB.  If not, write to
21  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  *  Boston, MA 02110-1301, USA.
23  *
24  */
25
26 #include "config.h"
27 #include "JSFunction.h"
28
29 #include "AsyncGeneratorPrototype.h"
30 #include "BuiltinNames.h"
31 #include "ClonedArguments.h"
32 #include "CodeBlock.h"
33 #include "CommonIdentifiers.h"
34 #include "CallFrame.h"
35 #include "ExceptionHelpers.h"
36 #include "FunctionPrototype.h"
37 #include "GeneratorPrototype.h"
38 #include "GetterSetter.h"
39 #include "JSArray.h"
40 #include "JSBoundFunction.h"
41 #include "JSCInlines.h"
42 #include "JSFunctionInlines.h"
43 #include "JSGlobalObject.h"
44 #include "Interpreter.h"
45 #include "ObjectConstructor.h"
46 #include "ObjectPrototype.h"
47 #include "Parser.h"
48 #include "PropertyNameArray.h"
49 #include "StackVisitor.h"
50
51 namespace JSC {
52
53 EncodedJSValue JSC_HOST_CALL callHostFunctionAsConstructor(ExecState* exec)
54 {
55     VM& vm = exec->vm();
56     auto scope = DECLARE_THROW_SCOPE(vm);
57     return throwVMError(exec, scope, createNotAConstructorError(exec, exec->jsCallee()));
58 }
59
60 const ClassInfo JSFunction::s_info = { "Function", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSFunction) };
61
62 bool JSFunction::isHostFunctionNonInline() const
63 {
64     return isHostFunction();
65 }
66
67 JSFunction* JSFunction::create(VM& vm, FunctionExecutable* executable, JSScope* scope)
68 {
69     return create(vm, executable, scope, scope->globalObject(vm)->functionStructure());
70 }
71
72 JSFunction* JSFunction::create(VM& vm, FunctionExecutable* executable, JSScope* scope, Structure* structure)
73 {
74     JSFunction* result = createImpl(vm, executable, scope, structure);
75     executable->singletonFunction()->notifyWrite(vm, result, "Allocating a function");
76     return result;
77 }
78
79 JSFunction* JSFunction::create(VM& vm, JSGlobalObject* globalObject, int length, const String& name, NativeFunction nativeFunction, Intrinsic intrinsic, NativeFunction nativeConstructor, const DOMJIT::Signature* signature)
80 {
81     NativeExecutable* executable = vm.getHostFunction(nativeFunction, intrinsic, nativeConstructor, signature, name);
82     JSFunction* function = new (NotNull, allocateCell<JSFunction>(vm.heap)) JSFunction(vm, globalObject, globalObject->functionStructure());
83     // Can't do this during initialization because getHostFunction might do a GC allocation.
84     function->finishCreation(vm, executable, length, name);
85     return function;
86 }
87
88 JSFunction::JSFunction(VM& vm, JSGlobalObject* globalObject, Structure* structure)
89     : Base(vm, globalObject, structure)
90     , m_executable()
91 {
92 }
93
94 void JSFunction::finishCreation(VM& vm, NativeExecutable* executable, int length, const String& name)
95 {
96     Base::finishCreation(vm);
97     ASSERT(inherits(vm, info()));
98     m_executable.set(vm, this, executable);
99     // Some NativeExecutable functions, like JSBoundFunction, decide to lazily allocate their name string.
100     if (!name.isNull())
101         putDirect(vm, vm.propertyNames->name, jsString(&vm, name), ReadOnly | DontEnum);
102     putDirect(vm, vm.propertyNames->length, jsNumber(length), ReadOnly | DontEnum);
103 }
104
105 JSFunction* JSFunction::createBuiltinFunction(VM& vm, FunctionExecutable* executable, JSGlobalObject* globalObject)
106 {
107     JSFunction* function = create(vm, executable, globalObject);
108     function->putDirect(vm, vm.propertyNames->name, jsString(&vm, executable->name().string()), ReadOnly | DontEnum);
109     return function;
110 }
111
112 JSFunction* JSFunction::createBuiltinFunction(VM& vm, FunctionExecutable* executable, JSGlobalObject* globalObject, const String& name)
113 {
114     JSFunction* function = create(vm, executable, globalObject);
115     function->putDirect(vm, vm.propertyNames->name, jsString(&vm, name), ReadOnly | DontEnum);
116     return function;
117 }
118
119 FunctionRareData* JSFunction::allocateRareData(VM& vm)
120 {
121     ASSERT(!m_rareData);
122     FunctionRareData* rareData = FunctionRareData::create(vm);
123
124     // A DFG compilation thread may be trying to read the rare data
125     // We want to ensure that it sees it properly allocated
126     WTF::storeStoreFence();
127
128     m_rareData.set(vm, this, rareData);
129     return m_rareData.get();
130 }
131
132 FunctionRareData* JSFunction::allocateAndInitializeRareData(ExecState* exec, size_t inlineCapacity)
133 {
134     ASSERT(!m_rareData);
135     VM& vm = exec->vm();
136     JSObject* prototype = jsDynamicCast<JSObject*>(vm, get(exec, vm.propertyNames->prototype));
137     JSGlobalObject* globalObject = this->globalObject(vm);
138     if (!prototype)
139         prototype = globalObject->objectPrototype();
140     FunctionRareData* rareData = FunctionRareData::create(vm);
141     rareData->initializeObjectAllocationProfile(vm, globalObject, prototype, inlineCapacity);
142
143     // A DFG compilation thread may be trying to read the rare data
144     // We want to ensure that it sees it properly allocated
145     WTF::storeStoreFence();
146
147     m_rareData.set(vm, this, rareData);
148     return m_rareData.get();
149 }
150
151 FunctionRareData* JSFunction::initializeRareData(ExecState* exec, size_t inlineCapacity)
152 {
153     ASSERT(!!m_rareData);
154     VM& vm = exec->vm();
155     JSObject* prototype = jsDynamicCast<JSObject*>(vm, get(exec, vm.propertyNames->prototype));
156     JSGlobalObject* globalObject = this->globalObject(vm);
157     if (!prototype)
158         prototype = globalObject->objectPrototype();
159     m_rareData->initializeObjectAllocationProfile(vm, globalObject, prototype, inlineCapacity);
160     return m_rareData.get();
161 }
162
163 String JSFunction::name(VM& vm)
164 {
165     if (isHostFunction()) {
166         NativeExecutable* executable = jsCast<NativeExecutable*>(this->executable());
167         return executable->name();
168     }
169     const Identifier identifier = jsExecutable()->name();
170     if (identifier == vm.propertyNames->builtinNames().starDefaultPrivateName())
171         return emptyString();
172     return identifier.string();
173 }
174
175 String JSFunction::displayName(VM& vm)
176 {
177     JSValue displayName = getDirect(vm, vm.propertyNames->displayName);
178     
179     if (displayName && isJSString(displayName))
180         return asString(displayName)->tryGetValue();
181     
182     return String();
183 }
184
185 const String JSFunction::calculatedDisplayName(VM& vm)
186 {
187     const String explicitName = displayName(vm);
188     
189     if (!explicitName.isEmpty())
190         return explicitName;
191     
192     const String actualName = name(vm);
193     if (!actualName.isEmpty() || isHostOrBuiltinFunction())
194         return actualName;
195     
196     return jsExecutable()->inferredName().string();
197 }
198
199 const SourceCode* JSFunction::sourceCode() const
200 {
201     if (isHostOrBuiltinFunction())
202         return 0;
203     return &jsExecutable()->source();
204 }
205     
206 void JSFunction::visitChildren(JSCell* cell, SlotVisitor& visitor)
207 {
208     JSFunction* thisObject = jsCast<JSFunction*>(cell);
209     ASSERT_GC_OBJECT_INHERITS(thisObject, info());
210     Base::visitChildren(thisObject, visitor);
211
212     visitor.append(thisObject->m_executable);
213     visitor.append(thisObject->m_rareData);
214 }
215
216 CallType JSFunction::getCallData(JSCell* cell, CallData& callData)
217 {
218     JSFunction* thisObject = jsCast<JSFunction*>(cell);
219     if (thisObject->isHostFunction()) {
220         callData.native.function = thisObject->nativeFunction();
221         return CallType::Host;
222     }
223     callData.js.functionExecutable = thisObject->jsExecutable();
224     callData.js.scope = thisObject->scope();
225     return CallType::JS;
226 }
227
228 class RetrieveArgumentsFunctor {
229 public:
230     RetrieveArgumentsFunctor(JSFunction* functionObj)
231         : m_targetCallee(functionObj)
232         , m_result(jsNull())
233     {
234     }
235
236     JSValue result() const { return m_result; }
237
238     StackVisitor::Status operator()(StackVisitor& visitor) const
239     {
240         if (!visitor->callee().isCell())
241             return StackVisitor::Continue;
242
243         JSCell* callee = visitor->callee().asCell();
244         if (callee != m_targetCallee)
245             return StackVisitor::Continue;
246
247         m_result = JSValue(visitor->createArguments());
248         return StackVisitor::Done;
249     }
250
251 private:
252     JSObject* m_targetCallee;
253     mutable JSValue m_result;
254 };
255
256 static JSValue retrieveArguments(ExecState* exec, JSFunction* functionObj)
257 {
258     RetrieveArgumentsFunctor functor(functionObj);
259     exec->iterate(functor);
260     return functor.result();
261 }
262
263 EncodedJSValue JSFunction::argumentsGetter(ExecState* exec, EncodedJSValue thisValue, PropertyName)
264 {
265     JSFunction* thisObj = jsCast<JSFunction*>(JSValue::decode(thisValue));
266     ASSERT(!thisObj->isHostFunction());
267
268     return JSValue::encode(retrieveArguments(exec, thisObj));
269 }
270
271 class RetrieveCallerFunctionFunctor {
272 public:
273     RetrieveCallerFunctionFunctor(JSFunction* functionObj)
274         : m_targetCallee(functionObj)
275         , m_hasFoundFrame(false)
276         , m_hasSkippedToCallerFrame(false)
277         , m_result(jsNull())
278     {
279     }
280
281     JSValue result() const { return m_result; }
282
283     StackVisitor::Status operator()(StackVisitor& visitor) const
284     {
285         if (!visitor->callee().isCell())
286             return StackVisitor::Continue;
287
288         JSCell* callee = visitor->callee().asCell();
289
290         if (callee && callee->inherits(*callee->vm(), JSBoundFunction::info()))
291             return StackVisitor::Continue;
292
293         if (!m_hasFoundFrame && (callee != m_targetCallee))
294             return StackVisitor::Continue;
295
296         m_hasFoundFrame = true;
297         if (!m_hasSkippedToCallerFrame) {
298             m_hasSkippedToCallerFrame = true;
299             return StackVisitor::Continue;
300         }
301
302         if (callee)
303             m_result = callee;
304         return StackVisitor::Done;
305     }
306
307 private:
308     JSObject* m_targetCallee;
309     mutable bool m_hasFoundFrame;
310     mutable bool m_hasSkippedToCallerFrame;
311     mutable JSValue m_result;
312 };
313
314 static JSValue retrieveCallerFunction(ExecState* exec, JSFunction* functionObj)
315 {
316     RetrieveCallerFunctionFunctor functor(functionObj);
317     exec->iterate(functor);
318     return functor.result();
319 }
320
321 EncodedJSValue JSFunction::callerGetter(ExecState* exec, EncodedJSValue thisValue, PropertyName)
322 {
323     VM& vm = exec->vm();
324     auto scope = DECLARE_THROW_SCOPE(vm);
325
326     JSFunction* thisObj = jsCast<JSFunction*>(JSValue::decode(thisValue));
327     ASSERT(!thisObj->isHostFunction());
328     JSValue caller = retrieveCallerFunction(exec, thisObj);
329
330     // See ES5.1 15.3.5.4 - Function.caller may not be used to retrieve a strict caller.
331     if (!caller.isObject() || !asObject(caller)->inherits(vm, JSFunction::info())) {
332         // It isn't a JSFunction, but if it is a JSCallee from a program or eval call or an internal constructor, return null.
333         if (jsDynamicCast<JSCallee*>(vm, caller) || jsDynamicCast<InternalFunction*>(vm, caller))
334             return JSValue::encode(jsNull());
335         return JSValue::encode(caller);
336     }
337     JSFunction* function = jsCast<JSFunction*>(caller);
338
339     // Firefox returns null for native code callers, so we match that behavior.
340     if (function->isHostOrBuiltinFunction())
341         return JSValue::encode(jsNull());
342     if (!function->jsExecutable()->isStrictMode())
343         return JSValue::encode(caller);
344     return JSValue::encode(throwTypeError(exec, scope, ASCIILiteral("Function.caller used to retrieve strict caller")));
345 }
346
347 bool JSFunction::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
348 {
349     VM& vm = exec->vm();
350     JSFunction* thisObject = jsCast<JSFunction*>(object);
351     if (thisObject->isHostOrBuiltinFunction()) {
352         thisObject->reifyLazyPropertyForHostOrBuiltinIfNeeded(vm, exec, propertyName);
353         return Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
354     }
355
356     if (propertyName == vm.propertyNames->prototype && thisObject->jsExecutable()->hasPrototypeProperty() && !thisObject->jsExecutable()->isClassConstructorFunction()) {
357         unsigned attributes;
358         PropertyOffset offset = thisObject->getDirectOffset(vm, propertyName, attributes);
359         if (!isValidOffset(offset)) {
360             JSObject* prototype = nullptr;
361             if (isGeneratorWrapperParseMode(thisObject->jsExecutable()->parseMode())) {
362                 // Unlike function instances, the object that is the value of the a GeneratorFunction's prototype
363                 // property does not have a constructor property whose value is the GeneratorFunction instance.
364                 // https://tc39.github.io/ecma262/#sec-generatorfunction-instances-prototype
365                 prototype = constructEmptyObject(exec, thisObject->globalObject(vm)->generatorPrototype());
366             } else if (thisObject->jsExecutable()->parseMode() == SourceParseMode::AsyncGeneratorWrapperFunctionMode)
367                 prototype = constructEmptyObject(exec, thisObject->globalObject(vm)->asyncGeneratorPrototype());
368             else {
369                 prototype = constructEmptyObject(exec);
370                 prototype->putDirect(vm, vm.propertyNames->constructor, thisObject, DontEnum);
371             }
372
373             thisObject->putDirect(vm, vm.propertyNames->prototype, prototype, DontDelete | DontEnum);
374             offset = thisObject->getDirectOffset(vm, vm.propertyNames->prototype, attributes);
375             ASSERT(isValidOffset(offset));
376         }
377
378         slot.setValue(thisObject, attributes, thisObject->getDirect(offset), offset);
379     }
380
381     if (propertyName == exec->propertyNames().arguments) {
382         if (!thisObject->jsExecutable()->hasCallerAndArgumentsProperties())
383             return Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
384         
385         slot.setCacheableCustom(thisObject, ReadOnly | DontEnum | DontDelete, argumentsGetter);
386         return true;
387     }
388
389     if (propertyName == exec->propertyNames().caller) {
390         if (!thisObject->jsExecutable()->hasCallerAndArgumentsProperties())
391             return Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
392
393         slot.setCacheableCustom(thisObject, ReadOnly | DontEnum | DontDelete, callerGetter);
394         return true;
395     }
396
397     thisObject->reifyLazyPropertyIfNeeded(vm, exec, propertyName);
398
399     return Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
400 }
401
402 void JSFunction::getOwnNonIndexPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
403 {
404     JSFunction* thisObject = jsCast<JSFunction*>(object);
405     VM& vm = exec->vm();
406     if (mode.includeDontEnumProperties()) {
407         if (!thisObject->isHostOrBuiltinFunction()) {
408             // Make sure prototype has been reified.
409             PropertySlot slot(thisObject, PropertySlot::InternalMethodType::VMInquiry);
410             thisObject->methodTable(vm)->getOwnPropertySlot(thisObject, exec, vm.propertyNames->prototype, slot);
411
412             if (thisObject->jsExecutable()->hasCallerAndArgumentsProperties()) {
413                 propertyNames.add(vm.propertyNames->arguments);
414                 propertyNames.add(vm.propertyNames->caller);
415             }
416             if (!thisObject->hasReifiedLength())
417                 propertyNames.add(vm.propertyNames->length);
418             if (!thisObject->hasReifiedName())
419                 propertyNames.add(vm.propertyNames->name);
420         } else {
421             if (thisObject->isBuiltinFunction() && !thisObject->hasReifiedLength())
422                 propertyNames.add(vm.propertyNames->length);
423             if (thisObject->inherits(vm, JSBoundFunction::info()) && !thisObject->hasReifiedName())
424                 propertyNames.add(vm.propertyNames->name);
425         }
426     }
427     Base::getOwnNonIndexPropertyNames(thisObject, exec, propertyNames, mode);
428 }
429
430 bool JSFunction::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
431 {
432     VM& vm = exec->vm();
433     auto scope = DECLARE_THROW_SCOPE(vm);
434
435     JSFunction* thisObject = jsCast<JSFunction*>(cell);
436
437     if (UNLIKELY(isThisValueAltered(slot, thisObject))) {
438         scope.release();
439         return ordinarySetSlow(exec, thisObject, propertyName, value, slot.thisValue(), slot.isStrictMode());
440     }
441
442     if (thisObject->isHostOrBuiltinFunction()) {
443         LazyPropertyType propType = thisObject->reifyLazyPropertyForHostOrBuiltinIfNeeded(vm, exec, propertyName);
444         if (propType == LazyPropertyType::IsLazyProperty)
445             slot.disableCaching();
446         scope.release();
447         return Base::put(thisObject, exec, propertyName, value, slot);
448     }
449
450     if (propertyName == vm.propertyNames->prototype) {
451         slot.disableCaching();
452         // Make sure prototype has been reified, such that it can only be overwritten
453         // following the rules set out in ECMA-262 8.12.9.
454         PropertySlot getSlot(thisObject, PropertySlot::InternalMethodType::VMInquiry);
455         thisObject->methodTable(vm)->getOwnPropertySlot(thisObject, exec, propertyName, getSlot);
456         if (thisObject->m_rareData)
457             thisObject->m_rareData->clear("Store to prototype property of a function");
458         scope.release();
459         return Base::put(thisObject, exec, propertyName, value, slot);
460     }
461
462     if (propertyName == vm.propertyNames->arguments || propertyName == vm.propertyNames->caller) {
463         slot.disableCaching();
464         if (!thisObject->jsExecutable()->hasCallerAndArgumentsProperties()) {
465             // This will trigger the property to be reified, if this is not already the case!
466             // FIXME: Investigate if the `hasProperty()` call is even needed, as in the `!hasCallerAndArgumentsProperties()` case,
467             // these properties are not lazy and should not need to be reified. (https://bugs.webkit.org/show_bug.cgi?id=163579)
468             bool okay = thisObject->hasProperty(exec, propertyName);
469             RETURN_IF_EXCEPTION(scope, false);
470             ASSERT_UNUSED(okay, okay);
471             scope.release();
472             return Base::put(thisObject, exec, propertyName, value, slot);
473         }
474         return typeError(exec, scope, slot.isStrictMode(), ASCIILiteral(ReadonlyPropertyWriteError));
475     }
476     LazyPropertyType propType = thisObject->reifyLazyPropertyIfNeeded(vm, exec, propertyName);
477     if (propType == LazyPropertyType::IsLazyProperty)
478         slot.disableCaching();
479     scope.release();
480     return Base::put(thisObject, exec, propertyName, value, slot);
481 }
482
483 bool JSFunction::deleteProperty(JSCell* cell, ExecState* exec, PropertyName propertyName)
484 {
485     VM& vm = exec->vm();
486     JSFunction* thisObject = jsCast<JSFunction*>(cell);
487     if (thisObject->isHostOrBuiltinFunction())
488         thisObject->reifyLazyPropertyForHostOrBuiltinIfNeeded(vm, exec, propertyName);
489     else if (vm.deletePropertyMode() != VM::DeletePropertyMode::IgnoreConfigurable) {
490         // For non-host functions, don't let these properties by deleted - except by DefineOwnProperty.
491         FunctionExecutable* executable = thisObject->jsExecutable();
492         
493         if (propertyName == exec->propertyNames().caller || propertyName == exec->propertyNames().arguments)
494             return !executable->hasCallerAndArgumentsProperties();
495
496         if (propertyName == exec->propertyNames().prototype && !executable->isArrowFunction())
497             return false;
498
499         thisObject->reifyLazyPropertyIfNeeded(vm, exec, propertyName);
500     }
501     
502     return Base::deleteProperty(thisObject, exec, propertyName);
503 }
504
505 bool JSFunction::defineOwnProperty(JSObject* object, ExecState* exec, PropertyName propertyName, const PropertyDescriptor& descriptor, bool throwException)
506 {
507     VM& vm = exec->vm();
508     auto scope = DECLARE_THROW_SCOPE(vm);
509
510     JSFunction* thisObject = jsCast<JSFunction*>(object);
511     if (thisObject->isHostOrBuiltinFunction()) {
512         thisObject->reifyLazyPropertyForHostOrBuiltinIfNeeded(vm, exec, propertyName);
513         scope.release();
514         return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
515     }
516
517     if (propertyName == vm.propertyNames->prototype) {
518         // Make sure prototype has been reified, such that it can only be overwritten
519         // following the rules set out in ECMA-262 8.12.9.
520         PropertySlot slot(thisObject, PropertySlot::InternalMethodType::VMInquiry);
521         thisObject->methodTable(vm)->getOwnPropertySlot(thisObject, exec, propertyName, slot);
522         if (thisObject->m_rareData)
523             thisObject->m_rareData->clear("Store to prototype property of a function");
524         scope.release();
525         return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
526     }
527
528     bool valueCheck;
529     if (propertyName == vm.propertyNames->arguments) {
530         if (!thisObject->jsExecutable()->hasCallerAndArgumentsProperties()) {
531             if (thisObject->jsExecutable()->isClass()) {
532                 thisObject->reifyLazyPropertyIfNeeded(vm, exec, propertyName);
533                 scope.release();
534                 return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
535             }
536             PropertySlot slot(thisObject, PropertySlot::InternalMethodType::VMInquiry);
537             if (!Base::getOwnPropertySlot(thisObject, exec, propertyName, slot))
538                 thisObject->putDirectAccessor(exec, propertyName, thisObject->globalObject(vm)->throwTypeErrorArgumentsCalleeAndCallerGetterSetter(), DontDelete | DontEnum | Accessor);
539             scope.release();
540             return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
541         }
542         valueCheck = !descriptor.value() || sameValue(exec, descriptor.value(), retrieveArguments(exec, thisObject));
543     } else if (propertyName == vm.propertyNames->caller) {
544         if (!thisObject->jsExecutable()->hasCallerAndArgumentsProperties()) {
545             if (thisObject->jsExecutable()->isClass()) {
546                 thisObject->reifyLazyPropertyIfNeeded(vm, exec, propertyName);
547                 scope.release();
548                 return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
549             }
550             PropertySlot slot(thisObject, PropertySlot::InternalMethodType::VMInquiry);
551             if (!Base::getOwnPropertySlot(thisObject, exec, propertyName, slot))
552                 thisObject->putDirectAccessor(exec, propertyName, thisObject->globalObject(vm)->throwTypeErrorArgumentsCalleeAndCallerGetterSetter(), DontDelete | DontEnum | Accessor);
553             scope.release();
554             return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
555         }
556         valueCheck = !descriptor.value() || sameValue(exec, descriptor.value(), retrieveCallerFunction(exec, thisObject));
557     } else {
558         thisObject->reifyLazyPropertyIfNeeded(vm, exec, propertyName);
559         scope.release();
560         return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
561     }
562      
563     if (descriptor.configurablePresent() && descriptor.configurable())
564         return typeError(exec, scope, throwException, ASCIILiteral(UnconfigurablePropertyChangeConfigurabilityError));
565     if (descriptor.enumerablePresent() && descriptor.enumerable())
566         return typeError(exec, scope, throwException, ASCIILiteral(UnconfigurablePropertyChangeEnumerabilityError));
567     if (descriptor.isAccessorDescriptor())
568         return typeError(exec, scope, throwException, ASCIILiteral(UnconfigurablePropertyChangeAccessMechanismError));
569     if (descriptor.writablePresent() && descriptor.writable())
570         return typeError(exec, scope, throwException, ASCIILiteral(UnconfigurablePropertyChangeWritabilityError));
571     if (!valueCheck)
572         return typeError(exec, scope, throwException, ASCIILiteral(ReadonlyPropertyChangeError));
573     return true;
574 }
575
576 // ECMA 13.2.2 [[Construct]]
577 ConstructType JSFunction::getConstructData(JSCell* cell, ConstructData& constructData)
578 {
579     JSFunction* thisObject = jsCast<JSFunction*>(cell);
580
581     if (thisObject->isHostFunction()) {
582         if (thisObject->nativeConstructor() == callHostFunctionAsConstructor)
583             return ConstructType::None;
584         constructData.native.function = thisObject->nativeConstructor();
585         return ConstructType::Host;
586     }
587
588     FunctionExecutable* functionExecutable = thisObject->jsExecutable();
589     if (functionExecutable->constructAbility() == ConstructAbility::CannotConstruct)
590         return ConstructType::None;
591
592     constructData.js.functionExecutable = functionExecutable;
593     constructData.js.scope = thisObject->scope();
594     return ConstructType::JS;
595 }
596
597 String getCalculatedDisplayName(VM& vm, JSObject* object)
598 {
599     if (JSFunction* function = jsDynamicCast<JSFunction*>(vm, object))
600         return function->calculatedDisplayName(vm);
601     if (InternalFunction* function = jsDynamicCast<InternalFunction*>(vm, object))
602         return function->calculatedDisplayName(vm);
603     return emptyString();
604 }
605
606 void JSFunction::setFunctionName(ExecState* exec, JSValue value)
607 {
608     VM& vm = exec->vm();
609     auto scope = DECLARE_THROW_SCOPE(vm);
610
611     // The "name" property may have been already been defined as part of a property list in an
612     // object literal (and therefore reified).
613     if (hasReifiedName())
614         return;
615
616     ASSERT(!isHostFunction());
617     ASSERT(jsExecutable()->ecmaName().isNull());
618     String name;
619     if (value.isSymbol()) {
620         SymbolImpl& uid = asSymbol(value)->privateName().uid();
621         if (uid.isNullSymbol())
622             name = emptyString();
623         else
624             name = makeString('[', String(&uid), ']');
625     } else {
626         JSString* jsStr = value.toString(exec);
627         RETURN_IF_EXCEPTION(scope, void());
628         name = jsStr->value(exec);
629         RETURN_IF_EXCEPTION(scope, void());
630     }
631     reifyName(vm, exec, name);
632 }
633
634 void JSFunction::reifyLength(VM& vm)
635 {
636     FunctionRareData* rareData = this->rareData(vm);
637
638     ASSERT(!hasReifiedLength());
639     ASSERT(!isHostFunction());
640     JSValue initialValue = jsNumber(jsExecutable()->parameterCount());
641     unsigned initialAttributes = DontEnum | ReadOnly;
642     const Identifier& identifier = vm.propertyNames->length;
643     putDirect(vm, identifier, initialValue, initialAttributes);
644
645     rareData->setHasReifiedLength();
646 }
647
648 void JSFunction::reifyName(VM& vm, ExecState* exec)
649 {
650     const Identifier& ecmaName = jsExecutable()->ecmaName();
651     String name;
652     // https://tc39.github.io/ecma262/#sec-exports-runtime-semantics-evaluation
653     // When the ident is "*default*", we need to set "default" for the ecma name.
654     // This "*default*" name is never shown to users.
655     if (ecmaName == exec->propertyNames().builtinNames().starDefaultPrivateName())
656         name = exec->propertyNames().defaultKeyword.string();
657     else
658         name = ecmaName.string();
659     reifyName(vm, exec, name);
660 }
661
662 void JSFunction::reifyName(VM& vm, ExecState* exec, String name)
663 {
664     FunctionRareData* rareData = this->rareData(vm);
665
666     ASSERT(!hasReifiedName());
667     ASSERT(!isHostFunction());
668     unsigned initialAttributes = DontEnum | ReadOnly;
669     const Identifier& propID = vm.propertyNames->name;
670
671     if (exec->lexicalGlobalObject()->needsSiteSpecificQuirks()) {
672         auto illegalCharMatcher = [] (UChar ch) -> bool {
673             return ch == ' ' || ch == '|';
674         };
675         if (name.find(illegalCharMatcher) != notFound)
676             name = String();
677     }
678     
679     if (jsExecutable()->isGetter())
680         name = makeString("get ", name);
681     else if (jsExecutable()->isSetter())
682         name = makeString("set ", name);
683
684     putDirect(vm, propID, jsString(exec, name), initialAttributes);
685     rareData->setHasReifiedName();
686 }
687
688 JSFunction::LazyPropertyType JSFunction::reifyLazyPropertyIfNeeded(VM& vm, ExecState* exec, PropertyName propertyName)
689 {
690     if (reifyLazyLengthIfNeeded(vm, exec, propertyName) == LazyPropertyType::IsLazyProperty)
691         return LazyPropertyType::IsLazyProperty;
692     if (propertyName == vm.propertyNames->name) {
693         if (!hasReifiedName())
694             reifyName(vm, exec);
695         return LazyPropertyType::IsLazyProperty;
696     }
697     return LazyPropertyType::NotLazyProperty;
698 }
699
700 JSFunction::LazyPropertyType JSFunction::reifyLazyPropertyForHostOrBuiltinIfNeeded(VM& vm, ExecState* exec, PropertyName propertyName)
701 {
702     ASSERT(isHostOrBuiltinFunction());
703     if (isBuiltinFunction()) {
704         if (reifyLazyLengthIfNeeded(vm, exec, propertyName) == LazyPropertyType::IsLazyProperty)
705             return LazyPropertyType::IsLazyProperty;
706     }
707     return reifyLazyBoundNameIfNeeded(vm, exec, propertyName);
708 }
709
710 JSFunction::LazyPropertyType JSFunction::reifyLazyLengthIfNeeded(VM& vm, ExecState*, PropertyName propertyName)
711 {
712     if (propertyName == vm.propertyNames->length) {
713         if (!hasReifiedLength())
714             reifyLength(vm);
715         return LazyPropertyType::IsLazyProperty;
716     }
717     return LazyPropertyType::NotLazyProperty;
718 }
719
720 JSFunction::LazyPropertyType JSFunction::reifyLazyBoundNameIfNeeded(VM& vm, ExecState* exec, PropertyName propertyName)
721 {
722     const Identifier& nameIdent = vm.propertyNames->name;
723     if (propertyName != nameIdent)
724         return LazyPropertyType::NotLazyProperty;
725
726     if (hasReifiedName())
727         return LazyPropertyType::IsLazyProperty;
728
729     if (this->inherits(vm, JSBoundFunction::info())) {
730         FunctionRareData* rareData = this->rareData(vm);
731         String name = makeString("bound ", static_cast<NativeExecutable*>(m_executable.get())->name());
732         unsigned initialAttributes = DontEnum | ReadOnly;
733         putDirect(vm, nameIdent, jsString(exec, name), initialAttributes);
734         rareData->setHasReifiedName();
735     }
736     return LazyPropertyType::IsLazyProperty;
737 }
738
739 } // namespace JSC