@shopify/react-native-skia 2.4.15 → 2.4.17

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 (226) hide show
  1. package/android/CMakeLists.txt +69 -0
  2. package/android/src/main/java/com/shopify/reactnative/skia/SkiaBaseView.java +19 -7
  3. package/android/src/main/java/com/shopify/reactnative/skia/SkiaBaseViewManager.java +7 -0
  4. package/apple/RNSkApplePlatformContext.mm +4 -0
  5. package/cpp/api/JsiSkApi.h +31 -0
  6. package/cpp/api/JsiSkImageFactory.h +69 -1
  7. package/cpp/api/JsiSkImageFilter.h +1 -0
  8. package/cpp/api/JsiSkPath.h +1 -1
  9. package/cpp/jsi/RuntimeAwareCache.h +0 -2
  10. package/cpp/jsi2/EnumMapper.h +45 -0
  11. package/cpp/jsi2/JSIConverter.h +468 -0
  12. package/cpp/jsi2/NativeObject.h +598 -0
  13. package/cpp/jsi2/Promise.cpp +44 -0
  14. package/cpp/jsi2/Promise.h +35 -0
  15. package/cpp/rnskia/RNDawnContext.h +114 -0
  16. package/cpp/rnskia/RNSkManager.cpp +41 -1
  17. package/cpp/rnwgpu/ArrayBuffer.h +68 -0
  18. package/cpp/rnwgpu/api/Convertors.h +761 -0
  19. package/cpp/rnwgpu/api/External.h +12 -0
  20. package/cpp/rnwgpu/api/GPU.cpp +129 -0
  21. package/cpp/rnwgpu/api/GPU.h +57 -0
  22. package/cpp/rnwgpu/api/GPUAdapter.cpp +178 -0
  23. package/cpp/rnwgpu/api/GPUAdapter.h +59 -0
  24. package/cpp/rnwgpu/api/GPUAdapterInfo.h +57 -0
  25. package/cpp/rnwgpu/api/GPUBindGroup.cpp +3 -0
  26. package/cpp/rnwgpu/api/GPUBindGroup.h +51 -0
  27. package/cpp/rnwgpu/api/GPUBindGroupLayout.h +52 -0
  28. package/cpp/rnwgpu/api/GPUBuffer.cpp +97 -0
  29. package/cpp/rnwgpu/api/GPUBuffer.h +87 -0
  30. package/cpp/rnwgpu/api/GPUCommandBuffer.h +45 -0
  31. package/cpp/rnwgpu/api/GPUCommandEncoder.cpp +179 -0
  32. package/cpp/rnwgpu/api/GPUCommandEncoder.h +111 -0
  33. package/cpp/rnwgpu/api/GPUCompilationInfo.h +79 -0
  34. package/cpp/rnwgpu/api/GPUCompilationMessage.h +36 -0
  35. package/cpp/rnwgpu/api/GPUComputePassEncoder.cpp +60 -0
  36. package/cpp/rnwgpu/api/GPUComputePassEncoder.h +85 -0
  37. package/cpp/rnwgpu/api/GPUComputePipeline.cpp +12 -0
  38. package/cpp/rnwgpu/api/GPUComputePipeline.h +60 -0
  39. package/cpp/rnwgpu/api/GPUDevice.cpp +434 -0
  40. package/cpp/rnwgpu/api/GPUDevice.h +174 -0
  41. package/cpp/rnwgpu/api/GPUDeviceLostInfo.cpp +7 -0
  42. package/cpp/rnwgpu/api/GPUDeviceLostInfo.h +40 -0
  43. package/cpp/rnwgpu/api/GPUError.h +35 -0
  44. package/cpp/rnwgpu/api/GPUExtent3D.h +66 -0
  45. package/cpp/rnwgpu/api/GPUExternalTexture.h +45 -0
  46. package/cpp/rnwgpu/api/GPUFeatures.h +212 -0
  47. package/cpp/rnwgpu/api/GPUInternalError.h +31 -0
  48. package/cpp/rnwgpu/api/GPUOrigin2D.h +55 -0
  49. package/cpp/rnwgpu/api/GPUOrigin3D.h +62 -0
  50. package/cpp/rnwgpu/api/GPUOutOfMemoryError.h +33 -0
  51. package/cpp/rnwgpu/api/GPUPipelineLayout.h +45 -0
  52. package/cpp/rnwgpu/api/GPUQuerySet.cpp +11 -0
  53. package/cpp/rnwgpu/api/GPUQuerySet.h +73 -0
  54. package/cpp/rnwgpu/api/GPUQueue.cpp +169 -0
  55. package/cpp/rnwgpu/api/GPUQueue.h +84 -0
  56. package/cpp/rnwgpu/api/GPURenderBundle.h +44 -0
  57. package/cpp/rnwgpu/api/GPURenderBundleEncoder.cpp +131 -0
  58. package/cpp/rnwgpu/api/GPURenderBundleEncoder.h +110 -0
  59. package/cpp/rnwgpu/api/GPURenderPassEncoder.cpp +165 -0
  60. package/cpp/rnwgpu/api/GPURenderPassEncoder.h +130 -0
  61. package/cpp/rnwgpu/api/GPURenderPipeline.cpp +13 -0
  62. package/cpp/rnwgpu/api/GPURenderPipeline.h +61 -0
  63. package/cpp/rnwgpu/api/GPUSampler.h +44 -0
  64. package/cpp/rnwgpu/api/GPUShaderModule.cpp +51 -0
  65. package/cpp/rnwgpu/api/GPUShaderModule.h +66 -0
  66. package/cpp/rnwgpu/api/GPUSupportedLimits.cpp +129 -0
  67. package/cpp/rnwgpu/api/GPUSupportedLimits.h +131 -0
  68. package/cpp/rnwgpu/api/GPUTexture.cpp +47 -0
  69. package/cpp/rnwgpu/api/GPUTexture.h +150 -0
  70. package/cpp/rnwgpu/api/GPUTextureView.h +44 -0
  71. package/cpp/rnwgpu/api/GPUValidationError.h +32 -0
  72. package/cpp/rnwgpu/api/descriptors/GPUBindGroupDescriptor.h +64 -0
  73. package/cpp/rnwgpu/api/descriptors/GPUBindGroupEntry.h +71 -0
  74. package/cpp/rnwgpu/api/descriptors/GPUBindGroupLayoutDescriptor.h +56 -0
  75. package/cpp/rnwgpu/api/descriptors/GPUBindGroupLayoutEntry.h +97 -0
  76. package/cpp/rnwgpu/api/descriptors/GPUBlendComponent.h +57 -0
  77. package/cpp/rnwgpu/api/descriptors/GPUBlendState.h +52 -0
  78. package/cpp/rnwgpu/api/descriptors/GPUBufferBinding.h +56 -0
  79. package/cpp/rnwgpu/api/descriptors/GPUBufferBindingLayout.h +56 -0
  80. package/cpp/rnwgpu/api/descriptors/GPUBufferDescriptor.h +59 -0
  81. package/cpp/rnwgpu/api/descriptors/GPUBufferUsage.h +39 -0
  82. package/cpp/rnwgpu/api/descriptors/GPUColor.h +66 -0
  83. package/cpp/rnwgpu/api/descriptors/GPUColorTargetState.h +58 -0
  84. package/cpp/rnwgpu/api/descriptors/GPUColorWrite.h +29 -0
  85. package/cpp/rnwgpu/api/descriptors/GPUCommandBufferDescriptor.h +45 -0
  86. package/cpp/rnwgpu/api/descriptors/GPUCommandEncoderDescriptor.h +45 -0
  87. package/cpp/rnwgpu/api/descriptors/GPUComputePassDescriptor.h +56 -0
  88. package/cpp/rnwgpu/api/descriptors/GPUComputePassTimestampWrites.h +58 -0
  89. package/cpp/rnwgpu/api/descriptors/GPUComputePipelineDescriptor.h +67 -0
  90. package/cpp/rnwgpu/api/descriptors/GPUDepthStencilState.h +103 -0
  91. package/cpp/rnwgpu/api/descriptors/GPUDeviceDescriptor.h +99 -0
  92. package/cpp/rnwgpu/api/descriptors/GPUExternalTextureBindingLayout.h +38 -0
  93. package/cpp/rnwgpu/api/descriptors/GPUExternalTextureDescriptor.h +71 -0
  94. package/cpp/rnwgpu/api/descriptors/GPUFragmentState.h +73 -0
  95. package/cpp/rnwgpu/api/descriptors/GPUImageCopyBuffer.h +62 -0
  96. package/cpp/rnwgpu/api/descriptors/GPUImageCopyExternalImage.h +62 -0
  97. package/cpp/rnwgpu/api/descriptors/GPUImageCopyTexture.h +65 -0
  98. package/cpp/rnwgpu/api/descriptors/GPUImageCopyTextureTagged.h +81 -0
  99. package/cpp/rnwgpu/api/descriptors/GPUImageDataLayout.h +54 -0
  100. package/cpp/rnwgpu/api/descriptors/GPUMapMode.h +22 -0
  101. package/cpp/rnwgpu/api/descriptors/GPUMultisampleState.h +54 -0
  102. package/cpp/rnwgpu/api/descriptors/GPUPipelineLayoutDescriptor.h +57 -0
  103. package/cpp/rnwgpu/api/descriptors/GPUPrimitiveState.h +69 -0
  104. package/cpp/rnwgpu/api/descriptors/GPUProgrammableStage.h +61 -0
  105. package/cpp/rnwgpu/api/descriptors/GPUQuerySetDescriptor.h +55 -0
  106. package/cpp/rnwgpu/api/descriptors/GPUQueueDescriptor.h +43 -0
  107. package/cpp/rnwgpu/api/descriptors/GPURenderBundleDescriptor.h +45 -0
  108. package/cpp/rnwgpu/api/descriptors/GPURenderBundleEncoderDescriptor.h +80 -0
  109. package/cpp/rnwgpu/api/descriptors/GPURenderPassColorAttachment.h +81 -0
  110. package/cpp/rnwgpu/api/descriptors/GPURenderPassDepthStencilAttachment.h +101 -0
  111. package/cpp/rnwgpu/api/descriptors/GPURenderPassDescriptor.h +92 -0
  112. package/cpp/rnwgpu/api/descriptors/GPURenderPassTimestampWrites.h +58 -0
  113. package/cpp/rnwgpu/api/descriptors/GPURenderPipelineDescriptor.h +103 -0
  114. package/cpp/rnwgpu/api/descriptors/GPURequestAdapterOptions.h +51 -0
  115. package/cpp/rnwgpu/api/descriptors/GPUSamplerBindingLayout.h +45 -0
  116. package/cpp/rnwgpu/api/descriptors/GPUSamplerDescriptor.h +110 -0
  117. package/cpp/rnwgpu/api/descriptors/GPUShaderModuleCompilationHint.h +60 -0
  118. package/cpp/rnwgpu/api/descriptors/GPUShaderModuleDescriptor.h +62 -0
  119. package/cpp/rnwgpu/api/descriptors/GPUShaderStage.h +25 -0
  120. package/cpp/rnwgpu/api/descriptors/GPUStencilFaceState.h +64 -0
  121. package/cpp/rnwgpu/api/descriptors/GPUStorageTextureBindingLayout.h +59 -0
  122. package/cpp/rnwgpu/api/descriptors/GPUTextureBindingLayout.h +59 -0
  123. package/cpp/rnwgpu/api/descriptors/GPUTextureDescriptor.h +91 -0
  124. package/cpp/rnwgpu/api/descriptors/GPUTextureUsage.h +29 -0
  125. package/cpp/rnwgpu/api/descriptors/GPUTextureViewDescriptor.h +91 -0
  126. package/cpp/rnwgpu/api/descriptors/GPUUncapturedErrorEventInit.h +64 -0
  127. package/cpp/rnwgpu/api/descriptors/GPUVertexAttribute.h +53 -0
  128. package/cpp/rnwgpu/api/descriptors/GPUVertexBufferLayout.h +62 -0
  129. package/cpp/rnwgpu/api/descriptors/GPUVertexState.h +73 -0
  130. package/cpp/rnwgpu/api/descriptors/Unions.h +1984 -0
  131. package/cpp/rnwgpu/async/AsyncDispatcher.h +28 -0
  132. package/cpp/rnwgpu/async/AsyncRunner.cpp +181 -0
  133. package/cpp/rnwgpu/async/AsyncRunner.h +56 -0
  134. package/cpp/rnwgpu/async/AsyncTaskHandle.cpp +181 -0
  135. package/cpp/rnwgpu/async/AsyncTaskHandle.h +55 -0
  136. package/cpp/rnwgpu/async/JSIMicrotaskDispatcher.cpp +23 -0
  137. package/cpp/rnwgpu/async/JSIMicrotaskDispatcher.h +22 -0
  138. package/lib/commonjs/Platform/Platform.web.js +1 -2
  139. package/lib/commonjs/Platform/Platform.web.js.map +1 -1
  140. package/lib/commonjs/external/reanimated/buffers.js.map +1 -1
  141. package/lib/commonjs/external/reanimated/interpolators.d.ts +1 -4
  142. package/lib/commonjs/renderer/Canvas.js +4 -3
  143. package/lib/commonjs/renderer/Canvas.js.map +1 -1
  144. package/lib/commonjs/renderer/components/Group.js +1 -2
  145. package/lib/commonjs/renderer/components/Group.js.map +1 -1
  146. package/lib/commonjs/renderer/components/shapes/FitBox.js +1 -2
  147. package/lib/commonjs/renderer/components/shapes/FitBox.js.map +1 -1
  148. package/lib/commonjs/skia/types/ContourMeasure.js.map +1 -1
  149. package/lib/commonjs/skia/types/Image/ImageFactory.d.ts +20 -0
  150. package/lib/commonjs/skia/types/Image/ImageFactory.js.map +1 -1
  151. package/lib/commonjs/skia/types/RuntimeEffect/RuntimeEffect.js.map +1 -1
  152. package/lib/commonjs/skia/types/Skia.d.ts +17 -0
  153. package/lib/commonjs/skia/types/Skia.js.map +1 -1
  154. package/lib/commonjs/skia/web/JsiSkImageFactory.d.ts +2 -0
  155. package/lib/commonjs/skia/web/JsiSkImageFactory.js +6 -0
  156. package/lib/commonjs/skia/web/JsiSkImageFactory.js.map +1 -1
  157. package/lib/commonjs/skia/web/JsiSkParagraphStyle.js +2 -2
  158. package/lib/commonjs/skia/web/JsiSkParagraphStyle.js.map +1 -1
  159. package/lib/commonjs/skia/web/JsiSkia.js +6 -0
  160. package/lib/commonjs/skia/web/JsiSkia.js.map +1 -1
  161. package/lib/commonjs/sksg/Recorder/commands/ColorFilters.js.map +1 -1
  162. package/lib/commonjs/sksg/Recorder/commands/ImageFilters.js.map +1 -1
  163. package/lib/commonjs/sksg/Recorder/commands/PathEffects.js.map +1 -1
  164. package/lib/commonjs/sksg/Recorder/commands/Shaders.js.map +1 -1
  165. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.d.ts +2 -0
  166. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.js +2 -0
  167. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.js.map +1 -1
  168. package/lib/commonjs/views/SkiaPictureView.web.js +4 -4
  169. package/lib/commonjs/views/SkiaPictureView.web.js.map +1 -1
  170. package/lib/commonjs/web/WithSkiaWeb.js +1 -2
  171. package/lib/commonjs/web/WithSkiaWeb.js.map +1 -1
  172. package/lib/module/external/reanimated/buffers.js.map +1 -1
  173. package/lib/module/external/reanimated/interpolators.d.ts +1 -4
  174. package/lib/module/renderer/Canvas.js +3 -1
  175. package/lib/module/renderer/Canvas.js.map +1 -1
  176. package/lib/module/skia/types/ContourMeasure.js.map +1 -1
  177. package/lib/module/skia/types/Image/ImageFactory.d.ts +20 -0
  178. package/lib/module/skia/types/Image/ImageFactory.js.map +1 -1
  179. package/lib/module/skia/types/RuntimeEffect/RuntimeEffect.js.map +1 -1
  180. package/lib/module/skia/types/Skia.d.ts +17 -0
  181. package/lib/module/skia/types/Skia.js.map +1 -1
  182. package/lib/module/skia/web/JsiSkImageFactory.d.ts +2 -0
  183. package/lib/module/skia/web/JsiSkImageFactory.js +6 -0
  184. package/lib/module/skia/web/JsiSkImageFactory.js.map +1 -1
  185. package/lib/module/skia/web/JsiSkParagraphStyle.js +2 -2
  186. package/lib/module/skia/web/JsiSkParagraphStyle.js.map +1 -1
  187. package/lib/module/skia/web/JsiSkia.js +6 -0
  188. package/lib/module/skia/web/JsiSkia.js.map +1 -1
  189. package/lib/module/sksg/Recorder/commands/ColorFilters.js.map +1 -1
  190. package/lib/module/sksg/Recorder/commands/ImageFilters.js.map +1 -1
  191. package/lib/module/sksg/Recorder/commands/PathEffects.js.map +1 -1
  192. package/lib/module/sksg/Recorder/commands/Shaders.js.map +1 -1
  193. package/lib/module/specs/SkiaPictureViewNativeComponent.d.ts +2 -0
  194. package/lib/module/specs/SkiaPictureViewNativeComponent.js +4 -0
  195. package/lib/module/specs/SkiaPictureViewNativeComponent.js.map +1 -1
  196. package/lib/module/views/SkiaPictureView.web.js +3 -2
  197. package/lib/module/views/SkiaPictureView.web.js.map +1 -1
  198. package/lib/typescript/lib/commonjs/skia/web/JsiSkImageFactory.d.ts +2 -0
  199. package/lib/typescript/lib/commonjs/skia/web/JsiSkia.d.ts +2 -0
  200. package/lib/typescript/lib/module/renderer/Canvas.d.ts +1 -1
  201. package/lib/typescript/lib/module/skia/Skia.web.d.ts +2 -0
  202. package/lib/typescript/lib/module/skia/web/JsiSkImageFactory.d.ts +2 -0
  203. package/lib/typescript/lib/module/skia/web/JsiSkia.d.ts +2 -0
  204. package/lib/typescript/lib/module/views/SkiaPictureView.d.ts +1 -1
  205. package/lib/typescript/src/external/reanimated/interpolators.d.ts +1 -4
  206. package/lib/typescript/src/skia/types/Image/ImageFactory.d.ts +20 -0
  207. package/lib/typescript/src/skia/types/Skia.d.ts +17 -0
  208. package/lib/typescript/src/skia/web/JsiSkImageFactory.d.ts +2 -0
  209. package/lib/typescript/src/specs/SkiaPictureViewNativeComponent.d.ts +2 -0
  210. package/package.json +20 -16
  211. package/react-native-skia.podspec +52 -13
  212. package/scripts/install-skia.mjs +99 -48
  213. package/src/external/reanimated/buffers.ts +1 -1
  214. package/src/renderer/Canvas.tsx +3 -2
  215. package/src/skia/types/ContourMeasure.tsx +1 -2
  216. package/src/skia/types/Image/ImageFactory.ts +22 -0
  217. package/src/skia/types/RuntimeEffect/RuntimeEffect.ts +1 -2
  218. package/src/skia/types/Skia.ts +17 -0
  219. package/src/skia/web/JsiSkImageFactory.ts +8 -0
  220. package/src/skia/web/JsiSkia.ts +6 -0
  221. package/src/sksg/Recorder/commands/ColorFilters.ts +3 -2
  222. package/src/sksg/Recorder/commands/ImageFilters.ts +3 -2
  223. package/src/sksg/Recorder/commands/PathEffects.ts +3 -2
  224. package/src/sksg/Recorder/commands/Shaders.ts +3 -2
  225. package/src/specs/SkiaPictureViewNativeComponent.ts +7 -0
  226. package/src/views/SkiaPictureView.web.tsx +4 -1
