1 |
|
|
#include "base_object-inl.h" |
2 |
|
|
#include "node_buffer.h" |
3 |
|
|
#include "node_errors.h" |
4 |
|
|
#include "node_external_reference.h" |
5 |
|
|
#include "node_internals.h" |
6 |
|
|
#include "util-inl.h" |
7 |
|
|
|
8 |
|
|
namespace node { |
9 |
|
|
|
10 |
|
|
using v8::Array; |
11 |
|
|
using v8::ArrayBuffer; |
12 |
|
|
using v8::Context; |
13 |
|
|
using v8::Function; |
14 |
|
|
using v8::FunctionCallbackInfo; |
15 |
|
|
using v8::FunctionTemplate; |
16 |
|
|
using v8::Integer; |
17 |
|
|
using v8::Isolate; |
18 |
|
|
using v8::Just; |
19 |
|
|
using v8::Local; |
20 |
|
|
using v8::Maybe; |
21 |
|
|
using v8::MaybeLocal; |
22 |
|
|
using v8::Nothing; |
23 |
|
|
using v8::Object; |
24 |
|
|
using v8::SharedArrayBuffer; |
25 |
|
|
using v8::String; |
26 |
|
|
using v8::Value; |
27 |
|
|
using v8::ValueDeserializer; |
28 |
|
|
using v8::ValueSerializer; |
29 |
|
|
|
30 |
|
|
namespace serdes { |
31 |
|
|
|
32 |
|
|
class SerializerContext : public BaseObject, |
33 |
|
|
public ValueSerializer::Delegate { |
34 |
|
|
public: |
35 |
|
|
SerializerContext(Environment* env, |
36 |
|
|
Local<Object> wrap); |
37 |
|
|
|
38 |
|
4003508 |
~SerializerContext() override = default; |
39 |
|
|
|
40 |
|
|
void ThrowDataCloneError(Local<String> message) override; |
41 |
|
|
Maybe<bool> WriteHostObject(Isolate* isolate, Local<Object> object) override; |
42 |
|
|
Maybe<uint32_t> GetSharedArrayBufferId( |
43 |
|
|
Isolate* isolate, Local<SharedArrayBuffer> shared_array_buffer) override; |
44 |
|
|
|
45 |
|
|
static void SetTreatArrayBufferViewsAsHostObjects( |
46 |
|
|
const FunctionCallbackInfo<Value>& args); |
47 |
|
|
|
48 |
|
|
static void New(const FunctionCallbackInfo<Value>& args); |
49 |
|
|
static void WriteHeader(const FunctionCallbackInfo<Value>& args); |
50 |
|
|
static void WriteValue(const FunctionCallbackInfo<Value>& args); |
51 |
|
|
static void ReleaseBuffer(const FunctionCallbackInfo<Value>& args); |
52 |
|
|
static void TransferArrayBuffer(const FunctionCallbackInfo<Value>& args); |
53 |
|
|
static void WriteUint32(const FunctionCallbackInfo<Value>& args); |
54 |
|
|
static void WriteUint64(const FunctionCallbackInfo<Value>& args); |
55 |
|
|
static void WriteDouble(const FunctionCallbackInfo<Value>& args); |
56 |
|
|
static void WriteRawBytes(const FunctionCallbackInfo<Value>& args); |
57 |
|
|
|
58 |
|
|
SET_NO_MEMORY_INFO() |
59 |
|
|
SET_MEMORY_INFO_NAME(SerializerContext) |
60 |
|
|
SET_SELF_SIZE(SerializerContext) |
61 |
|
|
|
62 |
|
|
private: |
63 |
|
|
ValueSerializer serializer_; |
64 |
|
|
}; |
65 |
|
|
|
66 |
|
|
class DeserializerContext : public BaseObject, |
67 |
|
|
public ValueDeserializer::Delegate { |
68 |
|
|
public: |
69 |
|
|
DeserializerContext(Environment* env, |
70 |
|
|
Local<Object> wrap, |
71 |
|
|
Local<Value> buffer); |
72 |
|
|
|
73 |
|
3636 |
~DeserializerContext() override = default; |
74 |
|
|
|
75 |
|
|
MaybeLocal<Object> ReadHostObject(Isolate* isolate) override; |
76 |
|
|
|
77 |
|
|
static void New(const FunctionCallbackInfo<Value>& args); |
78 |
|
|
static void ReadHeader(const FunctionCallbackInfo<Value>& args); |
79 |
|
|
static void ReadValue(const FunctionCallbackInfo<Value>& args); |
80 |
|
|
static void TransferArrayBuffer(const FunctionCallbackInfo<Value>& args); |
81 |
|
|
static void GetWireFormatVersion(const FunctionCallbackInfo<Value>& args); |
82 |
|
|
static void ReadUint32(const FunctionCallbackInfo<Value>& args); |
83 |
|
|
static void ReadUint64(const FunctionCallbackInfo<Value>& args); |
84 |
|
|
static void ReadDouble(const FunctionCallbackInfo<Value>& args); |
85 |
|
|
static void ReadRawBytes(const FunctionCallbackInfo<Value>& args); |
86 |
|
|
|
87 |
|
|
SET_NO_MEMORY_INFO() |
88 |
|
|
SET_MEMORY_INFO_NAME(DeserializerContext) |
89 |
|
|
SET_SELF_SIZE(DeserializerContext) |
90 |
|
|
|
91 |
|
|
private: |
92 |
|
|
const uint8_t* data_; |
93 |
|
|
const size_t length_; |
94 |
|
|
|
95 |
|
|
ValueDeserializer deserializer_; |
96 |
|
|
}; |
97 |
|
|
|
98 |
|
1000877 |
SerializerContext::SerializerContext(Environment* env, Local<Object> wrap) |
99 |
|
|
: BaseObject(env, wrap), |
100 |
|
1000877 |
serializer_(env->isolate(), this) { |
101 |
|
1000877 |
MakeWeak(); |
102 |
|
1000877 |
} |
103 |
|
|
|
104 |
|
2 |
void SerializerContext::ThrowDataCloneError(Local<String> message) { |
105 |
|
2 |
Local<Value> args[1] = { message }; |
106 |
|
|
Local<Value> get_data_clone_error = |
107 |
|
2 |
object()->Get(env()->context(), |
108 |
|
4 |
env()->get_data_clone_error_string()) |
109 |
|
2 |
.ToLocalChecked(); |
110 |
|
|
|
111 |
✗✓ |
2 |
CHECK(get_data_clone_error->IsFunction()); |
112 |
|
|
MaybeLocal<Value> error = |
113 |
|
2 |
get_data_clone_error.As<Function>()->Call(env()->context(), |
114 |
|
|
object(), |
115 |
|
2 |
arraysize(args), |
116 |
|
4 |
args); |
117 |
|
|
|
118 |
✗✓ |
2 |
if (error.IsEmpty()) return; |
119 |
|
|
|
120 |
|
4 |
env()->isolate()->ThrowException(error.ToLocalChecked()); |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
Maybe<uint32_t> SerializerContext::GetSharedArrayBufferId( |
124 |
|
|
Isolate* isolate, Local<SharedArrayBuffer> shared_array_buffer) { |
125 |
|
|
Local<Value> args[1] = { shared_array_buffer }; |
126 |
|
|
Local<Value> get_shared_array_buffer_id = |
127 |
|
|
object()->Get(env()->context(), |
128 |
|
|
env()->get_shared_array_buffer_id_string()) |
129 |
|
|
.ToLocalChecked(); |
130 |
|
|
|
131 |
|
|
if (!get_shared_array_buffer_id->IsFunction()) { |
132 |
|
|
return ValueSerializer::Delegate::GetSharedArrayBufferId( |
133 |
|
|
isolate, shared_array_buffer); |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
MaybeLocal<Value> id = |
137 |
|
|
get_shared_array_buffer_id.As<Function>()->Call(env()->context(), |
138 |
|
|
object(), |
139 |
|
|
arraysize(args), |
140 |
|
|
args); |
141 |
|
|
|
142 |
|
|
if (id.IsEmpty()) return Nothing<uint32_t>(); |
143 |
|
|
|
144 |
|
|
return id.ToLocalChecked()->Uint32Value(env()->context()); |
145 |
|
|
} |
146 |
|
|
|
147 |
|
46 |
Maybe<bool> SerializerContext::WriteHostObject(Isolate* isolate, |
148 |
|
|
Local<Object> input) { |
149 |
|
|
MaybeLocal<Value> ret; |
150 |
|
46 |
Local<Value> args[1] = { input }; |
151 |
|
|
|
152 |
|
|
Local<Value> write_host_object = |
153 |
|
46 |
object()->Get(env()->context(), |
154 |
|
138 |
env()->write_host_object_string()).ToLocalChecked(); |
155 |
|
|
|
156 |
✗✓ |
46 |
if (!write_host_object->IsFunction()) { |
157 |
|
|
return ValueSerializer::Delegate::WriteHostObject(isolate, input); |
158 |
|
|
} |
159 |
|
|
|
160 |
|
46 |
ret = write_host_object.As<Function>()->Call(env()->context(), |
161 |
|
|
object(), |
162 |
|
46 |
arraysize(args), |
163 |
|
92 |
args); |
164 |
|
|
|
165 |
✓✓ |
46 |
if (ret.IsEmpty()) |
166 |
|
2 |
return Nothing<bool>(); |
167 |
|
|
|
168 |
|
44 |
return Just(true); |
169 |
|
|
} |
170 |
|
|
|
171 |
|
1000878 |
void SerializerContext::New(const FunctionCallbackInfo<Value>& args) { |
172 |
|
1000878 |
Environment* env = Environment::GetCurrent(args); |
173 |
✓✓ |
1000878 |
if (!args.IsConstructCall()) { |
174 |
|
1 |
return THROW_ERR_CONSTRUCT_CALL_REQUIRED( |
175 |
|
1 |
env, "Class constructor Serializer cannot be invoked without 'new'"); |
176 |
|
|
} |
177 |
|
|
|
178 |
|
1000877 |
new SerializerContext(env, args.This()); |
179 |
|
|
} |
180 |
|
|
|
181 |
|
1000876 |
void SerializerContext::WriteHeader(const FunctionCallbackInfo<Value>& args) { |
182 |
|
|
SerializerContext* ctx; |
183 |
✗✓ |
1000876 |
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder()); |
184 |
|
1000876 |
ctx->serializer_.WriteHeader(); |
185 |
|
|
} |
186 |
|
|
|
187 |
|
1000884 |
void SerializerContext::WriteValue(const FunctionCallbackInfo<Value>& args) { |
188 |
|
|
SerializerContext* ctx; |
189 |
✗✓ |
1000884 |
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder()); |
190 |
|
|
Maybe<bool> ret = |
191 |
✓✗ |
2001768 |
ctx->serializer_.WriteValue(ctx->env()->context(), args[0]); |
192 |
|
|
|
193 |
✓✓✓✗
|
4003524 |
if (ret.IsJust()) args.GetReturnValue().Set(ret.FromJust()); |
194 |
|
|
} |
195 |
|
|
|
196 |
|
1000875 |
void SerializerContext::SetTreatArrayBufferViewsAsHostObjects( |
197 |
|
|
const FunctionCallbackInfo<Value>& args) { |
198 |
|
|
SerializerContext* ctx; |
199 |
✗✓ |
1000875 |
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder()); |
200 |
|
|
|
201 |
|
1000875 |
bool value = args[0]->BooleanValue(ctx->env()->isolate()); |
202 |
|
1000875 |
ctx->serializer_.SetTreatArrayBufferViewsAsHostObjects(value); |
203 |
|
|
} |
204 |
|
|
|
205 |
|
1000872 |
void SerializerContext::ReleaseBuffer(const FunctionCallbackInfo<Value>& args) { |
206 |
|
|
SerializerContext* ctx; |
207 |
✗✓ |
1000872 |
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder()); |
208 |
|
|
|
209 |
|
|
// Note: Both ValueSerializer and this Buffer::New() variant use malloc() |
210 |
|
|
// as the underlying allocator. |
211 |
|
1000872 |
std::pair<uint8_t*, size_t> ret = ctx->serializer_.Release(); |
212 |
|
|
auto buf = Buffer::New(ctx->env(), |
213 |
|
1000872 |
reinterpret_cast<char*>(ret.first), |
214 |
|
1000872 |
ret.second); |
215 |
|
|
|
216 |
✓✗ |
1000872 |
if (!buf.IsEmpty()) { |
217 |
✗✓ |
2001744 |
args.GetReturnValue().Set(buf.ToLocalChecked()); |
218 |
|
|
} |
219 |
|
|
} |
220 |
|
|
|
221 |
|
|
void SerializerContext::TransferArrayBuffer( |
222 |
|
|
const FunctionCallbackInfo<Value>& args) { |
223 |
|
|
SerializerContext* ctx; |
224 |
|
|
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder()); |
225 |
|
|
|
226 |
|
|
Maybe<uint32_t> id = args[0]->Uint32Value(ctx->env()->context()); |
227 |
|
|
if (id.IsNothing()) return; |
228 |
|
|
|
229 |
|
|
if (!args[1]->IsArrayBuffer()) |
230 |
|
|
return node::THROW_ERR_INVALID_ARG_TYPE( |
231 |
|
|
ctx->env(), "arrayBuffer must be an ArrayBuffer"); |
232 |
|
|
|
233 |
|
|
Local<ArrayBuffer> ab = args[1].As<ArrayBuffer>(); |
234 |
|
|
ctx->serializer_.TransferArrayBuffer(id.FromJust(), ab); |
235 |
|
|
return; |
236 |
|
|
} |
237 |
|
|
|
238 |
|
93 |
void SerializerContext::WriteUint32(const FunctionCallbackInfo<Value>& args) { |
239 |
|
|
SerializerContext* ctx; |
240 |
✗✓ |
93 |
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder()); |
241 |
|
|
|
242 |
|
93 |
Maybe<uint32_t> value = args[0]->Uint32Value(ctx->env()->context()); |
243 |
✗✓ |
93 |
if (value.IsNothing()) return; |
244 |
|
|
|
245 |
|
186 |
ctx->serializer_.WriteUint32(value.FromJust()); |
246 |
|
|
} |
247 |
|
|
|
248 |
|
1 |
void SerializerContext::WriteUint64(const FunctionCallbackInfo<Value>& args) { |
249 |
|
|
SerializerContext* ctx; |
250 |
✗✓ |
1 |
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder()); |
251 |
|
|
|
252 |
✓✗ |
1 |
Maybe<uint32_t> arg0 = args[0]->Uint32Value(ctx->env()->context()); |
253 |
|
1 |
Maybe<uint32_t> arg1 = args[1]->Uint32Value(ctx->env()->context()); |
254 |
✓✗✗✓ ✗✓ |
2 |
if (arg0.IsNothing() || arg1.IsNothing()) |
255 |
|
|
return; |
256 |
|
|
|
257 |
|
1 |
uint64_t hi = arg0.FromJust(); |
258 |
|
1 |
uint64_t lo = arg1.FromJust(); |
259 |
|
1 |
ctx->serializer_.WriteUint64((hi << 32) | lo); |
260 |
|
|
} |
261 |
|
|
|
262 |
|
1 |
void SerializerContext::WriteDouble(const FunctionCallbackInfo<Value>& args) { |
263 |
|
|
SerializerContext* ctx; |
264 |
✗✓ |
1 |
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder()); |
265 |
|
|
|
266 |
|
1 |
Maybe<double> value = args[0]->NumberValue(ctx->env()->context()); |
267 |
✗✓ |
1 |
if (value.IsNothing()) return; |
268 |
|
|
|
269 |
|
2 |
ctx->serializer_.WriteDouble(value.FromJust()); |
270 |
|
|
} |
271 |
|
|
|
272 |
|
45 |
void SerializerContext::WriteRawBytes(const FunctionCallbackInfo<Value>& args) { |
273 |
|
|
SerializerContext* ctx; |
274 |
✗✓ |
46 |
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder()); |
275 |
|
|
|
276 |
✓✓ |
45 |
if (!args[0]->IsArrayBufferView()) { |
277 |
|
1 |
return node::THROW_ERR_INVALID_ARG_TYPE( |
278 |
|
1 |
ctx->env(), "source must be a TypedArray or a DataView"); |
279 |
|
|
} |
280 |
|
|
|
281 |
|
44 |
ArrayBufferViewContents<char> bytes(args[0]); |
282 |
|
44 |
ctx->serializer_.WriteRawBytes(bytes.data(), bytes.length()); |
283 |
|
|
} |
284 |
|
|
|
285 |
|
909 |
DeserializerContext::DeserializerContext(Environment* env, |
286 |
|
|
Local<Object> wrap, |
287 |
|
909 |
Local<Value> buffer) |
288 |
|
|
: BaseObject(env, wrap), |
289 |
|
909 |
data_(reinterpret_cast<const uint8_t*>(Buffer::Data(buffer))), |
290 |
|
909 |
length_(Buffer::Length(buffer)), |
291 |
|
909 |
deserializer_(env->isolate(), data_, length_, this) { |
292 |
|
2727 |
object()->Set(env->context(), env->buffer_string(), buffer).Check(); |
293 |
|
|
|
294 |
|
909 |
MakeWeak(); |
295 |
|
909 |
} |
296 |
|
|
|
297 |
|
57 |
MaybeLocal<Object> DeserializerContext::ReadHostObject(Isolate* isolate) { |
298 |
|
|
Local<Value> read_host_object = |
299 |
|
57 |
object()->Get(env()->context(), |
300 |
|
171 |
env()->read_host_object_string()).ToLocalChecked(); |
301 |
|
|
|
302 |
✗✓ |
57 |
if (!read_host_object->IsFunction()) { |
303 |
|
|
return ValueDeserializer::Delegate::ReadHostObject(isolate); |
304 |
|
|
} |
305 |
|
|
|
306 |
|
114 |
Isolate::AllowJavascriptExecutionScope allow_js(isolate); |
307 |
|
|
MaybeLocal<Value> ret = |
308 |
|
57 |
read_host_object.As<Function>()->Call(env()->context(), |
309 |
|
|
object(), |
310 |
|
|
0, |
311 |
|
114 |
nullptr); |
312 |
|
|
|
313 |
✗✓ |
57 |
if (ret.IsEmpty()) |
314 |
|
|
return MaybeLocal<Object>(); |
315 |
|
|
|
316 |
|
57 |
Local<Value> return_value = ret.ToLocalChecked(); |
317 |
✗✓ |
57 |
if (!return_value->IsObject()) { |
318 |
|
|
env()->ThrowTypeError("readHostObject must return an object"); |
319 |
|
|
return MaybeLocal<Object>(); |
320 |
|
|
} |
321 |
|
|
|
322 |
|
57 |
return return_value.As<Object>(); |
323 |
|
|
} |
324 |
|
|
|
325 |
|
912 |
void DeserializerContext::New(const FunctionCallbackInfo<Value>& args) { |
326 |
|
912 |
Environment* env = Environment::GetCurrent(args); |
327 |
✓✓ |
912 |
if (!args.IsConstructCall()) { |
328 |
|
1 |
return THROW_ERR_CONSTRUCT_CALL_REQUIRED( |
329 |
|
1 |
env, "Class constructor Deserializer cannot be invoked without 'new'"); |
330 |
|
|
} |
331 |
|
|
|
332 |
✓✓ |
911 |
if (!args[0]->IsArrayBufferView()) { |
333 |
|
2 |
return node::THROW_ERR_INVALID_ARG_TYPE( |
334 |
|
2 |
env, "buffer must be a TypedArray or a DataView"); |
335 |
|
|
} |
336 |
|
|
|
337 |
✓✗ |
1818 |
new DeserializerContext(env, args.This(), args[0]); |
338 |
|
|
} |
339 |
|
|
|
340 |
|
908 |
void DeserializerContext::ReadHeader(const FunctionCallbackInfo<Value>& args) { |
341 |
|
|
DeserializerContext* ctx; |
342 |
✗✓ |
908 |
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder()); |
343 |
|
|
|
344 |
|
908 |
Maybe<bool> ret = ctx->deserializer_.ReadHeader(ctx->env()->context()); |
345 |
|
|
|
346 |
✓✗✓✗
|
3632 |
if (ret.IsJust()) args.GetReturnValue().Set(ret.FromJust()); |
347 |
|
|
} |
348 |
|
|
|
349 |
|
917 |
void DeserializerContext::ReadValue(const FunctionCallbackInfo<Value>& args) { |
350 |
|
|
DeserializerContext* ctx; |
351 |
✗✓ |
917 |
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder()); |
352 |
|
|
|
353 |
|
917 |
MaybeLocal<Value> ret = ctx->deserializer_.ReadValue(ctx->env()->context()); |
354 |
|
|
|
355 |
✓✗✗✓
|
2751 |
if (!ret.IsEmpty()) args.GetReturnValue().Set(ret.ToLocalChecked()); |
356 |
|
|
} |
357 |
|
|
|
358 |
|
|
void DeserializerContext::TransferArrayBuffer( |
359 |
|
|
const FunctionCallbackInfo<Value>& args) { |
360 |
|
|
DeserializerContext* ctx; |
361 |
|
|
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder()); |
362 |
|
|
|
363 |
|
|
Maybe<uint32_t> id = args[0]->Uint32Value(ctx->env()->context()); |
364 |
|
|
if (id.IsNothing()) return; |
365 |
|
|
|
366 |
|
|
if (args[1]->IsArrayBuffer()) { |
367 |
|
|
Local<ArrayBuffer> ab = args[1].As<ArrayBuffer>(); |
368 |
|
|
ctx->deserializer_.TransferArrayBuffer(id.FromJust(), ab); |
369 |
|
|
return; |
370 |
|
|
} |
371 |
|
|
|
372 |
|
|
if (args[1]->IsSharedArrayBuffer()) { |
373 |
|
|
Local<SharedArrayBuffer> sab = args[1].As<SharedArrayBuffer>(); |
374 |
|
|
ctx->deserializer_.TransferSharedArrayBuffer(id.FromJust(), sab); |
375 |
|
|
return; |
376 |
|
|
} |
377 |
|
|
|
378 |
|
|
return node::THROW_ERR_INVALID_ARG_TYPE( |
379 |
|
|
ctx->env(), "arrayBuffer must be an ArrayBuffer or SharedArrayBuffer"); |
380 |
|
|
} |
381 |
|
|
|
382 |
|
1 |
void DeserializerContext::GetWireFormatVersion( |
383 |
|
|
const FunctionCallbackInfo<Value>& args) { |
384 |
|
|
DeserializerContext* ctx; |
385 |
✗✓ |
1 |
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder()); |
386 |
|
|
|
387 |
|
2 |
args.GetReturnValue().Set(ctx->deserializer_.GetWireFormatVersion()); |
388 |
|
|
} |
389 |
|
|
|
390 |
|
119 |
void DeserializerContext::ReadUint32(const FunctionCallbackInfo<Value>& args) { |
391 |
|
|
DeserializerContext* ctx; |
392 |
✗✓ |
119 |
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder()); |
393 |
|
|
|
394 |
|
|
uint32_t value; |
395 |
|
119 |
bool ok = ctx->deserializer_.ReadUint32(&value); |
396 |
✗✓ |
119 |
if (!ok) return ctx->env()->ThrowError("ReadUint32() failed"); |
397 |
✓✗ |
238 |
return args.GetReturnValue().Set(value); |
398 |
|
|
} |
399 |
|
|
|
400 |
|
1 |
void DeserializerContext::ReadUint64(const FunctionCallbackInfo<Value>& args) { |
401 |
|
|
DeserializerContext* ctx; |
402 |
✗✓ |
1 |
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder()); |
403 |
|
|
|
404 |
|
|
uint64_t value; |
405 |
|
1 |
bool ok = ctx->deserializer_.ReadUint64(&value); |
406 |
✗✓ |
1 |
if (!ok) return ctx->env()->ThrowError("ReadUint64() failed"); |
407 |
|
|
|
408 |
|
1 |
uint32_t hi = static_cast<uint32_t>(value >> 32); |
409 |
|
1 |
uint32_t lo = static_cast<uint32_t>(value); |
410 |
|
|
|
411 |
|
1 |
Isolate* isolate = ctx->env()->isolate(); |
412 |
|
|
|
413 |
|
|
Local<Value> ret[] = { |
414 |
|
|
Integer::NewFromUnsigned(isolate, hi), |
415 |
|
|
Integer::NewFromUnsigned(isolate, lo) |
416 |
|
3 |
}; |
417 |
|
2 |
return args.GetReturnValue().Set(Array::New(isolate, ret, arraysize(ret))); |
418 |
|
|
} |
419 |
|
|
|
420 |
|
2 |
void DeserializerContext::ReadDouble(const FunctionCallbackInfo<Value>& args) { |
421 |
|
|
DeserializerContext* ctx; |
422 |
✗✓ |
2 |
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder()); |
423 |
|
|
|
424 |
|
|
double value; |
425 |
|
2 |
bool ok = ctx->deserializer_.ReadDouble(&value); |
426 |
✓✓ |
2 |
if (!ok) return ctx->env()->ThrowError("ReadDouble() failed"); |
427 |
|
2 |
return args.GetReturnValue().Set(value); |
428 |
|
|
} |
429 |
|
|
|
430 |
|
57 |
void DeserializerContext::ReadRawBytes( |
431 |
|
|
const FunctionCallbackInfo<Value>& args) { |
432 |
|
|
DeserializerContext* ctx; |
433 |
✗✓ |
57 |
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder()); |
434 |
|
|
|
435 |
|
57 |
Maybe<int64_t> length_arg = args[0]->IntegerValue(ctx->env()->context()); |
436 |
✗✓ |
57 |
if (length_arg.IsNothing()) return; |
437 |
|
57 |
size_t length = length_arg.FromJust(); |
438 |
|
|
|
439 |
|
|
const void* data; |
440 |
|
57 |
bool ok = ctx->deserializer_.ReadRawBytes(length, &data); |
441 |
✗✓ |
57 |
if (!ok) return ctx->env()->ThrowError("ReadRawBytes() failed"); |
442 |
|
|
|
443 |
|
57 |
const uint8_t* position = reinterpret_cast<const uint8_t*>(data); |
444 |
✗✓ |
57 |
CHECK_GE(position, ctx->data_); |
445 |
✗✓ |
57 |
CHECK_LE(position + length, ctx->data_ + ctx->length_); |
446 |
|
|
|
447 |
|
57 |
const uint32_t offset = static_cast<uint32_t>(position - ctx->data_); |
448 |
✗✓ |
57 |
CHECK_EQ(ctx->data_ + offset, position); |
449 |
|
|
|
450 |
✓✗ |
114 |
args.GetReturnValue().Set(offset); |
451 |
|
|
} |
452 |
|
|
|
453 |
|
844 |
void Initialize(Local<Object> target, |
454 |
|
|
Local<Value> unused, |
455 |
|
|
Local<Context> context, |
456 |
|
|
void* priv) { |
457 |
|
844 |
Environment* env = Environment::GetCurrent(context); |
458 |
|
|
Local<FunctionTemplate> ser = |
459 |
|
844 |
env->NewFunctionTemplate(SerializerContext::New); |
460 |
|
|
|
461 |
|
1688 |
ser->InstanceTemplate()->SetInternalFieldCount( |
462 |
|
|
SerializerContext::kInternalFieldCount); |
463 |
|
844 |
ser->Inherit(BaseObject::GetConstructorTemplate(env)); |
464 |
|
|
|
465 |
|
844 |
env->SetProtoMethod(ser, "writeHeader", SerializerContext::WriteHeader); |
466 |
|
844 |
env->SetProtoMethod(ser, "writeValue", SerializerContext::WriteValue); |
467 |
|
844 |
env->SetProtoMethod(ser, "releaseBuffer", SerializerContext::ReleaseBuffer); |
468 |
|
844 |
env->SetProtoMethod(ser, |
469 |
|
|
"transferArrayBuffer", |
470 |
|
|
SerializerContext::TransferArrayBuffer); |
471 |
|
844 |
env->SetProtoMethod(ser, "writeUint32", SerializerContext::WriteUint32); |
472 |
|
844 |
env->SetProtoMethod(ser, "writeUint64", SerializerContext::WriteUint64); |
473 |
|
844 |
env->SetProtoMethod(ser, "writeDouble", SerializerContext::WriteDouble); |
474 |
|
844 |
env->SetProtoMethod(ser, "writeRawBytes", SerializerContext::WriteRawBytes); |
475 |
|
844 |
env->SetProtoMethod(ser, |
476 |
|
|
"_setTreatArrayBufferViewsAsHostObjects", |
477 |
|
|
SerializerContext::SetTreatArrayBufferViewsAsHostObjects); |
478 |
|
|
|
479 |
|
844 |
ser->ReadOnlyPrototype(); |
480 |
|
844 |
env->SetConstructorFunction(target, "Serializer", ser); |
481 |
|
|
|
482 |
|
|
Local<FunctionTemplate> des = |
483 |
|
844 |
env->NewFunctionTemplate(DeserializerContext::New); |
484 |
|
|
|
485 |
|
1688 |
des->InstanceTemplate()->SetInternalFieldCount( |
486 |
|
|
DeserializerContext::kInternalFieldCount); |
487 |
|
844 |
des->Inherit(BaseObject::GetConstructorTemplate(env)); |
488 |
|
|
|
489 |
|
844 |
env->SetProtoMethod(des, "readHeader", DeserializerContext::ReadHeader); |
490 |
|
844 |
env->SetProtoMethod(des, "readValue", DeserializerContext::ReadValue); |
491 |
|
844 |
env->SetProtoMethod(des, |
492 |
|
|
"getWireFormatVersion", |
493 |
|
|
DeserializerContext::GetWireFormatVersion); |
494 |
|
844 |
env->SetProtoMethod(des, |
495 |
|
|
"transferArrayBuffer", |
496 |
|
|
DeserializerContext::TransferArrayBuffer); |
497 |
|
844 |
env->SetProtoMethod(des, "readUint32", DeserializerContext::ReadUint32); |
498 |
|
844 |
env->SetProtoMethod(des, "readUint64", DeserializerContext::ReadUint64); |
499 |
|
844 |
env->SetProtoMethod(des, "readDouble", DeserializerContext::ReadDouble); |
500 |
|
844 |
env->SetProtoMethod(des, "_readRawBytes", DeserializerContext::ReadRawBytes); |
501 |
|
|
|
502 |
|
844 |
des->SetLength(1); |
503 |
|
844 |
des->ReadOnlyPrototype(); |
504 |
|
844 |
env->SetConstructorFunction(target, "Deserializer", des); |
505 |
|
844 |
} |
506 |
|
|
|
507 |
|
5111 |
void RegisterExternalReferences(ExternalReferenceRegistry* registry) { |
508 |
|
5111 |
registry->Register(SerializerContext::New); |
509 |
|
|
|
510 |
|
5111 |
registry->Register(SerializerContext::WriteHeader); |
511 |
|
5111 |
registry->Register(SerializerContext::WriteValue); |
512 |
|
5111 |
registry->Register(SerializerContext::ReleaseBuffer); |
513 |
|
5111 |
registry->Register(SerializerContext::TransferArrayBuffer); |
514 |
|
5111 |
registry->Register(SerializerContext::WriteUint32); |
515 |
|
5111 |
registry->Register(SerializerContext::WriteUint64); |
516 |
|
5111 |
registry->Register(SerializerContext::WriteDouble); |
517 |
|
5111 |
registry->Register(SerializerContext::WriteRawBytes); |
518 |
|
5111 |
registry->Register(SerializerContext::SetTreatArrayBufferViewsAsHostObjects); |
519 |
|
|
|
520 |
|
5111 |
registry->Register(DeserializerContext::New); |
521 |
|
5111 |
registry->Register(DeserializerContext::ReadHeader); |
522 |
|
5111 |
registry->Register(DeserializerContext::ReadValue); |
523 |
|
5111 |
registry->Register(DeserializerContext::GetWireFormatVersion); |
524 |
|
5111 |
registry->Register(DeserializerContext::TransferArrayBuffer); |
525 |
|
5111 |
registry->Register(DeserializerContext::ReadUint32); |
526 |
|
5111 |
registry->Register(DeserializerContext::ReadUint64); |
527 |
|
5111 |
registry->Register(DeserializerContext::ReadDouble); |
528 |
|
5111 |
registry->Register(DeserializerContext::ReadRawBytes); |
529 |
|
5111 |
} |
530 |
|
|
|
531 |
|
|
} // namespace serdes |
532 |
|
|
} // namespace node |
533 |
|
|
|
534 |
|
5169 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL(serdes, node::serdes::Initialize) |
535 |
|
5111 |
NODE_MODULE_EXTERNAL_REFERENCE(serdes, node::serdes::RegisterExternalReferences) |