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