@finogeeks2026/finshield-opencode 0.0.0-stage → 0.11.50

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 (143) hide show
  1. package/DESIGN.md +593 -0
  2. package/LICENSE.rtf +28 -0
  3. package/README.md +132 -2
  4. package/THIRD-PARTY-NOTICES.md +3131 -0
  5. package/bin/node.exe +4 -0
  6. package/dist/gui-experimental.cjs +96 -0
  7. package/dist/updater/server.js +1046 -0
  8. package/install/download-source.json +3 -0
  9. package/install/runtime.js +60 -0
  10. package/node_modules/@finogeeks2026/finsafe-sdk/LICENSE.rtf +28 -0
  11. package/node_modules/@finogeeks2026/finsafe-sdk/README.md +116 -0
  12. package/node_modules/@finogeeks2026/finsafe-sdk/bin/finsafe_native.dll +0 -0
  13. package/node_modules/@finogeeks2026/finsafe-sdk/native-build.json +5 -0
  14. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/README.md +6 -0
  15. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/index.js +12 -0
  16. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/package.json +24 -0
  17. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/win32_x64/koffi.lib +0 -0
  18. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/win32_x64/koffi.node +0 -0
  19. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/CHANGELOG.md +1183 -0
  20. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/LICENSE.txt +22 -0
  21. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/README.md +42 -0
  22. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/cnoke.cjs +816 -0
  23. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/benchmarks.md +80 -0
  24. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/callbacks.md +182 -0
  25. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/composites.md +549 -0
  26. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/contribute.md +163 -0
  27. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/index.md +46 -0
  28. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/load.md +191 -0
  29. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/migration.md +256 -0
  30. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/misc.md +76 -0
  31. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/output.md +299 -0
  32. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/pointers.md +385 -0
  33. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/primitives.md +151 -0
  34. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/start.md +110 -0
  35. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/types.md +88 -0
  36. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/unions.md +0 -0
  37. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/values.md +134 -0
  38. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/index.cjs +1 -0
  39. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/index.d.ts +389 -0
  40. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/index.js +2 -0
  41. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/indirect.cjs +1 -0
  42. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/indirect.js +2 -0
  43. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/lib/native/base/base.cc +11064 -0
  44. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/lib/native/base/base.hh +5951 -0
  45. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/lib/native/base/unicode.inc +408 -0
  46. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/package.json +68 -0
  47. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/CMakeLists.txt +249 -0
  48. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/index.cjs +296 -0
  49. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/index.js +281 -0
  50. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/indirect.cjs +295 -0
  51. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/indirect.js +280 -0
  52. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/arm64.cc +1408 -0
  53. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/arm64_asm.S +216 -0
  54. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/arm64_asm.asm +181 -0
  55. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/loong64.cc +5 -0
  56. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/loong64_asm.S +200 -0
  57. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/riscv64.cc +1345 -0
  58. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/riscv64_asm.S +200 -0
  59. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64sysv.cc +1434 -0
  60. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64sysv_asm.S +224 -0
  61. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64win.cc +1086 -0
  62. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64win_asm.S +162 -0
  63. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64win_asm.asm +189 -0
  64. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x86.cc +1185 -0
  65. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x86_asm.S +217 -0
  66. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x86_asm.asm +174 -0
  67. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/call.cc +2094 -0
  68. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/call.hh +179 -0
  69. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/errno.inc +462 -0
  70. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/ffi.cc +3009 -0
  71. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/ffi.hh +461 -0
  72. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/parser.cc +218 -0
  73. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/parser.hh +54 -0
  74. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/primitives.inc +36 -0
  75. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/static.cjs +130 -0
  76. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/static.js +133 -0
  77. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/trampolines.cjs +77 -0
  78. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/type.cc +725 -0
  79. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/type.hh +71 -0
  80. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/util.cc +1040 -0
  81. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/util.hh +493 -0
  82. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/uv.cc +214 -0
  83. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/uv.def +11 -0
  84. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/uv.hh +112 -0
  85. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/win32.cc +198 -0
  86. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/win32.hh +148 -0
  87. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/LICENSE.md +9 -0
  88. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/README.md +95 -0
  89. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/napi-inl.deprecated.h +186 -0
  90. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/napi-inl.h +7211 -0
  91. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/napi.h +3421 -0
  92. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/LICENSE +21 -0
  93. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/README.md +95 -0
  94. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/def/js_native_api.def +125 -0
  95. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/def/node_api.def +157 -0
  96. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/include/js_native_api.h +591 -0
  97. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/include/js_native_api_types.h +210 -0
  98. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/include/node_api.h +265 -0
  99. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/include/node_api_types.h +58 -0
  100. package/node_modules/@finogeeks2026/finsafe-sdk/package.json +36 -0
  101. package/node_modules/@finogeeks2026/finsafe-sdk/release.json +4 -0
  102. package/node_modules/@finogeeks2026/finsafe-sdk/src/native.js +85 -0
  103. package/node_modules/@finogeeks2026/finsafe-sdk/src/shell.js +20 -0
  104. package/node_modules/@finogeeks2026/finsafe-sdk/src/status.js +57 -0
  105. package/package.json +47 -4
  106. package/policy/candidate.yaml +347 -0
  107. package/release.json +4 -0
  108. package/src/activation.js +2 -0
  109. package/src/attachment.js +34 -0
  110. package/src/connector.js +91 -0
  111. package/src/contract.js +8 -0
  112. package/src/desktop-contract.js +4 -0
  113. package/src/desktop.js +25 -0
  114. package/src/gui.tsx +58 -0
  115. package/src/host.js +38 -0
  116. package/src/index.js +12 -0
  117. package/src/instance-settings.js +27 -0
  118. package/src/known-folders.js +22 -0
  119. package/src/management.js +17 -0
  120. package/src/preparation.js +18 -0
  121. package/src/presentation.js +153 -0
  122. package/src/settings.js +18 -0
  123. package/ui/feedback.cjs +45 -0
  124. package/ui/page.cjs +225 -0
  125. package/ui/state.cjs +44 -0
  126. package/updater/bootstrap.js +41 -0
  127. package/updater/common/activation.js +81 -0
  128. package/updater/common/coordinator.js +329 -0
  129. package/updater/common/download.js +57 -0
  130. package/updater/common/index.js +12 -0
  131. package/updater/common/package.js +53 -0
  132. package/updater/common/protocol.js +38 -0
  133. package/updater/common/transport.js +65 -0
  134. package/updater/coordinator.js +2 -0
  135. package/updater/launch.js +40 -0
  136. package/updater/management.js +9 -0
  137. package/updater/native.js +18 -0
  138. package/updater/operations.js +45 -0
  139. package/updater/protocol.js +1 -0
  140. package/updater/server.js +100 -0
  141. package/updater/transport.js +10 -0
  142. package/updater/windows-launch-reader.js +40 -0
  143. package/updater/windows-launch.js +96 -0
