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.
- package/CHANGELOG.md +2 -2
- package/README.md +8 -8
- package/build/koffi/darwin_arm64/koffi.node +0 -0
- package/build/koffi/darwin_x64/koffi.node +0 -0
- package/build/koffi/freebsd_arm64/koffi.node +0 -0
- package/build/koffi/freebsd_ia32/koffi.node +0 -0
- package/build/koffi/freebsd_x64/koffi.node +0 -0
- package/build/koffi/linux_arm64/koffi.node +0 -0
- package/build/koffi/linux_ia32/koffi.node +0 -0
- package/build/koffi/linux_x64/koffi.node +0 -0
- package/build/koffi/musl_arm64/koffi.node +0 -0
- package/build/koffi/musl_x64/koffi.node +0 -0
- package/build/koffi/openbsd_ia32/koffi.node +0 -0
- package/build/koffi/openbsd_x64/koffi.node +0 -0
- package/build/koffi/win32_ia32/koffi.node +0 -0
- package/build/koffi/win32_x64/koffi.node +0 -0
- package/doc/benchmarks.md +33 -33
- package/doc/benchmarks.xlsx +0 -0
- package/doc/contribute.md +37 -25
- package/doc/index.md +8 -8
- package/doc/variables.md +1 -1
- package/index.d.ts +11 -9
- package/index.js +1 -634
- package/indirect.js +1 -533
- package/lib/native/base/base.cc +63 -85
- package/lib/native/base/base.hh +178 -151
- package/package.json +3 -4
- package/src/build.js +1178 -0
- package/src/koffi/CMakeLists.txt +97 -34
- package/src/koffi/index.js +359 -0
- package/src/koffi/indirect.js +216 -0
- package/src/koffi/src/{abi_arm32.cc → abi/arm32.cc} +279 -275
- package/src/koffi/src/{abi_arm32_asm.S → abi/arm32_asm.S} +2 -5
- package/src/koffi/src/abi/arm64.cc +1511 -0
- package/src/koffi/src/{abi_arm64_asm.S → abi/arm64_asm.S} +44 -23
- package/src/koffi/src/{abi_arm64_asm.asm → abi/arm64_asm.asm} +32 -25
- package/src/koffi/src/{abi_loong64_asm.S → abi/loong64_asm.S} +1 -5
- package/src/koffi/src/{abi_riscv64.cc → abi/riscv64.cc} +279 -273
- package/src/koffi/src/{abi_riscv64_asm.S → abi/riscv64_asm.S} +1 -4
- package/src/koffi/src/abi/x64sysv.cc +1456 -0
- package/src/koffi/src/{abi_x64_sysv_asm.S → abi/x64sysv_asm.S} +30 -33
- package/src/koffi/src/abi/x64win.cc +1121 -0
- package/src/koffi/src/{abi_x64_win_asm.S → abi/x64win_asm.S} +8 -12
- package/src/koffi/src/{abi_x64_win_asm.asm → abi/x64win_asm.asm} +11 -14
- package/src/koffi/src/abi/x86.cc +1233 -0
- package/src/koffi/src/{abi_x86_asm.S → abi/x86_asm.S} +49 -25
- package/src/koffi/src/{abi_x86_asm.asm → abi/x86_asm.asm} +11 -14
- package/src/koffi/src/call.cc +458 -598
- package/src/koffi/src/call.hh +61 -82
- package/src/koffi/src/ffi.cc +595 -309
- package/src/koffi/src/ffi.hh +103 -89
- package/src/koffi/src/parser.cc +0 -4
- package/src/koffi/src/primitives.inc +39 -0
- package/src/koffi/{tools/write_trampolines.js → src/trampolines.js} +1 -1
- package/src/koffi/src/util.cc +503 -446
- package/src/koffi/src/util.hh +269 -57
- package/src/koffi/src/uv.cc +2 -2
- package/src/koffi/src/uv.def +3 -0
- package/src/koffi/src/win32.hh +30 -1
- package/vendor/node-addon-api/napi-inl.h +4 -0
- package/vendor/node-addon-api/napi.h +1 -0
- package/build/koffi/linux_armhf/koffi.node +0 -0
- package/build/koffi/linux_loong64/koffi.node +0 -0
- package/build/koffi/linux_riscv64d/koffi.node +0 -0
- package/build/koffi/win32_arm64/koffi.exp +0 -0
- package/build/koffi/win32_arm64/koffi.lib +0 -0
- package/build/koffi/win32_arm64/koffi.node +0 -0
- package/doc/platforms.md +0 -36
- package/src/cnoke/LICENSE.txt +0 -22
- package/src/cnoke/README.md +0 -99
- package/src/cnoke/assets/FindCNoke.cmake +0 -127
- package/src/cnoke/assets/toolchains.json +0 -126
- package/src/cnoke/assets/win_delay_hook.c +0 -36
- package/src/cnoke/cnoke.js +0 -170
- package/src/cnoke/package.json +0 -24
- package/src/cnoke/src/builder.js +0 -511
- package/src/cnoke/src/index.js +0 -10
- package/src/cnoke/src/tools.js +0 -407
- package/src/koffi/src/abi_arm64.cc +0 -1295
- package/src/koffi/src/abi_x64_sysv.cc +0 -939
- package/src/koffi/src/abi_x64_win.cc +0 -715
- package/src/koffi/src/abi_x86.cc +0 -860
- package/src/koffi/src/init.js +0 -105
- /package/src/koffi/src/{abi_loong64.cc → abi/loong64.cc} +0 -0
|
@@ -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 ¶m: func->parameters) {
|
|
212
|
-
AnalyseParameter(¶m, 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 ¶m = 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 ¶m = 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
|