@shopify/react-native-skia 1.11.5 → 1.11.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. package/android/CMakeLists.txt +0 -1
  2. package/cpp/api/JsiSkParagraph.h +1 -3
  3. package/cpp/api/recorder/Convertor.h +19 -17
  4. package/cpp/api/recorder/Drawings.h +7 -10
  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/Canvas.d.ts +3 -2
  12. package/lib/commonjs/renderer/Canvas.js +4 -2
  13. package/lib/commonjs/renderer/Canvas.js.map +1 -1
  14. package/lib/commonjs/renderer/__tests__/setup.d.ts +1 -1
  15. package/lib/commonjs/skia/types/Image/ColorType.js +20 -2
  16. package/lib/commonjs/skia/types/Image/ColorType.js.map +1 -1
  17. package/lib/commonjs/skia/types/Image/ColorType.web.js +18 -2
  18. package/lib/commonjs/skia/types/Image/ColorType.web.js.map +1 -1
  19. package/lib/commonjs/skia/types/ImageFilter/ImageFilter.js +14 -0
  20. package/lib/commonjs/skia/types/ImageFilter/ImageFilter.js.map +1 -1
  21. package/lib/commonjs/skia/types/MaskFilter.js +5 -2
  22. package/lib/commonjs/skia/types/MaskFilter.js.map +1 -1
  23. package/lib/commonjs/skia/types/Paint/BlendMode.js +31 -2
  24. package/lib/commonjs/skia/types/Paint/BlendMode.js.map +1 -1
  25. package/lib/commonjs/skia/types/Paragraph/ParagraphBuilder.js +14 -0
  26. package/lib/commonjs/skia/types/Paragraph/ParagraphBuilder.js.map +1 -1
  27. package/lib/commonjs/skia/types/Path/Path.js +4 -0
  28. package/lib/commonjs/skia/types/Path/Path.js.map +1 -1
  29. package/lib/commonjs/skia/web/JsiSkCanvas.d.ts +1 -1
  30. package/lib/commonjs/skia/web/JsiSkImage.d.ts +2 -2
  31. package/lib/commonjs/skia/web/JsiSkPaint.d.ts +1 -1
  32. package/lib/commonjs/skia/web/JsiSkPicture.d.ts +1 -1
  33. package/lib/commonjs/skia/web/JsiSkPoint.d.ts +1 -1
  34. package/lib/commonjs/skia/web/JsiSkRRect.d.ts +1 -1
  35. package/lib/commonjs/skia/web/JsiSkRSXform.d.ts +1 -1
  36. package/lib/commonjs/skia/web/JsiSkRect.d.ts +1 -1
  37. package/lib/commonjs/sksg/Container.d.ts +1 -4
  38. package/lib/commonjs/sksg/Container.js +8 -9
  39. package/lib/commonjs/sksg/Container.js.map +1 -1
  40. package/lib/commonjs/sksg/Recorder/Core.js +2 -0
  41. package/lib/commonjs/sksg/Recorder/Core.js.map +1 -1
  42. package/lib/commonjs/specs/NativeSkiaModule.web.d.ts +6 -0
  43. package/lib/commonjs/specs/NativeSkiaModule.web.js +33 -0
  44. package/lib/commonjs/specs/NativeSkiaModule.web.js.map +1 -1
  45. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.web.d.ts +9 -0
  46. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.web.js +32 -0
  47. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.web.js.map +1 -0
  48. package/lib/commonjs/views/types.d.ts +1 -0
  49. package/lib/commonjs/views/types.js.map +1 -1
  50. package/lib/commonjs/web/LoadSkiaWeb.js +1 -2
  51. package/lib/commonjs/web/LoadSkiaWeb.js.map +1 -1
  52. package/lib/module/animation/functions/interpolateColors.d.ts +1 -1
  53. package/lib/module/dom/nodes/datatypes/Gradient.d.ts +2 -2
  54. package/lib/module/dom/types/NodeType.js +8 -0
  55. package/lib/module/dom/types/NodeType.js.map +1 -1
  56. package/lib/module/external/reanimated/buffers.d.ts +1 -1
  57. package/lib/module/renderer/Canvas.d.ts +3 -2
  58. package/lib/module/renderer/Canvas.js +1 -1
  59. package/lib/module/renderer/Canvas.js.map +1 -1
  60. package/lib/module/renderer/__tests__/setup.d.ts +1 -1
  61. package/lib/module/skia/types/Image/ColorType.js +20 -2
  62. package/lib/module/skia/types/Image/ColorType.js.map +1 -1
  63. package/lib/module/skia/types/Image/ColorType.web.js +18 -2
  64. package/lib/module/skia/types/Image/ColorType.web.js.map +1 -1
  65. package/lib/module/skia/types/ImageFilter/ImageFilter.js +14 -0
  66. package/lib/module/skia/types/ImageFilter/ImageFilter.js.map +1 -1
  67. package/lib/module/skia/types/MaskFilter.js +5 -2
  68. package/lib/module/skia/types/MaskFilter.js.map +1 -1
  69. package/lib/module/skia/types/Paint/BlendMode.js +31 -2
  70. package/lib/module/skia/types/Paint/BlendMode.js.map +1 -1
  71. package/lib/module/skia/types/Paragraph/ParagraphBuilder.js +14 -0
  72. package/lib/module/skia/types/Paragraph/ParagraphBuilder.js.map +1 -1
  73. package/lib/module/skia/types/Path/Path.js +4 -0
  74. package/lib/module/skia/types/Path/Path.js.map +1 -1
  75. package/lib/module/skia/web/JsiSkCanvas.d.ts +1 -1
  76. package/lib/module/skia/web/JsiSkImage.d.ts +2 -2
  77. package/lib/module/skia/web/JsiSkPaint.d.ts +1 -1
  78. package/lib/module/skia/web/JsiSkPicture.d.ts +1 -1
  79. package/lib/module/skia/web/JsiSkPoint.d.ts +1 -1
  80. package/lib/module/skia/web/JsiSkRRect.d.ts +1 -1
  81. package/lib/module/skia/web/JsiSkRSXform.d.ts +1 -1
  82. package/lib/module/skia/web/JsiSkRect.d.ts +1 -1
  83. package/lib/module/sksg/Container.d.ts +1 -4
  84. package/lib/module/sksg/Container.js +8 -9
  85. package/lib/module/sksg/Container.js.map +1 -1
  86. package/lib/module/sksg/Recorder/Core.js +2 -0
  87. package/lib/module/sksg/Recorder/Core.js.map +1 -1
  88. package/lib/module/specs/NativeSkiaModule.web.d.ts +6 -0
  89. package/lib/module/specs/NativeSkiaModule.web.js +30 -1
  90. package/lib/module/specs/NativeSkiaModule.web.js.map +1 -1
  91. package/lib/module/specs/SkiaPictureViewNativeComponent.web.d.ts +9 -0
  92. package/lib/module/specs/SkiaPictureViewNativeComponent.web.js +26 -0
  93. package/lib/module/specs/SkiaPictureViewNativeComponent.web.js.map +1 -0
  94. package/lib/module/views/types.d.ts +1 -0
  95. package/lib/module/views/types.js.map +1 -1
  96. package/lib/typescript/lib/commonjs/animation/functions/interpolateColors.d.ts +1 -1
  97. package/lib/typescript/lib/commonjs/renderer/Canvas.d.ts +2 -3
  98. package/lib/typescript/lib/commonjs/renderer/components/image/ImageShader.d.ts +1 -1
  99. package/lib/typescript/lib/commonjs/skia/web/JsiSkColor.d.ts +1 -1
  100. package/lib/typescript/lib/commonjs/skia/web/JsiSkia.d.ts +1 -1
  101. package/lib/typescript/lib/commonjs/specs/NativeSkiaModule.web.d.ts +1 -0
  102. package/lib/typescript/lib/commonjs/specs/SkiaPictureViewNativeComponent.web.d.ts +10 -0
  103. package/lib/typescript/lib/module/animation/functions/interpolateColors.d.ts +1 -1
  104. package/lib/typescript/lib/module/mock/index.d.ts +2 -2
  105. package/lib/typescript/lib/module/renderer/Canvas.d.ts +3 -2
  106. package/lib/typescript/lib/module/renderer/components/image/ImageShader.d.ts +1 -1
  107. package/lib/typescript/lib/module/skia/Skia.web.d.ts +1 -1
  108. package/lib/typescript/lib/module/skia/web/JsiSkColor.d.ts +1 -1
  109. package/lib/typescript/lib/module/skia/web/JsiSkia.d.ts +1 -1
  110. package/lib/typescript/lib/module/specs/NativeSkiaModule.web.d.ts +1 -0
  111. package/lib/typescript/lib/module/specs/SkiaPictureViewNativeComponent.web.d.ts +9 -0
  112. package/lib/typescript/src/animation/functions/interpolateColors.d.ts +1 -1
  113. package/lib/typescript/src/dom/nodes/datatypes/Gradient.d.ts +2 -2
  114. package/lib/typescript/src/external/reanimated/buffers.d.ts +1 -1
  115. package/lib/typescript/src/renderer/Canvas.d.ts +3 -2
  116. package/lib/typescript/src/renderer/__tests__/setup.d.ts +1 -1
  117. package/lib/typescript/src/skia/web/JsiSkCanvas.d.ts +1 -1
  118. package/lib/typescript/src/skia/web/JsiSkImage.d.ts +2 -2
  119. package/lib/typescript/src/skia/web/JsiSkPaint.d.ts +1 -1
  120. package/lib/typescript/src/skia/web/JsiSkPicture.d.ts +1 -1
  121. package/lib/typescript/src/skia/web/JsiSkPoint.d.ts +1 -1
  122. package/lib/typescript/src/skia/web/JsiSkRRect.d.ts +1 -1
  123. package/lib/typescript/src/skia/web/JsiSkRSXform.d.ts +1 -1
  124. package/lib/typescript/src/skia/web/JsiSkRect.d.ts +1 -1
  125. package/lib/typescript/src/sksg/Container.d.ts +1 -4
  126. package/lib/typescript/src/specs/NativeSkiaModule.web.d.ts +6 -0
  127. package/lib/typescript/src/specs/SkiaPictureViewNativeComponent.web.d.ts +9 -0
  128. package/lib/typescript/src/views/types.d.ts +1 -0
  129. package/package.json +1 -1
  130. package/src/renderer/Canvas.tsx +1 -1
  131. package/src/sksg/Container.ts +7 -12
  132. package/src/specs/NativeSkiaModule.web.ts +38 -0
  133. package/src/specs/SkiaPictureViewNativeComponent.web.ts +39 -0
  134. package/src/views/types.ts +1 -0
  135. package/cpp/jsi/JsiValue.cpp +0 -345
  136. package/cpp/jsi/JsiValue.h +0 -222
  137. package/lib/commonjs/renderer/Canvas.web.d.ts +0 -11
  138. package/lib/commonjs/renderer/Canvas.web.js +0 -112
  139. package/lib/commonjs/renderer/Canvas.web.js.map +0 -1
  140. package/lib/module/renderer/Canvas.web.d.ts +0 -11
  141. package/lib/module/renderer/Canvas.web.js +0 -105
  142. package/lib/module/renderer/Canvas.web.js.map +0 -1
  143. package/lib/typescript/lib/commonjs/renderer/Canvas.web.d.ts +0 -2
  144. package/lib/typescript/lib/module/renderer/Canvas.web.d.ts +0 -2
  145. package/lib/typescript/src/renderer/Canvas.web.d.ts +0 -11
  146. package/src/renderer/Canvas.web.tsx +0 -134
