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