Speed up JSGlobalObject initialization by making some properties lazy
[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 NativeExecutable* JSFunction::lookUpOrCreateNativeExecutable(VM& vm, NativeFunction nativeFunction, Intrinsic intrinsic, NativeFunction nativeConstructor, const String& name)
86 {
87     return vm.getHostFunction(nativeFunction, intrinsic, nativeConstructor, name);
88 }
89
90 JSFunction* JSFunction::create(VM& vm, JSGlobalObject* globalObject, int length, const String& name, NativeFunction nativeFunction, Intrinsic intrinsic, NativeFunction nativeConstructor)
91 {
92     NativeExecutable* executable = lookUpOrCreateNativeExecutable(vm, nativeFunction, intrinsic, nativeConstructor, name);
93     JSFunction* function = new (NotNull, allocateCell<JSFunction>(vm.heap)) JSFunction(vm, globalObject, globalObject->functionStructure());
94     // Can't do this during initialization because getHostFunction might do a GC allocation.
95     function->finishCreation(vm, executable, length, name);
96     return function;
97 }
98
99 JSFunction::JSFunction(VM& vm, JSGlobalObject* globalObject, Structure* structure)
100     : Base(vm, globalObject, structure)
101     , m_executable()
102 {
103 }
104
105 void JSFunction::finishCreation(VM& vm, NativeExecutable* executable, int length, const String& name)
106 {
107     Base::finishCreation(vm);
108     ASSERT(inherits(info()));
109     m_executable.set(vm, this, executable);
110     putDirect(vm, vm.propertyNames->name, jsString(&vm, name), ReadOnly | DontEnum);
111     putDirect(vm, vm.propertyNames->length, jsNumber(length), ReadOnly | DontEnum);
112 }
113
114 JSFunction* JSFunction::createBuiltinFunction(VM& vm, FunctionExecutable* executable, JSGlobalObject* globalObject)
115 {
116     JSFunction* function = create(vm, executable, globalObject);
117     function->putDirect(vm, vm.propertyNames->name, jsString(&vm, executable->name().string()), ReadOnly | DontEnum);
118     function->putDirect(vm, vm.propertyNames->length, jsNumber(executable->parameterCount()), ReadOnly | DontEnum);
119     return function;
120 }
121
122 JSFunction* JSFunction::createBuiltinFunction(VM& vm, FunctionExecutable* executable, JSGlobalObject* globalObject, const String& name)
123 {
124     JSFunction* function = create(vm, executable, globalObject);
125     function->putDirect(vm, vm.propertyNames->name, jsString(&vm, name), ReadOnly | DontEnum);
126     function->putDirect(vm, vm.propertyNames->length, jsNumber(executable->parameterCount()), ReadOnly | DontEnum);
127     return function;
128 }
129
130 FunctionRareData* JSFunction::allocateRareData(VM& vm)
131 {
132     ASSERT(!m_rareData);
133     FunctionRareData* rareData = FunctionRareData::create(vm);
134
135     // A DFG compilation thread may be trying to read the rare data
136     // We want to ensure that it sees it properly allocated
137     WTF::storeStoreFence();
138
139     m_rareData.set(vm, this, rareData);
140     return m_rareData.get();
141 }
142
143 FunctionRareData* JSFunction::allocateAndInitializeRareData(ExecState* exec, size_t inlineCapacity)
144 {
145     ASSERT(!m_rareData);
146     VM& vm = exec->vm();
147     JSObject* prototype = jsDynamicCast<JSObject*>(get(exec, vm.propertyNames->prototype));
148     if (!prototype)
149         prototype = globalObject()->objectPrototype();
150     FunctionRareData* rareData = FunctionRareData::create(vm);
151     rareData->initializeObjectAllocationProfile(globalObject()->vm(), prototype, inlineCapacity);
152
153     // A DFG compilation thread may be trying to read the rare data
154     // We want to ensure that it sees it properly allocated
155     WTF::storeStoreFence();
156
157     m_rareData.set(vm, this, rareData);
158     return m_rareData.get();
159 }
160
161 FunctionRareData* JSFunction::initializeRareData(ExecState* exec, size_t inlineCapacity)
162 {
163     ASSERT(!!m_rareData);
164     VM& vm = exec->vm();
165     JSObject* prototype = jsDynamicCast<JSObject*>(get(exec, vm.propertyNames->prototype));
166     if (!prototype)
167         prototype = globalObject()->objectPrototype();
168     m_rareData->initializeObjectAllocationProfile(globalObject()->vm(), prototype, inlineCapacity);
169     return m_rareData.get();
170 }
171
172 String JSFunction::name()
173 {
174     if (isHostFunction()) {
175         NativeExecutable* executable = jsCast<NativeExecutable*>(this->executable());
176         return executable->name();
177     }
178     return jsExecutable()->name().string();
179 }
180
181 String JSFunction::displayName(ExecState* exec)
182 {
183     JSValue displayName = getDirect(exec->vm(), exec->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(ExecState* exec)
192 {
193     const String explicitName = displayName(exec);
194     
195     if (!explicitName.isEmpty())
196         return explicitName;
197     
198     const String actualName = name();
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 static GetterSetter* getThrowTypeErrorGetterSetter(JSFunction* function)
323 {
324     return function->jsExecutable()->isClassConstructorFunction() || function->jsExecutable()->parseMode() == SourceParseMode::MethodMode
325         ? function->globalObject()->throwTypeErrorArgumentsAndCallerGetterSetter()
326         : function->globalObject()->throwTypeErrorGetterSetter();
327 }
328
329 EncodedJSValue JSFunction::callerGetter(ExecState* exec, EncodedJSValue thisValue, PropertyName)
330 {
331     JSFunction* thisObj = jsCast<JSFunction*>(JSValue::decode(thisValue));
332     ASSERT(!thisObj->isHostFunction());
333     JSValue caller = retrieveCallerFunction(exec, thisObj);
334
335     // See ES5.1 15.3.5.4 - Function.caller may not be used to retrieve a strict caller.
336     if (!caller.isObject() || !asObject(caller)->inherits(JSFunction::info())) {
337         // It isn't a JSFunction, but if it is a JSCallee from a program or call eval, return null.
338         if (jsDynamicCast<JSCallee*>(caller))
339             return JSValue::encode(jsNull());
340         return JSValue::encode(caller);
341     }
342     JSFunction* function = jsCast<JSFunction*>(caller);
343     if (function->isHostOrBuiltinFunction() || !function->jsExecutable()->isStrictMode())
344         return JSValue::encode(caller);
345     return JSValue::encode(throwTypeError(exec, ASCIILiteral("Function.caller used to retrieve strict caller")));
346 }
347
348 bool JSFunction::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
349 {
350     JSFunction* thisObject = jsCast<JSFunction*>(object);
351     if (thisObject->isHostOrBuiltinFunction())
352         return Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
353
354     if (propertyName == exec->propertyNames().prototype && !thisObject->jsExecutable()->isArrowFunction()) {
355         VM& vm = exec->vm();
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                 prototype = constructEmptyObject(exec, thisObject->globalObject()->generatorPrototype());
362             else
363                 prototype = constructEmptyObject(exec);
364
365             prototype->putDirect(vm, exec->propertyNames().constructor, thisObject, DontEnum);
366             thisObject->putDirect(vm, exec->propertyNames().prototype, prototype, DontDelete | DontEnum);
367             offset = thisObject->getDirectOffset(vm, exec->propertyNames().prototype, attributes);
368             ASSERT(isValidOffset(offset));
369         }
370
371         slot.setValue(thisObject, attributes, thisObject->getDirect(offset), offset);
372     }
373
374     if (propertyName == exec->propertyNames().arguments) {
375         if (thisObject->jsExecutable()->isStrictMode() || thisObject->jsExecutable()->isClassConstructorFunction()) {
376             bool result = Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
377             if (!result) {
378                 GetterSetter* errorGetterSetter = getThrowTypeErrorGetterSetter(thisObject);
379                 thisObject->putDirectAccessor(exec, propertyName, errorGetterSetter, DontDelete | DontEnum | Accessor);
380                 result = Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
381                 ASSERT(result);
382             }
383             return result;
384         }
385         slot.setCacheableCustom(thisObject, ReadOnly | DontEnum | DontDelete, argumentsGetter);
386         return true;
387     }
388
389     if (propertyName == exec->propertyNames().caller) {
390         if (thisObject->jsExecutable()->isStrictMode() || thisObject->jsExecutable()->isClassConstructorFunction()) {
391             bool result = Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
392             if (!result) {
393                 GetterSetter* errorGetterSetter = getThrowTypeErrorGetterSetter(thisObject);
394                 thisObject->putDirectAccessor(exec, propertyName, errorGetterSetter, DontDelete | DontEnum | Accessor);
395                 result = Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
396                 ASSERT(result);
397             }
398             return result;
399         }
400         slot.setCacheableCustom(thisObject, ReadOnly | DontEnum | DontDelete, callerGetter);
401         return true;
402     }
403
404     thisObject->reifyLazyPropertyIfNeeded(exec, propertyName);
405
406     return Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
407 }
408
409 void JSFunction::getOwnNonIndexPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
410 {
411     JSFunction* thisObject = jsCast<JSFunction*>(object);
412     if (!thisObject->isHostOrBuiltinFunction() && mode.includeDontEnumProperties()) {
413         VM& vm = exec->vm();
414         // Make sure prototype has been reified.
415         PropertySlot slot(thisObject, PropertySlot::InternalMethodType::VMInquiry);
416         thisObject->methodTable(vm)->getOwnPropertySlot(thisObject, exec, vm.propertyNames->prototype, slot);
417
418         propertyNames.add(vm.propertyNames->arguments);
419         propertyNames.add(vm.propertyNames->caller);
420         if (!thisObject->hasReifiedLength())
421             propertyNames.add(vm.propertyNames->length);
422         if (!thisObject->hasReifiedName())
423             propertyNames.add(vm.propertyNames->name);
424     }
425     Base::getOwnNonIndexPropertyNames(thisObject, exec, propertyNames, mode);
426 }
427
428 bool JSFunction::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
429 {
430     JSFunction* thisObject = jsCast<JSFunction*>(cell);
431
432     if (UNLIKELY(isThisValueAltered(slot, thisObject)))
433         return ordinarySetSlow(exec, thisObject, propertyName, value, slot.thisValue(), slot.isStrictMode());
434
435     if (thisObject->isHostOrBuiltinFunction())
436         return Base::put(thisObject, exec, propertyName, value, slot);
437
438     if (propertyName == exec->propertyNames().prototype) {
439         // Make sure prototype has been reified, such that it can only be overwritten
440         // following the rules set out in ECMA-262 8.12.9.
441         PropertySlot slot(thisObject, PropertySlot::InternalMethodType::VMInquiry);
442         thisObject->methodTable(exec->vm())->getOwnPropertySlot(thisObject, exec, propertyName, slot);
443         if (thisObject->m_rareData)
444             thisObject->m_rareData->clear("Store to prototype property of a function");
445         // Don't allow this to be cached, since a [[Put]] must clear m_rareData.
446         PutPropertySlot dontCache(thisObject);
447         return Base::put(thisObject, exec, propertyName, value, dontCache);
448     }
449     if (thisObject->jsExecutable()->isStrictMode() && (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         return Base::put(thisObject, exec, propertyName, value, slot);
454     }
455     if (propertyName == exec->propertyNames().arguments || propertyName == exec->propertyNames().caller) {
456         if (slot.isStrictMode())
457             throwTypeError(exec, StrictModeReadonlyPropertyWriteError);
458         return false;
459     }
460     thisObject->reifyLazyPropertyIfNeeded(exec, propertyName);
461     return Base::put(thisObject, exec, propertyName, value, slot);
462 }
463
464 bool JSFunction::deleteProperty(JSCell* cell, ExecState* exec, PropertyName propertyName)
465 {
466     JSFunction* thisObject = jsCast<JSFunction*>(cell);
467     // For non-host functions, don't let these properties by deleted - except by DefineOwnProperty.
468     if (!thisObject->isHostOrBuiltinFunction() && !exec->vm().isInDefineOwnProperty()) {
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         return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
486
487     if (propertyName == exec->propertyNames().prototype) {
488         // Make sure prototype has been reified, such that it can only be overwritten
489         // following the rules set out in ECMA-262 8.12.9.
490         PropertySlot slot(thisObject, PropertySlot::InternalMethodType::VMInquiry);
491         thisObject->methodTable(exec->vm())->getOwnPropertySlot(thisObject, exec, propertyName, slot);
492         if (thisObject->m_rareData)
493             thisObject->m_rareData->clear("Store to prototype property of a function");
494         return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
495     }
496
497     bool valueCheck;
498     if (propertyName == exec->propertyNames().arguments) {
499         if (thisObject->jsExecutable()->isStrictMode()) {
500             PropertySlot slot(thisObject, PropertySlot::InternalMethodType::VMInquiry);
501             if (!Base::getOwnPropertySlot(thisObject, exec, propertyName, slot))
502                 thisObject->putDirectAccessor(exec, propertyName, thisObject->globalObject()->throwTypeErrorGetterSetter(), DontDelete | DontEnum | Accessor);
503             return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
504         }
505         valueCheck = !descriptor.value() || sameValue(exec, descriptor.value(), retrieveArguments(exec, thisObject));
506     } else if (propertyName == exec->propertyNames().caller) {
507         if (thisObject->jsExecutable()->isStrictMode()) {
508             PropertySlot slot(thisObject, PropertySlot::InternalMethodType::VMInquiry);
509             if (!Base::getOwnPropertySlot(thisObject, exec, propertyName, slot))
510                 thisObject->putDirectAccessor(exec, propertyName, thisObject->globalObject()->throwTypeErrorGetterSetter(), DontDelete | DontEnum | Accessor);
511             return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
512         }
513         valueCheck = !descriptor.value() || sameValue(exec, descriptor.value(), retrieveCallerFunction(exec, thisObject));
514     } else {
515         thisObject->reifyLazyPropertyIfNeeded(exec, propertyName);
516         return Base::defineOwnProperty(object, exec, propertyName, descriptor, throwException);
517     }
518      
519     if (descriptor.configurablePresent() && descriptor.configurable()) {
520         if (throwException)
521             exec->vm().throwException(exec, createTypeError(exec, ASCIILiteral("Attempting to change configurable attribute of unconfigurable property.")));
522         return false;
523     }
524     if (descriptor.enumerablePresent() && descriptor.enumerable()) {
525         if (throwException)
526             exec->vm().throwException(exec, createTypeError(exec, ASCIILiteral("Attempting to change enumerable attribute of unconfigurable property.")));
527         return false;
528     }
529     if (descriptor.isAccessorDescriptor()) {
530         if (throwException)
531             exec->vm().throwException(exec, createTypeError(exec, ASCIILiteral(UnconfigurablePropertyChangeAccessMechanismError)));
532         return false;
533     }
534     if (descriptor.writablePresent() && descriptor.writable()) {
535         if (throwException)
536             exec->vm().throwException(exec, createTypeError(exec, ASCIILiteral("Attempting to change writable attribute of unconfigurable property.")));
537         return false;
538     }
539     if (!valueCheck) {
540         if (throwException)
541             exec->vm().throwException(exec, createTypeError(exec, ASCIILiteral("Attempting to change value of a readonly property.")));
542         return false;
543     }
544     return true;
545 }
546
547 // ECMA 13.2.2 [[Construct]]
548 ConstructType JSFunction::getConstructData(JSCell* cell, ConstructData& constructData)
549 {
550     JSFunction* thisObject = jsCast<JSFunction*>(cell);
551
552     if (thisObject->isHostFunction()) {
553         constructData.native.function = thisObject->nativeConstructor();
554         return ConstructType::Host;
555     }
556
557     FunctionExecutable* functionExecutable = thisObject->jsExecutable();
558     if (functionExecutable->constructAbility() == ConstructAbility::CannotConstruct)
559         return ConstructType::None;
560
561     constructData.js.functionExecutable = functionExecutable;
562     constructData.js.scope = thisObject->scope();
563     return ConstructType::JS;
564 }
565
566 String getCalculatedDisplayName(CallFrame* callFrame, JSObject* object)
567 {
568     if (JSFunction* function = jsDynamicCast<JSFunction*>(object))
569         return function->calculatedDisplayName(callFrame);
570     if (InternalFunction* function = jsDynamicCast<InternalFunction*>(object))
571         return function->calculatedDisplayName(callFrame);
572     return emptyString();
573 }
574
575 void JSFunction::setFunctionName(ExecState* exec, JSValue value)
576 {
577     // The "name" property may have been already been defined as part of a property list in an
578     // object literal (and therefore reified).
579     if (hasReifiedName())
580         return;
581
582     ASSERT(!isHostFunction());
583     ASSERT(jsExecutable()->ecmaName().isNull());
584     String name;
585     if (value.isSymbol()) {
586         SymbolImpl* uid = asSymbol(value)->privateName().uid();
587         if (uid->isNullSymbol())
588             name = emptyString();
589         else
590             name = makeString("[", String(asSymbol(value)->privateName().uid()), ']');
591     } else {
592         VM& vm = exec->vm();
593         JSString* jsStr = value.toString(exec);
594         if (vm.exception())
595             return;
596         name = jsStr->value(exec);
597         if (vm.exception())
598             return;
599     }
600     reifyName(exec, name);
601 }
602
603 void JSFunction::reifyLength(ExecState* exec)
604 {
605     VM& vm = exec->vm();
606     FunctionRareData* rareData = this->rareData(vm);
607
608     ASSERT(!hasReifiedLength());
609     ASSERT(!isHostFunction());
610     JSValue initialValue = jsNumber(jsExecutable()->parameterCount());
611     unsigned initialAttributes = DontEnum | ReadOnly;
612     const Identifier& identifier = exec->propertyNames().length;
613     putDirect(vm, identifier, initialValue, initialAttributes);
614
615     rareData->setHasReifiedLength();
616 }
617
618 void JSFunction::reifyName(ExecState* exec)
619 {
620     String name = jsExecutable()->ecmaName().string();
621     reifyName(exec, name);
622 }
623
624 void JSFunction::reifyName(ExecState* exec, String name)
625 {
626     VM& vm = exec->vm();
627     FunctionRareData* rareData = this->rareData(vm);
628
629     ASSERT(!hasReifiedName());
630     ASSERT(!isHostFunction());
631     unsigned initialAttributes = DontEnum | ReadOnly;
632     const Identifier& propID = exec->propertyNames().name;
633
634     if (jsExecutable()->isGetter())
635         name = makeString("get ", name);
636     else if (jsExecutable()->isSetter())
637         name = makeString("set ", name);
638
639     putDirect(vm, propID, jsString(exec, name), initialAttributes);
640     rareData->setHasReifiedName();
641 }
642
643 void JSFunction::reifyLazyPropertyIfNeeded(ExecState* exec, PropertyName propertyName)
644 {
645     if (propertyName == exec->propertyNames().length) {
646         if (!hasReifiedLength())
647             reifyLength(exec);
648     } else if (propertyName == exec->propertyNames().name) {
649         if (!hasReifiedName())
650             reifyName(exec);
651     }
652 }
653
654 } // namespace JSC