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