@shopify/react-native-skia 1.11.4 → 1.11.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. package/android/CMakeLists.txt +0 -1
  2. package/cpp/api/JsiSkParagraph.h +1 -3
  3. package/cpp/api/recorder/Convertor.h +57 -9
  4. package/cpp/api/recorder/Drawings.h +11 -36
  5. package/cpp/jsi/ViewProperty.h +1 -3
  6. package/lib/commonjs/animation/functions/interpolateColors.d.ts +1 -1
  7. package/lib/commonjs/dom/nodes/datatypes/Gradient.d.ts +2 -2
  8. package/lib/commonjs/dom/types/NodeType.js +8 -0
  9. package/lib/commonjs/dom/types/NodeType.js.map +1 -1
  10. package/lib/commonjs/external/reanimated/buffers.d.ts +1 -1
  11. package/lib/commonjs/renderer/__tests__/setup.d.ts +1 -1
  12. package/lib/commonjs/skia/types/Image/ColorType.js +20 -2
  13. package/lib/commonjs/skia/types/Image/ColorType.js.map +1 -1
  14. package/lib/commonjs/skia/types/Image/ColorType.web.js +18 -2
  15. package/lib/commonjs/skia/types/Image/ColorType.web.js.map +1 -1
  16. package/lib/commonjs/skia/types/ImageFilter/ImageFilter.js +14 -0
  17. package/lib/commonjs/skia/types/ImageFilter/ImageFilter.js.map +1 -1
  18. package/lib/commonjs/skia/types/MaskFilter.js +5 -2
  19. package/lib/commonjs/skia/types/MaskFilter.js.map +1 -1
  20. package/lib/commonjs/skia/types/Paint/BlendMode.js +31 -2
  21. package/lib/commonjs/skia/types/Paint/BlendMode.js.map +1 -1
  22. package/lib/commonjs/skia/types/Paragraph/ParagraphBuilder.js +14 -0
  23. package/lib/commonjs/skia/types/Paragraph/ParagraphBuilder.js.map +1 -1
  24. package/lib/commonjs/skia/types/Path/Path.js +4 -0
  25. package/lib/commonjs/skia/types/Path/Path.js.map +1 -1
  26. package/lib/commonjs/skia/web/JsiSkCanvas.d.ts +1 -1
  27. package/lib/commonjs/skia/web/JsiSkImage.d.ts +2 -2
  28. package/lib/commonjs/skia/web/JsiSkPaint.d.ts +1 -1
  29. package/lib/commonjs/skia/web/JsiSkPicture.d.ts +1 -1
  30. package/lib/commonjs/skia/web/JsiSkPoint.d.ts +1 -1
  31. package/lib/commonjs/skia/web/JsiSkRRect.d.ts +1 -1
  32. package/lib/commonjs/skia/web/JsiSkRSXform.d.ts +1 -1
  33. package/lib/commonjs/skia/web/JsiSkRect.d.ts +1 -1
  34. package/lib/commonjs/sksg/Container.d.ts +1 -0
  35. package/lib/commonjs/sksg/Container.js +8 -9
  36. package/lib/commonjs/sksg/Container.js.map +1 -1
  37. package/lib/commonjs/sksg/Recorder/Core.js +2 -0
  38. package/lib/commonjs/sksg/Recorder/Core.js.map +1 -1
  39. package/lib/commonjs/specs/NativeSkiaModule.web.d.ts +6 -0
  40. package/lib/commonjs/specs/NativeSkiaModule.web.js +33 -0
  41. package/lib/commonjs/specs/NativeSkiaModule.web.js.map +1 -1
  42. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.web.d.ts +9 -0
  43. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.web.js +32 -0
  44. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.web.js.map +1 -0
  45. package/lib/commonjs/views/types.d.ts +1 -0
  46. package/lib/commonjs/views/types.js.map +1 -1
  47. package/lib/commonjs/web/LoadSkiaWeb.js +1 -2
  48. package/lib/commonjs/web/LoadSkiaWeb.js.map +1 -1
  49. package/lib/module/animation/functions/interpolateColors.d.ts +1 -1
  50. package/lib/module/dom/nodes/datatypes/Gradient.d.ts +2 -2
  51. package/lib/module/dom/types/NodeType.js +8 -0
  52. package/lib/module/dom/types/NodeType.js.map +1 -1
  53. package/lib/module/external/reanimated/buffers.d.ts +1 -1
  54. package/lib/module/renderer/__tests__/setup.d.ts +1 -1
  55. package/lib/module/skia/types/Image/ColorType.js +20 -2
  56. package/lib/module/skia/types/Image/ColorType.js.map +1 -1
  57. package/lib/module/skia/types/Image/ColorType.web.js +18 -2
  58. package/lib/module/skia/types/Image/ColorType.web.js.map +1 -1
  59. package/lib/module/skia/types/ImageFilter/ImageFilter.js +14 -0
  60. package/lib/module/skia/types/ImageFilter/ImageFilter.js.map +1 -1
  61. package/lib/module/skia/types/MaskFilter.js +5 -2
  62. package/lib/module/skia/types/MaskFilter.js.map +1 -1
  63. package/lib/module/skia/types/Paint/BlendMode.js +31 -2
  64. package/lib/module/skia/types/Paint/BlendMode.js.map +1 -1
  65. package/lib/module/skia/types/Paragraph/ParagraphBuilder.js +14 -0
  66. package/lib/module/skia/types/Paragraph/ParagraphBuilder.js.map +1 -1
  67. package/lib/module/skia/types/Path/Path.js +4 -0
  68. package/lib/module/skia/types/Path/Path.js.map +1 -1
  69. package/lib/module/skia/web/JsiSkCanvas.d.ts +1 -1
  70. package/lib/module/skia/web/JsiSkImage.d.ts +2 -2
  71. package/lib/module/skia/web/JsiSkPaint.d.ts +1 -1
  72. package/lib/module/skia/web/JsiSkPicture.d.ts +1 -1
  73. package/lib/module/skia/web/JsiSkPoint.d.ts +1 -1
  74. package/lib/module/skia/web/JsiSkRRect.d.ts +1 -1
  75. package/lib/module/skia/web/JsiSkRSXform.d.ts +1 -1
  76. package/lib/module/skia/web/JsiSkRect.d.ts +1 -1
  77. package/lib/module/sksg/Container.d.ts +1 -0
  78. package/lib/module/sksg/Container.js +8 -9
  79. package/lib/module/sksg/Container.js.map +1 -1
  80. package/lib/module/sksg/Recorder/Core.js +2 -0
  81. package/lib/module/sksg/Recorder/Core.js.map +1 -1
  82. package/lib/module/specs/NativeSkiaModule.web.d.ts +6 -0
  83. package/lib/module/specs/NativeSkiaModule.web.js +30 -1
  84. package/lib/module/specs/NativeSkiaModule.web.js.map +1 -1
  85. package/lib/module/specs/SkiaPictureViewNativeComponent.web.d.ts +9 -0
  86. package/lib/module/specs/SkiaPictureViewNativeComponent.web.js +26 -0
  87. package/lib/module/specs/SkiaPictureViewNativeComponent.web.js.map +1 -0
  88. package/lib/module/views/types.d.ts +1 -0
  89. package/lib/module/views/types.js.map +1 -1
  90. package/lib/typescript/lib/commonjs/animation/functions/interpolateColors.d.ts +1 -1
  91. package/lib/typescript/lib/commonjs/renderer/components/image/ImageShader.d.ts +1 -1
  92. package/lib/typescript/lib/commonjs/skia/web/JsiSkColor.d.ts +1 -1
  93. package/lib/typescript/lib/commonjs/skia/web/JsiSkia.d.ts +1 -1
  94. package/lib/typescript/lib/commonjs/specs/NativeSkiaModule.web.d.ts +1 -0
  95. package/lib/typescript/lib/commonjs/specs/SkiaPictureViewNativeComponent.web.d.ts +10 -0
  96. package/lib/typescript/lib/module/animation/functions/interpolateColors.d.ts +1 -1
  97. package/lib/typescript/lib/module/mock/index.d.ts +2 -2
  98. package/lib/typescript/lib/module/renderer/components/image/ImageShader.d.ts +1 -1
  99. package/lib/typescript/lib/module/skia/Skia.web.d.ts +1 -1
  100. package/lib/typescript/lib/module/skia/web/JsiSkColor.d.ts +1 -1
  101. package/lib/typescript/lib/module/skia/web/JsiSkia.d.ts +1 -1
  102. package/lib/typescript/lib/module/specs/NativeSkiaModule.web.d.ts +1 -0
  103. package/lib/typescript/lib/module/specs/SkiaPictureViewNativeComponent.web.d.ts +9 -0
  104. package/lib/typescript/src/animation/functions/interpolateColors.d.ts +1 -1
  105. package/lib/typescript/src/dom/nodes/datatypes/Gradient.d.ts +2 -2
  106. package/lib/typescript/src/external/reanimated/buffers.d.ts +1 -1
  107. package/lib/typescript/src/renderer/__tests__/setup.d.ts +1 -1
  108. package/lib/typescript/src/skia/web/JsiSkCanvas.d.ts +1 -1
  109. package/lib/typescript/src/skia/web/JsiSkImage.d.ts +2 -2
  110. package/lib/typescript/src/skia/web/JsiSkPaint.d.ts +1 -1
  111. package/lib/typescript/src/skia/web/JsiSkPicture.d.ts +1 -1
  112. package/lib/typescript/src/skia/web/JsiSkPoint.d.ts +1 -1
  113. package/lib/typescript/src/skia/web/JsiSkRRect.d.ts +1 -1
  114. package/lib/typescript/src/skia/web/JsiSkRSXform.d.ts +1 -1
  115. package/lib/typescript/src/skia/web/JsiSkRect.d.ts +1 -1
  116. package/lib/typescript/src/sksg/Container.d.ts +1 -0
  117. package/lib/typescript/src/specs/NativeSkiaModule.web.d.ts +6 -0
  118. package/lib/typescript/src/specs/SkiaPictureViewNativeComponent.web.d.ts +9 -0
  119. package/lib/typescript/src/views/types.d.ts +1 -0
  120. package/package.json +1 -1
  121. package/src/sksg/Container.ts +8 -8
  122. package/src/specs/NativeSkiaModule.web.ts +38 -0
  123. package/src/specs/SkiaPictureViewNativeComponent.web.ts +39 -0
  124. package/src/views/types.ts +1 -0
  125. package/cpp/jsi/JsiValue.cpp +0 -345
  126. package/cpp/jsi/JsiValue.h +0 -222
  127. package/lib/commonjs/renderer/Canvas.web.d.ts +0 -10
  128. package/lib/commonjs/renderer/Canvas.web.js +0 -110
  129. package/lib/commonjs/renderer/Canvas.web.js.map +0 -1
  130. package/lib/module/renderer/Canvas.web.d.ts +0 -10
  131. package/lib/module/renderer/Canvas.web.js +0 -105
  132. package/lib/module/renderer/Canvas.web.js.map +0 -1
  133. package/lib/typescript/lib/commonjs/renderer/Canvas.web.d.ts +0 -3
  134. package/lib/typescript/lib/module/renderer/Canvas.web.d.ts +0 -1
  135. package/lib/typescript/src/renderer/Canvas.web.d.ts +0 -10
  136. package/src/renderer/Canvas.web.tsx +0 -134
