@encharm/cws 4.3.0 → 4.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (238) hide show
  1. package/README.md +5 -7
  2. package/dist/bindings/cws_darwin_arm64_node108.node +0 -0
  3. package/dist/bindings/cws_darwin_arm64_node115.node +0 -0
  4. package/dist/bindings/cws_darwin_arm64_node83.node +0 -0
  5. package/dist/bindings/cws_darwin_arm64_node93.node +0 -0
  6. package/dist/bindings/cws_darwin_x64_node108.node +0 -0
  7. package/dist/bindings/cws_darwin_x64_node115.node +0 -0
  8. package/dist/bindings/cws_darwin_x64_node83.node +0 -0
  9. package/dist/bindings/cws_darwin_x64_node93.node +0 -0
  10. package/dist/bindings/cws_linux_arm64_node108.node +0 -0
  11. package/dist/bindings/cws_linux_arm64_node115.node +0 -0
  12. package/dist/bindings/cws_linux_arm64_node83.node +0 -0
  13. package/dist/bindings/cws_linux_arm64_node93.node +0 -0
  14. package/dist/bindings/cws_linux_x64_node108.node +0 -0
  15. package/dist/bindings/cws_linux_x64_node115.node +0 -0
  16. package/dist/bindings/cws_linux_x64_node83.node +0 -0
  17. package/dist/bindings/cws_linux_x64_node93.node +0 -0
  18. package/dist/bindings/cws_win32_x64_node108.node +0 -0
  19. package/dist/bindings/cws_win32_x64_node115.node +0 -0
  20. package/dist/bindings/cws_win32_x64_node83.node +0 -0
  21. package/dist/bindings/cws_win32_x64_node93.node +0 -0
  22. package/dist/shared.js +1 -1
  23. package/package.json +5 -5
  24. package/src/Addon.cpp +4 -12
  25. package/src/Addon.h +67 -91
  26. package/src/Socket.h +1 -0
  27. package/src/cSNode.cpp +3 -8
  28. package/src/headers/20/acorn_version.h +6 -0
  29. package/src/headers/20/aliased_buffer-inl.h +243 -0
  30. package/src/headers/20/aliased_buffer.h +206 -0
  31. package/src/headers/20/aliased_struct-inl.h +54 -0
  32. package/src/headers/20/aliased_struct.h +63 -0
  33. package/src/headers/20/async_wrap-inl.h +93 -0
  34. package/src/headers/20/async_wrap.h +252 -0
  35. package/src/headers/20/base64-inl.h +193 -0
  36. package/src/headers/20/base64.h +53 -0
  37. package/src/headers/20/base64_version.h +6 -0
  38. package/src/headers/20/base_object-inl.h +316 -0
  39. package/src/headers/20/base_object.h +320 -0
  40. package/src/headers/20/base_object_types.h +73 -0
  41. package/src/headers/20/blob_serializer_deserializer-inl.h +361 -0
  42. package/src/headers/20/blob_serializer_deserializer.h +132 -0
  43. package/src/headers/20/callback_queue-inl.h +97 -0
  44. package/src/headers/20/callback_queue.h +79 -0
  45. package/src/headers/20/cares_wrap.h +524 -0
  46. package/src/headers/20/cjs_module_lexer_version.h +6 -0
  47. package/src/headers/20/cleanup_queue-inl.h +62 -0
  48. package/src/headers/20/cleanup_queue.h +85 -0
  49. package/src/headers/20/connect_wrap.h +26 -0
  50. package/src/headers/20/connection_wrap.h +30 -0
  51. package/src/headers/20/cppgc/DEPS +8 -0
  52. package/src/headers/20/cppgc/OWNERS +2 -0
  53. package/src/headers/20/cppgc/README.md +135 -0
  54. package/src/headers/20/cppgc/allocation.h +310 -0
  55. package/src/headers/20/cppgc/common.h +28 -0
  56. package/src/headers/20/cppgc/cross-thread-persistent.h +466 -0
  57. package/src/headers/20/cppgc/custom-space.h +97 -0
  58. package/src/headers/20/cppgc/default-platform.h +67 -0
  59. package/src/headers/20/cppgc/ephemeron-pair.h +30 -0
  60. package/src/headers/20/cppgc/explicit-management.h +100 -0
  61. package/src/headers/20/cppgc/garbage-collected.h +106 -0
  62. package/src/headers/20/cppgc/heap-consistency.h +309 -0
  63. package/src/headers/20/cppgc/heap-handle.h +48 -0
  64. package/src/headers/20/cppgc/heap-state.h +82 -0
  65. package/src/headers/20/cppgc/heap-statistics.h +120 -0
  66. package/src/headers/20/cppgc/heap.h +202 -0
  67. package/src/headers/20/cppgc/internal/api-constants.h +68 -0
  68. package/src/headers/20/cppgc/internal/atomic-entry-flag.h +48 -0
  69. package/src/headers/20/cppgc/internal/base-page-handle.h +45 -0
  70. package/src/headers/20/cppgc/internal/caged-heap-local-data.h +111 -0
  71. package/src/headers/20/cppgc/internal/caged-heap.h +61 -0
  72. package/src/headers/20/cppgc/internal/compiler-specific.h +38 -0
  73. package/src/headers/20/cppgc/internal/finalizer-trait.h +93 -0
  74. package/src/headers/20/cppgc/internal/gc-info.h +157 -0
  75. package/src/headers/20/cppgc/internal/logging.h +50 -0
  76. package/src/headers/20/cppgc/internal/member-storage.h +248 -0
  77. package/src/headers/20/cppgc/internal/name-trait.h +137 -0
  78. package/src/headers/20/cppgc/internal/persistent-node.h +214 -0
  79. package/src/headers/20/cppgc/internal/pointer-policies.h +243 -0
  80. package/src/headers/20/cppgc/internal/write-barrier.h +487 -0
  81. package/src/headers/20/cppgc/liveness-broker.h +78 -0
  82. package/src/headers/20/cppgc/macros.h +35 -0
  83. package/src/headers/20/cppgc/member.h +604 -0
  84. package/src/headers/20/cppgc/name-provider.h +65 -0
  85. package/src/headers/20/cppgc/object-size-trait.h +58 -0
  86. package/src/headers/20/cppgc/persistent.h +373 -0
  87. package/src/headers/20/cppgc/platform.h +158 -0
  88. package/src/headers/20/cppgc/prefinalizer.h +75 -0
  89. package/src/headers/20/cppgc/process-heap-statistics.h +36 -0
  90. package/src/headers/20/cppgc/sentinel-pointer.h +32 -0
  91. package/src/headers/20/cppgc/source-location.h +92 -0
  92. package/src/headers/20/cppgc/testing.h +106 -0
  93. package/src/headers/20/cppgc/trace-trait.h +120 -0
  94. package/src/headers/20/cppgc/type-traits.h +250 -0
  95. package/src/headers/20/cppgc/visitor.h +426 -0
  96. package/src/headers/20/crypto/crypto_clienthello.h +131 -0
  97. package/src/headers/20/crypto/crypto_context.h +160 -0
  98. package/src/headers/20/crypto/crypto_keys.h +419 -0
  99. package/src/headers/20/crypto/crypto_tls.h +304 -0
  100. package/src/headers/20/crypto/crypto_util.h +806 -0
  101. package/src/headers/20/crypto_clienthello.h +131 -0
  102. package/src/headers/20/debug_utils-inl.h +226 -0
  103. package/src/headers/20/debug_utils.h +188 -0
  104. package/src/headers/20/diagnosticfilename-inl.h +33 -0
  105. package/src/headers/20/encoding_binding.h +59 -0
  106. package/src/headers/20/env-inl.h +923 -0
  107. package/src/headers/20/env.h +1193 -0
  108. package/src/headers/20/env_properties.h +446 -0
  109. package/src/headers/20/handle_wrap.h +123 -0
  110. package/src/headers/20/histogram-inl.h +107 -0
  111. package/src/headers/20/histogram.h +240 -0
  112. package/src/headers/20/inspector_agent.h +144 -0
  113. package/src/headers/20/inspector_io.h +78 -0
  114. package/src/headers/20/inspector_profiler.h +149 -0
  115. package/src/headers/20/inspector_socket.h +60 -0
  116. package/src/headers/20/inspector_socket_server.h +110 -0
  117. package/src/headers/20/js_native_api.h +601 -0
  118. package/src/headers/20/js_native_api_types.h +168 -0
  119. package/src/headers/20/js_native_api_v8.h +463 -0
  120. package/src/headers/20/js_native_api_v8_internals.h +45 -0
  121. package/src/headers/20/js_stream.h +52 -0
  122. package/src/headers/20/json_parser.h +38 -0
  123. package/src/headers/20/json_utils.h +172 -0
  124. package/src/headers/20/memory_tracker-inl.h +356 -0
  125. package/src/headers/20/memory_tracker.h +289 -0
  126. package/src/headers/20/module_wrap.h +117 -0
  127. package/src/headers/20/node.h +1516 -0
  128. package/src/headers/20/node_api.h +261 -0
  129. package/src/headers/20/node_api_internals.h +42 -0
  130. package/src/headers/20/node_api_types.h +52 -0
  131. package/src/headers/20/node_binding.h +142 -0
  132. package/src/headers/20/node_blob.h +159 -0
  133. package/src/headers/20/node_bob-inl.h +36 -0
  134. package/src/headers/20/node_bob.h +107 -0
  135. package/src/headers/20/node_buffer.h +92 -0
  136. package/src/headers/20/node_builtins.h +197 -0
  137. package/src/headers/20/node_constants.h +82 -0
  138. package/src/headers/20/node_context_data.h +98 -0
  139. package/src/headers/20/node_contextify.h +206 -0
  140. package/src/headers/20/node_crypto.h +56 -0
  141. package/src/headers/20/node_dir.h +52 -0
  142. package/src/headers/20/node_dotenv.h +37 -0
  143. package/src/headers/20/node_errors.h +313 -0
  144. package/src/headers/20/node_exit_code.h +54 -0
  145. package/src/headers/20/node_external_reference.h +172 -0
  146. package/src/headers/20/node_file-inl.h +389 -0
  147. package/src/headers/20/node_file.h +537 -0
  148. package/src/headers/20/node_http2.h +1146 -0
  149. package/src/headers/20/node_http2_state.h +147 -0
  150. package/src/headers/20/node_http_common-inl.h +201 -0
  151. package/src/headers/20/node_http_common.h +532 -0
  152. package/src/headers/20/node_i18n.h +145 -0
  153. package/src/headers/20/node_internals.h +443 -0
  154. package/src/headers/20/node_main_instance.h +67 -0
  155. package/src/headers/20/node_mem-inl.h +112 -0
  156. package/src/headers/20/node_mem.h +45 -0
  157. package/src/headers/20/node_messaging.h +377 -0
  158. package/src/headers/20/node_metadata.h +138 -0
  159. package/src/headers/20/node_mutex.h +323 -0
  160. package/src/headers/20/node_object_wrap.h +132 -0
  161. package/src/headers/20/node_options-inl.h +478 -0
  162. package/src/headers/20/node_options.h +538 -0
  163. package/src/headers/20/node_perf.h +166 -0
  164. package/src/headers/20/node_perf_common.h +98 -0
  165. package/src/headers/20/node_platform.h +195 -0
  166. package/src/headers/20/node_process-inl.h +26 -0
  167. package/src/headers/20/node_process.h +99 -0
  168. package/src/headers/20/node_realm-inl.h +139 -0
  169. package/src/headers/20/node_realm.h +184 -0
  170. package/src/headers/20/node_report.h +42 -0
  171. package/src/headers/20/node_revert.h +82 -0
  172. package/src/headers/20/node_root_certs.h +3443 -0
  173. package/src/headers/20/node_sea.h +56 -0
  174. package/src/headers/20/node_shadow_realm.h +46 -0
  175. package/src/headers/20/node_snapshot_builder.h +54 -0
  176. package/src/headers/20/node_snapshotable.h +164 -0
  177. package/src/headers/20/node_sockaddr-inl.h +266 -0
  178. package/src/headers/20/node_sockaddr.h +405 -0
  179. package/src/headers/20/node_stat_watcher.h +73 -0
  180. package/src/headers/20/node_threadsafe_cow-inl.h +54 -0
  181. package/src/headers/20/node_threadsafe_cow.h +105 -0
  182. package/src/headers/20/node_union_bytes.h +81 -0
  183. package/src/headers/20/node_url.h +92 -0
  184. package/src/headers/20/node_v8.h +77 -0
  185. package/src/headers/20/node_v8_platform-inl.h +205 -0
  186. package/src/headers/20/node_version.h +109 -0
  187. package/src/headers/20/node_wasi.h +184 -0
  188. package/src/headers/20/node_wasm_web_api.h +55 -0
  189. package/src/headers/20/node_watchdog.h +154 -0
  190. package/src/headers/20/node_worker.h +151 -0
  191. package/src/headers/20/permission/child_process_permission.h +29 -0
  192. package/src/headers/20/permission/fs_permission.h +154 -0
  193. package/src/headers/20/permission/inspector_permission.h +29 -0
  194. package/src/headers/20/permission/permission.h +74 -0
  195. package/src/headers/20/permission/permission_base.h +53 -0
  196. package/src/headers/20/permission/worker_permission.h +29 -0
  197. package/src/headers/20/pipe_wrap.h +80 -0
  198. package/src/headers/20/req_wrap-inl.h +171 -0
  199. package/src/headers/20/req_wrap.h +77 -0
  200. package/src/headers/20/spawn_sync.h +242 -0
  201. package/src/headers/20/stream_base-inl.h +173 -0
  202. package/src/headers/20/stream_base.h +469 -0
  203. package/src/headers/20/stream_pipe.h +76 -0
  204. package/src/headers/20/stream_wrap.h +131 -0
  205. package/src/headers/20/string_bytes.h +120 -0
  206. package/src/headers/20/string_decoder-inl.h +37 -0
  207. package/src/headers/20/string_decoder.h +50 -0
  208. package/src/headers/20/string_search.h +638 -0
  209. package/src/headers/20/tcp_wrap.h +105 -0
  210. package/src/headers/20/threadpoolwork-inl.h +70 -0
  211. package/src/headers/20/timer_wrap-inl.h +32 -0
  212. package/src/headers/20/timer_wrap.h +85 -0
  213. package/src/headers/20/timers.h +70 -0
  214. package/src/headers/20/tracing/agent.h +195 -0
  215. package/src/headers/20/tracing/node_trace_buffer.h +83 -0
  216. package/src/headers/20/tracing/node_trace_writer.h +75 -0
  217. package/src/headers/20/tracing/trace_event.h +720 -0
  218. package/src/headers/20/tracing/trace_event_common.h +1109 -0
  219. package/src/headers/20/tracing/traced_value.h +70 -0
  220. package/src/headers/20/tty_wrap.h +65 -0
  221. package/src/headers/20/udp_wrap.h +227 -0
  222. package/src/headers/20/undici_version.h +6 -0
  223. package/src/headers/20/util-inl.h +623 -0
  224. package/src/headers/20/util.h +988 -0
  225. package/src/headers/20/v8-cppgc.h +245 -0
  226. package/src/headers/20/v8-fast-api-calls.h +957 -0
  227. package/dist/bindings/cws_darwin_arm64_node64.node +0 -0
  228. package/dist/bindings/cws_darwin_arm64_node72.node +0 -0
  229. package/dist/bindings/cws_darwin_arm64_node79.node +0 -0
  230. package/dist/bindings/cws_darwin_x64_node64.node +0 -0
  231. package/dist/bindings/cws_darwin_x64_node72.node +0 -0
  232. package/dist/bindings/cws_darwin_x64_node79.node +0 -0
  233. package/dist/bindings/cws_linux_x64_node64.node +0 -0
  234. package/dist/bindings/cws_linux_x64_node72.node +0 -0
  235. package/dist/bindings/cws_linux_x64_node79.node +0 -0
  236. package/dist/bindings/cws_win32_x64_node64.node +0 -0
  237. package/dist/bindings/cws_win32_x64_node72.node +0 -0
  238. package/dist/bindings/cws_win32_x64_node79.node +0 -0