@@ -4,7 +4,7 @@ import { BaseHostObject } from "./Host";
4
4
  import { JsiSkRect } from "./JsiSkRect";
5
5
  export declare class JsiSkRRect extends BaseHostObject<RRect, "RRect"> implements SkRRect {
6
6
  dispose: () => void;
7
- static fromValue(CanvasKit: CanvasKit, rect: InputRRect): Float32Array;
7
+ static fromValue(CanvasKit: CanvasKit, rect: InputRRect): Float32Array<ArrayBuffer>;
8
8
  constructor(CanvasKit: CanvasKit, rect: SkRect, rx: number, ry: number);
9
9
  get rx(): number;
10
10
  get ry(): number;
@@ -3,7 +3,7 @@ import type { SkRSXform } from "../types";
3
3
  import { BaseHostObject } from "./Host";
4
4
  export type RSXform = Float32Array;
5
5
  export declare class JsiSkRSXform extends BaseHostObject<RSXform, "RSXform"> implements SkRSXform {
6
- static fromValue(rsxform: SkRSXform): Float32Array;
6
+ static fromValue(rsxform: SkRSXform): Float32Array<ArrayBuffer>;
7
7
  constructor(CanvasKit: CanvasKit, ref: RSXform);
8
8
  set(scos: number, ssin: number, tx: number, ty: number): void;
9
9
  get scos(): number;
@@ -2,7 +2,7 @@ import type { CanvasKit, Rect } from "canvaskit-wasm";
2
2
  import type { SkHostRect, SkRect } from "../types";
3
3
  import { BaseHostObject } from "./Host";
4
4
  export declare class JsiSkRect extends BaseHostObject<Rect, "Rect"> implements SkHostRect {
5
- static fromValue(CanvasKit: CanvasKit, rect: SkRect): Float32Array;
5
+ static fromValue(CanvasKit: CanvasKit, rect: SkRect): Rect;
6
6
  dispose: () => void;
7
7
  constructor(CanvasKit: CanvasKit, ref: Rect);
8
8
  setXYWH(x: number, y: number, width: number, height: number): void;
@@ -1,10 +1,7 @@
1
1
  import type { Skia, SkCanvas } from "../skia/types";
2
- import type { ISkiaViewApi } from "../views/types";
3
2
  import type { Node } from "./Node";
4
3
  import type { Recording } from "./Recorder/Recorder";
5
- declare global {
6
- var SkiaViewApi: ISkiaViewApi;
7
- }
4
+ import "../views/api";
8
5
  export declare abstract class Container {
9
6
  protected Skia: Skia;
10
7
  protected nativeId: number;
@@ -0,0 +1,6 @@
1
+ import type { ISkiaViewApi } from "../views/types";
2
+ import type { SkiaPictureView } from "../views/SkiaPictureView.web";
3
+ export type ISkiaViewApiWeb = ISkiaViewApi & {
4
+ views: Record<string, SkiaPictureView>;
5
+ registerView(nativeId: string, view: SkiaPictureView): void;
6
+ };
@@ -0,0 +1,9 @@
1
+ import type { ViewProps } from "react-native";
2
+ import { SkiaPictureView } from "../views/SkiaPictureView.web";
3
+ export interface NativeProps extends ViewProps {
4
+ debug?: boolean;
5
+ opaque?: boolean;
6
+ nativeID: string;
7
+ }
8
+ declare const SkiaPictureViewNativeComponent: ({ nativeID, debug, opaque, onLayout, ...viewProps }: NativeProps) => import("react").CElement<import("..").SkiaPictureViewNativeProps, SkiaPictureView>;
9
+ export default SkiaPictureViewNativeComponent;
@@ -7,6 +7,7 @@ export type NativeSkiaViewProps = ViewProps & {
7
7
  opaque?: boolean;
8
8
  };
9
9
  export interface ISkiaViewApi {
10
+ web?: boolean;
10
11
  setJsiProperty: <T>(nativeId: number, name: string, value: T) => void;
11
12
  requestRedraw: (nativeId: number) => void;
12
13
  makeImageSnapshot: (nativeId: number, rect?: SkRect) => SkImage;
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "setup-skia-web": "./scripts/setup-canvaskit.js"
9
9
  },
10
10
  "title": "React Native Skia",
11
- "version": "1.11.5",
11
+ "version": "1.11.7",
12
12
  "description": "High-performance React Native Graphics using Skia",
13
13
  "main": "lib/module/index.js",
14
14
  "react-native": "src/index.ts",
@@ -1,5 +1,5 @@
1
1
  import type { FC } from "react";
2
- import {
2
+ import React, {
3
3
  forwardRef,
4
4
  useCallback,
5
5
  useEffect,
@@ -2,7 +2,6 @@ import Rea from "../external/reanimated/ReanimatedProxy";
2
2
  import type { Skia, SkCanvas } from "../skia/types";
3
3
  import { HAS_REANIMATED_3 } from "../external/reanimated/renderHelpers";
4
4
  import type { JsiRecorder } from "../skia/types/Recorder";
5
- import type { ISkiaViewApi } from "../views/types";
6
5
 
7
6
  import type { Node } from "./Node";
8
7
  import type { Recording } from "./Recorder/Recorder";
@@ -12,9 +11,7 @@ import { replay } from "./Recorder/Player";
12
11
  import { createDrawingContext } from "./Recorder/DrawingContext";
13
12
  import { ReanimatedRecorder } from "./Recorder/ReanimatedRecorder";
14
13
 
15
- declare global {
16
- var SkiaViewApi: ISkiaViewApi;
17
- }
14
+ import "../views/api";
18
15
 
19
16
  const drawOnscreen = (Skia: Skia, nativeId: number, recording: Recording) => {
20
17
  "worklet";
@@ -29,8 +26,6 @@ const drawOnscreen = (Skia: Skia, nativeId: number, recording: Recording) => {
29
26
  //const end = performance.now();
30
27
  //console.log("Recording time: ", end - start);
31
28
  SkiaViewApi.setJsiProperty(nativeId, "picture", picture);
32
- rec.dispose();
33
- picture.dispose();
34
29
  };
35
30
 
36
31
  const nativeDrawOnscreen = (nativeId: number, recorder: JsiRecorder) => {
@@ -145,6 +140,10 @@ class NativeReanimatedContainer extends Container {
145
140
  visit(recorder, this.root);
146
141
  const sharedValues = recorder.getSharedValues();
147
142
  const sharedRecorder = recorder.getRecorder();
143
+ Rea.runOnUI(() => {
144
+ "worklet";
145
+ nativeDrawOnscreen(nativeId, sharedRecorder);
146
+ })();
148
147
  if (sharedValues.length > 0) {
149
148
  this.mapperId = Rea.startMapper(() => {
150
149
  "worklet";
@@ -152,17 +151,13 @@ class NativeReanimatedContainer extends Container {
152
151
  nativeDrawOnscreen(nativeId, sharedRecorder);
153
152
  }, sharedValues);
154
153
  }
155
- Rea.runOnUI(() => {
156
- "worklet";
157
- nativeDrawOnscreen(nativeId, sharedRecorder);
158
- })();
159
154
  }
160
155
  }
161
156
 
162
157
  export const createContainer = (Skia: Skia, nativeId: number) => {
163
- const native = global.SkiaViewApi !== undefined;
158
+ const web = global.SkiaViewApi && global.SkiaViewApi.web;
164
159
  if (HAS_REANIMATED_3 && nativeId !== -1) {
165
- if (native) {
160
+ if (!web) {
166
161
  return new NativeReanimatedContainer(Skia, nativeId);
167
162
  } else {
168
163
  return new ReanimatedContainer(Skia, nativeId);
@@ -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