@@ -0,0 +1,468 @@
1
+ #pragma once
2
+
3
+ #include <memory>
4
+ #include <vector>
5
+ #include <string>
6
+ #include <utility>
7
+ #include <type_traits>
8
+ #include <limits>
9
+ #include <variant>
10
+ #include <map>
11
+ #include <unordered_set>
12
+
13
+ #include <jsi/jsi.h>
14
+
15
+ #include "EnumMapper.h"
16
+ #include "Promise.h"
17
+
18
+ #ifdef SK_GRAPHITE
19
+ #include "rnwgpu/api/descriptors/Unions.h"
20
+ #include "rnwgpu/async/AsyncTaskHandle.h"
21
+ #endif
22
+
23
+ // This number is the maximum integer that can be represented exactly as a double
24
+ #define MAX_SAFE_INTEGER static_cast<uint64_t>(9007199254740991)
25
+
26
+ #if __has_include(<cxxabi.h>)
27
+ #include <cxxabi.h>
28
+ #endif
29
+
30
+ namespace rnwgpu {
31
+
32
+ namespace jsi = facebook::jsi;
33
+
34
+ // Unknown type (error)
35
+ template <typename ArgType, typename Enable = void> struct JSIConverter {
36
+ static ArgType fromJSI(jsi::Runtime&, const jsi::Value&, bool outOfBound) {
37
+ static_assert(always_false<ArgType>::value, "This type is not supported by the JSIConverter!");
38
+ return ArgType();
39
+ }
40
+ static jsi::Value toJSI(jsi::Runtime&, ArgType) {
41
+ static_assert(always_false<ArgType>::value, "This type is not supported by the JSIConverter!");
42
+ return jsi::Value::undefined();
43
+ }
44
+
45
+ private:
46
+ template <typename> struct always_false : std::false_type {};
47
+ };
48
+
49
+ // int <> number
50
+ template <> struct JSIConverter<int> {
51
+ static int fromJSI(jsi::Runtime&, const jsi::Value& arg, bool outOfBound) {
52
+ return static_cast<int>(arg.asNumber());
53
+ }
54
+ static jsi::Value toJSI(jsi::Runtime&, int arg) {
55
+ return jsi::Value(arg);
56
+ }
57
+ };
58
+
59
+ // double <> number
60
+ template <> struct JSIConverter<double> {
61
+ static double fromJSI(jsi::Runtime&, const jsi::Value& arg, bool outOfBound) {
62
+ return arg.asNumber();
63
+ }
64
+ static jsi::Value toJSI(jsi::Runtime&, double arg) {
65
+ return jsi::Value(arg);
66
+ }
67
+ };
68
+
69
+ // float <> number
70
+ template <> struct JSIConverter<float> {
71
+ static float fromJSI(jsi::Runtime&, const jsi::Value& arg, bool outOfBound) {
72
+ return static_cast<float>(arg.asNumber());
73
+ }
74
+ static jsi::Value toJSI(jsi::Runtime&, float arg) {
75
+ return jsi::Value(static_cast<double>(arg));
76
+ }
77
+ };
78
+
79
+ template <> struct JSIConverter<std::nullptr_t> {
80
+ static std::nullptr_t fromJSI(jsi::Runtime&, const jsi::Value& arg, bool outOfBound) {
81
+ return nullptr;
82
+ }
83
+ static jsi::Value toJSI(jsi::Runtime&, std::nullptr_t arg) {
84
+ return jsi::Value::null();
85
+ }
86
+ };
87
+
88
+ template <typename T>
89
+ struct JSIConverter<T, std::enable_if_t<!std::is_same_v<T, uint64_t> && std::is_same_v<T, size_t>>> {
90
+ static size_t fromJSI(jsi::Runtime& runtime, const jsi::Value& arg, bool outOfBound) {
91
+ if (arg.isNumber()) {
92
+ double value = arg.asNumber();
93
+ return static_cast<size_t>(value);
94
+ } else {
95
+ return arg.asBigInt(runtime).asInt64(runtime);
96
+ }
97
+ }
98
+
99
+ static jsi::Value toJSI(jsi::Runtime& runtime, size_t arg) {
100
+ return jsi::Value(static_cast<double>(arg));
101
+ }
102
+ };
103
+
104
+ // uint32_t <> double
105
+ template <> struct JSIConverter<uint32_t> {
106
+ static double fromJSI(jsi::Runtime& runtime, const jsi::Value& arg, bool outOfBound) {
107
+ double value = arg.asNumber();
108
+ return static_cast<uint32_t>(value);
109
+ }
110
+ static jsi::Value toJSI(jsi::Runtime& runtime, uint32_t arg) {
111
+ return jsi::Value(static_cast<double>(arg));
112
+ }
113
+ };
114
+
115
+ // uint64_t <> BigInt
116
+ template <> struct JSIConverter<uint64_t> {
117
+ static uint64_t fromJSI(jsi::Runtime& runtime, const jsi::Value& arg, bool outOfBound) {
118
+ if (arg.isNumber()) {
119
+ double value = arg.asNumber();
120
+ if (value < 0 || value > MAX_SAFE_INTEGER) {
121
+ throw jsi::JSError(runtime, "Number out of range for uint64_t");
122
+ }
123
+ return static_cast<uint64_t>(value);
124
+ } else {
125
+ return arg.asBigInt(runtime).getUint64(runtime);
126
+ }
127
+ }
128
+
129
+ static jsi::Value toJSI(jsi::Runtime& runtime, uint64_t arg) {
130
+ if (arg <= MAX_SAFE_INTEGER) {
131
+ return jsi::Value(static_cast<double>(arg));
132
+ } else {
133
+ throw jsi::JSError(runtime, "Number too large to be represented as a double");
134
+ }
135
+ }
136
+ };
137
+
138
+ template <> struct JSIConverter<void *> {
139
+ static void* fromJSI(jsi::Runtime& runtime, const jsi::Value& arg, bool outOfBound) {
140
+ return reinterpret_cast<void *>(arg.asBigInt(runtime).getUint64(runtime));
141
+ }
142
+
143
+ static jsi::Value toJSI(jsi::Runtime& runtime, void* arg) {
144
+ return jsi::BigInt::fromUint64(runtime, reinterpret_cast<uint64_t>(arg));
145
+ }
146
+ };
147
+
148
+ // bool <> boolean
149
+ template <> struct JSIConverter<bool> {
150
+ static bool fromJSI(jsi::Runtime&, const jsi::Value& arg, bool outOfBound) {
151
+ return arg.asBool();
152
+ }
153
+ static jsi::Value toJSI(jsi::Runtime&, bool arg) {
154
+ return jsi::Value(arg);
155
+ }
156
+ };
157
+
158
+ // std::string <> string
159
+ template <> struct JSIConverter<std::string> {
160
+ static std::string fromJSI(jsi::Runtime& runtime, const jsi::Value& arg, bool outOfBound) {
161
+ return arg.asString(runtime).utf8(runtime);
162
+ }
163
+ static jsi::Value toJSI(jsi::Runtime& runtime, const std::string& arg) {
164
+ return jsi::String::createFromUtf8(runtime, arg);
165
+ }
166
+ };
167
+
168
+ // std::optional<T> <> T | undefined
169
+ template <typename TInner> struct JSIConverter<std::optional<TInner>> {
170
+ static std::optional<TInner> fromJSI(jsi::Runtime& runtime, const jsi::Value& arg, bool outOfBound) {
171
+ if (outOfBound || arg.isUndefined()) {
172
+ return {};
173
+ } else {
174
+ return JSIConverter<TInner>::fromJSI(runtime, std::move(arg), outOfBound);
175
+ }
176
+ }
177
+ static jsi::Value toJSI(jsi::Runtime& runtime, const std::optional<TInner>& arg) {
178
+ if (arg == std::nullopt) {
179
+ return jsi::Value::undefined();
180
+ } else {
181
+ return JSIConverter<TInner>::toJSI(runtime, arg.value());
182
+ }
183
+ }
184
+ };
185
+
186
+ // Enum <> Union
187
+ template <typename TEnum> struct JSIConverter<TEnum, std::enable_if_t<std::is_enum<TEnum>::value>> {
188
+ static TEnum fromJSI(jsi::Runtime& runtime, const jsi::Value& arg, bool outOfBound) {
189
+ std::string string = arg.asString(runtime).utf8(runtime);
190
+ TEnum outEnum;
191
+ EnumMapper::convertJSUnionToEnum(string, &outEnum);
192
+ return outEnum;
193
+ }
194
+ static jsi::Value toJSI(jsi::Runtime& runtime, const TEnum& arg) {
195
+ std::string outUnion;
196
+ EnumMapper::convertEnumToJSUnion(arg, &outUnion);
197
+ return jsi::String::createFromUtf8(runtime, outUnion);
198
+ }
199
+ };
200
+
201
+ #ifdef SK_GRAPHITE
202
+ // AsyncTaskHandle <> Promise
203
+ template <> struct JSIConverter<rnwgpu::async::AsyncTaskHandle> {
204
+ static rnwgpu::async::AsyncTaskHandle fromJSI(jsi::Runtime&, const jsi::Value&, bool) {
205
+ throw std::runtime_error("Cannot convert a Promise to AsyncTaskHandle on the native side.");
206
+ }
207
+
208
+ static jsi::Value toJSI(jsi::Runtime& runtime, rnwgpu::async::AsyncTaskHandle&& handle) {
209
+ return rnwgpu::Promise::createPromise(runtime, [handle = std::move(handle)](jsi::Runtime& runtime,
210
+ std::shared_ptr<rnwgpu::Promise> promise) mutable {
211
+ if (!handle.valid()) {
212
+ promise->resolve(jsi::Value::undefined());
213
+ return;
214
+ }
215
+
216
+ handle.attachPromise(promise);
217
+ });
218
+ }
219
+ };
220
+ #endif
221
+
222
+ // std::map<std::string, T> <> Record<string, T>
223
+ template <typename ValueType> struct JSIConverter<std::map<std::string, ValueType>> {
224
+ static std::map<std::string, ValueType> fromJSI(jsi::Runtime& runtime, const jsi::Value& arg, bool outOfBound) {
225
+ jsi::Object object = arg.asObject(runtime);
226
+ jsi::Array propertyNames = object.getPropertyNames(runtime);
227
+ size_t length = propertyNames.size(runtime);
228
+
229
+ std::map<std::string, ValueType> map;
230
+ for (size_t i = 0; i < length; ++i) {
231
+ std::string key = propertyNames.getValueAtIndex(runtime, i).asString(runtime).utf8(runtime);
232
+ jsi::Value value = object.getProperty(runtime, key.c_str());
233
+ map.emplace(key, JSIConverter<ValueType>::fromJSI(runtime, value, outOfBound));
234
+ }
235
+ return map;
236
+ }
237
+ static jsi::Value toJSI(jsi::Runtime& runtime, const std::map<std::string, ValueType>& map) {
238
+ jsi::Object object(runtime);
239
+ for (const auto& pair : map) {
240
+ jsi::Value value = JSIConverter<ValueType>::toJSI(runtime, pair.second);
241
+ jsi::String key = jsi::String::createFromUtf8(runtime, pair.first);
242
+ object.setProperty(runtime, key, std::move(value));
243
+ }
244
+ return object;
245
+ }
246
+ };
247
+
248
+
249
+ // std::vector<T> <> T[]
250
+ template <typename ElementType> struct JSIConverter<std::vector<ElementType>> {
251
+ static std::vector<ElementType> fromJSI(jsi::Runtime& runtime, const jsi::Value& arg, bool outOfBound) {
252
+ jsi::Array array = arg.asObject(runtime).asArray(runtime);
253
+ size_t length = array.size(runtime);
254
+
255
+ std::vector<ElementType> vector;
256
+ vector.reserve(length);
257
+ for (size_t i = 0; i < length; ++i) {
258
+ jsi::Value elementValue = array.getValueAtIndex(runtime, i);
259
+ vector.emplace_back(JSIConverter<ElementType>::fromJSI(runtime, elementValue, outOfBound));
260
+ }
261
+ return vector;
262
+ }
263
+ static jsi::Value toJSI(jsi::Runtime& runtime, const std::vector<ElementType>& vector) {
264
+ jsi::Array array(runtime, vector.size());
265
+ for (size_t i = 0; i < vector.size(); i++) {
266
+ jsi::Value value = JSIConverter<ElementType>::toJSI(runtime, vector[i]);
267
+ array.setValueAtIndex(runtime, i, std::move(value));
268
+ }
269
+ return array;
270
+ }
271
+ };
272
+
273
+ // NativeState <> {}
274
+ template <typename T> struct is_shared_ptr_to_native_state : std::false_type {};
275
+
276
+ template <typename T> struct is_shared_ptr_to_native_state<std::shared_ptr<T>> : std::is_base_of<jsi::NativeState, T> {};
277
+
278
+ template <typename T> struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_native_state<T>::value>> {
279
+ using TPointee = typename T::element_type;
280
+
281
+ #if DEBUG
282
+ inline static std::string getFriendlyTypename() {
283
+ std::string name = std::string(typeid(TPointee).name());
284
+ #if __has_include(<cxxabi.h>)
285
+ int status = 0;
286
+ char* demangled_name = abi::__cxa_demangle(name.c_str(), NULL, NULL, &status);
287
+ if (status == 0) {
288
+ name = demangled_name;
289
+ std::free(demangled_name);
290
+ }
291
+ #endif
292
+ return name;
293
+ }
294
+
295
+ inline static std::string invalidTypeErrorMessage(const std::string& typeDescription, const std::string& reason) {
296
+ return "Cannot convert \"" + typeDescription + "\" to NativeState<" + getFriendlyTypename() + ">! " + reason;
297
+ }
298
+ #endif
299
+
300
+ static T fromJSI(jsi::Runtime& runtime, const jsi::Value& arg, bool outOfBound) {
301
+ #if DEBUG
302
+ if (arg.isUndefined()) {
303
+ [[unlikely]];
304
+ throw jsi::JSError(runtime, invalidTypeErrorMessage("undefined", "It is undefined!"));
305
+ }
306
+ if (!arg.isObject()) {
307
+ [[unlikely]];
308
+ std::string stringRepresentation = arg.toString(runtime).utf8(runtime);
309
+ throw jsi::JSError(runtime, invalidTypeErrorMessage(stringRepresentation, "It is not an object!"));
310
+ }
311
+ #endif
312
+ jsi::Object object = arg.getObject(runtime);
313
+ #if DEBUG
314
+ if (!object.hasNativeState<TPointee>(runtime)) {
315
+ [[unlikely]];
316
+ std::string stringRepresentation = arg.toString(runtime).utf8(runtime);
317
+ throw jsi::JSError(runtime, invalidTypeErrorMessage(stringRepresentation, "It is a different NativeState<T>!"));
318
+ }
319
+ #endif
320
+ return object.getNativeState<TPointee>(runtime);
321
+ }
322
+ private:
323
+ // SFINAE helper to detect if T has a IsNativeObject marker type
324
+ template <typename U, typename = typename U::IsNativeObject>
325
+ static std::true_type hasNativeObjectMarker(int);
326
+ template <typename U>
327
+ static std::false_type hasNativeObjectMarker(...);
328
+
329
+ // Type trait for detection
330
+ static constexpr bool is_native_object = decltype(hasNativeObjectMarker<TPointee>(0))::value;
331
+
332
+ public:
333
+ static jsi::Value toJSI(jsi::Runtime& runtime, const T& arg) {
334
+ #if DEBUG
335
+ if (arg == nullptr) {
336
+ [[unlikely]];
337
+ throw jsi::JSError(runtime, "Cannot convert nullptr to NativeState<" + getFriendlyTypename() + ">!");
338
+ }
339
+ #endif
340
+ // Check if the type is a NativeObject (has IsNativeObject marker)
341
+ // Use TPointee::create() if it's a NativeObject, otherwise fall back to plain setNativeState
342
+ if constexpr (is_native_object) {
343
+ return TPointee::create(runtime, arg);
344
+ } else {
345
+ jsi::Object object(runtime);
346
+ object.setNativeState(runtime, arg);
347
+ return object;
348
+ }
349
+ }
350
+ };
351
+
352
+
353
+ template <typename O, typename T>
354
+ struct JSIConverter<std::variant<std::vector<T>, std::shared_ptr<O>>> {
355
+ using Target = std::variant<std::vector<T>, std::shared_ptr<O>>;
356
+
357
+ static Target fromJSI(jsi::Runtime &runtime, const jsi::Value &arg,
358
+ bool outOfBound) {
359
+ if (arg.isObject()) {
360
+ auto object = arg.getObject(runtime);
361
+ if (object.isArray(runtime)) {
362
+ return Target(
363
+ JSIConverter<std::vector<T>>::fromJSI(runtime, arg, outOfBound));
364
+ }
365
+ throw std::runtime_error("Invalid variant type expected array");
366
+ }
367
+ return Target(
368
+ JSIConverter<std::shared_ptr<O>>::fromJSI(runtime, arg, outOfBound));
369
+ }
370
+
371
+ static jsi::Value toJSI(jsi::Runtime &, Target arg) {
372
+ return jsi::Value::null();
373
+ }
374
+ };
375
+
376
+ template <typename O>
377
+ struct JSIConverter<std::variant<std::nullptr_t, std::shared_ptr<O>>> {
378
+ using Target = std::variant<std::nullptr_t, std::shared_ptr<O>>;
379
+
380
+ static Target fromJSI(jsi::Runtime &runtime, const jsi::Value &arg,
381
+ bool outOfBound) {
382
+ if (arg.isNull()) {
383
+ return Target(nullptr);
384
+ }
385
+ return Target(
386
+ JSIConverter<std::shared_ptr<O>>::fromJSI(runtime, arg, outOfBound));
387
+ }
388
+
389
+ static jsi::Value toJSI(jsi::Runtime &runtime, Target arg) {
390
+ if (std::holds_alternative<std::nullptr_t>(arg)) {
391
+ return jsi::Value::null();
392
+ }
393
+ return JSIConverter<std::shared_ptr<O>>::toJSI(
394
+ runtime, std::get<std::shared_ptr<O>>(arg));
395
+ }
396
+ };
397
+
398
+ template <typename O>
399
+ struct JSIConverter<std::variant<std::nullptr_t, O>,
400
+ std::enable_if_t<std::is_enum_v<O>>> {
401
+ using Target = std::variant<std::nullptr_t, O>;
402
+
403
+ static Target fromJSI(jsi::Runtime &runtime, const jsi::Value &arg,
404
+ bool outOfBound) {
405
+ if (arg.isNull()) {
406
+ return Target(nullptr);
407
+ } else if (arg.isNumber()) {
408
+ return Target(static_cast<O>(arg.asNumber()));
409
+ }
410
+ return Target(JSIConverter<O>::fromJSI(runtime, arg, outOfBound));
411
+ }
412
+
413
+ static jsi::Value toJSI(jsi::Runtime &runtime, Target arg) {
414
+ if (std::holds_alternative<std::nullptr_t>(arg)) {
415
+ return jsi::Value::null();
416
+ }
417
+ return JSIConverter<O>::toJSI(runtime, std::get<O>(arg));
418
+ }
419
+ };
420
+
421
+ // TODO: careful std::variant<O, std::nullptr_t> doesn't overload
422
+ // std::variant<std::nullptr_t, 0> (order's matter)
423
+ // variant<nullptr_t, numeric>
424
+ template <typename O>
425
+ struct JSIConverter<
426
+ std::variant<O, std::nullptr_t>,
427
+ std::enable_if_t<std::is_arithmetic_v<O> || std::is_enum_v<O>>> {
428
+ using Target = std::variant<O, std::nullptr_t>;
429
+ static Target fromJSI(jsi::Runtime &runtime, const jsi::Value &arg,
430
+ bool outOfBound) {
431
+ if (arg.isNull()) {
432
+ return Target(nullptr);
433
+ }
434
+ if (arg.isNumber()) {
435
+ return Target(static_cast<O>(arg.asNumber()));
436
+ }
437
+ throw jsi::JSError(runtime, "Expected null or number");
438
+ }
439
+
440
+ static jsi::Value toJSI(jsi::Runtime &runtime, Target arg) {
441
+ if (std::holds_alternative<std::nullptr_t>(arg)) {
442
+ return jsi::Value::null();
443
+ }
444
+ return jsi::Value(static_cast<double>(std::get<O>(arg)));
445
+ }
446
+ };
447
+
448
+ template <>
449
+ struct JSIConverter<std::unordered_set<std::string>> {
450
+ using Target = std::unordered_set<std::string>;
451
+ static Target fromJSI(jsi::Runtime &runtime, const jsi::Value &arg,
452
+ bool outOfBound) {
453
+ throw jsi::JSError(runtime, "JSIConverter<std::unordered_set<std::string>>::fromJSI not implemented");
454
+ }
455
+
456
+ static jsi::Value toJSI(jsi::Runtime &runtime, Target arg) {
457
+ auto setConstructor = runtime.global().getPropertyAsFunction(runtime, "Set");
458
+ auto set = setConstructor.callAsConstructor(runtime).asObject(runtime);
459
+ auto add = set.getPropertyAsFunction(runtime, "add");
460
+ for (const auto& value : arg) {
461
+ jsi::Value jsiValue = JSIConverter<std::string>::toJSI(runtime, value);
462
+ add.callWithThis(runtime, set, jsiValue);
463
+ }
464
+ return std::move(set);
465
+ }
466
+ };
467
+
468
+ } // namespace rnwgpu