koffi 2.16.1 → 3.0.0-alpha.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 (84) hide show
  1. package/CHANGELOG.md +2 -2
  2. package/README.md +8 -8
  3. package/build/koffi/darwin_arm64/koffi.node +0 -0
  4. package/build/koffi/darwin_x64/koffi.node +0 -0
  5. package/build/koffi/freebsd_arm64/koffi.node +0 -0
  6. package/build/koffi/freebsd_ia32/koffi.node +0 -0
  7. package/build/koffi/freebsd_x64/koffi.node +0 -0
  8. package/build/koffi/linux_arm64/koffi.node +0 -0
  9. package/build/koffi/linux_ia32/koffi.node +0 -0
  10. package/build/koffi/linux_x64/koffi.node +0 -0
  11. package/build/koffi/musl_arm64/koffi.node +0 -0
  12. package/build/koffi/musl_x64/koffi.node +0 -0
  13. package/build/koffi/openbsd_ia32/koffi.node +0 -0
  14. package/build/koffi/openbsd_x64/koffi.node +0 -0
  15. package/build/koffi/win32_ia32/koffi.node +0 -0
  16. package/build/koffi/win32_x64/koffi.node +0 -0
  17. package/doc/benchmarks.md +33 -33
  18. package/doc/benchmarks.xlsx +0 -0
  19. package/doc/contribute.md +37 -25
  20. package/doc/index.md +8 -8
  21. package/doc/variables.md +1 -1
  22. package/index.d.ts +11 -9
  23. package/index.js +1 -634
  24. package/indirect.js +1 -533
  25. package/lib/native/base/base.cc +63 -85
  26. package/lib/native/base/base.hh +178 -151
  27. package/package.json +3 -4
  28. package/src/build.js +1178 -0
  29. package/src/koffi/CMakeLists.txt +97 -34
  30. package/src/koffi/index.js +359 -0
  31. package/src/koffi/indirect.js +216 -0
  32. package/src/koffi/src/{abi_arm32.cc → abi/arm32.cc} +279 -275
  33. package/src/koffi/src/{abi_arm32_asm.S → abi/arm32_asm.S} +2 -5
  34. package/src/koffi/src/abi/arm64.cc +1511 -0
  35. package/src/koffi/src/{abi_arm64_asm.S → abi/arm64_asm.S} +44 -23
  36. package/src/koffi/src/{abi_arm64_asm.asm → abi/arm64_asm.asm} +32 -25
  37. package/src/koffi/src/{abi_loong64_asm.S → abi/loong64_asm.S} +1 -5
  38. package/src/koffi/src/{abi_riscv64.cc → abi/riscv64.cc} +279 -273
  39. package/src/koffi/src/{abi_riscv64_asm.S → abi/riscv64_asm.S} +1 -4
  40. package/src/koffi/src/abi/x64sysv.cc +1456 -0
  41. package/src/koffi/src/{abi_x64_sysv_asm.S → abi/x64sysv_asm.S} +30 -33
  42. package/src/koffi/src/abi/x64win.cc +1121 -0
  43. package/src/koffi/src/{abi_x64_win_asm.S → abi/x64win_asm.S} +8 -12
  44. package/src/koffi/src/{abi_x64_win_asm.asm → abi/x64win_asm.asm} +11 -14
  45. package/src/koffi/src/abi/x86.cc +1233 -0
  46. package/src/koffi/src/{abi_x86_asm.S → abi/x86_asm.S} +49 -25
  47. package/src/koffi/src/{abi_x86_asm.asm → abi/x86_asm.asm} +11 -14
  48. package/src/koffi/src/call.cc +458 -598
  49. package/src/koffi/src/call.hh +61 -82
  50. package/src/koffi/src/ffi.cc +595 -309
  51. package/src/koffi/src/ffi.hh +103 -89
  52. package/src/koffi/src/parser.cc +0 -4
  53. package/src/koffi/src/primitives.inc +39 -0
  54. package/src/koffi/{tools/write_trampolines.js → src/trampolines.js} +1 -1
  55. package/src/koffi/src/util.cc +503 -446
  56. package/src/koffi/src/util.hh +269 -57
  57. package/src/koffi/src/uv.cc +2 -2
  58. package/src/koffi/src/uv.def +3 -0
  59. package/src/koffi/src/win32.hh +30 -1
  60. package/vendor/node-addon-api/napi-inl.h +4 -0
  61. package/vendor/node-addon-api/napi.h +1 -0
  62. package/build/koffi/linux_armhf/koffi.node +0 -0
  63. package/build/koffi/linux_loong64/koffi.node +0 -0
  64. package/build/koffi/linux_riscv64d/koffi.node +0 -0
  65. package/build/koffi/win32_arm64/koffi.exp +0 -0
  66. package/build/koffi/win32_arm64/koffi.lib +0 -0
  67. package/build/koffi/win32_arm64/koffi.node +0 -0
  68. package/doc/platforms.md +0 -36
  69. package/src/cnoke/LICENSE.txt +0 -22
  70. package/src/cnoke/README.md +0 -99
  71. package/src/cnoke/assets/FindCNoke.cmake +0 -127
  72. package/src/cnoke/assets/toolchains.json +0 -126
  73. package/src/cnoke/assets/win_delay_hook.c +0 -36
  74. package/src/cnoke/cnoke.js +0 -170
  75. package/src/cnoke/package.json +0 -24
  76. package/src/cnoke/src/builder.js +0 -511
  77. package/src/cnoke/src/index.js +0 -10
  78. package/src/cnoke/src/tools.js +0 -407
  79. package/src/koffi/src/abi_arm64.cc +0 -1295
  80. package/src/koffi/src/abi_x64_sysv.cc +0 -939
  81. package/src/koffi/src/abi_x64_win.cc +0 -715
  82. package/src/koffi/src/abi_x86.cc +0 -860
  83. package/src/koffi/src/init.js +0 -105
  84. /package/src/koffi/src/{abi_loong64.cc → abi/loong64.cc} +0 -0
