]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/JSArrayBufferConstructor.cpp
JavaScriptCore-7600.1.4.11.8.tar.gz
[apple/javascriptcore.git] / runtime / JSArrayBufferConstructor.cpp
CommitLineData
81345200
A
1/*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "JSArrayBufferConstructor.h"
28
29#include "Error.h"
30#include "ExceptionHelpers.h"
31#include "JSArrayBuffer.h"
32#include "JSArrayBufferPrototype.h"
33#include "JSGlobalObject.h"
34#include "JSCInlines.h"
35
36namespace JSC {
37
38static EncodedJSValue JSC_HOST_CALL arrayBufferFuncIsView(ExecState*);
39
40const ClassInfo JSArrayBufferConstructor::s_info = {
41 "Function", &Base::s_info, 0, 0,
42 CREATE_METHOD_TABLE(JSArrayBufferConstructor)
43};
44
45JSArrayBufferConstructor::JSArrayBufferConstructor(VM& vm, Structure* structure)
46 : Base(vm, structure)
47{
48}
49
50void JSArrayBufferConstructor::finishCreation(VM& vm, JSArrayBufferPrototype* prototype)
51{
52 Base::finishCreation(vm, "ArrayBuffer");
53 putDirectWithoutTransition(vm, vm.propertyNames->prototype, prototype, DontEnum | DontDelete | ReadOnly);
54 putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), DontEnum | DontDelete | ReadOnly);
55
56 JSGlobalObject* globalObject = this->globalObject();
57 JSC_NATIVE_FUNCTION(vm.propertyNames->isView, arrayBufferFuncIsView, DontEnum, 1);
58}
59
60JSArrayBufferConstructor* JSArrayBufferConstructor::create(VM& vm, Structure* structure, JSArrayBufferPrototype* prototype)
61{
62 JSArrayBufferConstructor* result =
63 new (NotNull, allocateCell<JSArrayBufferConstructor>(vm.heap))
64 JSArrayBufferConstructor(vm, structure);
65 result->finishCreation(vm, prototype);
66 return result;
67}
68
69Structure* JSArrayBufferConstructor::createStructure(
70 VM& vm, JSGlobalObject* globalObject, JSValue prototype)
71{
72 return Structure::create(
73 vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
74}
75
76static EncodedJSValue JSC_HOST_CALL constructArrayBuffer(ExecState* exec)
77{
78 JSArrayBufferConstructor* constructor =
79 jsCast<JSArrayBufferConstructor*>(exec->callee());
80
81 unsigned length;
82 if (exec->argumentCount()) {
83 length = exec->uncheckedArgument(0).toUInt32(exec);
84 if (exec->hadException())
85 return JSValue::encode(jsUndefined());
86 } else {
87 // Although the documentation doesn't say so, it is in fact correct to say
88 // "new ArrayBuffer()". The result is the same as allocating an array buffer
89 // with a zero length.
90 length = 0;
91 }
92
93 RefPtr<ArrayBuffer> buffer = ArrayBuffer::create(length, 1);
94 if (!buffer)
95 return throwVMError(exec, createOutOfMemoryError(constructor->globalObject()));
96
97 JSArrayBuffer* result = JSArrayBuffer::create(
98 exec->vm(), constructor->globalObject()->arrayBufferStructure(), buffer);
99
100 return JSValue::encode(result);
101}
102
103ConstructType JSArrayBufferConstructor::getConstructData(
104 JSCell*, ConstructData& constructData)
105{
106 constructData.native.function = constructArrayBuffer;
107 return ConstructTypeHost;
108}
109
110CallType JSArrayBufferConstructor::getCallData(JSCell*, CallData& callData)
111{
112 callData.native.function = constructArrayBuffer;
113 return CallTypeHost;
114}
115
116// ------------------------------ Functions --------------------------------
117
118// ECMA 24.1.3.1
119EncodedJSValue JSC_HOST_CALL arrayBufferFuncIsView(ExecState* exec)
120{
121 return JSValue::encode(jsBoolean(jsDynamicCast<JSArrayBufferView*>(exec->argument(0))));
122}
123
124
125} // namespace JSC
126