@psnext/slingcli 2.4.20260525-4 → 2.4.20260527-2

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 (243) hide show
  1. package/bin/sling.js +4 -1
  2. package/node_modules/@aws-sdk/core/package.json +2 -2
  3. package/node_modules/@aws-sdk/credential-provider-env/package.json +2 -2
  4. package/node_modules/@aws-sdk/credential-provider-http/dist-cjs/fromHttp/fromHttp.js +12 -6
  5. package/node_modules/@aws-sdk/credential-provider-http/dist-es/fromHttp/fromHttp.js +12 -6
  6. package/node_modules/@aws-sdk/credential-provider-http/package.json +3 -2
  7. package/node_modules/@aws-sdk/credential-provider-ini/package.json +9 -9
  8. package/node_modules/@aws-sdk/credential-provider-login/package.json +3 -3
  9. package/node_modules/@aws-sdk/credential-provider-node/package.json +7 -7
  10. package/node_modules/@aws-sdk/credential-provider-process/package.json +2 -2
  11. package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/package.json +3 -3
  12. package/node_modules/@aws-sdk/credential-provider-sso/package.json +4 -4
  13. package/node_modules/@aws-sdk/credential-provider-web-identity/package.json +3 -3
  14. package/node_modules/@aws-sdk/middleware-websocket/package.json +2 -2
  15. package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/cognito-identity/index.js +1 -1
  16. package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/signin/index.js +1 -1
  17. package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/sso/index.js +1 -1
  18. package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/sso-oidc/index.js +1 -1
  19. package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/sts/index.js +1 -1
  20. package/node_modules/@aws-sdk/nested-clients/package.json +3 -3
  21. package/node_modules/@aws-sdk/signature-v4-multi-region/package.json +1 -2
  22. package/node_modules/@aws-sdk/xml-builder/package.json +2 -2
  23. package/node_modules/@earendil-works/pi-agent-core/dist/harness/compaction/compaction.js +18 -24
  24. package/node_modules/@earendil-works/pi-agent-core/package.json +2 -2
  25. package/node_modules/@earendil-works/pi-ai/dist/cli.js +14 -0
  26. package/node_modules/@earendil-works/pi-ai/dist/models.generated.js +78 -63
  27. package/node_modules/@earendil-works/pi-ai/dist/providers/amazon-bedrock.js +2 -1
  28. package/node_modules/@earendil-works/pi-ai/dist/providers/anthropic.js +8 -22
  29. package/node_modules/@earendil-works/pi-ai/dist/providers/azure-openai-responses.js +1 -1
  30. package/node_modules/@earendil-works/pi-ai/dist/providers/images/openrouter.js +1 -1
  31. package/node_modules/@earendil-works/pi-ai/dist/providers/openai-codex-responses.js +60 -40
  32. package/node_modules/@earendil-works/pi-ai/dist/providers/openai-completions.js +1 -1
  33. package/node_modules/@earendil-works/pi-ai/dist/providers/openai-responses.js +1 -1
  34. package/node_modules/@earendil-works/pi-ai/dist/utils/oauth/device-code.js +55 -0
  35. package/node_modules/@earendil-works/pi-ai/dist/utils/oauth/github-copilot.js +45 -69
  36. package/node_modules/@earendil-works/pi-ai/dist/utils/oauth/index.js +1 -0
  37. package/node_modules/@earendil-works/pi-ai/dist/utils/overflow.js +5 -2
  38. package/node_modules/@earendil-works/pi-ai/package.json +4 -3
  39. package/node_modules/@earendil-works/pi-coding-agent/dist/cli/file-processor.js +2 -3
  40. package/node_modules/@earendil-works/pi-coding-agent/dist/config.js +18 -12
  41. package/node_modules/@earendil-works/pi-coding-agent/dist/core/agent-session-runtime.js +2 -1
  42. package/node_modules/@earendil-works/pi-coding-agent/dist/core/agent-session-services.js +3 -2
  43. package/node_modules/@earendil-works/pi-coding-agent/dist/core/agent-session.js +8 -2
  44. package/node_modules/@earendil-works/pi-coding-agent/dist/core/auth-storage.js +2 -1
  45. package/node_modules/@earendil-works/pi-coding-agent/dist/core/compaction/compaction.js +18 -24
  46. package/node_modules/@earendil-works/pi-coding-agent/dist/core/export-html/index.js +8 -6
  47. package/node_modules/@earendil-works/pi-coding-agent/dist/core/export-html/template.js +6 -3
  48. package/node_modules/@earendil-works/pi-coding-agent/dist/core/extensions/loader.js +12 -29
  49. package/node_modules/@earendil-works/pi-coding-agent/dist/core/model-registry.js +5 -1
  50. package/node_modules/@earendil-works/pi-coding-agent/dist/core/output-guard.js +52 -22
  51. package/node_modules/@earendil-works/pi-coding-agent/dist/core/package-manager.js +63 -36
  52. package/node_modules/@earendil-works/pi-coding-agent/dist/core/prompt-templates.js +6 -20
  53. package/node_modules/@earendil-works/pi-coding-agent/dist/core/resource-loader.js +38 -31
  54. package/node_modules/@earendil-works/pi-coding-agent/dist/core/sdk.js +9 -4
  55. package/node_modules/@earendil-works/pi-coding-agent/dist/core/session-manager.js +32 -24
  56. package/node_modules/@earendil-works/pi-coding-agent/dist/core/settings-manager.js +6 -13
  57. package/node_modules/@earendil-works/pi-coding-agent/dist/core/skills.js +8 -22
  58. package/node_modules/@earendil-works/pi-coding-agent/dist/core/tools/bash.js +54 -53
  59. package/node_modules/@earendil-works/pi-coding-agent/dist/core/tools/edit-diff.js +8 -1
  60. package/node_modules/@earendil-works/pi-coding-agent/dist/core/tools/edit.js +44 -81
  61. package/node_modules/@earendil-works/pi-coding-agent/dist/core/tools/file-mutation-queue.js +27 -12
  62. package/node_modules/@earendil-works/pi-coding-agent/dist/core/tools/find.js +2 -3
  63. package/node_modules/@earendil-works/pi-coding-agent/dist/core/tools/grep.js +3 -3
  64. package/node_modules/@earendil-works/pi-coding-agent/dist/core/tools/ls.js +5 -5
  65. package/node_modules/@earendil-works/pi-coding-agent/dist/core/tools/output-accumulator.js +9 -3
  66. package/node_modules/@earendil-works/pi-coding-agent/dist/core/tools/path-utils.js +39 -21
  67. package/node_modules/@earendil-works/pi-coding-agent/dist/core/tools/read.js +9 -8
  68. package/node_modules/@earendil-works/pi-coding-agent/dist/core/tools/truncate.js +12 -2
  69. package/node_modules/@earendil-works/pi-coding-agent/dist/core/tools/write.js +20 -35
  70. package/node_modules/@earendil-works/pi-coding-agent/dist/main.js +5 -6
  71. package/node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/components/config-selector.js +1 -1
  72. package/node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/components/footer.js +14 -5
  73. package/node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/components/login-dialog.js +29 -4
  74. package/node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/components/user-message.js +1 -1
  75. package/node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/interactive-mode.js +4 -4
  76. package/node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/theme/theme.js +37 -28
  77. package/node_modules/@earendil-works/pi-coding-agent/dist/modes/rpc/rpc-client.js +64 -7
  78. package/node_modules/@earendil-works/pi-coding-agent/dist/modes/rpc/rpc-mode.js +15 -3
  79. package/node_modules/@earendil-works/pi-coding-agent/dist/utils/clipboard-native.js +14 -8
  80. package/node_modules/@earendil-works/pi-coding-agent/dist/utils/image-resize-core.js +124 -0
  81. package/node_modules/@earendil-works/pi-coding-agent/dist/utils/image-resize-worker.js +31 -0
  82. package/node_modules/@earendil-works/pi-coding-agent/dist/utils/image-resize.js +75 -115
  83. package/node_modules/@earendil-works/pi-coding-agent/dist/utils/paths.js +33 -7
  84. package/node_modules/@earendil-works/pi-coding-agent/npm-shrinkwrap.json +13 -26
  85. package/node_modules/@earendil-works/pi-coding-agent/package.json +6 -6
  86. package/node_modules/@earendil-works/pi-tui/dist/components/editor.js +40 -55
  87. package/node_modules/@earendil-works/pi-tui/dist/components/input.js +2 -2
  88. package/node_modules/@earendil-works/pi-tui/dist/components/markdown.js +12 -2
  89. package/node_modules/@earendil-works/pi-tui/dist/native-modifiers.js +53 -0
  90. package/node_modules/@earendil-works/pi-tui/dist/terminal-image.js +4 -1
  91. package/node_modules/@earendil-works/pi-tui/dist/terminal.js +38 -16
  92. package/node_modules/@earendil-works/pi-tui/dist/utils.js +19 -12
  93. package/node_modules/@earendil-works/pi-tui/native/darwin/prebuilds/darwin-arm64/darwin-modifiers.node +0 -0
  94. package/node_modules/@earendil-works/pi-tui/native/darwin/prebuilds/darwin-x64/darwin-modifiers.node +0 -0
  95. package/node_modules/@earendil-works/pi-tui/native/win32/prebuilds/win32-arm64/win32-console-mode.node +0 -0
  96. package/node_modules/@earendil-works/pi-tui/native/win32/prebuilds/win32-x64/win32-console-mode.node +0 -0
  97. package/node_modules/@earendil-works/pi-tui/package.json +3 -5
  98. package/node_modules/@smithy/node-http-handler/package.json +2 -2
  99. package/node_modules/undici/lib/api/api-request.js +1 -1
  100. package/node_modules/undici/lib/cache/sqlite-cache-store.js +3 -3
  101. package/node_modules/undici/lib/core/connect.js +16 -0
  102. package/node_modules/undici/lib/core/request.js +17 -2
  103. package/node_modules/undici/lib/core/socks5-client.js +10 -5
  104. package/node_modules/undici/lib/core/socks5-utils.js +17 -22
  105. package/node_modules/undici/lib/dispatcher/client-h1.js +64 -20
  106. package/node_modules/undici/lib/dispatcher/client.js +6 -2
  107. package/node_modules/undici/lib/dispatcher/h2c-client.js +1 -1
  108. package/node_modules/undici/lib/mock/mock-call-history.js +15 -15
  109. package/node_modules/undici/lib/util/cache.js +8 -7
  110. package/node_modules/undici/lib/web/fetch/formdata-parser.js +17 -6
  111. package/node_modules/undici/lib/web/fetch/index.js +5 -2
  112. package/node_modules/undici/lib/web/webidl/index.js +5 -5
  113. package/node_modules/undici/lib/web/websocket/stream/websocketstream.js +1 -7
  114. package/node_modules/undici/package.json +1 -1
  115. package/package.json +6 -6
  116. package/slingshot/index.js +269 -266
  117. package/node_modules/koffi/CHANGELOG.md +0 -1093
  118. package/node_modules/koffi/LICENSE.txt +0 -22
  119. package/node_modules/koffi/README.md +0 -43
  120. package/node_modules/koffi/build/koffi/darwin_arm64/koffi.node +0 -0
  121. package/node_modules/koffi/build/koffi/darwin_x64/koffi.node +0 -0
  122. package/node_modules/koffi/build/koffi/freebsd_arm64/koffi.node +0 -0
  123. package/node_modules/koffi/build/koffi/freebsd_ia32/koffi.node +0 -0
  124. package/node_modules/koffi/build/koffi/freebsd_x64/koffi.node +0 -0
  125. package/node_modules/koffi/build/koffi/linux_arm64/koffi.node +0 -0
  126. package/node_modules/koffi/build/koffi/linux_armhf/koffi.node +0 -0
  127. package/node_modules/koffi/build/koffi/linux_ia32/koffi.node +0 -0
  128. package/node_modules/koffi/build/koffi/linux_loong64/koffi.node +0 -0
  129. package/node_modules/koffi/build/koffi/linux_riscv64d/koffi.node +0 -0
  130. package/node_modules/koffi/build/koffi/linux_x64/koffi.node +0 -0
  131. package/node_modules/koffi/build/koffi/musl_arm64/koffi.node +0 -0
  132. package/node_modules/koffi/build/koffi/musl_x64/koffi.node +0 -0
  133. package/node_modules/koffi/build/koffi/openbsd_ia32/koffi.node +0 -0
  134. package/node_modules/koffi/build/koffi/openbsd_x64/koffi.node +0 -0
  135. package/node_modules/koffi/build/koffi/win32_arm64/koffi.exp +0 -0
  136. package/node_modules/koffi/build/koffi/win32_arm64/koffi.lib +0 -0
  137. package/node_modules/koffi/build/koffi/win32_arm64/koffi.node +0 -0
  138. package/node_modules/koffi/build/koffi/win32_ia32/koffi.exp +0 -0
  139. package/node_modules/koffi/build/koffi/win32_ia32/koffi.lib +0 -0
  140. package/node_modules/koffi/build/koffi/win32_ia32/koffi.node +0 -0
  141. package/node_modules/koffi/build/koffi/win32_x64/koffi.exp +0 -0
  142. package/node_modules/koffi/build/koffi/win32_x64/koffi.lib +0 -0
  143. package/node_modules/koffi/build/koffi/win32_x64/koffi.node +0 -0
  144. package/node_modules/koffi/doc/benchmarks.md +0 -126
  145. package/node_modules/koffi/doc/callbacks.md +0 -210
  146. package/node_modules/koffi/doc/contribute.md +0 -148
  147. package/node_modules/koffi/doc/functions.md +0 -250
  148. package/node_modules/koffi/doc/index.md +0 -61
  149. package/node_modules/koffi/doc/input.md +0 -471
  150. package/node_modules/koffi/doc/migration.md +0 -159
  151. package/node_modules/koffi/doc/misc.md +0 -180
  152. package/node_modules/koffi/doc/output.md +0 -305
  153. package/node_modules/koffi/doc/packaging.md +0 -88
  154. package/node_modules/koffi/doc/platforms.md +0 -36
  155. package/node_modules/koffi/doc/pointers.md +0 -328
  156. package/node_modules/koffi/doc/start.md +0 -118
  157. package/node_modules/koffi/doc/unions.md +0 -186
  158. package/node_modules/koffi/doc/variables.md +0 -102
  159. package/node_modules/koffi/index.d.ts +0 -288
  160. package/node_modules/koffi/index.js +0 -634
  161. package/node_modules/koffi/indirect.js +0 -533
  162. package/node_modules/koffi/lib/native/base/base.cc +0 -11015
  163. package/node_modules/koffi/lib/native/base/base.hh +0 -6003
  164. package/node_modules/koffi/lib/native/base/crc.inc +0 -2214
  165. package/node_modules/koffi/lib/native/base/crc_gen.py +0 -72
  166. package/node_modules/koffi/lib/native/base/mimetypes.inc +0 -1248
  167. package/node_modules/koffi/lib/native/base/mimetypes_gen.py +0 -58
  168. package/node_modules/koffi/lib/native/base/tower.cc +0 -821
  169. package/node_modules/koffi/lib/native/base/tower.hh +0 -81
  170. package/node_modules/koffi/lib/native/base/unicode.inc +0 -408
  171. package/node_modules/koffi/lib/native/base/unicode_gen.py +0 -152
  172. package/node_modules/koffi/package.json +0 -38
  173. package/node_modules/koffi/src/cnoke/LICENSE.txt +0 -22
  174. package/node_modules/koffi/src/cnoke/README.md +0 -99
  175. package/node_modules/koffi/src/cnoke/assets/FindCNoke.cmake +0 -127
  176. package/node_modules/koffi/src/cnoke/assets/toolchains.json +0 -126
  177. package/node_modules/koffi/src/cnoke/assets/win_delay_hook.c +0 -36
  178. package/node_modules/koffi/src/cnoke/cnoke.js +0 -170
  179. package/node_modules/koffi/src/cnoke/package.json +0 -24
  180. package/node_modules/koffi/src/cnoke/src/builder.js +0 -511
  181. package/node_modules/koffi/src/cnoke/src/index.js +0 -10
  182. package/node_modules/koffi/src/cnoke/src/tools.js +0 -407
  183. package/node_modules/koffi/src/koffi/CMakeLists.txt +0 -182
  184. package/node_modules/koffi/src/koffi/src/abi_arm32.cc +0 -1018
  185. package/node_modules/koffi/src/koffi/src/abi_arm32_asm.S +0 -169
  186. package/node_modules/koffi/src/koffi/src/abi_arm64.cc +0 -1295
  187. package/node_modules/koffi/src/koffi/src/abi_arm64_asm.S +0 -195
  188. package/node_modules/koffi/src/koffi/src/abi_arm64_asm.asm +0 -174
  189. package/node_modules/koffi/src/koffi/src/abi_loong64.cc +0 -5
  190. package/node_modules/koffi/src/koffi/src/abi_loong64_asm.S +0 -204
  191. package/node_modules/koffi/src/koffi/src/abi_riscv64.cc +0 -915
  192. package/node_modules/koffi/src/koffi/src/abi_riscv64_asm.S +0 -203
  193. package/node_modules/koffi/src/koffi/src/abi_x64_sysv.cc +0 -939
  194. package/node_modules/koffi/src/koffi/src/abi_x64_sysv_asm.S +0 -231
  195. package/node_modules/koffi/src/koffi/src/abi_x64_win.cc +0 -715
  196. package/node_modules/koffi/src/koffi/src/abi_x64_win_asm.S +0 -166
  197. package/node_modules/koffi/src/koffi/src/abi_x64_win_asm.asm +0 -192
  198. package/node_modules/koffi/src/koffi/src/abi_x86.cc +0 -860
  199. package/node_modules/koffi/src/koffi/src/abi_x86_asm.S +0 -193
  200. package/node_modules/koffi/src/koffi/src/abi_x86_asm.asm +0 -177
  201. package/node_modules/koffi/src/koffi/src/call.cc +0 -1326
  202. package/node_modules/koffi/src/koffi/src/call.hh +0 -179
  203. package/node_modules/koffi/src/koffi/src/errno.inc +0 -462
  204. package/node_modules/koffi/src/koffi/src/ffi.cc +0 -2702
  205. package/node_modules/koffi/src/koffi/src/ffi.hh +0 -354
  206. package/node_modules/koffi/src/koffi/src/init.js +0 -105
  207. package/node_modules/koffi/src/koffi/src/parser.cc +0 -220
  208. package/node_modules/koffi/src/koffi/src/parser.hh +0 -54
  209. package/node_modules/koffi/src/koffi/src/util.cc +0 -1807
  210. package/node_modules/koffi/src/koffi/src/util.hh +0 -221
  211. package/node_modules/koffi/src/koffi/src/uv.cc +0 -193
  212. package/node_modules/koffi/src/koffi/src/uv.def +0 -10
  213. package/node_modules/koffi/src/koffi/src/uv.hh +0 -40
  214. package/node_modules/koffi/src/koffi/src/win32.cc +0 -198
  215. package/node_modules/koffi/src/koffi/src/win32.hh +0 -119
  216. package/node_modules/koffi/src/koffi/tools/write_trampolines.js +0 -77
  217. package/node_modules/koffi/vendor/node-addon-api/LICENSE.md +0 -9
  218. package/node_modules/koffi/vendor/node-addon-api/README.md +0 -95
  219. package/node_modules/koffi/vendor/node-addon-api/napi-inl.deprecated.h +0 -186
  220. package/node_modules/koffi/vendor/node-addon-api/napi-inl.h +0 -7033
  221. package/node_modules/koffi/vendor/node-addon-api/napi.h +0 -3309
  222. package/node_modules/koffi/vendor/node-api-headers/LICENSE +0 -21
  223. package/node_modules/koffi/vendor/node-api-headers/README.md +0 -95
  224. package/node_modules/koffi/vendor/node-api-headers/def/js_native_api.def +0 -125
  225. package/node_modules/koffi/vendor/node-api-headers/def/node_api.def +0 -157
  226. package/node_modules/koffi/vendor/node-api-headers/include/js_native_api.h +0 -591
  227. package/node_modules/koffi/vendor/node-api-headers/include/js_native_api_types.h +0 -210
  228. package/node_modules/koffi/vendor/node-api-headers/include/node_api.h +0 -265
  229. package/node_modules/koffi/vendor/node-api-headers/include/node_api_types.h +0 -58
  230. package/node_modules/koffi/vendor/node-api-headers/include/uv/aix.h +0 -32
  231. package/node_modules/koffi/vendor/node-api-headers/include/uv/bsd.h +0 -34
  232. package/node_modules/koffi/vendor/node-api-headers/include/uv/darwin.h +0 -61
  233. package/node_modules/koffi/vendor/node-api-headers/include/uv/errno.h +0 -483
  234. package/node_modules/koffi/vendor/node-api-headers/include/uv/linux.h +0 -34
  235. package/node_modules/koffi/vendor/node-api-headers/include/uv/os390.h +0 -33
  236. package/node_modules/koffi/vendor/node-api-headers/include/uv/posix.h +0 -31
  237. package/node_modules/koffi/vendor/node-api-headers/include/uv/sunos.h +0 -44
  238. package/node_modules/koffi/vendor/node-api-headers/include/uv/threadpool.h +0 -37
  239. package/node_modules/koffi/vendor/node-api-headers/include/uv/tree.h +0 -521
  240. package/node_modules/koffi/vendor/node-api-headers/include/uv/unix.h +0 -512
  241. package/node_modules/koffi/vendor/node-api-headers/include/uv/version.h +0 -43
  242. package/node_modules/koffi/vendor/node-api-headers/include/uv/win.h +0 -698
  243. package/node_modules/koffi/vendor/node-api-headers/include/uv.h +0 -1990
