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,860 +0,0 @@
1
- // SPDX-License-Identifier: MIT
2
- // SPDX-FileCopyrightText: 2025 Niels Martignène <niels.martignene@protonmail.com>
3
-
4
- #if defined(__i386__) || defined(_M_IX86)
5
-
6
- #include "lib/native/base/base.hh"
7
- #include "ffi.hh"
8
- #include "call.hh"
9
- #include "util.hh"
10
- #if defined(_WIN32)
11
- #include "win32.hh"
12
- #endif
13
-
14
- #include <napi.h>
15
-
16
- namespace K {
17
-
18
- struct BackRegisters {
19
- uint32_t eax;
20
- uint32_t edx;
21
- union {
22
- double d;
23
- float f;
24
- } x87;
25
- int ret_type;
26
- int ret_pop;
27
- };
28
-
29
- extern "C" uint64_t ForwardCallG(const void *func, uint8_t *sp, uint8_t **out_old_sp);
30
- extern "C" float ForwardCallF(const void *func, uint8_t *sp, uint8_t **out_old_sp);
31
- extern "C" double ForwardCallD(const void *func, uint8_t *sp, uint8_t **out_old_sp);
32
- extern "C" uint64_t ForwardCallRG(const void *func, uint8_t *sp, uint8_t **out_old_sp);
33
- extern "C" float ForwardCallRF(const void *func, uint8_t *sp, uint8_t **out_old_sp);
34
- extern "C" double ForwardCallRD(const void *func, uint8_t *sp, uint8_t **out_old_sp);
35
-
36
- bool AnalyseFunction(Napi::Env env, InstanceData *instance, FunctionInfo *func)
37
- {
38
- if (!func->lib && func->convention != CallConvention::Cdecl &&
39
- func->convention != CallConvention::Stdcall) {
40
- ThrowError<Napi::Error>(env, "Only Cdecl and Stdcall callbacks are supported");
41
- return false;
42
- }
43
-
44
- int fast = (func->convention == CallConvention::Fastcall) ? 2 :
45
- (func->convention == CallConvention::Thiscall) ? 1 : 0;
46
- func->fast = fast;
47
-
48
- if (func->ret.type->primitive != PrimitiveKind::Record &&
49
- func->ret.type->primitive != PrimitiveKind::Union) {
50
- func->ret.trivial = true;
51
- #if defined(_WIN32) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
52
- } else {
53
- func->ret.trivial = IsRegularSize(func->ret.type->size, 8);
54
-
55
- #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
56
- if (func->ret.type->members.len == 1) {
57
- const RecordMember &member = func->ret.type->members[0];
58
-
59
- if (member.type->primitive == PrimitiveKind::Float32) {
60
- func->ret.fast = 1;
61
- } else if (member.type->primitive == PrimitiveKind::Float64) {
62
- func->ret.fast = 2;
63
- }
64
- }
65
- #endif
66
- #endif
67
- }
68
- #if !defined(_WIN32)
69
- if (fast && !func->ret.trivial) {
70
- func->ret.fast = 1;
71
- fast--;
72
- }
73
- #endif
74
-
75
- Size params_size = 0;
76
- for (ParameterInfo &param: func->parameters) {
77
- if (fast && param.type->size <= 4) {
78
- param.fast = 1;
79
- fast--;
80
- }
81
-
82
- params_size += std::max(4, AlignLen(param.type->size, 4));
83
- }
84
- func->args_size = params_size + 4 * !func->ret.trivial;
85
-
86
- switch (func->convention) {
87
- case CallConvention::Cdecl: {
88
- func->decorated_name = Fmt(&instance->str_alloc, "_%1", func->name).ptr;
89
- } break;
90
- case CallConvention::Stdcall: {
91
- K_ASSERT(!func->variadic);
92
- func->decorated_name = Fmt(&instance->str_alloc, "_%1@%2", func->name, params_size).ptr;
93
- } break;
94
- case CallConvention::Fastcall: {
95
- K_ASSERT(!func->variadic);
96
- func->decorated_name = Fmt(&instance->str_alloc, "@%1@%2", func->name, params_size).ptr;
97
- func->args_size += 16;
98
- } break;
99
- case CallConvention::Thiscall: {
100
- K_ASSERT(!func->variadic);
101
- func->args_size += 16;
102
- } break;
103
- }
104
-
105
- return true;
106
- }
107
-
108
- bool CallData::Prepare(const FunctionInfo *func, const Napi::CallbackInfo &info)
109
- {
110
- uint32_t *args_ptr = nullptr;
111
- uint32_t *fast_ptr = nullptr;
112
-
113
- // Pass return value in register or through memory
114
- if (!AllocStack(func->args_size, 16, &args_ptr)) [[unlikely]]
115
- return false;
116
- if (func->fast) {
117
- fast_ptr = args_ptr;
118
- args_ptr += 4;
119
- }
120
- if (!func->ret.trivial) {
121
- return_ptr = AllocHeap(func->ret.type->size, 16);
122
- *((func->ret.fast ? fast_ptr : args_ptr)++) = (uint32_t)return_ptr;
123
- }
124
-
125
- #define PUSH_INTEGER_32(CType) \
126
- do { \
127
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] { \
128
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
129
- return false; \
130
- } \
131
- \
132
- CType v = GetNumber<CType>(value); \
133
- *((param.fast ? fast_ptr : args_ptr)++) = (uint32_t)v; \
134
- } while (false)
135
- #define PUSH_INTEGER_32_SWAP(CType) \
136
- do { \
137
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] { \
138
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
139
- return false; \
140
- } \
141
- \
142
- CType v = GetNumber<CType>(value); \
143
- *((param.fast ? fast_ptr : args_ptr)++) = (uint32_t)ReverseBytes(v); \
144
- } while (false)
145
- #define PUSH_INTEGER_64(CType) \
146
- do { \
147
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] { \
148
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
149
- return false; \
150
- } \
151
- \
152
- CType v = GetNumber<CType>(value); \
153
- \
154
- *(uint64_t *)args_ptr = (uint64_t)v; \
155
- args_ptr += 2; \
156
- } while (false)
157
- #define PUSH_INTEGER_64_SWAP(CType) \
158
- do { \
159
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] { \
160
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
161
- return false; \
162
- } \
163
- \
164
- CType v = GetNumber<CType>(value); \
165
- \
166
- *(uint64_t *)args_ptr = (uint64_t)ReverseBytes(v); \
167
- args_ptr += 2; \
168
- } while (false)
169
-
170
- // Push arguments
171
- for (Size i = 0; i < func->parameters.len; i++) {
172
- const ParameterInfo &param = func->parameters[i];
173
- K_ASSERT(param.directions >= 1 && param.directions <= 3);
174
-
175
- Napi::Value value = info[param.offset];
176
-
177
- switch (param.type->primitive) {
178
- case PrimitiveKind::Void: { K_UNREACHABLE(); } break;
179
-
180
- case PrimitiveKind::Bool: {
181
- if (!value.IsBoolean()) [[unlikely]] {
182
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected boolean", GetValueType(instance, value));
183
- return false;
184
- }
185
-
186
- bool b = value.As<Napi::Boolean>();
187
- *(bool *)((param.fast ? fast_ptr : args_ptr)++) = b;
188
- } break;
189
- case PrimitiveKind::Int8: { PUSH_INTEGER_32(int8_t); } break;
190
- case PrimitiveKind::UInt8: { PUSH_INTEGER_32(uint8_t); } break;
191
- case PrimitiveKind::Int16: { PUSH_INTEGER_32(int16_t); } break;
192
- case PrimitiveKind::Int16S: { PUSH_INTEGER_32_SWAP(int16_t); } break;
193
- case PrimitiveKind::UInt16: { PUSH_INTEGER_32(uint16_t); } break;
194
- case PrimitiveKind::UInt16S: { PUSH_INTEGER_32_SWAP(uint16_t); } break;
195
- case PrimitiveKind::Int32: { PUSH_INTEGER_32(int32_t); } break;
196
- case PrimitiveKind::Int32S: { PUSH_INTEGER_32_SWAP(int32_t); } break;
197
- case PrimitiveKind::UInt32: { PUSH_INTEGER_32(uint32_t); } break;
198
- case PrimitiveKind::UInt32S: { PUSH_INTEGER_32_SWAP(uint32_t); } break;
199
- case PrimitiveKind::Int64: { PUSH_INTEGER_64(int64_t); } break;
200
- case PrimitiveKind::Int64S: { PUSH_INTEGER_64_SWAP(int64_t); } break;
201
- case PrimitiveKind::UInt64: { PUSH_INTEGER_64(uint64_t); } break;
202
- case PrimitiveKind::UInt64S: { PUSH_INTEGER_64_SWAP(uint64_t); } break;
203
- case PrimitiveKind::String: {
204
- const char *str;
205
- if (!PushString(value, param.directions, &str)) [[unlikely]]
206
- return false;
207
-
208
- *(const char **)((param.fast ? fast_ptr : args_ptr)++) = str;
209
- } break;
210
- case PrimitiveKind::String16: {
211
- const char16_t *str16;
212
- if (!PushString16(value, param.directions, &str16)) [[unlikely]]
213
- return false;
214
-
215
- *(const char16_t **)((param.fast ? fast_ptr : args_ptr)++) = str16;
216
- } break;
217
- case PrimitiveKind::String32: {
218
- const char32_t *str32;
219
- if (!PushString32(value, param.directions, &str32)) [[unlikely]]
220
- return false;
221
-
222
- *(const char32_t **)((param.fast ? fast_ptr : args_ptr)++) = str32;
223
- } break;
224
- case PrimitiveKind::Pointer: {
225
- void *ptr;
226
- if (!PushPointer(value, param.type, param.directions, &ptr)) [[unlikely]]
227
- return false;
228
-
229
- *(void **)((param.fast ? fast_ptr : args_ptr)++) = ptr;
230
- } break;
231
- case PrimitiveKind::Record:
232
- case PrimitiveKind::Union: {
233
- if (!IsObject(value)) [[unlikely]] {
234
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected object", GetValueType(instance, value));
235
- return false;
236
- }
237
-
238
- Napi::Object obj = value.As<Napi::Object>();
239
-
240
- if (param.fast) {
241
- uint8_t *ptr = (uint8_t *)(fast_ptr++);
242
- if (!PushObject(obj, param.type, ptr))
243
- return false;
244
- } else {
245
- uint8_t *ptr = (uint8_t *)args_ptr;
246
- if (!PushObject(obj, param.type, ptr))
247
- return false;
248
- args_ptr = (uint32_t *)AlignUp(ptr + param.type->size, 4);
249
- }
250
- } break;
251
- case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
252
- case PrimitiveKind::Float32: {
253
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] {
254
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value));
255
- return false;
256
- }
257
-
258
- float f = GetNumber<float>(value);
259
- *(float *)((param.fast ? fast_ptr : args_ptr)++) = f;
260
- } break;
261
- case PrimitiveKind::Float64: {
262
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] {
263
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value));
264
- return false;
265
- }
266
-
267
- double d = GetNumber<double>(value);
268
- *(double *)args_ptr = d;
269
- args_ptr += 2;
270
- } break;
271
- case PrimitiveKind::Callback: {
272
- void *ptr;
273
- if (!PushCallback(value, param.type, &ptr)) [[unlikely]]
274
- return false;
275
-
276
- *(void **)((param.fast ? fast_ptr : args_ptr)++) = ptr;
277
- } break;
278
-
279
- case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
280
- }
281
- }
282
-
283
- #undef PUSH_INTEGER_64_SWAP
284
- #undef PUSH_INTEGER_64
285
- #undef PUSH_INTEGER_32_SWAP
286
- #undef PUSH_INTEGER_32
287
-
288
- new_sp = mem->stack.end();
289
-
290
- return true;
291
- }
292
-
293
- void CallData::Execute(const FunctionInfo *func, void *native)
294
- {
295
- #if defined(_WIN32)
296
- TEB *teb = GetTEB();
297
-
298
- // Restore previous stack limits at the end
299
- K_DEFER_C(exception_list = teb->ExceptionList,
300
- base = teb->StackBase,
301
- limit = teb->StackLimit,
302
- dealloc = teb->DeallocationStack,
303
- guaranteed = teb->GuaranteedStackBytes,
304
- stfs = teb->SameTebFlags) {
305
- teb->ExceptionList = exception_list;
306
- teb->StackBase = base;
307
- teb->StackLimit = limit;
308
- teb->DeallocationStack = dealloc;
309
- teb->GuaranteedStackBytes = guaranteed;
310
- teb->SameTebFlags = stfs;
311
-
312
- instance->last_error = teb->LastErrorValue;
313
- };
314
-
315
- // Adjust stack limits so SEH works correctly
316
- teb->ExceptionList = mem->stack0.end() - K_SIZE(SehFrame);
317
- teb->StackBase = mem->stack0.end();
318
- teb->StackLimit = mem->stack0.ptr;
319
- teb->DeallocationStack = mem->stack0.ptr;
320
- teb->GuaranteedStackBytes = 0;
321
- teb->SameTebFlags &= ~0x200;
322
-
323
- teb->LastErrorValue = instance->last_error;
324
- #endif
325
-
326
- #define PERFORM_CALL(Suffix) \
327
- ([&]() { \
328
- auto ret = (func->fast ? ForwardCallR ## Suffix(native, new_sp, &old_sp) \
329
- : ForwardCall ## Suffix(native, new_sp, &old_sp)); \
330
- return ret; \
331
- })()
332
-
333
- // Execute and convert return value
334
- switch (func->ret.type->primitive) {
335
- case PrimitiveKind::Void:
336
- case PrimitiveKind::Bool:
337
- case PrimitiveKind::Int8:
338
- case PrimitiveKind::UInt8:
339
- case PrimitiveKind::Int16:
340
- case PrimitiveKind::Int16S:
341
- case PrimitiveKind::UInt16:
342
- case PrimitiveKind::UInt16S:
343
- case PrimitiveKind::Int32:
344
- case PrimitiveKind::Int32S:
345
- case PrimitiveKind::UInt32:
346
- case PrimitiveKind::UInt32S:
347
- case PrimitiveKind::Int64:
348
- case PrimitiveKind::Int64S:
349
- case PrimitiveKind::UInt64:
350
- case PrimitiveKind::UInt64S:
351
- case PrimitiveKind::String:
352
- case PrimitiveKind::String16:
353
- case PrimitiveKind::String32:
354
- case PrimitiveKind::Pointer:
355
- case PrimitiveKind::Callback: { result.u64 = PERFORM_CALL(G); } break;
356
- #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
357
- case PrimitiveKind::Record:
358
- case PrimitiveKind::Union: {
359
- if (!func->ret.fast) {
360
- result.u64 = PERFORM_CALL(G);
361
- } else if (func->ret.fast == 1) {
362
- result.f = PERFORM_CALL(F);
363
- } else if (func->ret.fast == 2) {
364
- result.d = PERFORM_CALL(D);
365
- } else {
366
- K_UNREACHABLE();
367
- }
368
- } break;
369
- #else
370
- case PrimitiveKind::Record:
371
- case PrimitiveKind::Union: { result.u64 = PERFORM_CALL(G); } break;
372
- #endif
373
- case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
374
- case PrimitiveKind::Float32: { result.f = PERFORM_CALL(F); } break;
375
- case PrimitiveKind::Float64: { result.d = PERFORM_CALL(D); } break;
376
-
377
- case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
378
- }
379
-
380
- #undef PERFORM_CALL
381
- }
382
-
383
- Napi::Value CallData::Complete(const FunctionInfo *func)
384
- {
385
- K_DEFER {
386
- PopOutArguments();
387
-
388
- if (func->ret.type->dispose) {
389
- func->ret.type->dispose(env, func->ret.type, result.ptr);
390
- }
391
- };
392
-
393
- switch (func->ret.type->primitive) {
394
- case PrimitiveKind::Void: return env.Undefined();
395
- case PrimitiveKind::Bool: return Napi::Boolean::New(env, result.u8 & 0x1);
396
- case PrimitiveKind::Int8: return Napi::Number::New(env, (double)result.i8);
397
- case PrimitiveKind::UInt8: return Napi::Number::New(env, (double)result.u8);
398
- case PrimitiveKind::Int16: return Napi::Number::New(env, (double)result.i16);
399
- case PrimitiveKind::Int16S: return Napi::Number::New(env, (double)ReverseBytes(result.i16));
400
- case PrimitiveKind::UInt16: return Napi::Number::New(env, (double)result.u16);
401
- case PrimitiveKind::UInt16S: return Napi::Number::New(env, (double)ReverseBytes(result.u16));
402
- case PrimitiveKind::Int32: return Napi::Number::New(env, (double)result.i32);
403
- case PrimitiveKind::Int32S: return Napi::Number::New(env, (double)ReverseBytes(result.i32));
404
- case PrimitiveKind::UInt32: return Napi::Number::New(env, (double)result.u32);
405
- case PrimitiveKind::UInt32S: return Napi::Number::New(env, (double)ReverseBytes(result.u32));
406
- case PrimitiveKind::Int64: return NewBigInt(env, result.i64);
407
- case PrimitiveKind::Int64S: return NewBigInt(env, ReverseBytes(result.i64));
408
- case PrimitiveKind::UInt64: return NewBigInt(env, result.u64);
409
- case PrimitiveKind::UInt64S: return NewBigInt(env, ReverseBytes(result.u64));
410
- case PrimitiveKind::String: return result.ptr ? Napi::String::New(env, (const char *)result.ptr) : env.Null();
411
- case PrimitiveKind::String16: return result.ptr ? Napi::String::New(env, (const char16_t *)result.ptr) : env.Null();
412
- case PrimitiveKind::String32: return result.ptr ? MakeStringFromUTF32(env, (const char32_t *)result.ptr) : env.Null();
413
- case PrimitiveKind::Pointer:
414
- case PrimitiveKind::Callback: {
415
- if (result.ptr) {
416
- Napi::External<void> external = Napi::External<void>::New(env, result.ptr);
417
- SetValueTag(external, func->ret.type->ref.marker);
418
-
419
- return external;
420
- } else {
421
- return env.Null();
422
- }
423
- } break;
424
- case PrimitiveKind::Record:
425
- case PrimitiveKind::Union: {
426
- const uint8_t *ptr = return_ptr ? (const uint8_t *)return_ptr
427
- : (const uint8_t *)&result.buf;
428
-
429
- Napi::Object obj = DecodeObject(env, ptr, func->ret.type);
430
- return obj;
431
- } break;
432
- case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
433
- case PrimitiveKind::Float32: return Napi::Number::New(env, (double)result.f);
434
- case PrimitiveKind::Float64: return Napi::Number::New(env, result.d);
435
-
436
- case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
437
- }
438
-
439
- K_UNREACHABLE();
440
- }
441
-
442
- void CallData::Relay(Size idx, uint8_t *sp)
443
- {
444
- const TrampolineInfo &trampoline = shared.trampolines[idx];
445
-
446
- uint8_t *caller_sp = sp + 48;
447
- BackRegisters *out_reg = (BackRegisters *)(sp + 16);
448
-
449
- const FunctionInfo *proto = trampoline.proto;
450
- Napi::Function func = trampoline.func.Value();
451
-
452
- uint32_t *args_ptr = (uint32_t *)caller_sp;
453
-
454
- uint8_t *return_ptr = !proto->ret.trivial ? (uint8_t *)args_ptr[0] : nullptr;
455
- args_ptr += !proto->ret.trivial;
456
-
457
- if (proto->convention == CallConvention::Stdcall) {
458
- out_reg->ret_pop = (int)proto->args_size;
459
- } else {
460
- #if defined(_WIN32)
461
- out_reg->ret_pop = 0;
462
- #else
463
- out_reg->ret_pop = return_ptr ? 4 : 0;
464
- #endif
465
- }
466
-
467
- K_DEFER_N(err_guard) {
468
- int pop = out_reg->ret_pop;
469
- memset(out_reg, 0, K_SIZE(*out_reg));
470
- out_reg->ret_type = 0;
471
- out_reg->ret_pop = pop;
472
- };
473
-
474
- LocalArray<napi_value, MaxParameters + 1> arguments;
475
-
476
- arguments.Append(!trampoline.recv.IsEmpty() ? trampoline.recv.Value() : env.Undefined());
477
-
478
- // Convert to JS arguments
479
- for (Size i = 0; i < proto->parameters.len; i++) {
480
- const ParameterInfo &param = proto->parameters[i];
481
- K_ASSERT(param.directions >= 1 && param.directions <= 3);
482
-
483
- switch (param.type->primitive) {
484
- case PrimitiveKind::Void: { K_UNREACHABLE(); } break;
485
-
486
- case PrimitiveKind::Bool: {
487
- bool b = *(bool *)(args_ptr++);
488
-
489
- Napi::Value arg = Napi::Boolean::New(env, b);
490
- arguments.Append(arg);
491
- } break;
492
- case PrimitiveKind::Int8: {
493
- double d = (double)*(int8_t *)(args_ptr++);
494
-
495
- Napi::Value arg = Napi::Number::New(env, d);
496
- arguments.Append(arg);
497
- } break;
498
- case PrimitiveKind::UInt8: {
499
- double d = (double)*(uint8_t *)(args_ptr++);
500
-
501
- Napi::Value arg = Napi::Number::New(env, d);
502
- arguments.Append(arg);
503
- } break;
504
- case PrimitiveKind::Int16: {
505
- double d = (double)*(int16_t *)(args_ptr++);
506
-
507
- Napi::Value arg = Napi::Number::New(env, d);
508
- arguments.Append(arg);
509
- } break;
510
- case PrimitiveKind::Int16S: {
511
- int16_t v = *(int16_t *)(args_ptr++);
512
- double d = (double)ReverseBytes(v);
513
-
514
- Napi::Value arg = Napi::Number::New(env, d);
515
- arguments.Append(arg);
516
- } break;
517
- case PrimitiveKind::UInt16: {
518
- double d = (double)*(uint16_t *)(args_ptr++);
519
-
520
- Napi::Value arg = Napi::Number::New(env, d);
521
- arguments.Append(arg);
522
- } break;
523
- case PrimitiveKind::UInt16S: {
524
- uint16_t v = *(uint16_t *)(args_ptr++);
525
- double d = (double)ReverseBytes(v);
526
-
527
- Napi::Value arg = Napi::Number::New(env, d);
528
- arguments.Append(arg);
529
- } break;
530
- case PrimitiveKind::Int32: {
531
- double d = (double)*(int32_t *)(args_ptr++);
532
-
533
- Napi::Value arg = Napi::Number::New(env, d);
534
- arguments.Append(arg);
535
- } break;
536
- case PrimitiveKind::Int32S: {
537
- int32_t v = *(int32_t *)(args_ptr++);
538
- double d = (double)ReverseBytes(v);
539
-
540
- Napi::Value arg = Napi::Number::New(env, d);
541
- arguments.Append(arg);
542
- } break;
543
- case PrimitiveKind::UInt32: {
544
- double d = (double)*(uint32_t *)(args_ptr++);
545
-
546
- Napi::Value arg = Napi::Number::New(env, d);
547
- arguments.Append(arg);
548
- } break;
549
- case PrimitiveKind::UInt32S: {
550
- uint32_t v = *(uint32_t *)(args_ptr++);
551
- double d = (double)ReverseBytes(v);
552
-
553
- Napi::Value arg = Napi::Number::New(env, d);
554
- arguments.Append(arg);
555
- } break;
556
- case PrimitiveKind::Int64: {
557
- int64_t v = *(int64_t *)args_ptr;
558
- args_ptr += 2;
559
-
560
- Napi::Value arg = NewBigInt(env, v);
561
- arguments.Append(arg);
562
- } break;
563
- case PrimitiveKind::Int64S: {
564
- int64_t v = *(int64_t *)args_ptr;
565
- args_ptr += 2;
566
-
567
- Napi::Value arg = NewBigInt(env, ReverseBytes(v));
568
- arguments.Append(arg);
569
- } break;
570
- case PrimitiveKind::UInt64: {
571
- uint64_t v = *(uint64_t *)args_ptr;
572
- args_ptr += 2;
573
-
574
- Napi::Value arg = NewBigInt(env, v);
575
- arguments.Append(arg);
576
- } break;
577
- case PrimitiveKind::UInt64S: {
578
- uint64_t v = *(uint64_t *)args_ptr;
579
- args_ptr += 2;
580
-
581
- Napi::Value arg = NewBigInt(env, ReverseBytes(v));
582
- arguments.Append(arg);
583
- } break;
584
- case PrimitiveKind::String: {
585
- const char *str = *(const char **)(args_ptr++);
586
-
587
- Napi::Value arg = str ? Napi::String::New(env, str) : env.Null();
588
- arguments.Append(arg);
589
-
590
- if (param.type->dispose) {
591
- param.type->dispose(env, param.type, str);
592
- }
593
- } break;
594
- case PrimitiveKind::String16: {
595
- const char16_t *str16 = *(const char16_t **)(args_ptr++);
596
-
597
- Napi::Value arg = str16 ? Napi::String::New(env, str16) : env.Null();
598
- arguments.Append(arg);
599
-
600
- if (param.type->dispose) {
601
- param.type->dispose(env, param.type, str16);
602
- }
603
- } break;
604
- case PrimitiveKind::String32: {
605
- const char32_t *str32 = *(const char32_t **)(args_ptr++);
606
-
607
- Napi::Value arg = str32 ? MakeStringFromUTF32(env, str32) : env.Null();
608
- arguments.Append(arg);
609
- } break;
610
- case PrimitiveKind::Pointer:
611
- case PrimitiveKind::Callback: {
612
- void *ptr2 = *(void **)(args_ptr++);
613
-
614
- if (ptr2) {
615
- Napi::External<void> external = Napi::External<void>::New(env, ptr2);
616
- SetValueTag(external, param.type->ref.marker);
617
-
618
- arguments.Append(external);
619
- } else {
620
- arguments.Append(env.Null());
621
- }
622
-
623
- if (param.type->dispose) {
624
- param.type->dispose(env, param.type, ptr2);
625
- }
626
- } break;
627
- case PrimitiveKind::Record:
628
- case PrimitiveKind::Union: {
629
- K_ASSERT(!param.fast);
630
-
631
- uint8_t *ptr = (uint8_t *)args_ptr;
632
-
633
- Napi::Object obj2 = DecodeObject(env, ptr, param.type);
634
- arguments.Append(obj2);
635
-
636
- args_ptr = (uint32_t *)AlignUp(ptr + param.type->size, 4);
637
- } break;
638
- case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
639
- case PrimitiveKind::Float32: {
640
- float f = *(float *)(args_ptr++);
641
-
642
- Napi::Value arg = Napi::Number::New(env, (double)f);
643
- arguments.Append(arg);
644
- } break;
645
- case PrimitiveKind::Float64: {
646
- double d = *(double *)args_ptr;
647
- args_ptr += 2;
648
-
649
- Napi::Value arg = Napi::Number::New(env, d);
650
- arguments.Append(arg);
651
- } break;
652
-
653
- case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
654
- }
655
- }
656
-
657
- const TypeInfo *type = proto->ret.type;
658
-
659
- // Make the call!
660
- Napi::Value value = func.Call(arguments[0], arguments.len - 1, arguments.data + 1);
661
-
662
- if (env.IsExceptionPending()) [[unlikely]]
663
- return;
664
-
665
- #define RETURN_INTEGER_32(CType) \
666
- do { \
667
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] { \
668
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
669
- return; \
670
- } \
671
- \
672
- CType v = GetNumber<CType>(value); \
673
- out_reg->eax = (uint32_t)v; \
674
- out_reg->ret_type = 0; \
675
- } while (false)
676
- #define RETURN_INTEGER_32_SWAP(CType) \
677
- do { \
678
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] { \
679
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
680
- return; \
681
- } \
682
- \
683
- CType v = GetNumber<CType>(value); \
684
- out_reg->eax = (uint32_t)ReverseBytes(v); \
685
- out_reg->ret_type = 0; \
686
- } while (false)
687
- #define RETURN_INTEGER_64(CType) \
688
- do { \
689
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] { \
690
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
691
- return; \
692
- } \
693
- \
694
- CType v = GetNumber<CType>(value); \
695
- \
696
- out_reg->eax = (uint32_t)((uint64_t)v >> 32); \
697
- out_reg->edx = (uint32_t)((uint64_t)v & 0xFFFFFFFFu); \
698
- out_reg->ret_type = 0; \
699
- } while (false)
700
- #define RETURN_INTEGER_64_SWAP(CType) \
701
- do { \
702
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] { \
703
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
704
- return; \
705
- } \
706
- \
707
- CType v = ReverseBytes(GetNumber<CType>(value)); \
708
- \
709
- out_reg->eax = (uint32_t)((uint64_t)v >> 32); \
710
- out_reg->edx = (uint32_t)((uint64_t)v & 0xFFFFFFFFu); \
711
- out_reg->ret_type = 0; \
712
- } while (false)
713
-
714
- switch (type->primitive) {
715
- case PrimitiveKind::Void: { out_reg->ret_type = 0; } break;
716
- case PrimitiveKind::Bool: {
717
- if (!value.IsBoolean()) [[unlikely]] {
718
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected boolean", GetValueType(instance, value));
719
- return;
720
- }
721
-
722
- bool b = value.As<Napi::Boolean>();
723
- out_reg->eax = (uint32_t)b;
724
- out_reg->ret_type = 0;
725
- } break;
726
- case PrimitiveKind::Int8: { RETURN_INTEGER_32(int8_t); } break;
727
- case PrimitiveKind::UInt8: { RETURN_INTEGER_32(uint8_t); } break;
728
- case PrimitiveKind::Int16: { RETURN_INTEGER_32(int16_t); } break;
729
- case PrimitiveKind::Int16S: { RETURN_INTEGER_32_SWAP(int16_t); } break;
730
- case PrimitiveKind::UInt16: { RETURN_INTEGER_32(uint16_t); } break;
731
- case PrimitiveKind::UInt16S: { RETURN_INTEGER_32_SWAP(uint16_t); } break;
732
- case PrimitiveKind::Int32: { RETURN_INTEGER_32(int32_t); } break;
733
- case PrimitiveKind::Int32S: { RETURN_INTEGER_32_SWAP(int32_t); } break;
734
- case PrimitiveKind::UInt32: { RETURN_INTEGER_32(uint32_t); } break;
735
- case PrimitiveKind::UInt32S: { RETURN_INTEGER_32_SWAP(uint32_t); } break;
736
- case PrimitiveKind::Int64: { RETURN_INTEGER_64(int64_t); } break;
737
- case PrimitiveKind::Int64S: { RETURN_INTEGER_64_SWAP(int64_t); } break;
738
- case PrimitiveKind::UInt64: { RETURN_INTEGER_64(uint64_t); } break;
739
- case PrimitiveKind::UInt64S: { RETURN_INTEGER_64_SWAP(uint64_t); } break;
740
- case PrimitiveKind::String: {
741
- const char *str;
742
- if (!PushString(value, 1, &str)) [[unlikely]]
743
- return;
744
-
745
- out_reg->eax = (uint32_t)str;
746
- out_reg->ret_type = 0;
747
- } break;
748
- case PrimitiveKind::String16: {
749
- const char16_t *str16;
750
- if (!PushString16(value, 1, &str16)) [[unlikely]]
751
- return;
752
-
753
- out_reg->eax = (uint32_t)str16;
754
- out_reg->ret_type = 0;
755
- } break;
756
- case PrimitiveKind::String32: {
757
- const char32_t *str32;
758
- if (!PushString32(value, 1, &str32)) [[unlikely]]
759
- return;
760
-
761
- out_reg->eax = (uint32_t)str32;
762
- out_reg->ret_type = 0;
763
- } break;
764
- case PrimitiveKind::Pointer: {
765
- uint8_t *ptr;
766
-
767
- if (CheckValueTag(value, type->ref.marker)) {
768
- ptr = value.As<Napi::External<uint8_t>>().Data();
769
- } else if (IsObject(value) && (type->ref.type->primitive == PrimitiveKind::Record ||
770
- type->ref.type->primitive == PrimitiveKind::Union)) {
771
- Napi::Object obj = value.As<Napi::Object>();
772
-
773
- ptr = AllocHeap(type->ref.type->size, 16);
774
-
775
- if (!PushObject(obj, type->ref.type, ptr))
776
- return;
777
- } else if (IsNullOrUndefined(value)) {
778
- ptr = nullptr;
779
- } else {
780
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected %2", GetValueType(instance, value), type->name);
781
- return;
782
- }
783
-
784
- out_reg->eax = (uint32_t)ptr;
785
- out_reg->ret_type = 0;
786
- } break;
787
- case PrimitiveKind::Record:
788
- case PrimitiveKind::Union: {
789
- if (!IsObject(value)) [[unlikely]] {
790
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected object", GetValueType(instance, value));
791
- return;
792
- }
793
-
794
- Napi::Object obj = value.As<Napi::Object>();
795
-
796
- if (return_ptr) {
797
- if (!PushObject(obj, type, return_ptr))
798
- return;
799
- out_reg->eax = (uint32_t)return_ptr;
800
- } else {
801
- PushObject(obj, type, (uint8_t *)&out_reg->eax);
802
- }
803
-
804
- out_reg->ret_type = 0;
805
- } break;
806
- case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
807
- case PrimitiveKind::Float32: {
808
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] {
809
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value));
810
- return;
811
- }
812
-
813
- out_reg->x87.f = GetNumber<float>(value);
814
- out_reg->ret_type = 1;
815
- } break;
816
- case PrimitiveKind::Float64: {
817
- if (!value.IsNumber() && !value.IsBigInt()) [[unlikely]] {
818
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value));
819
- return;
820
- }
821
-
822
- out_reg->x87.d = GetNumber<double>(value);
823
- out_reg->ret_type = 2;
824
- } break;
825
- case PrimitiveKind::Callback: {
826
- void *ptr;
827
-
828
- if (value.IsFunction()) {
829
- Napi::Function func2 = value.As<Napi::Function>();
830
-
831
- ptr = ReserveTrampoline(type->ref.proto, func2);
832
- if (!ptr) [[unlikely]]
833
- return;
834
- } else if (CheckValueTag(value, type->ref.marker)) {
835
- ptr = value.As<Napi::External<void>>().Data();
836
- } else if (IsNullOrUndefined(value)) {
837
- ptr = nullptr;
838
- } else {
839
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected %2", GetValueType(instance, value), type->name);
840
- return;
841
- }
842
-
843
- out_reg->eax = (uint32_t)ptr;
844
- out_reg->ret_type = 0;
845
- } break;
846
-
847
- case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
848
- }
849
-
850
- #undef RETURN_INTEGER_64_SWAP
851
- #undef RETURN_INTEGER_64
852
- #undef RETURN_INTEGER_32_SWAP
853
- #undef RETURN_INTEGER_32
854
-
855
- err_guard.Disable();
856
- }
857
-
858
- }
859
-
860
- #endif