summaryrefslogtreecommitdiff
path: root/src/node_union_bytes.h
blob: 4a3f67980fdc925699cc67e40736bddce8f7b857 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

#ifndef SRC_NODE_UNION_BYTES_H_
#define SRC_NODE_UNION_BYTES_H_

#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "v8.h"

namespace node {

// An external resource intended to be used with static lifetime.
template <typename Char, typename IChar, typename Base>
class StaticExternalByteResource : public Base {
  static_assert(sizeof(IChar) == sizeof(Char),
                "incompatible interface and internal pointers");

 public:
  explicit StaticExternalByteResource(const Char* data,
                                      size_t length,
                                      std::shared_ptr<void> owning_ptr)
      : data_(data), length_(length), owning_ptr_(owning_ptr) {}

  const IChar* data() const override {
    return reinterpret_cast<const IChar*>(data_);
  }
  size_t length() const override { return length_; }

  void Dispose() override {
    // We ignore Dispose calls from V8, even if we "own" a resource via
    // owning_ptr_. All instantiations of this class are static or owned by a
    // static map, and will be destructed when static variables are destructed.
  }

  StaticExternalByteResource(const StaticExternalByteResource&) = delete;
  StaticExternalByteResource& operator=(const StaticExternalByteResource&) =
      delete;

 private:
  const Char* data_;
  const size_t length_;
  std::shared_ptr<void> owning_ptr_;
};

using StaticExternalOneByteResource =
    StaticExternalByteResource<uint8_t,
                               char,
                               v8::String::ExternalOneByteStringResource>;
using StaticExternalTwoByteResource =
    StaticExternalByteResource<uint16_t,
                               uint16_t,
                               v8::String::ExternalStringResource>;

// Similar to a v8::String, but it's independent from Isolates
// and can be materialized in Isolates as external Strings
// via ToStringChecked.
class UnionBytes {
 public:
  explicit UnionBytes(StaticExternalOneByteResource* one_byte_resource)
      : one_byte_resource_(one_byte_resource), two_byte_resource_(nullptr) {}
  explicit UnionBytes(StaticExternalTwoByteResource* two_byte_resource)
      : one_byte_resource_(nullptr), two_byte_resource_(two_byte_resource) {}

  UnionBytes(const UnionBytes&) = default;
  UnionBytes& operator=(const UnionBytes&) = default;
  UnionBytes(UnionBytes&&) = default;
  UnionBytes& operator=(UnionBytes&&) = default;

  bool is_one_byte() const { return one_byte_resource_ != nullptr; }

  v8::Local<v8::String> ToStringChecked(v8::Isolate* isolate) const;

 private:
  StaticExternalOneByteResource* one_byte_resource_;
  StaticExternalTwoByteResource* two_byte_resource_;
};

}  // namespace node

#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#endif  // SRC_NODE_UNION_BYTES_H_