@@ -0,0 +1,243 @@
1
+ #ifndef SRC_ALIASED_BUFFER_INL_H_
2
+ #define SRC_ALIASED_BUFFER_INL_H_
3
+
4
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
+
6
+ #include "aliased_buffer.h"
7
+ #include "util-inl.h"
8
+
9
+ namespace node {
10
+
11
+ typedef size_t AliasedBufferIndex;
12
+
13
+ template <typename NativeT, typename V8T>
14
+ AliasedBufferBase<NativeT, V8T>::AliasedBufferBase(
15
+ v8::Isolate* isolate, const size_t count, const AliasedBufferIndex* index)
16
+ : isolate_(isolate), count_(count), byte_offset_(0), index_(index) {
17
+ CHECK_GT(count, 0);
18
+ if (index != nullptr) {
19
+ // Will be deserialized later.
20
+ return;
21
+ }
22
+ const v8::HandleScope handle_scope(isolate_);
23
+ const size_t size_in_bytes =
24
+ MultiplyWithOverflowCheck(sizeof(NativeT), count);
25
+
26
+ // allocate v8 ArrayBuffer
27
+ v8::Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(isolate_, size_in_bytes);
28
+ buffer_ = static_cast<NativeT*>(ab->Data());
29
+
30
+ // allocate v8 TypedArray
31
+ v8::Local<V8T> js_array = V8T::New(ab, byte_offset_, count);
32
+ js_array_ = v8::Global<V8T>(isolate, js_array);
33
+ }
34
+
35
+ template <typename NativeT, typename V8T>
36
+ AliasedBufferBase<NativeT, V8T>::AliasedBufferBase(
37
+ v8::Isolate* isolate,
38
+ const size_t byte_offset,
39
+ const size_t count,
40
+ const AliasedBufferBase<uint8_t, v8::Uint8Array>& backing_buffer,
41
+ const AliasedBufferIndex* index)
42
+ : isolate_(isolate),
43
+ count_(count),
44
+ byte_offset_(byte_offset),
45
+ index_(index) {
46
+ if (index != nullptr) {
47
+ // Will be deserialized later.
48
+ return;
49
+ }
50
+ const v8::HandleScope handle_scope(isolate_);
51
+ v8::Local<v8::ArrayBuffer> ab = backing_buffer.GetArrayBuffer();
52
+
53
+ // validate that the byte_offset is aligned with sizeof(NativeT)
54
+ CHECK_EQ(byte_offset & (sizeof(NativeT) - 1), 0);
55
+ // validate this fits inside the backing buffer
56
+ CHECK_LE(MultiplyWithOverflowCheck(sizeof(NativeT), count),
57
+ ab->ByteLength() - byte_offset);
58
+
59
+ buffer_ = reinterpret_cast<NativeT*>(
60
+ const_cast<uint8_t*>(backing_buffer.GetNativeBuffer() + byte_offset));
61
+
62
+ v8::Local<V8T> js_array = V8T::New(ab, byte_offset, count);
63
+ js_array_ = v8::Global<V8T>(isolate, js_array);
64
+ }
65
+
66
+ template <typename NativeT, typename V8T>
67
+ AliasedBufferBase<NativeT, V8T>::AliasedBufferBase(
68
+ const AliasedBufferBase& that)
69
+ : isolate_(that.isolate_),
70
+ count_(that.count_),
71
+ byte_offset_(that.byte_offset_),
72
+ buffer_(that.buffer_) {
73
+ js_array_ = v8::Global<V8T>(that.isolate_, that.GetJSArray());
74
+ DCHECK(is_valid());
75
+ }
76
+
77
+ template <typename NativeT, typename V8T>
78
+ AliasedBufferIndex AliasedBufferBase<NativeT, V8T>::Serialize(
79
+ v8::Local<v8::Context> context, v8::SnapshotCreator* creator) {
80
+ DCHECK(is_valid());
81
+ return creator->AddData(context, GetJSArray());
82
+ }
83
+
84
+ template <typename NativeT, typename V8T>
85
+ inline void AliasedBufferBase<NativeT, V8T>::Deserialize(
86
+ v8::Local<v8::Context> context) {
87
+ DCHECK_NOT_NULL(index_);
88
+ v8::Local<V8T> arr =
89
+ context->GetDataFromSnapshotOnce<V8T>(*index_).ToLocalChecked();
90
+ // These may not hold true for AliasedBuffers that have grown, so should
91
+ // be removed when we expand the snapshot support.
92
+ DCHECK_EQ(count_, arr->Length());
93
+ DCHECK_EQ(byte_offset_, arr->ByteOffset());
94
+ uint8_t* raw = static_cast<uint8_t*>(arr->Buffer()->Data());
95
+ buffer_ = reinterpret_cast<NativeT*>(raw + byte_offset_);
96
+ js_array_.Reset(isolate_, arr);
97
+ index_ = nullptr;
98
+ }
99
+
100
+ template <typename NativeT, typename V8T>
101
+ AliasedBufferBase<NativeT, V8T>& AliasedBufferBase<NativeT, V8T>::operator=(
102
+ AliasedBufferBase<NativeT, V8T>&& that) noexcept {
103
+ DCHECK(is_valid());
104
+ this->~AliasedBufferBase();
105
+ isolate_ = that.isolate_;
106
+ count_ = that.count_;
107
+ byte_offset_ = that.byte_offset_;
108
+ buffer_ = that.buffer_;
109
+
110
+ js_array_.Reset(isolate_, that.js_array_.Get(isolate_));
111
+
112
+ that.buffer_ = nullptr;
113
+ that.js_array_.Reset();
114
+ return *this;
115
+ }
116
+
117
+ template <typename NativeT, typename V8T>
118
+ v8::Local<V8T> AliasedBufferBase<NativeT, V8T>::GetJSArray() const {
119
+ DCHECK(is_valid());
120
+ return js_array_.Get(isolate_);
121
+ }
122
+
123
+ template <typename NativeT, typename V8T>
124
+ void AliasedBufferBase<NativeT, V8T>::Release() {
125
+ DCHECK_NULL(index_);
126
+ js_array_.Reset();
127
+ }
128
+
129
+ template <typename NativeT, typename V8T>
130
+ inline void AliasedBufferBase<NativeT, V8T>::MakeWeak() {
131
+ DCHECK(is_valid());
132
+ js_array_.SetWeak();
133
+ }
134
+
135
+ template <typename NativeT, typename V8T>
136
+ v8::Local<v8::ArrayBuffer> AliasedBufferBase<NativeT, V8T>::GetArrayBuffer()
137
+ const {
138
+ return GetJSArray()->Buffer();
139
+ }
140
+
141
+ template <typename NativeT, typename V8T>
142
+ inline const NativeT* AliasedBufferBase<NativeT, V8T>::GetNativeBuffer() const {
143
+ DCHECK(is_valid());
144
+ return buffer_;
145
+ }
146
+
147
+ template <typename NativeT, typename V8T>
148
+ inline const NativeT* AliasedBufferBase<NativeT, V8T>::operator*() const {
149
+ return GetNativeBuffer();
150
+ }
151
+
152
+ template <typename NativeT, typename V8T>
153
+ inline void AliasedBufferBase<NativeT, V8T>::SetValue(const size_t index,
154
+ NativeT value) {
155
+ DCHECK_LT(index, count_);
156
+ DCHECK(is_valid());
157
+ buffer_[index] = value;
158
+ }
159
+
160
+ template <typename NativeT, typename V8T>
161
+ inline const NativeT AliasedBufferBase<NativeT, V8T>::GetValue(
162
+ const size_t index) const {
163
+ DCHECK(is_valid());
164
+ DCHECK_LT(index, count_);
165
+ return buffer_[index];
166
+ }
167
+
168
+ template <typename NativeT, typename V8T>
169
+ typename AliasedBufferBase<NativeT, V8T>::Reference
170
+ AliasedBufferBase<NativeT, V8T>::operator[](size_t index) {
171
+ DCHECK(is_valid());
172
+ return Reference(this, index);
173
+ }
174
+
175
+ template <typename NativeT, typename V8T>
176
+ NativeT AliasedBufferBase<NativeT, V8T>::operator[](size_t index) const {
177
+ return GetValue(index);
178
+ }
179
+
180
+ template <typename NativeT, typename V8T>
181
+ size_t AliasedBufferBase<NativeT, V8T>::Length() const {
182
+ return count_;
183
+ }
184
+
185
+ template <typename NativeT, typename V8T>
186
+ void AliasedBufferBase<NativeT, V8T>::reserve(size_t new_capacity) {
187
+ DCHECK(is_valid());
188
+ DCHECK_GE(new_capacity, count_);
189
+ DCHECK_EQ(byte_offset_, 0);
190
+ const v8::HandleScope handle_scope(isolate_);
191
+
192
+ const size_t old_size_in_bytes = sizeof(NativeT) * count_;
193
+ const size_t new_size_in_bytes =
194
+ MultiplyWithOverflowCheck(sizeof(NativeT), new_capacity);
195
+
196
+ // allocate v8 new ArrayBuffer
197
+ v8::Local<v8::ArrayBuffer> ab =
198
+ v8::ArrayBuffer::New(isolate_, new_size_in_bytes);
199
+
200
+ // allocate new native buffer
201
+ NativeT* new_buffer = static_cast<NativeT*>(ab->Data());
202
+ // copy old content
203
+ memcpy(new_buffer, buffer_, old_size_in_bytes);
204
+
205
+ // allocate v8 TypedArray
206
+ v8::Local<V8T> js_array = V8T::New(ab, byte_offset_, new_capacity);
207
+
208
+ // move over old v8 TypedArray
209
+ js_array_ = std::move(v8::Global<V8T>(isolate_, js_array));
210
+
211
+ buffer_ = new_buffer;
212
+ count_ = new_capacity;
213
+ }
214
+
215
+ template <typename NativeT, typename V8T>
216
+ inline bool AliasedBufferBase<NativeT, V8T>::is_valid() const {
217
+ return index_ == nullptr && !js_array_.IsEmpty();
218
+ }
219
+
220
+ template <typename NativeT, typename V8T>
221
+ inline size_t AliasedBufferBase<NativeT, V8T>::SelfSize() const {
222
+ return sizeof(*this);
223
+ }
224
+
225
+ #define VM(NativeT, V8T) \
226
+ template <> \
227
+ inline const char* AliasedBufferBase<NativeT, v8::V8T>::MemoryInfoName() \
228
+ const { \
229
+ return "Aliased" #V8T; \
230
+ } \
231
+ template <> \
232
+ inline void AliasedBufferBase<NativeT, v8::V8T>::MemoryInfo( \
233
+ node::MemoryTracker* tracker) const { \
234
+ tracker->TrackField("js_array", js_array_); \
235
+ }
236
+ ALIASED_BUFFER_LIST(VM)
237
+ #undef VM
238
+
239
+ } // namespace node
240
+
241
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
242
+
243
+ #endif // SRC_ALIASED_BUFFER_INL_H_
@@ -0,0 +1,206 @@
1
+ #ifndef SRC_ALIASED_BUFFER_H_
2
+ #define SRC_ALIASED_BUFFER_H_
3
+
4
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
+
6
+ #include <cinttypes>
7
+ #include "memory_tracker.h"
8
+ #include "v8.h"
9
+
10
+ namespace node {
11
+
12
+ typedef size_t AliasedBufferIndex;
13
+
14
+ /**
15
+ * Do not use this class directly when creating instances of it - use the
16
+ * Aliased*Array defined at the end of this file instead.
17
+ *
18
+ * This class encapsulates the technique of having a native buffer mapped to
19
+ * a JS object. Writes to the native buffer can happen efficiently without
20
+ * going through JS, and the data is then available to user's via the exposed
21
+ * JS object.
22
+ *
23
+ * While this technique is computationally efficient, it is effectively a
24
+ * write to JS program state w/out going through the standard
25
+ * (monitored) API. Thus any VM capabilities to detect the modification are
26
+ * circumvented.
27
+ *
28
+ * The encapsulation herein provides a placeholder where such writes can be
29
+ * observed. Any notification APIs will be left as a future exercise.
30
+ */
31
+ template <class NativeT, class V8T>
32
+ class AliasedBufferBase : public MemoryRetainer {
33
+ public:
34
+ static_assert(std::is_scalar<NativeT>::value);
35
+
36
+ AliasedBufferBase(v8::Isolate* isolate,
37
+ const size_t count,
38
+ const AliasedBufferIndex* index = nullptr);
39
+
40
+ /**
41
+ * Create an AliasedBufferBase over a sub-region of another aliased buffer.
42
+ * The two will share a v8::ArrayBuffer instance &
43
+ * a native buffer, but will each read/write to different sections of the
44
+ * native buffer.
45
+ *
46
+ * Note that byte_offset must by aligned by sizeof(NativeT).
47
+ */
48
+ // TODO(refack): refactor into a non-owning `AliasedBufferBaseView`
49
+ AliasedBufferBase(
50
+ v8::Isolate* isolate,
51
+ const size_t byte_offset,
52
+ const size_t count,
53
+ const AliasedBufferBase<uint8_t, v8::Uint8Array>& backing_buffer,
54
+ const AliasedBufferIndex* index = nullptr);
55
+
56
+ AliasedBufferBase(const AliasedBufferBase& that);
57
+
58
+ AliasedBufferIndex Serialize(v8::Local<v8::Context> context,
59
+ v8::SnapshotCreator* creator);
60
+
61
+ inline void Deserialize(v8::Local<v8::Context> context);
62
+
63
+ AliasedBufferBase& operator=(AliasedBufferBase&& that) noexcept;
64
+
65
+ /**
66
+ * Helper class that is returned from operator[] to support assignment into
67
+ * a specified location.
68
+ */
69
+ class Reference {
70
+ public:
71
+ Reference(AliasedBufferBase<NativeT, V8T>* aliased_buffer, size_t index)
72
+ : aliased_buffer_(aliased_buffer), index_(index) {}
73
+
74
+ Reference(const Reference& that)
75
+ : aliased_buffer_(that.aliased_buffer_),
76
+ index_(that.index_) {
77
+ }
78
+
79
+ inline Reference& operator=(const NativeT& val) {
80
+ aliased_buffer_->SetValue(index_, val);
81
+ return *this;
82
+ }
83
+
84
+ inline Reference& operator=(const Reference& val) {
85
+ return *this = static_cast<NativeT>(val);
86
+ }
87
+
88
+ operator NativeT() const {
89
+ return aliased_buffer_->GetValue(index_);
90
+ }
91
+
92
+ inline Reference& operator+=(const NativeT& val) {
93
+ const NativeT current = aliased_buffer_->GetValue(index_);
94
+ aliased_buffer_->SetValue(index_, current + val);
95
+ return *this;
96
+ }
97
+
98
+ inline Reference& operator+=(const Reference& val) {
99
+ return this->operator+=(static_cast<NativeT>(val));
100
+ }
101
+
102
+ inline Reference& operator-=(const NativeT& val) {
103
+ const NativeT current = aliased_buffer_->GetValue(index_);
104
+ aliased_buffer_->SetValue(index_, current - val);
105
+ return *this;
106
+ }
107
+
108
+ private:
109
+ AliasedBufferBase<NativeT, V8T>* aliased_buffer_;
110
+ size_t index_;
111
+ };
112
+
113
+ /**
114
+ * Get the underlying v8 TypedArray overlayed on top of the native buffer
115
+ */
116
+ v8::Local<V8T> GetJSArray() const;
117
+
118
+ void Release();
119
+
120
+ /**
121
+ * Make the global reference to the typed array weak. The caller must make
122
+ * sure that no operation can be done on the AliasedBuffer when the typed
123
+ * array becomes unreachable. Usually this means the caller must maintain
124
+ * a JS reference to the typed array from JS object.
125
+ */
126
+ inline void MakeWeak();
127
+
128
+ /**
129
+ * Get the underlying v8::ArrayBuffer underlying the TypedArray and
130
+ * overlaying the native buffer
131
+ */
132
+ v8::Local<v8::ArrayBuffer> GetArrayBuffer() const;
133
+
134
+ /**
135
+ * Get the underlying native buffer. Note that all reads/writes should occur
136
+ * through the GetValue/SetValue/operator[] methods
137
+ */
138
+ inline const NativeT* GetNativeBuffer() const;
139
+
140
+ /**
141
+ * Synonym for GetBuffer()
142
+ */
143
+ inline const NativeT* operator*() const;
144
+
145
+ /**
146
+ * Set position index to given value.
147
+ */
148
+ inline void SetValue(const size_t index, NativeT value);
149
+
150
+ /**
151
+ * Get value at position index
152
+ */
153
+ inline const NativeT GetValue(const size_t index) const;
154
+
155
+ /**
156
+ * Effectively, a synonym for GetValue/SetValue
157
+ */
158
+ Reference operator[](size_t index);
159
+
160
+ NativeT operator[](size_t index) const;
161
+
162
+ size_t Length() const;
163
+
164
+ // Should only be used to extend the array.
165
+ // Should only be used on an owning array, not one created as a sub array of
166
+ // an owning `AliasedBufferBase`.
167
+ void reserve(size_t new_capacity);
168
+
169
+ inline size_t SelfSize() const override;
170
+
171
+ inline const char* MemoryInfoName() const override;
172
+ inline void MemoryInfo(node::MemoryTracker* tracker) const override;
173
+
174
+ private:
175
+ inline bool is_valid() const;
176
+ v8::Isolate* isolate_ = nullptr;
177
+ size_t count_ = 0;
178
+ size_t byte_offset_ = 0;
179
+ NativeT* buffer_ = nullptr;
180
+ v8::Global<V8T> js_array_;
181
+
182
+ // Deserialize data
183
+ const AliasedBufferIndex* index_ = nullptr;
184
+ };
185
+
186
+ #define ALIASED_BUFFER_LIST(V) \
187
+ V(int8_t, Int8Array) \
188
+ V(uint8_t, Uint8Array) \
189
+ V(int16_t, Int16Array) \
190
+ V(uint16_t, Uint16Array) \
191
+ V(int32_t, Int32Array) \
192
+ V(uint32_t, Uint32Array) \
193
+ V(float, Float32Array) \
194
+ V(double, Float64Array) \
195
+ V(int64_t, BigInt64Array)
196
+
197
+ #define V(NativeT, V8T) \
198
+ typedef AliasedBufferBase<NativeT, v8::V8T> Aliased##V8T;
199
+ ALIASED_BUFFER_LIST(V)
200
+ #undef V
201
+
202
+ } // namespace node
203
+
204
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
205
+
206
+ #endif // SRC_ALIASED_BUFFER_H_
@@ -0,0 +1,54 @@
1
+ #ifndef SRC_ALIASED_STRUCT_INL_H_
2
+ #define SRC_ALIASED_STRUCT_INL_H_
3
+
4
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
+
6
+ #include "aliased_struct.h"
7
+ #include "v8.h"
8
+ #include <memory>
9
+
10
+ namespace node {
11
+
12
+ template <typename T>
13
+ template <typename... Args>
14
+ AliasedStruct<T>::AliasedStruct(v8::Isolate* isolate, Args&&... args)
15
+ : isolate_(isolate) {
16
+ const v8::HandleScope handle_scope(isolate);
17
+
18
+ store_ = v8::ArrayBuffer::NewBackingStore(isolate, sizeof(T));
19
+ ptr_ = new (store_->Data()) T(std::forward<Args>(args)...);
20
+ DCHECK_NOT_NULL(ptr_);
21
+
22
+ v8::Local<v8::ArrayBuffer> buffer = v8::ArrayBuffer::New(isolate, store_);
23
+ buffer_ = v8::Global<v8::ArrayBuffer>(isolate, buffer);
24
+ }
25
+
26
+ template <typename T>
27
+ AliasedStruct<T>::AliasedStruct(const AliasedStruct& that)
28
+ : AliasedStruct(that.isolate_, *that) {}
29
+
30
+ template <typename T>
31
+ AliasedStruct<T>& AliasedStruct<T>::operator=(
32
+ AliasedStruct<T>&& that) noexcept {
33
+ this->~AliasedStruct();
34
+ isolate_ = that.isolate_;
35
+ store_ = that.store_;
36
+ ptr_ = that.ptr_;
37
+
38
+ buffer_ = std::move(that.buffer_);
39
+
40
+ that.ptr_ = nullptr;
41
+ that.store_.reset();
42
+ return *this;
43
+ }
44
+
45
+ template <typename T>
46
+ AliasedStruct<T>::~AliasedStruct() {
47
+ if (ptr_ != nullptr) ptr_->~T();
48
+ }
49
+
50
+ } // namespace node
51
+
52
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
53
+
54
+ #endif // SRC_ALIASED_STRUCT_INL_H_
@@ -0,0 +1,63 @@
1
+ #ifndef SRC_ALIASED_STRUCT_H_
2
+ #define SRC_ALIASED_STRUCT_H_
3
+
4
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
+
6
+ #include "node_internals.h"
7
+ #include "v8.h"
8
+ #include <memory>
9
+
10
+ namespace node {
11
+
12
+ // AliasedStruct is a utility that allows uses a V8 Backing Store
13
+ // to be exposed to the C++/C side as a struct and to the
14
+ // JavaScript side as an ArrayBuffer to efficiently share
15
+ // data without marshalling. It is similar in nature to
16
+ // AliasedBuffer.
17
+ //
18
+ // struct Foo { int x; }
19
+ //
20
+ // AliasedStruct<Foo> foo;
21
+ // foo->x = 1;
22
+ //
23
+ // Local<ArrayBuffer> ab = foo.GetArrayBuffer();
24
+ template <typename T>
25
+ class AliasedStruct final {
26
+ public:
27
+ template <typename... Args>
28
+ explicit AliasedStruct(v8::Isolate* isolate, Args&&... args);
29
+
30
+ inline AliasedStruct(const AliasedStruct& that);
31
+
32
+ inline ~AliasedStruct();
33
+
34
+ inline AliasedStruct& operator=(AliasedStruct&& that) noexcept;
35
+
36
+ v8::Local<v8::ArrayBuffer> GetArrayBuffer() const {
37
+ return buffer_.Get(isolate_);
38
+ }
39
+
40
+ const T* Data() const { return ptr_; }
41
+
42
+ T* Data() { return ptr_; }
43
+
44
+ const T& operator*() const { return *ptr_; }
45
+
46
+ T& operator*() { return *ptr_; }
47
+
48
+ const T* operator->() const { return ptr_; }
49
+
50
+ T* operator->() { return ptr_; }
51
+
52
+ private:
53
+ v8::Isolate* isolate_;
54
+ std::shared_ptr<v8::BackingStore> store_;
55
+ T* ptr_;
56
+ v8::Global<v8::ArrayBuffer> buffer_;
57
+ };
58
+
59
+ } // namespace node
60
+
61
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
62
+
63
+ #endif // SRC_ALIASED_STRUCT_H_
@@ -0,0 +1,93 @@
1
+ // Copyright Joyent, Inc. and other Node contributors.
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a
4
+ // copy of this software and associated documentation files (the
5
+ // "Software"), to deal in the Software without restriction, including
6
+ // without limitation the rights to use, copy, modify, merge, publish,
7
+ // distribute, sublicense, and/or sell copies of the Software, and to permit
8
+ // persons to whom the Software is furnished to do so, subject to the
9
+ // following conditions:
10
+ //
11
+ // The above copyright notice and this permission notice shall be included
12
+ // in all copies or substantial portions of the Software.
13
+ //
14
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
+ // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
+ // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
+ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
+ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
+ // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ #ifndef SRC_ASYNC_WRAP_INL_H_
23
+ #define SRC_ASYNC_WRAP_INL_H_
24
+
25
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26
+
27
+ #include "async_wrap.h"
28
+ #include "base_object-inl.h"
29
+ #include "node_internals.h"
30
+
31
+ namespace node {
32
+
33
+ inline AsyncWrap::ProviderType AsyncWrap::provider_type() const {
34
+ return provider_type_;
35
+ }
36
+
37
+ inline AsyncWrap::ProviderType AsyncWrap::set_provider_type(
38
+ AsyncWrap::ProviderType provider) {
39
+ provider_type_ = provider;
40
+ return provider_type_;
41
+ }
42
+
43
+ inline double AsyncWrap::get_async_id() const {
44
+ return async_id_;
45
+ }
46
+
47
+
48
+ inline double AsyncWrap::get_trigger_async_id() const {
49
+ return trigger_async_id_;
50
+ }
51
+
52
+
53
+ inline v8::MaybeLocal<v8::Value> AsyncWrap::MakeCallback(
54
+ const v8::Local<v8::String> symbol,
55
+ int argc,
56
+ v8::Local<v8::Value>* argv) {
57
+ return MakeCallback(symbol.As<v8::Name>(), argc, argv);
58
+ }
59
+
60
+
61
+ inline v8::MaybeLocal<v8::Value> AsyncWrap::MakeCallback(
62
+ const v8::Local<v8::Symbol> symbol,
63
+ int argc,
64
+ v8::Local<v8::Value>* argv) {
65
+ return MakeCallback(symbol.As<v8::Name>(), argc, argv);
66
+ }
67
+
68
+
69
+ inline v8::MaybeLocal<v8::Value> AsyncWrap::MakeCallback(
70
+ const v8::Local<v8::Name> symbol,
71
+ int argc,
72
+ v8::Local<v8::Value>* argv) {
73
+ v8::Local<v8::Value> cb_v;
74
+ if (!object()->Get(env()->context(), symbol).ToLocal(&cb_v))
75
+ return v8::MaybeLocal<v8::Value>();
76
+ if (!cb_v->IsFunction()) {
77
+ v8::Isolate* isolate = env()->isolate();
78
+ return Undefined(isolate);
79
+ }
80
+ return MakeCallback(cb_v.As<v8::Function>(), argc, argv);
81
+ }
82
+
83
+ // static
84
+ inline v8::Local<v8::FunctionTemplate> AsyncWrap::GetConstructorTemplate(
85
+ Environment* env) {
86
+ return GetConstructorTemplate(env->isolate_data());
87
+ }
88
+
89
+ } // namespace node
90
+
91
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
92
+
93
+ #endif // SRC_ASYNC_WRAP_INL_H_