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