7c90ab627eab2558996966f55a4be2ef40ea73b7
[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 "ClonedArguments.h"
30 #include "CodeBlock.h"
31 #include "CommonIdentifiers.h"
32 #include "CallFrame.h"
33 #include "ExceptionHelpers.h"
34 #include "FunctionPrototype.h"
35 #include "GeneratorPrototype.h"
36 #include "GetterSetter.h"
37 #include "JSArray.h"
38 #include "JSBoundFunction.h"
39 #include "JSCInlines.h"
40 #include "JSFunctionInlines.h"
41 #include "JSGlobalObject.h"
42 #include "Interpreter.h"
43 #include "ObjectConstructor.h"
44 #include "ObjectPrototype.h"
45 #include "Parser.h"
46 #include "PropertyNameArray.h"
47 #include "StackVisitor.h"
48
49 namespace JSC {
50
51 EncodedJSValue JSC_HOST_CALL callHostFunctionAsConstructor(ExecState* exec)
52 {
53     VM& vm = exec->vm();
54     auto scope = DECLARE_THROW_SCOPE(vm);
55     return throwVMError(exec, scope, createNotAConstructorError(exec, exec->jsCallee()));
56 }
57
58 const ClassInfo JSFunction::s_info = { "Function", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSFunction) };
59
60 bool JSFunction::isHostFunctionNonInline() const
61 {
62     return isHostFunction();
63 }
64
65 JSFunction* JSFunction::create(VM& vm, FunctionExecutable* executable, JSScope* scope)
66 {
67     return create(vm, executable, scope, scope->globalObject(vm)->functionStructure());
68 }
69
70 JSFunction* JSFunction::create(VM& vm, FunctionExecutable* executable, JSScope* scope, Structure* structure)
71 {
72     JSFunction* result = createImpl(vm, executable, scope, structure);
73     executable->singletonFunction()->notifyWrite(vm, result, "Allocating a function");
74     return result;
75 }
76
77 JSFunction* JSFunction::create(VM& vm, JSGlobalObject* globalObject, int length, const String& name, NativeFunction nativeFunction, Intrinsic intrinsic, NativeFunction nativeConstructor, const DOMJIT::Signature* signature)
78 {
79     NativeExecutable* executable = vm.getHostFunction(nativeFunction, intrinsic, nativeConstructor, signature, name);
80     JSFunction* function = new (NotNull, allocateCell<JSFunction>(vm.heap)) JSFunction(vm, globalObject, globalObject->functionStructure());
81     // Can't do this during initialization because getHostFunction might do a GC allocation.
82     function->finishCreation(vm, executable, length, name);
83     return function;
84 }
85
86 JSFunction::JSFunction(VM& vm, JSGlobalObject* globalObject, Structure* structure)
87     : Base(vm, globalObject, structure)
88     , m_executable()
89 {
90 }
91
92 void JSFunction::finishCreation(VM& vm, NativeExecutable* executable, int length, const String& name)
93 {
94     Base::finishCreation(vm);
95     ASSERT(inherits(vm, info()));
96     m_executable.set(vm, this, executable);
97     // Some NativeExecutable functions, like JSBoundFunction, decide to lazily allocate their name string.
98     if (!name.isNull())
99         putDirect(vm, vm.propertyNames->name, jsString(&vm, name), ReadOnly | DontEnum);
100     putDirect(vm, vm.propertyNames->length, jsNumber(length), ReadOnly | DontEnum);
101 }
102
103 JSFunction* JSFunction::createBuiltinFunction(VM& vm, FunctionExecutable* executable, JSGlobalObject* globalObject)
104 {
105     JSFunction* function = create(vm, executable, globalObject);
106     function->putDirect(vm, vm.propertyNames->name, jsString(&vm, executable->name().string()), ReadOnly | DontEnum);
107     function->putDirect(vm, vm.propertyNames->length, jsNumber(executable->parameterCount()), ReadOnly | DontEnum);
108     return function;
109 }
110
111 JSFunction* JSFunction::createBuiltinFunction(VM& vm, FunctionExecutable* executable, JSGlobalObject* globalObject, const String& name)
112 {
113     JSFunction* function = create(vm, executable, globalObject);
114     function->putDirect(vm, vm.propertyNames->name, jsString(&vm, name), ReadOnly | DontEnum);
115     function->putDirect(vm, vm.propertyNames->length, jsNumber(executable->parameterCount()), 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->reifyBoundNameIfNeeded(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 {
367                 prototype = constructEmptyObject(exec);
368                 prototype->putDirect(vm, vm.propertyNames->constructor, thisObject, DontEnum);
369             }
370
371             thisObject->putDirect(vm, vm.propertyNames->prototype, prototype, DontDelete | DontEnum);
372             offset = thisObject->getDirectOffset(vm, vm.propertyNames->prototype, attributes);
373             ASSERT(isValidOffset(offset));
374         }
375
376         slot.setValue(thisObject, attributes, thisObject->getDirect(offset), offset);
377     }
378
379     if (propertyName == exec->propertyNames().arguments) {
380         if (!thisObject->jsExecutable()->hasCallerAndArgumentsProperties())
381             return Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
382         
383         slot.setCacheableCustom(thisObject, ReadOnly | DontEnum | DontDelete, argumentsGetter);
384         return true;
385     }
386
387     if (propertyName == exec->propertyNames().caller) {
388         if (!thisObject->jsExecutable()->hasCallerAndArgumentsProperties())
389             return Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
390
391         slot.setCacheableCustom(thisObject, ReadOnly | DontEnum | DontDelete, callerGetter);
392         return true;
393     }
394
395     thisObject->reifyLazyPropertyIfNeeded(vm, exec, propertyName);
396
397     return Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
398 }
399
400 void JSFunction::getOwnNonIndexPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
401 {
402     JSFunction* thisObject = jsCast<JSFunction*>(object);
403     VM& vm = exec->vm();
404     if (!thisObject->isHostOrBuiltinFunction() && mode.includeDontEnumProperties()) {
405         // Make sure prototype has been reified.
406         PropertySlot slot(thisObject, PropertySlot::InternalMethodType::VMInquiry);
407         thisObject->methodTable(vm)->getOwnPropertySlot(thisObject, exec, vm.propertyNames->prototype, slot);
408
409         if (thisObject->jsExecutable()->hasCallerAndArgumentsProperties()) {
410             propertyNames.add(vm.propertyNames->arguments);
411             propertyNames.add(vm.propertyNames->caller);
412         }
413         if (!thisObject->hasReifiedLength())
414             propertyNames.add(vm.propertyNames->length);
415         if (!thisObject->hasReifiedName())
416             propertyNames.add(vm.propertyNames->name);
417     } else if (thisObject->isHostOrBuiltinFunction() && mode.includeDontEnumProperties() && thisObject->inherits(vm, JSBoundFunction::info()) && !thisObject->hasReifiedName())
418         propertyNames.add(exec->vm().propertyNames->name);
419     Base::getOwnNonIndexPropertyNames(thisObject, exec, propertyNames, mode);
420 }
421
422 bool JSFunction::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
423 {
424     VM& vm = exec->vm();
425     auto scope = DECLARE_THROW_SCOPE(vm);
426
427     JSFunction* thisObject = jsCast<JSFunction*>(cell);
428
429     if (UNLIKELY(isThisValueAltered(slot, thisObject))) {
430         scope.release();
431         return ordinarySetSlow(exec, thisObject, propertyName, value, slot.thisValue(), slot.isStrictMode());
432     }
433
434     if (thisObject->isHostOrBuiltinFunction()) {
435         LazyPropertyType propType = thisObject->reifyBoundNameIfNeeded(vm, exec, propertyName);
436         if (propType == LazyPropertyType::IsLazyProperty)
437             slot.disableCaching();
438         scope.release();
439         return Base::put(thisObject, exec, propertyName, value, slot);
440     }
441
442     if (propertyName == vm.propertyNames->prototype) {
443         slot.disableCaching();
444         // Make sure prototype has been reified, such that it can only be overwritten
445         // following the rules set out in ECMA-262 8.12.9.
446         PropertySlot getSlot(thisObject, PropertySlot::InternalMethodType::VMInquiry);
447         thisObject->methodTable(vm)->getOwnPropertySlot(thisObject, exec, propertyName, getSlot);
448         if (thisObject->m_rareData)
449             thisObject->m_rareData->clear("Store to prototype property of a function");
450         scope.release();
451         return Base::put(thisObject, exec, propertyName, value, slot);
452     }
453
454     if (propertyName == vm.propertyNames->arguments || propertyName == vm.propertyNames->caller) {
455         slot.disableCaching();
456         if (!thisObject->jsExecutable()->hasCallerAndArgumentsProperties()) {
457             // This will trigger the property to be reified, if this is not already the case!
458             // FIXME: Investigate if the `hasProperty()` call is even needed, as in the `!hasCallerAndArgumentsProperties()` case,
459             // these properties are not lazy and should not need to be reified. (https://bugs.webkit.org/show_bug.cgi?id=163579)
460             bool okay = thisObject->hasProperty(exec, propertyName);
461             RETURN_IF_EXCEPTION(scope, false);
462             ASSERT_UNUSED(okay, okay);
463             scope.release();
464             return Base::put(thisObject, exec, propertyName, value, slot);
465         }
466         return typeError(exec, scope, slot.isStrictMode(), ASCIILiteral(ReadonlyPropertyWriteError));
467     }
468     LazyPropertyType propType = thisObject->reifyLazyPropertyIfNeeded(vm, exec, propertyName);
469     if (propType == LazyPropertyType::IsLazyProperty)
470         slot.disableCaching();
471     scope.release();
472     return Base::put(thisObject, exec, propertyName, value, slot);
473 }
474
475 bool JSFunction::deleteProperty(JSCell* cell, ExecState* exec, PropertyName propertyName)
476 {
477     JSFunction* thisObject = jsCast<JSFunction*>(cell);
478     if (thisObject->isHostOrBuiltinFunction())
479         thisObject->reifyBoundNameIfNeeded(exec->vm(), exec, propertyName);
480     else if (exec->vm().deletePropertyMode() != VM::DeletePropertyMode::IgnoreConfigurable) {
481         // For non-host functions, don't let these properties by deleted - except by DefineOwnProperty.
482         VM& vm = exec->vm();
483         FunctionExecutable* executable = thisObject->jsExecutable();
484         
485         if (propertyName == exec->propertyNames().caller || propertyName == exec->propertyNames().arguments)
486             return !executable->hasCallerAndArgumentsProperties();
487
488         if (propertyName == exec->propertyNames().prototype && !executable->isArrowFunction())
489             return false;
490
491         thisObject->reifyLazyPropertyIfNeeded(vm, exec, propertyName);
492     }
493     
494     return Base::deleteProperty(thisObject, exec, propertyName);
495 }
496
497 bool JSFunction::defineOwnProperty(JSObject* object, ExecState* exec, PropertyName propertyName, const PropertyDescriptor& descriptor, bool throwException)
498 {
499     VM& vm = exec->vm();
500     auto scope = DECLARE_THROW_SCOPE(vm);
501
502     JSFunction* thisObject = jsCast<JSFunction*>(object);
503     if (thisObject->isHostOrBuiltinFunction()) {
504         thisObject->reifyBoundNameIfNeeded(vm, exec, propertyName);
505         scope.release();
506         return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
507     }
508
509     if (propertyName == vm.propertyNames->prototype) {
510         // Make sure prototype has been reified, such that it can only be overwritten
511         // following the rules set out in ECMA-262 8.12.9.
512         PropertySlot slot(thisObject, PropertySlot::InternalMethodType::VMInquiry);
513         thisObject->methodTable(vm)->getOwnPropertySlot(thisObject, exec, propertyName, slot);
514         if (thisObject->m_rareData)
515             thisObject->m_rareData->clear("Store to prototype property of a function");
516         scope.release();
517         return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
518     }
519
520     bool valueCheck;
521     if (propertyName == vm.propertyNames->arguments) {
522         if (!thisObject->jsExecutable()->hasCallerAndArgumentsProperties()) {
523             if (thisObject->jsExecutable()->isClass()) {
524                 thisObject->reifyLazyPropertyIfNeeded(vm, exec, propertyName);
525                 scope.release();
526                 return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
527             }
528             PropertySlot slot(thisObject, PropertySlot::InternalMethodType::VMInquiry);
529             if (!Base::getOwnPropertySlot(thisObject, exec, propertyName, slot))
530                 thisObject->putDirectAccessor(exec, propertyName, thisObject->globalObject(vm)->throwTypeErrorArgumentsCalleeAndCallerGetterSetter(), DontDelete | DontEnum | Accessor);
531             scope.release();
532             return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
533         }
534         valueCheck = !descriptor.value() || sameValue(exec, descriptor.value(), retrieveArguments(exec, thisObject));
535     } else if (propertyName == vm.propertyNames->caller) {
536         if (!thisObject->jsExecutable()->hasCallerAndArgumentsProperties()) {
537             if (thisObject->jsExecutable()->isClass()) {
538                 thisObject->reifyLazyPropertyIfNeeded(vm, exec, propertyName);
539                 scope.release();
540                 return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
541             }
542             PropertySlot slot(thisObject, PropertySlot::InternalMethodType::VMInquiry);
543             if (!Base::getOwnPropertySlot(thisObject, exec, propertyName, slot))
544                 thisObject->putDirectAccessor(exec, propertyName, thisObject->globalObject(vm)->throwTypeErrorArgumentsCalleeAndCallerGetterSetter(), DontDelete | DontEnum | Accessor);
545             scope.release();
546             return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
547         }
548         valueCheck = !descriptor.value() || sameValue(exec, descriptor.value(), retrieveCallerFunction(exec, thisObject));
549     } else {
550         thisObject->reifyLazyPropertyIfNeeded(vm, exec, propertyName);
551         scope.release();
552         return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
553     }
554      
555     if (descriptor.configurablePresent() && descriptor.configurable())
556         return typeError(exec, scope, throwException, ASCIILiteral(UnconfigurablePropertyChangeConfigurabilityError));
557     if (descriptor.enumerablePresent() && descriptor.enumerable())
558         return typeError(exec, scope, throwException, ASCIILiteral(UnconfigurablePropertyChangeEnumerabilityError));
559     if (descriptor.isAccessorDescriptor())
560         return typeError(exec, scope, throwException, ASCIILiteral(UnconfigurablePropertyChangeAccessMechanismError));
561     if (descriptor.writablePresent() && descriptor.writable())
562         return typeError(exec, scope, throwException, ASCIILiteral(UnconfigurablePropertyChangeWritabilityError));
563     if (!valueCheck)
564         return typeError(exec, scope, throwException, ASCIILiteral(ReadonlyPropertyChangeError));
565     return true;
566 }
567
568 // ECMA 13.2.2 [[Construct]]
569 ConstructType JSFunction::getConstructData(JSCell* cell, ConstructData& constructData)
570 {
571     JSFunction* thisObject = jsCast<JSFunction*>(cell);
572
573     if (thisObject->isHostFunction()) {
574         if (thisObject->nativeConstructor() == callHostFunctionAsConstructor)
575             return ConstructType::None;
576         constructData.native.function = thisObject->nativeConstructor();
577         return ConstructType::Host;
578     }
579
580     FunctionExecutable* functionExecutable = thisObject->jsExecutable();
581     if (functionExecutable->constructAbility() == ConstructAbility::CannotConstruct)
582         return ConstructType::None;
583
584     constructData.js.functionExecutable = functionExecutable;
585     constructData.js.scope = thisObject->scope();
586     return ConstructType::JS;
587 }
588
589 String getCalculatedDisplayName(VM& vm, JSObject* object)
590 {
591     if (JSFunction* function = jsDynamicCast<JSFunction*>(vm, object))
592         return function->calculatedDisplayName(vm);
593     if (InternalFunction* function = jsDynamicCast<InternalFunction*>(vm, object))
594         return function->calculatedDisplayName(vm);
595     return emptyString();
596 }
597
598 void JSFunction::setFunctionName(ExecState* exec, JSValue value)
599 {
600     VM& vm = exec->vm();
601     auto scope = DECLARE_THROW_SCOPE(vm);
602
603     // The "name" property may have been already been defined as part of a property list in an
604     // object literal (and therefore reified).
605     if (hasReifiedName())
606         return;
607
608     ASSERT(!isHostFunction());
609     ASSERT(jsExecutable()->ecmaName().isNull());
610     String name;
611     if (value.isSymbol()) {
612         SymbolImpl& uid = asSymbol(value)->privateName().uid();
613         if (uid.isNullSymbol())
614             name = emptyString();
615         else
616             name = makeString('[', String(&uid), ']');
617     } else {
618         JSString* jsStr = value.toString(exec);
619         RETURN_IF_EXCEPTION(scope, void());
620         name = jsStr->value(exec);
621         RETURN_IF_EXCEPTION(scope, void());
622     }
623     reifyName(vm, exec, name);
624 }
625
626 void JSFunction::reifyLength(VM& vm)
627 {
628     FunctionRareData* rareData = this->rareData(vm);
629
630     ASSERT(!hasReifiedLength());
631     ASSERT(!isHostFunction());
632     JSValue initialValue = jsNumber(jsExecutable()->parameterCount());
633     unsigned initialAttributes = DontEnum | ReadOnly;
634     const Identifier& identifier = vm.propertyNames->length;
635     putDirect(vm, identifier, initialValue, initialAttributes);
636
637     rareData->setHasReifiedLength();
638 }
639
640 void JSFunction::reifyName(VM& vm, ExecState* exec)
641 {
642     const Identifier& ecmaName = jsExecutable()->ecmaName();
643     String name;
644     // https://tc39.github.io/ecma262/#sec-exports-runtime-semantics-evaluation
645     // When the ident is "*default*", we need to set "default" for the ecma name.
646     // This "*default*" name is never shown to users.
647     if (ecmaName == exec->propertyNames().builtinNames().starDefaultPrivateName())
648         name = exec->propertyNames().defaultKeyword.string();
649     else
650         name = ecmaName.string();
651     reifyName(vm, exec, name);
652 }
653
654 void JSFunction::reifyName(VM& vm, ExecState* exec, String name)
655 {
656     FunctionRareData* rareData = this->rareData(vm);
657
658     ASSERT(!hasReifiedName());
659     ASSERT(!isHostFunction());
660     unsigned initialAttributes = DontEnum | ReadOnly;
661     const Identifier& propID = vm.propertyNames->name;
662
663     if (exec->lexicalGlobalObject()->needsSiteSpecificQuirks()) {
664         auto illegalCharMatcher = [] (UChar ch) -> bool {
665             return ch == ' ' || ch == '|';
666         };
667         if (name.find(illegalCharMatcher) != notFound)
668             name = String();
669     }
670     
671     if (jsExecutable()->isGetter())
672         name = makeString("get ", name);
673     else if (jsExecutable()->isSetter())
674         name = makeString("set ", name);
675
676     putDirect(vm, propID, jsString(exec, name), initialAttributes);
677     rareData->setHasReifiedName();
678 }
679
680 JSFunction::LazyPropertyType JSFunction::reifyLazyPropertyIfNeeded(VM& vm, ExecState* exec, PropertyName propertyName)
681 {
682     if (propertyName == vm.propertyNames->length) {
683         if (!hasReifiedLength())
684             reifyLength(vm);
685         return LazyPropertyType::IsLazyProperty;
686     } else if (propertyName == vm.propertyNames->name) {
687         if (!hasReifiedName())
688             reifyName(vm, exec);
689         return LazyPropertyType::IsLazyProperty;
690     }
691     return LazyPropertyType::NotLazyProperty;
692 }
693
694 JSFunction::LazyPropertyType JSFunction::reifyBoundNameIfNeeded(VM& vm, ExecState* exec, PropertyName propertyName)
695 {
696     const Identifier& nameIdent = vm.propertyNames->name;
697     if (propertyName != nameIdent)
698         return LazyPropertyType::NotLazyProperty;
699
700     if (hasReifiedName())
701         return LazyPropertyType::IsLazyProperty;
702
703     if (this->inherits(vm, JSBoundFunction::info())) {
704         FunctionRareData* rareData = this->rareData(vm);
705         String name = makeString("bound ", static_cast<NativeExecutable*>(m_executable.get())->name());
706         unsigned initialAttributes = DontEnum | ReadOnly;
707         putDirect(vm, nameIdent, jsString(exec, name), initialAttributes);
708         rareData->setHasReifiedName();
709     }
710     return LazyPropertyType::IsLazyProperty;
711 }
712
713 } // namespace JSC