@@ -0,0 +1,38 @@
1
+ import type { SkRect } from "../skia/types";
2
+ import type { ISkiaViewApi } from "../views/types";
3
+ import type { SkiaPictureView } from "../views/SkiaPictureView.web";
4
+
5
+ export type ISkiaViewApiWeb = ISkiaViewApi & {
6
+ views: Record<string, SkiaPictureView>;
7
+ registerView(nativeId: string, view: SkiaPictureView): void;
8
+ };
9
+
10
+ global.SkiaViewApi = {
11
+ views: {},
12
+ web: true,
13
+ registerView(nativeId: string, view: SkiaPictureView) {
14
+ this.views[nativeId] = view;
15
+ },
16
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
+ setJsiProperty(nativeId: number, name: string, value: any) {
18
+ if (name === "picture") {
19
+ this.views[`${nativeId}`].setPicture(value);
20
+ }
21
+ },
22
+ requestRedraw(nativeId: number) {
23
+ this.views[`${nativeId}`].redraw();
24
+ },
25
+ makeImageSnapshot(nativeId: number, rect?: SkRect) {
26
+ return this.views[`${nativeId}`].makeImageSnapshot(rect);
27
+ },
28
+ makeImageSnapshotAsync(nativeId: number, rect?: SkRect) {
29
+ return new Promise((resolve, reject) => {
30
+ const result = this.views[`${nativeId}`].makeImageSnapshot(rect);
31
+ if (result) {
32
+ resolve(result);
33
+ } else {
34
+ reject(new Error("Failed to make image snapshot"));
35
+ }
36
+ });
37
+ },
38
+ } as ISkiaViewApiWeb;
@@ -0,0 +1,39 @@
1
+ import type { ViewProps } from "react-native";
2
+ import { createElement, useEffect, useRef } from "react";
3
+
4
+ import { SkiaPictureView } from "../views/SkiaPictureView.web";
5
+
6
+ import type { ISkiaViewApiWeb } from "./NativeSkiaModule.web";
7
+
8
+ export interface NativeProps extends ViewProps {
9
+ debug?: boolean;
10
+ opaque?: boolean;
11
+ nativeID: string;
12
+ }
13
+
14
+ const SkiaPictureViewNativeComponent = ({
15
+ nativeID,
16
+ debug,
17
+ opaque,
18
+ onLayout,
19
+ ...viewProps
20
+ }: NativeProps) => {
21
+ const ref = useRef(null);
22
+ useEffect(() => {
23
+ if (ref.current) {
24
+ (global.SkiaViewApi as ISkiaViewApiWeb).registerView(
25
+ nativeID,
26
+ ref.current
27
+ );
28
+ }
29
+ }, [nativeID]);
30
+ return createElement(SkiaPictureView, {
31
+ ref,
32
+ debug,
33
+ opaque,
34
+ onLayout,
35
+ ...viewProps,
36
+ });
37
+ };
38
+ // eslint-disable-next-line import/no-default-export
39
+ export default SkiaPictureViewNativeComponent;
@@ -10,6 +10,7 @@ export type NativeSkiaViewProps = ViewProps & {
10
10
  };
