@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,1345 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // SPDX-FileCopyrightText: 2026 Niels Martignène <niels.martignene@protonmail.com>
3
+
4
+ #if __riscv_xlen == 64 || defined(__loongarch64)
5
+
6
+ #include "lib/native/base/base.hh"
7
+ #include "../ffi.hh"
8
+ #include "../call.hh"
9
+ #include "../type.hh"
10
+ #include "../util.hh"
11
+
12
+ #include <napi.h>
13
+
14
+ namespace K {
15
+
16
+ struct A0A1Ret {
17
+ uint64_t a0;
18
+ uint64_t a1;
19
+ };
20
+ struct A0Fa0Ret {
21
+ uint64_t a0;
22
+ double fa0;
23
+ };
24
+ struct Fa0A0Ret {
25
+ double fa0;
26
+ uint64_t a0;
27
+ };
28
+ struct Fa0Fa1Ret {
29
+ double fa0;
30
+ double fa1;
31
+ };
32
+
33
+ struct BackRegisters {
34
+ uint64_t a0;
35
+ uint64_t a1;
36
+ double fa0;
37
+ double fa1;
38
+ };
39
+
40
+ extern "C" A0A1Ret ForwardCallGG(const void *func, uint8_t *sp, uint8_t **out_old_sp);
41
+ extern "C" float ForwardCallF(const void *func, uint8_t *sp, uint8_t **out_old_sp);
42
+ extern "C" Fa0A0Ret ForwardCallDG(const void *func, uint8_t *sp, uint8_t **out_old_sp);
43
+ extern "C" A0Fa0Ret ForwardCallGD(const void *func, uint8_t *sp, uint8_t **out_old_sp);
44
+ extern "C" Fa0Fa1Ret ForwardCallDD(const void *func, uint8_t *sp, uint8_t **out_old_sp);
45
+
46
+ extern "C" A0A1Ret ForwardCallGGX(const void *func, uint8_t *sp, uint8_t **out_old_sp);
47
+ extern "C" float ForwardCallFX(const void *func, uint8_t *sp, uint8_t **out_old_sp);
48
+ extern "C" Fa0A0Ret ForwardCallDGX(const void *func, uint8_t *sp, uint8_t **out_old_sp);
49
+ extern "C" A0Fa0Ret ForwardCallGDX(const void *func, uint8_t *sp, uint8_t **out_old_sp);
50
+ extern "C" Fa0Fa1Ret ForwardCallDDX(const void *func, uint8_t *sp, uint8_t **out_old_sp);
51
+
52
+ enum class AbiOpcode {
53
+ #define PRIMITIVE(Name) Push ## Name,
54
+ #include "../primitives.inc"
55
+ PushAggregateReg,
56
+ PushAggregateMem,
57
+ #define PRIMITIVE(Name) Run ## Name,
58
+ #include "../primitives.inc"
59
+ RunAggregateGG,
60
+ RunAggregateDD,
61
+ RunAggregateGD,
62
+ RunAggregateDG,
63
+ RunAggregateMem,
64
+ #define PRIMITIVE(Name) Run ## Name ## X,
65
+ #include "../primitives.inc"
66
+ RunAggregateGGX,
67
+ RunAggregateDDX,
68
+ RunAggregateGDX,
69
+ RunAggregateDGX,
70
+ RunAggregateMemX,
71
+ Yield,
72
+ CallGG,
73
+ CallF,
74
+ CallDG,
75
+ CallGD,
76
+ CallDD,
77
+ CallStack,
78
+ CallGGX,
79
+ CallFX,
80
+ CallDGX,
81
+ CallGDX,
82
+ CallDDX,
83
+ CallStackX,
84
+ #define PRIMITIVE(Name) Return ## Name,
85
+ #include "../primitives.inc"
86
+ ReturnAggregateReg,
87
+ ReturnAggregateMem
88
+ };
89
+
90
+ enum class AbiMethod {
91
+ Memory,
92
+ Gpr,
93
+ GprGpr,
94
+ Vec,
95
+ VecVec,
96
+ GprVec,
97
+ VecGpr,
98
+ GprStack
99
+ };
100
+
101
+ struct ClassResult {
102
+ AbiMethod method;
103
+
104
+ int gpr_index;
105
+ int vec_index;
106
+ };
107
+
108
+ namespace {
109
+ #if defined(MUST_TAIL)
110
+ PRESERVE_NONE typedef napi_value ForwardFunc(CallData *call, napi_value *args, uint8_t *base, const AbiInstruction *inst);
111
+
112
+ extern ForwardFunc *const ForwardDispatch[256];
113
+
114
+ inline void *Code2Op(AbiOpcode code)
115
+ {
116
+ return (void *)ForwardDispatch[(int)code];
117
+ }
118
+ #else
119
+ inline void *Code2Op(AbiOpcode code)
120
+ {
121
+ return (void *)code;
122
+ }
123
+ #endif
124
+ }
125
+
126
+ class ClassAnalyser {
127
+ int gpr_max;
128
+ int vec_max;
129
+ int gpr_index; // Can go beyond gpr_max when using stack
130
+ int vec_index;
131
+ int stack_start;
132
+
133
+ public:
134
+ ClassAnalyser(int gpr_index, int gpr_max, int vec_index, int vec_max, int stack_start = -1)
135
+ : gpr_max(gpr_max), vec_max(vec_max), gpr_index(gpr_index), vec_index(vec_index), stack_start(stack_start) {}
136
+
137
+ ClassResult Analyse(const TypeInfo *type, bool variadic);
138
+
139
+ int GprCount() const { return gpr_index; }
140
+ int VecCount() const { return vec_index; }
141
+ };
142
+
143
+ ClassResult ClassAnalyser::Analyse(const TypeInfo *type, bool variadic)
144
+ {
145
+ ClassResult ret = {};
146
+
147
+ // Use memory for values bigger than two registers
148
+ if (type->size > 16) {
149
+ ret.method = AbiMethod::Memory;
150
+ ret.gpr_index = gpr_index;
151
+
152
+ gpr_index++;
153
+
154
+ return ret;
155
+ }
156
+
157
+ int gpr_avail = std::min(2, gpr_max - gpr_index); // Can go negative
158
+ int vec_avail = std::min(2, vec_max - vec_index);
159
+
160
+ #if defined(__riscv_float_abi_double) || defined(__loongarch64)
161
+ if (type->primitive != PrimitiveKind::Union && !variadic) {
162
+ int gpr_count = 0;
163
+ int vec_count = 0;
164
+ bool gpr_vec = false;
165
+
166
+ AnalyseFlat(type, [&](const TypeInfo *type, int offset, int count) {
167
+ if (IsFloat(type)) {
168
+ vec_count += count;
169
+ } else {
170
+ gpr_count += count;
171
+ gpr_vec |= !vec_count;
172
+ }
173
+ });
174
+
175
+ // Pass mixed float-integer structs in one GPR and one FP register
176
+ if (gpr_count == 1 && vec_count == 1 && gpr_avail > 0 && vec_avail) {
177
+ ret.method = gpr_vec ? AbiMethod::GprVec : AbiMethod::VecGpr;
178
+ ret.gpr_index = gpr_index;
179
+ ret.vec_index = vec_index;
180
+
181
+ gpr_index++;
182
+ vec_index++;
183
+
184
+ return ret;
185
+ }
186
+
187
+ // HFA rules
188
+ if (vec_count && !gpr_count && vec_count <= vec_avail) {
189
+ ret.method = (vec_count > 1) ? AbiMethod::VecVec : AbiMethod::Vec;
190
+ ret.vec_index = vec_index;
191
+
192
+ vec_index += vec_count;
193
+
194
+ return ret;
195
+ }
196
+ }
197
+ #elif defined(__riscv_float_abi_single)
198
+ #error The RISC-V single-precision float ABI (LP64F) is not supported
199
+ #elif defined(__riscv_float_abi_soft)
200
+ // Use integer conventions
201
+ #else
202
+ #error Unknown or unsupported floating-point ABI
203
+ #endif
204
+
205
+ // Default case: GPR, GPR-stack or stack if no GPR is left
206
+ if (type->size > 8 && gpr_avail == 1) {
207
+ ret.method = AbiMethod::GprStack;
208
+ ret.gpr_index = gpr_index;
209
+
210
+ gpr_index = stack_start + 1;
211
+ } else if (type->size > 8) {
212
+ ret.method = AbiMethod::GprGpr;
213
+ ret.gpr_index = gpr_index;
214
+
215
+ gpr_index += 2;
216
+ } else {
217
+ ret.method = AbiMethod::Gpr;
218
+ ret.gpr_index = gpr_index;
219
+
220
+ gpr_index++;
221
+ }
222
+
223
+ // Switch to stack once GPRs are exhausted
224
+ if (gpr_index >= gpr_max && gpr_index < stack_start) {
225
+ gpr_index = stack_start;
226
+ };
227
+
228
+ return ret;
229
+ }
230
+
231
+ bool AnalyseFunction(Napi::Env, InstanceData *instance, FunctionInfo *func)
232
+ {
233
+ // Handle return value
234
+ {
235
+ ClassAnalyser analyser(0, 2, 0, 2);
236
+ ClassResult ret = analyser.Analyse(func->ret.type, false);
237
+
238
+ func->ret.abi.method = ret.method;
239
+
240
+ switch (ret.method) {
241
+ case AbiMethod::Memory: {} break;
242
+
243
+ case AbiMethod::Gpr: {
244
+ func->ret.abi.offsets[0] = offsetof(BackRegisters, a0);
245
+ } break;
246
+ case AbiMethod::GprGpr: {
247
+ func->ret.abi.offsets[0] = offsetof(BackRegisters, a0);
248
+ func->ret.abi.offsets[1] = offsetof(BackRegisters, a1);
249
+ } break;
250
+ case AbiMethod::Vec: {
251
+ func->ret.abi.offsets[0] = offsetof(BackRegisters, fa0);
252
+ } break;
253
+ case AbiMethod::VecVec: {
254
+ func->ret.abi.offsets[0] = offsetof(BackRegisters, fa0);
255
+ func->ret.abi.offsets[1] = offsetof(BackRegisters, fa1);
256
+
257
+ func->ret.type = ReshapeType(instance, func->ret.type, 8, (int)TypeFlag::FillWithOnes);
258
+ } break;
259
+ case AbiMethod::GprVec: {
260
+ func->ret.abi.offsets[0] = offsetof(BackRegisters, a0);
261
+ func->ret.abi.offsets[1] = offsetof(BackRegisters, fa0);
262
+
263
+ func->ret.type = ReshapeType(instance, func->ret.type, 8, (int)TypeFlag::FillWithOnes);
264
+ } break;
265
+ case AbiMethod::VecGpr: {
266
+ func->ret.abi.offsets[0] = offsetof(BackRegisters, fa0);
267
+ func->ret.abi.offsets[1] = offsetof(BackRegisters, a0);
268
+
269
+ func->ret.type = ReshapeType(instance, func->ret.type, 8, (int)TypeFlag::FillWithOnes);
270
+ } break;
271
+
272
+ case AbiMethod::GprStack: { K_UNREACHABLE(); } break;
273
+ }
274
+ }
275
+
276
+ // Handle parameters
277
+ {
278
+ int gpr_result = (func->ret.abi.method == AbiMethod::Memory);
279
+ ClassAnalyser analyser(gpr_result, 8, 0, 8, 18);
280
+
281
+ for (ParameterInfo &param: func->parameters) {
282
+ ClassResult ret = analyser.Analyse(param.type, param.variadic);
283
+
284
+ param.abi.method = ret.method;
285
+
286
+ switch (param.abi.method) {
287
+ case AbiMethod::Memory: {
288
+ param.abi.offsets[0] = 8 * (0 + ret.gpr_index);
289
+ } break;
290
+
291
+ case AbiMethod::Gpr: {
292
+ param.abi.offsets[0] = 8 * (0 + ret.gpr_index);
293
+ param.abi.offsets[1] = 8 * (0 + ret.gpr_index);
294
+ } break;
295
+ case AbiMethod::GprGpr: {
296
+ param.abi.offsets[0] = 8 * (0 + ret.gpr_index);
297
+ param.abi.offsets[1] = 8 * (1 + ret.gpr_index);
298
+ } break;
299
+ case AbiMethod::Vec: {
300
+ param.abi.offsets[0] = 8 * (8 + ret.vec_index);
301
+ param.abi.offsets[1] = 8 * (8 + ret.vec_index);
302
+ } break;
303
+ case AbiMethod::VecVec: {
304
+ param.type = ReshapeType(instance, param.type, 8, (int)TypeFlag::FillWithOnes);
305
+
306
+ param.abi.offsets[0] = 8 * (8 + ret.vec_index);
307
+ param.abi.offsets[1] = 8 * (9 + ret.vec_index);
308
+ } break;
309
+ case AbiMethod::GprVec: {
310
+ param.type = ReshapeType(instance, param.type, 8, (int)TypeFlag::FillWithOnes);
311
+
312
+ param.abi.offsets[0] = 8 * (0 + ret.gpr_index);
313
+ param.abi.offsets[1] = 8 * (8 + ret.vec_index);
314
+ } break;
315
+ case AbiMethod::VecGpr: {
316
+ param.type = ReshapeType(instance, param.type, 8, (int)TypeFlag::FillWithOnes);
317
+
318
+ param.abi.offsets[0] = 8 * (8 + ret.vec_index);
319
+ param.abi.offsets[1] = 8 * (0 + ret.gpr_index);
320
+ } break;
321
+ case AbiMethod::GprStack: {
322
+ param.abi.offsets[0] = 8 * (0 + ret.gpr_index);
323
+ param.abi.offsets[1] = 18 * 8;
324
+ } break;
325
+ }
326
+
327
+ if (param.type->primitive == PrimitiveKind::Record || param.type->primitive == PrimitiveKind::Union) {
328
+ AbiOpcode code = (param.abi.method != AbiMethod::Memory) ? AbiOpcode::PushAggregateReg : AbiOpcode::PushAggregateMem;
329
+
330
+ func->sync.Append({ .op = Code2Op(code), .a = param.offset, .b1 = (int16_t)param.abi.offsets[0], .b2 = (int16_t)param.abi.offsets[1], .type = param.type });
331
+ func->async.Append({ .op = Code2Op(code), .a = param.offset, .b1 = (int16_t)param.abi.offsets[0], .b2 = (int16_t)param.abi.offsets[1], .type = param.type });
332
+ } else {
333
+ int delta = (int)AbiOpcode::PushVoid - (int)PrimitiveKind::Void;
334
+ AbiOpcode code = (AbiOpcode)((int)param.type->primitive + delta);
335
+
336
+ func->sync.Append({ .op = Code2Op(code), .a = param.offset, .b1 = (int16_t)param.abi.offsets[0], .b2 = (int16_t)param.directions, .type = param.type });
337
+ func->async.Append({ .op = Code2Op(code), .a = param.offset, .b1 = (int16_t)param.abi.offsets[0], .b2 = (int16_t)param.directions, .type = param.type });
338
+ }
339
+ }
340
+
341
+ func->stk_size = AlignLen((10 + std::max(8, analyser.GprCount())) * 8, 16);
342
+ func->forward_fp = analyser.VecCount();
343
+ }
344
+
345
+ func->async.Append({ .op = Code2Op(AbiOpcode::Yield) });
346
+
347
+ switch (func->ret.type->primitive) {
348
+ case PrimitiveKind::Void:
349
+ case PrimitiveKind::Bool:
350
+ case PrimitiveKind::Int8:
351
+ case PrimitiveKind::UInt8:
352
+ case PrimitiveKind::Int16:
353
+ case PrimitiveKind::Int16S:
354
+ case PrimitiveKind::UInt16:
355
+ case PrimitiveKind::UInt16S:
356
+ case PrimitiveKind::Int32:
357
+ case PrimitiveKind::Int32S:
358
+ case PrimitiveKind::UInt32:
359
+ case PrimitiveKind::UInt32S:
360
+ case PrimitiveKind::Int64:
361
+ case PrimitiveKind::Int64S:
362
+ case PrimitiveKind::UInt64:
363
+ case PrimitiveKind::UInt64S:
364
+ case PrimitiveKind::String:
365
+ case PrimitiveKind::String16:
366
+ case PrimitiveKind::String32: {
367
+ if (func->forward_fp) {
368
+ int delta = (int)AbiOpcode::RunVoidX - (int)PrimitiveKind::Void;
369
+ AbiOpcode run = (AbiOpcode)((int)func->ret.type->primitive + delta);
370
+
371
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
372
+ } else {
373
+ int delta = (int)AbiOpcode::RunVoid - (int)PrimitiveKind::Void;
374
+ AbiOpcode run = (AbiOpcode)((int)func->ret.type->primitive + delta);
375
+
376
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
377
+ }
378
+
379
+ // Async
380
+ {
381
+ int delta = (int)AbiOpcode::ReturnVoid - (int)PrimitiveKind::Void;
382
+ AbiOpcode call = func->forward_fp ? AbiOpcode::CallGGX : AbiOpcode::CallGG;
383
+ AbiOpcode ret = (AbiOpcode)((int)func->ret.type->primitive + delta);
384
+
385
+ func->async.Append({ .op = Code2Op(call) });
386
+ func->async.Append({ .op = Code2Op(ret), .type = func->ret.type });
387
+ }
388
+ } break;
389
+
390
+ case PrimitiveKind::Pointer: {
391
+ AbiOpcode run = func->forward_fp ? AbiOpcode::RunPointerX : AbiOpcode::RunPointer;
392
+ AbiOpcode call = func->forward_fp ? AbiOpcode::CallGGX : AbiOpcode::CallGG;
393
+
394
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type->ref.type });
395
+ func->async.Append({ .op = Code2Op(call) });
396
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnPointer), .type = func->ret.type->ref.type });
397
+ } break;
398
+ case PrimitiveKind::Callback: {
399
+ AbiOpcode run = func->forward_fp ? AbiOpcode::RunCallbackX : AbiOpcode::RunCallback;
400
+ AbiOpcode call = func->forward_fp ? AbiOpcode::CallGGX : AbiOpcode::CallGG;
401
+
402
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
403
+ func->async.Append({ .op = Code2Op(call) });
404
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnCallback), .type = func->ret.type });
405
+ } break;
406
+
407
+ case PrimitiveKind::Record:
408
+ case PrimitiveKind::Union: {
409
+ switch (func->ret.abi.method) {
410
+ case AbiMethod::Memory: {
411
+ AbiOpcode run = func->forward_fp ? AbiOpcode::RunAggregateMemX : AbiOpcode::RunAggregateMem;
412
+ AbiOpcode call = func->forward_fp ? AbiOpcode::CallStackX : AbiOpcode::CallStack;
413
+
414
+ func->sync.Append({ .op = Code2Op(run), .a = (int32_t)func->ret.type->size, .type = func->ret.type });
415
+ func->async.Append({ .op = Code2Op(call), .a = (int32_t)func->ret.type->size });
416
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnAggregateMem), .type = func->ret.type });
417
+ } break;
418
+
419
+ case AbiMethod::Gpr:
420
+ case AbiMethod::GprGpr:
421
+ case AbiMethod::GprStack: {
422
+ AbiOpcode run = func->forward_fp ? AbiOpcode::RunAggregateGGX : AbiOpcode::RunAggregateGG;
423
+ AbiOpcode call = func->forward_fp ? AbiOpcode::CallGGX : AbiOpcode::CallGG;
424
+
425
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
426
+ func->async.Append({ .op = Code2Op(call) });
427
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnAggregateReg), .type = func->ret.type });
428
+ } break;
429
+ case AbiMethod::Vec:
430
+ case AbiMethod::VecVec: {
431
+ AbiOpcode run = func->forward_fp ? AbiOpcode::RunAggregateDDX : AbiOpcode::RunAggregateDD;
432
+ AbiOpcode call = func->forward_fp ? AbiOpcode::CallDDX : AbiOpcode::CallDD;
433
+
434
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
435
+ func->async.Append({ .op = Code2Op(call) });
436
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnAggregateReg), .type = func->ret.type });
437
+ } break;
438
+ case AbiMethod::GprVec: {
439
+ AbiOpcode run = func->forward_fp ? AbiOpcode::RunAggregateGDX : AbiOpcode::RunAggregateGD;
440
+ AbiOpcode call = func->forward_fp ? AbiOpcode::CallGDX : AbiOpcode::CallGD;
441
+
442
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
443
+ func->async.Append({ .op = Code2Op(call) });
444
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnAggregateReg), .type = func->ret.type });
445
+ } break;
446
+ case AbiMethod::VecGpr: {
447
+ AbiOpcode run = func->forward_fp ? AbiOpcode::RunAggregateDGX : AbiOpcode::RunAggregateDG;
448
+ AbiOpcode call = func->forward_fp ? AbiOpcode::CallDGX : AbiOpcode::CallDG;
449
+
450
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
451
+ func->async.Append({ .op = Code2Op(call) });
452
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnAggregateReg), .type = func->ret.type });
453
+ } break;
454
+ }
455
+ } break;
456
+ case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
457
+
458
+ case PrimitiveKind::Float32: {
459
+ AbiOpcode run = func->forward_fp ? AbiOpcode::RunFloat32X : AbiOpcode::RunFloat32;
460
+ AbiOpcode call = func->forward_fp ? AbiOpcode::CallFX : AbiOpcode::CallF;
461
+
462
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
463
+ func->async.Append({ .op = Code2Op(call) });
464
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnFloat32), .type = func->ret.type });
465
+ } break;
466
+ case PrimitiveKind::Float64: {
467
+ AbiOpcode run = func->forward_fp ? AbiOpcode::RunFloat64X : AbiOpcode::RunFloat64;
468
+ AbiOpcode call = func->forward_fp ? AbiOpcode::CallDDX : AbiOpcode::CallDD;
469
+
470
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
471
+ func->async.Append({ .op = Code2Op(call) });
472
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnFloat64), .type = func->ret.type });
473
+ } break;
474
+
475
+ case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
476
+ }
477
+
478
+ return true;
479
+ }
480
+
481
+ #pragma GCC diagnostic push
482
+ #pragma GCC diagnostic ignored "-Wunused-parameter"
483
+
484
+ namespace {
485
+ #if defined(MUST_TAIL)
486
+ #define OP(Code) \
487
+ PRESERVE_NONE napi_value Handle ## Code(CallData *call, napi_value *args, uint8_t *base, const AbiInstruction *inst)
488
+ #define NEXT() \
489
+ do { \
490
+ const AbiInstruction *next = inst + 1; \
491
+ MUST_TAIL return ((ForwardFunc *)next->op)(call, args, base, next); \
492
+ } while (false)
493
+ #else
494
+ #warning Falling back to inlining instead of tail calls (missing attributes)
495
+
496
+ #define OP(Code) \
497
+ case (int)AbiOpcode::Code:
498
+ #define NEXT() \
499
+ break
500
+
501
+ napi_value RunLoop(CallData *call, napi_value *args, uint8_t *base, const AbiInstruction *inst)
502
+ {
503
+ for (;; ++inst) {
504
+ switch ((intptr_t)inst->op) {
505
+ #endif
506
+
507
+ #define INTEGER(CType) \
508
+ do { \
509
+ CType v; \
510
+ if (!TryNumber(call->env, args[inst->a], &v)) [[unlikely]] { \
511
+ ThrowError<Napi::TypeError>(call->env, "Unexpected %1 value, expected number", GetValueType(call->instance, args[inst->a])); \
512
+ return call->env.Null(); \
513
+ } \
514
+ \
515
+ *(uint64_t *)(base + inst->b1) = (uint64_t)v; \
516
+ } while (false)
517
+ #define INTEGER_SWAP(CType) \
518
+ do { \
519
+ CType v; \
520
+ if (!TryNumber(call->env, args[inst->a], &v)) [[unlikely]] { \
521
+ ThrowError<Napi::TypeError>(call->env, "Unexpected %1 value, expected number", GetValueType(call->instance, args[inst->a])); \
522
+ return call->env.Null(); \
523
+ } \
524
+ \
525
+ *(uint64_t *)(base + inst->b1) = (uint64_t)ReverseBytes(v); \
526
+ } while (false)
527
+
528
+ OP(PushVoid) { K_UNREACHABLE(); return call->env.Null(); }
529
+ OP(PushBool) {
530
+ bool b;
531
+ if (napi_get_value_bool(call->env, args[inst->a], &b) != napi_ok) [[unlikely]] {
532
+ ThrowError<Napi::TypeError>(call->env, "Unexpected %1 value, expected boolean", GetValueType(call->instance, args[inst->a]));
533
+ return call->env.Null();
534
+ }
535
+
536
+ *(uint64_t *)(base + inst->b1) = (uint64_t)b;
537
+
538
+ NEXT();
539
+ }
540
+ OP(PushInt8) { INTEGER(int8_t); NEXT(); }
541
+ OP(PushUInt8) { INTEGER(uint8_t); NEXT(); }
542
+ OP(PushInt16) { INTEGER(int16_t); NEXT(); }
543
+ OP(PushInt16S) { INTEGER_SWAP(int16_t); NEXT(); }
544
+ OP(PushUInt16) { INTEGER(uint16_t); NEXT(); }
545
+ OP(PushUInt16S) { INTEGER_SWAP(uint16_t); NEXT(); }
546
+ OP(PushInt32) { INTEGER(int32_t); NEXT(); }
547
+ OP(PushInt32S) { INTEGER_SWAP(int32_t); NEXT(); }
548
+ OP(PushUInt32) { INTEGER(uint32_t); NEXT(); }
549
+ OP(PushUInt32S) { INTEGER_SWAP(uint32_t); NEXT(); }
550
+ OP(PushInt64) { INTEGER(int64_t); NEXT(); }
551
+ OP(PushInt64S) { INTEGER_SWAP(int64_t); NEXT(); }
552
+ OP(PushUInt64) { INTEGER(int64_t); NEXT(); }
553
+ OP(PushUInt64S) { INTEGER_SWAP(int64_t); NEXT(); }
554
+ OP(PushString) {
555
+ const char *str;
556
+ if (!call->PushString(args[inst->a], inst->b2, &str)) [[unlikely]]
557
+ return call->env.Null();
558
+
559
+ *(const char **)(base + inst->b1) = str;
560
+
561
+ NEXT();
562
+ }
563
+ OP(PushString16) {
564
+ const char16_t *str16;
565
+ if (!call->PushString16(args[inst->a], inst->b2, &str16)) [[unlikely]]
566
+ return call->env.Null();
567
+
568
+ *(const char16_t **)(base + inst->b1) = str16;
569
+
570
+ NEXT();
571
+ }
572
+ OP(PushString32) {
573
+ const char32_t *str32;
574
+ if (!call->PushString32(args[inst->a], inst->b2, &str32)) [[unlikely]]
575
+ return call->env.Null();
576
+
577
+ *(const char32_t **)(base + inst->b1) = str32;
578
+
579
+ NEXT();
580
+ }
581
+ OP(PushPointer) {
582
+ void *ptr;
583
+ if (!call->PushPointer(args[inst->a], inst->type, inst->b2, &ptr)) [[unlikely]]
584
+ return call->env.Null();
585
+
586
+ *(void **)(base + inst->b1) = ptr;
587
+
588
+ NEXT();
589
+ }
590
+ OP(PushRecord) { K_UNREACHABLE(); return call->env.Null(); }
591
+ OP(PushUnion) { K_UNREACHABLE(); return call->env.Null(); }
592
+ OP(PushArray) { K_UNREACHABLE(); return call->env.Null(); }
593
+ OP(PushFloat32) {
594
+ float f;
595
+ if (!TryNumber(call->env, args[inst->a], &f)) [[unlikely]] {
596
+ ThrowError<Napi::TypeError>(call->env, "Unexpected %1 value, expected number", GetValueType(call->instance, args[inst->a]));
597
+ return call->env.Null();
598
+ }
599
+
600
+ memset(base + inst->b1, 0xFF, 8);
601
+ memcpy(base + inst->b1, &f, 4);
602
+
603
+ NEXT();
604
+ }
605
+ OP(PushFloat64) {
606
+ double d;
607
+ if (!TryNumber(call->env, args[inst->a], &d)) [[unlikely]] {
608
+ ThrowError<Napi::TypeError>(call->env, "Unexpected %1 value, expected number", GetValueType(call->instance, args[inst->a]));
609
+ return call->env.Null();
610
+ }
611
+
612
+ *(double *)(base + inst->b1) = d;
613
+
614
+ NEXT();
615
+ }
616
+ OP(PushCallback) {
617
+ void *ptr;
618
+ if (!call->PushCallback(args[inst->a], inst->type, &ptr)) [[unlikely]]
619
+ return call->env.Null();
620
+
621
+ *(void **)(base + inst->b1) = ptr;
622
+
623
+ NEXT();
624
+ }
625
+ OP(PushPrototype) { K_UNREACHABLE(); return call->env.Null(); }
626
+ OP(PushAggregateReg) {
627
+ napi_value arg = args[inst->a];
628
+
629
+ uint64_t buf[2] = {};
630
+ if (!call->PushObject(arg, inst->type, (uint8_t *)buf)) [[unlikely]]
631
+ return call->env.Null();
632
+
633
+ // The second part might be useless (if object fits in one register), in
634
+ // which case the analysis code will put the same value in both offsets to
635
+ // make sure we don't overwrite something else. Well, if we copy the second
636
+ // part first, that is, as we do below.
637
+ *(uint64_t *)(base + inst->b2) = buf[1];
638
+ *(uint64_t *)(base + inst->b1) = buf[0];
639
+
640
+ NEXT();
641
+ }
642
+ OP(PushAggregateMem) {
643
+ napi_value arg = args[inst->a];
644
+
645
+ uint8_t *ptr = call->AllocHeap(inst->type->size);
646
+ *(uint8_t **)(base + inst->b1) = ptr;
647
+
648
+ if (!call->PushObject(arg, inst->type, ptr)) [[unlikely]]
649
+ return call->env.Null();
650
+
651
+ NEXT();
652
+ }
653
+
654
+ #undef INTEGER_SWAP
655
+ #undef INTEGER
656
+
657
+ #define WRAP(Expr) (call->DebugForward(), (Expr))
658
+
659
+ #define INTEGER(Suffix, CType) \
660
+ do { \
661
+ uint64_t a0 = WRAP(ForwardCall ## Suffix(call->native, base, &call->saved_sp).a0); \
662
+ return NewInt(call->env, (CType)a0); \
663
+ } while (false)
664
+ #define INTEGER_SWAP(Suffix, CType) \
665
+ do { \
666
+ uint64_t a0 = WRAP(ForwardCall ## Suffix(call->native, base, &call->saved_sp).a0); \
667
+ return NewInt(call->env, ReverseBytes((CType)a0)); \
668
+ } while (false)
669
+ #define DISPOSE(Ptr) \
670
+ do { \
671
+ if (inst->type->dispose) { \
672
+ inst->type->dispose(call->instance, inst->type, (Ptr)); \
673
+ } \
674
+ } while (false)
675
+
676
+ OP(RunVoid) {
677
+ WRAP(ForwardCallGG(call->native, base, &call->saved_sp));
678
+ return nullptr;
679
+ }
680
+ OP(RunBool) {
681
+ uint64_t a0 = WRAP(ForwardCallGG(call->native, base, &call->saved_sp).a0);
682
+ return Napi::Boolean::New(call->env, a0 & 0x1);
683
+ }
684
+ OP(RunInt8) { INTEGER(GG, int8_t); }
685
+ OP(RunUInt8) { INTEGER(GG, uint8_t); }
686
+ OP(RunInt16) { INTEGER(GG, int16_t); }
687
+ OP(RunInt16S) { INTEGER_SWAP(GG, int16_t); }
688
+ OP(RunUInt16) { INTEGER(GG, uint16_t); }
689
+ OP(RunUInt16S) { INTEGER_SWAP(GG, uint16_t); }
690
+ OP(RunInt32) { INTEGER(GG, int32_t); }
691
+ OP(RunInt32S) { INTEGER_SWAP(GG, int32_t); }
692
+ OP(RunUInt32) { INTEGER(GG, uint32_t); }
693
+ OP(RunUInt32S) { INTEGER_SWAP(GG, uint32_t); }
694
+ OP(RunInt64) { INTEGER(GG, int64_t); }
695
+ OP(RunInt64S) { INTEGER_SWAP(GG, int64_t); }
696
+ OP(RunUInt64) { INTEGER(GG, uint64_t); }
697
+ OP(RunUInt64S) { INTEGER_SWAP(GG, uint64_t); }
698
+ OP(RunString) {
699
+ uint64_t a0 = WRAP(ForwardCallGG(call->native, base, &call->saved_sp).a0);
700
+ napi_value value = NewString(call->env, (const char *)a0);
701
+ DISPOSE((void *)a0);
702
+ return value;
703
+ }
704
+ OP(RunString16) {
705
+ uint64_t a0 = WRAP(ForwardCallGG(call->native, base, &call->saved_sp).a0);
706
+ napi_value value = NewString(call->env, (const char16_t *)a0);
707
+ DISPOSE((void *)a0);
708
+ return value;
709
+ }
710
+ OP(RunString32) {
711
+ uint64_t a0 = WRAP(ForwardCallGG(call->native, base, &call->saved_sp).a0);
712
+ napi_value value = NewString(call->env, (const char32_t *)a0);
713
+ DISPOSE((void *)a0);
714
+ return value;
715
+ }
716
+ OP(RunPointer) {
717
+ uint64_t a0 = WRAP(ForwardCallGG(call->native, base, &call->saved_sp).a0);
718
+ napi_value value = WrapPointer(call->env, inst->type, (void *)a0);
719
+ DISPOSE((void *)a0);
720
+ return value;
721
+ }
722
+ OP(RunCallback) {
723
+ uint64_t a0 = WRAP(ForwardCallGG(call->native, base, &call->saved_sp).a0);
724
+ return WrapPointer(call->env, inst->type, (void *)a0);
725
+ }
726
+ OP(RunRecord) { K_UNREACHABLE(); return call->env.Null(); }
727
+ OP(RunUnion) { K_UNREACHABLE(); return call->env.Null(); }
728
+ OP(RunArray) { K_UNREACHABLE(); return call->env.Null(); }
729
+ OP(RunFloat32) {
730
+ float f = WRAP(ForwardCallF(call->native, base, &call->saved_sp));
731
+ return NewFloat(call->env, f);
732
+ }
733
+ OP(RunFloat64) {
734
+ double d = WRAP(ForwardCallDD(call->native, base, &call->saved_sp).fa0);
735
+ return NewFloat(call->env, d);
736
+ }
737
+ OP(RunPrototype) { K_UNREACHABLE(); return call->env.Null(); }
738
+ OP(RunAggregateGG) {
739
+ auto ret = WRAP(ForwardCallGG(call->native, base, &call->saved_sp));
740
+ return DecodeObject(call->instance, (const uint8_t *)&ret, inst->type);
741
+ }
742
+ OP(RunAggregateDD) {
743
+ auto ret = WRAP(ForwardCallDD(call->native, base, &call->saved_sp));
744
+ return DecodeObject(call->instance, (const uint8_t *)&ret, inst->type);
745
+ }
746
+ OP(RunAggregateGD) {
747
+ auto ret = WRAP(ForwardCallGD(call->native, base, &call->saved_sp));
748
+ return DecodeObject(call->instance, (const uint8_t *)&ret, inst->type);
749
+ }
750
+ OP(RunAggregateDG) {
751
+ auto ret = WRAP(ForwardCallDG(call->native, base, &call->saved_sp));
752
+ return DecodeObject(call->instance, (const uint8_t *)&ret, inst->type);
753
+ }
754
+ OP(RunAggregateMem) {
755
+ uint8_t *ptr = call->AllocHeap(inst->a);
756
+ *(uint8_t **)base = ptr;
757
+ WRAP(ForwardCallGG(call->native, base, &call->saved_sp));
758
+ return DecodeObject(call->instance, ptr, inst->type);
759
+ }
760
+ OP(RunVoidX) {
761
+ WRAP(ForwardCallGGX(call->native, base, &call->saved_sp));
762
+ return nullptr;
763
+ }
764
+ OP(RunBoolX) {
765
+ uint64_t a0 = WRAP(ForwardCallGGX(call->native, base, &call->saved_sp).a0);
766
+ return Napi::Boolean::New(call->env, a0 & 0x1);
767
+ }
768
+ OP(RunInt8X) { INTEGER(GGX, int8_t); }
769
+ OP(RunUInt8X) { INTEGER(GGX, uint8_t); }
770
+ OP(RunInt16X) { INTEGER(GGX, int16_t); }
771
+ OP(RunInt16SX) { INTEGER_SWAP(GGX, int16_t); }
772
+ OP(RunUInt16X) { INTEGER(GGX, uint16_t); }
773
+ OP(RunUInt16SX) { INTEGER_SWAP(GGX, uint16_t); }
774
+ OP(RunInt32X) { INTEGER(GGX, int32_t); }
775
+ OP(RunInt32SX) { INTEGER_SWAP(GGX, int32_t); }
776
+ OP(RunUInt32X) { INTEGER(GGX, uint32_t); }
777
+ OP(RunUInt32SX) { INTEGER_SWAP(GGX, uint32_t); }
778
+ OP(RunInt64X) { INTEGER(GGX, int64_t); }
779
+ OP(RunInt64SX) { INTEGER_SWAP(GGX, int64_t); }
780
+ OP(RunUInt64X) { INTEGER(GGX, uint64_t); }
781
+ OP(RunUInt64SX) { INTEGER_SWAP(GGX, uint64_t); }
782
+ OP(RunStringX) {
783
+ uint64_t a0 = WRAP(ForwardCallGGX(call->native, base, &call->saved_sp).a0);
784
+ napi_value value = NewString(call->env, (const char *)a0);
785
+ DISPOSE((void *)a0);
786
+ return value;
787
+ }
788
+ OP(RunString16X) {
789
+ uint64_t a0 = WRAP(ForwardCallGGX(call->native, base, &call->saved_sp).a0);
790
+ napi_value value = NewString(call->env, (const char16_t *)a0);
791
+ DISPOSE((void *)a0);
792
+ return value;
793
+ }
794
+ OP(RunString32X) {
795
+ uint64_t a0 = WRAP(ForwardCallGGX(call->native, base, &call->saved_sp).a0);
796
+ napi_value value = NewString(call->env, (const char32_t *)a0);
797
+ DISPOSE((void *)a0);
798
+ return value;
799
+ }
800
+ OP(RunPointerX) {
801
+ uint64_t a0 = WRAP(ForwardCallGGX(call->native, base, &call->saved_sp).a0);
802
+ napi_value value = WrapPointer(call->env, inst->type, (void *)a0);
803
+ DISPOSE((void *)a0);
804
+ return value;
805
+ }
806
+ OP(RunCallbackX) {
807
+ uint64_t a0 = WRAP(ForwardCallGGX(call->native, base, &call->saved_sp).a0);
808
+ return WrapPointer(call->env, inst->type, (void *)a0);
809
+ }
810
+ OP(RunRecordX) { K_UNREACHABLE(); return call->env.Null(); }
811
+ OP(RunUnionX) { K_UNREACHABLE(); return call->env.Null(); }
812
+ OP(RunArrayX) { K_UNREACHABLE(); return call->env.Null(); }
813
+ OP(RunFloat32X) {
814
+ float f = WRAP(ForwardCallFX(call->native, base, &call->saved_sp));
815
+ return NewFloat(call->env, f);
816
+ }
817
+ OP(RunFloat64X) {
818
+ double d = WRAP(ForwardCallDDX(call->native, base, &call->saved_sp).fa0);
819
+ return NewFloat(call->env, d);
820
+ }
821
+ OP(RunPrototypeX) { K_UNREACHABLE(); return call->env.Null(); }
822
+ OP(RunAggregateGGX) {
823
+ auto ret = WRAP(ForwardCallGGX(call->native, base, &call->saved_sp));
824
+ return DecodeObject(call->instance, (const uint8_t *)&ret, inst->type);
825
+ }
826
+ OP(RunAggregateDDX) {
827
+ auto ret = WRAP(ForwardCallDDX(call->native, base, &call->saved_sp));
828
+ return DecodeObject(call->instance, (const uint8_t *)&ret, inst->type);
829
+ }
830
+ OP(RunAggregateGDX) {
831
+ auto ret = WRAP(ForwardCallGDX(call->native, base, &call->saved_sp));
832
+ return DecodeObject(call->instance, (const uint8_t *)&ret, inst->type);
833
+ }
834
+ OP(RunAggregateDGX) {
835
+ auto ret = WRAP(ForwardCallDGX(call->native, base, &call->saved_sp));
836
+ return DecodeObject(call->instance, (const uint8_t *)&ret, inst->type);
837
+ }
838
+ OP(RunAggregateMemX) {
839
+ uint8_t *ptr = call->AllocHeap(inst->a);
840
+ *(uint8_t **)base = ptr;
841
+ WRAP(ForwardCallGGX(call->native, base, &call->saved_sp));
842
+ return DecodeObject(call->instance, ptr, inst->type);
843
+ }
844
+
845
+ #undef DISPOSE
846
+ #undef INTEGER_SWAP
847
+ #undef INTEGER
848
+
849
+ #define CALL(Suffix) \
850
+ do { \
851
+ auto ret = WRAP(ForwardCall ## Suffix(call->native, base, &call->saved_sp)); \
852
+ memcpy(base, &ret, K_SIZE(ret)); \
853
+ } while (false)
854
+ #define DISPOSE() \
855
+ do { \
856
+ if (inst->type->dispose) { \
857
+ void *ptr = *(void **)base; \
858
+ inst->type->dispose(call->instance, inst->type, ptr); \
859
+ } \
860
+ } while (false)
861
+ #define INTEGER(CType) \
862
+ do { \
863
+ uint64_t a0 = *(uint64_t *)base; \
864
+ return NewInt(call->env, (CType)a0); \
865
+ } while (false)
866
+ #define INTEGER_SWAP(CType) \
867
+ do { \
868
+ uint64_t a0 = *(uint64_t *)base; \
869
+ return NewInt(call->env, ReverseBytes((CType)a0)); \
870
+ } while (false)
871
+
872
+ OP(Yield) {
873
+ call->async_ip = inst + 1;
874
+ return nullptr;
875
+ }
876
+
877
+ OP(CallGG) { CALL(GG); return nullptr; }
878
+ OP(CallF) { CALL(F); return nullptr; }
879
+ OP(CallDG) { CALL(DG); return nullptr; }
880
+ OP(CallGD) { CALL(GD); return nullptr; }
881
+ OP(CallDD) { CALL(DD); return nullptr; }
882
+ OP(CallStack) {
883
+ uint8_t *ptr = call->AllocHeap(inst->a);
884
+ *(uint8_t **)base = ptr;
885
+ CALL(GG);
886
+ return nullptr;
887
+ }
888
+ OP(CallGGX) { CALL(GGX); return nullptr; }
889
+ OP(CallFX) { CALL(FX); return nullptr; }
890
+ OP(CallDGX) { CALL(DGX); return nullptr; }
891
+ OP(CallGDX) { CALL(GDX); return nullptr; }
892
+ OP(CallDDX) { CALL(DDX); return nullptr; }
893
+ OP(CallStackX) {
894
+ uint8_t *ptr = call->AllocHeap(inst->a);
895
+ *(uint8_t **)base = ptr;
896
+ CALL(GGX);
897
+ return nullptr;
898
+ }
899
+
900
+ OP(ReturnVoid) { return nullptr; }
901
+ OP(ReturnBool) {
902
+ uint64_t a0 = *(uint64_t *)base;
903
+ return Napi::Boolean::New(call->env, a0 & 0x1);
904
+ }
905
+ OP(ReturnInt8) { INTEGER(int8_t); }
906
+ OP(ReturnUInt8) { INTEGER(uint8_t); }
907
+ OP(ReturnInt16) { INTEGER(int16_t); }
908
+ OP(ReturnInt16S) { INTEGER_SWAP(int16_t); }
909
+ OP(ReturnUInt16) { INTEGER(uint16_t); }
910
+ OP(ReturnUInt16S) { INTEGER_SWAP(uint16_t); }
911
+ OP(ReturnInt32) { INTEGER(int32_t); }
912
+ OP(ReturnInt32S) { INTEGER_SWAP(int32_t); }
913
+ OP(ReturnUInt32) { INTEGER(uint32_t); }
914
+ OP(ReturnUInt32S) { INTEGER_SWAP(uint32_t); }
915
+ OP(ReturnInt64) { INTEGER(int64_t); }
916
+ OP(ReturnInt64S) { INTEGER_SWAP(int64_t); }
917
+ OP(ReturnUInt64) { INTEGER(uint64_t); }
918
+ OP(ReturnUInt64S) { INTEGER_SWAP(uint64_t); }
919
+ OP(ReturnString) {
920
+ uint64_t a0 = *(uint64_t *)base;
921
+ napi_value value = NewString(call->env, (const char *)a0);
922
+ DISPOSE();
923
+ return value;
924
+ }
925
+ OP(ReturnString16) {
926
+ uint64_t a0 = *(uint64_t *)base;
927
+ napi_value value = NewString(call->env, (const char16_t *)a0);
928
+ DISPOSE();
929
+ return value;
930
+ }
931
+ OP(ReturnString32) {
932
+ uint64_t a0 = *(uint64_t *)base;
933
+ napi_value value = NewString(call->env, (const char32_t *)a0);
934
+ DISPOSE();
935
+ return value;
936
+ }
937
+ OP(ReturnPointer) {
938
+ uint64_t a0 = *(uint64_t *)base;
939
+ napi_value value = WrapPointer(call->env, inst->type, (void *)a0);
940
+ DISPOSE();
941
+ return value;
942
+ }
943
+ OP(ReturnCallback) {
944
+ uint64_t a0 = *(uint64_t *)base;
945
+ return WrapPointer(call->env, inst->type, (void *)a0);
946
+ }
947
+ OP(ReturnRecord) { K_UNREACHABLE(); return call->env.Null(); }
948
+ OP(ReturnUnion) { K_UNREACHABLE(); return call->env.Null(); }
949
+ OP(ReturnArray) { K_UNREACHABLE(); return call->env.Null(); }
950
+ OP(ReturnFloat32) {
951
+ float f = *(float *)base;
952
+ return NewFloat(call->env, f);
953
+ }
954
+ OP(ReturnFloat64) {
955
+ double d = *(double *)base;
956
+ return NewFloat(call->env, d);
957
+ }
958
+ OP(ReturnPrototype) { K_UNREACHABLE(); return call->env.Null(); }
959
+ OP(ReturnAggregateReg) { return DecodeObject(call->instance, (const uint8_t *)base, inst->type); }
960
+ OP(ReturnAggregateMem) {
961
+ uint64_t a0 = *(uint64_t *)base;
962
+ return DecodeObject(call->instance, (const uint8_t *)a0, inst->type);
963
+ }
964
+
965
+ #undef INTEGER_SWAP
966
+ #undef INTEGER
967
+ #undef DISPOSE
968
+ #undef CALL
969
+
970
+ #if defined(MUST_TAIL)
971
+ ForwardFunc *const ForwardDispatch[256] = {
972
+ #define PRIMITIVE(Name) HandlePush ## Name,
973
+ #include "../primitives.inc"
974
+ HandlePushAggregateReg,
975
+ HandlePushAggregateMem,
976
+ #define PRIMITIVE(Name) HandleRun ## Name,
977
+ #include "../primitives.inc"
978
+ HandleRunAggregateGG,
979
+ HandleRunAggregateDD,
980
+ HandleRunAggregateGD,
981
+ HandleRunAggregateDG,
982
+ HandleRunAggregateMem,
983
+ #define PRIMITIVE(Name) HandleRun ## Name ## X,
984
+ #include "../primitives.inc"
985
+ HandleRunAggregateGGX,
986
+ HandleRunAggregateDDX,
987
+ HandleRunAggregateGDX,
988
+ HandleRunAggregateDGX,
989
+ HandleRunAggregateMemX,
990
+ HandleYield,
991
+ HandleCallGG,
992
+ HandleCallF,
993
+ HandleCallDG,
994
+ HandleCallGD,
995
+ HandleCallDD,
996
+ HandleCallStack,
997
+ HandleCallGGX,
998
+ HandleCallFX,
999
+ HandleCallDGX,
1000
+ HandleCallGDX,
1001
+ HandleCallDDX,
1002
+ HandleCallStackX,
1003
+ #define PRIMITIVE(Name) HandleReturn ## Name,
1004
+ #include "../primitives.inc"
1005
+ HandleReturnAggregateReg,
1006
+ HandleReturnAggregateMem
1007
+ };
1008
+
1009
+ FORCE_INLINE napi_value RunLoop(CallData *call, napi_value *args, uint8_t *base, const AbiInstruction *inst)
1010
+ {
1011
+ return ((ForwardFunc *)inst->op)(call, args, base, inst);
1012
+ }
1013
+ #else
1014
+ }
1015
+ }
1016
+
1017
+ K_UNREACHABLE();
1018
+ }
1019
+ #endif
1020
+
1021
+ #undef WRAP
1022
+
1023
+ #undef NEXT
1024
+ #undef OP
1025
+ }
1026
+
1027
+ #pragma GCC diagnostic pop
1028
+
1029
+ napi_value CallData::Run(const FunctionInfo *func, napi_value *args)
1030
+ {
1031
+ uint8_t *base = AllocStack<uint8_t>(func->stk_size);
1032
+ if (!base) [[unlikely]]
1033
+ return env.Null();
1034
+
1035
+ const AbiInstruction *first = func->sync.ptr;
1036
+ return RunLoop(this, args, base, first);
1037
+ }
1038
+
1039
+ bool CallData::PrepareAsync(const FunctionInfo *func, napi_value *args)
1040
+ {
1041
+ uint8_t *base = AllocStack<uint8_t>(func->stk_size);
1042
+ if (!base) [[unlikely]]
1043
+ return env.Null();
1044
+ async_base = base;
1045
+
1046
+ const AbiInstruction *first = func->async.ptr;
1047
+ return !RunLoop(this, args, base, first); // Yield returns nullptr
1048
+ }
1049
+
1050
+ void CallData::ExecuteAsync()
1051
+ {
1052
+ const AbiInstruction *next = async_ip++;
1053
+ RunLoop(this, nullptr, async_base, next);
1054
+ }
1055
+
1056
+ napi_value CallData::EndAsync()
1057
+ {
1058
+ const AbiInstruction *next = async_ip++;
1059
+ return RunLoop(this, nullptr, async_base, next);
1060
+ }
1061
+
1062
+ void CallData::Relay(Size idx, uint8_t *sp)
1063
+ {
1064
+ TrampolineInfo *trampoline = &shared.trampolines[idx];
1065
+ const FunctionInfo *proto = trampoline->proto;
1066
+
1067
+ uint8_t *in_ptr = sp + 40;
1068
+ BackRegisters *out_reg = (BackRegisters *)sp;
1069
+
1070
+ K_DEFER_N(err_guard) {
1071
+ trampoline->state = -1;
1072
+ memset(out_reg, 0, K_SIZE(*out_reg));
1073
+ };
1074
+
1075
+ napi_value arguments[MaxParameters];
1076
+
1077
+ #define POP_INTEGER(CType) \
1078
+ do { \
1079
+ const uint8_t *src = in_ptr + param.abi.offsets[0]; \
1080
+ CType v = *(const CType *)src; \
1081
+ \
1082
+ arguments[i] = NewInt(env, v); \
1083
+ } while (false)
1084
+ #define POP_INTEGER_SWAP(CType) \
1085
+ do { \
1086
+ const uint8_t *src = in_ptr + param.abi.offsets[0]; \
1087
+ CType v = *(const CType *)src; \
1088
+ \
1089
+ arguments[i] = NewInt(env, ReverseBytes(v)); \
1090
+ } while (false)
1091
+
1092
+ // Convert to JS arguments
1093
+ for (Size i = 0; i < proto->parameters.len; i++) {
1094
+ const ParameterInfo &param = proto->parameters[i];
1095
+ K_ASSERT(param.directions >= 1 && param.directions <= 3);
1096
+
1097
+ switch (param.type->primitive) {
1098
+ case PrimitiveKind::Void: { K_UNREACHABLE(); } break;
1099
+
1100
+ case PrimitiveKind::Bool: {
1101
+ const uint8_t *src = in_ptr + param.abi.offsets[0];
1102
+ bool b = *(bool *)src;
1103
+
1104
+ arguments[i] = Napi::Boolean::New(env, b);
1105
+ } break;
1106
+
1107
+ case PrimitiveKind::Int8: { POP_INTEGER(int8_t); } break;
1108
+ case PrimitiveKind::UInt8: { POP_INTEGER(uint8_t); } break;
1109
+ case PrimitiveKind::Int16: { POP_INTEGER(int16_t); } break;
1110
+ case PrimitiveKind::Int16S: { POP_INTEGER_SWAP(int16_t); } break;
1111
+ case PrimitiveKind::UInt16: { POP_INTEGER(uint16_t); } break;
1112
+ case PrimitiveKind::UInt16S: { POP_INTEGER_SWAP(uint16_t); } break;
1113
+ case PrimitiveKind::Int32: { POP_INTEGER(int32_t); } break;
1114
+ case PrimitiveKind::Int32S: { POP_INTEGER_SWAP(int32_t); } break;
1115
+ case PrimitiveKind::UInt32: { POP_INTEGER(uint32_t); } break;
1116
+ case PrimitiveKind::UInt32S: { POP_INTEGER_SWAP(uint32_t); } break;
1117
+ case PrimitiveKind::Int64: { POP_INTEGER(int64_t); } break;
1118
+ case PrimitiveKind::Int64S: { POP_INTEGER_SWAP(int64_t); } break;
1119
+ case PrimitiveKind::UInt64: { POP_INTEGER(uint64_t); } break;
1120
+ case PrimitiveKind::UInt64S: { POP_INTEGER_SWAP(uint64_t); } break;
1121
+
1122
+ case PrimitiveKind::String: {
1123
+ const uint8_t *src = in_ptr + param.abi.offsets[0];
1124
+ const char *str = *(const char **)src;
1125
+
1126
+ arguments[i] = NewString(env, str);
1127
+
1128
+ if (param.type->dispose) {
1129
+ param.type->dispose(instance, param.type, str);
1130
+ }
1131
+ } break;
1132
+ case PrimitiveKind::String16: {
1133
+ const uint8_t *src = in_ptr + param.abi.offsets[0];
1134
+ const char16_t *str16 = *(const char16_t **)src;
1135
+
1136
+ arguments[i] = NewString(env, str16);
1137
+
1138
+ if (param.type->dispose) {
1139
+ param.type->dispose(instance, param.type, str16);
1140
+ }
1141
+ } break;
1142
+ case PrimitiveKind::String32: {
1143
+ const uint8_t *src = in_ptr + param.abi.offsets[0];
1144
+ const char32_t *str32 = *(const char32_t **)src;
1145
+
1146
+ arguments[i] = NewString(env, str32);
1147
+
1148
+ if (param.type->dispose) {
1149
+ param.type->dispose(instance, param.type, str32);
1150
+ }
1151
+ } break;
1152
+
1153
+ case PrimitiveKind::Pointer: {
1154
+ const uint8_t *src = in_ptr + param.abi.offsets[0];
1155
+ void *ptr2 = *(void **)src;
1156
+
1157
+ arguments[i] = WrapPointer(env, param.type->ref.type, ptr2);
1158
+
1159
+ if (param.type->dispose) {
1160
+ param.type->dispose(instance, param.type, ptr2);
1161
+ }
1162
+ } break;
1163
+
1164
+ case PrimitiveKind::Record:
1165
+ case PrimitiveKind::Union: {
1166
+ if (param.abi.method != AbiMethod::Memory) {
1167
+ uint8_t buf[16];
1168
+ memcpy(buf, in_ptr + param.abi.offsets[0], 8);
1169
+ memcpy(buf + 8, in_ptr + param.abi.offsets[1], 8);
1170
+
1171
+ arguments[i] = DecodeObject(instance, buf, param.type);
1172
+ } else {
1173
+ uint8_t *ptr = *(uint8_t **)(in_ptr + param.abi.offsets[0]);
1174
+ arguments[i] = DecodeObject(instance, ptr, param.type);
1175
+ }
1176
+ } break;
1177
+ case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
1178
+
1179
+ case PrimitiveKind::Float32: {
1180
+ const uint8_t *src = in_ptr + param.abi.offsets[0];
1181
+ float f = *(float *)src;
1182
+
1183
+ arguments[i] = NewFloat(env, f);
1184
+ } break;
1185
+ case PrimitiveKind::Float64: {
1186
+ const uint8_t *src = in_ptr + param.abi.offsets[0];
1187
+ double d = *(double *)src;
1188
+
1189
+ arguments[i] = NewFloat(env, d);
1190
+ } break;
1191
+
1192
+ case PrimitiveKind::Callback: {
1193
+ const uint8_t *src = in_ptr + param.abi.offsets[0];
1194
+ void *ptr2 = *(void **)src;
1195
+
1196
+ arguments[i] = WrapPointer(env, param.type->ref.type, ptr2);
1197
+ } break;
1198
+
1199
+ case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
1200
+ }
1201
+ }
1202
+
1203
+ #undef POP_INTEGER_SWAP
1204
+ #undef POP_INTEGER
1205
+
1206
+ const TypeInfo *type = proto->ret.type;
1207
+
1208
+ // We're ready, make the call!
1209
+ napi_value value = CallCallback(trampoline, arguments, proto->parameters.len);
1210
+
1211
+ if (!value) [[unlikely]]
1212
+ return;
1213
+
1214
+ #define RETURN_INTEGER(CType) \
1215
+ do { \
1216
+ CType v; \
1217
+ if (!TryNumber(env, value, &v)) [[unlikely]] { \
1218
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
1219
+ return; \
1220
+ } \
1221
+ \
1222
+ out_reg->a0 = (uint64_t)v; \
1223
+ } while (false)
1224
+ #define RETURN_INTEGER_SWAP(CType) \
1225
+ do { \
1226
+ CType v; \
1227
+ if (!TryNumber(env, value, &v)) [[unlikely]] { \
1228
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
1229
+ return; \
1230
+ } \
1231
+ \
1232
+ out_reg->a0 = (uint64_t)ReverseBytes(v); \
1233
+ } while (false)
1234
+
1235
+ // Convert the result
1236
+ switch (type->primitive) {
1237
+ case PrimitiveKind::Void: {} break;
1238
+ case PrimitiveKind::Bool: {
1239
+ bool b;
1240
+ if (napi_get_value_bool(env, value, &b) != napi_ok) [[unlikely]] {
1241
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected boolean", GetValueType(instance, value));
1242
+ return;
1243
+ }
1244
+
1245
+ out_reg->a0 = (uint64_t)b;
1246
+ } break;
1247
+ case PrimitiveKind::Int8: { RETURN_INTEGER(int8_t); } break;
1248
+ case PrimitiveKind::UInt8: { RETURN_INTEGER(uint8_t); } break;
1249
+ case PrimitiveKind::Int16: { RETURN_INTEGER(int16_t); } break;
1250
+ case PrimitiveKind::Int16S: { RETURN_INTEGER_SWAP(int16_t); } break;
1251
+ case PrimitiveKind::UInt16: { RETURN_INTEGER(uint16_t); } break;
1252
+ case PrimitiveKind::UInt16S: { RETURN_INTEGER_SWAP(uint16_t); } break;
1253
+ case PrimitiveKind::Int32: { RETURN_INTEGER(int32_t); } break;
1254
+ case PrimitiveKind::Int32S: { RETURN_INTEGER_SWAP(int32_t); } break;
1255
+ case PrimitiveKind::UInt32: { RETURN_INTEGER(uint32_t); } break;
1256
+ case PrimitiveKind::UInt32S: { RETURN_INTEGER_SWAP(uint32_t); } break;
1257
+ case PrimitiveKind::Int64: { RETURN_INTEGER(int64_t); } break;
1258
+ case PrimitiveKind::Int64S: { RETURN_INTEGER_SWAP(int64_t); } break;
1259
+ case PrimitiveKind::UInt64: { RETURN_INTEGER(uint64_t); } break;
1260
+ case PrimitiveKind::UInt64S: { RETURN_INTEGER_SWAP(uint64_t); } break;
1261
+ case PrimitiveKind::String: {
1262
+ const char *str;
1263
+ if (!PushString(value, 1, &str)) [[unlikely]]
1264
+ return;
1265
+
1266
+ out_reg->a0 = (uint64_t)str;
1267
+ } break;
1268
+ case PrimitiveKind::String16: {
1269
+ const char16_t *str16;
1270
+ if (!PushString16(value, 1, &str16)) [[unlikely]]
1271
+ return;
1272
+
1273
+ out_reg->a0 = (uint64_t)str16;
1274
+ } break;
1275
+ case PrimitiveKind::String32: {
1276
+ const char32_t *str32;
1277
+ if (!PushString32(value, 1, &str32)) [[unlikely]]
1278
+ return;
1279
+
1280
+ out_reg->a0 = (uint64_t)str32;
1281
+ } break;
1282
+ case PrimitiveKind::Pointer: {
1283
+ void *ptr;
1284
+ if (!PushPointer(value, type, 1, &ptr)) [[unlikely]]
1285
+ return;
1286
+
1287
+ out_reg->a0 = (uint64_t)ptr;
1288
+ } break;
1289
+ case PrimitiveKind::Record:
1290
+ case PrimitiveKind::Union: {
1291
+ if (proto->ret.abi.method != AbiMethod::Memory) {
1292
+ uint64_t buf[2];
1293
+ if (!PushObject(value, type, (uint8_t *)buf)) [[unlikely]]
1294
+ return;
1295
+
1296
+ memcpy((uint8_t *)&out_reg + proto->ret.abi.offsets[0], (const uint8_t *)buf, 8);
1297
+ memcpy((uint8_t *)&out_reg + proto->ret.abi.offsets[1], (const uint8_t *)buf + 8, 8);
1298
+ } else {
1299
+ uint64_t *gpr_ptr = (uint64_t *)in_ptr;
1300
+ uint8_t *dest = (uint8_t *)gpr_ptr[0]; // a0
1301
+
1302
+ if (!PushObject(value, type, dest)) [[unlikely]]
1303
+ return;
1304
+ }
1305
+ } break;
1306
+ case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
1307
+ case PrimitiveKind::Float32: {
1308
+ float f;
1309
+ if (!TryNumber(env, value, &f)) [[unlikely]] {
1310
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value));
1311
+ return;
1312
+ }
1313
+
1314
+ memset(&out_reg->fa0, 0xFF, 8);
1315
+ memcpy(&out_reg->fa0, &f, 4);
1316
+ } break;
1317
+ case PrimitiveKind::Float64: {
1318
+ double d;
1319
+ if (!TryNumber(env, value, &d)) [[unlikely]] {
1320
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value));
1321
+ return;
1322
+ }
1323
+
1324
+ out_reg->fa0 = d;
1325
+ } break;
1326
+ case PrimitiveKind::Callback: {
1327
+ void *ptr;
1328
+ if (!PushCallback(value, type, &ptr)) [[unlikely]]
1329
+ return;
1330
+
1331
+ out_reg->a0 = (uint64_t)ptr;
1332
+ } break;
1333
+
1334
+ case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
1335
+ }
1336
+
1337
+ #undef RETURN_INTEGER_SWAP
1338
+ #undef RETURN_INTEGER
1339
+
1340
+ err_guard.Disable();
1341
+ }
1342
+
1343
+ }
1344
+
1345
+ #endif