@encharm/cws 4.5.1 → 4.5.3

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 (249) hide show
  1. package/README.md +3 -5
  2. package/binding.gyp +1 -1
  3. package/dist/bindings/cws_darwin_arm64_node115.node +0 -0
  4. package/dist/bindings/cws_darwin_arm64_node115.node.dSYM/Contents/Info.plist +20 -0
  5. package/dist/bindings/cws_darwin_arm64_node115.node.dSYM/Contents/Resources/DWARF/cws_darwin_arm64_node115.node +0 -0
  6. package/dist/bindings/cws_darwin_arm64_node115.node.dSYM/Contents/Resources/Relocations/aarch64/cws_darwin_arm64_node115.node.yml +931 -0
  7. package/dist/bindings/cws_darwin_arm64_node127.node +0 -0
  8. package/dist/bindings/cws_darwin_arm64_node127.node.dSYM/Contents/Info.plist +20 -0
  9. package/dist/bindings/cws_darwin_arm64_node127.node.dSYM/Contents/Resources/DWARF/cws_darwin_arm64_node127.node +0 -0
  10. package/dist/bindings/cws_darwin_arm64_node127.node.dSYM/Contents/Resources/Relocations/aarch64/cws_darwin_arm64_node127.node.yml +931 -0
  11. package/dist/bindings/cws_darwin_arm64_node137.node +0 -0
  12. package/dist/bindings/cws_linux_x64_node137.node +0 -0
  13. package/dist/bindings/cws_win32_x64_node137.node +0 -0
  14. package/package.json +1 -1
  15. package/src/Addon.h +6 -1
  16. package/src/headers/24/acorn_version.h +6 -0
  17. package/src/headers/24/addon_permission.h +31 -0
  18. package/src/headers/24/agent.h +195 -0
  19. package/src/headers/24/aliased_buffer-inl.h +244 -0
  20. package/src/headers/24/aliased_buffer.h +206 -0
  21. package/src/headers/24/aliased_struct-inl.h +54 -0
  22. package/src/headers/24/aliased_struct.h +63 -0
  23. package/src/headers/24/amaro_version.h +6 -0
  24. package/src/headers/24/application.h +165 -0
  25. package/src/headers/24/async_context_frame.h +33 -0
  26. package/src/headers/24/async_wrap-inl.h +96 -0
  27. package/src/headers/24/async_wrap.h +244 -0
  28. package/src/headers/24/base_object-inl.h +330 -0
  29. package/src/headers/24/base_object.h +338 -0
  30. package/src/headers/24/base_object_types.h +74 -0
  31. package/src/headers/24/bindingdata.h +272 -0
  32. package/src/headers/24/blob_serializer_deserializer-inl.h +385 -0
  33. package/src/headers/24/blob_serializer_deserializer.h +132 -0
  34. package/src/headers/24/callback_queue-inl.h +97 -0
  35. package/src/headers/24/callback_queue.h +79 -0
  36. package/src/headers/24/cares_wrap.h +447 -0
  37. package/src/headers/24/child_process_permission.h +31 -0
  38. package/src/headers/24/cid.h +127 -0
  39. package/src/headers/24/cjs_module_lexer_version.h +6 -0
  40. package/src/headers/24/cleanup_queue-inl.h +36 -0
  41. package/src/headers/24/cleanup_queue.h +79 -0
  42. package/src/headers/24/compile_cache.h +113 -0
  43. package/src/headers/24/connect_wrap.h +26 -0
  44. package/src/headers/24/connection_wrap.h +30 -0
  45. package/src/headers/24/cppgc_helpers-inl.h +65 -0
  46. package/src/headers/24/cppgc_helpers.h +162 -0
  47. package/src/headers/24/crypto/crypto_aes.h +97 -0
  48. package/src/headers/24/crypto/crypto_argon2.h +86 -0
  49. package/src/headers/24/crypto/crypto_bio.h +193 -0
  50. package/src/headers/24/crypto/crypto_chacha20_poly1305.h +64 -0
  51. package/src/headers/24/crypto/crypto_cipher.h +281 -0
  52. package/src/headers/24/crypto/crypto_clienthello-inl.h +90 -0
  53. package/src/headers/24/crypto/crypto_clienthello.h +131 -0
  54. package/src/headers/24/crypto/crypto_common.h +48 -0
  55. package/src/headers/24/crypto/crypto_context.h +171 -0
  56. package/src/headers/24/crypto/crypto_dh.h +126 -0
  57. package/src/headers/24/crypto/crypto_dsa.h +50 -0
  58. package/src/headers/24/crypto/crypto_ec.h +163 -0
  59. package/src/headers/24/crypto/crypto_hash.h +89 -0
  60. package/src/headers/24/crypto/crypto_hkdf.h +61 -0
  61. package/src/headers/24/crypto/crypto_hmac.h +92 -0
  62. package/src/headers/24/crypto/crypto_kem.h +113 -0
  63. package/src/headers/24/crypto/crypto_keygen.h +290 -0
  64. package/src/headers/24/crypto/crypto_keys.h +392 -0
  65. package/src/headers/24/crypto/crypto_ml_dsa.h +21 -0
  66. package/src/headers/24/crypto/crypto_pbkdf2.h +74 -0
  67. package/src/headers/24/crypto/crypto_random.h +124 -0
  68. package/src/headers/24/crypto/crypto_rsa.h +136 -0
  69. package/src/headers/24/crypto/crypto_scrypt.h +85 -0
  70. package/src/headers/24/crypto/crypto_sig.h +156 -0
  71. package/src/headers/24/crypto/crypto_spkac.h +21 -0
  72. package/src/headers/24/crypto/crypto_timing.h +20 -0
  73. package/src/headers/24/crypto/crypto_tls.h +309 -0
  74. package/src/headers/24/crypto/crypto_util.h +598 -0
  75. package/src/headers/24/crypto/crypto_x509.h +140 -0
  76. package/src/headers/24/data.h +148 -0
  77. package/src/headers/24/debug_utils-inl.h +232 -0
  78. package/src/headers/24/debug_utils.h +193 -0
  79. package/src/headers/24/defs.h +254 -0
  80. package/src/headers/24/diagnosticfilename-inl.h +33 -0
  81. package/src/headers/24/embedded_data.h +17 -0
  82. package/src/headers/24/encoding_binding.h +60 -0
  83. package/src/headers/24/endpoint.h +421 -0
  84. package/src/headers/24/env-inl.h +935 -0
  85. package/src/headers/24/env.h +1257 -0
  86. package/src/headers/24/env_properties.h +561 -0
  87. package/src/headers/24/fs_permission.h +167 -0
  88. package/src/headers/24/handle_wrap.h +123 -0
  89. package/src/headers/24/histogram-inl.h +107 -0
  90. package/src/headers/24/histogram.h +279 -0
  91. package/src/headers/24/http3.h +44 -0
  92. package/src/headers/24/inspector_agent.h +165 -0
  93. package/src/headers/24/inspector_io.h +78 -0
  94. package/src/headers/24/inspector_permission.h +31 -0
  95. package/src/headers/24/inspector_profiler.h +149 -0
  96. package/src/headers/24/inspector_socket.h +60 -0
  97. package/src/headers/24/inspector_socket_server.h +110 -0
  98. package/src/headers/24/io_agent.h +30 -0
  99. package/src/headers/24/js_native_api.h +615 -0
  100. package/src/headers/24/js_native_api_types.h +211 -0
  101. package/src/headers/24/js_native_api_v8.h +485 -0
  102. package/src/headers/24/js_native_api_v8_internals.h +45 -0
  103. package/src/headers/24/js_stream.h +52 -0
  104. package/src/headers/24/json_parser.h +40 -0
  105. package/src/headers/24/json_utils.h +172 -0
  106. package/src/headers/24/logstream.h +81 -0
  107. package/src/headers/24/main_thread_interface.h +116 -0
  108. package/src/headers/24/memory_tracker-inl.h +454 -0
  109. package/src/headers/24/memory_tracker.h +333 -0
  110. package/src/headers/24/module_wrap.h +229 -0
  111. package/src/headers/24/ncrypto.h +1625 -0
  112. package/src/headers/24/network_agent.h +118 -0
  113. package/src/headers/24/network_inspector.h +45 -0
  114. package/src/headers/24/network_resource_manager.h +29 -0
  115. package/src/headers/24/node.h +1620 -0
  116. package/src/headers/24/node_api.h +269 -0
  117. package/src/headers/24/node_api_internals.h +46 -0
  118. package/src/headers/24/node_api_types.h +52 -0
  119. package/src/headers/24/node_binding.h +163 -0
  120. package/src/headers/24/node_blob.h +159 -0
  121. package/src/headers/24/node_bob-inl.h +36 -0
  122. package/src/headers/24/node_bob.h +107 -0
  123. package/src/headers/24/node_buffer.h +92 -0
  124. package/src/headers/24/node_builtins.h +223 -0
  125. package/src/headers/24/node_config_file.h +65 -0
  126. package/src/headers/24/node_constants.h +82 -0
  127. package/src/headers/24/node_context_data.h +168 -0
  128. package/src/headers/24/node_contextify.h +261 -0
  129. package/src/headers/24/node_crypto.h +62 -0
  130. package/src/headers/24/node_debug.h +25 -0
  131. package/src/headers/24/node_dir.h +52 -0
  132. package/src/headers/24/node_dotenv.h +45 -0
  133. package/src/headers/24/node_errors.h +391 -0
  134. package/src/headers/24/node_exit_code.h +54 -0
  135. package/src/headers/24/node_external_reference.h +167 -0
  136. package/src/headers/24/node_file-inl.h +412 -0
  137. package/src/headers/24/node_file.h +540 -0
  138. package/src/headers/24/node_http2.h +1175 -0
  139. package/src/headers/24/node_http2_state.h +159 -0
  140. package/src/headers/24/node_http_common-inl.h +198 -0
  141. package/src/headers/24/node_http_common.h +535 -0
  142. package/src/headers/24/node_i18n.h +132 -0
  143. package/src/headers/24/node_internals.h +476 -0
  144. package/src/headers/24/node_json.h +24 -0
  145. package/src/headers/24/node_large_page.h +34 -0
  146. package/src/headers/24/node_locks.h +180 -0
  147. package/src/headers/24/node_main_instance.h +67 -0
  148. package/src/headers/24/node_mem-inl.h +112 -0
  149. package/src/headers/24/node_mem.h +45 -0
  150. package/src/headers/24/node_messaging.h +389 -0
  151. package/src/headers/24/node_metadata.h +164 -0
  152. package/src/headers/24/node_modules.h +93 -0
  153. package/src/headers/24/node_mutex.h +323 -0
  154. package/src/headers/24/node_object_wrap.h +132 -0
  155. package/src/headers/24/node_options-inl.h +524 -0
  156. package/src/headers/24/node_options.h +682 -0
  157. package/src/headers/24/node_perf.h +166 -0
  158. package/src/headers/24/node_perf_common.h +102 -0
  159. package/src/headers/24/node_platform.h +286 -0
  160. package/src/headers/24/node_process-inl.h +26 -0
  161. package/src/headers/24/node_process.h +111 -0
  162. package/src/headers/24/node_realm-inl.h +153 -0
  163. package/src/headers/24/node_realm.h +236 -0
  164. package/src/headers/24/node_report.h +43 -0
  165. package/src/headers/24/node_revert.h +82 -0
  166. package/src/headers/24/node_root_certs.h +3546 -0
  167. package/src/headers/24/node_sea.h +74 -0
  168. package/src/headers/24/node_shadow_realm.h +46 -0
  169. package/src/headers/24/node_snapshot_builder.h +57 -0
  170. package/src/headers/24/node_snapshotable.h +174 -0
  171. package/src/headers/24/node_sockaddr-inl.h +257 -0
  172. package/src/headers/24/node_sockaddr.h +394 -0
  173. package/src/headers/24/node_sqlite.h +270 -0
  174. package/src/headers/24/node_stat_watcher.h +73 -0
  175. package/src/headers/24/node_string.h +101 -0
  176. package/src/headers/24/node_task_runner.h +92 -0
  177. package/src/headers/24/node_threadsafe_cow-inl.h +54 -0
  178. package/src/headers/24/node_threadsafe_cow.h +105 -0
  179. package/src/headers/24/node_trace_buffer.h +83 -0
  180. package/src/headers/24/node_trace_writer.h +75 -0
  181. package/src/headers/24/node_union_bytes.h +81 -0
  182. package/src/headers/24/node_url.h +101 -0
  183. package/src/headers/24/node_url_pattern.h +118 -0
  184. package/src/headers/24/node_v8.h +77 -0
  185. package/src/headers/24/node_v8_platform-inl.h +185 -0
  186. package/src/headers/24/node_version.h +110 -0
  187. package/src/headers/24/node_wasi.h +185 -0
  188. package/src/headers/24/node_wasm_web_api.h +55 -0
  189. package/src/headers/24/node_watchdog.h +154 -0
  190. package/src/headers/24/node_webstorage.h +60 -0
  191. package/src/headers/24/node_worker.h +159 -0
  192. package/src/headers/24/packet.h +155 -0
  193. package/src/headers/24/path.h +33 -0
  194. package/src/headers/24/permission/addon_permission.h +31 -0
  195. package/src/headers/24/permission/child_process_permission.h +31 -0
  196. package/src/headers/24/permission/fs_permission.h +167 -0
  197. package/src/headers/24/permission/inspector_permission.h +31 -0
  198. package/src/headers/24/permission/permission.h +100 -0
  199. package/src/headers/24/permission/permission_base.h +65 -0
  200. package/src/headers/24/permission/wasi_permission.h +31 -0
  201. package/src/headers/24/permission/worker_permission.h +31 -0
  202. package/src/headers/24/permission.h +100 -0
  203. package/src/headers/24/permission_base.h +65 -0
  204. package/src/headers/24/pipe_wrap.h +80 -0
  205. package/src/headers/24/preferredaddress.h +72 -0
  206. package/src/headers/24/protocol_helper.h +27 -0
  207. package/src/headers/24/queue.h +306 -0
  208. package/src/headers/24/req_wrap-inl.h +176 -0
  209. package/src/headers/24/req_wrap.h +79 -0
  210. package/src/headers/24/runtime_agent.h +41 -0
  211. package/src/headers/24/session.h +528 -0
  212. package/src/headers/24/sessionticket.h +108 -0
  213. package/src/headers/24/spawn_sync.h +243 -0
  214. package/src/headers/24/stream_base-inl.h +173 -0
  215. package/src/headers/24/stream_base.h +478 -0
  216. package/src/headers/24/stream_pipe.h +76 -0
  217. package/src/headers/24/stream_wrap.h +132 -0
  218. package/src/headers/24/streams.h +370 -0
  219. package/src/headers/24/string_bytes.h +109 -0
  220. package/src/headers/24/string_decoder-inl.h +31 -0
  221. package/src/headers/24/string_decoder.h +49 -0
  222. package/src/headers/24/target_agent.h +75 -0
  223. package/src/headers/24/tcp_wrap.h +105 -0
  224. package/src/headers/24/threadpoolwork-inl.h +70 -0
  225. package/src/headers/24/timer_wrap-inl.h +32 -0
  226. package/src/headers/24/timer_wrap.h +87 -0
  227. package/src/headers/24/timers.h +75 -0
  228. package/src/headers/24/tlscontext.h +228 -0
  229. package/src/headers/24/tokens.h +256 -0
  230. package/src/headers/24/trace_event.h +722 -0
  231. package/src/headers/24/trace_event_common.h +1109 -0
  232. package/src/headers/24/traced_value.h +129 -0
  233. package/src/headers/24/tracing/agent.h +195 -0
  234. package/src/headers/24/tracing/trace_event.h +722 -0
  235. package/src/headers/24/tracing_agent.h +43 -0
  236. package/src/headers/24/transportparams.h +166 -0
  237. package/src/headers/24/tty_wrap.h +65 -0
  238. package/src/headers/24/udp_wrap.h +230 -0
  239. package/src/headers/24/undici_version.h +6 -0
  240. package/src/headers/24/util-inl.h +711 -0
  241. package/src/headers/24/util.h +1016 -0
  242. package/src/headers/24/v8-external-memory-accounter.h +60 -0
  243. package/src/headers/24/v8-fast-api-calls.h +819 -0
  244. package/src/headers/24/v8-inspector.h +426 -0
  245. package/src/headers/24/wasi_permission.h +31 -0
  246. package/src/headers/24/worker_agent.h +40 -0
  247. package/src/headers/24/worker_inspector.h +124 -0
  248. package/src/headers/24/worker_permission.h +31 -0
  249. package/src/headers/24/zlib_version.h +6 -0
