]>
Commit | Line | Data |
---|---|---|
9dae56ea A |
1 | /* |
2 | * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) | |
14957cd0 | 3 | * Copyright (C) 2003, 2007, 2008, 2011 Apple Inc. All rights reserved. |
9dae56ea A |
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" | |
f9bf01c6 | 28 | #include "Error.h" |
14957cd0 | 29 | #include "ExceptionHelpers.h" |
9dae56ea | 30 | #include "JSArray.h" |
ba379fdc | 31 | #include "JSFunction.h" |
9dae56ea A |
32 | #include "Lookup.h" |
33 | ||
34 | namespace JSC { | |
35 | ||
14957cd0 A |
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 | ||
9dae56ea A |
52 | ASSERT_CLASS_FITS_IN_CELL(ArrayConstructor); |
53 | ||
14957cd0 A |
54 | ArrayConstructor::ArrayConstructor(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, ArrayPrototype* arrayPrototype) |
55 | : InternalFunction(&exec->globalData(), globalObject, structure, Identifier(exec, arrayPrototype->classInfo()->className)) | |
9dae56ea | 56 | { |
14957cd0 A |
57 | putDirectWithoutTransition(exec->globalData(), exec->propertyNames().prototype, arrayPrototype, DontEnum | DontDelete | ReadOnly); |
58 | putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(1), ReadOnly | DontEnum | DontDelete); | |
59 | } | |
9dae56ea | 60 | |
14957cd0 A |
61 | bool ArrayConstructor::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot) |
62 | { | |
63 | return getStaticFunctionSlot<InternalFunction>(exec, ExecState::arrayConstructorTable(exec), this, propertyName, slot); | |
64 | } | |
f9bf01c6 | 65 | |
14957cd0 A |
66 | bool ArrayConstructor::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor) |
67 | { | |
68 | return getStaticFunctionDescriptor<InternalFunction>(exec, ExecState::arrayConstructorTable(exec), this, propertyName, descriptor); | |
9dae56ea A |
69 | } |
70 | ||
14957cd0 A |
71 | // ------------------------------ Functions --------------------------- |
72 | ||
f9bf01c6 | 73 | static inline JSObject* constructArrayWithSizeQuirk(ExecState* exec, const ArgList& args) |
9dae56ea | 74 | { |
14957cd0 A |
75 | JSGlobalObject* globalObject = asInternalFunction(exec->callee())->globalObject(); |
76 | ||
9dae56ea | 77 | // a single numeric argument denotes the array size (!) |
ba379fdc A |
78 | if (args.size() == 1 && args.at(0).isNumber()) { |
79 | uint32_t n = args.at(0).toUInt32(exec); | |
80 | if (n != args.at(0).toNumber(exec)) | |
14957cd0 A |
81 | return throwError(exec, createRangeError(exec, "Array size is not a small enough positive integer.")); |
82 | return new (exec) JSArray(exec->globalData(), globalObject->arrayStructure(), n, CreateInitialized); | |
9dae56ea A |
83 | } |
84 | ||
85 | // otherwise the array is constructed with the arguments in it | |
14957cd0 | 86 | return new (exec) JSArray(exec->globalData(), globalObject->arrayStructure(), args); |
9dae56ea A |
87 | } |
88 | ||
14957cd0 | 89 | static EncodedJSValue JSC_HOST_CALL constructWithArrayConstructor(ExecState* exec) |
9dae56ea | 90 | { |
14957cd0 A |
91 | ArgList args(exec); |
92 | return JSValue::encode(constructArrayWithSizeQuirk(exec, args)); | |
9dae56ea A |
93 | } |
94 | ||
9dae56ea A |
95 | ConstructType ArrayConstructor::getConstructData(ConstructData& constructData) |
96 | { | |
97 | constructData.native.function = constructWithArrayConstructor; | |
98 | return ConstructTypeHost; | |
99 | } | |
100 | ||
14957cd0 | 101 | static EncodedJSValue JSC_HOST_CALL callArrayConstructor(ExecState* exec) |
9dae56ea | 102 | { |
14957cd0 A |
103 | ArgList args(exec); |
104 | return JSValue::encode(constructArrayWithSizeQuirk(exec, args)); | |
9dae56ea A |
105 | } |
106 | ||
9dae56ea A |
107 | CallType ArrayConstructor::getCallData(CallData& callData) |
108 | { | |
109 | // equivalent to 'new Array(....)' | |
110 | callData.native.function = callArrayConstructor; | |
111 | return CallTypeHost; | |
112 | } | |
113 | ||
14957cd0 | 114 | EncodedJSValue JSC_HOST_CALL arrayConstructorIsArray(ExecState* exec) |
f9bf01c6 | 115 | { |
14957cd0 | 116 | return JSValue::encode(jsBoolean(exec->argument(0).inherits(&JSArray::s_info))); |
f9bf01c6 A |
117 | } |
118 | ||
9dae56ea | 119 | } // namespace JSC |