koffi 0.9.0
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/CMakeLists.txt +94 -0
- package/README.md +153 -0
- package/build/ALL_BUILD.vcxproj +190 -0
- package/build/ALL_BUILD.vcxproj.filters +8 -0
- package/build/CMakeCache.txt +429 -0
- package/build/CMakeFiles/3.23.0-rc1/CMakeASMCompiler.cmake +20 -0
- package/build/CMakeFiles/3.23.0-rc1/CMakeASM_MASMCompiler.cmake +20 -0
- package/build/CMakeFiles/3.23.0-rc1/CMakeCCompiler.cmake +72 -0
- package/build/CMakeFiles/3.23.0-rc1/CMakeCXXCompiler.cmake +83 -0
- package/build/CMakeFiles/3.23.0-rc1/CMakeDetermineCompilerABI_C.bin +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CMakeDetermineCompilerABI_CXX.bin +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CMakeRCCompiler.cmake +6 -0
- package/build/CMakeFiles/3.23.0-rc1/CMakeSystem.cmake +15 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/CMakeCCompilerId.c +828 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/CompilerIdC.exe +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/CompilerIdC.vcxproj +71 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CMakeCCompilerId.obj +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.exe.recipe +11 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.tlog/CL.command.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.tlog/CL.read.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.tlog/CL.write.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.tlog/CompilerIdC.lastbuildstate +2 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.tlog/link.command.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.tlog/link.read.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.tlog/link.write.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/CMakeCXXCompilerId.cpp +816 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/CompilerIdCXX.exe +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/CompilerIdCXX.vcxproj +71 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CMakeCXXCompilerId.obj +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.exe.recipe +11 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.tlog/CL.command.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.tlog/CL.read.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.tlog/CL.write.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.tlog/CompilerIdCXX.lastbuildstate +2 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.tlog/link.command.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.tlog/link.read.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.tlog/link.write.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/VCTargetsPath.txt +1 -0
- package/build/CMakeFiles/3.23.0-rc1/VCTargetsPath.vcxproj +31 -0
- package/build/CMakeFiles/3.23.0-rc1/x64/Debug/VCTargetsPath.recipe +11 -0
- package/build/CMakeFiles/3.23.0-rc1/x64/Debug/VCTargetsPath.tlog/VCTargetsPath.lastbuildstate +2 -0
- package/build/CMakeFiles/41bcd16856091d4a38fd1f71fbe2f202/generate.stamp.rule +1 -0
- package/build/CMakeFiles/CMakeError.log +108 -0
- package/build/CMakeFiles/CMakeOutput.log +413 -0
- package/build/CMakeFiles/TargetDirectories.txt +4 -0
- package/build/CMakeFiles/cmake.check_cache +1 -0
- package/build/CMakeFiles/generate.stamp +1 -0
- package/build/CMakeFiles/generate.stamp.depend +28 -0
- package/build/CMakeFiles/generate.stamp.list +1 -0
- package/build/Raylib.dir/Release/Raylib.dll.recipe +14 -0
- package/build/Raylib.dir/Release/Raylib.tlog/CL.command.1.tlog +0 -0
- package/build/Raylib.dir/Release/Raylib.tlog/CL.read.1.tlog +0 -0
- package/build/Raylib.dir/Release/Raylib.tlog/CL.write.1.tlog +0 -0
- package/build/Raylib.dir/Release/Raylib.tlog/CustomBuild.command.1.tlog +10 -0
- package/build/Raylib.dir/Release/Raylib.tlog/CustomBuild.read.1.tlog +27 -0
- package/build/Raylib.dir/Release/Raylib.tlog/CustomBuild.write.1.tlog +2 -0
- package/build/Raylib.dir/Release/Raylib.tlog/Raylib.lastbuildstate +2 -0
- package/build/Raylib.dir/Release/Raylib.tlog/Raylib.write.1u.tlog +0 -0
- package/build/Raylib.dir/Release/Raylib.tlog/link.command.1.tlog +0 -0
- package/build/Raylib.dir/Release/Raylib.tlog/link.read.1.tlog +0 -0
- package/build/Raylib.dir/Release/Raylib.tlog/link.write.1.tlog +0 -0
- package/build/Raylib.dir/Release/raudio.obj +0 -0
- package/build/Raylib.dir/Release/rcore.obj +0 -0
- package/build/Raylib.dir/Release/rglfw.obj +0 -0
- package/build/Raylib.dir/Release/rmodels.obj +0 -0
- package/build/Raylib.dir/Release/rshapes.obj +0 -0
- package/build/Raylib.dir/Release/rtext.obj +0 -0
- package/build/Raylib.dir/Release/rtextures.obj +0 -0
- package/build/Raylib.dir/Release/utils.obj +0 -0
- package/build/Raylib.vcxproj +358 -0
- package/build/Raylib.vcxproj.filters +37 -0
- package/build/Release/Raylib.dll +0 -0
- package/build/Release/Raylib.exp +0 -0
- package/build/Release/Raylib.lib +0 -0
- package/build/Release/koffi.exp +0 -0
- package/build/Release/koffi.lib +0 -0
- package/build/Release/koffi.node +0 -0
- package/build/ZERO_CHECK.vcxproj +176 -0
- package/build/ZERO_CHECK.vcxproj.filters +13 -0
- package/build/cmake_install.cmake +44 -0
- package/build/koffi.dir/Release/call_arm64.obj +0 -0
- package/build/koffi.dir/Release/call_x64_sysv.obj +0 -0
- package/build/koffi.dir/Release/call_x64_win.obj +0 -0
- package/build/koffi.dir/Release/call_x64_win_fwd.obj +0 -0
- package/build/koffi.dir/Release/call_x86.obj +0 -0
- package/build/koffi.dir/Release/ffi.obj +0 -0
- package/build/koffi.dir/Release/koffi.node.recipe +14 -0
- package/build/koffi.dir/Release/koffi.tlog/CL.command.1.tlog +0 -0
- package/build/koffi.dir/Release/koffi.tlog/CL.read.1.tlog +0 -0
- package/build/koffi.dir/Release/koffi.tlog/CL.write.1.tlog +0 -0
- package/build/koffi.dir/Release/koffi.tlog/CustomBuild.command.1.tlog +10 -0
- package/build/koffi.dir/Release/koffi.tlog/CustomBuild.read.1.tlog +27 -0
- package/build/koffi.dir/Release/koffi.tlog/CustomBuild.write.1.tlog +2 -0
- package/build/koffi.dir/Release/koffi.tlog/Masm.read.1u.tlog +0 -0
- package/build/koffi.dir/Release/koffi.tlog/Masm.write.1u.tlog +0 -0
- package/build/koffi.dir/Release/koffi.tlog/koffi.lastbuildstate +2 -0
- package/build/koffi.dir/Release/koffi.tlog/koffi.write.1u.tlog +0 -0
- package/build/koffi.dir/Release/koffi.tlog/link.command.1.tlog +0 -0
- package/build/koffi.dir/Release/koffi.tlog/link.read.1.tlog +0 -0
- package/build/koffi.dir/Release/koffi.tlog/link.write.1.tlog +0 -0
- package/build/koffi.dir/Release/libcc.obj +0 -0
- package/build/koffi.dir/Release/util.obj +0 -0
- package/build/koffi.dir/Release/win_delay_load_hook.obj +0 -0
- package/build/koffi.sln +67 -0
- package/build/koffi.vcxproj +363 -0
- package/build/koffi.vcxproj.filters +40 -0
- package/build/x64/Release/ALL_BUILD/ALL_BUILD.recipe +20 -0
- package/build/x64/Release/ALL_BUILD/ALL_BUILD.tlog/ALL_BUILD.lastbuildstate +2 -0
- package/build/x64/Release/ALL_BUILD/ALL_BUILD.tlog/CustomBuild.command.1.tlog +10 -0
- package/build/x64/Release/ALL_BUILD/ALL_BUILD.tlog/CustomBuild.read.1.tlog +27 -0
- package/build/x64/Release/ALL_BUILD/ALL_BUILD.tlog/CustomBuild.write.1.tlog +2 -0
- package/build/x64/Release/ZERO_CHECK/ZERO_CHECK.recipe +11 -0
- package/build/x64/Release/ZERO_CHECK/ZERO_CHECK.tlog/CustomBuild.command.1.tlog +10 -0
- package/build/x64/Release/ZERO_CHECK/ZERO_CHECK.tlog/CustomBuild.read.1.tlog +28 -0
- package/build/x64/Release/ZERO_CHECK/ZERO_CHECK.tlog/CustomBuild.write.1.tlog +2 -0
- package/build/x64/Release/ZERO_CHECK/ZERO_CHECK.tlog/ZERO_CHECK.lastbuildstate +2 -0
- package/package.json +14 -0
- package/src/call.hh +27 -0
- package/src/call_arm64.cc +482 -0
- package/src/call_arm64_fwd.S +115 -0
- package/src/call_x64_sysv.cc +477 -0
- package/src/call_x64_sysv_fwd.S +131 -0
- package/src/call_x64_win.cc +243 -0
- package/src/call_x64_win_fwd.asm +105 -0
- package/src/call_x86.cc +259 -0
- package/src/call_x86_fwd.S +48 -0
- package/src/call_x86_fwd.asm +50 -0
- package/src/ffi.cc +504 -0
- package/src/ffi.hh +135 -0
- package/src/util.cc +296 -0
- package/src/util.hh +80 -0
package/src/util.cc
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
// This program is free software: you can redistribute it and/or modify
|
|
2
|
+
// it under the terms of the GNU Affero General Public License as published by
|
|
3
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
4
|
+
// (at your option) any later version.
|
|
5
|
+
//
|
|
6
|
+
// This program is distributed in the hope that it will be useful,
|
|
7
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
8
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
9
|
+
// GNU Affero General Public License for more details.
|
|
10
|
+
//
|
|
11
|
+
// You should have received a copy of the GNU Affero General Public License
|
|
12
|
+
// along with this program. If not, see https://www.gnu.org/licenses/.
|
|
13
|
+
|
|
14
|
+
#include "vendor/libcc/libcc.hh"
|
|
15
|
+
#include "ffi.hh"
|
|
16
|
+
#include "util.hh"
|
|
17
|
+
|
|
18
|
+
#include <napi.h>
|
|
19
|
+
|
|
20
|
+
namespace RG {
|
|
21
|
+
|
|
22
|
+
const char *GetValueType(const InstanceData *instance, Napi::Value value)
|
|
23
|
+
{
|
|
24
|
+
for (const TypeInfo &type: instance->types) {
|
|
25
|
+
if (CheckValueTag(instance, value, &type))
|
|
26
|
+
return type.name;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
switch (value.Type()) {
|
|
30
|
+
case napi_undefined: return "undefined";
|
|
31
|
+
case napi_null: return "null";
|
|
32
|
+
case napi_boolean: return "boolean";
|
|
33
|
+
case napi_number: return "number";
|
|
34
|
+
case napi_string: return "string";
|
|
35
|
+
case napi_symbol: return "symbol";
|
|
36
|
+
case napi_object: return "object";
|
|
37
|
+
case napi_function: return "fucntion";
|
|
38
|
+
case napi_external: return "external";
|
|
39
|
+
case napi_bigint: return "bigint";
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return "unknown";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
void SetValueTag(const InstanceData *instance, Napi::Value value, const void *marker)
|
|
46
|
+
{
|
|
47
|
+
napi_type_tag tag = { instance->tag_lower, (uint64_t)marker };
|
|
48
|
+
napi_status status = napi_type_tag_object(value.Env(), value, &tag);
|
|
49
|
+
RG_ASSERT(status == napi_ok);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
bool CheckValueTag(const InstanceData *instance, Napi::Value value, const void *marker)
|
|
53
|
+
{
|
|
54
|
+
bool match = false;
|
|
55
|
+
|
|
56
|
+
napi_type_tag tag = { instance->tag_lower, (uint64_t)marker };
|
|
57
|
+
napi_check_object_type_tag(value.Env(), value, &tag, &match);
|
|
58
|
+
|
|
59
|
+
return match;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const char *CopyNodeString(const Napi::Value &value, Allocator *alloc)
|
|
63
|
+
{
|
|
64
|
+
RG_ASSERT(value.IsString());
|
|
65
|
+
|
|
66
|
+
Napi::Env env = value.Env();
|
|
67
|
+
napi_status status;
|
|
68
|
+
|
|
69
|
+
size_t len = 0;
|
|
70
|
+
status = napi_get_value_string_utf8(env, value, nullptr, 0, &len);
|
|
71
|
+
RG_ASSERT(status == napi_ok);
|
|
72
|
+
|
|
73
|
+
Span<char> buf;
|
|
74
|
+
buf.len = (Size)len + 1;
|
|
75
|
+
buf.ptr = (char *)Allocator::Allocate(alloc, buf.len);
|
|
76
|
+
|
|
77
|
+
status = napi_get_value_string_utf8(env, value, buf.ptr, (size_t)buf.len, &len);
|
|
78
|
+
RG_ASSERT(status == napi_ok);
|
|
79
|
+
|
|
80
|
+
return buf.ptr;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
bool PushObject(const Napi::Object &obj, const TypeInfo *type, Allocator *alloc, uint8_t *dest)
|
|
84
|
+
{
|
|
85
|
+
Napi::Env env = obj.Env();
|
|
86
|
+
InstanceData *instance = env.GetInstanceData<InstanceData>();
|
|
87
|
+
|
|
88
|
+
RG_ASSERT(obj.IsObject());
|
|
89
|
+
RG_ASSERT(type->primitive == PrimitiveKind::Record);
|
|
90
|
+
|
|
91
|
+
dest = AlignUp(dest, type->align);
|
|
92
|
+
|
|
93
|
+
for (const RecordMember &member: type->members) {
|
|
94
|
+
Napi::Value value = obj.Get(member.name);
|
|
95
|
+
|
|
96
|
+
if (RG_UNLIKELY(value.IsUndefined())) {
|
|
97
|
+
ThrowError<Napi::TypeError>(env, "Missing expected object property '%1'", member.name);
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
dest = AlignUp(dest, member.type->align);
|
|
102
|
+
|
|
103
|
+
switch (member.type->primitive) {
|
|
104
|
+
case PrimitiveKind::Void: { RG_UNREACHABLE(); } break;
|
|
105
|
+
|
|
106
|
+
case PrimitiveKind::Bool: {
|
|
107
|
+
if (RG_UNLIKELY(!value.IsBoolean())) {
|
|
108
|
+
ThrowError<Napi::TypeError>(env, "Unexpected value %1 for member '%2', expected boolean", GetValueType(instance, value), member.name);
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
bool b = value.As<Napi::Boolean>();
|
|
113
|
+
*(bool *)dest = b;
|
|
114
|
+
} break;
|
|
115
|
+
|
|
116
|
+
case PrimitiveKind::Int8:
|
|
117
|
+
case PrimitiveKind::UInt8:
|
|
118
|
+
case PrimitiveKind::Int16:
|
|
119
|
+
case PrimitiveKind::UInt16:
|
|
120
|
+
case PrimitiveKind::Int32:
|
|
121
|
+
case PrimitiveKind::UInt32:
|
|
122
|
+
case PrimitiveKind::Int64:
|
|
123
|
+
case PrimitiveKind::UInt64: {
|
|
124
|
+
if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) {
|
|
125
|
+
ThrowError<Napi::TypeError>(env, "Unexpected value %1 for member '%2', expected number", GetValueType(instance, value), member.name);
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
int64_t v = CopyNodeNumber<int64_t>(value);
|
|
130
|
+
memcpy(dest, &v, member.type->size); // Little Endian
|
|
131
|
+
} break;
|
|
132
|
+
case PrimitiveKind::Float32: {
|
|
133
|
+
if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) {
|
|
134
|
+
ThrowError<Napi::TypeError>(env, "Unexpected value %1 for member '%2', expected number", GetValueType(instance, value), member.name);
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
float f = CopyNodeNumber<float>(value);
|
|
139
|
+
memcpy(dest, &f, 4);
|
|
140
|
+
} break;
|
|
141
|
+
case PrimitiveKind::Float64: {
|
|
142
|
+
if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) {
|
|
143
|
+
ThrowError<Napi::TypeError>(env, "Unexpected value %1 for member '%2', expected number", GetValueType(instance, value), member.name);
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
double d = CopyNodeNumber<double>(value);
|
|
148
|
+
memcpy(dest, &d, 8);
|
|
149
|
+
} break;
|
|
150
|
+
case PrimitiveKind::String: {
|
|
151
|
+
if (RG_UNLIKELY(!value.IsString())) {
|
|
152
|
+
ThrowError<Napi::TypeError>(env, "Unexpected value %1 for member '%2', expected string", GetValueType(instance, value), member.name);
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const char *str = CopyNodeString(value, alloc);
|
|
157
|
+
*(const char **)dest = str;
|
|
158
|
+
} break;
|
|
159
|
+
case PrimitiveKind::Pointer: {
|
|
160
|
+
if (RG_UNLIKELY(!CheckValueTag(instance, value, member.type))) {
|
|
161
|
+
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for member '%2', expected %3", GetValueType(instance, value), member.name, member.type->name);
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
Napi::External external = value.As<Napi::External<void>>();
|
|
166
|
+
void *ptr = external.Data();
|
|
167
|
+
*(void **)dest = ptr;
|
|
168
|
+
} break;
|
|
169
|
+
|
|
170
|
+
case PrimitiveKind::Record: {
|
|
171
|
+
if (RG_UNLIKELY(!value.IsObject())) {
|
|
172
|
+
ThrowError<Napi::TypeError>(env, "Unexpected value %1 for member '%2', expected object", GetValueType(instance, value), member.name);
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
Napi::Object obj = value.As<Napi::Object>();
|
|
177
|
+
if (!PushObject(obj, member.type, alloc, dest))
|
|
178
|
+
return false;
|
|
179
|
+
} break;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
dest += member.type->size;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
Napi::Object PopObject(Napi::Env env, const uint8_t *ptr, const TypeInfo *type)
|
|
189
|
+
{
|
|
190
|
+
InstanceData *instance = env.GetInstanceData<InstanceData>();
|
|
191
|
+
|
|
192
|
+
RG_ASSERT(type->primitive == PrimitiveKind::Record);
|
|
193
|
+
|
|
194
|
+
Napi::Object obj = Napi::Object::New(env);
|
|
195
|
+
|
|
196
|
+
ptr = AlignUp(ptr, type->align);
|
|
197
|
+
|
|
198
|
+
for (const RecordMember &member: type->members) {
|
|
199
|
+
ptr = AlignUp(ptr, member.type->align);
|
|
200
|
+
|
|
201
|
+
switch (member.type->primitive) {
|
|
202
|
+
case PrimitiveKind::Void: { RG_UNREACHABLE(); } break;
|
|
203
|
+
|
|
204
|
+
case PrimitiveKind::Bool: {
|
|
205
|
+
bool b = *(bool *)ptr;
|
|
206
|
+
obj.Set(member.name, Napi::Boolean::New(env, b));
|
|
207
|
+
} break;
|
|
208
|
+
case PrimitiveKind::Int8: {
|
|
209
|
+
double d = (double)*(int8_t *)ptr;
|
|
210
|
+
obj.Set(member.name, Napi::Number::New(env, d));
|
|
211
|
+
} break;
|
|
212
|
+
case PrimitiveKind::UInt8: {
|
|
213
|
+
double d = (double)*(uint8_t *)ptr;
|
|
214
|
+
obj.Set(member.name, Napi::Number::New(env, d));
|
|
215
|
+
} break;
|
|
216
|
+
case PrimitiveKind::Int16: {
|
|
217
|
+
double d = (double)*(int16_t *)ptr;
|
|
218
|
+
obj.Set(member.name, Napi::Number::New(env, d));
|
|
219
|
+
} break;
|
|
220
|
+
case PrimitiveKind::UInt16: {
|
|
221
|
+
double d = (double)*(uint16_t *)ptr;
|
|
222
|
+
obj.Set(member.name, Napi::Number::New(env, d));
|
|
223
|
+
} break;
|
|
224
|
+
case PrimitiveKind::Int32: {
|
|
225
|
+
double d = (double)*(int32_t *)ptr;
|
|
226
|
+
obj.Set(member.name, Napi::Number::New(env, d));
|
|
227
|
+
} break;
|
|
228
|
+
case PrimitiveKind::UInt32: {
|
|
229
|
+
double d = (double)*(uint32_t *)ptr;
|
|
230
|
+
obj.Set(member.name, Napi::Number::New(env, d));
|
|
231
|
+
} break;
|
|
232
|
+
case PrimitiveKind::Int64: {
|
|
233
|
+
int64_t v = *(int64_t *)ptr;
|
|
234
|
+
obj.Set(member.name, Napi::BigInt::New(env, v));
|
|
235
|
+
} break;
|
|
236
|
+
case PrimitiveKind::UInt64: {
|
|
237
|
+
uint64_t v = *(uint64_t *)ptr;
|
|
238
|
+
obj.Set(member.name, Napi::BigInt::New(env, v));
|
|
239
|
+
} break;
|
|
240
|
+
case PrimitiveKind::Float32: {
|
|
241
|
+
float f;
|
|
242
|
+
memcpy(&f, ptr, 4);
|
|
243
|
+
obj.Set(member.name, Napi::Number::New(env, (double)f));
|
|
244
|
+
} break;
|
|
245
|
+
case PrimitiveKind::Float64: {
|
|
246
|
+
double d;
|
|
247
|
+
memcpy(&d, ptr, 8);
|
|
248
|
+
obj.Set(member.name, Napi::Number::New(env, d));
|
|
249
|
+
} break;
|
|
250
|
+
case PrimitiveKind::String: {
|
|
251
|
+
const char *str = *(const char **)ptr;
|
|
252
|
+
obj.Set(member.name, Napi::String::New(env, str));
|
|
253
|
+
} break;
|
|
254
|
+
case PrimitiveKind::Pointer: {
|
|
255
|
+
void *ptr2 = *(void **)ptr;
|
|
256
|
+
|
|
257
|
+
Napi::External<void> external = Napi::External<void>::New(env, ptr2);
|
|
258
|
+
SetValueTag(instance, external, member.type);
|
|
259
|
+
|
|
260
|
+
obj.Set(member.name, external);
|
|
261
|
+
} break;
|
|
262
|
+
|
|
263
|
+
case PrimitiveKind::Record: {
|
|
264
|
+
Napi::Object obj2 = PopObject(env, ptr, member.type);
|
|
265
|
+
obj.Set(member.name, obj2);
|
|
266
|
+
} break;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
ptr += member.type->size;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return obj;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
void DumpStack(const FunctionInfo *func, Span<const uint8_t> sp)
|
|
276
|
+
{
|
|
277
|
+
PrintLn(stderr, "%!..+---- %1 ----%!0", func->name);
|
|
278
|
+
|
|
279
|
+
PrintLn(stderr, "Parameters:");
|
|
280
|
+
for (Size i = 0; i < func->parameters.len; i++) {
|
|
281
|
+
const ParameterInfo ¶m = func->parameters[i];
|
|
282
|
+
PrintLn(stderr, " %1 = %2", i, param.type->name);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
PrintLn(stderr, "Stack (%1 bytes) at 0x%2:", sp.len, sp.ptr);
|
|
286
|
+
for (const uint8_t *ptr = sp.begin(); ptr < sp.end();) {
|
|
287
|
+
Print(stderr, " [0x%1 %2 %3] ", FmtArg(ptr).Pad0(-16), FmtArg((ptr - sp.begin()) / 8).Pad(-4),
|
|
288
|
+
FmtArg(ptr - sp.begin()).Pad(-4));
|
|
289
|
+
for (int i = 0; ptr < sp.end() && i < 8; i++, ptr++) {
|
|
290
|
+
Print(stderr, " %1", FmtHex(*ptr).Pad0(-2));
|
|
291
|
+
}
|
|
292
|
+
PrintLn(stderr);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
}
|
package/src/util.hh
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// This program is free software: you can redistribute it and/or modify
|
|
2
|
+
// it under the terms of the GNU Affero General Public License as published by
|
|
3
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
4
|
+
// (at your option) any later version.
|
|
5
|
+
//
|
|
6
|
+
// This program is distributed in the hope that it will be useful,
|
|
7
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
8
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
9
|
+
// GNU Affero General Public License for more details.
|
|
10
|
+
//
|
|
11
|
+
// You should have received a copy of the GNU Affero General Public License
|
|
12
|
+
// along with this program. If not, see https://www.gnu.org/licenses/.
|
|
13
|
+
|
|
14
|
+
#pragma once
|
|
15
|
+
|
|
16
|
+
#include "vendor/libcc/libcc.hh"
|
|
17
|
+
|
|
18
|
+
#include <napi.h>
|
|
19
|
+
|
|
20
|
+
namespace RG {
|
|
21
|
+
|
|
22
|
+
struct InstanceData;
|
|
23
|
+
|
|
24
|
+
static inline Size AlignLen(Size len, Size align)
|
|
25
|
+
{
|
|
26
|
+
Size aligned = (len + align - 1) / align * align;
|
|
27
|
+
return aligned;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
static inline uint8_t *AlignUp(uint8_t *ptr, Size align)
|
|
31
|
+
{
|
|
32
|
+
uint8_t *aligned = (uint8_t *)(((uintptr_t)ptr + align - 1) / align * align);
|
|
33
|
+
return aligned;
|
|
34
|
+
}
|
|
35
|
+
static inline const uint8_t *AlignUp(const uint8_t *ptr, Size align)
|
|
36
|
+
{
|
|
37
|
+
const uint8_t *aligned = (const uint8_t *)(((uintptr_t)ptr + align - 1) / align * align);
|
|
38
|
+
return aligned;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
template <typename T, typename... Args>
|
|
42
|
+
static void ThrowError(Napi::Env env, const char *msg, Args... args)
|
|
43
|
+
{
|
|
44
|
+
char buf[1024];
|
|
45
|
+
Fmt(buf, msg, args...);
|
|
46
|
+
|
|
47
|
+
T::New(env, buf).ThrowAsJavaScriptException();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Can be slow, only use for error messages
|
|
51
|
+
const char *GetValueType(const InstanceData *instance, Napi::Value value);
|
|
52
|
+
|
|
53
|
+
void SetValueTag(const InstanceData *instance, Napi::Value value, const void *marker);
|
|
54
|
+
bool CheckValueTag(const InstanceData *instance, Napi::Value value, const void *marker);
|
|
55
|
+
|
|
56
|
+
template <typename T>
|
|
57
|
+
T CopyNodeNumber(const Napi::Value &value)
|
|
58
|
+
{
|
|
59
|
+
RG_ASSERT(value.IsNumber() || value.IsBigInt());
|
|
60
|
+
|
|
61
|
+
if (RG_LIKELY(value.IsNumber())) {
|
|
62
|
+
return (T)value.As<Napi::Number>();
|
|
63
|
+
} else if (value.IsBigInt()) {
|
|
64
|
+
Napi::BigInt bigint = value.As<Napi::BigInt>();
|
|
65
|
+
|
|
66
|
+
bool lossless;
|
|
67
|
+
return (T)bigint.Uint64Value(&lossless);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
RG_UNREACHABLE();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const char *CopyNodeString(const Napi::Value &value, Allocator *alloc);
|
|
74
|
+
|
|
75
|
+
bool PushObject(const Napi::Object &obj, const TypeInfo *type, Allocator *alloc, uint8_t *dest);
|
|
76
|
+
Napi::Object PopObject(Napi::Env env, const uint8_t *ptr, const TypeInfo *type);
|
|
77
|
+
|
|
78
|
+
void DumpStack(const FunctionInfo *func, Span<const uint8_t> sp);
|
|
79
|
+
|
|
80
|
+
}
|