/src/node/src/node_union_bytes.h
Line | Count | 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 | | #include "v8.h" |
8 | | |
9 | | namespace node { |
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 | 26.3k | : data_(data), length_(length), owning_ptr_(owning_ptr) {}node::StaticExternalByteResource<unsigned short, unsigned short, v8::String::ExternalStringResource>::StaticExternalByteResource(unsigned short const*, unsigned long, std::__1::shared_ptr<void>) Line | Count | Source | 21 | 288 | : data_(data), length_(length), owning_ptr_(owning_ptr) {} |
node::StaticExternalByteResource<unsigned char, char, v8::String::ExternalOneByteStringResource>::StaticExternalByteResource(unsigned char const*, unsigned long, std::__1::shared_ptr<void>) Line | Count | Source | 21 | 26.0k | : data_(data), length_(length), owning_ptr_(owning_ptr) {} |
|
22 | | |
23 | 18.4k | const IChar* data() const override { |
24 | 18.4k | return reinterpret_cast<const IChar*>(data_); |
25 | 18.4k | } node::StaticExternalByteResource<unsigned short, unsigned short, v8::String::ExternalStringResource>::data() const Line | Count | Source | 23 | 385 | const IChar* data() const override { | 24 | 385 | return reinterpret_cast<const IChar*>(data_); | 25 | 385 | } |
node::StaticExternalByteResource<unsigned char, char, v8::String::ExternalOneByteStringResource>::data() const Line | Count | Source | 23 | 18.0k | const IChar* data() const override { | 24 | 18.0k | return reinterpret_cast<const IChar*>(data_); | 25 | 18.0k | } |
|
26 | 10.3k | size_t length() const override { return length_; }node::StaticExternalByteResource<unsigned short, unsigned short, v8::String::ExternalStringResource>::length() const Line | Count | Source | 26 | 280 | size_t length() const override { return length_; } |
node::StaticExternalByteResource<unsigned char, char, v8::String::ExternalOneByteStringResource>::length() const Line | Count | Source | 26 | 10.0k | size_t length() const override { return length_; } |
|
27 | | |
28 | 2.59k | 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 | 2.59k | } node::StaticExternalByteResource<unsigned short, unsigned short, v8::String::ExternalStringResource>::Dispose() Line | Count | Source | 28 | 70 | 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 | 70 | } |
node::StaticExternalByteResource<unsigned char, char, v8::String::ExternalOneByteStringResource>::Dispose() Line | Count | Source | 28 | 2.52k | 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 | 2.52k | } |
|
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 | | |
53 | | // Similar to a v8::String, but it's independent from Isolates |
54 | | // and can be materialized in Isolates as external Strings |
55 | | // via ToStringChecked. |
56 | | class UnionBytes { |
57 | | public: |
58 | | explicit UnionBytes(StaticExternalOneByteResource* one_byte_resource) |
59 | 26.0k | : one_byte_resource_(one_byte_resource), two_byte_resource_(nullptr) {} |
60 | | explicit UnionBytes(StaticExternalTwoByteResource* two_byte_resource) |
61 | 288 | : one_byte_resource_(nullptr), two_byte_resource_(two_byte_resource) {} |
62 | | |
63 | | UnionBytes(const UnionBytes&) = default; |
64 | | UnionBytes& operator=(const UnionBytes&) = default; |
65 | | UnionBytes(UnionBytes&&) = default; |
66 | | UnionBytes& operator=(UnionBytes&&) = default; |
67 | | |
68 | 2.59k | bool is_one_byte() const { return one_byte_resource_ != nullptr; } |
69 | | |
70 | | v8::Local<v8::String> ToStringChecked(v8::Isolate* isolate) const; |
71 | | |
72 | | private: |
73 | | StaticExternalOneByteResource* one_byte_resource_; |
74 | | StaticExternalTwoByteResource* two_byte_resource_; |
75 | | }; |
76 | | |
77 | | } // namespace node |
78 | | |
79 | | #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
80 | | |
81 | | #endif // SRC_NODE_UNION_BYTES_H_ |