@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,1185 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // SPDX-FileCopyrightText: 2026 Niels Martignène <niels.martignene@protonmail.com>
3
+
4
+ #if defined(__i386__) || defined(_M_IX86)
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 BackRegisters {
20
+ uint32_t eax;
21
+ uint32_t edx;
22
+ union {
23
+ double d;
24
+ float f;
25
+ } x87;
26
+ int ret_type;
27
+ int ret_pop;
28
+ };
29
+
30
+ extern "C" uint64_t ForwardCallG(const void *func, uint8_t *sp, uint8_t **out_saved_sp);
31
+ extern "C" float ForwardCallF(const void *func, uint8_t *sp, uint8_t **out_saved_sp);
32
+ extern "C" double ForwardCallD(const void *func, uint8_t *sp, uint8_t **out_saved_sp);
33
+ extern "C" uint64_t ForwardCallGR(const void *func, uint8_t *sp, uint8_t **out_saved_sp);
34
+ extern "C" float ForwardCallFR(const void *func, uint8_t *sp, uint8_t **out_saved_sp);
35
+ extern "C" double ForwardCallDR(const void *func, uint8_t *sp, uint8_t **out_saved_sp);
36
+
37
+ enum class AbiOpcode {
38
+ #define PRIMITIVE(Name) Push ## Name,
39
+ #include "../primitives.inc"
40
+ PushAggregateStack,
41
+ #define PRIMITIVE(Name) Run ## Name,
42
+ #include "../primitives.inc"
43
+ RunAggregateG,
44
+ RunAggregateF,
45
+ RunAggregateD,
46
+ RunAggregateMem,
47
+ #define PRIMITIVE(Name) Run ## Name ## R,
48
+ #include "../primitives.inc"
49
+ RunAggregateGR,
50
+ RunAggregateFR,
51
+ RunAggregateDR,
52
+ RunAggregateMemR,
53
+ Yield,
54
+ CallG,
55
+ CallF,
56
+ CallD,
57
+ CallStack,
58
+ CallGR,
59
+ CallFR,
60
+ CallDR,
61
+ CallStackR,
62
+ #define PRIMITIVE(Name) Return ## Name,
63
+ #include "../primitives.inc"
64
+ ReturnAggregateReg,
65
+ ReturnAggregateMem
66
+ };
67
+
68
+ static inline void *Code2Op(AbiOpcode code)
69
+ {
70
+ return (void *)code;
71
+ }
72
+
73
+ bool AnalyseFunction(Napi::Env env, InstanceData *instance, FunctionInfo *func)
74
+ {
75
+ if (!func->lib && func->convention != CallConvention::Cdecl &&
76
+ func->convention != CallConvention::Stdcall) {
77
+ ThrowError<Napi::Error>(env, "Only Cdecl and Stdcall callbacks are supported");
78
+ return false;
79
+ }
80
+
81
+ if (func->ret.type->primitive != PrimitiveKind::Record &&
82
+ func->ret.type->primitive != PrimitiveKind::Union) {
83
+ K_ASSERT(IsRegularSize(func->ret.type->size, 8));
84
+ func->ret.abi.regular = true;
85
+ #if defined(_WIN32) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
86
+ } else {
87
+ func->ret.abi.regular = IsRegularSize(func->ret.type->size, 8);
88
+ #endif
89
+ }
90
+
91
+ int fast_regs = (func->convention == CallConvention::Fastcall) ? 2 :
92
+ (func->convention == CallConvention::Thiscall) ? 1 : 0;
93
+ bool fast = fast_regs;
94
+
95
+ Size fast_offset = 0;
96
+ Size stk_offset = fast ? 4 : 0;
97
+
98
+ if (!func->ret.abi.regular) {
99
+ #if defined(_WIN32)
100
+ stk_offset++;
101
+ #else
102
+ if (fast_regs) {
103
+ fast_offset++;
104
+ fast_regs--;
105
+ } else {
106
+ stk_offset++;
107
+ }
108
+ #endif
109
+ }
110
+
111
+ for (ParameterInfo &param: func->parameters) {
112
+ int16_t offset = 0;
113
+
114
+ if (fast_regs && param.type->size <= 4) {
115
+ offset = (int16_t)fast_offset++;
116
+ fast_regs--;
117
+ } else {
118
+ offset = (int16_t)stk_offset;
119
+ stk_offset += (param.type->size + 3) / 4;
120
+ }
121
+
122
+ if (param.type->primitive == PrimitiveKind::Record || param.type->primitive == PrimitiveKind::Union) {
123
+ func->sync.Append({ .op = Code2Op(AbiOpcode::PushAggregateStack), .a = param.offset, .b1 = (int16_t)(offset * 4), .b2 = (int16_t)param.directions, .type = param.type });
124
+ func->async.Append({ .op = Code2Op(AbiOpcode::PushAggregateStack), .a = param.offset, .b1 = (int16_t)(offset * 4), .b2 = (int16_t)param.directions, .type = param.type });
125
+ } else {
126
+ int delta = (int)AbiOpcode::PushVoid - (int)PrimitiveKind::Void;
127
+ AbiOpcode code = (AbiOpcode)((int)param.type->primitive + delta);
128
+
129
+ func->sync.Append({ .op = Code2Op(code), .a = param.offset, .b1 = (int16_t)(offset * 4), .b2 = (int16_t)param.directions, .type = param.type });
130
+ func->async.Append({ .op = Code2Op(code), .a = param.offset, .b1 = (int16_t)(offset * 4), .b2 = (int16_t)param.directions, .type = param.type });
131
+ }
132
+ }
133
+
134
+ // We need enough space to memcpy result in CallX instructions
135
+ func->ret_pop = (int)(4 * stk_offset);
136
+ func->stk_size = stk_offset ? AlignLen(4 * stk_offset, 16) : 16;
137
+
138
+ func->async.Append({ .op = Code2Op(AbiOpcode::Yield) });
139
+
140
+ switch (func->ret.type->primitive) {
141
+ case PrimitiveKind::Void:
142
+ case PrimitiveKind::Bool:
143
+ case PrimitiveKind::Int8:
144
+ case PrimitiveKind::UInt8:
145
+ case PrimitiveKind::Int16:
146
+ case PrimitiveKind::Int16S:
147
+ case PrimitiveKind::UInt16:
148
+ case PrimitiveKind::UInt16S:
149
+ case PrimitiveKind::Int32:
150
+ case PrimitiveKind::Int32S:
151
+ case PrimitiveKind::UInt32:
152
+ case PrimitiveKind::UInt32S:
153
+ case PrimitiveKind::Int64:
154
+ case PrimitiveKind::Int64S:
155
+ case PrimitiveKind::UInt64:
156
+ case PrimitiveKind::UInt64S:
157
+ case PrimitiveKind::String:
158
+ case PrimitiveKind::String16:
159
+ case PrimitiveKind::String32: {
160
+ if (fast) {
161
+ int delta = (int)AbiOpcode::RunVoidR - (int)PrimitiveKind::Void;
162
+ AbiOpcode run = (AbiOpcode)((int)func->ret.type->primitive + delta);
163
+
164
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
165
+ } else {
166
+ int delta = (int)AbiOpcode::RunVoid - (int)PrimitiveKind::Void;
167
+ AbiOpcode run = (AbiOpcode)((int)func->ret.type->primitive + delta);
168
+
169
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
170
+ }
171
+
172
+ // Async
173
+ {
174
+ int delta = (int)AbiOpcode::ReturnVoid - (int)PrimitiveKind::Void;
175
+ AbiOpcode call = fast ? AbiOpcode::CallGR : AbiOpcode::CallG;
176
+ AbiOpcode ret = (AbiOpcode)((int)func->ret.type->primitive + delta);
177
+
178
+ func->async.Append({ .op = Code2Op(call) });
179
+ func->async.Append({ .op = Code2Op(ret), .type = func->ret.type });
180
+ }
181
+ } break;
182
+
183
+ case PrimitiveKind::Pointer: {
184
+ AbiOpcode run = fast ? AbiOpcode::RunPointerR : AbiOpcode::RunPointer;
185
+ AbiOpcode call = fast ? AbiOpcode::CallGR : AbiOpcode::CallG;
186
+
187
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type->ref.type });
188
+ func->async.Append({ .op = Code2Op(call) });
189
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnPointer), .type = func->ret.type->ref.type });
190
+ } break;
191
+ case PrimitiveKind::Callback: {
192
+ AbiOpcode run = fast ? AbiOpcode::RunCallbackR : AbiOpcode::RunCallback;
193
+ AbiOpcode call = fast ? AbiOpcode::CallGR : AbiOpcode::CallG;
194
+
195
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
196
+ func->async.Append({ .op = Code2Op(call) });
197
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnCallback), .type = func->ret.type });
198
+ } break;
199
+
200
+ case PrimitiveKind::Record:
201
+ case PrimitiveKind::Union: {
202
+ #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
203
+ if (func->ret.type->members.len == 1) {
204
+ const RecordMember &member = func->ret.type->members[0];
205
+
206
+ if (member.type->primitive == PrimitiveKind::Float32) {
207
+ AbiOpcode run = fast ? AbiOpcode::RunAggregateFR : AbiOpcode::RunAggregateF;
208
+ AbiOpcode call = fast ? AbiOpcode::CallFR : AbiOpcode::CallF;
209
+
210
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
211
+ func->async.Append({ .op = Code2Op(call) });
212
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnAggregateReg), .type = func->ret.type });
213
+
214
+ break;
215
+ } else if (member.type->primitive == PrimitiveKind::Float64) {
216
+ AbiOpcode run = fast ? AbiOpcode::RunAggregateDR : AbiOpcode::RunAggregateD;
217
+ AbiOpcode call = fast ? AbiOpcode::CallDR : AbiOpcode::CallD;
218
+
219
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
220
+ func->async.Append({ .op = Code2Op(call) });
221
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnAggregateReg), .type = func->ret.type });
222
+
223
+ break;
224
+ }
225
+ }
226
+ #endif
227
+
228
+ if (func->ret.abi.regular) {
229
+ AbiOpcode run = fast ? AbiOpcode::RunAggregateGR : AbiOpcode::RunAggregateG;
230
+ AbiOpcode call = fast ? AbiOpcode::CallGR : AbiOpcode::CallG;
231
+
232
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
233
+ func->async.Append({ .op = Code2Op(call) });
234
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnAggregateReg), .type = func->ret.type });
235
+ } else {
236
+ AbiOpcode run = fast ? AbiOpcode::RunAggregateMemR : AbiOpcode::RunAggregateMem;
237
+ AbiOpcode call = fast ? AbiOpcode::CallStackR : AbiOpcode::CallStack;
238
+
239
+ func->sync.Append({ .op = Code2Op(run), .a = (int32_t)func->ret.type->size, .type = func->ret.type });
240
+ func->async.Append({ .op = Code2Op(call), .a = (int32_t)func->ret.type->size });
241
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnAggregateMem), .type = func->ret.type });
242
+ }
243
+ } break;
244
+ case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
245
+
246
+ case PrimitiveKind::Float32: {
247
+ AbiOpcode run = fast ? AbiOpcode::RunFloat32R : AbiOpcode::RunFloat32;
248
+ AbiOpcode call = fast ? AbiOpcode::CallFR : AbiOpcode::CallF;
249
+
250
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
251
+ func->async.Append({ .op = Code2Op(call) });
252
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnFloat32), .type = func->ret.type });
253
+ } break;
254
+ case PrimitiveKind::Float64: {
255
+ AbiOpcode run = fast ? AbiOpcode::RunFloat64R : AbiOpcode::RunFloat64;
256
+ AbiOpcode call = fast ? AbiOpcode::CallDR : AbiOpcode::CallD;
257
+
258
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
259
+ func->async.Append({ .op = Code2Op(call) });
260
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnFloat64), .type = func->ret.type });
261
+ } break;
262
+
263
+ case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
264
+ }
265
+
266
+ switch (func->convention) {
267
+ case CallConvention::Cdecl: {
268
+ func->decorated_name = Fmt(&instance->str_alloc, "_%1", func->name).ptr;
269
+ } break;
270
+ case CallConvention::Stdcall: {
271
+ K_ASSERT(!func->variadic);
272
+
273
+ Size suffix = (stk_offset - !func->ret.abi.regular) * 4;
274
+ func->decorated_name = Fmt(&instance->str_alloc, "_%1@%2", func->name, suffix).ptr;
275
+ } break;
276
+ case CallConvention::Fastcall: {
277
+ K_ASSERT(!func->variadic);
278
+
279
+ Size suffix = (fast_offset + stk_offset - 4 - !func->ret.abi.regular) * 4;
280
+ func->decorated_name = Fmt(&instance->str_alloc, "@%1@%2", func->name, suffix).ptr;
281
+ } break;
282
+ case CallConvention::Thiscall: {
283
+ K_ASSERT(!func->variadic);
284
+ // Name does not change
285
+ } break;
286
+ }
287
+
288
+ return true;
289
+ }
290
+
291
+ #if defined(__GNUC__) || defined(__clang__)
292
+ #pragma GCC diagnostic push
293
+ #pragma GCC diagnostic ignored "-Wunused-parameter"
294
+ #endif
295
+
296
+ namespace {
297
+ #define OP(Code) \
298
+ case (int)AbiOpcode::Code:
299
+ #define NEXT() \
300
+ break
301
+
302
+ napi_value RunLoop(CallData *call, napi_value *args, uint8_t *base, const AbiInstruction *inst)
303
+ {
304
+ for (;; ++inst) {
305
+ switch ((intptr_t)inst->op) {
306
+
307
+ #if defined(_WIN32)
308
+ #define WRAP(Expr) \
309
+ [&]() { \
310
+ TEB *teb = GetTEB(); \
311
+ \
312
+ K_DEFER { call->instance->last_error = teb->LastErrorValue; }; \
313
+ teb->LastErrorValue = call->instance->last_error; \
314
+ \
315
+ ADJUST_TEB(teb, call->mem->stack0.ptr, call->mem->stack0.end); \
316
+ \
317
+ call->DebugForward(); \
318
+ return (Expr); \
319
+ }()
320
+ #else
321
+ #define WRAP(Expr) (call->DebugForward(), (Expr))
322
+ #endif
323
+
324
+ #define INTEGER32(CType) \
325
+ do { \
326
+ CType v; \
327
+ if (!TryNumber(call->env, args[inst->a], &v)) [[unlikely]] { \
328
+ ThrowError<Napi::TypeError>(call->env, "Unexpected %1 value, expected number", GetValueType(call->instance, args[inst->a])); \
329
+ return call->env.Null(); \
330
+ } \
331
+ \
332
+ *(uint32_t *)(base + inst->b1) = (uint32_t)v; \
333
+ } while (false)
334
+ #define INTEGER32_SWAP(CType) \
335
+ do { \
336
+ CType v; \
337
+ if (!TryNumber(call->env, args[inst->a], &v)) [[unlikely]] { \
338
+ ThrowError<Napi::TypeError>(call->env, "Unexpected %1 value, expected number", GetValueType(call->instance, args[inst->a])); \
339
+ return call->env.Null(); \
340
+ } \
341
+ \
342
+ *(uint32_t *)(base + inst->b1) = (uint32_t)ReverseBytes(v); \
343
+ } while (false)
344
+ #define INTEGER64(CType) \
345
+ do { \
346
+ CType v; \
347
+ if (!TryNumber(call->env, args[inst->a], &v)) [[unlikely]] { \
348
+ ThrowError<Napi::TypeError>(call->env, "Unexpected %1 value, expected number", GetValueType(call->instance, args[inst->a])); \
349
+ return call->env.Null(); \
350
+ } \
351
+ \
352
+ memcpy(base + inst->b1, &v, 8); \
353
+ } while (false)
354
+ #define INTEGER64_SWAP(CType) \
355
+ do { \
356
+ CType v; \
357
+ if (!TryNumber(call->env, args[inst->a], &v)) [[unlikely]] { \
358
+ ThrowError<Napi::TypeError>(call->env, "Unexpected %1 value, expected number", GetValueType(call->instance, args[inst->a])); \
359
+ return call->env.Null(); \
360
+ } \
361
+ \
362
+ v = ReverseBytes(v); \
363
+ memcpy(base + inst->b1, &v, 8); \
364
+ } while (false)
365
+
366
+ OP(PushVoid) { K_UNREACHABLE(); return call->env.Null(); }
367
+ OP(PushBool) {
368
+ bool b;
369
+ if (napi_get_value_bool(call->env, args[inst->a], &b) != napi_ok) [[unlikely]] {
370
+ ThrowError<Napi::TypeError>(call->env, "Unexpected %1 value, expected boolean", GetValueType(call->instance, args[inst->a]));
371
+ return call->env.Null();
372
+ }
373
+
374
+ *(bool *)(base + inst->b1) = b;
375
+
376
+ NEXT();
377
+ }
378
+ OP(PushInt8) { INTEGER32(int8_t); NEXT(); }
379
+ OP(PushUInt8) { INTEGER32(uint8_t); NEXT(); }
380
+ OP(PushInt16) { INTEGER32(int16_t); NEXT(); }
381
+ OP(PushInt16S) { INTEGER32_SWAP(int16_t); NEXT(); }
382
+ OP(PushUInt16) { INTEGER32(uint16_t); NEXT(); }
383
+ OP(PushUInt16S) { INTEGER32_SWAP(uint16_t); NEXT(); }
384
+ OP(PushInt32) { INTEGER32(int32_t); NEXT(); }
385
+ OP(PushInt32S) { INTEGER32_SWAP(int32_t); NEXT(); }
386
+ OP(PushUInt32) { INTEGER32(uint32_t); NEXT(); }
387
+ OP(PushUInt32S) { INTEGER32_SWAP(uint32_t); NEXT(); }
388
+ OP(PushInt64) { INTEGER64(int64_t); NEXT(); }
389
+ OP(PushInt64S) { INTEGER64_SWAP(int64_t); NEXT(); }
390
+ OP(PushUInt64) { INTEGER64(uint64_t); NEXT(); }
391
+ OP(PushUInt64S) { INTEGER64_SWAP(uint64_t); NEXT(); }
392
+ OP(PushString) {
393
+ const char *str;
394
+ if (!call->PushString(args[inst->a], inst->b2, &str)) [[unlikely]]
395
+ return call->env.Null();
396
+
397
+ *(const char **)(base + inst->b1) = str;
398
+
399
+ NEXT();
400
+ }
401
+ OP(PushString16) {
402
+ const char16_t *str16;
403
+ if (!call->PushString16(args[inst->a], inst->b2, &str16)) [[unlikely]]
404
+ return call->env.Null();
405
+
406
+ *(const char16_t **)(base + inst->b1) = str16;
407
+
408
+ NEXT();
409
+ }
410
+ OP(PushString32) {
411
+ const char32_t *str32;
412
+ if (!call->PushString32(args[inst->a], inst->b2, &str32)) [[unlikely]]
413
+ return call->env.Null();
414
+
415
+ *(const char32_t **)(base + inst->b1) = str32;
416
+
417
+ NEXT();
418
+ }
419
+ OP(PushPointer) {
420
+ void *ptr;
421
+ if (!call->PushPointer(args[inst->a], inst->type, inst->b2, &ptr)) [[unlikely]]
422
+ return call->env.Null();
423
+
424
+ *(void **)(base + inst->b1) = ptr;
425
+
426
+ NEXT();
427
+ }
428
+ OP(PushRecord) { K_UNREACHABLE(); return call->env.Null(); }
429
+ OP(PushUnion) { K_UNREACHABLE(); return call->env.Null(); }
430
+ OP(PushArray) { K_UNREACHABLE(); return call->env.Null(); }
431
+ OP(PushFloat32) {
432
+ float f;
433
+ if (!TryNumber(call->env, args[inst->a], &f)) [[unlikely]] {
434
+ ThrowError<Napi::TypeError>(call->env, "Unexpected %1 value, expected number", GetValueType(call->instance, args[inst->a]));
435
+ return call->env.Null();
436
+ }
437
+
438
+ *(float *)(base + inst->b1) = f;
439
+
440
+ NEXT();
441
+ }
442
+ OP(PushFloat64) {
443
+ double d;
444
+ if (!TryNumber(call->env, args[inst->a], &d)) [[unlikely]] {
445
+ ThrowError<Napi::TypeError>(call->env, "Unexpected %1 value, expected number", GetValueType(call->instance, args[inst->a]));
446
+ return call->env.Null();
447
+ }
448
+
449
+ memcpy(base + inst->b1, &d, 8);
450
+
451
+ NEXT();
452
+ }
453
+ OP(PushCallback) {
454
+ void *ptr;
455
+ if (!call->PushCallback(args[inst->a], inst->type, &ptr)) [[unlikely]]
456
+ return call->env.Null();
457
+
458
+ *(void **)(base + inst->b1) = ptr;
459
+
460
+ NEXT();
461
+ }
462
+ OP(PushPrototype) { K_UNREACHABLE(); return call->env.Null(); }
463
+ OP(PushAggregateStack) {
464
+ napi_value arg = args[inst->a];
465
+
466
+ uint8_t *ptr = (uint8_t *)(base + inst->b1);
467
+
468
+ if (!call->PushObject(arg, inst->type, ptr)) [[unlikely]]
469
+ return call->env.Null();
470
+
471
+ NEXT();
472
+ }
473
+
474
+ #undef INTEGER64_SWAP
475
+ #undef INTEGER64
476
+ #undef INTEGER32_SWAP
477
+ #undef INTEGER32
478
+
479
+ #define INTEGER32(Suffix, CType) \
480
+ do { \
481
+ uint32_t eax = (uint32_t)WRAP(ForwardCall ## Suffix(call->native, base, &call->saved_sp)); \
482
+ return NewInt(call->env, (CType)eax); \
483
+ } while (false)
484
+ #define INTEGER32_SWAP(Suffix, CType) \
485
+ do { \
486
+ uint32_t eax = (uint32_t)WRAP(ForwardCall ## Suffix(call->native, base, &call->saved_sp)); \
487
+ return NewInt(call->env, ReverseBytes((CType)eax)); \
488
+ } while (false)
489
+ #define INTEGER64(Suffix, CType) \
490
+ do { \
491
+ uint64_t ret = WRAP(ForwardCall ## Suffix(call->native, base, &call->saved_sp)); \
492
+ return NewInt(call->env, (CType)ret); \
493
+ } while (false)
494
+ #define INTEGER64_SWAP(Suffix, CType) \
495
+ do { \
496
+ uint64_t ret = WRAP(ForwardCall ## Suffix(call->native, base, &call->saved_sp)); \
497
+ return NewInt(call->env, ReverseBytes((CType)ret)); \
498
+ } while (false)
499
+ #define DISPOSE(Ptr) \
500
+ do { \
501
+ if (inst->type->dispose) { \
502
+ inst->type->dispose(call->instance, inst->type, (Ptr)); \
503
+ } \
504
+ } while (false)
505
+
506
+ OP(RunVoid) {
507
+ WRAP(ForwardCallG(call->native, base, &call->saved_sp));
508
+ return nullptr;
509
+ }
510
+ OP(RunBool) {
511
+ uint32_t eax = (uint32_t)WRAP(ForwardCallG(call->native, base, &call->saved_sp));
512
+ return Napi::Boolean::New(call->env, eax & 0x1);
513
+ }
514
+ OP(RunInt8) { INTEGER32(G, int8_t); }
515
+ OP(RunUInt8) { INTEGER32(G, uint8_t); }
516
+ OP(RunInt16) { INTEGER32(G, int16_t); }
517
+ OP(RunInt16S) { INTEGER32_SWAP(G, int16_t); }
518
+ OP(RunUInt16) { INTEGER32(G, uint16_t); }
519
+ OP(RunUInt16S) { INTEGER32_SWAP(G, uint16_t); }
520
+ OP(RunInt32) { INTEGER32(G, int32_t); }
521
+ OP(RunInt32S) { INTEGER32_SWAP(G, int32_t); }
522
+ OP(RunUInt32) { INTEGER32(G, uint32_t); }
523
+ OP(RunUInt32S) { INTEGER32_SWAP(G, uint32_t); }
524
+ OP(RunInt64) { INTEGER64(G, int64_t); }
525
+ OP(RunInt64S) { INTEGER64_SWAP(G, int64_t); }
526
+ OP(RunUInt64) { INTEGER64(G, uint64_t); }
527
+ OP(RunUInt64S) { INTEGER64_SWAP(G, uint64_t); }
528
+ OP(RunString) {
529
+ uint32_t eax = (uint32_t)WRAP(ForwardCallG(call->native, base, &call->saved_sp));
530
+ napi_value value = NewString(call->env, (const char *)eax);
531
+ DISPOSE((void *)eax);
532
+ return value;
533
+ }
534
+ OP(RunString16) {
535
+ uint32_t eax = (uint32_t)WRAP(ForwardCallG(call->native, base, &call->saved_sp));
536
+ napi_value value = NewString(call->env, (const char16_t *)eax);
537
+ DISPOSE((void *)eax);
538
+ return value;
539
+ }
540
+ OP(RunString32) {
541
+ uint32_t eax = (uint32_t)WRAP(ForwardCallG(call->native, base, &call->saved_sp));
542
+ napi_value value = NewString(call->env, (const char32_t *)eax);
543
+ DISPOSE((void *)eax);
544
+ return value;
545
+ }
546
+ OP(RunPointer) {
547
+ uint32_t eax = (uint32_t)WRAP(ForwardCallG(call->native, base, &call->saved_sp));
548
+ napi_value value = WrapPointer(call->env, inst->type, (void *)eax);
549
+ DISPOSE((void *)eax);
550
+ return value;
551
+ }
552
+ OP(RunCallback) {
553
+ uint32_t eax = (uint32_t)WRAP(ForwardCallG(call->native, base, &call->saved_sp));
554
+ return WrapPointer(call->env, inst->type, (void *)eax);
555
+ }
556
+ OP(RunRecord) { K_UNREACHABLE(); return call->env.Null(); }
557
+ OP(RunUnion) { K_UNREACHABLE(); return call->env.Null(); }
558
+ OP(RunArray) { K_UNREACHABLE(); return call->env.Null(); }
559
+ OP(RunFloat32) {
560
+ float f = WRAP(ForwardCallF(call->native, base, &call->saved_sp));
561
+ return NewFloat(call->env, f);
562
+ }
563
+ OP(RunFloat64) {
564
+ double d = WRAP(ForwardCallD(call->native, base, &call->saved_sp));
565
+ return NewFloat(call->env, d);
566
+ }
567
+ OP(RunPrototype) { K_UNREACHABLE(); return call->env.Null(); }
568
+ OP(RunAggregateG) {
569
+ auto ret = WRAP(ForwardCallG(call->native, base, &call->saved_sp));
570
+ return DecodeObject(call->instance, (const uint8_t *)&ret, inst->type);
571
+ }
572
+ OP(RunAggregateF) {
573
+ auto ret = WRAP(ForwardCallF(call->native, base, &call->saved_sp));
574
+ return DecodeObject(call->instance, (const uint8_t *)&ret, inst->type);
575
+ }
576
+ OP(RunAggregateD) {
577
+ auto ret = WRAP(ForwardCallD(call->native, base, &call->saved_sp));
578
+ return DecodeObject(call->instance, (const uint8_t *)&ret, inst->type);
579
+ }
580
+ OP(RunAggregateMem) {
581
+ *(uint8_t **)base = call->AllocHeap(inst->a);
582
+ uint32_t eax = (uint32_t)WRAP(ForwardCallG(call->native, base, &call->saved_sp));
583
+ return DecodeObject(call->instance, (const uint8_t *)eax, inst->type);
584
+ }
585
+ OP(RunVoidR) {
586
+ WRAP(ForwardCallGR(call->native, base, &call->saved_sp));
587
+ return nullptr;
588
+ }
589
+ OP(RunBoolR) {
590
+ uint32_t eax = (uint32_t)WRAP(ForwardCallGR(call->native, base, &call->saved_sp));
591
+ return Napi::Boolean::New(call->env, eax & 0x1);
592
+ }
593
+ OP(RunInt8R) { INTEGER32(GR, int8_t); }
594
+ OP(RunUInt8R) { INTEGER32(GR, uint8_t); }
595
+ OP(RunInt16R) { INTEGER32(GR, int16_t); }
596
+ OP(RunInt16SR) { INTEGER32_SWAP(GR, int16_t); }
597
+ OP(RunUInt16R) { INTEGER32(GR, uint16_t); }
598
+ OP(RunUInt16SR) { INTEGER32_SWAP(GR, uint16_t); }
599
+ OP(RunInt32R) { INTEGER32(GR, int32_t); }
600
+ OP(RunInt32SR) { INTEGER32_SWAP(GR, int32_t); }
601
+ OP(RunUInt32R) { INTEGER32(GR, uint32_t); }
602
+ OP(RunUInt32SR) { INTEGER32_SWAP(GR, uint32_t); }
603
+ OP(RunInt64R) { INTEGER64(GR, int64_t); }
604
+ OP(RunInt64SR) { INTEGER64_SWAP(GR, int64_t); }
605
+ OP(RunUInt64R) { INTEGER64(GR, uint64_t); }
606
+ OP(RunUInt64SR) { INTEGER64_SWAP(GR, uint64_t); }
607
+ OP(RunStringR) {
608
+ uint32_t eax = (uint32_t)WRAP(ForwardCallGR(call->native, base, &call->saved_sp));
609
+ napi_value value = NewString(call->env, (const char *)eax);
610
+ DISPOSE((void *)eax);
611
+ return value;
612
+ }
613
+ OP(RunString16R) {
614
+ uint32_t eax = (uint32_t)WRAP(ForwardCallGR(call->native, base, &call->saved_sp));
615
+ napi_value value = NewString(call->env, (const char16_t *)eax);
616
+ DISPOSE((void *)eax);
617
+ return value;
618
+ }
619
+ OP(RunString32R) {
620
+ uint32_t eax = (uint32_t)WRAP(ForwardCallGR(call->native, base, &call->saved_sp));
621
+ napi_value value = NewString(call->env, (const char32_t *)eax);
622
+ DISPOSE((void *)eax);
623
+ return value;
624
+ }
625
+ OP(RunPointerR) {
626
+ uint32_t eax = (uint32_t)WRAP(ForwardCallGR(call->native, base, &call->saved_sp));
627
+ napi_value value = WrapPointer(call->env, inst->type, (void *)eax);
628
+ DISPOSE((void *)eax);
629
+ return value;
630
+ }
631
+ OP(RunCallbackR) {
632
+ uint32_t eax = (uint32_t)WRAP(ForwardCallGR(call->native, base, &call->saved_sp));
633
+ return WrapPointer(call->env, inst->type, (void *)eax);
634
+ }
635
+ OP(RunRecordR) { K_UNREACHABLE(); return call->env.Null(); }
636
+ OP(RunUnionR) { K_UNREACHABLE(); return call->env.Null(); }
637
+ OP(RunArrayR) { K_UNREACHABLE(); return call->env.Null(); }
638
+ OP(RunFloat32R) {
639
+ float f = WRAP(ForwardCallFR(call->native, base, &call->saved_sp));
640
+ return NewFloat(call->env, f);
641
+ }
642
+ OP(RunFloat64R) {
643
+ double d = WRAP(ForwardCallDR(call->native, base, &call->saved_sp));
644
+ return NewFloat(call->env, d);
645
+ }
646
+ OP(RunPrototypeR) { K_UNREACHABLE(); return call->env.Null(); }
647
+ OP(RunAggregateGR) {
648
+ auto ret = WRAP(ForwardCallGR(call->native, base, &call->saved_sp));
649
+ return DecodeObject(call->instance, (const uint8_t *)&ret, inst->type);
650
+ }
651
+ OP(RunAggregateFR) {
652
+ auto ret = WRAP(ForwardCallFR(call->native, base, &call->saved_sp));
653
+ return DecodeObject(call->instance, (const uint8_t *)&ret, inst->type);
654
+ }
655
+ OP(RunAggregateDR) {
656
+ auto ret = WRAP(ForwardCallDR(call->native, base, &call->saved_sp));
657
+ return DecodeObject(call->instance, (const uint8_t *)&ret, inst->type);
658
+ }
659
+ OP(RunAggregateMemR) {
660
+ #if defined(_WIN32)
661
+ *(uint8_t **)(base + 16) = call->AllocHeap(inst->a);
662
+ #else
663
+ *(uint8_t **)base = call->AllocHeap(inst->a);
664
+ #endif
665
+ uint32_t eax = (uint32_t)WRAP(ForwardCallGR(call->native, base, &call->saved_sp));
666
+ return DecodeObject(call->instance, (const uint8_t *)eax, inst->type);
667
+ }
668
+
669
+ #undef DISPOSE
670
+ #undef INTEGER64_SWAP
671
+ #undef INTEGER64
672
+ #undef INTEGER32_SWAP
673
+ #undef INTEGER32
674
+
675
+ #define CALL(Suffix) \
676
+ do { \
677
+ auto ret = WRAP(ForwardCall ## Suffix(call->native, base, &call->saved_sp)); \
678
+ memcpy(base, &ret, K_SIZE(ret)); \
679
+ } while (false)
680
+ #define DISPOSE() \
681
+ do { \
682
+ if (inst->type->dispose) { \
683
+ void *ptr = *(void **)base; \
684
+ inst->type->dispose(call->instance, inst->type, ptr); \
685
+ } \
686
+ } while (false)
687
+ #define INTEGER32(CType) \
688
+ do { \
689
+ uint32_t eax = *(uint32_t *)base; \
690
+ return NewInt(call->env, (CType)eax); \
691
+ } while (false)
692
+ #define INTEGER32_SWAP(CType) \
693
+ do { \
694
+ uint32_t eax = *(uint32_t *)base; \
695
+ return NewInt(call->env, ReverseBytes((CType)eax)); \
696
+ } while (false)
697
+ #define INTEGER64(CType) \
698
+ do { \
699
+ uint64_t ret = *(uint64_t *)base; \
700
+ return NewInt(call->env, (CType)ret); \
701
+ } while (false)
702
+ #define INTEGER64_SWAP(CType) \
703
+ do { \
704
+ uint64_t ret = *(uint64_t *)base; \
705
+ return NewInt(call->env, ReverseBytes((CType)ret)); \
706
+ } while (false)
707
+
708
+ OP(Yield) {
709
+ call->async_ip = inst + 1;
710
+ call->async_base = base;
711
+ return nullptr;
712
+ }
713
+
714
+ OP(CallG) { CALL(G); return nullptr; }
715
+ OP(CallF) { CALL(F); return nullptr; }
716
+ OP(CallD) { CALL(D); return nullptr; }
717
+ OP(CallStack) {
718
+ *(uint8_t **)base = call->AllocHeap(inst->a);
719
+ CALL(G);
720
+ return nullptr;
721
+ }
722
+ OP(CallGR) { CALL(GR); return nullptr; }
723
+ OP(CallFR) { CALL(FR); return nullptr; }
724
+ OP(CallDR) { CALL(DR); return nullptr; }
725
+ OP(CallStackR) {
726
+ #if defined(_WIN32)
727
+ *(uint8_t **)(base + 16) = call->AllocHeap(inst->a);
728
+ #else
729
+ *(uint8_t **)base = call->AllocHeap(inst->a);
730
+ #endif
731
+ CALL(GR);
732
+ return nullptr;
733
+ }
734
+
735
+ OP(ReturnVoid) { return nullptr; }
736
+ OP(ReturnBool) {
737
+ uint32_t eax = *(uint32_t *)base;
738
+ return Napi::Boolean::New(call->env, eax & 0x1);
739
+ }
740
+ OP(ReturnInt8) { INTEGER32(int8_t); }
741
+ OP(ReturnUInt8) { INTEGER32(uint8_t); }
742
+ OP(ReturnInt16) { INTEGER32(int16_t); }
743
+ OP(ReturnInt16S) { INTEGER32_SWAP(int16_t); }
744
+ OP(ReturnUInt16) { INTEGER32(uint16_t); }
745
+ OP(ReturnUInt16S) { INTEGER32_SWAP(uint16_t); }
746
+ OP(ReturnInt32) { INTEGER32(int32_t); }
747
+ OP(ReturnInt32S) { INTEGER32_SWAP(int32_t); }
748
+ OP(ReturnUInt32) { INTEGER32(uint32_t); }
749
+ OP(ReturnUInt32S) { INTEGER32_SWAP(uint32_t); }
750
+ OP(ReturnInt64) { INTEGER64(int64_t); }
751
+ OP(ReturnInt64S) { INTEGER64_SWAP(int64_t); }
752
+ OP(ReturnUInt64) { INTEGER64(uint64_t); }
753
+ OP(ReturnUInt64S) { INTEGER64_SWAP(uint64_t); }
754
+ OP(ReturnString) {
755
+ uint32_t eax = *(uint32_t *)base;
756
+ napi_value value = NewString(call->env, (const char *)eax);
757
+ DISPOSE();
758
+ return value;
759
+ }
760
+ OP(ReturnString16) {
761
+ uint32_t eax = *(uint32_t *)base;
762
+ napi_value value = NewString(call->env, (const char16_t *)eax);
763
+ DISPOSE();
764
+ return value;
765
+ }
766
+ OP(ReturnString32) {
767
+ uint32_t eax = *(uint32_t *)base;
768
+ napi_value value = NewString(call->env, (const char32_t *)eax);
769
+ DISPOSE();
770
+ return value;
771
+ }
772
+ OP(ReturnPointer) {
773
+ uint32_t eax = *(uint32_t *)base;
774
+ napi_value value = WrapPointer(call->env, inst->type, (void *)eax);
775
+ DISPOSE();
776
+ return value;
777
+ }
778
+ OP(ReturnCallback) {
779
+ uint32_t eax = *(uint32_t *)base;
780
+ return WrapPointer(call->env, inst->type, (void *)eax);
781
+ }
782
+ OP(ReturnRecord) { K_UNREACHABLE(); return call->env.Null(); }
783
+ OP(ReturnUnion) { K_UNREACHABLE(); return call->env.Null(); }
784
+ OP(ReturnArray) { K_UNREACHABLE(); return call->env.Null(); }
785
+ OP(ReturnFloat32) {
786
+ float f = *(float *)base;
787
+ return NewFloat(call->env, f);
788
+ }
789
+ OP(ReturnFloat64) {
790
+ double d = *(double *)base;
791
+ return NewFloat(call->env, d);
792
+ }
793
+ OP(ReturnPrototype) { K_UNREACHABLE(); return call->env.Null(); }
794
+ OP(ReturnAggregateReg) { return DecodeObject(call->instance, (const uint8_t *)base, inst->type); }
795
+ OP(ReturnAggregateMem) {
796
+ uint32_t eax = *(uint32_t *)base;
797
+ return DecodeObject(call->instance, (const uint8_t *)eax, inst->type);
798
+ }
799
+
800
+ #undef DISPOSE
801
+ #undef CALL
802
+ }
803
+ }
804
+
805
+ K_UNREACHABLE();
806
+ }
807
+
808
+ #undef WRAP
809
+
810
+ #undef NEXT
811
+ #undef OP
812
+ }
813
+
814
+ napi_value CallData::Run(const FunctionInfo *func, napi_value *args)
815
+ {
816
+ uint8_t *base = AllocStack<uint8_t>(func->stk_size);
817
+ if (!base) [[unlikely]]
818
+ return env.Null();
819
+
820
+ const AbiInstruction *first = func->sync.ptr;
821
+ return RunLoop(this, args, base, first);
822
+ }
823
+
824
+ bool CallData::PrepareAsync(const FunctionInfo *func, napi_value *args)
825
+ {
826
+ uint8_t *base = AllocStack<uint8_t>(func->stk_size);
827
+ if (!base) [[unlikely]]
828
+ return env.Null();
829
+ async_base = base;
830
+
831
+ const AbiInstruction *first = func->async.ptr;
832
+ return !RunLoop(this, args, base, first); // Yield returns nullptr
833
+ }
834
+
835
+ void CallData::ExecuteAsync()
836
+ {
837
+ const AbiInstruction *next = async_ip++;
838
+ RunLoop(this, nullptr, async_base, next);
839
+ }
840
+
841
+ napi_value CallData::EndAsync()
842
+ {
843
+ const AbiInstruction *next = async_ip++;
844
+ return RunLoop(this, nullptr, async_base, next);
845
+ }
846
+
847
+ void CallData::Relay(Size idx, uint8_t *sp)
848
+ {
849
+ TrampolineInfo *trampoline = &shared.trampolines[idx];
850
+ const FunctionInfo *proto = trampoline->proto;
851
+
852
+ uint8_t *caller_sp = sp + 48;
853
+ BackRegisters *out_reg = (BackRegisters *)(sp + 16);
854
+
855
+ uint32_t *args_ptr = (uint32_t *)caller_sp;
856
+
857
+ uint8_t *return_ptr = !proto->ret.abi.regular ? (uint8_t *)args_ptr[0] : nullptr;
858
+ args_ptr += !proto->ret.abi.regular;
859
+
860
+ if (proto->convention == CallConvention::Stdcall) {
861
+ out_reg->ret_pop = (int)proto->ret_pop;
862
+ } else {
863
+ #if defined(_WIN32)
864
+ out_reg->ret_pop = 0;
865
+ #else
866
+ out_reg->ret_pop = return_ptr ? 4 : 0;
867
+ #endif
868
+ }
869
+
870
+ K_DEFER_N(err_guard) {
871
+ trampoline->state = -1;
872
+
873
+ int pop = out_reg->ret_pop;
874
+ memset(out_reg, 0, K_SIZE(*out_reg));
875
+ out_reg->ret_type = 0;
876
+ out_reg->ret_pop = pop;
877
+ };
878
+
879
+ napi_value arguments[MaxParameters];
880
+
881
+ // Convert to JS arguments
882
+ for (Size i = 0; i < proto->parameters.len; i++) {
883
+ const ParameterInfo &param = proto->parameters[i];
884
+ K_ASSERT(param.directions >= 1 && param.directions <= 3);
885
+
886
+ switch (param.type->primitive) {
887
+ case PrimitiveKind::Void: { K_UNREACHABLE(); } break;
888
+
889
+ case PrimitiveKind::Bool: {
890
+ bool b = *(bool *)(args_ptr++);
891
+ arguments[i] = Napi::Boolean::New(env, b);
892
+ } break;
893
+ case PrimitiveKind::Int8: {
894
+ int8_t v = *(int8_t *)(args_ptr++);
895
+ arguments[i] = NewInt(env, v);
896
+ } break;
897
+ case PrimitiveKind::UInt8: {
898
+ uint8_t v = *(uint8_t *)(args_ptr++);
899
+ arguments[i] = NewInt(env, v);
900
+ } break;
901
+ case PrimitiveKind::Int16: {
902
+ int16_t v = *(int16_t *)(args_ptr++);
903
+ arguments[i] = NewInt(env, v);
904
+ } break;
905
+ case PrimitiveKind::Int16S: {
906
+ int16_t v = *(int16_t *)(args_ptr++);
907
+ arguments[i] = NewInt(env, ReverseBytes(v));
908
+ } break;
909
+ case PrimitiveKind::UInt16: {
910
+ uint16_t v = *(uint16_t *)(args_ptr++);
911
+ arguments[i] = NewInt(env, v);
912
+ } break;
913
+ case PrimitiveKind::UInt16S: {
914
+ uint16_t v = *(uint16_t *)(args_ptr++);
915
+ arguments[i] = NewInt(env, ReverseBytes(v));
916
+ } break;
917
+ case PrimitiveKind::Int32: {
918
+ int32_t v = *(int32_t *)(args_ptr++);
919
+ arguments[i] = NewInt(env, v);
920
+ } break;
921
+ case PrimitiveKind::Int32S: {
922
+ int32_t v = *(int32_t *)(args_ptr++);
923
+ arguments[i] = NewInt(env, ReverseBytes(v));
924
+ } break;
925
+ case PrimitiveKind::UInt32: {
926
+ uint32_t v = *(uint32_t *)(args_ptr++);
927
+ arguments[i] = NewInt(env, v);
928
+ } break;
929
+ case PrimitiveKind::UInt32S: {
930
+ uint32_t v = *(uint32_t *)(args_ptr++);
931
+ arguments[i] = NewInt(env, ReverseBytes(v));
932
+ } break;
933
+ case PrimitiveKind::Int64: {
934
+ int64_t v = *(int64_t *)args_ptr;
935
+ args_ptr += 2;
936
+
937
+ arguments[i] = NewInt(env, v);
938
+ } break;
939
+ case PrimitiveKind::Int64S: {
940
+ int64_t v = *(int64_t *)args_ptr;
941
+ args_ptr += 2;
942
+
943
+ arguments[i] = NewInt(env, ReverseBytes(v));
944
+ } break;
945
+ case PrimitiveKind::UInt64: {
946
+ uint64_t v = *(uint64_t *)args_ptr;
947
+ args_ptr += 2;
948
+
949
+ arguments[i] = NewInt(env, v);
950
+ } break;
951
+ case PrimitiveKind::UInt64S: {
952
+ uint64_t v = *(uint64_t *)args_ptr;
953
+ args_ptr += 2;
954
+
955
+ arguments[i] = NewInt(env, ReverseBytes(v));
956
+ } break;
957
+ case PrimitiveKind::String: {
958
+ const char *str = *(const char **)(args_ptr++);
959
+ arguments[i] = NewString(env, str);
960
+
961
+ if (param.type->dispose) {
962
+ param.type->dispose(instance, param.type, str);
963
+ }
964
+ } break;
965
+ case PrimitiveKind::String16: {
966
+ const char16_t *str16 = *(const char16_t **)(args_ptr++);
967
+ arguments[i] = NewString(env, str16);
968
+
969
+ if (param.type->dispose) {
970
+ param.type->dispose(instance, param.type, str16);
971
+ }
972
+ } break;
973
+ case PrimitiveKind::String32: {
974
+ const char32_t *str32 = *(const char32_t **)(args_ptr++);
975
+ arguments[i] = NewString(env, str32);
976
+
977
+ if (param.type->dispose) {
978
+ param.type->dispose(instance, param.type, str32);
979
+ }
980
+ } break;
981
+ case PrimitiveKind::Pointer: {
982
+ void *ptr2 = *(void **)(args_ptr++);
983
+ arguments[i] = WrapPointer(env, param.type->ref.type, ptr2);
984
+
985
+ if (param.type->dispose) {
986
+ param.type->dispose(instance, param.type, ptr2);
987
+ }
988
+ } break;
989
+ case PrimitiveKind::Callback: {
990
+ void *ptr2 = *(void **)(args_ptr++);
991
+ arguments[i] = WrapPointer(env, param.type->ref.type, ptr2);
992
+ } break;
993
+ case PrimitiveKind::Record:
994
+ case PrimitiveKind::Union: {
995
+ uint8_t *ptr = (uint8_t *)args_ptr;
996
+ arguments[i] = DecodeObject(instance, ptr, param.type);
997
+
998
+ args_ptr = (uint32_t *)AlignUp(ptr + param.type->size, 4);
999
+ } break;
1000
+ case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
1001
+ case PrimitiveKind::Float32: {
1002
+ float f = *(float *)(args_ptr++);
1003
+ arguments[i] = NewFloat(env, f);
1004
+ } break;
1005
+ case PrimitiveKind::Float64: {
1006
+ double d = *(double *)args_ptr;
1007
+ args_ptr += 2;
1008
+
1009
+ arguments[i] = NewFloat(env, d);
1010
+ } break;
1011
+
1012
+ case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
1013
+ }
1014
+ }
1015
+
1016
+ const TypeInfo *type = proto->ret.type;
1017
+
1018
+ // We're ready, make the call!
1019
+ napi_value value = CallCallback(trampoline, arguments, proto->parameters.len);
1020
+
1021
+ if (!value) [[unlikely]]
1022
+ return;
1023
+
1024
+ #define RETURN_INTEGER_32(CType) \
1025
+ do { \
1026
+ CType v; \
1027
+ if (!TryNumber(env, value, &v)) [[unlikely]] { \
1028
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
1029
+ return; \
1030
+ } \
1031
+ \
1032
+ out_reg->eax = (uint32_t)v; \
1033
+ out_reg->ret_type = 0; \
1034
+ } while (false)
1035
+ #define RETURN_INTEGER_32_SWAP(CType) \
1036
+ do { \
1037
+ CType v; \
1038
+ if (!TryNumber(env, value, &v)) [[unlikely]] { \
1039
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
1040
+ return; \
1041
+ } \
1042
+ \
1043
+ out_reg->eax = (uint32_t)ReverseBytes(v); \
1044
+ out_reg->ret_type = 0; \
1045
+ } while (false)
1046
+ #define RETURN_INTEGER_64(CType) \
1047
+ do { \
1048
+ CType v; \
1049
+ if (!TryNumber(env, value, &v)) [[unlikely]] { \
1050
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
1051
+ return; \
1052
+ } \
1053
+ \
1054
+ out_reg->eax = (uint32_t)((uint64_t)v >> 32); \
1055
+ out_reg->edx = (uint32_t)((uint64_t)v & 0xFFFFFFFFu); \
1056
+ out_reg->ret_type = 0; \
1057
+ } while (false)
1058
+ #define RETURN_INTEGER_64_SWAP(CType) \
1059
+ do { \
1060
+ CType v; \
1061
+ if (!TryNumber(env, value, &v)) [[unlikely]] { \
1062
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
1063
+ return; \
1064
+ } \
1065
+ \
1066
+ out_reg->eax = (uint32_t)((uint64_t)v >> 32); \
1067
+ out_reg->edx = (uint32_t)((uint64_t)v & 0xFFFFFFFFu); \
1068
+ out_reg->ret_type = 0; \
1069
+ } while (false)
1070
+
1071
+ switch (type->primitive) {
1072
+ case PrimitiveKind::Void: { out_reg->ret_type = 0; } break;
1073
+ case PrimitiveKind::Bool: {
1074
+ bool b;
1075
+ if (napi_get_value_bool(env, value, &b) != napi_ok) [[unlikely]] {
1076
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected boolean", GetValueType(instance, value));
1077
+ return;
1078
+ }
1079
+
1080
+ out_reg->eax = (uint32_t)b;
1081
+ out_reg->ret_type = 0;
1082
+ } break;
1083
+ case PrimitiveKind::Int8: { RETURN_INTEGER_32(int8_t); } break;
1084
+ case PrimitiveKind::UInt8: { RETURN_INTEGER_32(uint8_t); } break;
1085
+ case PrimitiveKind::Int16: { RETURN_INTEGER_32(int16_t); } break;
1086
+ case PrimitiveKind::Int16S: { RETURN_INTEGER_32_SWAP(int16_t); } break;
1087
+ case PrimitiveKind::UInt16: { RETURN_INTEGER_32(uint16_t); } break;
1088
+ case PrimitiveKind::UInt16S: { RETURN_INTEGER_32_SWAP(uint16_t); } break;
1089
+ case PrimitiveKind::Int32: { RETURN_INTEGER_32(int32_t); } break;
1090
+ case PrimitiveKind::Int32S: { RETURN_INTEGER_32_SWAP(int32_t); } break;
1091
+ case PrimitiveKind::UInt32: { RETURN_INTEGER_32(uint32_t); } break;
1092
+ case PrimitiveKind::UInt32S: { RETURN_INTEGER_32_SWAP(uint32_t); } break;
1093
+ case PrimitiveKind::Int64: { RETURN_INTEGER_64(int64_t); } break;
1094
+ case PrimitiveKind::Int64S: { RETURN_INTEGER_64_SWAP(int64_t); } break;
1095
+ case PrimitiveKind::UInt64: { RETURN_INTEGER_64(uint64_t); } break;
1096
+ case PrimitiveKind::UInt64S: { RETURN_INTEGER_64_SWAP(uint64_t); } break;
1097
+ case PrimitiveKind::String: {
1098
+ const char *str;
1099
+ if (!PushString(value, 1, &str)) [[unlikely]]
1100
+ return;
1101
+
1102
+ out_reg->eax = (uint32_t)str;
1103
+ out_reg->ret_type = 0;
1104
+ } break;
1105
+ case PrimitiveKind::String16: {
1106
+ const char16_t *str16;
1107
+ if (!PushString16(value, 1, &str16)) [[unlikely]]
1108
+ return;
1109
+
1110
+ out_reg->eax = (uint32_t)str16;
1111
+ out_reg->ret_type = 0;
1112
+ } break;
1113
+ case PrimitiveKind::String32: {
1114
+ const char32_t *str32;
1115
+ if (!PushString32(value, 1, &str32)) [[unlikely]]
1116
+ return;
1117
+
1118
+ out_reg->eax = (uint32_t)str32;
1119
+ out_reg->ret_type = 0;
1120
+ } break;
1121
+ case PrimitiveKind::Pointer: {
1122
+ void *ptr;
1123
+ if (!PushPointer(value, type, 1, &ptr)) [[unlikely]]
1124
+ return;
1125
+
1126
+ out_reg->eax = (uint32_t)ptr;
1127
+ out_reg->ret_type = 0;
1128
+ } break;
1129
+ case PrimitiveKind::Record:
1130
+ case PrimitiveKind::Union: {
1131
+ if (return_ptr) {
1132
+ if (!PushObject(value, type, return_ptr)) [[unlikely]]
1133
+ return;
1134
+ out_reg->eax = (uint32_t)return_ptr;
1135
+ } else {
1136
+ if (!PushObject(value, type, (uint8_t *)&out_reg->eax))
1137
+ return;
1138
+ }
1139
+
1140
+ out_reg->ret_type = 0;
1141
+ } break;
1142
+ case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
1143
+ case PrimitiveKind::Float32: {
1144
+ float f;
1145
+ if (!TryNumber(env, value, &f)) [[unlikely]] {
1146
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value));
1147
+ return;
1148
+ }
1149
+
1150
+ out_reg->x87.f = f;
1151
+ out_reg->ret_type = 1;
1152
+ } break;
1153
+ case PrimitiveKind::Float64: {
1154
+ double d;
1155
+ if (!TryNumber(env, value, &d)) [[unlikely]] {
1156
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value));
1157
+ return;
1158
+ }
1159
+
1160
+ out_reg->x87.d = d;
1161
+ out_reg->ret_type = 2;
1162
+ } break;
1163
+ case PrimitiveKind::Callback: {
1164
+ void *ptr;
1165
+ if (!PushCallback(value, type, &ptr)) [[unlikely]]
1166
+ return;
1167
+
1168
+ out_reg->eax = (uint32_t)ptr;
1169
+ out_reg->ret_type = 0;
1170
+ } break;
1171
+
1172
+ case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
1173
+ }
1174
+
1175
+ #undef RETURN_INTEGER_64_SWAP
1176
+ #undef RETURN_INTEGER_64
1177
+ #undef RETURN_INTEGER_32_SWAP
1178
+ #undef RETURN_INTEGER_32
1179
+
1180
+ err_guard.Disable();
1181
+ }
1182
+
1183
+ }
1184
+
1185
+ #endif