-
Notifications
You must be signed in to change notification settings - Fork 471
/
Copy patharray_buffer.cc
243 lines (198 loc) · 6.16 KB
/
array_buffer.cc
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "napi.h"
using namespace Napi;
namespace {
const size_t testLength = 4;
uint8_t testData[testLength];
int finalizeCount = 0;
void InitData(uint8_t* data, size_t length) {
for (size_t i = 0; i < length; i++) {
data[i] = static_cast<uint8_t>(i);
}
}
bool VerifyData(uint8_t* data, size_t length) {
for (size_t i = 0; i < length; i++) {
if (data[i] != static_cast<uint8_t>(i)) {
return false;
}
}
return true;
}
Value CreateBuffer(const CallbackInfo& info) {
ArrayBuffer buffer = ArrayBuffer::New(info.Env(), testLength);
if (buffer.ByteLength() != testLength) {
Error::New(info.Env(), "Incorrect buffer length.")
.ThrowAsJavaScriptException();
return Value();
}
InitData(static_cast<uint8_t*>(buffer.Data()), testLength);
return buffer;
}
Value CreateExternalBuffer(const CallbackInfo& info) {
finalizeCount = 0;
ArrayBuffer buffer = ArrayBuffer::New(info.Env(), testData, testLength);
if (buffer.ByteLength() != testLength) {
Error::New(info.Env(), "Incorrect buffer length.")
.ThrowAsJavaScriptException();
return Value();
}
if (buffer.Data() != testData) {
Error::New(info.Env(), "Incorrect buffer data.")
.ThrowAsJavaScriptException();
return Value();
}
InitData(testData, testLength);
return buffer;
}
Value CreateExternalBufferWithFinalize(const CallbackInfo& info) {
finalizeCount = 0;
uint8_t* data = new uint8_t[testLength];
ArrayBuffer buffer = ArrayBuffer::New(
info.Env(), data, testLength, [](Env /*env*/, void* finalizeData) {
delete[] static_cast<uint8_t*>(finalizeData);
finalizeCount++;
});
if (buffer.ByteLength() != testLength) {
Error::New(info.Env(), "Incorrect buffer length.")
.ThrowAsJavaScriptException();
return Value();
}
if (buffer.Data() != data) {
Error::New(info.Env(), "Incorrect buffer data.")
.ThrowAsJavaScriptException();
return Value();
}
InitData(data, testLength);
return buffer;
}
Value CreateExternalBufferWithFinalizeHint(const CallbackInfo& info) {
finalizeCount = 0;
uint8_t* data = new uint8_t[testLength];
char* hint = nullptr;
ArrayBuffer buffer = ArrayBuffer::New(
info.Env(),
data,
testLength,
[](Env /*env*/, void* finalizeData, char* /*finalizeHint*/) {
delete[] static_cast<uint8_t*>(finalizeData);
finalizeCount++;
},
hint);
if (buffer.ByteLength() != testLength) {
Error::New(info.Env(), "Incorrect buffer length.")
.ThrowAsJavaScriptException();
return Value();
}
if (buffer.Data() != data) {
Error::New(info.Env(), "Incorrect buffer data.")
.ThrowAsJavaScriptException();
return Value();
}
InitData(data, testLength);
return buffer;
}
void CheckBuffer(const CallbackInfo& info) {
if (!info[0].IsArrayBuffer()) {
Error::New(info.Env(), "A buffer was expected.")
.ThrowAsJavaScriptException();
return;
}
ArrayBuffer buffer = info[0].As<ArrayBuffer>();
if (buffer.ByteLength() != testLength) {
Error::New(info.Env(), "Incorrect buffer length.")
.ThrowAsJavaScriptException();
return;
}
if (!VerifyData(static_cast<uint8_t*>(buffer.Data()), testLength)) {
Error::New(info.Env(), "Incorrect buffer data.")
.ThrowAsJavaScriptException();
return;
}
}
Value GetFinalizeCount(const CallbackInfo& info) {
return Number::New(info.Env(), finalizeCount);
}
Value CreateBufferWithConstructor(const CallbackInfo& info) {
ArrayBuffer buffer = ArrayBuffer::New(info.Env(), testLength);
if (buffer.ByteLength() != testLength) {
Error::New(info.Env(), "Incorrect buffer length.")
.ThrowAsJavaScriptException();
return Value();
}
InitData(static_cast<uint8_t*>(buffer.Data()), testLength);
ArrayBuffer buffer2(info.Env(), buffer);
return buffer2;
}
Value CheckEmptyBuffer(const CallbackInfo& info) {
ArrayBuffer buffer;
return Boolean::New(info.Env(), buffer.IsEmpty());
}
void CheckDetachUpdatesData(const CallbackInfo& info) {
if (!info[0].IsArrayBuffer()) {
Error::New(info.Env(), "A buffer was expected.")
.ThrowAsJavaScriptException();
return;
}
ArrayBuffer buffer = info[0].As<ArrayBuffer>();
// This potentially causes the buffer to cache its data pointer and length.
buffer.Data();
buffer.ByteLength();
#if NAPI_VERSION >= 7
if (buffer.IsDetached()) {
Error::New(info.Env(), "Buffer should not be detached.")
.ThrowAsJavaScriptException();
return;
}
#endif
if (info.Length() == 2) {
// Detach externally (in JavaScript).
if (!info[1].IsFunction()) {
Error::New(info.Env(), "A function was expected.")
.ThrowAsJavaScriptException();
return;
}
Function detach = info[1].As<Function>();
detach.Call({});
} else {
#if NAPI_VERSION >= 7
// Detach directly.
buffer.Detach();
#else
return;
#endif
}
#if NAPI_VERSION >= 7
if (!buffer.IsDetached()) {
Error::New(info.Env(), "Buffer should be detached.")
.ThrowAsJavaScriptException();
return;
}
#endif
if (buffer.Data() != nullptr) {
Error::New(info.Env(), "Incorrect data pointer.")
.ThrowAsJavaScriptException();
return;
}
if (buffer.ByteLength() != 0) {
Error::New(info.Env(), "Incorrect buffer length.")
.ThrowAsJavaScriptException();
return;
}
}
} // end anonymous namespace
Object InitArrayBuffer(Env env) {
Object exports = Object::New(env);
exports["createBuffer"] = Function::New(env, CreateBuffer);
exports["createExternalBuffer"] = Function::New(env, CreateExternalBuffer);
exports["createExternalBufferWithFinalize"] =
Function::New(env, CreateExternalBufferWithFinalize);
exports["createExternalBufferWithFinalizeHint"] =
Function::New(env, CreateExternalBufferWithFinalizeHint);
exports["checkBuffer"] = Function::New(env, CheckBuffer);
exports["getFinalizeCount"] = Function::New(env, GetFinalizeCount);
exports["createBufferWithConstructor"] =
Function::New(env, CreateBufferWithConstructor);
exports["checkEmptyBuffer"] = Function::New(env, CheckEmptyBuffer);
exports["checkDetachUpdatesData"] =
Function::New(env, CheckDetachUpdatesData);
return exports;
}