GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_union_bytes.h Lines: 23 23 100.0 %
Date: 2019-07-27 22:37:30 Branches: 6 10 60.0 %

Line Branch Exec Source
1
2
#ifndef SRC_NODE_UNION_BYTES_H_
3
#define SRC_NODE_UNION_BYTES_H_
4
5
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
6
7
// A union of const uint8_t* or const uint16_t* data that can be
8
// turned into external v8::String when given an isolate.
9
10
#include "v8.h"
11
12
namespace node {
13
14
class NonOwningExternalOneByteResource
15
    : public v8::String::ExternalOneByteStringResource {
16
 public:
17
444047
  explicit NonOwningExternalOneByteResource(const uint8_t* data, size_t length)
18
444047
      : data_(data), length_(length) {}
19
829194
  ~NonOwningExternalOneByteResource() override = default;
20
21
1730377249
  const char* data() const override {
22
1730377249
    return reinterpret_cast<const char*>(data_);
23
  }
24
1776193
  size_t length() const override { return length_; }
25
26
  NonOwningExternalOneByteResource(const NonOwningExternalOneByteResource&) =
27
      delete;
28
  NonOwningExternalOneByteResource& operator=(
29
      const NonOwningExternalOneByteResource&) = delete;
30
31
 private:
32
  const uint8_t* data_;
33
  size_t length_;
34
};
35
36
class NonOwningExternalTwoByteResource
37
    : public v8::String::ExternalStringResource {
38
 public:
39
6038
  explicit NonOwningExternalTwoByteResource(const uint16_t* data, size_t length)
40
6038
      : data_(data), length_(length) {}
41
11198
  ~NonOwningExternalTwoByteResource() override = default;
42
43
16624152
  const uint16_t* data() const override { return data_; }
44
24152
  size_t length() const override { return length_; }
45
46
  NonOwningExternalTwoByteResource(const NonOwningExternalTwoByteResource&) =
47
      delete;
48
  NonOwningExternalTwoByteResource& operator=(
49
      const NonOwningExternalTwoByteResource&) = delete;
50
51
 private:
52
  const uint16_t* data_;
53
  size_t length_;
54
};
55
56
// Similar to a v8::String, but it's independent from Isolates
57
// and can be materialized in Isolates as external Strings
58
// via ToStringChecked. The data pointers are owned by the caller.
59
class UnionBytes {
60
 public:
61
19328
  UnionBytes(const uint16_t* data, size_t length)
62
19328
      : is_one_byte_(false), two_bytes_(data), length_(length) {}
63
961568
  UnionBytes(const uint8_t* data, size_t length)
64
961568
      : is_one_byte_(true), one_bytes_(data), length_(length) {}
65
66
  UnionBytes(const UnionBytes&) = default;
67
  UnionBytes& operator=(const UnionBytes&) = default;
68
  UnionBytes(UnionBytes&&) = default;
69
  UnionBytes& operator=(UnionBytes&&) = default;
70
71
  bool is_one_byte() const { return is_one_byte_; }
72
  const uint16_t* two_bytes_data() const {
73
    CHECK(!is_one_byte_);
74
    CHECK_NOT_NULL(two_bytes_);
75
    return two_bytes_;
76
  }
77
  const uint8_t* one_bytes_data() const {
78
    CHECK(is_one_byte_);
79
    CHECK_NOT_NULL(one_bytes_);
80
    return one_bytes_;
81
  }
82
450082
  v8::Local<v8::String> ToStringChecked(v8::Isolate* isolate) const {
83
450082
    if (is_one_byte_) {
84
444044
      CHECK_NOT_NULL(one_bytes_);
85
      NonOwningExternalOneByteResource* source =
86
444044
          new NonOwningExternalOneByteResource(one_bytes_, length_);
87
888095
      return v8::String::NewExternalOneByte(isolate, source).ToLocalChecked();
88
    } else {
89
6038
      CHECK_NOT_NULL(two_bytes_);
90
      NonOwningExternalTwoByteResource* source =
91
6038
          new NonOwningExternalTwoByteResource(two_bytes_, length_);
92
12076
      return v8::String::NewExternalTwoByte(isolate, source).ToLocalChecked();
93
    }
94
  }
95
  size_t length() { return length_; }
96
97
 private:
98
  bool is_one_byte_;
99
  union {
100
    const uint8_t* one_bytes_;
101
    const uint16_t* two_bytes_;
102
  };
103
  size_t length_;
104
};
105
106
}  // namespace node
107
108
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
109
110
#endif  // SRC_NODE_UNION_BYTES_H_