@@ -1,1295 +0,0 @@
1
- // SPDX-License-Identifier: MIT
2
- // SPDX-FileCopyrightText: 2025 Niels Martignène <niels.martignene@protonmail.com>
3
-
4
- #if defined(__aarch64__) || defined(_M_ARM64)
5
-
6
- #include "lib/native/base/base.hh"
7
- #include "ffi.hh"
8
- #include "call.hh"
9
- #include "util.hh"
10
- #if defined(_WIN32)
11
- #include "win32.hh"
12
- #endif
13
-
14
- #include <napi.h>
15
-
16
- namespace K {
17
-
18
- struct HfaInfo {
19
- int count;
20
- bool float32;
21
- };
22
-
23
- struct X0X1Ret {
24
- uint64_t x0;
25
- uint64_t x1;
26
- };
27
- struct HfaRet {
28
- double d0;
29
- double d1;
30
- double d2;
31
- double d3;
32
- };
33
-
34
- struct BackRegisters {
35
- uint64_t x0;
36
- uint64_t x1;
37
- double d0;
38
- double d1;
39
- double d2;
40
- double d3;
41
- };
42
-
43
- extern "C" X0X1Ret ForwardCallGG(const void *func, uint8_t *sp, uint8_t **out_old_sp);
44
- extern "C" float ForwardCallF(const void *func, uint8_t *sp, uint8_t **out_old_sp);
45
- extern "C" HfaRet ForwardCallDDDD(const void *func, uint8_t *sp, uint8_t **out_old_sp);
46
-
47
- extern "C" X0X1Ret ForwardCallXGG(const void *func, uint8_t *sp, uint8_t **out_old_sp);
48
- extern "C" float ForwardCallXF(const void *func, uint8_t *sp, uint8_t **out_old_sp);
49
- extern "C" HfaRet ForwardCallXDDDD(const void *func, uint8_t *sp, uint8_t **out_old_sp);
50
-
51
- extern "C" uint8_t Trampoline0;
52
- extern "C" uint8_t TrampolineEnd;
53
- extern "C" void *FindTrampolineStart();
54
- extern "C" void *FindTrampolineEnd();
55
-
56
- static HfaInfo IsHFA(const TypeInfo *type)
57
- {
58
- bool float32 = false;
59
- bool float64 = false;
60
- int count = 0;
61
-
62
- count = AnalyseFlat(type, [&](const TypeInfo *type, int, int) {
63
- if (type->primitive == PrimitiveKind::Float32) {
64
- float32 = true;
65
- } else if (type->primitive == PrimitiveKind::Float64) {
66
- float64 = true;
67
- } else {
68
- float32 = true;
69
- float64 = true;
70
- }
71
- });
72
-
73
- HfaInfo info = {};
74
-
75
- if (count < 1 || count > 4)
76
- return info;
77
- if (float32 && float64)
78
- return info;
79
-
80
- info.count = count;
81
- info.float32 = float32;
82
-
83
- return info;
84
- }
85
-
86
- static inline void ExpandFloats(uint8_t *ptr, Size len, Size bytes)
87
- {
88
- for (Size i = len - 1; i >= 0; i--) {
89
- const uint8_t *src = ptr + i * bytes;
90
- uint8_t *dest = ptr + i * 8;
91
-
92
- memmove(dest, src, bytes);
93
- }
94
- }
95
-
96
- static inline void CompactFloats(uint8_t *ptr, Size len, Size bytes)
97
- {
98
- for (Size i = 0; i < len; i++) {
99
- const uint8_t *src = ptr + i * 8;
100
- uint8_t *dest = ptr + i * bytes;
101
-
102
- memmove(dest, src, bytes);
103
- }
104
- }
105
-
106
- bool AnalyseFunction(Napi::Env, InstanceData *, FunctionInfo *func)
107
- {
108
- if (HfaInfo hfa = IsHFA(func->ret.type); hfa.count) {
109
- func->ret.vec_count = (int8_t)hfa.count;
110
- func->ret.vec_bytes = hfa.float32 ? 4 : 8;
111
- } else if (func->ret.type->size <= 16) {
112
- func->ret.gpr_count = (int8_t)((func->ret.type->size + 7) / 8);
113
- } else {
114
- func->ret.use_memory = true;
115
- }
116
-
117
- int gpr_avail = 8;
118
- int vec_avail = 8;
119
- #if defined(_M_ARM64EC)
120
- if (func->variadic) {
121
- gpr_avail = 4;
122
- }
123
- #endif
124
-
125
- for (ParameterInfo &param: func->parameters) {
126
- switch (param.type->primitive) {
127
- case PrimitiveKind::Void: { K_UNREACHABLE(); } break;
128
-
129
- case PrimitiveKind::Bool:
130
- case PrimitiveKind::Int8:
131
- case PrimitiveKind::UInt8:
132
- case PrimitiveKind::Int16:
133
- case PrimitiveKind::Int16S:
134
- case PrimitiveKind::UInt16:
135
- case PrimitiveKind::UInt16S:
136
- case PrimitiveKind::Int32:
137
- case PrimitiveKind::Int32S:
138
- case PrimitiveKind::UInt32:
139
- case PrimitiveKind::UInt32S:
140
- case PrimitiveKind::Int64:
141
- case PrimitiveKind::Int64S:
142
- case PrimitiveKind::UInt64:
143
- case PrimitiveKind::UInt64S:
144
- case PrimitiveKind::String:
145
- case PrimitiveKind::String16:
146
- case PrimitiveKind::String32:
147
- case PrimitiveKind::Pointer:
148
- case PrimitiveKind::Callback: {
149
- #if defined(__APPLE__)
150
- if (param.variadic)
151
- break;
152
- #endif
153
-
154
- if (gpr_avail) {
155
- param.gpr_count = 1;
156
- gpr_avail--;
157
- }
158
- } break;
159
- case PrimitiveKind::Record:
160
- case PrimitiveKind::Union: {
161
- HfaInfo hfa = IsHFA(param.type);
162
-
163
- #if defined(_M_ARM64EC)
164
- if (func->variadic) {
165
- if (IsRegularSize(param.type->size, 8) && gpr_avail) {
166
- param.gpr_count = 1;
167
- gpr_avail--;
168
- } else {
169
- if (gpr_avail) {
170
- param.gpr_count = 1;
171
- gpr_avail--;
172
- }
173
- param.use_memory = true;
174
- }
175
-
176
- break;
177
- }
178
- #endif
179
-
180
- #if defined(_WIN32)
181
- if (param.variadic) {
182
- hfa.count = 0;
183
- }
184
- #elif defined(__APPLE__)
185
- if (param.variadic) {
186
- param.use_memory = (param.type->size > 16);
187
- break;
188
- }
189
- #endif
190
-
191
- if (hfa.count) {
192
- if (hfa.count <= vec_avail) {
193
- param.vec_count = (int8_t)hfa.count;
194
- param.vec_bytes = hfa.float32 ? 4 : 8;
195
- vec_avail -= hfa.count;
196
- } else {
197
- vec_avail = 0;
198
- }
199
- } else if (param.type->size <= 16) {
200
- int gpr_count = (param.type->size + 7) / 8;
201
-
202
- if (gpr_count <= gpr_avail) {
203
- param.gpr_count = (int8_t)gpr_count;
204
- gpr_avail -= gpr_count;
205
- } else {
206
- gpr_avail = 0;
207
- }
208
- } else {
209
- // Big types (more than 16 bytes) are replaced by a pointer
210
- if (gpr_avail) {
211
- param.gpr_count = 1;
212
- gpr_avail--;
213
- }
214
- param.use_memory = true;
215
- }
216
- } break;
217
- case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
218
- case PrimitiveKind::Float32:
219
- case PrimitiveKind::Float64: {
220
- #if defined(_WIN32)
221
- if (param.variadic) {
222
- if (gpr_avail) {
223
- param.gpr_count = 1;
224
- gpr_avail--;
225
- }
226
- break;
227
- }
228
- #elif defined(__APPLE__)
229
- if (param.variadic)
230
- break;
231
- #endif
232
-
233
- if (vec_avail) {
234
- param.vec_count = 1;
235
- vec_avail--;
236
- }
237
- } break;
238
-
239
- case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
240
- }
241
- }
242
-
243
- func->args_size = 16 * func->parameters.len;
244
- func->forward_fp = (vec_avail < 8);
245
-
246
- return true;
247
- }
248
-
249
- bool CallData::Prepare(const FunctionInfo *func, const Napi::CallbackInfo &info)
250
- {
251
- uint64_t *args_ptr = nullptr;
252
- uint64_t *gpr_ptr = nullptr;
253
- uint64_t *vec_ptr = nullptr;
254
-
255
- // Return through registers unless it's too big
256
- if (!AllocStack(func->args_size, 16, &args_ptr)) [[unlikely]]
257
- return false;
258
- if (!AllocStack(8 * 8, 8, &vec_ptr)) [[unlikely]]
259
- return false;
260
- if (!AllocStack(9 * 8, 8, &gpr_ptr)) [[unlikely]]
261
- return false;
262
- if (func->ret.use_memory) {
263
- return_ptr = AllocHeap(func->ret.type->size, 16);
264
- gpr_ptr[8] = (uint64_t)return_ptr;
265
- }
266
-
267
- #if defined(_M_ARM64EC)
268
- if (func->variadic) {
269
- gpr_ptr[4] = (uint64_t)args_ptr;
270
- gpr_ptr[5] = 0;
271
-
272
- for (Size i = 4; i < func->parameters.len; i++) {
273
- const ParameterInfo &param = func->parameters[i];
274
- gpr_ptr[5] += std::max((Size)8, param.type->size);
275
- }
276
- }
277
- #endif
278
-
279
- #if defined(__APPLE__)
280
- #define PUSH_INTEGER(CType) \
281
- do { \
282
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] { \
283
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
284
- return false; \
285
- } \
286
- \
287
- CType v = GetNumber<CType>(value); \
288
- \
289
- if (param.gpr_count) [[likely]] { \
290
- *(gpr_ptr++) = (uint64_t)v; \
291
- } else { \
292
- args_ptr = AlignUp(args_ptr, param.variadic ? 8 : param.type->align); \
293
- *args_ptr = (uint64_t)v; \
294
- args_ptr = (uint64_t *)((uint8_t *)args_ptr + param.type->size); \
295
- } \
296
- } while (false)
297
- #define PUSH_INTEGER_SWAP(CType) \
298
- do { \
299
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] { \
300
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
301
- return false; \
302
- } \
303
- \
304
- CType v = GetNumber<CType>(value); \
305
- \
306
- if (param.gpr_count) [[likely]] { \
307
- *(gpr_ptr++) = (uint64_t)ReverseBytes(v); \
308
- } else { \
309
- args_ptr = AlignUp(args_ptr, param.variadic ? 8 : param.type->align); \
310
- *args_ptr = (uint64_t)ReverseBytes(v); \
311
- args_ptr = (uint64_t *)((uint8_t *)args_ptr + param.type->size); \
312
- } \
313
- } while (false)
314
- #else
315
- #define PUSH_INTEGER(CType) \
316
- do { \
317
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] { \
318
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
319
- return false; \
320
- } \
321
- \
322
- CType v = GetNumber<CType>(value); \
323
- *((param.gpr_count ? gpr_ptr : args_ptr)++) = (uint64_t)v; \
324
- } while (false)
325
- #define PUSH_INTEGER_SWAP(CType) \
326
- do { \
327
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] { \
328
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
329
- return false; \
330
- } \
331
- \
332
- CType v = GetNumber<CType>(value); \
333
- *((param.gpr_count ? gpr_ptr : args_ptr)++) = (uint64_t)ReverseBytes(v); \
334
- } while (false)
335
- #endif
336
-
337
- // Push arguments
338
- for (Size i = 0; i < func->parameters.len; i++) {
339
- const ParameterInfo &param = func->parameters[i];
340
- K_ASSERT(param.directions >= 1 && param.directions <= 3);
341
-
342
- Napi::Value value = info[param.offset];
343
-
344
- switch (param.type->primitive) {
345
- case PrimitiveKind::Void: { K_UNREACHABLE(); } break;
346
-
347
- case PrimitiveKind::Bool: {
348
- if (!value.IsBoolean()) [[unlikely]] {
349
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected boolean", GetValueType(instance, value));
350
- return false;
351
- }
352
-
353
- bool b = value.As<Napi::Boolean>();
354
-
355
- #if defined(__APPLE__)
356
- if (param.gpr_count) [[likely]] {
357
- *(gpr_ptr++) = (uint64_t)b;
358
- } else {
359
- args_ptr = AlignUp(args_ptr, param.variadic ? 8 : 1);
360
- *(uint8_t *)args_ptr = b;
361
- args_ptr = (uint64_t *)((uint8_t *)args_ptr + 1);
362
- }
363
- #else
364
- *((param.gpr_count ? gpr_ptr : args_ptr)++) = (uint64_t)b;
365
- #endif
366
- } break;
367
- case PrimitiveKind::Int8: { PUSH_INTEGER(int8_t); } break;
368
- case PrimitiveKind::UInt8: { PUSH_INTEGER(uint8_t); } break;
369
- case PrimitiveKind::Int16: { PUSH_INTEGER(int16_t); } break;
370
- case PrimitiveKind::Int16S: { PUSH_INTEGER_SWAP(int16_t); } break;
371
- case PrimitiveKind::UInt16: { PUSH_INTEGER(uint16_t); } break;
372
- case PrimitiveKind::UInt16S: { PUSH_INTEGER_SWAP(uint16_t); } break;
373
- case PrimitiveKind::Int32: { PUSH_INTEGER(int32_t); } break;
374
- case PrimitiveKind::Int32S: { PUSH_INTEGER_SWAP(int32_t); } break;
375
- case PrimitiveKind::UInt32: { PUSH_INTEGER(uint32_t); } break;
376
- case PrimitiveKind::UInt32S: { PUSH_INTEGER_SWAP(uint32_t); } break;
377
- case PrimitiveKind::Int64: { PUSH_INTEGER(int64_t); } break;
378
- case PrimitiveKind::Int64S: { PUSH_INTEGER_SWAP(int64_t); } break;
379
- case PrimitiveKind::UInt64: { PUSH_INTEGER(uint64_t); } break;
380
- case PrimitiveKind::UInt64S: { PUSH_INTEGER_SWAP(uint64_t); } break;
381
- case PrimitiveKind::String: {
382
- const char *str;
383
- if (!PushString(value, param.directions, &str)) [[unlikely]]
384
- return false;
385
-
386
- #if defined(__APPLE__)
387
- args_ptr = param.gpr_count ? args_ptr : AlignUp(args_ptr, 8);
388
- #endif
389
- *(const char **)((param.gpr_count ? gpr_ptr : args_ptr)++) = str;
390
- } break;
391
- case PrimitiveKind::String16: {
392
- const char16_t *str16;
393
- if (!PushString16(value, param.directions, &str16)) [[unlikely]]
394
- return false;
395
-
396
- #if defined(__APPLE__)
397
- args_ptr = param.gpr_count ? args_ptr : AlignUp(args_ptr, 8);
398
- #endif
399
- *(const char16_t **)((param.gpr_count ? gpr_ptr : args_ptr)++) = str16;
400
- } break;
401
- case PrimitiveKind::String32: {
402
- const char32_t *str32;
403
- if (!PushString32(value, param.directions, &str32)) [[unlikely]]
404
- return false;
405
-
406
- #if defined(__APPLE__)
407
- args_ptr = param.gpr_count ? args_ptr : AlignUp(args_ptr, 8);
408
- #endif
409
- *(const char32_t **)((param.gpr_count ? gpr_ptr : args_ptr)++) = str32;
410
- } break;
411
- case PrimitiveKind::Pointer: {
412
- void *ptr;
413
- if (!PushPointer(value, param.type, param.directions, &ptr)) [[unlikely]]
414
- return false;
415
-
416
- #if defined(__APPLE__)
417
- args_ptr = param.gpr_count ? args_ptr : AlignUp(args_ptr, 8);
418
- #endif
419
- *(void **)((param.gpr_count ? gpr_ptr : args_ptr)++) = ptr;
420
- } break;
421
- case PrimitiveKind::Record:
422
- case PrimitiveKind::Union: {
423
- if (!IsObject(value)) [[unlikely]] {
424
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected object", GetValueType(instance, value));
425
- return false;
426
- }
427
-
428
- Napi::Object obj = value.As<Napi::Object>();
429
-
430
- if (param.vec_count) { // HFA
431
- uint8_t *ptr = (uint8_t *)vec_ptr;
432
-
433
- if (!PushObject(obj, param.type, ptr))
434
- return false;
435
- ExpandFloats(ptr, param.vec_count, param.vec_bytes);
436
-
437
- vec_ptr += param.vec_count;
438
- } else if (!param.use_memory) {
439
- if (param.gpr_count) {
440
- K_ASSERT(param.type->align <= 8);
441
-
442
- if (!PushObject(obj, param.type, (uint8_t *)gpr_ptr))
443
- return false;
444
- gpr_ptr += param.gpr_count;
445
- } else if (param.type->size) {
446
- #if defined(__APPLE__)
447
- args_ptr = AlignUp(args_ptr, 8);
448
- #endif
449
- if (!PushObject(obj, param.type, (uint8_t *)args_ptr))
450
- return false;
451
- args_ptr += (param.type->size + 7) / 8;
452
- }
453
- } else {
454
- uint8_t *ptr = AllocHeap(param.type->size, 16);
455
-
456
- if (param.gpr_count) {
457
- K_ASSERT(param.gpr_count == 1);
458
- K_ASSERT(param.vec_count == 0);
459
-
460
- *(uint8_t **)(gpr_ptr++) = ptr;
461
- } else {
462
- #if defined(__APPLE__)
463
- args_ptr = AlignUp(args_ptr, 8);
464
- #endif
465
- *(uint8_t **)(args_ptr++) = ptr;
466
- }
467
-
468
- if (!PushObject(obj, param.type, ptr))
469
- return false;
470
- }
471
- } break;
472
- case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
473
- case PrimitiveKind::Float32: {
474
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] {
475
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value));
476
- return false;
477
- }
478
-
479
- float f = GetNumber<float>(value);
480
-
481
- if (param.vec_count) [[likely]] {
482
- memset((uint8_t *)vec_ptr + 4, 0, 4);
483
- *(float *)(vec_ptr++) = f;
484
- #if defined(_WIN32)
485
- } else if (param.gpr_count) {
486
- memset((uint8_t *)gpr_ptr + 4, 0, 4);
487
- *(float *)(gpr_ptr++) = f;
488
- #endif
489
- } else {
490
- #if defined(__APPLE__)
491
- args_ptr = AlignUp(args_ptr, param.variadic ? 8 : 4);
492
- *(float *)args_ptr = f;
493
- args_ptr = (uint64_t *)((uint8_t *)args_ptr + 4);
494
- #else
495
- memset((uint8_t *)args_ptr + 4, 0, 4);
496
- *(float *)(args_ptr++) = f;
497
- #endif
498
- }
499
- } break;
500
- case PrimitiveKind::Float64: {
501
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] {
502
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value));
503
- return false;
504
- }
505
-
506
- double d = GetNumber<double>(value);
507
-
508
- if (param.vec_count) [[likely]] {
509
- *(double *)(vec_ptr++) = d;
510
- #if defined(_WIN32)
511
- } else if (param.gpr_count) {
512
- *(double *)(gpr_ptr++) = d;
513
- #endif
514
- } else {
515
- #if defined(__APPLE__)
516
- args_ptr = AlignUp(args_ptr, 8);
517
- #endif
518
- *(double *)(args_ptr++) = d;
519
- }
520
- } break;
521
- case PrimitiveKind::Callback: {
522
- void *ptr;
523
- if (!PushCallback(value, param.type, &ptr)) [[unlikely]]
524
- return false;
525
-
526
- #if defined(__APPLE__)
527
- args_ptr = param.gpr_count ? args_ptr : AlignUp(args_ptr, 8);
528
- #endif
529
- *(void **)((param.gpr_count ? gpr_ptr : args_ptr)++) = ptr;
530
- } break;
531
-
532
- case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
533
- }
534
- }
535
-
536
- #undef PUSH_INTEGER_SWAP
537
- #undef PUSH_INTEGER
538
-
539
- new_sp = mem->stack.end();
540
-
541
- return true;
542
- }
543
-
544
- void CallData::Execute(const FunctionInfo *func, void *native)
545
- {
546
- #if defined(_WIN32)
547
- TEB *teb = GetTEB();
548
-
549
- // Restore previous stack limits at the end
550
- K_DEFER_C(exception_list = teb->ExceptionList,
551
- base = teb->StackBase,
552
- limit = teb->StackLimit,
553
- dealloc = teb->DeallocationStack,
554
- guaranteed = teb->GuaranteedStackBytes) {
555
- teb->ExceptionList = exception_list;
556
- teb->StackBase = base;
557
- teb->StackLimit = limit;
558
- teb->DeallocationStack = dealloc;
559
- teb->GuaranteedStackBytes = guaranteed;
560
-
561
- instance->last_error = teb->LastErrorValue;
562
- };
563
-
564
- // Adjust stack limits so SEH works correctly
565
- teb->ExceptionList = (void *)-1; // EXCEPTION_CHAIN_END
566
- teb->StackBase = mem->stack0.end();
567
- teb->StackLimit = mem->stack0.ptr;
568
- teb->DeallocationStack = mem->stack0.ptr;
569
- teb->GuaranteedStackBytes = 0;
570
-
571
- teb->LastErrorValue = instance->last_error;
572
- #endif
573
-
574
- #define PERFORM_CALL(Suffix) \
575
- ([&]() { \
576
- auto ret = (func->forward_fp ? ForwardCallX ## Suffix(native, new_sp, &old_sp) \
577
- : ForwardCall ## Suffix(native, new_sp, &old_sp)); \
578
- return ret; \
579
- })()
580
-
581
- // Execute and convert return value
582
- switch (func->ret.type->primitive) {
583
- case PrimitiveKind::Void:
584
- case PrimitiveKind::Bool:
585
- case PrimitiveKind::Int8:
586
- case PrimitiveKind::UInt8:
587
- case PrimitiveKind::Int16:
588
- case PrimitiveKind::Int16S:
589
- case PrimitiveKind::UInt16:
590
- case PrimitiveKind::UInt16S:
591
- case PrimitiveKind::Int32:
592
- case PrimitiveKind::Int32S:
593
- case PrimitiveKind::UInt32:
594
- case PrimitiveKind::UInt32S:
595
- case PrimitiveKind::Int64:
596
- case PrimitiveKind::Int64S:
597
- case PrimitiveKind::UInt64:
598
- case PrimitiveKind::UInt64S:
599
- case PrimitiveKind::String:
600
- case PrimitiveKind::String16:
601
- case PrimitiveKind::String32:
602
- case PrimitiveKind::Pointer:
603
- case PrimitiveKind::Callback: { result.u64 = PERFORM_CALL(GG).x0; } break;
604
- case PrimitiveKind::Record:
605
- case PrimitiveKind::Union: {
606
- if (func->ret.gpr_count) {
607
- X0X1Ret ret = PERFORM_CALL(GG);
608
- memcpy(&result.buf, &ret, K_SIZE(ret));
609
- } else if (func->ret.vec_count) {
610
- HfaRet ret = PERFORM_CALL(DDDD);
611
- memcpy(&result.buf, &ret, K_SIZE(ret));
612
- } else {
613
- PERFORM_CALL(GG);
614
- }
615
- } break;
616
- case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
617
- case PrimitiveKind::Float32: { result.f = PERFORM_CALL(F); } break;
618
- case PrimitiveKind::Float64: { result.d = PERFORM_CALL(DDDD).d0; } break;
619
-
620
- case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
621
- }
622
-
623
- #undef PERFORM_CALL
624
- }
625
-
626
- Napi::Value CallData::Complete(const FunctionInfo *func)
627
- {
628
- K_DEFER {
629
- PopOutArguments();
630
-
631
- if (func->ret.type->dispose) {
632
- func->ret.type->dispose(env, func->ret.type, result.ptr);
633
- }
634
- };
635
-
636
- switch (func->ret.type->primitive) {
637
- case PrimitiveKind::Void: return env.Undefined();
638
- case PrimitiveKind::Bool: return Napi::Boolean::New(env, result.u8 & 0x1);
639
- case PrimitiveKind::Int8: return Napi::Number::New(env, (double)result.i8);
640
- case PrimitiveKind::UInt8: return Napi::Number::New(env, (double)result.u8);
641
- case PrimitiveKind::Int16: return Napi::Number::New(env, (double)result.i16);
642
- case PrimitiveKind::Int16S: return Napi::Number::New(env, (double)ReverseBytes(result.i16));
643
- case PrimitiveKind::UInt16: return Napi::Number::New(env, (double)result.u16);
644
- case PrimitiveKind::UInt16S: return Napi::Number::New(env, (double)ReverseBytes(result.u16));
645
- case PrimitiveKind::Int32: return Napi::Number::New(env, (double)result.i32);
646
- case PrimitiveKind::Int32S: return Napi::Number::New(env, (double)ReverseBytes(result.i32));
647
- case PrimitiveKind::UInt32: return Napi::Number::New(env, (double)result.u32);
648
- case PrimitiveKind::UInt32S: return Napi::Number::New(env, (double)ReverseBytes(result.u32));
649
- case PrimitiveKind::Int64: return NewBigInt(env, result.i64);
650
- case PrimitiveKind::Int64S: return NewBigInt(env, ReverseBytes(result.i64));
651
- case PrimitiveKind::UInt64: return NewBigInt(env, result.u64);
652
- case PrimitiveKind::UInt64S: return NewBigInt(env, ReverseBytes(result.u64));
653
- case PrimitiveKind::String: return result.ptr ? Napi::String::New(env, (const char *)result.ptr) : env.Null();
654
- case PrimitiveKind::String16: return result.ptr ? Napi::String::New(env, (const char16_t *)result.ptr) : env.Null();
655
- case PrimitiveKind::String32: return result.ptr ? MakeStringFromUTF32(env, (const char32_t *)result.ptr) : env.Null();
656
- case PrimitiveKind::Pointer:
657
- case PrimitiveKind::Callback: {
658
- if (result.ptr) {
659
- Napi::External<void> external = Napi::External<void>::New(env, result.ptr);
660
- SetValueTag(external, func->ret.type->ref.marker);
661
-
662
- return external;
663
- } else {
664
- return env.Null();
665
- }
666
- } break;
667
- case PrimitiveKind::Record:
668
- case PrimitiveKind::Union: {
669
- if (func->ret.vec_count) { // HFA
670
- uint8_t *ptr = (uint8_t *)&result.buf;
671
-
672
- CompactFloats(ptr, func->ret.vec_count, func->ret.vec_bytes);
673
-
674
- Napi::Object obj = DecodeObject(env, ptr, func->ret.type);
675
- return obj;
676
- } else {
677
- const uint8_t *ptr = return_ptr ? (const uint8_t *)return_ptr
678
- : (const uint8_t *)&result.buf;
679
-
680
- Napi::Object obj = DecodeObject(env, ptr, func->ret.type);
681
- return obj;
682
- }
683
- } break;
684
- case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
685
- case PrimitiveKind::Float32: return Napi::Number::New(env, (double)result.f);
686
- case PrimitiveKind::Float64: return Napi::Number::New(env, result.d);
687
-
688
- case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
689
- }
690
-
691
- K_UNREACHABLE();
692
- }
693
-
694
- void CallData::Relay(Size idx, uint8_t *sp)
695
- {
696
- const TrampolineInfo &trampoline = shared.trampolines[idx];
697
-
698
- uint8_t *own_sp = sp;
699
- uint8_t *caller_sp = sp + 208;
700
- BackRegisters *out_reg = (BackRegisters *)(sp + 136);
701
-
702
- const FunctionInfo *proto = trampoline.proto;
703
- Napi::Function func = trampoline.func.Value();
704
-
705
- uint64_t *gpr_ptr = (uint64_t *)own_sp;
706
- uint64_t *vec_ptr = gpr_ptr + 9;
707
- uint64_t *args_ptr = (uint64_t *)caller_sp;
708
-
709
- uint8_t *return_ptr = proto->ret.use_memory ? (uint8_t *)gpr_ptr[8] : nullptr;
710
-
711
- K_DEFER_N(err_guard) { memset(out_reg, 0, K_SIZE(*out_reg)); };
712
-
713
- LocalArray<napi_value, MaxParameters + 1> arguments;
714
-
715
- arguments.Append(!trampoline.recv.IsEmpty() ? trampoline.recv.Value() : env.Undefined());
716
-
717
- // Convert to JS arguments
718
- for (Size i = 0; i < proto->parameters.len; i++) {
719
- const ParameterInfo &param = proto->parameters[i];
720
- K_ASSERT(param.directions >= 1 && param.directions <= 3);
721
-
722
- switch (param.type->primitive) {
723
- case PrimitiveKind::Void: { K_UNREACHABLE(); } break;
724
-
725
- case PrimitiveKind::Bool: {
726
- #if defined(__APPLE__)
727
- bool b;
728
- if (param.gpr_count) {
729
- b = *(bool *)(gpr_ptr++);
730
- } else {
731
- b = *(bool *)args_ptr;
732
- args_ptr = (uint64_t *)((uint8_t *)args_ptr + 1);
733
- }
734
- #else
735
- bool b = *(bool *)((param.gpr_count ? gpr_ptr : args_ptr)++);
736
- #endif
737
-
738
- Napi::Value arg = Napi::Boolean::New(env, b);
739
- arguments.Append(arg);
740
- } break;
741
- case PrimitiveKind::Int8: {
742
- #if defined(__APPLE__)
743
- double d;
744
- if (param.gpr_count) {
745
- d = (double)*(int8_t *)(gpr_ptr++);
746
- } else {
747
- d = (double)*(int8_t *)args_ptr;
748
- args_ptr = (uint64_t *)((uint8_t *)args_ptr + 1);
749
- }
750
- #else
751
- double d = (double)*(int8_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
752
- #endif
753
-
754
- Napi::Value arg = Napi::Number::New(env, d);
755
- arguments.Append(arg);
756
- } break;
757
- case PrimitiveKind::UInt8: {
758
- #if defined(__APPLE__)
759
- double d;
760
- if (param.gpr_count) {
761
- d = (double)*(uint8_t *)(gpr_ptr++);
762
- } else {
763
- d = (double)*(uint8_t *)args_ptr;
764
- args_ptr = (uint64_t *)((uint8_t *)args_ptr + 1);
765
- }
766
- #else
767
- double d = (double)*(uint8_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
768
- #endif
769
-
770
- Napi::Value arg = Napi::Number::New(env, d);
771
- arguments.Append(arg);
772
- } break;
773
- case PrimitiveKind::Int16: {
774
- #if defined(__APPLE__)
775
- double d;
776
- if (param.gpr_count) {
777
- d = (double)*(int16_t *)(gpr_ptr++);
778
- } else {
779
- args_ptr = AlignUp(args_ptr, 2);
780
- d = (double)*(int16_t *)args_ptr;
781
- args_ptr = (uint64_t *)((uint8_t *)args_ptr + 2);
782
- }
783
- #else
784
- double d = (double)*(int16_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
785
- #endif
786
-
787
- Napi::Value arg = Napi::Number::New(env, d);
788
- arguments.Append(arg);
789
- } break;
790
- case PrimitiveKind::Int16S: {
791
- #if defined(__APPLE__)
792
- double d;
793
- if (param.gpr_count) {
794
- int16_t v = *(int16_t *)(gpr_ptr++);
795
- d = (double)ReverseBytes(v);
796
- } else {
797
- args_ptr = AlignUp(args_ptr, 2);
798
-
799
- int16_t v = *(int16_t *)args_ptr;
800
- d = (double)ReverseBytes(v);
801
-
802
- args_ptr = (uint64_t *)((uint8_t *)args_ptr + 2);
803
- }
804
- #else
805
- int16_t v = *(int16_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
806
- double d = (double)ReverseBytes(v);
807
- #endif
808
-
809
- Napi::Value arg = Napi::Number::New(env, d);
810
- arguments.Append(arg);
811
- } break;
812
- case PrimitiveKind::UInt16: {
813
- #if defined(__APPLE__)
814
- double d;
815
- if (param.gpr_count) {
816
- d = (double)*(uint16_t *)(gpr_ptr++);
817
- } else {
818
- args_ptr = AlignUp(args_ptr, 2);
819
- d = (double)*(uint16_t *)args_ptr;
820
- args_ptr = (uint64_t *)((uint8_t *)args_ptr + 2);
821
- }
822
- #else
823
- double d = (double)*(uint16_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
824
- #endif
825
-
826
- Napi::Value arg = Napi::Number::New(env, d);
827
- arguments.Append(arg);
828
- } break;
829
- case PrimitiveKind::UInt16S: {
830
- #if defined(__APPLE__)
831
- double d;
832
- if (param.gpr_count) {
833
- uint16_t v = *(uint16_t *)(gpr_ptr++);
834
- d = (double)ReverseBytes(v);
835
- } else {
836
- args_ptr = AlignUp(args_ptr, 2);
837
-
838
- uint16_t v = *(uint16_t *)args_ptr;
839
- d = (double)ReverseBytes(v);
840
-
841
- args_ptr = (uint64_t *)((uint8_t *)args_ptr + 2);
842
- }
843
- #else
844
- uint16_t v = *(uint16_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
845
- double d = (double)ReverseBytes(v);
846
- #endif
847
-
848
- Napi::Value arg = Napi::Number::New(env, d);
849
- arguments.Append(arg);
850
- } break;
851
- case PrimitiveKind::Int32: {
852
- #if defined(__APPLE__)
853
- double d;
854
- if (param.gpr_count) {
855
- d = (double)*(int32_t *)(gpr_ptr++);
856
- } else {
857
- args_ptr = AlignUp(args_ptr, 4);
858
- d = (double)*(int32_t *)args_ptr;
859
- args_ptr = (uint64_t *)((uint8_t *)args_ptr + 4);
860
- }
861
- #else
862
- double d = (double)*(int32_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
863
- #endif
864
-
865
- Napi::Value arg = Napi::Number::New(env, d);
866
- arguments.Append(arg);
867
- } break;
868
- case PrimitiveKind::Int32S: {
869
- #if defined(__APPLE__)
870
- double d;
871
- if (param.gpr_count) {
872
- int32_t v = *(int32_t *)(gpr_ptr++);
873
- d = (double)ReverseBytes(v);
874
- } else {
875
- args_ptr = AlignUp(args_ptr, 4);
876
-
877
- int32_t v = *(int32_t *)args_ptr;
878
- d = (double)ReverseBytes(v);
879
-
880
- args_ptr = (uint64_t *)((uint8_t *)args_ptr + 4);
881
- }
882
- #else
883
- int32_t v = *(int32_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
884
- double d = (double)ReverseBytes(v);
885
- #endif
886
-
887
- Napi::Value arg = Napi::Number::New(env, d);
888
- arguments.Append(arg);
889
- } break;
890
- case PrimitiveKind::UInt32: {
891
- #if defined(__APPLE__)
892
- double d;
893
- if (param.gpr_count) {
894
- d = (double)*(uint32_t *)(gpr_ptr++);
895
- } else {
896
- args_ptr = AlignUp(args_ptr, 4);
897
- d = (double)*(uint32_t *)args_ptr;
898
- args_ptr = (uint64_t *)((uint8_t *)args_ptr + 4);
899
- }
900
- #else
901
- double d = (double)*(uint32_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
902
- #endif
903
-
904
- Napi::Value arg = Napi::Number::New(env, d);
905
- arguments.Append(arg);
906
- } break;
907
- case PrimitiveKind::UInt32S: {
908
- #if defined(__APPLE__)
909
- double d;
910
- if (param.gpr_count) {
911
- uint32_t v = *(uint32_t *)(gpr_ptr++);
912
- d = (double)ReverseBytes(v);
913
- } else {
914
- args_ptr = AlignUp(args_ptr, 4);
915
-
916
- uint32_t v = *(uint32_t *)args_ptr;
917
- d = (double)ReverseBytes(v);
918
-
919
- args_ptr = (uint64_t *)((uint8_t *)args_ptr + 4);
920
- }
921
- #else
922
- uint32_t v = *(uint32_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
923
- double d = (double)ReverseBytes(v);
924
- #endif
925
-
926
- Napi::Value arg = Napi::Number::New(env, d);
927
- arguments.Append(arg);
928
- } break;
929
- case PrimitiveKind::Int64: {
930
- #if defined(__APPLE__)
931
- args_ptr = AlignUp(args_ptr, 8);
932
- #endif
933
-
934
- int64_t v = *(int64_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
935
-
936
- Napi::Value arg = NewBigInt(env, v);
937
- arguments.Append(arg);
938
- } break;
939
- case PrimitiveKind::Int64S: {
940
- #if defined(__APPLE__)
941
- args_ptr = AlignUp(args_ptr, 8);
942
- #endif
943
-
944
- int64_t v = *(int64_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
945
-
946
- Napi::Value arg = NewBigInt(env, ReverseBytes(v));
947
- arguments.Append(arg);
948
- } break;
949
- case PrimitiveKind::UInt64: {
950
- #if defined(__APPLE__)
951
- args_ptr = AlignUp(args_ptr, 8);
952
- #endif
953
-
954
- uint64_t v = *(uint64_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
955
-
956
- Napi::Value arg = NewBigInt(env, v);
957
- arguments.Append(arg);
958
- } break;
959
- case PrimitiveKind::UInt64S: {
960
- #if defined(__APPLE__)
961
- args_ptr = AlignUp(args_ptr, 8);
962
- #endif
963
-
964
- uint64_t v = *(uint64_t *)((param.gpr_count ? gpr_ptr : args_ptr)++);
965
-
966
- Napi::Value arg = NewBigInt(env, ReverseBytes(v));
967
- arguments.Append(arg);
968
- } break;
969
- case PrimitiveKind::String: {
970
- #if defined(__APPLE__)
971
- args_ptr = AlignUp(args_ptr, 8);
972
- #endif
973
-
974
- const char *str = *(const char **)((param.gpr_count ? gpr_ptr : args_ptr)++);
975
-
976
- Napi::Value arg = str ? Napi::String::New(env, str) : env.Null();
977
- arguments.Append(arg);
978
-
979
- if (param.type->dispose) {
980
- param.type->dispose(env, param.type, str);
981
- }
982
- } break;
983
- case PrimitiveKind::String16: {
984
- #if defined(__APPLE__)
985
- args_ptr = AlignUp(args_ptr, 8);
986
- #endif
987
-
988
- const char16_t *str16 = *(const char16_t **)((param.gpr_count ? gpr_ptr : args_ptr)++);
989
-
990
- Napi::Value arg = str16 ? Napi::String::New(env, str16) : env.Null();
991
- arguments.Append(arg);
992
-
993
- if (param.type->dispose) {
994
- param.type->dispose(env, param.type, str16);
995
- }
996
- } break;
997
- case PrimitiveKind::String32: {
998
- #if defined(__APPLE__)
999
- args_ptr = AlignUp(args_ptr, 8);
1000
- #endif
1001
-
1002
- const char32_t *str32 = *(const char32_t **)((param.gpr_count ? gpr_ptr : args_ptr)++);
1003
-
1004
- Napi::Value arg = str32 ? MakeStringFromUTF32(env, str32) : env.Null();
1005
- arguments.Append(arg);
1006
- } break;
1007
- case PrimitiveKind::Pointer:
1008
- case PrimitiveKind::Callback: {
1009
- #if defined(__APPLE__)
1010
- args_ptr = AlignUp(args_ptr, 8);
1011
- #endif
1012
-
1013
- void *ptr2 = *(void **)((param.gpr_count ? gpr_ptr : args_ptr)++);
1014
-
1015
- if (ptr2) {
1016
- Napi::External<void> external = Napi::External<void>::New(env, ptr2);
1017
- SetValueTag(external, param.type->ref.marker);
1018
-
1019
- arguments.Append(external);
1020
- } else {
1021
- arguments.Append(env.Null());
1022
- }
1023
-
1024
- if (param.type->dispose) {
1025
- param.type->dispose(env, param.type, ptr2);
1026
- }
1027
- } break;
1028
- case PrimitiveKind::Record:
1029
- case PrimitiveKind::Union: {
1030
- if (param.vec_count) { // HFA
1031
- uint8_t *ptr = (uint8_t *)vec_ptr;
1032
-
1033
- CompactFloats(ptr, param.vec_count, param.vec_bytes);
1034
-
1035
- Napi::Object obj = DecodeObject(env, ptr, param.type);
1036
- arguments.Append(obj);
1037
-
1038
- vec_ptr += param.vec_count;
1039
- } else if (!param.use_memory) {
1040
- if (param.gpr_count) {
1041
- K_ASSERT(param.type->align <= 8);
1042
-
1043
- Napi::Object obj = DecodeObject(env, (uint8_t *)gpr_ptr, param.type);
1044
- arguments.Append(obj);
1045
-
1046
- gpr_ptr += param.gpr_count;
1047
- } else if (param.type->size) {
1048
- args_ptr = AlignUp(args_ptr, param.type->align);
1049
-
1050
- Napi::Object obj = DecodeObject(env, (uint8_t *)args_ptr, param.type);
1051
- arguments.Append(obj);
1052
-
1053
- args_ptr += (param.type->size + 7) / 8;
1054
- }
1055
- } else {
1056
- #if defined(__APPLE__)
1057
- args_ptr = AlignUp(args_ptr, 8);
1058
- #endif
1059
-
1060
- void *ptr2 = *(void **)((param.gpr_count ? gpr_ptr : args_ptr)++);
1061
-
1062
- Napi::Object obj = DecodeObject(env, (uint8_t *)ptr2, param.type);
1063
- arguments.Append(obj);
1064
- }
1065
- } break;
1066
- case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
1067
- case PrimitiveKind::Float32: {
1068
- float f;
1069
- if (param.vec_count) [[likely]] {
1070
- f = *(float *)(vec_ptr++);
1071
- #if defined(_WIN32)
1072
- } else if (param.gpr_count) {
1073
- f = *(float *)(gpr_ptr++);
1074
- #endif
1075
- } else {
1076
- #if defined(__APPLE__)
1077
- args_ptr = AlignUp(args_ptr, 4);
1078
- f = *(float *)args_ptr;
1079
- args_ptr = (uint64_t *)((uint8_t *)args_ptr + 4);
1080
- #else
1081
- f = *(float *)(args_ptr++);
1082
- #endif
1083
- }
1084
-
1085
- Napi::Value arg = Napi::Number::New(env, (double)f);
1086
- arguments.Append(arg);
1087
- } break;
1088
- case PrimitiveKind::Float64: {
1089
- double d;
1090
- if (param.vec_count) [[likely]] {
1091
- d = *(double *)(vec_ptr++);
1092
- #if defined(_WIN32)
1093
- } else if (param.gpr_count) {
1094
- d = *(double *)(gpr_ptr++);
1095
- #endif
1096
- } else {
1097
- #if defined(__APPLE__)
1098
- args_ptr = AlignUp(args_ptr, 8);
1099
- #endif
1100
-
1101
- d = *(double *)(args_ptr++);
1102
- }
1103
-
1104
- Napi::Value arg = Napi::Number::New(env, d);
1105
- arguments.Append(arg);
1106
- } break;
1107
-
1108
- case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
1109
- }
1110
- }
1111
-
1112
- const TypeInfo *type = proto->ret.type;
1113
-
1114
- // Make the call!
1115
- Napi::Value value = func.Call(arguments[0], arguments.len - 1, arguments.data + 1);
1116
-
1117
- if (env.IsExceptionPending()) [[unlikely]]
1118
- return;
1119
-
1120
- #define RETURN_INTEGER(CType) \
1121
- do { \
1122
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] { \
1123
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
1124
- return; \
1125
- } \
1126
- \
1127
- CType v = GetNumber<CType>(value); \
1128
- out_reg->x0 = (uint64_t)v; \
1129
- } while (false)
1130
- #define RETURN_INTEGER_SWAP(CType) \
1131
- do { \
1132
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] { \
1133
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
1134
- return; \
1135
- } \
1136
- \
1137
- CType v = GetNumber<CType>(value); \
1138
- out_reg->x0 = (uint64_t)ReverseBytes(v); \
1139
- } while (false)
1140
-
1141
- // Convert the result
1142
- switch (type->primitive) {
1143
- case PrimitiveKind::Void: {} break;
1144
- case PrimitiveKind::Bool: {
1145
- if (!value.IsBoolean()) [[unlikely]] {
1146
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected boolean", GetValueType(instance, value));
1147
- return;
1148
- }
1149
-
1150
- bool b = value.As<Napi::Boolean>();
1151
- out_reg->x0 = (uint64_t)b;
1152
- } break;
1153
- case PrimitiveKind::Int8: { RETURN_INTEGER(int8_t); } break;
1154
- case PrimitiveKind::UInt8: { RETURN_INTEGER(uint8_t); } break;
1155
- case PrimitiveKind::Int16: { RETURN_INTEGER(int16_t); } break;
1156
- case PrimitiveKind::Int16S: { RETURN_INTEGER_SWAP(int16_t); } break;
1157
- case PrimitiveKind::UInt16: { RETURN_INTEGER(uint16_t); } break;
1158
- case PrimitiveKind::UInt16S: { RETURN_INTEGER_SWAP(uint16_t); } break;
1159
- case PrimitiveKind::Int32: { RETURN_INTEGER(int32_t); } break;
1160
- case PrimitiveKind::Int32S: { RETURN_INTEGER_SWAP(int32_t); } break;
1161
- case PrimitiveKind::UInt32: { RETURN_INTEGER(uint32_t); } break;
1162
- case PrimitiveKind::UInt32S: { RETURN_INTEGER_SWAP(uint32_t); } break;
1163
- case PrimitiveKind::Int64: { RETURN_INTEGER(int64_t); } break;
1164
- case PrimitiveKind::Int64S: { RETURN_INTEGER_SWAP(int64_t); } break;
1165
- case PrimitiveKind::UInt64: { RETURN_INTEGER(uint64_t); } break;
1166
- case PrimitiveKind::UInt64S: { RETURN_INTEGER_SWAP(uint64_t); } break;
1167
- case PrimitiveKind::String: {
1168
- const char *str;
1169
- if (!PushString(value, 1, &str)) [[unlikely]]
1170
- return;
1171
-
1172
- out_reg->x0 = (uint64_t)str;
1173
- } break;
1174
- case PrimitiveKind::String16: {
1175
- const char16_t *str16;
1176
- if (!PushString16(value, 1, &str16)) [[unlikely]]
1177
- return;
1178
-
1179
- out_reg->x0 = (uint64_t)str16;
1180
- } break;
1181
- case PrimitiveKind::String32: {
1182
- const char32_t *str32;
1183
- if (!PushString32(value, 1, &str32)) [[unlikely]]
1184
- return;
1185
-
1186
- out_reg->x0 = (uint64_t)str32;
1187
- } break;
1188
- case PrimitiveKind::Pointer: {
1189
- uint8_t *ptr;
1190
-
1191
- if (CheckValueTag(value, type->ref.marker)) {
1192
- ptr = value.As<Napi::External<uint8_t>>().Data();
1193
- } else if (IsObject(value) && (type->ref.type->primitive == PrimitiveKind::Record ||
1194
- type->ref.type->primitive == PrimitiveKind::Union)) {
1195
- Napi::Object obj = value.As<Napi::Object>();
1196
-
1197
- ptr = AllocHeap(type->ref.type->size, 16);
1198
-
1199
- if (!PushObject(obj, type->ref.type, ptr))
1200
- return;
1201
- } else if (IsNullOrUndefined(value)) {
1202
- ptr = nullptr;
1203
- } else {
1204
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected %2", GetValueType(instance, value), type->name);
1205
- return;
1206
- }
1207
-
1208
- out_reg->x0 = (uint64_t)ptr;
1209
- } break;
1210
- case PrimitiveKind::Record:
1211
- case PrimitiveKind::Union: {
1212
- if (!IsObject(value)) [[unlikely]] {
1213
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected object", GetValueType(instance, value));
1214
- return;
1215
- }
1216
-
1217
- Napi::Object obj = value.As<Napi::Object>();
1218
-
1219
- if (return_ptr) {
1220
- if (!PushObject(obj, type, return_ptr))
1221
- return;
1222
- out_reg->x0 = (uint64_t)return_ptr;
1223
- } else if (proto->ret.vec_count) { // HFA
1224
- uint8_t *ptr = (uint8_t *)&out_reg->d0;
1225
-
1226
- ExpandFloats(ptr, proto->ret.vec_count, proto->ret.vec_bytes);
1227
- PushObject(obj, type, ptr);
1228
- } else {
1229
- PushObject(obj, type, (uint8_t *)&out_reg->x0);
1230
- }
1231
- } break;
1232
- case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
1233
- case PrimitiveKind::Float32: {
1234
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] {
1235
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value));
1236
- return;
1237
- }
1238
-
1239
- float f = GetNumber<float>(value);
1240
-
1241
- memset((uint8_t *)&out_reg->d0 + 4, 0, 4);
1242
- memcpy(&out_reg->d0, &f, 4);
1243
- } break;
1244
- case PrimitiveKind::Float64: {
1245
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] {
1246
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value));
1247
- return;
1248
- }
1249
-
1250
- double d = GetNumber<double>(value);
1251
- out_reg->d0 = d;
1252
- } break;
1253
- case PrimitiveKind::Callback: {
1254
- void *ptr;
1255
-
1256
- if (value.IsFunction()) {
1257
- Napi::Function func2 = value.As<Napi::Function>();
1258
-
1259
- ptr = ReserveTrampoline(type->ref.proto, func2);
1260
- if (!ptr) [[unlikely]]
1261
- return;
1262
- } else if (CheckValueTag(value, type->ref.marker)) {
1263
- ptr = value.As<Napi::External<void>>().Data();
1264
- } else if (IsNullOrUndefined(value)) {
1265
- ptr = nullptr;
1266
- } else {
1267
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected %2", GetValueType(instance, value), type->name);
1268
- return;
1269
- }
1270
-
1271
- out_reg->x0 = (uint64_t)ptr;
1272
- } break;
1273
-
1274
- case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
1275
- }
1276
-
1277
- #undef RETURN_INTEGER_SWAP
1278
- #undef RETURN_INTEGER
1279
-
1280
- err_guard.Disable();
1281
- }
1282
-
1283
- void *FindTrampolineStart()
1284
- {
1285
- return &Trampoline0;
1286
- }
1287
-
1288
- void *FindTrampolineEnd()
1289
- {
1290
- return &TrampolineEnd;
1291
- }
1292
-
1293
- }
1294
-
1295
- #endif