~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/JavaScriptCore/runtime/ArrayConstructor.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "ButterflyInlines.h"
 
29
#include "CopiedSpaceInlines.h"
 
30
#include "Error.h"
 
31
#include "ExceptionHelpers.h"
 
32
#include "JSArray.h"
 
33
#include "JSFunction.h"
 
34
#include "Lookup.h"
 
35
 
 
36
namespace JSC {
 
37
 
 
38
static EncodedJSValue JSC_HOST_CALL arrayConstructorIsArray(ExecState*);
 
39
 
 
40
}
 
41
 
 
42
#include "ArrayConstructor.lut.h"
 
43
 
 
44
namespace JSC {
 
45
 
 
46
ASSERT_HAS_TRIVIAL_DESTRUCTOR(ArrayConstructor);
 
47
 
 
48
const ClassInfo ArrayConstructor::s_info = { "Function", &InternalFunction::s_info, 0, ExecState::arrayConstructorTable, CREATE_METHOD_TABLE(ArrayConstructor) };
 
49
 
 
50
/* Source for ArrayConstructor.lut.h
 
51
@begin arrayConstructorTable
 
52
  isArray   arrayConstructorIsArray     DontEnum|Function 1
 
53
@end
 
54
*/
 
55
 
 
56
ArrayConstructor::ArrayConstructor(JSGlobalObject* globalObject, Structure* structure)
 
57
    : InternalFunction(globalObject, structure)
 
58
{
 
59
}
 
60
 
 
61
void ArrayConstructor::finishCreation(ExecState* exec, ArrayPrototype* arrayPrototype)
 
62
{
 
63
    Base::finishCreation(exec->globalData(), arrayPrototype->classInfo()->className);
 
64
    putDirectWithoutTransition(exec->globalData(), exec->propertyNames().prototype, arrayPrototype, DontEnum | DontDelete | ReadOnly);
 
65
    putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(1), ReadOnly | DontEnum | DontDelete);
 
66
}
 
67
 
 
68
bool ArrayConstructor::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot &slot)
 
69
{
 
70
    return getStaticFunctionSlot<InternalFunction>(exec, ExecState::arrayConstructorTable(exec), jsCast<ArrayConstructor*>(cell), propertyName, slot);
 
71
}
 
72
 
 
73
bool ArrayConstructor::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
 
74
{
 
75
    return getStaticFunctionDescriptor<InternalFunction>(exec, ExecState::arrayConstructorTable(exec), jsCast<ArrayConstructor*>(object), propertyName, descriptor);
 
76
}
 
77
 
 
78
// ------------------------------ Functions ---------------------------
 
79
 
 
80
JSObject* constructArrayWithSizeQuirk(ExecState* exec, ArrayAllocationProfile* profile, JSGlobalObject* globalObject, JSValue length)
 
81
{
 
82
    if (!length.isNumber())
 
83
        return constructArray(exec, profile, globalObject, &length, 1);
 
84
    
 
85
    uint32_t n = length.toUInt32(exec);
 
86
    if (n != length.toNumber(exec))
 
87
        return throwError(exec, createRangeError(exec, ASCIILiteral("Array size is not a small enough positive integer.")));
 
88
    return constructEmptyArray(exec, profile, globalObject, n);
 
89
}
 
90
 
 
91
static inline JSObject* constructArrayWithSizeQuirk(ExecState* exec, const ArgList& args)
 
92
{
 
93
    JSGlobalObject* globalObject = asInternalFunction(exec->callee())->globalObject();
 
94
 
 
95
    // a single numeric argument denotes the array size (!)
 
96
    if (args.size() == 1)
 
97
        return constructArrayWithSizeQuirk(exec, 0, globalObject, args.at(0));
 
98
 
 
99
    // otherwise the array is constructed with the arguments in it
 
100
    return constructArray(exec, 0, globalObject, args);
 
101
}
 
102
 
 
103
static EncodedJSValue JSC_HOST_CALL constructWithArrayConstructor(ExecState* exec)
 
104
{
 
105
    ArgList args(exec);
 
106
    return JSValue::encode(constructArrayWithSizeQuirk(exec, args));
 
107
}
 
108
 
 
109
ConstructType ArrayConstructor::getConstructData(JSCell*, ConstructData& constructData)
 
110
{
 
111
    constructData.native.function = constructWithArrayConstructor;
 
112
    return ConstructTypeHost;
 
113
}
 
114
 
 
115
static EncodedJSValue JSC_HOST_CALL callArrayConstructor(ExecState* exec)
 
116
{
 
117
    ArgList args(exec);
 
118
    return JSValue::encode(constructArrayWithSizeQuirk(exec, args));
 
119
}
 
120
 
 
121
CallType ArrayConstructor::getCallData(JSCell*, CallData& callData)
 
122
{
 
123
    // equivalent to 'new Array(....)'
 
124
    callData.native.function = callArrayConstructor;
 
125
    return CallTypeHost;
 
126
}
 
127
 
 
128
EncodedJSValue JSC_HOST_CALL arrayConstructorIsArray(ExecState* exec)
 
129
{
 
130
    return JSValue::encode(jsBoolean(exec->argument(0).inherits(&JSArray::s_info)));
 
131
}
 
132
 
 
133
} // namespace JSC