@@ -0,0 +1,461 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // SPDX-FileCopyrightText: 2026 Niels Martignène <niels.martignene@protonmail.com>
3
+
4
+ #pragma once
5
+
6
+ #include "lib/native/base/base.hh"
7
+
8
+ #include <napi.h>
9
+
10
+ namespace K {
11
+
12
+ // #define EXTERNAL_POINTERS
13
+ // #define EXTERNAL_TYPES
14
+
15
+ #if defined(_MSC_VER)
16
+ #define FORCE_INLINE __forceinline
17
+ #else
18
+ #define FORCE_INLINE __attribute__((always_inline)) inline
19
+ #endif
20
+ #if defined(UNITY_BUILD)
21
+ #define INLINE_UNITY FORCE_INLINE
22
+ #else
23
+ #define INLINE_UNITY
24
+ #endif
25
+
26
+ #if defined(__GNUC__) || defined(__clang__)
27
+ #if __has_attribute(musttail) && __has_attribute(preserve_none)
28
+ #define MUST_TAIL __attribute__((musttail))
29
+ #define PRESERVE_NONE __attribute__((preserve_none))
30
+ #elif __has_attribute(musttail) && !defined(__clang__)
31
+ // GCC regalloc seems better, so the generated code is not too bad even without preserve_none
32
+ #define MUST_TAIL __attribute__((musttail))
33
+ #define PRESERVE_NONE
34
+ #endif
35
+ #endif
36
+
37
+ #define NAPI_OK(Call) \
38
+ do { \
39
+ napi_status _status = (Call); \
40
+ K_ASSERT(_status == napi_ok); \
41
+ } while (false)
42
+
43
+ static const Size DefaultSyncStackSize = Mebibytes(1);
44
+ static const Size DefaultSyncHeapSize = Mebibytes(2);
45
+ static const Size DefaultAsyncStackSize = Kibibytes(128);
46
+ static const Size DefaultAsyncHeapSize = Kibibytes(128);
47
+ static const int DefaultResidentAsyncPools = 4;
48
+ static const int DefaultMaxAsyncCalls = 256;
49
+ static const Size DefaultMaxTypeSize = Mebibytes(64);
50
+
51
+ static const int MaxAsyncCalls = 4096;
52
+ static const Size MaxParameters = 64;
53
+ static const Size MaxTrampolines = 8192;
54
+
55
+ enum class PrimitiveKind {
56
+ #define PRIMITIVE(Name) Name,
57
+ #include "primitives.inc"
58
+ };
59
+ static const char *const PrimitiveKindNames[] = {
60
+ #define PRIMITIVE(Name) K_STRINGIFY(Name),
61
+ #include "primitives.inc"
62
+ };
63
+
64
+ struct InstanceData;
65
+ struct TypeInfo;
66
+ struct RecordMember;
67
+ struct FunctionInfo;
68
+ struct CallData;
69
+
70
+ typedef void DisposeFunc (InstanceData *instance, const TypeInfo *type, const void *ptr);
71
+
72
+ enum class TypeFlag {
73
+ HasTypedArray = 1 << 0,
74
+ IsCharLike = 1 << 1,
75
+ FillWithOnes = 1 << 2
76
+ };
77
+
78
+ enum class ArrayHint {
79
+ Array,
80
+ Typed,
81
+ Buffer,
82
+ String
83
+ };
84
+ static const char *const ArrayHintNames[] = {
85
+ "Array",
86
+ "Typed",
87
+ "Buffer",
88
+ "String"
89
+ };
90
+
91
+ struct RecordMember {
92
+ const char *name;
93
+ const TypeInfo *type;
94
+ int32_t offset;
95
+ Size countedby;
96
+
97
+ napi_ref key = nullptr;
98
+
99
+ #if defined(K_DEBUG)
100
+ ~RecordMember() { K_ASSERT(!key); }
101
+ #endif
102
+ };
103
+
104
+ struct TypeInfo {
105
+ K_DELETE_COPY(TypeInfo)
106
+
107
+ InstanceData *instance = nullptr;
108
+
109
+ const char *name;
110
+ alignas(8) PrimitiveKind primitive;
111
+ int32_t size;
112
+ int16_t align;
113
+ uint16_t flags;
114
+
115
+ DisposeFunc *dispose = nullptr;
116
+ napi_ref dispose_ref = nullptr;
117
+
118
+ HeapArray<RecordMember> members; // Record or Union
119
+ struct {
120
+ const TypeInfo *type; // Pointer or array
121
+ int32_t stride; // Array only
122
+ } ref;
123
+ const FunctionInfo *proto; // Callback only
124
+ ArrayHint hint; // Array only
125
+ const char *countedby; // Pointer or array
126
+
127
+ mutable napi_ref construct = nullptr; // Union only
128
+ mutable napi_ref defn = nullptr;
129
+
130
+ mutable const TypeInfo *reshaped = nullptr;
131
+
132
+ TypeInfo() = default;
133
+ ~TypeInfo();
134
+
135
+ TypeInfo(TypeInfo &&other) = default;
136
+ TypeInfo &operator=(TypeInfo &&other) = default;
137
+
138
+ K_HASHTABLE_HANDLER(TypeInfo, name);
139
+ };
140
+
141
+ struct LibraryHolder {
142
+ K_DELETE_COPY(LibraryHolder)
143
+
144
+ void *module = nullptr; // HMODULE on Windows
145
+ mutable int refcount = 1;
146
+
147
+ LibraryHolder(void *module) : module(module) {}
148
+ ~LibraryHolder() { Unload(); }
149
+
150
+ void Unload();
151
+
152
+ const LibraryHolder *Ref() const
153
+ {
154
+ refcount++;
155
+ return this;
156
+ }
157
+
158
+ void Unref() const
159
+ {
160
+ if (--refcount)
161
+ return;
162
+ delete this;
163
+ }
164
+ };
165
+
166
+ class LibraryHandle: public Napi::ObjectWrap<LibraryHandle> {
167
+ LibraryHolder *lib = nullptr;
168
+
169
+ public:
170
+ static Napi::Function InitClass(InstanceData *instance);
171
+
172
+ LibraryHandle(const Napi::CallbackInfo &info);
173
+
174
+ void Finalize(Napi::BasicEnv env) override;
175
+
176
+ private:
177
+ Napi::Value Func(const Napi::CallbackInfo &info);
178
+ Napi::Value Symbol(const Napi::CallbackInfo &info);
179
+ Napi::Value Unload(const Napi::CallbackInfo &info);
180
+ };
181
+
182
+ enum class CallConvention {
183
+ Cdecl,
184
+ Stdcall,
185
+ Fastcall,
186
+ Thiscall
187
+ };
188
+ static const char *const CallConventionNames[] = {
189
+ "Cdecl",
190
+ "Stdcall",
191
+ "Fastcall",
192
+ "Thiscall"
193
+ };
194
+
195
+ enum class AbiMethod : int;
196
+
197
+ struct AbiInstruction {
198
+ void *op;
199
+
200
+ int32_t a = 0;
201
+ int16_t b1 = 0;
202
+ int16_t b2 = 0;
203
+ const TypeInfo *type = nullptr;
204
+ };
205
+
206
+ struct ParameterInfo {
207
+ const TypeInfo *type;
208
+ int directions;
209
+ bool variadic;
210
+ int8_t offset;
211
+
212
+ // ABI-specific part
213
+
214
+ #if defined(_M_X64)
215
+ struct {
216
+ bool regular; // Used for structs and unions
217
+ } abi;
218
+ #elif defined(__x86_64__)
219
+ struct {
220
+ bool regular;
221
+ int offsets[2];
222
+ } abi;
223
+ #elif defined(__arm__) || defined(__aarch64__) || defined(_M_ARM64)
224
+ struct {
225
+ bool regular;
226
+ bool indirect;
227
+ int offset;
228
+ } abi;
229
+ #elif __riscv_xlen == 64 || defined(__loongarch64)
230
+ struct {
231
+ AbiMethod method;
232
+ int offsets[2];
233
+ } abi;
234
+ #endif
235
+ };
236
+
237
+ struct ReturnInfo {
238
+ const TypeInfo *type;
239
+
240
+ // ABI-specific part
241
+
242
+ #if defined(_M_X64)
243
+ struct {
244
+ bool regular;
245
+ } abi;
246
+ #elif defined(__x86_64__)
247
+ struct {
248
+ AbiMethod method;
249
+ } abi;
250
+ #elif defined(__arm__) || defined(__aarch64__) || defined(_M_ARM64)
251
+ struct {
252
+ bool regular;
253
+ int offset;
254
+ } abi;
255
+ #elif defined(__i386__) || defined(_M_IX86)
256
+ struct {
257
+ bool regular;
258
+ } abi;
259
+ #elif __riscv_xlen == 64 || defined(__loongarch64)
260
+ struct {
261
+ AbiMethod method;
262
+ int offsets[2];
263
+ } abi;
264
+ #endif
265
+ };
266
+
267
+ struct ValueCast {
268
+ napi_env env;
269
+ napi_ref ref;
270
+ const TypeInfo *type;
271
+
272
+ ~ValueCast();
273
+ };
274
+
275
+ // Also used for callbacks, even though many members are not used in this case
276
+ struct FunctionInfo {
277
+ mutable int refcount = 1;
278
+
279
+ napi_env env;
280
+ InstanceData *instance;
281
+ const LibraryHolder *lib = nullptr;
282
+
283
+ const char *name;
284
+ const char *decorated_name; // Only set for some platforms/calling conventions
285
+ #if defined(_WIN32)
286
+ int ordinal_name = -1;
287
+ #endif
288
+
289
+ void *native;
290
+ CallConvention convention;
291
+
292
+ ReturnInfo ret;
293
+ HeapArray<ParameterInfo> parameters;
294
+ int8_t required_parameters;
295
+ bool variadic;
296
+
297
+ // ABI-specific part
298
+
299
+ HeapArray<AbiInstruction> sync;
300
+ HeapArray<AbiInstruction> async;
301
+ Size stk_size;
302
+ #if defined(__i386__) || defined(_M_IX86)
303
+ int ret_pop;
304
+ #else
305
+ bool forward_fp;
306
+ #endif
307
+
308
+ ~FunctionInfo();
309
+
310
+ const FunctionInfo *Ref() const
311
+ {
312
+ refcount++;
313
+ return this;
314
+ }
315
+
316
+ void Unref() const
317
+ {
318
+ if (--refcount)
319
+ return;
320
+ delete this;
321
+ }
322
+ };
323
+
324
+ template <typename T>
325
+ struct MemoryRange {
326
+ T *ptr;
327
+ T *end;
328
+ };
329
+
330
+ struct InstanceMemory {
331
+ ~InstanceMemory();
332
+
333
+ MemoryRange<uint8_t> stack;
334
+ MemoryRange<uint8_t> stack0;
335
+ MemoryRange<uint8_t> heap;
336
+
337
+ // For big heap allocations
338
+ LinkedAllocator allocator;
339
+
340
+ bool busy;
341
+ bool temporary;
342
+ };
343
+
344
+ struct InstanceData {
345
+ ~InstanceData();
346
+
347
+ napi_env env;
348
+
349
+ BucketArray<TypeInfo> types;
350
+ HashMap<const char *, const TypeInfo *> types_map;
351
+ BucketArray<FunctionInfo> callbacks;
352
+ Size base_types_count;
353
+
354
+ const TypeInfo *void_type;
355
+ const TypeInfo *char_type;
356
+ const TypeInfo *char16_type;
357
+ const TypeInfo *char32_type;
358
+ const TypeInfo *str_type;
359
+ const TypeInfo *str16_type;
360
+ const TypeInfo *str32_type;
361
+
362
+ Napi::ObjectReference object_constructor;
363
+ Napi::FunctionReference construct_lib;
364
+ #if !defined(EXTERNAL_TYPES)
365
+ Napi::FunctionReference construct_type;
366
+ #endif
367
+ Napi::FunctionReference construct_poll;
368
+ Napi::Reference<Napi::Symbol> active_symbol;
369
+
370
+ std::mutex mem_mutex;
371
+ LocalArray<InstanceMemory *, 17> memories;
372
+ int temporaries = 0;
373
+
374
+ // Variadic cache
375
+ FunctionInfo *variadic_func = nullptr;
376
+
377
+ std::thread::id main_thread_id;
378
+ napi_threadsafe_function broker = nullptr;
379
+
380
+ CallData *sync_call = nullptr;
381
+
382
+ #if defined(_WIN32)
383
+ void *main_stack_max;
384
+ void *main_stack_min;
385
+
386
+ uint32_t last_error = 0;
387
+ #endif
388
+
389
+ BucketArray<LinkedAllocator> encode_allocators;
390
+ HashMap<void *, LinkedAllocator *> encode_map;
391
+
392
+ HashMap<void *, int16_t> trampolines_map;
393
+
394
+ BlockAllocator str_alloc;
395
+
396
+ struct {
397
+ Size sync_stack_size = DefaultSyncStackSize;
398
+ Size sync_heap_size = DefaultSyncHeapSize;
399
+ Size async_stack_size = DefaultAsyncStackSize;
400
+ Size async_heap_size = DefaultAsyncHeapSize;
401
+ int resident_async_pools = DefaultResidentAsyncPools;
402
+ int max_temporaries = DefaultMaxAsyncCalls - DefaultResidentAsyncPools;
403
+ Size max_type_size = DefaultMaxTypeSize;
404
+ } config;
405
+
406
+ struct {
407
+ int64_t disposed = 0;
408
+ } stats;
409
+ };
410
+ static_assert(DefaultResidentAsyncPools <= K_LEN(InstanceData::memories.data) - 1);
411
+ static_assert(DefaultMaxAsyncCalls >= DefaultResidentAsyncPools);
412
+ static_assert(MaxAsyncCalls >= DefaultMaxAsyncCalls);
413
+
414
+ struct TrampolineInfo {
415
+ int state;
416
+
417
+ napi_env env;
418
+ InstanceData *instance;
419
+ MemoryRange<uint8_t> stack0;
420
+
421
+ const FunctionInfo *proto;
422
+ napi_ref func;
423
+ };
424
+
425
+ struct SharedData {
426
+ std::mutex mutex;
427
+
428
+ TrampolineInfo trampolines[MaxTrampolines];
429
+ LocalArray<int16_t, MaxTrampolines> available;
430
+
431
+ SharedData()
432
+ {
433
+ available.len = MaxTrampolines;
434
+
435
+ for (int16_t i = 0; i < MaxTrampolines; i++) {
436
+ available[i] = i;
437
+ }
438
+ }
439
+ };
440
+ static_assert(MaxTrampolines <= INT16_MAX);
441
+
442
+ extern const napi_type_tag LibraryHandleMarker;
443
+ extern const napi_type_tag TypeObjectMarker;
444
+ extern const napi_type_tag DirectionMarker;
445
+ extern const napi_type_tag UnionValueMarker;
446
+ extern const napi_type_tag CastMarker;
447
+
448
+ extern SharedData shared;
449
+
450
+ // Some Node-API functions are loaded dynamically to work around bugs or because they are recent
451
+ extern napi_status (NAPI_CDECL *node_api_delete_reference)(node_api_basic_env env, napi_ref ref);
452
+ extern napi_status (NAPI_CDECL *node_api_get_buffer_info)(napi_env env, napi_value value, void **data, size_t *length);
453
+ extern napi_status (NAPI_CDECL *node_api_create_property_key_utf8)(napi_env env, const char* str, size_t length, napi_value* result);
454
+ extern napi_status (NAPI_CDECL *node_api_post_finalizer)(node_api_basic_env env, napi_finalize finalize_cb, void* finalize_data, void* finalize_hint);
455
+ extern napi_status (NAPI_CDECL *node_api_create_object_with_properties)(napi_env env, napi_value prototype_or_null, const napi_value *property_names,
456
+ const napi_value *property_values, size_t property_count, napi_value *result);
457
+
458
+ InstanceMemory *AllocateMemory(InstanceData *instance, Size stack_size, Size heap_size);
459
+ void ReleaseMemory(InstanceData *instance, InstanceMemory *mem);
460
+
461
+ }
@@ -0,0 +1,218 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // SPDX-FileCopyrightText: 2026 Niels Martignène <niels.martignene@protonmail.com>
3
+
4
+ #include "lib/native/base/base.hh"
5
+ #include "call.hh"
6
+ #include "ffi.hh"
7
+ #include "parser.hh"
8
+ #include "type.hh"
9
+
10
+ #include <napi.h>
11
+
12
+ namespace K {
13
+
14
+ bool PrototypeParser::Parse(const char *str, bool concrete, FunctionInfo *out_func)
15
+ {
16
+ tokens.Clear();
17
+ offset = 0;
18
+ valid = true;
19
+
20
+ Tokenize(str);
21
+
22
+ out_func->ret.type = ParseType(nullptr);
23
+ if (!CanReturnType(out_func->ret.type)) {
24
+ MarkError("You are not allowed to directly return %1 values (maybe try %1 *)", out_func->ret.type->name);
25
+ return false;
26
+ }
27
+ offset += (offset < tokens.len && DetectCallConvention(tokens[offset], &out_func->convention));
28
+
29
+ if (offset >= tokens.len) {
30
+ MarkError("Unexpected end of prototype, expected identifier");
31
+ return false;
32
+ }
33
+ if (IsIdentifier(tokens[offset])) {
34
+ Span<const char> tok = tokens[offset++];
35
+ out_func->name = DuplicateString(tok, &instance->str_alloc).ptr;
36
+ } else if (!concrete) {
37
+ // Leave anonymous naming responsibility to caller
38
+ out_func->name = nullptr;
39
+ } else {
40
+ MarkError("Unexpected token '%1', expected identifier", tokens[offset]);
41
+ return false;
42
+ }
43
+
44
+ Consume("(");
45
+ offset += (offset + 1 < tokens.len && tokens[offset] == "void" && tokens[offset + 1] == ")");
46
+ if (offset < tokens.len && tokens[offset] != ")") {
47
+ for (;;) {
48
+ ParameterInfo param = {};
49
+
50
+ if (Match("...")) {
51
+ out_func->variadic = true;
52
+ break;
53
+ }
54
+
55
+ param.type = ParseType(&param.directions);
56
+
57
+ if (!CanPassType(param.type, param.directions)) {
58
+ MarkError("Type %1 cannot be used as a parameter", param.type->name);
59
+ return false;
60
+ }
61
+ if (out_func->parameters.len >= MaxParameters) {
62
+ MarkError("Functions cannot have more than %1 parameters", MaxParameters);
63
+ return false;
64
+ }
65
+
66
+ param.offset = (int8_t)out_func->parameters.len;
67
+
68
+ out_func->parameters.Append(param);
69
+
70
+ offset += (offset < tokens.len && IsIdentifier(tokens[offset]));
71
+ if (offset >= tokens.len || tokens[offset] != ",")
72
+ break;
73
+ offset++;
74
+ }
75
+ }
76
+ Consume(")");
77
+
78
+ out_func->required_parameters = (int8_t)out_func->parameters.len;
79
+
80
+ Match(";");
81
+ if (offset < tokens.len) {
82
+ MarkError("Unexpected token '%1' after prototype", tokens[offset]);
83
+ }
84
+
85
+ return valid;
86
+ }
87
+
88
+ void PrototypeParser::Tokenize(const char *str)
89
+ {
90
+ for (Size i = 0; str[i]; i++) {
91
+ char c = str[i];
92
+
93
+ if (IsAsciiWhite(c)) {
94
+ continue;
95
+ } else if (IsXidStart(c)) {
96
+ Size j = i;
97
+ while (str[++j] && IsXidContinue(str[j]));
98
+
99
+ Span<const char> tok = MakeSpan(str + i, j - i);
100
+ tokens.Append(tok);
101
+
102
+ i = j - 1;
103
+ } else if (IsAsciiDigit(c)) {
104
+ Size j = i;
105
+ while (str[++j] && IsAsciiDigit(str[j]));
106
+ if (str[j] == '.') {
107
+ while (str[++j] && IsAsciiDigit(str[j]));
108
+ }
109
+
110
+ Span<const char> tok = MakeSpan(str + i, j - i);
111
+ tokens.Append(tok);
112
+
113
+ i = j - 1;
114
+ } else if (c == '.' && str[i + 1] == '.' && str[i + 2] == '.') {
115
+ tokens.Append("...");
116
+ i += 2;
117
+ } else {
118
+ Span<const char> tok = MakeSpan(str + i, 1);
119
+ tokens.Append(tok);
120
+ }
121
+ }
122
+ }
123
+
124
+ const TypeInfo *PrototypeParser::ParseType(int *out_directions)
125
+ {
126
+ Size start = offset;
127
+
128
+ if (offset >= tokens.len) {
129
+ MarkError("Unexpected end of prototype, expected type");
130
+ return instance->void_type;
131
+ } else if (!IsIdentifier(tokens[offset])) {
132
+ MarkError("Unexpected token '%1', expected type", tokens[offset]);
133
+ return instance->void_type;
134
+ }
135
+
136
+ while (++offset < tokens.len && IsIdentifier(tokens[offset]));
137
+ offset--;
138
+ while (++offset < tokens.len && (tokens[offset] == '*' ||
139
+ tokens[offset] == '!' ||
140
+ tokens[offset] == "const"));
141
+ if (offset < tokens.len && tokens[offset] == "[") [[unlikely]] {
142
+ MarkError("Array types decay to pointers in prototypes (C standard), use pointers");
143
+ return instance->void_type;
144
+ }
145
+ offset--;
146
+
147
+ if (out_directions) {
148
+ if (offset > start) {
149
+ int directions = ResolveDirections(tokens[start]);
150
+
151
+ if (directions) {
152
+ *out_directions = directions;
153
+ start++;
154
+ } else {
155
+ *out_directions = 1;
156
+ }
157
+ } else {
158
+ *out_directions = 1;
159
+ }
160
+ }
161
+
162
+ while (offset >= start) {
163
+ Span<const char> str = MakeSpan(tokens[start].ptr, tokens[offset].end() - tokens[start].ptr);
164
+ const TypeInfo *type = ResolveType(instance, str);
165
+
166
+ if (type) {
167
+ offset++;
168
+ return type;
169
+ }
170
+ if (env.IsExceptionPending()) [[unlikely]]
171
+ return instance->void_type;
172
+
173
+ offset--;
174
+ }
175
+ offset = start;
176
+
177
+ MarkError("Unknown or invalid type name '%1'", tokens[offset]);
178
+ return instance->void_type;
179
+ }
180
+
181
+ bool PrototypeParser::Consume(const char *expect)
182
+ {
183
+ if (offset >= tokens.len) {
184
+ MarkError("Unexpected end of prototype, expected '%1'", expect);
185
+ return false;
186
+ }
187
+ if (tokens[offset] != expect) {
188
+ MarkError("Unexpected token '%1', expected '%2'", tokens[offset], expect);
189
+ return false;
190
+ }
191
+
192
+ offset++;
193
+ return true;
194
+ }
195
+
196
+ bool PrototypeParser::Match(const char *expect)
197
+ {
198
+ if (offset < tokens.len && tokens[offset] == expect) {
199
+ offset++;
200
+ return true;
201
+ } else {
202
+ return false;
203
+ }
204
+ }
205
+
206
+ bool PrototypeParser::IsIdentifier(Span<const char> tok) const
207
+ {
208
+ K_ASSERT(tok.len);
209
+ return IsAsciiAlpha(tok[0]) || tok[0] == '_';
210
+ }
211
+
212
+ bool ParsePrototype(InstanceData *instance, const char *str, bool concrete, FunctionInfo *out_func)
213
+ {
214
+ PrototypeParser parser(instance);
215
+ return parser.Parse(str, concrete, out_func);
216
+ }
217
+
218
+ }