initial import
[vuplus_webkit] / Source / JavaScriptCore / runtime / ArrayConstructor.cpp
1 /*
2  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3  *  Copyright (C) 2003, 2007, 2008, 2011 Apple Inc. All rights reserved.
4  *  Copyright (C) 2003 Peter Kelly (pmk@post.com)
5  *  Copyright (C) 2006 Alexey Proskuryakov (ap@nypop.com)
6  *
7  *  This library is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU Lesser General Public
9  *  License as published by the Free Software Foundation; either
10  *  version 2 of the License, or (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this library; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
20  *  USA
21  *
22  */
23
24 #include "config.h"
25 #include "ArrayConstructor.h"
26
27 #include "ArrayPrototype.h"
28 #include "Error.h"
29 #include "ExceptionHelpers.h"
30 #include "JSArray.h"
31 #include "JSFunction.h"
32 #include "Lookup.h"
33
34 namespace JSC {
35
36 static EncodedJSValue JSC_HOST_CALL arrayConstructorIsArray(ExecState*);
37
38 }
39
40 #include "ArrayConstructor.lut.h"
41
42 namespace JSC {
43
44 const ClassInfo ArrayConstructor::s_info = { "Function", &InternalFunction::s_info, 0, ExecState::arrayConstructorTable };
45
46 /* Source for ArrayConstructor.lut.h
47 @begin arrayConstructorTable
48   isArray   arrayConstructorIsArray     DontEnum|Function 1
49 @end
50 */
51
52 ASSERT_CLASS_FITS_IN_CELL(ArrayConstructor);
53
54 ArrayConstructor::ArrayConstructor(JSGlobalObject* globalObject, Structure* structure)
55     : InternalFunction(globalObject, structure)
56 {
57 }
58
59 void ArrayConstructor::finishCreation(ExecState* exec, ArrayPrototype* arrayPrototype)
60 {
61     Base::finishCreation(exec->globalData(), Identifier(exec, arrayPrototype->classInfo()->className));
62     putDirectWithoutTransition(exec->globalData(), exec->propertyNames().prototype, arrayPrototype, DontEnum | DontDelete | ReadOnly);
63     putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(1), ReadOnly | DontEnum | DontDelete);
64 }
65
66 bool ArrayConstructor::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
67 {
68     return getStaticFunctionSlot<InternalFunction>(exec, ExecState::arrayConstructorTable(exec), this, propertyName, slot);
69 }
70
71 bool ArrayConstructor::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
72 {
73     return getStaticFunctionDescriptor<InternalFunction>(exec, ExecState::arrayConstructorTable(exec), this, propertyName, descriptor);
74 }
75
76 // ------------------------------ Functions ---------------------------
77
78 static inline JSObject* constructArrayWithSizeQuirk(ExecState* exec, const ArgList& args)
79 {
80     JSGlobalObject* globalObject = asInternalFunction(exec->callee())->globalObject();
81
82     // a single numeric argument denotes the array size (!)
83     if (args.size() == 1 && args.at(0).isNumber()) {
84         uint32_t n = args.at(0).toUInt32(exec);
85         if (n != args.at(0).toNumber(exec))
86             return throwError(exec, createRangeError(exec, "Array size is not a small enough positive integer."));
87         return JSArray::create(exec->globalData(), globalObject->arrayStructure(), n, CreateInitialized);
88     }
89
90     // otherwise the array is constructed with the arguments in it
91     return JSArray::create(exec->globalData(), globalObject->arrayStructure(), args);
92 }
93
94 static EncodedJSValue JSC_HOST_CALL constructWithArrayConstructor(ExecState* exec)
95 {
96     ArgList args(exec);
97     return JSValue::encode(constructArrayWithSizeQuirk(exec, args));
98 }
99
100 ConstructType ArrayConstructor::getConstructData(ConstructData& constructData)
101 {
102     constructData.native.function = constructWithArrayConstructor;
103     return ConstructTypeHost;
104 }
105
106 static EncodedJSValue JSC_HOST_CALL callArrayConstructor(ExecState* exec)
107 {
108     ArgList args(exec);
109     return JSValue::encode(constructArrayWithSizeQuirk(exec, args));
110 }
111
112 CallType ArrayConstructor::getCallData(CallData& callData)
113 {
114     // equivalent to 'new Array(....)'
115     callData.native.function = callArrayConstructor;
116     return CallTypeHost;
117 }
118
119 EncodedJSValue JSC_HOST_CALL arrayConstructorIsArray(ExecState* exec)
120 {
121     return JSValue::encode(jsBoolean(exec->argument(0).inherits(&JSArray::s_info)));
122 }
123
124 } // namespace JSC