forked from Samsung/escargot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayBufferObject.cpp
More file actions
190 lines (161 loc) · 7.03 KB
/
ArrayBufferObject.cpp
File metadata and controls
190 lines (161 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Copyright (c) 2017-present Samsung Electronics Co., Ltd
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
#include "Escargot.h"
#include "runtime/VMInstance.h"
#include "runtime/BackingStore.h"
#include "runtime/ArrayBufferObject.h"
#include "runtime/TypedArrayInlines.h"
namespace Escargot {
unsigned TypedArrayHelper::elementSizeTable[11] = { 1, 2, 4, 1, 2, 4, 1, 4, 8, 8, 8 };
ArrayBufferObject* ArrayBufferObject::allocateArrayBuffer(ExecutionState& state, Object* constructor, uint64_t byteLength, Optional<uint64_t> maxByteLength)
{
// https://www.ecma-international.org/ecma-262/10.0/#sec-allocatearraybuffer
Object* proto = Object::getPrototypeFromConstructor(state, constructor, [](ExecutionState& state, Context* constructorRealm) -> Object* {
return constructorRealm->globalObject()->arrayBufferPrototype();
});
if (UNLIKELY(byteLength >= ArrayBuffer::maxArrayBufferSize)) {
ErrorObject::throwBuiltinError(state, ErrorObject::RangeError, state.context()->staticStrings().ArrayBuffer.string(), false, String::emptyString, ErrorObject::Messages::GlobalObject_InvalidArrayBufferSize);
}
ArrayBufferObject* obj = new ArrayBufferObject(state, proto);
if (UNLIKELY(maxByteLength.hasValue())) {
ASSERT(byteLength <= maxByteLength.value());
uint64_t maxLength = maxByteLength.value();
if (UNLIKELY(maxLength >= ArrayBuffer::maxArrayBufferSize)) {
ErrorObject::throwBuiltinError(state, ErrorObject::RangeError, state.context()->staticStrings().ArrayBuffer.string(), false, String::emptyString, ErrorObject::Messages::GlobalObject_InvalidArrayBufferSize);
}
// allocate default resizable non-shared BackingStore
obj->allocateResizableBuffer(state, byteLength, maxLength);
return obj;
}
// allocate default non-shared BackingStore
obj->allocateBuffer(state, byteLength);
return obj;
}
ArrayBufferObject* ArrayBufferObject::cloneArrayBuffer(ExecutionState& state, ArrayBuffer* srcBuffer, size_t srcByteOffset, uint64_t srcLength, Object* constructor)
{
// https://www.ecma-international.org/ecma-262/10.0/#sec-clonearraybuffer
ASSERT(constructor->isConstructor());
ArrayBufferObject* targetBuffer = ArrayBufferObject::allocateArrayBuffer(state, constructor, srcLength);
srcBuffer->throwTypeErrorIfDetached(state);
targetBuffer->fillData(srcBuffer->data() + srcByteOffset, srcLength);
return targetBuffer;
}
ArrayBufferObject::ArrayBufferObject(ExecutionState& state)
: ArrayBufferObject(state, state.context()->globalObject()->arrayBufferPrototype())
{
}
ArrayBufferObject::ArrayBufferObject(ExecutionState& state, Object* proto)
: ArrayBuffer(state, proto)
{
}
void ArrayBufferObject::allocateBuffer(ExecutionState& state, size_t byteLength)
{
detachArrayBuffer();
ASSERT(byteLength < ArrayBuffer::maxArrayBufferSize);
const size_t ratio = std::max((size_t)GC_get_free_space_divisor() / 6, (size_t)1);
if (byteLength > (GC_get_heap_size() / ratio)) {
size_t n = 0;
size_t times = byteLength / (GC_get_heap_size() / ratio) / 3;
do {
GC_gcollect_and_unmap();
n += 1;
} while (n < times);
GC_invoke_finalizers();
}
updateBackingStore(BackingStore::createDefaultNonSharedBackingStore(byteLength));
}
void ArrayBufferObject::allocateResizableBuffer(ExecutionState& state, size_t byteLength, size_t maxByteLength)
{
detachArrayBuffer();
ASSERT(byteLength <= maxByteLength);
ASSERT(maxByteLength < ArrayBuffer::maxArrayBufferSize);
const size_t ratio = std::max((size_t)GC_get_free_space_divisor() / 6, (size_t)1);
if (maxByteLength > (GC_get_heap_size() / ratio)) {
size_t n = 0;
size_t times = maxByteLength / (GC_get_heap_size() / ratio) / 3;
do {
GC_gcollect_and_unmap();
n += 1;
} while (n < times);
GC_invoke_finalizers();
}
updateBackingStore(BackingStore::createDefaultResizableNonSharedBackingStore(byteLength, maxByteLength));
}
void ArrayBufferObject::attachBuffer(BackingStore* backingStore)
{
// BackingStore should not be Shared Data Block
ASSERT(!backingStore->isShared());
detachArrayBuffer();
updateBackingStore(backingStore);
}
void ArrayBufferObject::detachArrayBuffer()
{
updateBackingStore(nullptr);
}
void* ArrayBufferObject::operator new(size_t size)
{
#ifdef NDEBUG
static MAY_THREAD_LOCAL bool typeInited = false;
static MAY_THREAD_LOCAL GC_descr descr;
if (!typeInited) {
GC_word obj_bitmap[GC_BITMAP_SIZE(ArrayBufferObject)] = { 0 };
ArrayBuffer::fillGCDescriptor(obj_bitmap);
descr = GC_make_descriptor(obj_bitmap, GC_WORD_LEN(ArrayBufferObject));
typeInited = true;
}
return GC_MALLOC_EXPLICITLY_TYPED(size, descr);
#else
return CustomAllocator<ArrayBufferObject>().allocate(1);
#endif
}
Value ArrayBufferObject::getValueFromBuffer(ExecutionState& state, size_t byteindex, TypedArrayType type, bool isLittleEndian)
{
// If isLittleEndian is not present, set isLittleEndian to either true or false.
ASSERT(byteLength());
size_t elemSize = TypedArrayHelper::elementSize(type);
ASSERT(byteindex + elemSize <= byteLength());
uint8_t* rawStart = data() + byteindex;
if (LIKELY(isLittleEndian)) {
return TypedArrayHelper::rawBytesToNumber(state, type, rawStart);
} else {
uint8_t* rawBytes = ALLOCA(8, uint8_t, state);
for (size_t i = 0; i < elemSize; i++) {
rawBytes[elemSize - i - 1] = rawStart[i];
}
return TypedArrayHelper::rawBytesToNumber(state, type, rawBytes);
}
}
void ArrayBufferObject::setValueInBuffer(ExecutionState& state, size_t byteindex, TypedArrayType type, const Value& val, bool isLittleEndian)
{
// If isLittleEndian is not present, set isLittleEndian to either true or false.
ASSERT(byteLength());
size_t elemSize = TypedArrayHelper::elementSize(type);
ASSERT(byteindex + elemSize <= byteLength());
uint8_t* rawStart = data() + byteindex;
uint8_t* rawBytes = ALLOCA(8, uint8_t, state);
TypedArrayHelper::numberToRawBytes(state, type, val, rawBytes);
if (LIKELY(isLittleEndian)) {
memcpy(rawStart, rawBytes, elemSize);
} else {
for (size_t i = 0; i < elemSize; i++) {
rawStart[i] = rawBytes[elemSize - i - 1];
}
}
}
} // namespace Escargot