GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_serdes.cc Lines: 168 213 78.9 %
Date: 2019-03-02 22:23:06 Branches: 47 104 45.2 %

Line Branch Exec Source
1
#include "node_internals.h"
2
#include "node_buffer.h"
3
#include "node_errors.h"
4
#include "base_object-inl.h"
5
6
namespace node {
7
8
using v8::Array;
9
using v8::ArrayBuffer;
10
using v8::Context;
11
using v8::Function;
12
using v8::FunctionCallbackInfo;
13
using v8::FunctionTemplate;
14
using v8::Integer;
15
using v8::Isolate;
16
using v8::Just;
17
using v8::Local;
18
using v8::Maybe;
19
using v8::MaybeLocal;
20
using v8::Nothing;
21
using v8::Object;
22
using v8::SharedArrayBuffer;
23
using v8::String;
24
using v8::Value;
25
using v8::ValueDeserializer;
26
using v8::ValueSerializer;
27
28
namespace {
29
30
class SerializerContext : public BaseObject,
31
                          public ValueSerializer::Delegate {
32
 public:
33
  SerializerContext(Environment* env,
34
                    Local<Object> wrap);
35
36
330
  ~SerializerContext() override {}
37
38
  void ThrowDataCloneError(Local<String> message) override;
39
  Maybe<bool> WriteHostObject(Isolate* isolate, Local<Object> object) override;
40
  Maybe<uint32_t> GetSharedArrayBufferId(
41
      Isolate* isolate, Local<SharedArrayBuffer> shared_array_buffer) override;
42
43
  static void SetTreatArrayBufferViewsAsHostObjects(
44
      const FunctionCallbackInfo<Value>& args);
45
46
  static void New(const FunctionCallbackInfo<Value>& args);
47
  static void WriteHeader(const FunctionCallbackInfo<Value>& args);
48
  static void WriteValue(const FunctionCallbackInfo<Value>& args);
49
  static void ReleaseBuffer(const FunctionCallbackInfo<Value>& args);
50
  static void TransferArrayBuffer(const FunctionCallbackInfo<Value>& args);
51
  static void WriteUint32(const FunctionCallbackInfo<Value>& args);
52
  static void WriteUint64(const FunctionCallbackInfo<Value>& args);
53
  static void WriteDouble(const FunctionCallbackInfo<Value>& args);
54
  static void WriteRawBytes(const FunctionCallbackInfo<Value>& args);
55
56
  SET_NO_MEMORY_INFO()
57
  SET_MEMORY_INFO_NAME(SerializerContext)
58
  SET_SELF_SIZE(SerializerContext)
59
60
 private:
61
  ValueSerializer serializer_;
62
};
63
64
class DeserializerContext : public BaseObject,
65
                            public ValueDeserializer::Delegate {
66
 public:
67
  DeserializerContext(Environment* env,
68
                      Local<Object> wrap,
69
                      Local<Value> buffer);
70
71
402
  ~DeserializerContext() override {}
72
73
  MaybeLocal<Object> ReadHostObject(Isolate* isolate) override;
74
75
  static void New(const FunctionCallbackInfo<Value>& args);
76
  static void ReadHeader(const FunctionCallbackInfo<Value>& args);
77
  static void ReadValue(const FunctionCallbackInfo<Value>& args);
78
  static void TransferArrayBuffer(const FunctionCallbackInfo<Value>& args);
79
  static void GetWireFormatVersion(const FunctionCallbackInfo<Value>& args);
80
  static void ReadUint32(const FunctionCallbackInfo<Value>& args);
81
  static void ReadUint64(const FunctionCallbackInfo<Value>& args);
82
  static void ReadDouble(const FunctionCallbackInfo<Value>& args);
83
  static void ReadRawBytes(const FunctionCallbackInfo<Value>& args);
84
85
  SET_NO_MEMORY_INFO()
86
  SET_MEMORY_INFO_NAME(DeserializerContext)
87
  SET_SELF_SIZE(DeserializerContext)
88
89
 private:
90
  const uint8_t* data_;
91
  const size_t length_;
92
93
  ValueDeserializer deserializer_;
94
};
95
96
165
SerializerContext::SerializerContext(Environment* env, Local<Object> wrap)
97
  : BaseObject(env, wrap),
98
165
    serializer_(env->isolate(), this) {
99
165
  MakeWeak();
100
165
}
101
102
2
void SerializerContext::ThrowDataCloneError(Local<String> message) {
103
4
  Local<Value> args[1] = { message };
104
  Local<Value> get_data_clone_error =
105
2
      object()->Get(env()->context(),
106
10
                    env()->get_data_clone_error_string())
107
4
                      .ToLocalChecked();
108
109
2
  CHECK(get_data_clone_error->IsFunction());
110
  MaybeLocal<Value> error =
111
      get_data_clone_error.As<Function>()->Call(env()->context(),
112
                                                object(),
113
2
                                                arraysize(args),
114
8
                                                args);
115
116
4
  if (error.IsEmpty()) return;
117
118
2
  env()->isolate()->ThrowException(error.ToLocalChecked());
119
}
120
121
Maybe<uint32_t> SerializerContext::GetSharedArrayBufferId(
122
    Isolate* isolate, Local<SharedArrayBuffer> shared_array_buffer) {
123
  Local<Value> args[1] = { shared_array_buffer };
124
  Local<Value> get_shared_array_buffer_id =
125
      object()->Get(env()->context(),
126
                    env()->get_shared_array_buffer_id_string())
127
                      .ToLocalChecked();
128
129
  if (!get_shared_array_buffer_id->IsFunction()) {
130
    return ValueSerializer::Delegate::GetSharedArrayBufferId(
131
        isolate, shared_array_buffer);
132
  }
133
134
  MaybeLocal<Value> id =
135
      get_shared_array_buffer_id.As<Function>()->Call(env()->context(),
136
                                                      object(),
137
                                                      arraysize(args),
138
                                                      args);
139
140
  if (id.IsEmpty()) return Nothing<uint32_t>();
141
142
  return id.ToLocalChecked()->Uint32Value(env()->context());
143
}
144
145
29
Maybe<bool> SerializerContext::WriteHostObject(Isolate* isolate,
146
                                               Local<Object> input) {
147
  MaybeLocal<Value> ret;
148
58
  Local<Value> args[1] = { input };
149
150
  Local<Value> write_host_object =
151
29
      object()->Get(env()->context(),
152
145
                    env()->write_host_object_string()).ToLocalChecked();
153
154
29
  if (!write_host_object->IsFunction()) {
155
    return ValueSerializer::Delegate::WriteHostObject(isolate, input);
156
  }
157
158
  ret = write_host_object.As<Function>()->Call(env()->context(),
159
                                               object(),
160
29
                                               arraysize(args),
161
116
                                               args);
162
163
29
  if (ret.IsEmpty())
164
2
    return Nothing<bool>();
165
166
27
  return Just(true);
167
}
168
169
165
void SerializerContext::New(const FunctionCallbackInfo<Value>& args) {
170
165
  Environment* env = Environment::GetCurrent(args);
171
172
165
  new SerializerContext(env, args.This());
173
165
}
174
175
165
void SerializerContext::WriteHeader(const FunctionCallbackInfo<Value>& args) {
176
  SerializerContext* ctx;
177
330
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
178
165
  ctx->serializer_.WriteHeader();
179
}
180
181
173
void SerializerContext::WriteValue(const FunctionCallbackInfo<Value>& args) {
182
  SerializerContext* ctx;
183
346
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
184
  Maybe<bool> ret =
185
173
      ctx->serializer_.WriteValue(ctx->env()->context(), args[0]);
186
187
680
  if (ret.IsJust()) args.GetReturnValue().Set(ret.FromJust());
188
}
189
190
164
void SerializerContext::SetTreatArrayBufferViewsAsHostObjects(
191
    const FunctionCallbackInfo<Value>& args) {
192
  SerializerContext* ctx;
193
328
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
194
195
492
  bool value = args[0]->BooleanValue(ctx->env()->isolate());
196
164
  ctx->serializer_.SetTreatArrayBufferViewsAsHostObjects(value);
197
}
198
199
160
void SerializerContext::ReleaseBuffer(const FunctionCallbackInfo<Value>& args) {
200
  SerializerContext* ctx;
201
320
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
202
203
  // Note: Both ValueSerializer and this Buffer::New() variant use malloc()
204
  // as the underlying allocator.
205
160
  std::pair<uint8_t*, size_t> ret = ctx->serializer_.Release();
206
  auto buf = Buffer::New(ctx->env(),
207
                         reinterpret_cast<char*>(ret.first),
208
                         ret.second,
209
160
                         true /* uses_malloc */);
210
211
160
  if (!buf.IsEmpty()) {
212
320
    args.GetReturnValue().Set(buf.ToLocalChecked());
213
  }
214
}
215
216
void SerializerContext::TransferArrayBuffer(
217
    const FunctionCallbackInfo<Value>& args) {
218
  SerializerContext* ctx;
219
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
220
221
  Maybe<uint32_t> id = args[0]->Uint32Value(ctx->env()->context());
222
  if (id.IsNothing()) return;
223
224
  if (!args[1]->IsArrayBuffer())
225
    return node::THROW_ERR_INVALID_ARG_TYPE(
226
        ctx->env(), "arrayBuffer must be an ArrayBuffer");
227
228
  Local<ArrayBuffer> ab = args[1].As<ArrayBuffer>();
229
  ctx->serializer_.TransferArrayBuffer(id.FromJust(), ab);
230
  return;
231
}
232
233
49
void SerializerContext::WriteUint32(const FunctionCallbackInfo<Value>& args) {
234
  SerializerContext* ctx;
235
49
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
236
237
147
  Maybe<uint32_t> value = args[0]->Uint32Value(ctx->env()->context());
238
49
  if (value.IsNothing()) return;
239
240
49
  ctx->serializer_.WriteUint32(value.FromJust());
241
}
242
243
1
void SerializerContext::WriteUint64(const FunctionCallbackInfo<Value>& args) {
244
  SerializerContext* ctx;
245
1
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
246
247
3
  Maybe<uint32_t> arg0 = args[0]->Uint32Value(ctx->env()->context());
248
3
  Maybe<uint32_t> arg1 = args[1]->Uint32Value(ctx->env()->context());
249

2
  if (arg0.IsNothing() || arg1.IsNothing())
250
    return;
251
252
1
  uint64_t hi = arg0.FromJust();
253
1
  uint64_t lo = arg1.FromJust();
254
1
  ctx->serializer_.WriteUint64((hi << 32) | lo);
255
}
256
257
1
void SerializerContext::WriteDouble(const FunctionCallbackInfo<Value>& args) {
258
  SerializerContext* ctx;
259
1
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
260
261
3
  Maybe<double> value = args[0]->NumberValue(ctx->env()->context());
262
1
  if (value.IsNothing()) return;
263
264
1
  ctx->serializer_.WriteDouble(value.FromJust());
265
}
266
267
28
void SerializerContext::WriteRawBytes(const FunctionCallbackInfo<Value>& args) {
268
  SerializerContext* ctx;
269
29
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
270
271
56
  if (!args[0]->IsArrayBufferView()) {
272
    return node::THROW_ERR_INVALID_ARG_TYPE(
273
1
        ctx->env(), "source must be a TypedArray or a DataView");
274
  }
275
276
27
  ctx->serializer_.WriteRawBytes(Buffer::Data(args[0]),
277
54
                                 Buffer::Length(args[0]));
278
}
279
280
202
DeserializerContext::DeserializerContext(Environment* env,
281
                                         Local<Object> wrap,
282
                                         Local<Value> buffer)
283
  : BaseObject(env, wrap),
284
202
    data_(reinterpret_cast<const uint8_t*>(Buffer::Data(buffer))),
285
202
    length_(Buffer::Length(buffer)),
286
606
    deserializer_(env->isolate(), data_, length_, this) {
287
808
  object()->Set(env->context(), env->buffer_string(), buffer).FromJust();
288
202
  deserializer_.SetExpectInlineWasm(true);
289
290
202
  MakeWeak();
291
202
}
292
293
37
MaybeLocal<Object> DeserializerContext::ReadHostObject(Isolate* isolate) {
294
  Local<Value> read_host_object =
295
37
      object()->Get(env()->context(),
296
185
                    env()->read_host_object_string()).ToLocalChecked();
297
298
37
  if (!read_host_object->IsFunction()) {
299
    return ValueDeserializer::Delegate::ReadHostObject(isolate);
300
  }
301
302
37
  Isolate::AllowJavascriptExecutionScope allow_js(isolate);
303
  MaybeLocal<Value> ret =
304
      read_host_object.As<Function>()->Call(env()->context(),
305
                                            object(),
306
                                            0,
307
148
                                            nullptr);
308
309
37
  if (ret.IsEmpty())
310
    return MaybeLocal<Object>();
311
312
37
  Local<Value> return_value = ret.ToLocalChecked();
313
37
  if (!return_value->IsObject()) {
314
    env()->ThrowTypeError("readHostObject must return an object");
315
    return MaybeLocal<Object>();
316
  }
317
318
37
  return return_value.As<Object>();
319
}
320
321
204
void DeserializerContext::New(const FunctionCallbackInfo<Value>& args) {
322
204
  Environment* env = Environment::GetCurrent(args);
323
324
408
  if (!args[0]->IsArrayBufferView()) {
325
    return node::THROW_ERR_INVALID_ARG_TYPE(
326
206
        env, "buffer must be a TypedArray or a DataView");
327
  }
328
329
404
  new DeserializerContext(env, args.This(), args[0]);
330
}
331
332
202
void DeserializerContext::ReadHeader(const FunctionCallbackInfo<Value>& args) {
333
  DeserializerContext* ctx;
334
404
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
335
336
202
  Maybe<bool> ret = ctx->deserializer_.ReadHeader(ctx->env()->context());
337
338
808
  if (ret.IsJust()) args.GetReturnValue().Set(ret.FromJust());
339
}
340
341
211
void DeserializerContext::ReadValue(const FunctionCallbackInfo<Value>& args) {
342
  DeserializerContext* ctx;
343
422
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
344
345
211
  MaybeLocal<Value> ret = ctx->deserializer_.ReadValue(ctx->env()->context());
346
347
633
  if (!ret.IsEmpty()) args.GetReturnValue().Set(ret.ToLocalChecked());
348
}
349
350
void DeserializerContext::TransferArrayBuffer(
351
    const FunctionCallbackInfo<Value>& args) {
352
  DeserializerContext* ctx;
353
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
354
355
  Maybe<uint32_t> id = args[0]->Uint32Value(ctx->env()->context());
356
  if (id.IsNothing()) return;
357
358
  if (args[1]->IsArrayBuffer()) {
359
    Local<ArrayBuffer> ab = args[1].As<ArrayBuffer>();
360
    ctx->deserializer_.TransferArrayBuffer(id.FromJust(), ab);
361
    return;
362
  }
363
364
  if (args[1]->IsSharedArrayBuffer()) {
365
    Local<SharedArrayBuffer> sab = args[1].As<SharedArrayBuffer>();
366
    ctx->deserializer_.TransferSharedArrayBuffer(id.FromJust(), sab);
367
    return;
368
  }
369
370
  return node::THROW_ERR_INVALID_ARG_TYPE(
371
      ctx->env(), "arrayBuffer must be an ArrayBuffer or SharedArrayBuffer");
372
}
373
374
1
void DeserializerContext::GetWireFormatVersion(
375
    const FunctionCallbackInfo<Value>& args) {
376
  DeserializerContext* ctx;
377
2
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
378
379
3
  args.GetReturnValue().Set(ctx->deserializer_.GetWireFormatVersion());
380
}
381
382
69
void DeserializerContext::ReadUint32(const FunctionCallbackInfo<Value>& args) {
383
  DeserializerContext* ctx;
384
69
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
385
386
  uint32_t value;
387
69
  bool ok = ctx->deserializer_.ReadUint32(&value);
388
69
  if (!ok) return ctx->env()->ThrowError("ReadUint32() failed");
389
207
  return args.GetReturnValue().Set(value);
390
}
391
392
1
void DeserializerContext::ReadUint64(const FunctionCallbackInfo<Value>& args) {
393
  DeserializerContext* ctx;
394
1
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
395
396
  uint64_t value;
397
1
  bool ok = ctx->deserializer_.ReadUint64(&value);
398
1
  if (!ok) return ctx->env()->ThrowError("ReadUint64() failed");
399
400
1
  uint32_t hi = static_cast<uint32_t>(value >> 32);
401
1
  uint32_t lo = static_cast<uint32_t>(value);
402
403
1
  Isolate* isolate = ctx->env()->isolate();
404
405
  Local<Value> ret[] = {
406
    Integer::NewFromUnsigned(isolate, hi),
407
    Integer::NewFromUnsigned(isolate, lo)
408
3
  };
409
3
  return args.GetReturnValue().Set(Array::New(isolate, ret, arraysize(ret)));
410
}
411
412
1
void DeserializerContext::ReadDouble(const FunctionCallbackInfo<Value>& args) {
413
  DeserializerContext* ctx;
414
1
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
415
416
  double value;
417
1
  bool ok = ctx->deserializer_.ReadDouble(&value);
418
1
  if (!ok) return ctx->env()->ThrowError("ReadDouble() failed");
419
3
  return args.GetReturnValue().Set(value);
420
}
421
422
37
void DeserializerContext::ReadRawBytes(
423
    const FunctionCallbackInfo<Value>& args) {
424
  DeserializerContext* ctx;
425
37
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
426
427
111
  Maybe<int64_t> length_arg = args[0]->IntegerValue(ctx->env()->context());
428
37
  if (length_arg.IsNothing()) return;
429
37
  size_t length = length_arg.FromJust();
430
431
  const void* data;
432
37
  bool ok = ctx->deserializer_.ReadRawBytes(length, &data);
433
37
  if (!ok) return ctx->env()->ThrowError("ReadRawBytes() failed");
434
435
37
  const uint8_t* position = reinterpret_cast<const uint8_t*>(data);
436
37
  CHECK_GE(position, ctx->data_);
437
37
  CHECK_LE(position + length, ctx->data_ + ctx->length_);
438
439
37
  const uint32_t offset = position - ctx->data_;
440
37
  CHECK_EQ(ctx->data_ + offset, position);
441
442
74
  args.GetReturnValue().Set(offset);
443
}
444
445
4404
void Initialize(Local<Object> target,
446
                Local<Value> unused,
447
                Local<Context> context,
448
                void* priv) {
449
4404
  Environment* env = Environment::GetCurrent(context);
450
  Local<FunctionTemplate> ser =
451
4404
      env->NewFunctionTemplate(SerializerContext::New);
452
453
8808
  ser->InstanceTemplate()->SetInternalFieldCount(1);
454
455
4404
  env->SetProtoMethod(ser, "writeHeader", SerializerContext::WriteHeader);
456
4404
  env->SetProtoMethod(ser, "writeValue", SerializerContext::WriteValue);
457
4404
  env->SetProtoMethod(ser, "releaseBuffer", SerializerContext::ReleaseBuffer);
458
  env->SetProtoMethod(ser,
459
                      "transferArrayBuffer",
460
4404
                      SerializerContext::TransferArrayBuffer);
461
4404
  env->SetProtoMethod(ser, "writeUint32", SerializerContext::WriteUint32);
462
4404
  env->SetProtoMethod(ser, "writeUint64", SerializerContext::WriteUint64);
463
4404
  env->SetProtoMethod(ser, "writeDouble", SerializerContext::WriteDouble);
464
4404
  env->SetProtoMethod(ser, "writeRawBytes", SerializerContext::WriteRawBytes);
465
  env->SetProtoMethod(ser,
466
                      "_setTreatArrayBufferViewsAsHostObjects",
467
4404
                      SerializerContext::SetTreatArrayBufferViewsAsHostObjects);
468
469
  Local<String> serializerString =
470
4404
      FIXED_ONE_BYTE_STRING(env->isolate(), "Serializer");
471
4404
  ser->SetClassName(serializerString);
472
  target->Set(env->context(),
473
              serializerString,
474
22020
              ser->GetFunction(env->context()).ToLocalChecked()).FromJust();
475
476
  Local<FunctionTemplate> des =
477
4404
      env->NewFunctionTemplate(DeserializerContext::New);
478
479
8808
  des->InstanceTemplate()->SetInternalFieldCount(1);
480
481
4404
  env->SetProtoMethod(des, "readHeader", DeserializerContext::ReadHeader);
482
4404
  env->SetProtoMethod(des, "readValue", DeserializerContext::ReadValue);
483
  env->SetProtoMethod(des,
484
                      "getWireFormatVersion",
485
4404
                      DeserializerContext::GetWireFormatVersion);
486
  env->SetProtoMethod(des,
487
                      "transferArrayBuffer",
488
4404
                      DeserializerContext::TransferArrayBuffer);
489
4404
  env->SetProtoMethod(des, "readUint32", DeserializerContext::ReadUint32);
490
4404
  env->SetProtoMethod(des, "readUint64", DeserializerContext::ReadUint64);
491
4404
  env->SetProtoMethod(des, "readDouble", DeserializerContext::ReadDouble);
492
4404
  env->SetProtoMethod(des, "_readRawBytes", DeserializerContext::ReadRawBytes);
493
494
  Local<String> deserializerString =
495
4404
      FIXED_ONE_BYTE_STRING(env->isolate(), "Deserializer");
496
4404
  des->SetClassName(deserializerString);
497
  target->Set(env->context(),
498
              deserializerString,
499
22020
              des->GetFunction(env->context()).ToLocalChecked()).FromJust();
500
4404
}
501
502
}  // anonymous namespace
503
}  // namespace node
504
505
4292
NODE_MODULE_CONTEXT_AWARE_INTERNAL(serdes, node::Initialize)