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-03-02 22:23:06 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 "env.h"
11
#include "v8.h"
12
13
namespace node {
14
15
class NonOwningExternalOneByteResource
16
    : public v8::String::ExternalOneByteStringResource {
17
 public:
18
377136
  explicit NonOwningExternalOneByteResource(const uint8_t* data, size_t length)
19
377136
      : data_(data), length_(length) {}
20
694014
  ~NonOwningExternalOneByteResource() override = default;
21
22
773171395
  const char* data() const override {
23
773171395
    return reinterpret_cast<const char*>(data_);
24
  }
25
1508538
  size_t length() const override { return length_; }
26
27
 private:
28
  const uint8_t* data_;
29
  size_t length_;
30
  DISALLOW_COPY_AND_ASSIGN(NonOwningExternalOneByteResource);
31
};
32
33
class NonOwningExternalTwoByteResource
34
    : public v8::String::ExternalStringResource {
35
 public:
36
4559
  explicit NonOwningExternalTwoByteResource(const uint16_t* data, size_t length)
37
4559
      : data_(data), length_(length) {}
38
8294
  ~NonOwningExternalTwoByteResource() override = default;
39
40
15565776
  const uint16_t* data() const override { return data_; }
41
18236
  size_t length() const override { return length_; }
42
43
 private:
44
  const uint16_t* data_;
45
  size_t length_;
46
  DISALLOW_COPY_AND_ASSIGN(NonOwningExternalTwoByteResource);
47
};
48
49
// Similar to a v8::String, but it's independent from Isolates
50
// and can be materialized in Isolates as external Strings
51
// via ToStringChecked. The data pointers are owned by the caller.
52
class UnionBytes {
53
 public:
54
17168
  UnionBytes(const uint16_t* data, size_t length)
55
17168
      : is_one_byte_(false), two_bytes_(data), length_(length) {}
56
832648
  UnionBytes(const uint8_t* data, size_t length)
57
832648
      : is_one_byte_(true), one_bytes_(data), length_(length) {}
58
59
  UnionBytes(const UnionBytes&) = default;
60
  UnionBytes& operator=(const UnionBytes&) = default;
61
  UnionBytes(UnionBytes&&) = default;
62
  UnionBytes& operator=(UnionBytes&&) = default;
63
64
  bool is_one_byte() const { return is_one_byte_; }
65
  const uint16_t* two_bytes_data() const {
66
    CHECK(!is_one_byte_);
67
    CHECK_NOT_NULL(two_bytes_);
68
    return two_bytes_;
69
  }
70
  const uint8_t* one_bytes_data() const {
71
    CHECK(is_one_byte_);
72
    CHECK_NOT_NULL(one_bytes_);
73
    return one_bytes_;
74
  }
75
381691
  v8::Local<v8::String> ToStringChecked(v8::Isolate* isolate) const {
76
381691
    if (is_one_byte_) {
77
377132
      CHECK_NOT_NULL(one_bytes_);
78
      NonOwningExternalOneByteResource* source =
79
377132
          new NonOwningExternalOneByteResource(one_bytes_, length_);
80
754271
      return v8::String::NewExternalOneByte(isolate, source).ToLocalChecked();
81
    } else {
82
4559
      CHECK_NOT_NULL(two_bytes_);
83
      NonOwningExternalTwoByteResource* source =
84
4559
          new NonOwningExternalTwoByteResource(two_bytes_, length_);
85
9118
      return v8::String::NewExternalTwoByte(isolate, source).ToLocalChecked();
86
    }
87
  }
88
  size_t length() { return length_; }
89
90
 private:
91
  bool is_one_byte_;
92
  union {
93
    const uint8_t* one_bytes_;
94
    const uint16_t* two_bytes_;
95
  };
96
  size_t length_;
97
};
98
99
}  // namespace node
100
101
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
102
103
#endif  // SRC_NODE_UNION_BYTES_H_