@@ -1,939 +0,0 @@
1
- // SPDX-License-Identifier: MIT
2
- // SPDX-FileCopyrightText: 2025 Niels Martignène <niels.martignene@protonmail.com>
3
-
4
- #if defined(__x86_64__) && !defined(_WIN32)
5
-
6
- #include "lib/native/base/base.hh"
7
- #include "ffi.hh"
8
- #include "call.hh"
9
- #include "util.hh"
10
-
11
- #include <napi.h>
12
-
13
- namespace K {
14
-
15
- enum class RegisterClass {
16
- NoClass = 0, // Explicitly 0
17
- Integer,
18
- SSE,
19
- Memory
20
- };
21
-
22
- struct RaxRdxRet {
23
- uint64_t rax;
24
- uint64_t rdx;
25
- };
26
- struct RaxXmm0Ret {
27
- uint64_t rax;
28
- double xmm0;
29
- };
30
- struct Xmm0RaxRet {
31
- double xmm0;
32
- uint64_t rax;
33
- };
34
- struct Xmm0Xmm1Ret {
35
- double xmm0;
36
- double xmm1;
37
- };
38
-
39
- struct BackRegisters {
40
- uint64_t rax;
41
- uint64_t rdx;
42
- double xmm0;
43
- double xmm1;
44
- };
45
-
46
- extern "C" RaxRdxRet ForwardCallGG(const void *func, uint8_t *sp, uint8_t **out_old_sp);
47
- extern "C" float ForwardCallF(const void *func, uint8_t *sp, uint8_t **out_old_sp);
48
- extern "C" Xmm0RaxRet ForwardCallDG(const void *func, uint8_t *sp, uint8_t **out_old_sp);
49
- extern "C" RaxXmm0Ret ForwardCallGD(const void *func, uint8_t *sp, uint8_t **out_old_sp);
50
- extern "C" Xmm0Xmm1Ret ForwardCallDD(const void *func, uint8_t *sp, uint8_t **out_old_sp);
51
-
52
- extern "C" RaxRdxRet ForwardCallXGG(const void *func, uint8_t *sp, uint8_t **out_old_sp);
53
- extern "C" float ForwardCallXF(const void *func, uint8_t *sp, uint8_t **out_old_sp);
54
- extern "C" Xmm0RaxRet ForwardCallXDG(const void *func, uint8_t *sp, uint8_t **out_old_sp);
55
- extern "C" RaxXmm0Ret ForwardCallXGD(const void *func, uint8_t *sp, uint8_t **out_old_sp);
56
- extern "C" Xmm0Xmm1Ret ForwardCallXDD(const void *func, uint8_t *sp, uint8_t **out_old_sp);
57
-
58
- extern "C" uint8_t Trampoline0;
59
- extern "C" uint8_t TrampolineEnd;
60
- extern "C" void *FindTrampolineStart();
61
- extern "C" void *FindTrampolineEnd();
62
-
63
- static inline RegisterClass MergeClasses(RegisterClass cls1, RegisterClass cls2)
64
- {
65
- if (cls1 == cls2)
66
- return cls1;
67
-
68
- if (cls1 == RegisterClass::NoClass)
69
- return cls2;
70
- if (cls2 == RegisterClass::NoClass)
71
- return cls1;
72
-
73
- if (cls1 == RegisterClass::Memory || cls2 == RegisterClass::Memory)
74
- return RegisterClass::Memory;
75
- if (cls1 == RegisterClass::Integer || cls2 == RegisterClass::Integer)
76
- return RegisterClass::Integer;
77
-
78
- return RegisterClass::SSE;
79
- }
80
-
81
- static Size ClassifyType(const TypeInfo *type, Size offset, Span<RegisterClass> classes)
82
- {
83
- K_ASSERT(classes.len > 0);
84
-
85
- switch (type->primitive) {
86
- case PrimitiveKind::Void: { return 0; } break;
87
-
88
- case PrimitiveKind::Bool:
89
- case PrimitiveKind::Int8:
90
- case PrimitiveKind::UInt8:
91
- case PrimitiveKind::Int16:
92
- case PrimitiveKind::Int16S:
93
- case PrimitiveKind::UInt16:
94
- case PrimitiveKind::UInt16S:
95
- case PrimitiveKind::Int32:
96
- case PrimitiveKind::Int32S:
97
- case PrimitiveKind::UInt32:
98
- case PrimitiveKind::UInt32S:
99
- case PrimitiveKind::Int64:
100
- case PrimitiveKind::Int64S:
101
- case PrimitiveKind::UInt64:
102
- case PrimitiveKind::UInt64S:
103
- case PrimitiveKind::String:
104
- case PrimitiveKind::String16:
105
- case PrimitiveKind::String32:
106
- case PrimitiveKind::Pointer:
107
- case PrimitiveKind::Callback: {
108
- classes[0] = MergeClasses(classes[0], RegisterClass::Integer);
109
- return 1;
110
- } break;
111
- case PrimitiveKind::Record: {
112
- if (type->size > 64) {
113
- classes[0] = MergeClasses(classes[0], RegisterClass::Memory);
114
- return 1;
115
- }
116
-
117
- for (const RecordMember &member: type->members) {
118
- Size member_offset = offset + member.offset;
119
- Size start = member_offset / 8;
120
- ClassifyType(member.type, member_offset % 8, classes.Take(start, classes.len - start));
121
- }
122
- offset += type->size;
123
-
124
- return (offset + 7) / 8;
125
- } break;
126
- case PrimitiveKind::Union: {
127
- if (type->size > 64) {
128
- classes[0] = MergeClasses(classes[0], RegisterClass::Memory);
129
- return 1;
130
- }
131
-
132
- for (const RecordMember &member: type->members) {
133
- Size start = offset / 8;
134
- ClassifyType(member.type, offset % 8, classes.Take(start, classes.len - start));
135
- }
136
- offset += type->size;
137
-
138
- return (offset + 7) / 8;
139
- } break;
140
- case PrimitiveKind::Array: {
141
- if (type->size > 64) {
142
- classes[0] = MergeClasses(classes[0], RegisterClass::Memory);
143
- return 1;
144
- }
145
-
146
- Size len = type->size / type->ref.type->size;
147
-
148
- for (Size i = 0; i < len; i++) {
149
- Size start = offset / 8;
150
- ClassifyType(type->ref.type, offset % 8, classes.Take(start, classes.len - start));
151
- offset += type->ref.type->size;
152
- }
153
-
154
- return (offset + 7) / 8;
155
- } break;
156
- case PrimitiveKind::Float32:
157
- case PrimitiveKind::Float64: {
158
- classes[0] = MergeClasses(classes[0], RegisterClass::SSE);
159
- return 1;
160
- } break;
161
-
162
- case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
163
- }
164
-
165
- K_UNREACHABLE();
166
- }
167
-
168
- static void AnalyseParameter(ParameterInfo *param, int gpr_avail, int xmm_avail)
169
- {
170
- LocalArray<RegisterClass, 8> classes = {};
171
- classes.len = ClassifyType(param->type, 0, classes.data);
172
-
173
- if (!classes.len)
174
- return;
175
- if (classes.len > 2) {
176
- param->use_memory = true;
177
- return;
178
- }
179
-
180
- int gpr_count = 0;
181
- int xmm_count = 0;
182
-
183
- for (RegisterClass cls: classes) {
184
- K_ASSERT(cls != RegisterClass::NoClass);
185
-
186
- if (cls == RegisterClass::Memory) {
187
- param->use_memory = true;
188
- return;
189
- }
190
-
191
- gpr_count += (cls == RegisterClass::Integer);
192
- xmm_count += (cls == RegisterClass::SSE);
193
- }
194
-
195
- if (gpr_count <= gpr_avail && xmm_count <= xmm_avail) {
196
- param->gpr_count = (int8_t)gpr_count;
197
- param->xmm_count = (int8_t)xmm_count;
198
- param->gpr_first = (classes[0] == RegisterClass::Integer);
199
- } else {
200
- param->use_memory = true;
201
- }
202
- }
203
-
204
- bool AnalyseFunction(Napi::Env, InstanceData *, FunctionInfo *func)
205
- {
206
- AnalyseParameter(&func->ret, 2, 2);
207
-
208
- int gpr_avail = 6 - func->ret.use_memory;
209
- int xmm_avail = 8;
210
-
211
- for (ParameterInfo &param: func->parameters) {
212
- AnalyseParameter(&param, gpr_avail, xmm_avail);
213
-
214
- gpr_avail -= param.gpr_count;
215
- xmm_avail -= param.xmm_count;
216
-
217
- func->args_size += AlignLen(param.type->size, 16);
218
- }
219
-
220
- func->forward_fp = (xmm_avail < 8);
221
-
222
- return true;
223
- }
224
-
225
- bool CallData::Prepare(const FunctionInfo *func, const Napi::CallbackInfo &info)
226
- {
227
- uint64_t *args_ptr = nullptr;
228
- uint64_t *gpr_ptr = nullptr;
229
- uint64_t *xmm_ptr = nullptr;
230
-
231
- // Return through registers unless it's too big
232
- if (!AllocStack(func->args_size, 16, &args_ptr)) [[unlikely]]
233
- return false;
234
- if (!AllocStack(8 * 8, 8, &xmm_ptr)) [[unlikely]]
235
- return false;
236
- if (!AllocStack(6 * 8, 8, &gpr_ptr)) [[unlikely]]
237
- return false;
238
- if (func->ret.use_memory) {
239
- return_ptr = AllocHeap(func->ret.type->size, 16);
240
- *(uint8_t **)(gpr_ptr++) = return_ptr;
241
- }
242
-
243
- #define PUSH_INTEGER(CType) \
244
- do { \
245
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] { \
246
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
247
- return false; \
248
- } \
249
- \
250
- CType v = GetNumber<CType>(value); \
251
- *((param.gpr_count ? gpr_ptr : args_ptr)++) = (uint64_t)v; \
252
- } while (false)
253
- #define PUSH_INTEGER_SWAP(CType) \
254
- do { \
255
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] { \
256
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
257
- return false; \
258
- } \
259
- \
260
- CType v = GetNumber<CType>(value); \
261
- *((param.gpr_count ? gpr_ptr : args_ptr)++) = (uint64_t)ReverseBytes(v); \
262
- } while (false)
263
-
264
- // Push arguments
265
- for (Size i = 0; i < func->parameters.len; i++) {
266
- const ParameterInfo &param = func->parameters[i];
267
- K_ASSERT(param.directions >= 1 && param.directions <= 3);
268
-
269
- Napi::Value value = info[param.offset];
270
-
271
- switch (param.type->primitive) {
272
- case PrimitiveKind::Void: { K_UNREACHABLE(); } break;
273
-
274
- case PrimitiveKind::Bool: {
275
- if (!value.IsBoolean()) [[unlikely]] {
276
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argmument %2, expected boolean", GetValueType(instance, value));
277
- return false;
278
- }
279
-
280
- bool b = value.As<Napi::Boolean>();
281
- *((param.gpr_count ? gpr_ptr : args_ptr)++) = (uint64_t)b;
282
- } break;
283
- case PrimitiveKind::Int8: { PUSH_INTEGER(int8_t); } break;
284
- case PrimitiveKind::UInt8: { PUSH_INTEGER(uint8_t); } break;
285
- case PrimitiveKind::Int16: { PUSH_INTEGER(int16_t); } break;
286
- case PrimitiveKind::Int16S: { PUSH_INTEGER_SWAP(int16_t); } break;
287
- case PrimitiveKind::UInt16: { PUSH_INTEGER(uint16_t); } break;
288
- case PrimitiveKind::UInt16S: { PUSH_INTEGER_SWAP(uint16_t); } break;
289
- case PrimitiveKind::Int32: { PUSH_INTEGER(int32_t); } break;
290
- case PrimitiveKind::Int32S: { PUSH_INTEGER_SWAP(int32_t); } break;
291
- case PrimitiveKind::UInt32: { PUSH_INTEGER(uint32_t); } break;
292
- case PrimitiveKind::UInt32S: { PUSH_INTEGER_SWAP(uint32_t); } break;
293
- case PrimitiveKind::Int64: { PUSH_INTEGER(int64_t); } break;
294
- case PrimitiveKind::Int64S: { PUSH_INTEGER_SWAP(int64_t); } break;
295
- case PrimitiveKind::UInt64: { PUSH_INTEGER(uint64_t); } break;
296
- case PrimitiveKind::UInt64S: { PUSH_INTEGER_SWAP(uint64_t); } break;
297
- case PrimitiveKind::String: {
298
- const char *str;
299
- if (!PushString(value, param.directions, &str)) [[unlikely]]
300
- return false;
301
-
302
- *(const char **)((param.gpr_count ? gpr_ptr : args_ptr)++) = str;
303
- } break;
304
- case PrimitiveKind::String16: {
305
- const char16_t *str16;
306
- if (!PushString16(value, param.directions, &str16)) [[unlikely]]
307
- return false;
308
-
309
- *(const char16_t **)((param.gpr_count ? gpr_ptr : args_ptr)++) = str16;
310
- } break;
311
- case PrimitiveKind::String32: {
312
- const char32_t *str32;
313
- if (!PushString32(value, param.directions, &str32)) [[unlikely]]
314
- return false;
315
-
316
- *(const char32_t **)((param.gpr_count ? gpr_ptr : args_ptr)++) = str32;
317
- } break;
318
- case PrimitiveKind::Pointer: {
319
- void *ptr;
320
- if (!PushPointer(value, param.type, param.directions, &ptr)) [[unlikely]]
321
- return false;
322
-
323
- *(void **)((param.gpr_count ? gpr_ptr : args_ptr)++) = ptr;
324
- } break;
325
- case PrimitiveKind::Record:
326
- case PrimitiveKind::Union: {
327
- if (!IsObject(value)) [[unlikely]] {
328
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected object", GetValueType(instance, value));
329
- return false;
330
- }
331
-
332
- Napi::Object obj = value.As<Napi::Object>();
333
-
334
- if (param.gpr_count || param.xmm_count) {
335
- K_ASSERT(param.type->size <= 16);
336
-
337
- uint64_t buf[2] = {};
338
- if (!PushObject(obj, param.type, (uint8_t *)buf))
339
- return false;
340
-
341
- if (param.gpr_first) {
342
- *(gpr_ptr++) = buf[0];
343
- if (param.gpr_count == 2) {
344
- *(gpr_ptr++) = buf[1];
345
- } else if (param.xmm_count == 1) {
346
- *(xmm_ptr++) = buf[1];
347
- }
348
- } else {
349
- *(xmm_ptr++) = buf[0];
350
- if (param.xmm_count == 2) {
351
- *(xmm_ptr++) = buf[1];
352
- } else if (param.gpr_count == 1) {
353
- *(gpr_ptr++) = buf[1];
354
- }
355
- }
356
- } else if (param.use_memory) {
357
- args_ptr = AlignUp(args_ptr, param.type->align);
358
- if (!PushObject(obj, param.type, (uint8_t *)args_ptr))
359
- return false;
360
- args_ptr += (param.type->size + 7) / 8;
361
- }
362
- } break;
363
- case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
364
- case PrimitiveKind::Float32: {
365
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] {
366
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value));
367
- return false;
368
- }
369
-
370
- float f = GetNumber<float>(value);
371
- uint64_t *ptr = (param.xmm_count ? xmm_ptr : args_ptr)++;
372
-
373
- memset((uint8_t *)ptr + 4, 0, 4);
374
- *(float *)ptr = f;
375
- } break;
376
- case PrimitiveKind::Float64: {
377
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] {
378
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value));
379
- return false;
380
- }
381
-
382
- double d = GetNumber<double>(value);
383
- *(double *)((param.xmm_count ? xmm_ptr : args_ptr)++) = d;
384
- } break;
385
- case PrimitiveKind::Callback: {
386
- void *ptr;
387
- if (!PushCallback(value, param.type, &ptr)) [[unlikely]]
388
- return false;
389
-
390
- *(void **)((param.gpr_count ? gpr_ptr : args_ptr)++) = ptr;
391
- } break;
392
-
393
- case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
394
- }
395
- }
396
-
397
- #undef PUSH_INTEGER_SWAP
398
- #undef PUSH_INTEGER
399
-
400
- new_sp = mem->stack.end();
401
-
402
- return true;
403
- }
404
-
405
- void CallData::Execute(const FunctionInfo *func, void *native)
406
- {
407
- #define PERFORM_CALL(Suffix) \
408
- ([&]() { \
409
- auto ret = (func->forward_fp ? ForwardCallX ## Suffix(native, new_sp, &old_sp) \
410
- : ForwardCall ## Suffix(native, new_sp, &old_sp)); \
411
- return ret; \
412
- })()
413
-
414
- // Execute and convert return value
415
- switch (func->ret.type->primitive) {
416
- case PrimitiveKind::Void:
417
- case PrimitiveKind::Bool:
418
- case PrimitiveKind::Int8:
419
- case PrimitiveKind::UInt8:
420
- case PrimitiveKind::Int16:
421
- case PrimitiveKind::Int16S:
422
- case PrimitiveKind::UInt16:
423
- case PrimitiveKind::UInt16S:
424
- case PrimitiveKind::Int32:
425
- case PrimitiveKind::Int32S:
426
- case PrimitiveKind::UInt32:
427
- case PrimitiveKind::UInt32S:
428
- case PrimitiveKind::Int64:
429
- case PrimitiveKind::Int64S:
430
- case PrimitiveKind::UInt64:
431
- case PrimitiveKind::UInt64S:
432
- case PrimitiveKind::String:
433
- case PrimitiveKind::String16:
434
- case PrimitiveKind::String32:
435
- case PrimitiveKind::Pointer:
436
- case PrimitiveKind::Callback: { result.u64 = PERFORM_CALL(GG).rax; } break;
437
- case PrimitiveKind::Record:
438
- case PrimitiveKind::Union: {
439
- if (func->ret.gpr_first && !func->ret.xmm_count) {
440
- RaxRdxRet ret = PERFORM_CALL(GG);
441
- memcpy(&result.buf, &ret, K_SIZE(ret));
442
- } else if (func->ret.gpr_first) {
443
- RaxXmm0Ret ret = PERFORM_CALL(GD);
444
- memcpy(&result.buf, &ret, K_SIZE(ret));
445
- } else if (func->ret.xmm_count == 2) {
446
- Xmm0Xmm1Ret ret = PERFORM_CALL(DD);
447
- memcpy(&result.buf, &ret, K_SIZE(ret));
448
- } else {
449
- Xmm0RaxRet ret = PERFORM_CALL(DG);
450
- memcpy(&result.buf, &ret, K_SIZE(ret));
451
- }
452
- } break;
453
- case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
454
- case PrimitiveKind::Float32: { result.f = PERFORM_CALL(F); } break;
455
- case PrimitiveKind::Float64: { result.d = PERFORM_CALL(DG).xmm0; } break;
456
-
457
- case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
458
- }
459
-
460
- #undef PERFORM_CALL
461
- }
462
-
463
- Napi::Value CallData::Complete(const FunctionInfo *func)
464
- {
465
- K_DEFER {
466
- PopOutArguments();
467
-
468
- if (func->ret.type->dispose) {
469
- func->ret.type->dispose(env, func->ret.type, result.ptr);
470
- }
471
- };
472
-
473
- switch (func->ret.type->primitive) {
474
- case PrimitiveKind::Void: return env.Undefined();
475
- case PrimitiveKind::Bool: return Napi::Boolean::New(env, result.u8 & 0x1);
476
- case PrimitiveKind::Int8: return Napi::Number::New(env, (double)result.i8);
477
- case PrimitiveKind::UInt8: return Napi::Number::New(env, (double)result.u8);
478
- case PrimitiveKind::Int16: return Napi::Number::New(env, (double)result.i16);
479
- case PrimitiveKind::Int16S: return Napi::Number::New(env, (double)ReverseBytes(result.i16));
480
- case PrimitiveKind::UInt16: return Napi::Number::New(env, (double)result.u16);
481
- case PrimitiveKind::UInt16S: return Napi::Number::New(env, (double)ReverseBytes(result.u16));
482
- case PrimitiveKind::Int32: return Napi::Number::New(env, (double)result.i32);
483
- case PrimitiveKind::Int32S: return Napi::Number::New(env, (double)ReverseBytes(result.i32));
484
- case PrimitiveKind::UInt32: return Napi::Number::New(env, (double)result.u32);
485
- case PrimitiveKind::UInt32S: return Napi::Number::New(env, (double)ReverseBytes(result.u32));
486
- case PrimitiveKind::Int64: return NewBigInt(env, result.i64);
487
- case PrimitiveKind::Int64S: return NewBigInt(env, ReverseBytes(result.i64));
488
- case PrimitiveKind::UInt64: return NewBigInt(env, result.u64);
489
- case PrimitiveKind::UInt64S: return NewBigInt(env, ReverseBytes(result.u64));
490
- case PrimitiveKind::String: return result.ptr ? Napi::String::New(env, (const char *)result.ptr) : env.Null();
491
- case PrimitiveKind::String16: return result.ptr ? Napi::String::New(env, (const char16_t *)result.ptr) : env.Null();
492
- case PrimitiveKind::String32: return result.ptr ? MakeStringFromUTF32(env, (const char32_t *)result.ptr) : env.Null();
493
- case PrimitiveKind::Pointer:
494
- case PrimitiveKind::Callback: {
495
- if (result.ptr) {
496
- Napi::External<void> external = Napi::External<void>::New(env, result.ptr);
497
- SetValueTag(external, func->ret.type->ref.marker);
498
-
499
- return external;
500
- } else {
501
- return env.Null();
502
- }
503
- } break;
504
- case PrimitiveKind::Record:
505
- case PrimitiveKind::Union: {
506
- const uint8_t *ptr = return_ptr ? (const uint8_t *)return_ptr
507
- : (const uint8_t *)&result.buf;
508
-
509
- Napi::Object obj = DecodeObject(env, ptr, func->ret.type);
510
- return obj;
511
- } break;
512
- case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
513
- case PrimitiveKind::Float32: return Napi::Number::New(env, (double)result.f);
514
- case PrimitiveKind::Float64: return Napi::Number::New(env, result.d);
515
-
516
- case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
517
- }
518
-
519
- K_UNREACHABLE();
520
- }
521
-
522
- void CallData::Relay(Size idx, uint8_t *sp)
523
- {
524
- const TrampolineInfo &trampoline = shared.trampolines[idx];
525
-
526
- uint8_t *own_sp = sp;
527
- uint8_t *caller_sp = sp + 160;
528
- BackRegisters *out_reg = (BackRegisters *)(sp + 112);
529
-
530
- const FunctionInfo *proto = trampoline.proto;
531
- Napi::Function func = trampoline.func.Value();
532
-
533
- uint64_t *gpr_ptr = (uint64_t *)own_sp;
534
- uint64_t *xmm_ptr = gpr_ptr + 6;
535
- uint64_t *args_ptr = (uint64_t *)caller_sp;
536
-
537
- uint8_t *return_ptr = proto->ret.use_memory ? (uint8_t *)gpr_ptr[0] : nullptr;
538
- gpr_ptr += proto->ret.use_memory;
539
-
540
- K_DEFER_N(err_guard) { memset(out_reg, 0, K_SIZE(*out_reg)); };
541
-
542
- LocalArray<napi_value, MaxParameters + 1> arguments;
543
-
544
- arguments.Append(!trampoline.recv.IsEmpty() ? trampoline.recv.Value() : env.Undefined());
545
-
546
- // Convert to JS arguments
547
- for (Size i = 0; i < proto->parameters.len; i++) {
548
- const ParameterInfo &param = proto->parameters[i];
549
- K_ASSERT(param.directions >= 1 && param.directions <= 3);
550
-
551
- switch (param.type->primitive) {
552
- case PrimitiveKind::Void: { K_UNREACHABLE(); } break;
553
-
554
- case PrimitiveKind::Bool: {
555
- bool b = *(bool *)((param.gpr_count ? gpr_ptr : args_ptr)++);
556
-
557
- Napi::Value arg = Napi::Boolean::New(env, b);
558
- arguments.Append(arg);
559
- } break;
560
- case PrimitiveKind::Int8: {
561
- double d = (double)*(int8_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
562
-
563
- Napi::Value arg = Napi::Number::New(env, d);
564
- arguments.Append(arg);
565
- } break;
566
- case PrimitiveKind::UInt8: {
567
- double d = (double)*(uint8_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
568
-
569
- Napi::Value arg = Napi::Number::New(env, d);
570
- arguments.Append(arg);
571
- } break;
572
- case PrimitiveKind::Int16: {
573
- double d = (double)*(int16_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
574
-
575
- Napi::Value arg = Napi::Number::New(env, d);
576
- arguments.Append(arg);
577
- } break;
578
- case PrimitiveKind::Int16S: {
579
- int16_t v = *(int16_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
580
- double d = (double)ReverseBytes(v);
581
-
582
- Napi::Value arg = Napi::Number::New(env, d);
583
- arguments.Append(arg);
584
- } break;
585
- case PrimitiveKind::UInt16: {
586
- double d = (double)*(uint16_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
587
-
588
- Napi::Value arg = Napi::Number::New(env, d);
589
- arguments.Append(arg);
590
- } break;
591
- case PrimitiveKind::UInt16S: {
592
- uint16_t v = *(uint16_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
593
- double d = (double)ReverseBytes(v);
594
-
595
- Napi::Value arg = Napi::Number::New(env, d);
596
- arguments.Append(arg);
597
- } break;
598
- case PrimitiveKind::Int32: {
599
- double d = (double)*(int32_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
600
-
601
- Napi::Value arg = Napi::Number::New(env, d);
602
- arguments.Append(arg);
603
- } break;
604
- case PrimitiveKind::Int32S: {
605
- int32_t v = *(int32_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
606
- double d = (double)ReverseBytes(v);
607
-
608
- Napi::Value arg = Napi::Number::New(env, d);
609
- arguments.Append(arg);
610
- } break;
611
- case PrimitiveKind::UInt32: {
612
- double d = (double)*(uint32_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
613
-
614
- Napi::Value arg = Napi::Number::New(env, d);
615
- arguments.Append(arg);
616
- } break;
617
- case PrimitiveKind::UInt32S: {
618
- uint32_t v = *(uint32_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
619
- double d = (double)ReverseBytes(v);
620
-
621
- Napi::Value arg = Napi::Number::New(env, d);
622
- arguments.Append(arg);
623
- } break;
624
- case PrimitiveKind::Int64: {
625
- int64_t v = *(int64_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
626
-
627
- Napi::Value arg = NewBigInt(env, v);
628
- arguments.Append(arg);
629
- } break;
630
- case PrimitiveKind::Int64S: {
631
- int64_t v = *(int64_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
632
-
633
- Napi::Value arg = NewBigInt(env, ReverseBytes(v));
634
- arguments.Append(arg);
635
- } break;
636
- case PrimitiveKind::UInt64: {
637
- uint64_t v = *(uint64_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
638
-
639
- Napi::Value arg = NewBigInt(env, v);
640
- arguments.Append(arg);
641
- } break;
642
- case PrimitiveKind::UInt64S: {
643
- uint64_t v = *(uint64_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
644
-
645
- Napi::Value arg = NewBigInt(env, ReverseBytes(v));
646
- arguments.Append(arg);
647
- } break;
648
- case PrimitiveKind::String: {
649
- const char *str = *(const char **)((param.gpr_count ? gpr_ptr : args_ptr)++);
650
-
651
- Napi::Value arg = str ? Napi::String::New(env, str) : env.Null();
652
- arguments.Append(arg);
653
-
654
- if (param.type->dispose) {
655
- param.type->dispose(env, param.type, str);
656
- }
657
- } break;
658
- case PrimitiveKind::String16: {
659
- const char16_t *str16 = *(const char16_t **)((param.gpr_count ? gpr_ptr : args_ptr)++);
660
-
661
- Napi::Value arg = str16 ? Napi::String::New(env, str16) : env.Null();
662
- arguments.Append(arg);
663
-
664
- if (param.type->dispose) {
665
- param.type->dispose(env, param.type, str16);
666
- }
667
- } break;
668
- case PrimitiveKind::String32: {
669
- const char32_t *str32 = *(const char32_t **)((param.gpr_count ? gpr_ptr : args_ptr)++);
670
-
671
- Napi::Value arg = str32 ? MakeStringFromUTF32(env, str32) : env.Null();
672
- arguments.Append(arg);
673
- } break;
674
- case PrimitiveKind::Pointer:
675
- case PrimitiveKind::Callback: {
676
- void *ptr2 = *(void **)((param.gpr_count ? gpr_ptr : args_ptr)++);
677
-
678
- if (ptr2) {
679
- Napi::External<void> external = Napi::External<void>::New(env, ptr2);
680
- SetValueTag(external, param.type->ref.marker);
681
-
682
- arguments.Append(external);
683
- } else {
684
- arguments.Append(env.Null());
685
- }
686
-
687
- if (param.type->dispose) {
688
- param.type->dispose(env, param.type, ptr2);
689
- }
690
- } break;
691
- case PrimitiveKind::Record:
692
- case PrimitiveKind::Union: {
693
- if (param.gpr_count || param.xmm_count) {
694
- K_ASSERT(param.type->size <= 16);
695
-
696
- uint64_t buf[2] = {};
697
-
698
- if (param.gpr_first) {
699
- buf[0] = *(gpr_ptr++);
700
- if (param.gpr_count == 2) {
701
- buf[1] = *(gpr_ptr++);
702
- } else if (param.xmm_count == 1) {
703
- buf[1] = *(xmm_ptr++);
704
- }
705
- } else {
706
- buf[0] = *(xmm_ptr++);
707
- if (param.xmm_count == 2) {
708
- buf[1] = *(xmm_ptr++);
709
- } else if (param.gpr_count == 1) {
710
- buf[1] = *(gpr_ptr++);
711
- }
712
- }
713
-
714
- Napi::Object obj = DecodeObject(env, (const uint8_t *)buf, param.type);
715
- arguments.Append(obj);
716
- } else if (param.use_memory) {
717
- args_ptr = AlignUp(args_ptr, param.type->align);
718
-
719
- Napi::Object obj = DecodeObject(env, (const uint8_t *)args_ptr, param.type);
720
- arguments.Append(obj);
721
-
722
- args_ptr += (param.type->size + 7) / 8;
723
- }
724
- } break;
725
- case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
726
- case PrimitiveKind::Float32: {
727
- float f = *(float *)((param.xmm_count ? xmm_ptr : args_ptr)++);
728
-
729
- Napi::Value arg = Napi::Number::New(env, (double)f);
730
- arguments.Append(arg);
731
- } break;
732
- case PrimitiveKind::Float64: {
733
- double d = *(double *)((param.xmm_count ? xmm_ptr : args_ptr)++);
734
-
735
- Napi::Value arg = Napi::Number::New(env, d);
736
- arguments.Append(arg);
737
- } break;
738
-
739
- case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
740
- }
741
- }
742
-
743
- const TypeInfo *type = proto->ret.type;
744
-
745
- // Make the call!
746
- Napi::Value value = func.Call(arguments[0], arguments.len - 1, arguments.data + 1);
747
-
748
- if (env.IsExceptionPending()) [[unlikely]]
749
- return;
750
-
751
- #define RETURN_INTEGER(CType) \
752
- do { \
753
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] { \
754
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
755
- return; \
756
- } \
757
- \
758
- CType v = GetNumber<CType>(value); \
759
- out_reg->rax = (uint64_t)v; \
760
- } while (false)
761
- #define RETURN_INTEGER_SWAP(CType) \
762
- do { \
763
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] { \
764
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
765
- return; \
766
- } \
767
- \
768
- CType v = GetNumber<CType>(value); \
769
- out_reg->rax = (uint64_t)ReverseBytes(v); \
770
- } while (false)
771
-
772
- // Convert the result
773
- switch (type->primitive) {
774
- case PrimitiveKind::Void: {} break;
775
- case PrimitiveKind::Bool: {
776
- if (!value.IsBoolean()) [[unlikely]] {
777
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected boolean", GetValueType(instance, value));
778
- return;
779
- }
780
-
781
- bool b = value.As<Napi::Boolean>();
782
- out_reg->rax = (uint64_t)b;
783
- } break;
784
- case PrimitiveKind::Int8: { RETURN_INTEGER(int8_t); } break;
785
- case PrimitiveKind::UInt8: { RETURN_INTEGER(uint8_t); } break;
786
- case PrimitiveKind::Int16: { RETURN_INTEGER(int16_t); } break;
787
- case PrimitiveKind::Int16S: { RETURN_INTEGER_SWAP(int16_t); } break;
788
- case PrimitiveKind::UInt16: { RETURN_INTEGER(uint16_t); } break;
789
- case PrimitiveKind::UInt16S: { RETURN_INTEGER_SWAP(uint16_t); } break;
790
- case PrimitiveKind::Int32: { RETURN_INTEGER(int32_t); } break;
791
- case PrimitiveKind::Int32S: { RETURN_INTEGER_SWAP(int32_t); } break;
792
- case PrimitiveKind::UInt32: { RETURN_INTEGER(uint32_t); } break;
793
- case PrimitiveKind::UInt32S: { RETURN_INTEGER_SWAP(uint32_t); } break;
794
- case PrimitiveKind::Int64: { RETURN_INTEGER(int64_t); } break;
795
- case PrimitiveKind::Int64S: { RETURN_INTEGER_SWAP(int64_t); } break;
796
- case PrimitiveKind::UInt64: { RETURN_INTEGER(uint64_t); } break;
797
- case PrimitiveKind::UInt64S: { RETURN_INTEGER_SWAP(uint64_t); } break;
798
- case PrimitiveKind::String: {
799
- const char *str;
800
- if (!PushString(value, 1, &str)) [[unlikely]]
801
- return;
802
-
803
- out_reg->rax = (uint64_t)str;
804
- } break;
805
- case PrimitiveKind::String16: {
806
- const char16_t *str16;
807
- if (!PushString16(value, 1, &str16)) [[unlikely]]
808
- return;
809
-
810
- out_reg->rax = (uint64_t)str16;
811
- } break;
812
- case PrimitiveKind::String32: {
813
- const char32_t *str32;
814
- if (!PushString32(value, 1, &str32)) [[unlikely]]
815
- return;
816
-
817
- out_reg->rax = (uint64_t)str32;
818
- } break;
819
- case PrimitiveKind::Pointer: {
820
- uint8_t *ptr;
821
-
822
- if (CheckValueTag(value, type->ref.marker)) {
823
- ptr = value.As<Napi::External<uint8_t>>().Data();
824
- } else if (IsObject(value) && (type->ref.type->primitive == PrimitiveKind::Record ||
825
- type->ref.type->primitive == PrimitiveKind::Union)) {
826
- Napi::Object obj = value.As<Napi::Object>();
827
-
828
- ptr = AllocHeap(type->ref.type->size, 16);
829
-
830
- if (!PushObject(obj, type->ref.type, ptr))
831
- return;
832
- } else if (IsNullOrUndefined(value)) {
833
- ptr = nullptr;
834
- } else {
835
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected %2", GetValueType(instance, value), type->name);
836
- return;
837
- }
838
-
839
- out_reg->rax = (uint64_t)ptr;
840
- } break;
841
- case PrimitiveKind::Record:
842
- case PrimitiveKind::Union: {
843
- if (!IsObject(value)) [[unlikely]] {
844
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected object", GetValueType(instance, value));
845
- return;
846
- }
847
-
848
- Napi::Object obj = value.As<Napi::Object>();
849
-
850
- if (return_ptr) {
851
- if (!PushObject(obj, type, return_ptr))
852
- return;
853
- out_reg->rax = (uint64_t)return_ptr;
854
- } else {
855
- K_ASSERT(type->size <= 16);
856
-
857
- uint8_t buf[16] = {};
858
- if (!PushObject(obj, type, buf))
859
- return;
860
-
861
- if (proto->ret.gpr_first && !proto->ret.xmm_count) {
862
- memcpy(&out_reg->rax, buf + 0, 8);
863
- memcpy(&out_reg->rdx, buf + 8, 8);
864
- } else if (proto->ret.gpr_first) {
865
- memcpy(&out_reg->rax, buf + 0, 8);
866
- memcpy(&out_reg->xmm0, buf + 8, 8);
867
- } else if (proto->ret.xmm_count == 2) {
868
- memcpy(&out_reg->xmm0, buf + 0, 8);
869
- memcpy(&out_reg->xmm1, buf + 8, 8);
870
- } else {
871
- memcpy(&out_reg->xmm0, buf + 0, 8);
872
- memcpy(&out_reg->rax, buf + 8, 8);
873
- }
874
- }
875
- } break;
876
- case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
877
- case PrimitiveKind::Float32: {
878
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] {
879
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value));
880
- return;
881
- }
882
-
883
- float f = GetNumber<float>(value);
884
-
885
- memset((uint8_t *)&out_reg->xmm0 + 4, 0, 4);
886
- memcpy(&out_reg->xmm0, &f, 4);
887
- } break;
888
- case PrimitiveKind::Float64: {
889
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] {
890
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value));
891
- return;
892
- }
893
-
894
- double d = GetNumber<double>(value);
895
- out_reg->xmm0 = d;
896
- } break;
897
- case PrimitiveKind::Callback: {
898
- void *ptr;
899
-
900
- if (value.IsFunction()) {
901
- Napi::Function func2 = value.As<Napi::Function>();
902
-
903
- ptr = ReserveTrampoline(type->ref.proto, func2);
904
- if (!ptr) [[unlikely]]
905
- return;
906
- } else if (CheckValueTag(value, type->ref.marker)) {
907
- ptr = value.As<Napi::External<void>>().Data();
908
- } else if (IsNullOrUndefined(value)) {
909
- ptr = nullptr;
910
- } else {
911
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected %2", GetValueType(instance, value), type->name);
912
- return;
913
- }
914
-
915
- out_reg->rax = (uint64_t)ptr;
916
- } break;
917
-
918
- case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
919
- }
920
-
921
- #undef RETURN_INTEGER_SWAP
922
- #undef RETURN_INTEGER
923
-
924
- err_guard.Disable();
925
- }
926
-
927
- void *FindTrampolineStart()
928
- {
929
- return &Trampoline0;
930
- }
931
-
932
- void *FindTrampolineEnd()
933
- {
934
- return &TrampolineEnd;
935
- }
936
-
937
- }
938
-
939
- #endif