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: 31 210 14.8 %
Date: 2019-02-01 22:03:38 Branches: 0 104 0.0 %

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
  ~SerializerContext() {}
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
  ~DeserializerContext() {}
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
SerializerContext::SerializerContext(Environment* env, Local<Object> wrap)
97
  : BaseObject(env, wrap),
98
    serializer_(env->isolate(), this) {
99
  MakeWeak();
100
}
101
102
void SerializerContext::ThrowDataCloneError(Local<String> message) {
103
  Local<Value> args[1] = { message };
104
  Local<Value> get_data_clone_error =
105
      object()->Get(env()->context(),
106
                    env()->get_data_clone_error_string())
107
                      .ToLocalChecked();
108
109
  CHECK(get_data_clone_error->IsFunction());
110
  MaybeLocal<Value> error =
111
      get_data_clone_error.As<Function>()->Call(env()->context(),
112
                                                object(),
113
                                                arraysize(args),
114
                                                args);
115
116
  if (error.IsEmpty()) return;
117
118
  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
Maybe<bool> SerializerContext::WriteHostObject(Isolate* isolate,
146
                                               Local<Object> input) {
147
  MaybeLocal<Value> ret;
148
  Local<Value> args[1] = { input };
149
150
  Local<Value> write_host_object =
151
      object()->Get(env()->context(),
152
                    env()->write_host_object_string()).ToLocalChecked();
153
154
  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
                                               arraysize(args),
161
                                               args);
162
163
  if (ret.IsEmpty())
164
    return Nothing<bool>();
165
166
  return Just(true);
167
}
168
169
void SerializerContext::New(const FunctionCallbackInfo<Value>& args) {
170
  Environment* env = Environment::GetCurrent(args);
171
172
  new SerializerContext(env, args.This());
173
}
174
175
void SerializerContext::WriteHeader(const FunctionCallbackInfo<Value>& args) {
176
  SerializerContext* ctx;
177
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
178
  ctx->serializer_.WriteHeader();
179
}
180
181
void SerializerContext::WriteValue(const FunctionCallbackInfo<Value>& args) {
182
  SerializerContext* ctx;
183
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
184
  Maybe<bool> ret =
185
      ctx->serializer_.WriteValue(ctx->env()->context(), args[0]);
186
187
  if (ret.IsJust()) args.GetReturnValue().Set(ret.FromJust());
188
}
189
190
void SerializerContext::SetTreatArrayBufferViewsAsHostObjects(
191
    const FunctionCallbackInfo<Value>& args) {
192
  SerializerContext* ctx;
193
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
194
195
  bool value = args[0]->BooleanValue(ctx->env()->isolate());
196
  ctx->serializer_.SetTreatArrayBufferViewsAsHostObjects(value);
197
}
198
199
void SerializerContext::ReleaseBuffer(const FunctionCallbackInfo<Value>& args) {
200
  SerializerContext* ctx;
201
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
202
203
  std::pair<uint8_t*, size_t> ret = ctx->serializer_.Release();
204
  auto buf = Buffer::New(ctx->env(),
205
                         reinterpret_cast<char*>(ret.first),
206
                         ret.second);
207
208
  if (!buf.IsEmpty()) {
209
    args.GetReturnValue().Set(buf.ToLocalChecked());
210
  }
211
}
212
213
void SerializerContext::TransferArrayBuffer(
214
    const FunctionCallbackInfo<Value>& args) {
215
  SerializerContext* ctx;
216
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
217
218
  Maybe<uint32_t> id = args[0]->Uint32Value(ctx->env()->context());
219
  if (id.IsNothing()) return;
220
221
  if (!args[1]->IsArrayBuffer())
222
    return node::THROW_ERR_INVALID_ARG_TYPE(
223
        ctx->env(), "arrayBuffer must be an ArrayBuffer");
224
225
  Local<ArrayBuffer> ab = args[1].As<ArrayBuffer>();
226
  ctx->serializer_.TransferArrayBuffer(id.FromJust(), ab);
227
  return;
228
}
229
230
void SerializerContext::WriteUint32(const FunctionCallbackInfo<Value>& args) {
231
  SerializerContext* ctx;
232
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
233
234
  Maybe<uint32_t> value = args[0]->Uint32Value(ctx->env()->context());
235
  if (value.IsNothing()) return;
236
237
  ctx->serializer_.WriteUint32(value.FromJust());
238
}
239
240
void SerializerContext::WriteUint64(const FunctionCallbackInfo<Value>& args) {
241
  SerializerContext* ctx;
242
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
243
244
  Maybe<uint32_t> arg0 = args[0]->Uint32Value(ctx->env()->context());
245
  Maybe<uint32_t> arg1 = args[1]->Uint32Value(ctx->env()->context());
246
  if (arg0.IsNothing() || arg1.IsNothing())
247
    return;
248
249
  uint64_t hi = arg0.FromJust();
250
  uint64_t lo = arg1.FromJust();
251
  ctx->serializer_.WriteUint64((hi << 32) | lo);
252
}
253
254
void SerializerContext::WriteDouble(const FunctionCallbackInfo<Value>& args) {
255
  SerializerContext* ctx;
256
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
257
258
  Maybe<double> value = args[0]->NumberValue(ctx->env()->context());
259
  if (value.IsNothing()) return;
260
261
  ctx->serializer_.WriteDouble(value.FromJust());
262
}
263
264
void SerializerContext::WriteRawBytes(const FunctionCallbackInfo<Value>& args) {
265
  SerializerContext* ctx;
266
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
267
268
  if (!args[0]->IsArrayBufferView()) {
269
    return node::THROW_ERR_INVALID_ARG_TYPE(
270
        ctx->env(), "source must be a TypedArray or a DataView");
271
  }
272
273
  ctx->serializer_.WriteRawBytes(Buffer::Data(args[0]),
274
                                 Buffer::Length(args[0]));
275
}
276
277
DeserializerContext::DeserializerContext(Environment* env,
278
                                         Local<Object> wrap,
279
                                         Local<Value> buffer)