11
11
 
12
12
  export interface ISkiaViewApi {
13
+ web?: boolean;
13
14
  setJsiProperty: <T>(nativeId: number, name: string, value: T) => void;
14
15
  requestRedraw: (nativeId: number) => void;
15
16
  makeImageSnapshot: (nativeId: number, rect?: SkRect) => SkImage;
@@ -1,345 +0,0 @@
1
- #include "JsiValue.h"
2
-
3
- namespace RNJsi {
4
-
5
- JsiValue::JsiValue() : _type(PropType::Undefined) {}
6
-
7
- JsiValue::JsiValue(jsi::Runtime &runtime, const jsi::Value &value)
8
- : JsiValue() {
9
- setCurrent(runtime, value);
10
- }
11
-
12
- void JsiValue::setCurrent(jsi::Runtime &runtime, const jsi::Value &value) {
13
- _stringValue = "";
14
- _hostObject = nullptr;
15
- _hostFunction = nullptr;
16
- _props.clear();
17
- _array.clear();
18
- _keysCache.clear();
19
-
20
- if (value.isNumber()) {
21
- _type = PropType::Number;
22
- _numberValue = value.asNumber();
23
- } else if (value.isBool()) {
24
- _type = PropType::Bool;
25
- _boolValue = value.getBool();
26
- } else if (value.isString()) {
27
- _type = PropType::String;
28
- _stringValue = value.asString(runtime).utf8(runtime);
29
- } else if (value.isUndefined()) {
30
- _type = PropType::Undefined;
31
- } else if (value.isNull()) {
32
- _type = PropType::Null;
33
- } else if (value.isObject()) {
34
- setObject(runtime, value);
35
- } else {
36
- throw std::runtime_error("Could not store jsi::Value of provided type");
37
- }
38
- }
39
-
40
- bool JsiValue::getAsBool() const {
41
- if (_type != PropType::Bool) {
42
- throw std::runtime_error("Expected type bool, got " +
43
- getTypeAsString(_type));
44
- }
45
- return _boolValue;
46
- }
47
-
48
- double JsiValue::getAsNumber() const {
49
- if (_type != PropType::Number) {
50
- throw std::runtime_error("Expected type number, got " +
51
- getTypeAsString(_type));
52
- }
53
- return _numberValue;
54
- }
55
-
56
- const std::string &JsiValue::getAsString() const {
57
- if (_type == PropType::Number) {
58
- return std::to_string(_numberValue);
59
- }
60
- if (_type != PropType::String) {
61
- throw std::runtime_error("Expected type string, got " +
62
- getTypeAsString(_type));
63
- }
64
- return _stringValue;
65
- }
66
-
67
- const std::vector<JsiValue> &JsiValue::getAsArray() const {
68
- if (_type != PropType::Array) {
69
- throw std::runtime_error("Expected type array, got " +
70
- getTypeAsString(_type));
71
- }
72
- return _array;
73
- }
74
-
75
- const JsiValue &JsiValue::getValue(PropId name) const {
76
- if (_type != PropType::Object) {
77
- throw std::runtime_error("Expected type object, got " +
78
- getTypeAsString(_type));
79
- }
80
- return _props.at(name);
81
- }
82
-
83
- bool JsiValue::hasValue(PropId name) const {
84
- if (_type != PropType::Object) {
85
- throw std::runtime_error("Expected type object, got " +
86
- getTypeAsString(_type));
87
- }
88
- return _props.count(name) > 0;
89
- }
90
-
91
- std::vector<PropId> JsiValue::getKeys() const {
92
- if (_type != PropType::Object) {
93
- throw std::runtime_error("Expected type object, got " +
94
- getTypeAsString(_type));
95
- }
96
- return _keysCache;
97
- }
98
-
99
- std::shared_ptr<jsi::HostObject> JsiValue::getAsHostObject() const {
100
- if (_type != PropType::HostObject) {
101
- throw std::runtime_error("Expected type host object, got " +
102
- getTypeAsString(_type));
103
- }
104
- return _hostObject;
105
- }
106
-
107
- jsi::HostFunctionType JsiValue::getAsHostFunction() const {
108
- if (_type != PropType::HostFunction) {
109
- throw std::runtime_error("Expected type host function, got " +
110
- getTypeAsString(_type));
111
- }
112
- return _hostFunction;
113
- }
114
-
115
- const jsi::HostFunctionType JsiValue::getAsFunction() const {
116
- return getAsHostFunction();
117
- }
118
-
119
- std::string JsiValue::asString() const {
120
- switch (_type) {
121
- case PropType::Null:
122
- return "(null)";
123
- case PropType::Undefined:
124
- return "(undefined)";
125
- case PropType::Number:
126
- return std::to_string(_numberValue);
127
- case PropType::Bool:
128
- return std::to_string(_boolValue);
129
- case PropType::String:
130
- return _stringValue;
131
- case PropType::Object:
132
- return "[Object]";
133
- case PropType::Array:
134
- return "[Array]";
135
- case PropType::HostObject:
136
- return "[HostObject]";
137
- case PropType::HostFunction:
138
- return "[HostFunction]";
139
- }
140
- }
141
-
142
- jsi::Value JsiValue::getAsJsiValue(jsi::Runtime &runtime) const {
143
- switch (_type) {
144
- case PropType::Undefined:
145
- return jsi::Value::undefined();
146
- case PropType::Null:
147
- return jsi::Value::null();
148
- case PropType::Number:
149
- return _numberValue;
150
- case PropType::Bool:
151
- return _boolValue;
152
- case PropType::String:
153
- return jsi::String::createFromUtf8(runtime, _stringValue);
154
- case PropType::Object:
155
- return getObject(runtime);
156
- case PropType::Array:
157
- return getArray(runtime);
158
- case PropType::HostObject:
159
- return getHostObject(runtime);
160
- case PropType::HostFunction:
161
- return getHostFunction(runtime);
162
- }
163
- }
164
-
165
- std::string JsiValue::getTypeAsString(PropType type) {
166
- switch (type) {
167
- case PropType::Undefined:
168
- return "undefined";
169
- case PropType::Null:
170
- return "null";
171
- case PropType::Number:
172
- return "number";
173
- case PropType::Bool:
174
- return "boolean";
175
- case PropType::String:
176
- return "string";
177
- case PropType::Object:
178
- return "object";
179
- case PropType::Array:
180
- return "array";
181
- case PropType::HostObject:
182
- return "hostobject";
183
- case PropType::HostFunction:
184
- return "hostfunction";
185
- }
186
- }
187
-
188
- void JsiValue::setObject(jsi::Runtime &runtime, const jsi::Value &value) {
189
- auto obj = value.asObject(runtime);
190
- if (obj.isFunction(runtime)) {
191
- setFunction(runtime, value);
192
- } else if (obj.isArray(runtime)) {
193
- setArray(runtime, obj);
194
- } else if (obj.isHostObject(runtime)) {
195
- setHostObject(runtime, obj);
196
- } else {
197
- _type = PropType::Object;
198
- // Read object keys
199
- auto keys = obj.getPropertyNames(runtime);
200
- size_t size = keys.size(runtime);
201
- _keysCache.clear();
202
- _keysCache.reserve(size);
203
- _props.clear();
204
- _props.reserve(size);
205
-
206
- for (size_t i = 0; i < size; ++i) {
207
- auto key = JsiPropId::get(
208
- keys.getValueAtIndex(runtime, i).asString(runtime).utf8(runtime));
209
- try {
210
- _props.try_emplace(key, runtime, obj.getProperty(runtime, key));
211
- _keysCache.push_back(key);
212
- } catch (jsi::JSError e) {
213
- throw jsi::JSError(runtime,
214
- "Could not set property for key " +
215
- std::string(key) + ":\n" + e.getMessage(),
216
- e.getStack());
217
- }
218
- }
219
- }
220
- }
221
-
222
- jsi::Object JsiValue::getObject(jsi::Runtime &runtime) const {
223
- assert(_type == PropType::Object);
224
- auto obj = jsi::Object(runtime);
225
- for (auto &p : _props) {
226
- obj.setProperty(runtime, p.first, p.second.getAsJsiValue(runtime));
227
- }
228
- return obj;
229
- }
230
-
231
- bool JsiValue::operator!=(const JsiValue &other) const {
232
- return !(this->operator==(other));
233
- }
234
-
235
- bool JsiValue::operator==(const JsiValue &other) const {
236
-
237
- if (other.getType() != getType()) {
238
- return false;
239
- }
240
-
241
- switch (_type) {
242
- case PropType::Null:
243
- case PropType::Undefined:
244
- return true;
245
- case PropType::Number:
246
- return _numberValue == other.getAsNumber();
247
- case PropType::Bool:
248
- return _boolValue == other.getAsBool();
249
- case PropType::String:
250
- return _stringValue == other.getAsString();
251
- case PropType::Object: {
252
- if (_props.size() != other.getProps().size()) {
253
- return false;
254
- }
255
- for (auto &p : _props) {
256
- auto t = p.second.operator==(other.getValue(p.first));
257
- if (!t) {
258
- return false;
259
- }
260
- }
261
- return true;
262
- }
263
- case PropType::Array: {
264
- auto otherArr = other.getAsArray();
265
- if (_array.size() != otherArr.size()) {
266
- return false;
267
- }
268
- for (size_t i = 0; i < _array.size(); ++i) {
269
- if (!_array[i].operator==(otherArr[i])) {
270
- return false;
271
- }
272
- }
273
- return true;
274
- }
275
- case PropType::HostObject:
276
- return getAsHostObject() == other.getAsHostObject();
277
- case PropType::HostFunction:
278
- // Unable to compare host functions
279
- return false;
280
- }
281
-
282
- throw std::runtime_error(
283
- "Wrong type in equals call. Should not happen. File a bug.");
284
-
285
- return false;
286
- }
287
-
288
- void JsiValue::setFunction(jsi::Runtime &runtime, const jsi::Value &value) {
289
- auto func = value.asObject(runtime).asFunction(runtime);
290
- if (func.isHostFunction(runtime)) {
291
- _type = PropType::HostFunction;
292
- _hostFunction = func.getHostFunction(runtime);
293
- } else {
294
- _type = PropType::HostFunction;
295
- auto obj = std::make_shared<jsi::Object>(value.asObject(runtime));
296
- _hostFunction = [obj](jsi::Runtime &runtime, const jsi::Value &thisValue,
297
- const jsi::Value *arguments,
298
- size_t count) -> jsi::Value {
299
- auto func = obj->asFunction(runtime);
300
- if (thisValue.isNull() || thisValue.isUndefined()) {
301
- return func.call(runtime, arguments, count);
302
- } else {
303
- return func.callWithThis(runtime, thisValue.asObject(runtime),
304
- arguments, count);
305
- }
306
- };
307
- }
308
- }
309
-
310
- jsi::Object JsiValue::getHostFunction(jsi::Runtime &runtime) const {
311
- assert(_type == PropType::HostFunction);
312
- return jsi::Function::createFromHostFunction(
313
- runtime, jsi::PropNameID::forUtf8(runtime, "fn"), 0, _hostFunction);
314
- }
315
-
316
- void JsiValue::setArray(jsi::Runtime &runtime, const jsi::Object &obj) {
317
- _type = PropType::Array;
318
- auto arr = obj.asArray(runtime);
319
- size_t size = arr.size(runtime);
320
- _array.reserve(size);
321
- for (size_t i = 0; i < size; ++i) {
322
- _array.emplace_back(runtime, arr.getValueAtIndex(runtime, i));
323
- }
324
- }
325
-
326
- jsi::Array JsiValue::getArray(jsi::Runtime &runtime) const {
327
- assert(_type == PropType::Array);
328
- jsi::Array arr = jsi::Array(runtime, _array.size());
329
- for (size_t i = 0; i < _array.size(); ++i) {
330
- arr.setValueAtIndex(runtime, i, _array[i].getAsJsiValue(runtime));
331
- }
332
- return arr;
333
- }
334
-
335
- void JsiValue::setHostObject(jsi::Runtime &runtime, const jsi::Object &obj) {
336
- _type = PropType::HostObject;
337
- _hostObject = obj.asHostObject(runtime);
338
- }
339
-
340
- jsi::Object JsiValue::getHostObject(jsi::Runtime &runtime) const {
341
- assert(_type == PropType::HostObject);
342
- return jsi::Object::createFromHostObject(runtime, _hostObject);
343
- }
344
-
345
- } // namespace RNJsi
@@ -1,222 +0,0 @@
1
- #pragma once
2
-
3
- #include <jsi/jsi.h>
4
-
5
- #include <memory>
6
- #include <string>
7
- #include <unordered_map>
8
- #include <utility>
9
- #include <vector>
10
-
11
- namespace RNJsi {
12
-
13
- namespace jsi = facebook::jsi;
14
-
15
- enum struct PropType {
16
- Undefined = 0,
17
- Null = 1, // Keep undefined / null constant so that we can do checks faster
18
- Bool = 2,
19
- Number = 3,
20
- String = 4,
21
- Object = 5,
22
- HostObject = 6,
23
- HostFunction = 7,
24
- Array = 8
25
- };
26
-
27
- using PropId = const char *;
28
-
29
- class JsiPropId {
30
- public:
31
- static const char *get(const std::string &name) { return _get(name); }
32
-
33
- static const char *get(const std::string &&name) {
34
- return _get(std::move(name));
35
- }
36
-
37
- private:
38
- static const char *_get(const std::string &name) {
39
- if (_impls().count(name) == 0) {
40
- // Alloc string
41
- char *impl = new char[name.size() + 1];
42
- strncpy(impl, name.c_str(), name.size() + 1);
43
- _impls().emplace(name, impl);
44
- }
45
- return _impls().at(name);
46
- }
47
-
48
- static std::unordered_map<std::string, PropId> &_impls() {
49
- static std::unordered_map<std::string, PropId> impls;
50
- return impls;
51
- }
52
- };
53
-
54
- /**
55
- This is a class that deep copies values from JS to C++.
56
- */
57
- class JsiValue {
58
- public:
59
- /**
60
- Default constructor for an empty JsiValue
61
- */
62
- JsiValue();
63
-
64
- /**
65
- Constructs an instance of the JsiValue object with a current value
66
- */
67
- JsiValue(jsi::Runtime &runtime, const jsi::Value &value);
68
-
69
- /**
70
- Updates the current value from the Javascript value. This function will
71
- perform a deep copy of the javascript value.
72
- */
73
- void setCurrent(jsi::Runtime &runtime, const jsi::Value &value);
74
-
75
- /**
76
- Returns the type of value contained in this JsiValue
77
- */
78
- PropType getType() const { return _type; }
79
-
80
- /**
81
- Returns true if the value is undefined or null.
82
- */
83
- bool isUndefinedOrNull() const { return _type <= PropType::Null; }
84
-
85
- /**
86
- Returns true if the value is undefined.
87
- */
88
- bool isUndefined() const { return _type == PropType::Undefined; }
89
-
90
- /**
91
- Returns true if the value is null.
92
- */
93
- bool isNull() const { return _type == PropType::Null; }
94
-
95
- /**
96
- Returns the bool value. Requires that the underlying type is bool
97
- */
98
- bool getAsBool() const;
99
-
100
- /**
101
- Returns the numeric value. Requires that the underlying type is number
102
- */
103
- double getAsNumber() const;
104
-
105
- /**
106
- Returns the string value. Requires that the underlying type is string
107
- */
108
- const std::string &getAsString() const;
109
-
110
- /**
111
- Returns the array value. Requires that the underlying type is array
112
- */
113
- const std::vector<JsiValue> &getAsArray() const;
114
-
115
- /**
116
- Returns an inner value by name. Requires that the underlying type is Object
117
- */
118
- const JsiValue &getValue(PropId name) const;
119
-
120
- /**
121
- Returns true if the underlying type is Object and the property name exists.
122
- */
123
- bool hasValue(PropId name) const;
124
-
125
- /**
126
- Returns the names of the properties stored in this value
127
- */
128
- std::vector<PropId> getKeys() const;
129
-
130
- /**
131
- Returns the host object value. Requires that the underlying type is Host
132
- Object
133
- */
134
- std::shared_ptr<jsi::HostObject> getAsHostObject() const;
135
-
136
- /**
137
- Returns a dynamic cast of the host object value. Requires that the underlying
138
- type is Host Object
139
- */
140
- template <typename T> std::shared_ptr<T> getAs() const {
141
- if (_type != PropType::HostObject) {
142
- throw std::runtime_error("Expected type host object, got " +
143
- getTypeAsString(_type));
144
- }
145
- return std::dynamic_pointer_cast<T>(_hostObject);
146
- }
147
-
148
- /**
149
- Returns the host function. Requires that the type is HostFunction
150
- */
151
- jsi::HostFunctionType getAsHostFunction() const;
152
-
153
- /**
154
- Returns a callable HostFunction representing the undderlying js function.
155
- Requires that the type is Function
156
- */
157
- const jsi::HostFunctionType getAsFunction() const;
158
-
159
- /**
160
- Returns a string representation of the value
161
- */
162
- std::string asString() const;
163
-
164
- /**
165
- Converts the underlying value back to a JS value
166
- */
167
- jsi::Value getAsJsiValue(jsi::Runtime &runtime) const;
168
-
169
- /**
170
- Returns a string representing the type.
171
- */
172
- static std::string getTypeAsString(PropType type);
173
-
174
- /**
175
- Implements the equals operator
176
- */
177
- bool operator==(const JsiValue &other) const;
178
-
179
- /**
180
- Implements the inequals operator
181
- */
182
- bool operator!=(const JsiValue &other) const;
183
-
184
- protected:
185
- const std::unordered_map<PropId, JsiValue> &getProps() const {
186
- return _props;
187
- }
188
-
189
- bool boolValue() const { return _boolValue; }
190
- double numberValue() const { return _numberValue; }
191
- std::string stringValue() const { return _stringValue; }
192
- std::shared_ptr<jsi::HostObject> hostObject() const { return _hostObject; }
193
- jsi::HostFunctionType hostFunction() const { return _hostFunction; }
194
- std::vector<JsiValue> array() const { return _array; }
195
- std::unordered_map<PropId, JsiValue> props() const { return _props; }
196
- const std::vector<PropId> &keysCache() const { return _keysCache; }
197
-
198
- private:
199
- void setObject(jsi::Runtime &runtime, const jsi::Value &value);
200
- jsi::Object getObject(jsi::Runtime &runtime) const;
201
-
202
- void setFunction(jsi::Runtime &runtime, const jsi::Value &value);
203
- jsi::Object getHostFunction(jsi::Runtime &runtime) const;
204
-
205
- void setArray(jsi::Runtime &runtime, const jsi::Object &obj);
206
- jsi::Array getArray(jsi::Runtime &runtime) const;
207
-
208
- void setHostObject(jsi::Runtime &runtime, const jsi::Object &obj);
209
- jsi::Object getHostObject(jsi::Runtime &runtime) const;
210
-
211
- PropType _type = PropType::Undefined;
212
- bool _boolValue;
213
- double _numberValue;
214
- std::string _stringValue = "";
215
- std::shared_ptr<jsi::HostObject> _hostObject;
216
- jsi::HostFunctionType _hostFunction;
217
- std::vector<JsiValue> _array;
218
- std::unordered_map<PropId, JsiValue> _props;
219
- std::vector<PropId> _keysCache;
220
- };
221
-
222
- } // namespace RNJsi
@@ -1,10 +0,0 @@
1
- import type { ViewProps } from "react-native";
2
- import type { SharedValue } from "react-native-reanimated";
3
- import type { SkSize } from "../skia/types";
4
- export interface CanvasProps extends ViewProps {
5
- debug?: boolean;
6
- opaque?: boolean;
7
- onSize?: SharedValue<SkSize>;
8
- mode?: "continuous" | "default";
9
- }
10
- export declare const Canvas: import("react").ForwardRefExoticComponent<CanvasProps & import("react").RefAttributes<unknown>>;