@delali/narsil-react-native 0.0.0-stage → 0.3.1-next.151

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 (186) hide show
  1. package/LICENSE +201 -0
  2. package/NarsilReactNative.podspec +57 -0
  3. package/README.md +46 -2
  4. package/android/CMakeLists.txt +48 -0
  5. package/android/build.gradle +151 -0
  6. package/android/gradle.properties +5 -0
  7. package/android/src/main/AndroidManifest.xml +2 -0
  8. package/android/src/main/cpp/AndroidEnvironment.cpp +44 -0
  9. package/android/src/main/cpp/AndroidEnvironment.hpp +11 -0
  10. package/android/src/main/cpp/cpp-adapter.cpp +11 -0
  11. package/android/src/main/java/com/margelo/nitro/narsil/NarsilAndroidEnvironment.kt +29 -0
  12. package/android/src/main/java/com/margelo/nitro/narsil/NarsilReactNativePackage.kt +22 -0
  13. package/app.plugin.js +25 -0
  14. package/cpp/AttachedField.cpp +234 -0
  15. package/cpp/AttachedField.hpp +61 -0
  16. package/cpp/Compression.cpp +113 -0
  17. package/cpp/Compression.hpp +16 -0
  18. package/cpp/GraphHeldAlone.cpp +29 -0
  19. package/cpp/GraphHeldAlone.hpp +25 -0
  20. package/cpp/HostMemory.cpp +123 -0
  21. package/cpp/HostMemory.hpp +53 -0
  22. package/cpp/HybridNarsilCoreImpl.cpp +390 -0
  23. package/cpp/HybridNarsilCoreImpl.hpp +68 -0
  24. package/cpp/HybridNarsilFilesImpl.cpp +245 -0
  25. package/cpp/HybridNarsilFilesImpl.hpp +34 -0
  26. package/cpp/HybridNarsilHostImpl.cpp +126 -0
  27. package/cpp/HybridNarsilHostImpl.hpp +42 -0
  28. package/cpp/NativeAtomics.cpp +110 -0
  29. package/cpp/NativeAtomics.hpp +11 -0
  30. package/cpp/ProcessInfo.cpp +83 -0
  31. package/cpp/ProcessInfo.hpp +11 -0
  32. package/cpp/ThreadPriority.cpp +77 -0
  33. package/cpp/ThreadPriority.hpp +8 -0
  34. package/cpp/WorkletRuntimeDispatcher.cpp +61 -0
  35. package/cpp/WorkletRuntimeDispatcher.hpp +9 -0
  36. package/cpp/core/include/narsil_core.h +208 -0
  37. package/cpp/core/src/connect.c +213 -0
  38. package/cpp/core/src/distance.c +103 -0
  39. package/cpp/core/src/files.c +99 -0
  40. package/cpp/core/src/heap.c +84 -0
  41. package/cpp/core/src/internal.h +284 -0
  42. package/cpp/core/src/kernels_code.c +209 -0
  43. package/cpp/core/src/kernels_float.c +197 -0
  44. package/cpp/core/src/kernels_pair.c +133 -0
  45. package/cpp/core/src/last_links.c +145 -0
  46. package/cpp/core/src/links.c +195 -0
  47. package/cpp/core/src/locks.c +168 -0
  48. package/cpp/core/src/nearest_links.c +147 -0
  49. package/cpp/core/src/place.c +215 -0
  50. package/cpp/core/src/quantize.c +226 -0
  51. package/cpp/core/src/records.c +97 -0
  52. package/cpp/core/src/remove.c +205 -0
  53. package/cpp/core/src/vectors.c +139 -0
  54. package/cpp/core/src/walk.c +231 -0
  55. package/cpp/core/src/workspace.c +167 -0
  56. package/ios/PrivacyInfo.xcprivacy +23 -0
  57. package/lib/client.d.ts +19 -0
  58. package/lib/client.js +41 -0
  59. package/lib/constants.d.ts +9 -0
  60. package/lib/constants.js +9 -0
  61. package/lib/engine/byte-files.d.ts +5 -0
  62. package/lib/engine/byte-files.js +97 -0
  63. package/lib/engine/core.d.ts +3 -0
  64. package/lib/engine/core.js +113 -0
  65. package/lib/engine/files.d.ts +3 -0
  66. package/lib/engine/files.js +188 -0
  67. package/lib/engine/host.d.ts +13 -0
  68. package/lib/engine/host.js +102 -0
  69. package/lib/engine/install.d.ts +4 -0
  70. package/lib/engine/install.js +29 -0
  71. package/lib/engine/native-error.d.ts +1 -0
  72. package/lib/engine/native-error.js +12 -0
  73. package/lib/engine/stand-ins/atomics.d.ts +5 -0
  74. package/lib/engine/stand-ins/atomics.js +236 -0
  75. package/lib/engine/stand-ins/crypto.d.ts +2 -0
  76. package/lib/engine/stand-ins/crypto.js +65 -0
  77. package/lib/engine/stand-ins/process.d.ts +2 -0
  78. package/lib/engine/stand-ins/process.js +22 -0
  79. package/lib/engine/stand-ins/streams.d.ts +1 -0
  80. package/lib/engine/stand-ins/streams.js +31 -0
  81. package/lib/engine/stand-ins/structured-clone.d.ts +1 -0
  82. package/lib/engine/stand-ins/structured-clone.js +121 -0
  83. package/lib/engine/streamed.d.ts +6 -0
  84. package/lib/engine/streamed.js +29 -0
  85. package/lib/engine/transfer.d.ts +3 -0
  86. package/lib/engine/transfer.js +77 -0
  87. package/lib/errors.d.ts +25 -0
  88. package/lib/errors.js +59 -0
  89. package/lib/index.d.ts +3 -0
  90. package/lib/index.js +2 -0
  91. package/lib/methods.d.ts +5 -0
  92. package/lib/methods.js +47 -0
  93. package/lib/native-objects.d.ts +2 -0
  94. package/lib/native-objects.js +24 -0
  95. package/lib/specs/core.nitro.d.ts +79 -0
  96. package/lib/specs/core.nitro.js +1 -0
  97. package/lib/specs/files.nitro.d.ts +36 -0
  98. package/lib/specs/files.nitro.js +1 -0
  99. package/lib/specs/host.nitro.d.ts +24 -0
  100. package/lib/specs/host.nitro.js +1 -0
  101. package/lib/transferred.d.ts +15 -0
  102. package/lib/transferred.js +6 -0
  103. package/lib/types.d.ts +136 -0
  104. package/lib/types.js +1 -0
  105. package/lib/worklets/app-turns.d.ts +1 -0
  106. package/lib/worklets/app-turns.js +44 -0
  107. package/lib/worklets/client.d.ts +30 -0
  108. package/lib/worklets/client.js +144 -0
  109. package/lib/worklets/index.d.ts +4 -0
  110. package/lib/worklets/index.js +2 -0
  111. package/lib/worklets/instances.d.ts +15 -0
  112. package/lib/worklets/instances.js +194 -0
  113. package/lib/worklets/json-part.d.ts +1 -0
  114. package/lib/worklets/json-part.js +42 -0
  115. package/lib/worklets/runtime.d.ts +25 -0
  116. package/lib/worklets/runtime.js +80 -0
  117. package/lib/worklets/transfer-queue.d.ts +7 -0
  118. package/lib/worklets/transfer-queue.js +79 -0
  119. package/lib/worklets/types.d.ts +26 -0
  120. package/lib/worklets/types.js +1 -0
  121. package/nitro.json +32 -0
  122. package/nitrogen/generated/.gitattributes +1 -0
  123. package/nitrogen/generated/android/NarsilReactNative+autolinking.cmake +85 -0
  124. package/nitrogen/generated/android/NarsilReactNative+autolinking.gradle +27 -0
  125. package/nitrogen/generated/android/NarsilReactNativeOnLoad.cpp +69 -0
  126. package/nitrogen/generated/android/NarsilReactNativeOnLoad.hpp +34 -0
  127. package/nitrogen/generated/android/kotlin/com/margelo/nitro/narsil/NarsilReactNativeOnLoad.kt +35 -0
  128. package/nitrogen/generated/ios/NarsilReactNative+autolinking.rb +62 -0
  129. package/nitrogen/generated/ios/NarsilReactNative-Swift-Cxx-Bridge.cpp +17 -0
  130. package/nitrogen/generated/ios/NarsilReactNative-Swift-Cxx-Bridge.hpp +27 -0
  131. package/nitrogen/generated/ios/NarsilReactNative-Swift-Cxx-Umbrella.hpp +38 -0
  132. package/nitrogen/generated/ios/NarsilReactNativeAutolinking.mm +55 -0
  133. package/nitrogen/generated/ios/NarsilReactNativeAutolinking.swift +16 -0
  134. package/nitrogen/generated/shared/c++/DirectoryEntry.hpp +91 -0
  135. package/nitrogen/generated/shared/c++/FileStatus.hpp +103 -0
  136. package/nitrogen/generated/shared/c++/GraphMemory.hpp +120 -0
  137. package/nitrogen/generated/shared/c++/HybridNarsilAttachedGraphSpec.cpp +21 -0
  138. package/nitrogen/generated/shared/c++/HybridNarsilAttachedGraphSpec.hpp +62 -0
  139. package/nitrogen/generated/shared/c++/HybridNarsilAttachedStoreSpec.cpp +21 -0
  140. package/nitrogen/generated/shared/c++/HybridNarsilAttachedStoreSpec.hpp +62 -0
  141. package/nitrogen/generated/shared/c++/HybridNarsilCoreSpec.cpp +35 -0
  142. package/nitrogen/generated/shared/c++/HybridNarsilCoreSpec.hpp +100 -0
  143. package/nitrogen/generated/shared/c++/HybridNarsilFilesSpec.cpp +37 -0
  144. package/nitrogen/generated/shared/c++/HybridNarsilFilesSpec.hpp +86 -0
  145. package/nitrogen/generated/shared/c++/HybridNarsilHostSpec.cpp +34 -0
  146. package/nitrogen/generated/shared/c++/HybridNarsilHostSpec.hpp +78 -0
  147. package/nitrogen/generated/shared/c++/MemoryView.hpp +91 -0
  148. package/nitrogen/generated/shared/c++/NativeConnectOutcome.hpp +87 -0
  149. package/nitrogen/generated/shared/c++/NativeScores.hpp +87 -0
  150. package/nitrogen/generated/shared/c++/NativeSearchHits.hpp +91 -0
  151. package/nitrogen/generated/shared/c++/ProcessMemory.hpp +87 -0
  152. package/nitrogen/generated/shared/c++/StoreMemory.hpp +142 -0
  153. package/package.json +87 -3
  154. package/react-native.config.js +8 -0
  155. package/src/client.ts +44 -0
  156. package/src/constants.ts +9 -0
  157. package/src/engine/byte-files.ts +99 -0
  158. package/src/engine/core.ts +162 -0
  159. package/src/engine/files.ts +186 -0
  160. package/src/engine/host.ts +111 -0
  161. package/src/engine/install.ts +31 -0
  162. package/src/engine/native-error.ts +10 -0
  163. package/src/engine/stand-ins/atomics.ts +288 -0
  164. package/src/engine/stand-ins/crypto.ts +81 -0
  165. package/src/engine/stand-ins/process.ts +30 -0
  166. package/src/engine/stand-ins/streams.ts +45 -0
  167. package/src/engine/stand-ins/structured-clone.ts +118 -0
  168. package/src/engine/streamed.ts +40 -0
  169. package/src/engine/transfer.ts +72 -0
  170. package/src/errors.ts +73 -0
  171. package/src/index.ts +3 -0
  172. package/src/methods.ts +52 -0
  173. package/src/native-objects.ts +30 -0
  174. package/src/specs/core.nitro.ts +140 -0
  175. package/src/specs/files.nitro.ts +42 -0
  176. package/src/specs/host.nitro.ts +30 -0
  177. package/src/transferred.ts +18 -0
  178. package/src/types.ts +164 -0
  179. package/src/worklets/app-turns.ts +48 -0
  180. package/src/worklets/client.ts +165 -0
  181. package/src/worklets/index.ts +4 -0
  182. package/src/worklets/instances.ts +231 -0
  183. package/src/worklets/json-part.ts +38 -0
  184. package/src/worklets/runtime.ts +190 -0
  185. package/src/worklets/transfer-queue.ts +98 -0
  186. package/src/worklets/types.ts +37 -0