280
  : BaseObject(env, wrap),
281
    data_(reinterpret_cast<const uint8_t*>(Buffer::Data(buffer))),
282
    length_(Buffer::Length(buffer)),
283
    deserializer_(env->isolate(), data_, length_, this) {
284
  object()->Set(env->context(), env->buffer_string(), buffer).FromJust();
285
  deserializer_.SetExpectInlineWasm(true);
286
287
  MakeWeak();
288
}
289
290
MaybeLocal<Object> DeserializerContext::ReadHostObject(Isolate* isolate) {
291
  Local<Value> read_host_object =
292
      object()->Get(env()->context(),
293
                    env()->read_host_object_string()).ToLocalChecked();
294
295
  if (!read_host_object->IsFunction()) {
296
    return ValueDeserializer::Delegate::ReadHostObject(isolate);
297
  }
298
299
  Isolate::AllowJavascriptExecutionScope allow_js(isolate);
300
  MaybeLocal<Value> ret =
301
      read_host_object.As<Function>()->Call(env()->context(),
302
                                            object(),
303
                                            0,
304
                                            nullptr);
305
306
  if (ret.IsEmpty())
307
    return MaybeLocal<Object>();
308
309
  Local<Value> return_value = ret.ToLocalChecked();
310
  if (!return_value->IsObject()) {
311
    env()->ThrowTypeError("readHostObject must return an object");
312
    return MaybeLocal<Object>();
313
  }
314
315
  return return_value.As<Object>();
316
}
317
318
void DeserializerContext::New(const FunctionCallbackInfo<Value>& args) {
319
  Environment* env = Environment::GetCurrent(args);
320
321
  if (!args[0]->IsArrayBufferView()) {
322
    return node::THROW_ERR_INVALID_ARG_TYPE(
323
        env, "buffer must be a TypedArray or a DataView");
324
  }
325
326
  new DeserializerContext(env, args.This(), args[0]);
327
}
328
329
void DeserializerContext::ReadHeader(const FunctionCallbackInfo<Value>& args) {
330
  DeserializerContext* ctx;
331
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
332
333
  Maybe<bool> ret = ctx->deserializer_.ReadHeader(ctx->env()->context());
334
335
  if (ret.IsJust()) args.GetReturnValue().Set(ret.FromJust());
336
}
337
338
void DeserializerContext::ReadValue(const FunctionCallbackInfo<Value>& args) {
339
  DeserializerContext* ctx;
340
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
341
342
  MaybeLocal<Value> ret = ctx->deserializer_.ReadValue(ctx->env()->context());
343
344
  if (!ret.IsEmpty()) args.GetReturnValue().Set(ret.ToLocalChecked());
345
}
346
347
void DeserializerContext::TransferArrayBuffer(
348
    const FunctionCallbackInfo<Value>& args) {
349
  DeserializerContext* ctx;
350
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
351
352
  Maybe<uint32_t> id = args[0]->Uint32Value(ctx->env()->context());
353
  if (id.IsNothing()) return;
354
355
  if (args[1]->IsArrayBuffer()) {
356
    Local<ArrayBuffer> ab = args[1].As<ArrayBuffer>();
357
    ctx->deserializer_.TransferArrayBuffer(id.FromJust(), ab);
358
    return;
359
  }
360
361
  if (args[1]->IsSharedArrayBuffer()) {
362
    Local<SharedArrayBuffer> sab = args[1].As<SharedArrayBuffer>();
363
    ctx->deserializer_.TransferSharedArrayBuffer(id.FromJust(), sab);
364
    return;
365
  }
366
367
  return node::THROW_ERR_INVALID_ARG_TYPE(
368
      ctx->env(), "arrayBuffer must be an ArrayBuffer or SharedArrayBuffer");
369
}
370
371
void DeserializerContext::GetWireFormatVersion(
372
    const FunctionCallbackInfo<Value>& args) {
373
  DeserializerContext* ctx;
374
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
375
376
  args.GetReturnValue().Set(ctx->deserializer_.GetWireFormatVersion());
377
}
378
379
void DeserializerContext::ReadUint32(const FunctionCallbackInfo<Value>& args) {
380
  DeserializerContext* ctx;
381
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
382
383
  uint32_t value;
384
  bool ok = ctx->deserializer_.ReadUint32(&value);
385
  if (!ok) return ctx->env()->ThrowError("ReadUint32() failed");
386
  return args.GetReturnValue().Set(value);
387
}
388
389
void DeserializerContext::ReadUint64(const FunctionCallbackInfo<Value>& args) {
390
  DeserializerContext* ctx;
391
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
392
393
  uint64_t value;
394
  bool ok = ctx->deserializer_.ReadUint64(&value);
395
  if (!ok) return ctx->env()->ThrowError("ReadUint64() failed");
396
397
  uint32_t hi = static_cast<uint32_t>(value >> 32);
398
  uint32_t lo = static_cast<uint32_t>(value);
399
400
  Isolate* isolate = ctx->env()->isolate();
401
402
  Local<Value> ret[] = {
403
    Integer::NewFromUnsigned(isolate, hi),
404
    Integer::NewFromUnsigned(isolate, lo)
405
  };
406
  return args.GetReturnValue().Set(Array::New(isolate, ret, arraysize(ret)));
407
}
408
409
void DeserializerContext::ReadDouble(const FunctionCallbackInfo<Value>& args) {
410
  DeserializerContext* ctx;
411
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
412
413
  double value;
414
  bool ok = ctx->deserializer_.ReadDouble(&value);
415
  if (!ok) return ctx->env()->ThrowError("ReadDouble() failed");
416
  return args.GetReturnValue().Set(value);
417
}
418
419
void DeserializerContext::ReadRawBytes(
420
    const FunctionCallbackInfo<Value>& args) {
421
  DeserializerContext* ctx;
422
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
423
424
  Maybe<int64_t> length_arg = args[0]->IntegerValue(ctx->env()->context());
425
  if (length_arg.IsNothing()) return;
426
  size_t length = length_arg.FromJust();
427
428
  const void* data;
429
  bool ok = ctx->deserializer_.ReadRawBytes(length, &data);
430
  if (!ok) return ctx->env()->ThrowError("ReadRawBytes() failed");
431
432
  const uint8_t* position = reinterpret_cast<const uint8_t*>(data);
433
  CHECK_GE(position, ctx->data_);
434
  CHECK_LE(position + length, ctx->data_ + ctx->length_);
435
436
  const uint32_t offset = position - ctx->data_;
437
  CHECK_EQ(ctx->data_ + offset, position);
438
439
  args.GetReturnValue().Set(offset);
440
}
441
442
163
void Initialize(Local<Object> target,
443
                Local<Value> unused,
444
                Local<Context> context,
445
                void* priv) {
446
163
  Environment* env = Environment::GetCurrent(context);
447
  Local<FunctionTemplate> ser =
448
163
      env->NewFunctionTemplate(SerializerContext::New);
449
450
326
  ser->InstanceTemplate()->SetInternalFieldCount(1);
451
452
163
  env->SetProtoMethod(ser, "writeHeader", SerializerContext::WriteHeader);
453
163
  env->SetProtoMethod(ser, "writeValue", SerializerContext::WriteValue);
454
163
  env->SetProtoMethod(ser, "releaseBuffer", SerializerContext::ReleaseBuffer);
455
  env->SetProtoMethod(ser,
456
                      "transferArrayBuffer",
457
163
                      SerializerContext::TransferArrayBuffer);
458
163
  env->SetProtoMethod(ser, "writeUint32", SerializerContext::WriteUint32);
459
163
  env->SetProtoMethod(ser, "writeUint64", SerializerContext::WriteUint64);
460
163
  env->SetProtoMethod(ser, "writeDouble", SerializerContext::WriteDouble);
461
163
  env->SetProtoMethod(ser, "writeRawBytes", SerializerContext::WriteRawBytes);
462
  env->SetProtoMethod(ser,
463
                      "_setTreatArrayBufferViewsAsHostObjects",
464
163
                      SerializerContext::SetTreatArrayBufferViewsAsHostObjects);
465
466
  Local<String> serializerString =
467
163
      FIXED_ONE_BYTE_STRING(env->isolate(), "Serializer");
468
163
  ser->SetClassName(serializerString);
469
  target->Set(env->context(),
470
              serializerString,
471
815
              ser->GetFunction(env->context()).ToLocalChecked()).FromJust();
472
473
  Local<FunctionTemplate> des =
474
163
      env->NewFunctionTemplate(DeserializerContext::New);
475
476
326
  des->InstanceTemplate()->SetInternalFieldCount(1);
477
478
163
  env->SetProtoMethod(des, "readHeader", DeserializerContext::ReadHeader);
479
163
  env->SetProtoMethod(des, "readValue", DeserializerContext::ReadValue);
480
  env->SetProtoMethod(des,
481
                      "getWireFormatVersion",
482
163
                      DeserializerContext::GetWireFormatVersion);
483
  env->SetProtoMethod(des,
484
                      "transferArrayBuffer",
485
163
                      DeserializerContext::TransferArrayBuffer);
486
163
  env->SetProtoMethod(des, "readUint32", DeserializerContext::ReadUint32);
487
163
  env->SetProtoMethod(des, "readUint64", DeserializerContext::ReadUint64);
488
163
  env->SetProtoMethod(des, "readDouble", DeserializerContext::ReadDouble);
489
163
  env->SetProtoMethod(des, "_readRawBytes", DeserializerContext::ReadRawBytes);
490
491
  Local<String> deserializerString =
492
163
      FIXED_ONE_BYTE_STRING(env->isolate(), "Deserializer");
493
163
  des->SetClassName(deserializerString);
494
  target->Set(env->context(),
495
              deserializerString,
496
815
              des->GetFunction(env->context()).ToLocalChecked()).FromJust();
497
163
}
498
499
}  // anonymous namespace
500
}  // namespace node
501
502
164
NODE_MODULE_CONTEXT_AWARE_INTERNAL(serdes, node::Initialize)