2 * Copyright (C) 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "JSArrayBufferConstructor.h"
30 #include "ExceptionHelpers.h"
31 #include "JSArrayBuffer.h"
32 #include "JSArrayBufferPrototype.h"
33 #include "JSGlobalObject.h"
34 #include "JSCInlines.h"
38 static EncodedJSValue JSC_HOST_CALL
arrayBufferFuncIsView(ExecState
*);
40 const ClassInfo
JSArrayBufferConstructor::s_info
= {
41 "Function", &Base::s_info
, 0, 0,
42 CREATE_METHOD_TABLE(JSArrayBufferConstructor
)
45 JSArrayBufferConstructor::JSArrayBufferConstructor(VM
& vm
, Structure
* structure
)
50 void JSArrayBufferConstructor::finishCreation(VM
& vm
, JSArrayBufferPrototype
* prototype
)
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
);
56 JSGlobalObject
* globalObject
= this->globalObject();
57 JSC_NATIVE_FUNCTION(vm
.propertyNames
->isView
, arrayBufferFuncIsView
, DontEnum
, 1);
60 JSArrayBufferConstructor
* JSArrayBufferConstructor::create(VM
& vm
, Structure
* structure
, JSArrayBufferPrototype
* prototype
)
62 JSArrayBufferConstructor
* result
=
63 new (NotNull
, allocateCell
<JSArrayBufferConstructor
>(vm
.heap
))
64 JSArrayBufferConstructor(vm
, structure
);
65 result
->finishCreation(vm
, prototype
);
69 Structure
* JSArrayBufferConstructor::createStructure(
70 VM
& vm
, JSGlobalObject
* globalObject
, JSValue prototype
)
72 return Structure::create(
73 vm
, globalObject
, prototype
, TypeInfo(ObjectType
, StructureFlags
), info());
76 static EncodedJSValue JSC_HOST_CALL
constructArrayBuffer(ExecState
* exec
)
78 JSArrayBufferConstructor
* constructor
=
79 jsCast
<JSArrayBufferConstructor
*>(exec
->callee());
82 if (exec
->argumentCount()) {
83 length
= exec
->uncheckedArgument(0).toUInt32(exec
);
84 if (exec
->hadException())
85 return JSValue::encode(jsUndefined());
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.
93 RefPtr
<ArrayBuffer
> buffer
= ArrayBuffer::create(length
, 1);
95 return throwVMError(exec
, createOutOfMemoryError(constructor
->globalObject()));
97 JSArrayBuffer
* result
= JSArrayBuffer::create(
98 exec
->vm(), constructor
->globalObject()->arrayBufferStructure(), buffer
);
100 return JSValue::encode(result
);
103 ConstructType
JSArrayBufferConstructor::getConstructData(
104 JSCell
*, ConstructData
& constructData
)
106 constructData
.native
.function
= constructArrayBuffer
;
107 return ConstructTypeHost
;
110 CallType
JSArrayBufferConstructor::getCallData(JSCell
*, CallData
& callData
)
112 callData
.native
.function
= constructArrayBuffer
;
116 // ------------------------------ Functions --------------------------------
119 EncodedJSValue JSC_HOST_CALL
arrayBufferFuncIsView(ExecState
* exec
)
121 return JSValue::encode(jsBoolean(jsDynamicCast
<JSArrayBufferView
*>(exec
->argument(0))));