@@ -0,0 +1,113 @@
1
+ import { withNativeCode } from './native-error';
2
+ const ABSENT_REGION = { buffer: new ArrayBuffer(0), byteOffset: 0, byteLength: 0 };
3
+ function viewOf(array) {
4
+ return { buffer: array.buffer, byteOffset: array.byteOffset, byteLength: array.byteLength };
5
+ }
6
+ function graphMemoryOf(memory) {
7
+ return {
8
+ graphHeader: viewOf(memory.graphHeader),
9
+ nodeLevels: viewOf(memory.nodeLevels),
10
+ level0: viewOf(memory.level0),
11
+ upperBase: viewOf(memory.upperBase),
12
+ upper: viewOf(memory.upper),
13
+ locks: viewOf(memory.locks),
14
+ tombstones: viewOf(memory.tombstones),
15
+ incoming: viewOf(memory.incoming),
16
+ nearestLinkDue: viewOf(memory.nearestLinkDue),
17
+ heldLocks: viewOf(memory.heldLocks),
18
+ };
19
+ }
20
+ function storeMemoryOf(memory) {
21
+ return {
22
+ storeHeader: viewOf(memory.storeHeader),
23
+ dimension: memory.dimension,
24
+ bits: memory.bits,
25
+ recordsPerBlock: memory.recordsPerBlock,
26
+ vectorsPerBlock: memory.vectorsPerBlock,
27
+ vectorStrideBytes: memory.vectorStrideBytes,
28
+ codePresent: viewOf(memory.codePresent),
29
+ centroid: viewOf(memory.centroid),
30
+ magnitudes: viewOf(memory.magnitudes),
31
+ present: viewOf(memory.present),
32
+ vectorFile: viewOf(memory.vectorFile),
33
+ vectorOffset: viewOf(memory.vectorOffset),
34
+ vectorFiles: memory.vectorFiles,
35
+ codeBlocks: memory.codeBlocks.map(viewOf),
36
+ vectorBlocks: memory.vectorBlocks.map(block => (block === null ? ABSENT_REGION : viewOf(block))),
37
+ };
38
+ }
39
+ function graphOf(handle) {
40
+ return handle;
41
+ }
42
+ function storeOf(handle) {
43
+ return handle;
44
+ }
45
+ function backgroundCalls(core) {
46
+ return {
47
+ async connectUnreachedAsync(graph, store, metric, nearby, threadSlot) {
48
+ try {
49
+ const outcome = await core.connectUnreachedAsync(graphOf(graph), storeOf(store), metric, viewOf(nearby), threadSlot);
50
+ return { status: outcome.status, wakes: new Int32Array(outcome.wakes) };
51
+ }
52
+ catch (error) {
53
+ throw withNativeCode(error);
54
+ }
55
+ },
56
+ async searchAsync(graph, store, query, metric, candidateCount, threadSlot, accepted) {
57
+ try {
58
+ const hits = await core.searchAsync(graphOf(graph), storeOf(store), viewOf(query), metric, candidateCount, threadSlot, accepted === null ? ABSENT_REGION : viewOf(accepted), accepted !== null);
59
+ return {
60
+ count: hits.count,
61
+ ordinals: new Int32Array(hits.ordinals),
62
+ distances: new Float64Array(hits.distances),
63
+ };
64
+ }
65
+ catch (error) {
66
+ throw withNativeCode(error);
67
+ }
68
+ },
69
+ async scoreAsync(store, query, metric, ordinals, count, threadSlot) {
70
+ try {
71
+ const scores = await core.scoreAsync(storeOf(store), viewOf(query), metric, viewOf(ordinals), count, threadSlot);
72
+ return { status: scores.status, distances: new Float64Array(scores.distances) };
73
+ }
74
+ catch (error) {
75
+ throw withNativeCode(error);
76
+ }
77
+ },
78
+ };
79
+ }
80
+ export function adaptCore(core, nativeThreads) {
81
+ return {
82
+ abiVersion: () => core.abiVersion(),
83
+ workspaceBytes: () => core.workspaceBytes(),
84
+ attachGraph(memory) {
85
+ try {
86
+ return core.attachGraph(graphMemoryOf(memory));
87
+ }
88
+ catch (error) {
89
+ throw withNativeCode(error);
90
+ }
91
+ },
92
+ attachStore(memory) {
93
+ try {
94
+ return core.attachStore(storeMemoryOf(memory));
95
+ }
96
+ catch (error) {
97
+ throw withNativeCode(error);
98
+ }
99
+ },
100
+ detachStore: store => storeOf(store).detach(),
101
+ search(graph, store, query, metric, candidateCount, threadSlot, ordinals, distances, accepted) {
102
+ return core.search(graphOf(graph), storeOf(store), viewOf(query), metric, candidateCount, threadSlot, viewOf(ordinals), viewOf(distances), accepted === null ? ABSENT_REGION : viewOf(accepted), accepted !== null);
103
+ },
104
+ score: (store, query, metric, ordinals, count, distances) => core.score(storeOf(store), viewOf(query), metric, viewOf(ordinals), count, viewOf(distances)),
105
+ ...(nativeThreads ? backgroundCalls(core) : {}),
106
+ place: (graph, store, metric, ordinal, topLayer, graphHeldAlone, threadSlot, wakes) => core.place(graphOf(graph), storeOf(store), metric, ordinal, topLayer, graphHeldAlone, threadSlot, viewOf(wakes)),
107
+ remove: (graph, ordinal, threadSlot, wakes) => core.remove(graphOf(graph), ordinal, threadSlot, viewOf(wakes)),
108
+ compact: (graph, store, metric, threadSlot, wakes) => core.compact(graphOf(graph), storeOf(store), metric, threadSlot, viewOf(wakes)),
109
+ connectUnreached: (graph, store, metric, nearby, threadSlot, wakes) => core.connectUnreached(graphOf(graph), storeOf(store), metric, viewOf(nearby), threadSlot, viewOf(wakes)),
110
+ measure: (store, metric, ordinals, count, totals) => core.measure(storeOf(store), metric, viewOf(ordinals), count, viewOf(totals)),
111
+ quantise: (store, metric, ordinals, count) => core.quantise(storeOf(store), metric, viewOf(ordinals), count),
112
+ };
113
+ }
@@ -0,0 +1,3 @@
1
+ import type { ReactNativeFiles } from '@delali/narsil';
2
+ import type { NarsilFiles } from '../specs/files.nitro';
3
+ export declare function adaptFiles(files: NarsilFiles, nativeThreads: boolean): ReactNativeFiles;
@@ -0,0 +1,188 @@
1
+ import { withNativeCode } from './native-error';
2
+ function rejectedWithNativeCode(error) {
3
+ return Promise.reject(withNativeCode(error));
4
+ }
5
+ function blockingFiles(files) {
6
+ return {
7
+ open(path, flags) {
8
+ try {
9
+ return files.open(path, flags);
10
+ }
11
+ catch (error) {
12
+ throw withNativeCode(error);
13
+ }
14
+ },
15
+ close(descriptor) {
16
+ try {
17
+ files.close(descriptor);
18
+ }
19
+ catch (error) {
20
+ throw withNativeCode(error);
21
+ }
22
+ },
23
+ read(descriptor, target, position) {
24
+ try {
25
+ return files.read(descriptor, target.buffer, target.byteOffset, target.byteLength, position);
26
+ }
27
+ catch (error) {
28
+ throw withNativeCode(error);
29
+ }
30
+ },
31
+ write(descriptor, source, position) {
32
+ try {
33
+ return files.write(descriptor, source.buffer, source.byteOffset, source.byteLength, position);
34
+ }
35
+ catch (error) {
36
+ throw withNativeCode(error);
37
+ }
38
+ },
39
+ sync(descriptor) {
40
+ try {
41
+ files.sync(descriptor);
42
+ }
43
+ catch (error) {
44
+ throw withNativeCode(error);
45
+ }
46
+ },
47
+ truncate(descriptor, byteLength) {
48
+ try {
49
+ files.truncate(descriptor, byteLength);
50
+ }
51
+ catch (error) {
52
+ throw withNativeCode(error);
53
+ }
54
+ },
55
+ descriptorStatus(descriptor) {
56
+ try {
57
+ return files.descriptorStatus(descriptor);
58
+ }
59
+ catch (error) {
60
+ throw withNativeCode(error);
61
+ }
62
+ },
63
+ pathStatus(path) {
64
+ try {
65
+ return files.pathStatus(path);
66
+ }
67
+ catch (error) {
68
+ throw withNativeCode(error);
69
+ }
70
+ },
71
+ makeDirectory(path) {
72
+ try {
73
+ files.makeDirectory(path);
74
+ }
75
+ catch (error) {
76
+ throw withNativeCode(error);
77
+ }
78
+ },
79
+ readDirectory(path) {
80
+ try {
81
+ return files.readDirectory(path);
82
+ }
83
+ catch (error) {
84
+ throw withNativeCode(error);
85
+ }
86
+ },
87
+ rename(from, to) {
88
+ try {
89
+ files.rename(from, to);
90
+ }
91
+ catch (error) {
92
+ throw withNativeCode(error);
93
+ }
94
+ },
95
+ unlink(path) {
96
+ try {
97
+ files.unlink(path);
98
+ }
99
+ catch (error) {
100
+ throw withNativeCode(error);
101
+ }
102
+ },
103
+ removeDirectory(path) {
104
+ try {
105
+ files.removeDirectory(path);
106
+ }
107
+ catch (error) {
108
+ throw withNativeCode(error);
109
+ }
110
+ },
111
+ link(existingPath, newPath) {
112
+ try {
113
+ files.link(existingPath, newPath);
114
+ }
115
+ catch (error) {
116
+ throw withNativeCode(error);
117
+ }
118
+ },
119
+ };
120
+ }
121
+ function onTheCallingThread(blocking) {
122
+ return {
123
+ ...blocking,
124
+ readAsync: async (descriptor, target, position) => blocking.read(descriptor, target, position),
125
+ writeAsync: async (descriptor, source, position) => blocking.write(descriptor, source, position),
126
+ syncAsync: async (descriptor) => blocking.sync(descriptor),
127
+ };
128
+ }
129
+ function onNativeThreads(files, blocking) {
130
+ const pendingByDescriptor = new Map();
131
+ function inOrder(descriptor, run) {
132
+ const previous = pendingByDescriptor.get(descriptor);
133
+ let started;
134
+ if (previous !== undefined) {
135
+ started = previous.then(run, run);
136
+ }
137
+ else {
138
+ try {
139
+ started = run();
140
+ }
141
+ catch (error) {
142
+ started = Promise.reject(error);
143
+ }
144
+ }
145
+ const settled = started.then(() => { }, () => { });
146
+ pendingByDescriptor.set(descriptor, settled);
147
+ settled.then(() => {
148
+ if (pendingByDescriptor.get(descriptor) === settled)
149
+ pendingByDescriptor.delete(descriptor);
150
+ });
151
+ return started.catch(rejectedWithNativeCode);
152
+ }
153
+ return {
154
+ ...blocking,
155
+ close(descriptor) {
156
+ const pending = pendingByDescriptor.get(descriptor);
157
+ if (pending === undefined) {
158
+ blocking.close(descriptor);
159
+ return;
160
+ }
161
+ pendingByDescriptor.delete(descriptor);
162
+ pending.then(() => {
163
+ try {
164
+ files.close(descriptor);
165
+ }
166
+ catch { }
167
+ });
168
+ },
169
+ readAsync(descriptor, target, position) {
170
+ return inOrder(descriptor, async () => {
171
+ const bytes = await files.readAsync(descriptor, target.byteLength, position);
172
+ target.set(new Uint8Array(bytes));
173
+ return bytes.byteLength;
174
+ });
175
+ },
176
+ writeAsync(descriptor, source, position) {
177
+ const bytes = pendingByDescriptor.has(descriptor) ? source.slice() : source;
178
+ return inOrder(descriptor, () => files.writeAsync(descriptor, bytes.buffer, bytes.byteOffset, bytes.byteLength, position));
179
+ },
180
+ syncAsync(descriptor) {
181
+ return inOrder(descriptor, () => files.syncAsync(descriptor));
182
+ },
183
+ };
184
+ }
185
+ export function adaptFiles(files, nativeThreads) {
186
+ const blocking = blockingFiles(files);
187
+ return nativeThreads ? onNativeThreads(files, blocking) : onTheCallingThread(blocking);
188
+ }
@@ -0,0 +1,13 @@
1
+ import type { ReactNativeHost } from '@delali/narsil';
2
+ import type { NarsilCore } from '../specs/core.nitro';
3
+ import type { NarsilFiles } from '../specs/files.nitro';
4
+ import type { NarsilHost } from '../specs/host.nitro';
5
+ export interface NativeObjects {
6
+ host: NarsilHost;
7
+ files: NarsilFiles;
8
+ core: NarsilCore;
9
+ }
10
+ export declare function installWorkletDispatcherOn(host: NarsilHost): boolean;
11
+ export declare function threadPriorityOf(host: NarsilHost): number | null;
12
+ export declare function adoptAppThreadPriorityOn(host: NarsilHost, appThreadPriority: number): boolean;
13
+ export declare function adaptHost({ host, files, core }: NativeObjects, nativeThreads: boolean): ReactNativeHost;
@@ -0,0 +1,102 @@
1
+ import { adaptCore } from './core';
2
+ import { adaptFiles } from './files';
3
+ import { withNativeCode } from './native-error';
4
+ function bufferOf(view) {
5
+ return view.buffer;
6
+ }
7
+ function collectGarbageOn(host) {
8
+ const collect = Reflect.get(host, 'collectGarbage');
9
+ if (typeof collect !== 'function')
10
+ return false;
11
+ return Reflect.apply(collect, host, []) === true;
12
+ }
13
+ export function installWorkletDispatcherOn(host) {
14
+ const install = Reflect.get(host, 'installWorkletDispatcher');
15
+ if (typeof install !== 'function')
16
+ return false;
17
+ return Reflect.apply(install, host, []) === true;
18
+ }
19
+ export function threadPriorityOf(host) {
20
+ const read = Reflect.get(host, 'threadPriority');
21
+ if (typeof read !== 'function')
22
+ return null;
23
+ const priority = Reflect.apply(read, host, []);
24
+ return typeof priority === 'number' && Number.isFinite(priority) ? priority : null;
25
+ }
26
+ export function adoptAppThreadPriorityOn(host, appThreadPriority) {
27
+ const adopt = Reflect.get(host, 'adoptAppThreadPriority');
28
+ if (typeof adopt !== 'function')
29
+ return false;
30
+ return Reflect.apply(adopt, host, [appThreadPriority]) === true;
31
+ }
32
+ export function adaptHost({ host, files, core }, nativeThreads) {
33
+ return {
34
+ reserveMemory(bytes) {
35
+ try {
36
+ return host.reserveMemory(bytes);
37
+ }
38
+ catch (error) {
39
+ throw withNativeCode(error);
40
+ }
41
+ },
42
+ allocateMemory(bytes) {
43
+ try {
44
+ return host.allocateMemory(bytes);
45
+ }
46
+ catch (error) {
47
+ throw withNativeCode(error);
48
+ }
49
+ },
50
+ ownsMemory: (buffer) => host.ownsMemory(buffer),
51
+ fillRandom(target) {
52
+ host.fillRandom(bufferOf(target), target.byteOffset, target.byteLength);
53
+ },
54
+ processId: () => host.processId(),
55
+ processStartedAtMs: () => host.processStartedAtMs(),
56
+ processIsAlive: (processId) => host.processIsAlive(processId),
57
+ processMemory: () => host.processMemory(),
58
+ collectGarbage: () => collectGarbageOn(host),
59
+ appStorageDirectory() {
60
+ try {
61
+ return host.appStorageDirectory();
62
+ }
63
+ catch (error) {
64
+ throw withNativeCode(error);
65
+ }
66
+ },
67
+ deflateRaw(raw) {
68
+ try {
69
+ return new Uint8Array(host.deflateRaw(bufferOf(raw), raw.byteOffset, raw.byteLength));
70
+ }
71
+ catch (error) {
72
+ throw withNativeCode(error);
73
+ }
74
+ },
75
+ inflateRaw(compressed, rawByteLength) {
76
+ try {
77
+ return new Uint8Array(host.inflateRaw(bufferOf(compressed), compressed.byteOffset, compressed.byteLength, rawByteLength));
78
+ }
79
+ catch (error) {
80
+ throw withNativeCode(error);
81
+ }
82
+ },
83
+ gzip(data) {
84
+ try {
85
+ return new Uint8Array(host.gzip(bufferOf(data), data.byteOffset, data.byteLength));
86
+ }
87
+ catch (error) {
88
+ throw withNativeCode(error);
89
+ }
90
+ },
91
+ gunzip(compressed, maxByteLength) {
92
+ try {
93
+ return new Uint8Array(host.gunzip(bufferOf(compressed), compressed.byteOffset, compressed.byteLength, maxByteLength));
94
+ }
95
+ catch (error) {
96
+ throw withNativeCode(error);
97
+ }
98
+ },
99
+ files: adaptFiles(files, nativeThreads),
100
+ core: adaptCore(core, nativeThreads),
101
+ };
102
+ }
@@ -0,0 +1,4 @@
1
+ import type { ReactNativeHost } from '@delali/narsil';
2
+ import { type NativeObjects } from './host';
3
+ export declare function installedHost(): ReactNativeHost;
4
+ export declare function installEngineHost(natives: NativeObjects, nativeThreads: boolean): void;
@@ -0,0 +1,29 @@
1
+ import { adaptHost } from './host';
2
+ import { installAtomics } from './stand-ins/atomics';
3
+ import { installCrypto } from './stand-ins/crypto';
4
+ import { installProcess } from './stand-ins/process';
5
+ import { installStreams } from './stand-ins/streams';
6
+ import { installStructuredClone } from './stand-ins/structured-clone';
7
+ const REACT_NATIVE_HOST_KEY = Symbol.for('@delali/narsil-react-native/host');
8
+ export function installedHost() {
9
+ const host = globalThis[REACT_NATIVE_HOST_KEY];
10
+ if (host === undefined)
11
+ throw new Error('The Narsil engine runtime started without its native host');
12
+ return host;
13
+ }
14
+ export function installEngineHost(natives, nativeThreads) {
15
+ const scope = globalThis;
16
+ if (scope[REACT_NATIVE_HOST_KEY] !== undefined)
17
+ return;
18
+ installStructuredClone();
19
+ installStreams();
20
+ installAtomics(natives.host);
21
+ installCrypto(natives.host);
22
+ installProcess(natives.host);
23
+ Object.defineProperty(globalThis, REACT_NATIVE_HOST_KEY, {
24
+ value: adaptHost(natives, nativeThreads),
25
+ writable: false,
26
+ configurable: false,
27
+ enumerable: false,
28
+ });
29
+ }
@@ -0,0 +1 @@
1
+ export declare function withNativeCode(error: unknown): unknown;
@@ -0,0 +1,12 @@
1
+ const NATIVE_ERROR_CODE = /^[^[]*\[(E[A-Z0-9_]+)\]/;
2
+ export function withNativeCode(error) {
3
+ if (!(error instanceof Error))
4
+ return error;
5
+ if (error.code !== undefined)
6
+ return error;
7
+ const match = NATIVE_ERROR_CODE.exec(error.message);
8
+ if (match === null)
9
+ return error;
10
+ Object.defineProperty(error, 'code', { value: match[1], enumerable: true, configurable: true, writable: true });
11
+ return error;
12
+ }
@@ -0,0 +1,5 @@
1
+ export interface NativeAtomics {
2
+ ownsMemory(buffer: ArrayBuffer): boolean;
3
+ atomic(operation: number, elementKind: number, buffer: ArrayBuffer, byteIndex: number, value: number, replacement: number): number;
4
+ }
5
+ export declare function installAtomics(native: NativeAtomics): void;