0c00f63b567cdf556062cfc9e3d52bfaf6a8d5be
[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 == exec->propertyNames().arguments || propertyName == exec->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         if (slot.isStrictMode())
458             throwTypeError(exec, scope, ReadonlyPropertyWriteError);
459         return false;
460     }
461     thisObject->reifyLazyPropertyIfNeeded(vm, exec, propertyName);
462     scope.release();
463     return Base::put(thisObject, exec, propertyName, value, slot);
464 }
465
466 bool JSFunction::deleteProperty(JSCell* cell, ExecState* exec, PropertyName propertyName)
467 {
468     JSFunction* thisObject = jsCast<JSFunction*>(cell);
469     if (thisObject->isHostOrBuiltinFunction())
470         thisObject->reifyBoundNameIfNeeded(exec->vm(), exec, propertyName);
471     else if (exec->vm().deletePropertyMode() != VM::DeletePropertyMode::IgnoreConfigurable) {
472         // For non-host functions, don't let these properties by deleted - except by DefineOwnProperty.
473         VM& vm = exec->vm();
474         FunctionExecutable* executable = thisObject->jsExecutable();
475         bool isES6OrStrictMode = executable->isStrictMode() || executable->isES6Function();
476         if ((propertyName == exec->propertyNames().arguments && !isES6OrStrictMode)
477             || (propertyName == exec->propertyNames().prototype && !executable->isArrowFunction())
478             || (propertyName == exec->propertyNames().caller && !isES6OrStrictMode))
479             return false;
480         
481         if ((propertyName == exec->propertyNames().arguments && isES6OrStrictMode)
482             || (propertyName == exec->propertyNames().caller && isES6OrStrictMode))
483             return true;
484
485         thisObject->reifyLazyPropertyIfNeeded(vm, exec, propertyName);
486     }
487     
488     return Base::deleteProperty(thisObject, exec, propertyName);
489 }
490
491 bool JSFunction::defineOwnProperty(JSObject* object, ExecState* exec, PropertyName propertyName, const PropertyDescriptor& descriptor, bool throwException)
492 {
493     VM& vm = exec->vm();
494     auto scope = DECLARE_THROW_SCOPE(vm);
495
496     JSFunction* thisObject = jsCast<JSFunction*>(object);
497     if (thisObject->isHostOrBuiltinFunction()) {
498         thisObject->reifyBoundNameIfNeeded(vm, exec, propertyName);
499         return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
500     }
501
502     if (propertyName == vm.propertyNames->prototype) {
503         // Make sure prototype has been reified, such that it can only be overwritten
504         // following the rules set out in ECMA-262 8.12.9.
505         PropertySlot slot(thisObject, PropertySlot::InternalMethodType::VMInquiry);
506         thisObject->methodTable(vm)->getOwnPropertySlot(thisObject, exec, propertyName, slot);
507         if (thisObject->m_rareData)
508             thisObject->m_rareData->clear("Store to prototype property of a function");
509         return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
510     }
511
512     bool valueCheck;
513     if (propertyName == exec->propertyNames().arguments) {
514         if (thisObject->jsExecutable()->isClass()) {
515             thisObject->reifyLazyPropertyIfNeeded(vm, exec, propertyName);
516             return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
517         }
518         if (thisObject->jsExecutable()->isStrictMode() || thisObject->jsExecutable()->isES6Function()) {
519             PropertySlot slot(thisObject, PropertySlot::InternalMethodType::VMInquiry);
520             if (!Base::getOwnPropertySlot(thisObject, exec, propertyName, slot))
521                 thisObject->putDirectAccessor(exec, propertyName, thisObject->globalObject(vm)->throwTypeErrorArgumentsCalleeAndCallerGetterSetter(), DontDelete | DontEnum | Accessor);
522             return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
523         }
524         valueCheck = !descriptor.value() || sameValue(exec, descriptor.value(), retrieveArguments(exec, thisObject));
525     } else if (propertyName == exec->propertyNames().caller) {
526         if (thisObject->jsExecutable()->isClass()) {
527             thisObject->reifyLazyPropertyIfNeeded(vm, exec, propertyName);
528             return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
529         }
530         if (thisObject->jsExecutable()->isStrictMode() || thisObject->jsExecutable()->isES6Function()) {
531             PropertySlot slot(thisObject, PropertySlot::InternalMethodType::VMInquiry);
532             if (!Base::getOwnPropertySlot(thisObject, exec, propertyName, slot))
533                 thisObject->putDirectAccessor(exec, propertyName, thisObject->globalObject(vm)->throwTypeErrorArgumentsCalleeAndCallerGetterSetter(), DontDelete | DontEnum | Accessor);
534             return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
535         }
536         valueCheck = !descriptor.value() || sameValue(exec, descriptor.value(), retrieveCallerFunction(exec, thisObject));
537     } else {
538         thisObject->reifyLazyPropertyIfNeeded(vm, exec, propertyName);
539         return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
540     }
541      
542     if (descriptor.configurablePresent() && descriptor.configurable()) {
543         if (throwException)
544             throwTypeError(exec, scope, ASCIILiteral("Attempting to change configurable attribute of unconfigurable property."));
545         return false;
546     }
547     if (descriptor.enumerablePresent() && descriptor.enumerable()) {
548         if (throwException)
549             throwTypeError(exec, scope, ASCIILiteral("Attempting to change enumerable attribute of unconfigurable property."));
550         return false;
551     }
552     if (descriptor.isAccessorDescriptor()) {
553         if (throwException)
554             throwTypeError(exec, scope, ASCIILiteral(UnconfigurablePropertyChangeAccessMechanismError));
555         return false;
556     }
557     if (descriptor.writablePresent() && descriptor.writable()) {
558         if (throwException)
559             throwTypeError(exec, scope, ASCIILiteral("Attempting to change writable attribute of unconfigurable property."));
560         return false;
561     }
562     if (!valueCheck) {
563         if (throwException)
564             throwTypeError(exec, scope, ASCIILiteral("Attempting to change value of a readonly property."));
565         return false;
566     }
567     return true;
568 }
569
570 // ECMA 13.2.2 [[Construct]]
571 ConstructType JSFunction::getConstructData(JSCell* cell, ConstructData& constructData)
572 {
573     JSFunction* thisObject = jsCast<JSFunction*>(cell);
574
575     if (thisObject->isHostFunction()) {
576         if (thisObject->nativeConstructor() == callHostFunctionAsConstructor)
577             return ConstructType::None;
578         constructData.native.function = thisObject->nativeConstructor();
579         return ConstructType::Host;
580     }
581
582     FunctionExecutable* functionExecutable = thisObject->jsExecutable();
583     if (functionExecutable->constructAbility() == ConstructAbility::CannotConstruct)
584         return ConstructType::None;
585
586     constructData.js.functionExecutable = functionExecutable;
587     constructData.js.scope = thisObject->scope();
588     return ConstructType::JS;
589 }
590
591 String getCalculatedDisplayName(VM& vm, JSObject* object)
592 {
593     if (JSFunction* function = jsDynamicCast<JSFunction*>(object))
594         return function->calculatedDisplayName(vm);
595     if (InternalFunction* function = jsDynamicCast<InternalFunction*>(object))
596         return function->calculatedDisplayName(vm);
597     return emptyString();
598 }
599
600 void JSFunction::setFunctionName(ExecState* exec, JSValue value)
601 {
602     VM& vm = exec->vm();
603     auto scope = DECLARE_THROW_SCOPE(vm);
604
605     // The "name" property may have been already been defined as part of a property list in an
606     // object literal (and therefore reified).
607     if (hasReifiedName())
608         return;
609
610     ASSERT(!isHostFunction());
611     ASSERT(jsExecutable()->ecmaName().isNull());
612     String name;
613     if (value.isSymbol()) {
614         SymbolImpl& uid = asSymbol(value)->privateName().uid();
615         if (uid.isNullSymbol())
616             name = emptyString();
617         else
618             name = makeString('[', String(&uid), ']');
619     } else {
620         JSString* jsStr = value.toString(exec);
621         RETURN_IF_EXCEPTION(scope, void());
622         name = jsStr->value(exec);
623         RETURN_IF_EXCEPTION(scope, void());
624     }
625     reifyName(vm, exec, name);
626 }
627
628 void JSFunction::reifyLength(VM& vm)
629 {
630     FunctionRareData* rareData = this->rareData(vm);
631
632     ASSERT(!hasReifiedLength());
633     ASSERT(!isHostFunction());
634     JSValue initialValue = jsNumber(jsExecutable()->functionLength());
635     unsigned initialAttributes = DontEnum | ReadOnly;
636     const Identifier& identifier = vm.propertyNames->length;
637     putDirect(vm, identifier, initialValue, initialAttributes);
638
639     rareData->setHasReifiedLength();
640 }
641
642 void JSFunction::reifyName(VM& vm, ExecState* exec)
643 {
644     const Identifier& ecmaName = jsExecutable()->ecmaName();
645     String name;
646     // https://tc39.github.io/ecma262/#sec-exports-runtime-semantics-evaluation
647     // When the ident is "*default*", we need to set "default" for the ecma name.
648     // This "*default*" name is never shown to users.
649     if (ecmaName == exec->propertyNames().builtinNames().starDefaultPrivateName())
650         name = exec->propertyNames().defaultKeyword.string();
651     else
652         name = ecmaName.string();
653     reifyName(vm, exec, name);
654 }
655
656 void JSFunction::reifyName(VM& vm, ExecState* exec, String name)
657 {
658     FunctionRareData* rareData = this->rareData(vm);
659
660     ASSERT(!hasReifiedName());
661     ASSERT(!isHostFunction());
662     unsigned initialAttributes = DontEnum | ReadOnly;
663     const Identifier& propID = vm.propertyNames->name;
664
665     if (exec->lexicalGlobalObject()->needsSiteSpecificQuirks()) {
666         auto illegalCharMatcher = [] (UChar ch) -> bool {
667             return ch == ' ' || ch == '|';
668         };
669         if (name.find(illegalCharMatcher) != notFound)
670             name = String();
671     }
672     
673     if (jsExecutable()->isGetter())
674         name = makeString("get ", name);
675     else if (jsExecutable()->isSetter())
676         name = makeString("set ", name);
677
678     putDirect(vm, propID, jsString(exec, name), initialAttributes);
679     rareData->setHasReifiedName();
680 }
681
682 void JSFunction::reifyLazyPropertyIfNeeded(VM& vm, ExecState* exec, PropertyName propertyName)
683 {
684     if (propertyName == vm.propertyNames->length) {
685         if (!hasReifiedLength())
686             reifyLength(vm);
687     } else if (propertyName == vm.propertyNames->name) {
688         if (!hasReifiedName())
689             reifyName(vm, exec);
690     }
691 }
692
693 void JSFunction::reifyBoundNameIfNeeded(VM& vm, ExecState* exec, PropertyName propertyName)
694 {
695     const Identifier& nameIdent = vm.propertyNames->name;
696     if (propertyName != nameIdent)
697         return;
698
699     if (hasReifiedName())
700         return;
701
702     if (this->inherits(JSBoundFunction::info())) {
703         FunctionRareData* rareData = this->rareData(vm);
704         String name = makeString("bound ", static_cast<NativeExecutable*>(m_executable.get())->name());
705         unsigned initialAttributes = DontEnum | ReadOnly;
706         putDirect(vm, nameIdent, jsString(exec, name), initialAttributes);
707         rareData->setHasReifiedName();
708     }
709 }
710
711 } // namespace JSC