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