@@ -0,0 +1,819 @@
1
+ // Copyright 2020 the V8 project authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style license that can be
3
+ // found in the LICENSE file.
4
+
5
+ #ifndef INCLUDE_V8_FAST_API_CALLS_H_
6
+ #define INCLUDE_V8_FAST_API_CALLS_H_
7
+
8
+ /**
9
+ * This file provides additional API on top of the default one for making
10
+ * API calls, which come from embedder C++ functions. The functions are being
11
+ * called directly from optimized code, doing all the necessary typechecks
12
+ * in the compiler itself, instead of on the embedder side. Hence the "fast"
13
+ * in the name. Example usage might look like:
14
+ *
15
+ * \code
16
+ * void FastMethod(int param, bool another_param);
17
+ *
18
+ * v8::FunctionTemplate::New(isolate, SlowCallback, data,
19
+ * signature, length, constructor_behavior
20
+ * side_effect_type,
21
+ * &v8::CFunction::Make(FastMethod));
22
+ * \endcode
23
+ *
24
+ * By design, fast calls are limited by the following requirements, which
25
+ * the embedder should enforce themselves:
26
+ * - they should not allocate on the JS heap;
27
+ * - they should not trigger JS execution.
28
+ * To enforce them, the embedder could use the existing
29
+ * v8::Isolate::DisallowJavascriptExecutionScope and a utility similar to
30
+ * Blink's NoAllocationScope:
31
+ * https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/renderer/platform/heap/thread_state_scopes.h;l=16
32
+ *
33
+ * Due to these limitations, it's not directly possible to report errors by
34
+ * throwing a JS exception or to otherwise do an allocation. There is an
35
+ * alternative way of creating fast calls that supports falling back to the
36
+ * slow call and then performing the necessary allocation. When one creates
37
+ * the fast method by using CFunction::MakeWithFallbackSupport instead of
38
+ * CFunction::Make, the fast callback gets as last parameter an output variable,
39
+ * through which it can request falling back to the slow call. So one might
40
+ * declare their method like:
41
+ *
42
+ * \code
43
+ * void FastMethodWithFallback(int param, FastApiCallbackOptions& options);
44
+ * \endcode
45
+ *
46
+ * If the callback wants to signal an error condition or to perform an
47
+ * allocation, it must set options.fallback to true and do an early return from
48
+ * the fast method. Then V8 checks the value of options.fallback and if it's
49
+ * true, falls back to executing the SlowCallback, which is capable of reporting
50
+ * the error (either by throwing a JS exception or logging to the console) or
51
+ * doing the allocation. It's the embedder's responsibility to ensure that the
52
+ * fast callback is idempotent up to the point where error and fallback
53
+ * conditions are checked, because otherwise executing the slow callback might
54
+ * produce visible side-effects twice.
55
+ *
56
+ * An example for custom embedder type support might employ a way to wrap/
57
+ * unwrap various C++ types in JSObject instances, e.g:
58
+ *
59
+ * \code
60
+ *
61
+ * // Helper method with a check for field count.
62
+ * template <typename T, int offset>
63
+ * inline T* GetInternalField(v8::Local<v8::Object> wrapper) {
64
+ * assert(offset < wrapper->InternalFieldCount());
65
+ * return reinterpret_cast<T*>(
66
+ * wrapper->GetAlignedPointerFromInternalField(offset));
67
+ * }
68
+ *
69
+ * class CustomEmbedderType {
70
+ * public:
71
+ * // Returns the raw C object from a wrapper JS object.
72
+ * static CustomEmbedderType* Unwrap(v8::Local<v8::Object> wrapper) {
73
+ * return GetInternalField<CustomEmbedderType,
74
+ * kV8EmbedderWrapperObjectIndex>(wrapper);
75
+ * }
76
+ * static void FastMethod(v8::Local<v8::Object> receiver_obj, int param) {
77
+ * CustomEmbedderType* receiver = static_cast<CustomEmbedderType*>(
78
+ * receiver_obj->GetAlignedPointerFromInternalField(
79
+ * kV8EmbedderWrapperObjectIndex));
80
+ *
81
+ * // Type checks are already done by the optimized code.
82
+ * // Then call some performance-critical method like:
83
+ * // receiver->Method(param);
84
+ * }
85
+ *
86
+ * static void SlowMethod(
87
+ * const v8::FunctionCallbackInfo<v8::Value>& info) {
88
+ * v8::Local<v8::Object> instance =
89
+ * v8::Local<v8::Object>::Cast(info.Holder());
90
+ * CustomEmbedderType* receiver = Unwrap(instance);
91
+ * // TODO: Do type checks and extract {param}.
92
+ * receiver->Method(param);
93
+ * }
94
+ * };
95
+ *
96
+ * // TODO(mslekova): Clean-up these constants
97
+ * // The constants kV8EmbedderWrapperTypeIndex and
98
+ * // kV8EmbedderWrapperObjectIndex describe the offsets for the type info
99
+ * // struct and the native object, when expressed as internal field indices
100
+ * // within a JSObject. The existance of this helper function assumes that
101
+ * // all embedder objects have their JSObject-side type info at the same
102
+ * // offset, but this is not a limitation of the API itself. For a detailed
103
+ * // use case, see the third example.
104
+ * static constexpr int kV8EmbedderWrapperTypeIndex = 0;
105
+ * static constexpr int kV8EmbedderWrapperObjectIndex = 1;
106
+ *
107
+ * // The following setup function can be templatized based on
108
+ * // the {embedder_object} argument.
109
+ * void SetupCustomEmbedderObject(v8::Isolate* isolate,
110
+ * v8::Local<v8::Context> context,
111
+ * CustomEmbedderType* embedder_object) {
112
+ * isolate->set_embedder_wrapper_type_index(
113
+ * kV8EmbedderWrapperTypeIndex);
114
+ * isolate->set_embedder_wrapper_object_index(
115
+ * kV8EmbedderWrapperObjectIndex);
116
+ *
117
+ * v8::CFunction c_func =
118
+ * MakeV8CFunction(CustomEmbedderType::FastMethod);
119
+ *
120
+ * Local<v8::FunctionTemplate> method_template =
121
+ * v8::FunctionTemplate::New(
122
+ * isolate, CustomEmbedderType::SlowMethod, v8::Local<v8::Value>(),
123
+ * v8::Local<v8::Signature>(), 1, v8::ConstructorBehavior::kAllow,
124
+ * v8::SideEffectType::kHasSideEffect, &c_func);
125
+ *
126
+ * v8::Local<v8::ObjectTemplate> object_template =
127
+ * v8::ObjectTemplate::New(isolate);
128
+ * object_template->SetInternalFieldCount(
129
+ * kV8EmbedderWrapperObjectIndex + 1);
130
+ * object_template->Set(isolate, "method", method_template);
131
+ *
132
+ * // Instantiate the wrapper JS object.
133
+ * v8::Local<v8::Object> object =
134
+ * object_template->NewInstance(context).ToLocalChecked();
135
+ * object->SetAlignedPointerInInternalField(
136
+ * kV8EmbedderWrapperObjectIndex,
137
+ * reinterpret_cast<void*>(embedder_object));
138
+ *
139
+ * // TODO: Expose {object} where it's necessary.
140
+ * }
141
+ * \endcode
142
+ *
143
+ * For instance if {object} is exposed via a global "obj" variable,
144
+ * one could write in JS:
145
+ * function hot_func() {
146
+ * obj.method(42);
147
+ * }
148
+ * and once {hot_func} gets optimized, CustomEmbedderType::FastMethod
149
+ * will be called instead of the slow version, with the following arguments:
150
+ * receiver := the {embedder_object} from above
151
+ * param := 42
152
+ *
153
+ * Currently supported return types:
154
+ * - void
155
+ * - bool
156
+ * - int32_t
157
+ * - uint32_t
158
+ * - float32_t
159
+ * - float64_t
160
+ * Currently supported argument types:
161
+ * - pointer to an embedder type
162
+ * - JavaScript array of primitive types
163
+ * - bool
164
+ * - int32_t
165
+ * - uint32_t
166
+ * - int64_t
167
+ * - uint64_t
168
+ * - float32_t
169
+ * - float64_t
170
+ *
171
+ * The 64-bit integer types currently have the IDL (unsigned) long long
172
+ * semantics: https://heycam.github.io/webidl/#abstract-opdef-converttoint
173
+ * In the future we'll extend the API to also provide conversions from/to
174
+ * BigInt to preserve full precision.
175
+ * The floating point types currently have the IDL (unrestricted) semantics,
176
+ * which is the only one used by WebGL. We plan to add support also for
177
+ * restricted floats/doubles, similarly to the BigInt conversion policies.
178
+ * We also differ from the specific NaN bit pattern that WebIDL prescribes
179
+ * (https://heycam.github.io/webidl/#es-unrestricted-float) in that Blink
180
+ * passes NaN values as-is, i.e. doesn't normalize them.
181
+ *
182
+ * To be supported types:
183
+ * - TypedArrays and ArrayBuffers
184
+ * - arrays of embedder types
185
+ *
186
+ *
187
+ * The API offers a limited support for function overloads:
188
+ *
189
+ * \code
190
+ * void FastMethod_2Args(int param, bool another_param);
191
+ * void FastMethod_3Args(int param, bool another_param, int third_param);
192
+ *
193
+ * v8::CFunction fast_method_2args_c_func =
194
+ * MakeV8CFunction(FastMethod_2Args);
195
+ * v8::CFunction fast_method_3args_c_func =
196
+ * MakeV8CFunction(FastMethod_3Args);
197
+ * const v8::CFunction fast_method_overloads[] = {fast_method_2args_c_func,
198
+ * fast_method_3args_c_func};
199
+ * Local<v8::FunctionTemplate> method_template =
200
+ * v8::FunctionTemplate::NewWithCFunctionOverloads(
201
+ * isolate, SlowCallback, data, signature, length,
202
+ * constructor_behavior, side_effect_type,
203
+ * {fast_method_overloads, 2});
204
+ * \endcode
205
+ *
206
+ * In this example a single FunctionTemplate is associated to multiple C++
207
+ * functions. The overload resolution is currently only based on the number of
208
+ * arguments passed in a call. For example, if this method_template is
209
+ * registered with a wrapper JS object as described above, a call with two
210
+ * arguments:
211
+ * obj.method(42, true);
212
+ * will result in a fast call to FastMethod_2Args, while a call with three or
213
+ * more arguments:
214
+ * obj.method(42, true, 11);
215
+ * will result in a fast call to FastMethod_3Args. Instead a call with less than
216
+ * two arguments, like:
217
+ * obj.method(42);
218
+ * would not result in a fast call but would fall back to executing the
219
+ * associated SlowCallback.
220
+ */
221
+
222
+ #include <stddef.h>
223
+ #include <stdint.h>
224
+
225
+ #include <tuple>
226
+ #include <type_traits>
227
+
228
+ #include "v8-internal.h" // NOLINT(build/include_directory)
229
+ #include "v8-local-handle.h" // NOLINT(build/include_directory)
230
+ #include "v8-typed-array.h" // NOLINT(build/include_directory)
231
+ #include "v8-value.h" // NOLINT(build/include_directory)
232
+ #include "v8config.h" // NOLINT(build/include_directory)
233
+
234
+ namespace v8 {
235
+
236
+ class Isolate;
237
+
238
+ START_ALLOW_USE_DEPRECATED()
239
+ class CTypeInfo {
240
+ public:
241
+ enum class Type : uint8_t {
242
+ kVoid,
243
+ kBool,
244
+ kUint8,
245
+ kInt32,
246
+ kUint32,
247
+ kInt64,
248
+ kUint64,
249
+ kFloat32,
250
+ kFloat64,
251
+ kPointer,
252
+ kV8Value,
253
+ kSeqOneByteString,
254
+ kApiObject, // This will be deprecated once all users have
255
+ // migrated from v8::ApiObject to v8::Local<v8::Value>.
256
+ kAny, // This is added to enable untyped representation of fast
257
+ // call arguments for test purposes. It can represent any of
258
+ // the other types stored in the same memory as a union
259
+ // (see AnyCType declared below). This allows for
260
+ // uniform passing of arguments w.r.t. their location
261
+ // (in a register or on the stack), independent of their
262
+ // actual type. It's currently used by the arm64 simulator
263
+ // and can be added to the other simulators as well when fast
264
+ // calls having both GP and FP params need to be supported.
265
+ };
266
+
267
+ // kCallbackOptionsType is not part of the Type enum
268
+ // because it is only used internally. Use value 255 that is larger
269
+ // than any valid Type enum.
270
+ static constexpr Type kCallbackOptionsType = Type(255);
271
+
272
+ enum class V8_DEPRECATE_SOON(
273
+ "There is no special support in V8 anymore, there is no need to"
274
+ "use a SequenceType") SequenceType : uint8_t {
275
+ kScalar,
276
+ kIsSequence, // sequence<T>
277
+ kIsArrayBuffer // ArrayBuffer
278
+ };
279
+
280
+ enum class Flags : uint8_t {
281
+ kNone = 0,
282
+ kAllowSharedBit = 1 << 0, // Must be an ArrayBuffer or TypedArray
283
+ kEnforceRangeBit = 1 << 1, // T must be integral
284
+ kClampBit = 1 << 2, // T must be integral
285
+ kIsRestrictedBit = 1 << 3, // T must be float or double
286
+ };
287
+
288
+ explicit constexpr CTypeInfo(Type type, Flags flags = Flags::kNone)
289
+ : type_(type), sequence_type_(SequenceType::kScalar), flags_(flags) {}
290
+
291
+ V8_DEPRECATE_SOON("Use CTypeInfo(Type, Flags) instead")
292
+ constexpr CTypeInfo(Type type, SequenceType sequence_type,
293
+ Flags flags = Flags::kNone)
294
+ : type_(type), sequence_type_(sequence_type), flags_(flags) {}
295
+
296
+ typedef uint32_t Identifier;
297
+ explicit constexpr CTypeInfo(Identifier identifier)
298
+ : CTypeInfo(static_cast<Type>(identifier >> 16),
299
+ static_cast<SequenceType>((identifier >> 8) & 255),
300
+ static_cast<Flags>(identifier & 255)) {}
301
+ constexpr Identifier GetId() const {
302
+ return static_cast<uint8_t>(type_) << 16 |
303
+ static_cast<uint8_t>(sequence_type_) << 8 |
304
+ static_cast<uint8_t>(flags_);
305
+ }
306
+
307
+ constexpr Type GetType() const { return type_; }
308
+ V8_DEPRECATE_SOON("Use the constant SequenceType::kScalar instead")
309
+ constexpr SequenceType GetSequenceType() const { return sequence_type_; }
310
+ constexpr Flags GetFlags() const { return flags_; }
311
+
312
+ static constexpr bool IsIntegralType(Type type) {
313
+ return type == Type::kUint8 || type == Type::kInt32 ||
314
+ type == Type::kUint32 || type == Type::kInt64 ||
315
+ type == Type::kUint64;
316
+ }
317
+
318
+ static constexpr bool IsFloatingPointType(Type type) {
319
+ return type == Type::kFloat32 || type == Type::kFloat64;
320
+ }
321
+
322
+ static constexpr bool IsPrimitive(Type type) {
323
+ return IsIntegralType(type) || IsFloatingPointType(type) ||
324
+ type == Type::kBool;
325
+ }
326
+
327
+ private:
328
+ Type type_;
329
+ SequenceType sequence_type_;
330
+ Flags flags_;
331
+ };
332
+ END_ALLOW_USE_DEPRECATED()
333
+
334
+ struct FastOneByteString {
335
+ const char* data;
336
+ uint32_t length;
337
+ };
338
+
339
+ class V8_EXPORT CFunctionInfo {
340
+ public:
341
+ enum class Int64Representation : uint8_t {
342
+ kNumber = 0, // Use numbers to represent 64 bit integers.
343
+ kBigInt = 1, // Use BigInts to represent 64 bit integers.
344
+ };
345
+
346
+ // Construct a struct to hold a CFunction's type information.
347
+ // |return_info| describes the function's return type.
348
+ // |arg_info| is an array of |arg_count| CTypeInfos describing the
349
+ // arguments. Only the last argument may be of the special type
350
+ // CTypeInfo::kCallbackOptionsType.
351
+ CFunctionInfo(const CTypeInfo& return_info, unsigned int arg_count,
352
+ const CTypeInfo* arg_info,
353
+ Int64Representation repr = Int64Representation::kNumber);
354
+
355
+ const CTypeInfo& ReturnInfo() const { return return_info_; }
356
+
357
+ // The argument count, not including the v8::FastApiCallbackOptions
358
+ // if present.
359
+ unsigned int ArgumentCount() const {
360
+ return HasOptions() ? arg_count_ - 1 : arg_count_;
361
+ }
362
+
363
+ Int64Representation GetInt64Representation() const { return repr_; }
364
+
365
+ // |index| must be less than ArgumentCount().
366
+ // Note: if the last argument passed on construction of CFunctionInfo
367
+ // has type CTypeInfo::kCallbackOptionsType, it is not included in
368
+ // ArgumentCount().
369
+ const CTypeInfo& ArgumentInfo(unsigned int index) const;
370
+
371
+ bool HasOptions() const {
372
+ // The options arg is always the last one.
373
+ return arg_count_ > 0 && arg_info_[arg_count_ - 1].GetType() ==
374
+ CTypeInfo::kCallbackOptionsType;
375
+ }
376
+
377
+ private:
378
+ const CTypeInfo return_info_;
379
+ const Int64Representation repr_;
380
+ const unsigned int arg_count_;
381
+ const CTypeInfo* arg_info_;
382
+ };
383
+
384
+ struct FastApiCallbackOptions;
385
+
386
+ // Provided for testing.
387
+ union V8_TRIVIAL_ABI AnyCType {
388
+ AnyCType() : int64_value(0) {}
389
+
390
+ #if defined(V8_ENABLE_LOCAL_OFF_STACK_CHECK) && V8_HAS_ATTRIBUTE_TRIVIAL_ABI
391
+ // In this case, Local<T> is not trivially copyable and the implicit
392
+ // copy constructor and copy assignment for the union are deleted.
393
+ AnyCType(const AnyCType& other) : int64_value(other.int64_value) {}
394
+ AnyCType& operator=(const AnyCType& other) {
395
+ int64_value = other.int64_value;
396
+ return *this;
397
+ }
398
+ #endif
399
+
400
+ bool bool_value;
401
+ int32_t int32_value;
402
+ uint32_t uint32_value;
403
+ int64_t int64_value;
404
+ uint64_t uint64_value;
405
+ float float_value;
406
+ double double_value;
407
+ void* pointer_value;
408
+ Local<Object> object_value;
409
+ Local<Array> sequence_value;
410
+ const FastOneByteString* string_value;
411
+ FastApiCallbackOptions* options_value;
412
+ };
413
+
414
+ static_assert(
415
+ sizeof(AnyCType) == 8,
416
+ "The union AnyCType should have size == 64 bits, as this is assumed "
417
+ "by EffectControlLinearizer.");
418
+
419
+ class V8_EXPORT CFunction {
420
+ public:
421
+ constexpr CFunction() : address_(nullptr), type_info_(nullptr) {}
422
+
423
+ const CTypeInfo& ReturnInfo() const { return type_info_->ReturnInfo(); }
424
+
425
+ const CTypeInfo& ArgumentInfo(unsigned int index) const {
426
+ return type_info_->ArgumentInfo(index);
427
+ }
428
+
429
+ unsigned int ArgumentCount() const { return type_info_->ArgumentCount(); }
430
+
431
+ const void* GetAddress() const { return address_; }
432
+ CFunctionInfo::Int64Representation GetInt64Representation() const {
433
+ return type_info_->GetInt64Representation();
434
+ }
435
+ const CFunctionInfo* GetTypeInfo() const { return type_info_; }
436
+
437
+ enum class OverloadResolution { kImpossible, kAtRuntime, kAtCompileTime };
438
+
439
+ template <typename F>
440
+ static CFunction Make(F* func,
441
+ CFunctionInfo::Int64Representation int64_rep =
442
+ CFunctionInfo::Int64Representation::kNumber) {
443
+ CFunction result = ArgUnwrap<F*>::Make(func, int64_rep);
444
+ result.GetInt64Representation();
445
+ return result;
446
+ }
447
+
448
+ // Provided for testing purposes.
449
+ template <typename R, typename... Args, typename R_Patch,
450
+ typename... Args_Patch>
451
+ static CFunction Make(R (*func)(Args...),
452
+ R_Patch (*patching_func)(Args_Patch...),
453
+ CFunctionInfo::Int64Representation int64_rep =
454
+ CFunctionInfo::Int64Representation::kNumber) {
455
+ CFunction c_func = ArgUnwrap<R (*)(Args...)>::Make(func, int64_rep);
456
+ static_assert(
457
+ sizeof...(Args_Patch) == sizeof...(Args),
458
+ "The patching function must have the same number of arguments.");
459
+ c_func.address_ = reinterpret_cast<void*>(patching_func);
460
+ return c_func;
461
+ }
462
+
463
+ CFunction(const void* address, const CFunctionInfo* type_info);
464
+
465
+ private:
466
+ const void* address_;
467
+ const CFunctionInfo* type_info_;
468
+
469
+ template <typename F>
470
+ class ArgUnwrap {
471
+ static_assert(sizeof(F) != sizeof(F),
472
+ "CFunction must be created from a function pointer.");
473
+ };
474
+
475
+ template <typename R, typename... Args>
476
+ class ArgUnwrap<R (*)(Args...)> {
477
+ public:
478
+ static CFunction Make(R (*func)(Args...),
479
+ CFunctionInfo::Int64Representation int64_rep =
480
+ CFunctionInfo::Int64Representation::kNumber);
481
+ };
482
+ };
483
+
484
+ /**
485
+ * A struct which may be passed to a fast call callback, like so:
486
+ * \code
487
+ * void FastMethodWithOptions(int param, FastApiCallbackOptions& options);
488
+ * \endcode
489
+ */
490
+ struct FastApiCallbackOptions {
491
+ /**
492
+ * Creates a new instance of FastApiCallbackOptions for testing purpose. The
493
+ * returned instance may be filled with mock data.
494
+ */
495
+ static FastApiCallbackOptions CreateForTesting(Isolate* isolate) {
496
+ return {};
497
+ }
498
+
499
+ v8::Isolate* isolate = nullptr;
500
+
501
+ /**
502
+ * The `data` passed to the FunctionTemplate constructor, or `undefined`.
503
+ */
504
+ v8::Local<v8::Value> data;
505
+ };
506
+
507
+ namespace internal {
508
+
509
+ // Helper to count the number of occurances of `T` in `List`
510
+ template <typename T, typename... List>
511
+ struct count : std::integral_constant<int, 0> {};
512
+ template <typename T, typename... Args>
513
+ struct count<T, T, Args...>
514
+ : std::integral_constant<std::size_t, 1 + count<T, Args...>::value> {};
515
+ template <typename T, typename U, typename... Args>
516
+ struct count<T, U, Args...> : count<T, Args...> {};
517
+
518
+ template <CFunctionInfo::Int64Representation Representation,
519
+ typename RetBuilder, typename... ArgBuilders>
520
+ class CFunctionInfoImpl : public CFunctionInfo {
521
+ static constexpr int kOptionsArgCount =
522
+ count<FastApiCallbackOptions&, ArgBuilders...>();
523
+ static constexpr int kReceiverCount = 1;
524
+
525
+ static_assert(kOptionsArgCount == 0 || kOptionsArgCount == 1,
526
+ "Only one options parameter is supported.");
527
+
528
+ static_assert(sizeof...(ArgBuilders) >= kOptionsArgCount + kReceiverCount,
529
+ "The receiver or the options argument is missing.");
530
+
531
+ public:
532
+ constexpr CFunctionInfoImpl()
533
+ : CFunctionInfo(RetBuilder::Build(), sizeof...(ArgBuilders),
534
+ arg_info_storage_, Representation),
535
+ arg_info_storage_{ArgBuilders::Build()...} {
536
+ constexpr CTypeInfo::Type kReturnType = RetBuilder::Build().GetType();
537
+ static_assert(kReturnType == CTypeInfo::Type::kVoid ||
538
+ kReturnType == CTypeInfo::Type::kBool ||
539
+ kReturnType == CTypeInfo::Type::kInt32 ||
540
+ kReturnType == CTypeInfo::Type::kUint32 ||
541
+ kReturnType == CTypeInfo::Type::kInt64 ||
542
+ kReturnType == CTypeInfo::Type::kUint64 ||
543
+ kReturnType == CTypeInfo::Type::kFloat32 ||
544
+ kReturnType == CTypeInfo::Type::kFloat64 ||
545
+ kReturnType == CTypeInfo::Type::kPointer ||
546
+ kReturnType == CTypeInfo::Type::kAny,
547
+ "String and api object values are not currently "
548
+ "supported return types.");
549
+ }
550
+
551
+ private:
552
+ const CTypeInfo arg_info_storage_[sizeof...(ArgBuilders)];
553
+ };
554
+
555
+ template <typename T>
556
+ struct TypeInfoHelper {
557
+ static_assert(sizeof(T) != sizeof(T), "This type is not supported");
558
+ };
559
+
560
+ #define SPECIALIZE_GET_TYPE_INFO_HELPER_FOR(T, Enum) \
561
+ template <> \
562
+ struct TypeInfoHelper<T> { \
563
+ static constexpr CTypeInfo::Flags Flags() { \
564
+ return CTypeInfo::Flags::kNone; \
565
+ } \
566
+ \
567
+ static constexpr CTypeInfo::Type Type() { return CTypeInfo::Type::Enum; } \
568
+ };
569
+
570
+ template <CTypeInfo::Type type>
571
+ struct CTypeInfoTraits {};
572
+
573
+ #define DEFINE_TYPE_INFO_TRAITS(CType, Enum) \
574
+ template <> \
575
+ struct CTypeInfoTraits<CTypeInfo::Type::Enum> { \
576
+ using ctype = CType; \
577
+ };
578
+
579
+ #define PRIMITIVE_C_TYPES(V) \
580
+ V(bool, kBool) \
581
+ V(uint8_t, kUint8) \
582
+ V(int32_t, kInt32) \
583
+ V(uint32_t, kUint32) \
584
+ V(int64_t, kInt64) \
585
+ V(uint64_t, kUint64) \
586
+ V(float, kFloat32) \
587
+ V(double, kFloat64) \
588
+ V(void*, kPointer)
589
+
590
+ // Same as above, but includes deprecated types for compatibility.
591
+ #define ALL_C_TYPES(V) \
592
+ PRIMITIVE_C_TYPES(V) \
593
+ V(void, kVoid) \
594
+ V(v8::Local<v8::Value>, kV8Value) \
595
+ V(v8::Local<v8::Object>, kV8Value) \
596
+ V(v8::Local<v8::Array>, kV8Value) \
597
+ V(AnyCType, kAny)
598
+
599
+ // ApiObject was a temporary solution to wrap the pointer to the v8::Value.
600
+ // Please use v8::Local<v8::Value> in new code for the arguments and
601
+ // v8::Local<v8::Object> for the receiver, as ApiObject will be deprecated.
602
+
603
+ ALL_C_TYPES(SPECIALIZE_GET_TYPE_INFO_HELPER_FOR)
604
+ PRIMITIVE_C_TYPES(DEFINE_TYPE_INFO_TRAITS)
605
+
606
+ #undef PRIMITIVE_C_TYPES
607
+ #undef ALL_C_TYPES
608
+
609
+ #undef TYPED_ARRAY_C_TYPES
610
+
611
+ template <>
612
+ struct TypeInfoHelper<FastApiCallbackOptions&> {
613
+ static constexpr CTypeInfo::Flags Flags() { return CTypeInfo::Flags::kNone; }
614
+
615
+ static constexpr CTypeInfo::Type Type() {
616
+ return CTypeInfo::kCallbackOptionsType;
617
+ }
618
+ };
619
+
620
+ template <>
621
+ struct TypeInfoHelper<const FastOneByteString&> {
622
+ static constexpr CTypeInfo::Flags Flags() { return CTypeInfo::Flags::kNone; }
623
+
624
+ static constexpr CTypeInfo::Type Type() {
625
+ return CTypeInfo::Type::kSeqOneByteString;
626
+ }
627
+ };
628
+
629
+ #define STATIC_ASSERT_IMPLIES(COND, ASSERTION, MSG) \
630
+ static_assert(((COND) == 0) || (ASSERTION), MSG)
631
+
632
+ } // namespace internal
633
+
634
+ template <typename T, CTypeInfo::Flags... Flags>
635
+ class V8_EXPORT CTypeInfoBuilder {
636
+ public:
637
+ using BaseType = T;
638
+
639
+ static constexpr CTypeInfo Build() {
640
+ constexpr CTypeInfo::Flags kFlags =
641
+ MergeFlags(internal::TypeInfoHelper<T>::Flags(), Flags...);
642
+ constexpr CTypeInfo::Type kType = internal::TypeInfoHelper<T>::Type();
643
+
644
+ STATIC_ASSERT_IMPLIES(
645
+ uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kEnforceRangeBit),
646
+ CTypeInfo::IsIntegralType(kType),
647
+ "kEnforceRangeBit is only allowed for integral types.");
648
+ STATIC_ASSERT_IMPLIES(
649
+ uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kClampBit),
650
+ CTypeInfo::IsIntegralType(kType),
651
+ "kClampBit is only allowed for integral types.");
652
+ STATIC_ASSERT_IMPLIES(
653
+ uint8_t(kFlags) & uint8_t(CTypeInfo::Flags::kIsRestrictedBit),
654
+ CTypeInfo::IsFloatingPointType(kType),
655
+ "kIsRestrictedBit is only allowed for floating point types.");
656
+
657
+ // Return the same type with the merged flags.
658
+ return CTypeInfo(internal::TypeInfoHelper<T>::Type(), kFlags);
659
+ }
660
+
661
+ private:
662
+ template <typename... Rest>
663
+ static constexpr CTypeInfo::Flags MergeFlags(CTypeInfo::Flags flags,
664
+ Rest... rest) {
665
+ return CTypeInfo::Flags(uint8_t(flags) | uint8_t(MergeFlags(rest...)));
666
+ }
667
+ static constexpr CTypeInfo::Flags MergeFlags() { return CTypeInfo::Flags(0); }
668
+ };
669
+
670
+ namespace internal {
671
+ template <typename RetBuilder, typename... ArgBuilders>
672
+ class CFunctionBuilderWithFunction {
673
+ public:
674
+ explicit constexpr CFunctionBuilderWithFunction(const void* fn) : fn_(fn) {}
675
+
676
+ template <CTypeInfo::Flags... Flags>
677
+ constexpr auto Ret() {
678
+ return CFunctionBuilderWithFunction<
679
+ CTypeInfoBuilder<typename RetBuilder::BaseType, Flags...>,
680
+ ArgBuilders...>(fn_);
681
+ }
682
+
683
+ template <unsigned int N, CTypeInfo::Flags... Flags>
684
+ constexpr auto Arg() {
685
+ // Return a copy of the builder with the Nth arg builder merged with
686
+ // template parameter pack Flags.
687
+ return ArgImpl<N, Flags...>(
688
+ std::make_index_sequence<sizeof...(ArgBuilders)>());
689
+ }
690
+
691
+ // Provided for testing purposes.
692
+ template <typename Ret, typename... Args>
693
+ auto Patch(Ret (*patching_func)(Args...)) {
694
+ static_assert(
695
+ sizeof...(Args) == sizeof...(ArgBuilders),
696
+ "The patching function must have the same number of arguments.");
697
+ fn_ = reinterpret_cast<void*>(patching_func);
698
+ return *this;
699
+ }
700
+
701
+ template <CFunctionInfo::Int64Representation Representation =
702
+ CFunctionInfo::Int64Representation::kNumber>
703
+ auto Build() {
704
+ static CFunctionInfoImpl<Representation, RetBuilder, ArgBuilders...>
705
+ instance;
706
+ return CFunction(fn_, &instance);
707
+ }
708
+
709
+ private:
710
+ template <bool Merge, unsigned int N, CTypeInfo::Flags... Flags>
711
+ struct GetArgBuilder;
712
+
713
+ // Returns the same ArgBuilder as the one at index N, including its flags.
714
+ // Flags in the template parameter pack are ignored.
715
+ template <unsigned int N, CTypeInfo::Flags... Flags>
716
+ struct GetArgBuilder<false, N, Flags...> {
717
+ using type =
718
+ typename std::tuple_element<N, std::tuple<ArgBuilders...>>::type;
719
+ };
720
+
721
+ // Returns an ArgBuilder with the same base type as the one at index N,
722
+ // but merges the flags with the flags in the template parameter pack.
723
+ template <unsigned int N, CTypeInfo::Flags... Flags>
724
+ struct GetArgBuilder<true, N, Flags...> {
725
+ using type = CTypeInfoBuilder<
726
+ typename std::tuple_element<N,
727
+ std::tuple<ArgBuilders...>>::type::BaseType,
728
+ std::tuple_element<N, std::tuple<ArgBuilders...>>::type::Build()
729
+ .GetFlags(),
730
+ Flags...>;
731
+ };
732
+
733
+ // Return a copy of the CFunctionBuilder, but merges the Flags on
734
+ // ArgBuilder index N with the new Flags passed in the template parameter
735
+ // pack.
736
+ template <unsigned int N, CTypeInfo::Flags... Flags, size_t... I>
737
+ constexpr auto ArgImpl(std::index_sequence<I...>) {
738
+ return CFunctionBuilderWithFunction<
739
+ RetBuilder, typename GetArgBuilder<N == I, I, Flags...>::type...>(fn_);
740
+ }
741
+
742
+ const void* fn_;
743
+ };
744
+
745
+ class CFunctionBuilder {
746
+ public:
747
+ constexpr CFunctionBuilder() {}
748
+
749
+ template <typename R, typename... Args>
750
+ constexpr auto Fn(R (*fn)(Args...)) {
751
+ return CFunctionBuilderWithFunction<CTypeInfoBuilder<R>,
752
+ CTypeInfoBuilder<Args>...>(
753
+ reinterpret_cast<const void*>(fn));
754
+ }
755
+ };
756
+
757
+ } // namespace internal
758
+
759
+ // static
760
+ template <typename R, typename... Args>
761
+ CFunction CFunction::ArgUnwrap<R (*)(Args...)>::Make(
762
+ R (*func)(Args...), CFunctionInfo::Int64Representation int64_rep) {
763
+ if (int64_rep == CFunctionInfo::Int64Representation::kNumber) {
764
+ return internal::CFunctionBuilder().Fn(func).Build();
765
+ }
766
+ return internal::CFunctionBuilder()
767
+ .Fn(func)
768
+ .template Build<CFunctionInfo::Int64Representation::kBigInt>();
769
+ }
770
+
771
+ using CFunctionBuilder = internal::CFunctionBuilder;
772
+
773
+ static constexpr CTypeInfo kTypeInfoInt32 = CTypeInfo(CTypeInfo::Type::kInt32);
774
+ static constexpr CTypeInfo kTypeInfoFloat64 =
775
+ CTypeInfo(CTypeInfo::Type::kFloat64);
776
+
777
+ /**
778
+ * Copies the contents of this JavaScript array to a C++ buffer with
779
+ * a given max_length. A CTypeInfo is passed as an argument,
780
+ * instructing different rules for conversion (e.g. restricted float/double).
781
+ * The element type T of the destination array must match the C type
782
+ * corresponding to the CTypeInfo (specified by CTypeInfoTraits).
783
+ * If the array length is larger than max_length or the array is of
784
+ * unsupported type, the operation will fail, returning false. Generally, an
785
+ * array which contains objects, undefined, null or anything not convertible
786
+ * to the requested destination type, is considered unsupported. The operation
787
+ * returns true on success. `type_info` will be used for conversions.
788
+ */
789
+ template <CTypeInfo::Identifier type_info_id, typename T>
790
+ bool V8_EXPORT V8_WARN_UNUSED_RESULT TryToCopyAndConvertArrayToCppBuffer(
791
+ Local<Array> src, T* dst, uint32_t max_length);
792
+
793
+ template <>
794
+ bool V8_EXPORT V8_WARN_UNUSED_RESULT
795
+ TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<int32_t>::Build().GetId(),
796
+ int32_t>(Local<Array> src, int32_t* dst,
797
+ uint32_t max_length);
798
+
799
+ template <>
800
+ bool V8_EXPORT V8_WARN_UNUSED_RESULT
801
+ TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<uint32_t>::Build().GetId(),
802
+ uint32_t>(Local<Array> src, uint32_t* dst,
803
+ uint32_t max_length);
804
+
805
+ template <>
806
+ bool V8_EXPORT V8_WARN_UNUSED_RESULT
807
+ TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<float>::Build().GetId(),
808
+ float>(Local<Array> src, float* dst,
809
+ uint32_t max_length);
810
+
811
+ template <>
812
+ bool V8_EXPORT V8_WARN_UNUSED_RESULT
813
+ TryToCopyAndConvertArrayToCppBuffer<CTypeInfoBuilder<double>::Build().GetId(),
814
+ double>(Local<Array> src, double* dst,
815
+ uint32_t max_length);
816
+
817
+ } // namespace v8
818
+
819
+ #endif // INCLUDE_V8_FAST_API_CALLS_H_