|
4 | 4 |
|
5 | 5 | #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
6 | 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 | 7 | #include "v8.h" |
11 | 8 |
|
12 | 9 | namespace node { |
13 | 10 |
|
| 11 | +// An external resource intended to be used with static lifetime. |
| 12 | +template <typename Char, typename IChar, typename Base> |
| 13 | +class StaticExternalByteResource : public Base { |
| 14 | + static_assert(sizeof(IChar) == sizeof(Char), |
| 15 | + "incompatible interface and internal pointers"); |
| 16 | + |
| 17 | + public: |
| 18 | + explicit StaticExternalByteResource(const Char* data, |
| 19 | + size_t length, |
| 20 | + std::shared_ptr<void> owning_ptr) |
| 21 | + : data_(data), length_(length), owning_ptr_(owning_ptr) {} |
| 22 | + |
| 23 | + const IChar* data() const override { |
| 24 | + return reinterpret_cast<const IChar*>(data_); |
| 25 | + } |
| 26 | + size_t length() const override { return length_; } |
| 27 | + |
| 28 | + void Dispose() override { |
| 29 | + // We ignore Dispose calls from V8, even if we "own" a resource via |
| 30 | + // owning_ptr_. All instantiations of this class are static or owned by a |
| 31 | + // static map, and will be destructed when static variables are destructed. |
| 32 | + } |
| 33 | + |
| 34 | + StaticExternalByteResource(const StaticExternalByteResource&) = delete; |
| 35 | + StaticExternalByteResource& operator=(const StaticExternalByteResource&) = |
| 36 | + delete; |
| 37 | + |
| 38 | + private: |
| 39 | + const Char* data_; |
| 40 | + const size_t length_; |
| 41 | + std::shared_ptr<void> owning_ptr_; |
| 42 | +}; |
| 43 | + |
| 44 | +using StaticExternalOneByteResource = |
| 45 | + StaticExternalByteResource<uint8_t, |
| 46 | + char, |
| 47 | + v8::String::ExternalOneByteStringResource>; |
| 48 | +using StaticExternalTwoByteResource = |
| 49 | + StaticExternalByteResource<uint16_t, |
| 50 | + uint16_t, |
| 51 | + v8::String::ExternalStringResource>; |
| 52 | + |
14 | 53 | // Similar to a v8::String, but it's independent from Isolates |
15 | 54 | // and can be materialized in Isolates as external Strings |
16 | 55 | // via ToStringChecked. |
17 | 56 | class UnionBytes { |
18 | 57 | public: |
19 | | - UnionBytes(const uint16_t* data, size_t length) |
20 | | - : one_bytes_(nullptr), two_bytes_(data), length_(length) {} |
21 | | - UnionBytes(const uint8_t* data, size_t length) |
22 | | - : one_bytes_(data), two_bytes_(nullptr), length_(length) {} |
23 | | - template <typename T> // T = uint8_t or uint16_t |
24 | | - explicit UnionBytes(std::shared_ptr<std::vector</*const*/ T>> data) |
25 | | - : UnionBytes(data->data(), data->size()) { |
26 | | - owning_ptr_ = data; |
27 | | - } |
| 58 | + explicit UnionBytes(StaticExternalOneByteResource* one_byte_resource) |
| 59 | + : one_byte_resource_(one_byte_resource), two_byte_resource_(nullptr) {} |
| 60 | + explicit UnionBytes(StaticExternalTwoByteResource* two_byte_resource) |
| 61 | + : one_byte_resource_(nullptr), two_byte_resource_(two_byte_resource) {} |
28 | 62 |
|
29 | 63 | UnionBytes(const UnionBytes&) = default; |
30 | 64 | UnionBytes& operator=(const UnionBytes&) = default; |
31 | 65 | UnionBytes(UnionBytes&&) = default; |
32 | 66 | UnionBytes& operator=(UnionBytes&&) = default; |
33 | 67 |
|
34 | | - bool is_one_byte() const { return one_bytes_ != nullptr; } |
35 | | - const uint16_t* two_bytes_data() const { |
36 | | - CHECK_NOT_NULL(two_bytes_); |
37 | | - return two_bytes_; |
38 | | - } |
39 | | - const uint8_t* one_bytes_data() const { |
40 | | - CHECK_NOT_NULL(one_bytes_); |
41 | | - return one_bytes_; |
42 | | - } |
| 68 | + bool is_one_byte() const { return one_byte_resource_ != nullptr; } |
| 69 | + |
43 | 70 | v8::Local<v8::String> ToStringChecked(v8::Isolate* isolate) const; |
44 | | - size_t length() const { return length_; } |
45 | 71 |
|
46 | 72 | private: |
47 | | - const uint8_t* one_bytes_; |
48 | | - const uint16_t* two_bytes_; |
49 | | - size_t length_; |
50 | | - std::shared_ptr<void> owning_ptr_; |
| 73 | + StaticExternalOneByteResource* one_byte_resource_; |
| 74 | + StaticExternalTwoByteResource* two_byte_resource_; |
51 | 75 | }; |
52 | 76 |
|
53 | 77 | } // namespace node |
|
0 commit comments