@finogeeks2026/finshield-opencode 0.0.0-stage → 0.11.50
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/DESIGN.md +593 -0
- package/LICENSE.rtf +28 -0
- package/README.md +132 -2
- package/THIRD-PARTY-NOTICES.md +3131 -0
- package/bin/node.exe +4 -0
- package/dist/gui-experimental.cjs +96 -0
- package/dist/updater/server.js +1046 -0
- package/install/download-source.json +3 -0
- package/install/runtime.js +60 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/LICENSE.rtf +28 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/README.md +116 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/bin/finsafe_native.dll +0 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/native-build.json +5 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/README.md +6 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/index.js +12 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/package.json +24 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/win32_x64/koffi.lib +0 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/win32_x64/koffi.node +0 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/CHANGELOG.md +1183 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/LICENSE.txt +22 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/README.md +42 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/cnoke.cjs +816 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/benchmarks.md +80 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/callbacks.md +182 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/composites.md +549 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/contribute.md +163 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/index.md +46 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/load.md +191 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/migration.md +256 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/misc.md +76 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/output.md +299 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/pointers.md +385 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/primitives.md +151 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/start.md +110 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/types.md +88 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/unions.md +0 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/values.md +134 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/index.cjs +1 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/index.d.ts +389 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/index.js +2 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/indirect.cjs +1 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/indirect.js +2 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/lib/native/base/base.cc +11064 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/lib/native/base/base.hh +5951 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/lib/native/base/unicode.inc +408 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/package.json +68 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/CMakeLists.txt +249 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/index.cjs +296 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/index.js +281 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/indirect.cjs +295 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/indirect.js +280 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/arm64.cc +1408 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/arm64_asm.S +216 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/arm64_asm.asm +181 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/loong64.cc +5 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/loong64_asm.S +200 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/riscv64.cc +1345 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/riscv64_asm.S +200 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64sysv.cc +1434 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64sysv_asm.S +224 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64win.cc +1086 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64win_asm.S +162 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64win_asm.asm +189 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x86.cc +1185 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x86_asm.S +217 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x86_asm.asm +174 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/call.cc +2094 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/call.hh +179 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/errno.inc +462 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/ffi.cc +3009 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/ffi.hh +461 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/parser.cc +218 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/parser.hh +54 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/primitives.inc +36 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/static.cjs +130 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/static.js +133 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/trampolines.cjs +77 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/type.cc +725 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/type.hh +71 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/util.cc +1040 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/util.hh +493 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/uv.cc +214 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/uv.def +11 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/uv.hh +112 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/win32.cc +198 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/win32.hh +148 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/LICENSE.md +9 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/README.md +95 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/napi-inl.deprecated.h +186 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/napi-inl.h +7211 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/napi.h +3421 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/LICENSE +21 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/README.md +95 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/def/js_native_api.def +125 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/def/node_api.def +157 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/include/js_native_api.h +591 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/include/js_native_api_types.h +210 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/include/node_api.h +265 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/include/node_api_types.h +58 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/package.json +36 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/release.json +4 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/src/native.js +85 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/src/shell.js +20 -0
- package/node_modules/@finogeeks2026/finsafe-sdk/src/status.js +57 -0
- package/package.json +47 -4
- package/policy/candidate.yaml +347 -0
- package/release.json +4 -0
- package/src/activation.js +2 -0
- package/src/attachment.js +34 -0
- package/src/connector.js +91 -0
- package/src/contract.js +8 -0
- package/src/desktop-contract.js +4 -0
- package/src/desktop.js +25 -0
- package/src/gui.tsx +58 -0
- package/src/host.js +38 -0
- package/src/index.js +12 -0
- package/src/instance-settings.js +27 -0
- package/src/known-folders.js +22 -0
- package/src/management.js +17 -0
- package/src/preparation.js +18 -0
- package/src/presentation.js +153 -0
- package/src/settings.js +18 -0
- package/ui/feedback.cjs +45 -0
- package/ui/page.cjs +225 -0
- package/ui/state.cjs +44 -0
- package/updater/bootstrap.js +41 -0
- package/updater/common/activation.js +81 -0
- package/updater/common/coordinator.js +329 -0
- package/updater/common/download.js +57 -0
- package/updater/common/index.js +12 -0
- package/updater/common/package.js +53 -0
- package/updater/common/protocol.js +38 -0
- package/updater/common/transport.js +65 -0
- package/updater/coordinator.js +2 -0
- package/updater/launch.js +40 -0
- package/updater/management.js +9 -0
- package/updater/native.js +18 -0
- package/updater/operations.js +45 -0
- package/updater/protocol.js +1 -0
- package/updater/server.js +100 -0
- package/updater/transport.js +10 -0
- package/updater/windows-launch-reader.js +40 -0
- package/updater/windows-launch.js +96 -0
|
@@ -0,0 +1,1040 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// SPDX-FileCopyrightText: 2026 Niels Martignène <niels.martignene@protonmail.com>
|
|
3
|
+
|
|
4
|
+
#include "lib/native/base/base.hh"
|
|
5
|
+
#include "call.hh"
|
|
6
|
+
#include "ffi.hh"
|
|
7
|
+
#include "type.hh"
|
|
8
|
+
#include "util.hh"
|
|
9
|
+
|
|
10
|
+
#include <napi.h>
|
|
11
|
+
|
|
12
|
+
namespace K {
|
|
13
|
+
|
|
14
|
+
Napi::Function UnionValue::InitClass(InstanceData *instance, const TypeInfo *type)
|
|
15
|
+
{
|
|
16
|
+
K_ASSERT(type->primitive == PrimitiveKind::Union);
|
|
17
|
+
|
|
18
|
+
Napi::Env env = instance->env;
|
|
19
|
+
|
|
20
|
+
// node-addon-api wants std::vector
|
|
21
|
+
std::vector<Napi::ClassPropertyDescriptor<UnionValue>> properties;
|
|
22
|
+
properties.reserve(type->members.len);
|
|
23
|
+
|
|
24
|
+
for (Size i = 0; i < type->members.len; i++) {
|
|
25
|
+
const RecordMember &member = type->members[i];
|
|
26
|
+
|
|
27
|
+
napi_property_attributes attr = (napi_property_attributes)(napi_writable | napi_enumerable);
|
|
28
|
+
Napi::ClassPropertyDescriptor<UnionValue> prop = InstanceAccessor(member.name, &UnionValue::Getter,
|
|
29
|
+
&UnionValue::Setter, attr, (void *)i);
|
|
30
|
+
|
|
31
|
+
properties.push_back(prop);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
Napi::Function constructor = DefineClass(env, type->name, properties, (void *)type);
|
|
35
|
+
return constructor;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
UnionValue::UnionValue(const Napi::CallbackInfo &info)
|
|
39
|
+
: Napi::ObjectWrap<UnionValue>(info), type((const TypeInfo *)info.Data())
|
|
40
|
+
{
|
|
41
|
+
Napi::Env env = info.Env();
|
|
42
|
+
|
|
43
|
+
K_ASSERT(type->instance);
|
|
44
|
+
instance = type->instance;
|
|
45
|
+
|
|
46
|
+
if (info.Length() >= 1) {
|
|
47
|
+
if (!info[0].IsExternal()) [[unlikely]] {
|
|
48
|
+
ThrowError<Napi::Error>(env, "Union objects cannot be constructed manually");
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
Napi::External<void> external = info[0].As<Napi::External<void>>();
|
|
53
|
+
const uint8_t *ptr = (const uint8_t *)external.Data();
|
|
54
|
+
|
|
55
|
+
SetRaw(ptr);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
void UnionValue::Finalize(Napi::BasicEnv env)
|
|
60
|
+
{
|
|
61
|
+
node_api_delete_reference(env, *this);
|
|
62
|
+
SuppressDestruct();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
void UnionValue::SetRaw(const uint8_t *ptr)
|
|
66
|
+
{
|
|
67
|
+
raw.RemoveFrom(0);
|
|
68
|
+
raw.Append(MakeSpan(ptr, type->size));
|
|
69
|
+
|
|
70
|
+
Value().Set(instance->active_symbol.Value(), Env().Undefined());
|
|
71
|
+
active_idx = -1;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
Napi::Value UnionValue::Getter(const Napi::CallbackInfo &info)
|
|
75
|
+
{
|
|
76
|
+
Size idx = (Size)info.Data();
|
|
77
|
+
const RecordMember &member = type->members[idx];
|
|
78
|
+
|
|
79
|
+
napi_value value = nullptr;
|
|
80
|
+
|
|
81
|
+
if (idx == active_idx) {
|
|
82
|
+
value = Value().Get(instance->active_symbol.Value());
|
|
83
|
+
} else {
|
|
84
|
+
Napi::Env env = info.Env();
|
|
85
|
+
|
|
86
|
+
if (!raw.len) [[unlikely]] {
|
|
87
|
+
ThrowError<Napi::Error>(env, "Cannont convert %1 union value", active_idx < 0 ? "empty" : "assigned");
|
|
88
|
+
return env.Null();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
value = Decode(instance, raw.ptr, member.type);
|
|
92
|
+
|
|
93
|
+
Value().Set(instance->active_symbol.Value(), value);
|
|
94
|
+
active_idx = idx;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
K_ASSERT(value);
|
|
98
|
+
return Napi::Value(info.Env(), value);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
void UnionValue::Setter(const Napi::CallbackInfo &info, const Napi::Value &value)
|
|
102
|
+
{
|
|
103
|
+
Size idx = (Size)info.Data();
|
|
104
|
+
|
|
105
|
+
Value().Set(instance->active_symbol.Value(), value);
|
|
106
|
+
active_idx = idx;
|
|
107
|
+
|
|
108
|
+
raw.Clear();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
void SetValueTag(napi_env env, napi_value value, const void *marker)
|
|
112
|
+
{
|
|
113
|
+
static_assert(K_SIZE(TypeInfo) >= 16);
|
|
114
|
+
|
|
115
|
+
// We used to make a temporary tag on the stack with lower set to a constant value and
|
|
116
|
+
// upper to the pointer address, but this broke in Node 20.12 and Node 21.6 due to ExternalWrapper
|
|
117
|
+
// storing a pointer to the tag and not the tag value itself (which seems wrong, but anyway).
|
|
118
|
+
//
|
|
119
|
+
// Since this is no longer an option, we just type alias whatever marker points to to napi_type_tag.
|
|
120
|
+
// This may seem gross, but we disable strict aliasing anyway, so as long as what marker points
|
|
121
|
+
// to is bigger than 16 bytes and does not change, it works!
|
|
122
|
+
// Which holds true for us: the main thing pointed to is TypeInfo, which is constant and big enough,
|
|
123
|
+
// and the few other markers we use, such as CastMarker, are actual const napi_type_tag structs.
|
|
124
|
+
const napi_type_tag *tag = (const napi_type_tag *)marker;
|
|
125
|
+
|
|
126
|
+
NAPI_OK(napi_type_tag_object(env, value, tag));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
bool CheckValueTag(napi_env env, napi_value value, const void *marker)
|
|
130
|
+
{
|
|
131
|
+
K_ASSERT(IsObject(env, value) || GetKindOf(env, value) == napi_external);
|
|
132
|
+
|
|
133
|
+
bool match = false;
|
|
134
|
+
|
|
135
|
+
const napi_type_tag *tag = (const napi_type_tag *)marker;
|
|
136
|
+
napi_check_object_type_tag(env, value, tag, &match);
|
|
137
|
+
|
|
138
|
+
return match;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
int GetTypedArrayType(const TypeInfo *type)
|
|
142
|
+
{
|
|
143
|
+
switch (type->primitive) {
|
|
144
|
+
case PrimitiveKind::Int8: return napi_int8_array;
|
|
145
|
+
case PrimitiveKind::UInt8: return napi_uint8_array;
|
|
146
|
+
case PrimitiveKind::Int16: return napi_int16_array;
|
|
147
|
+
case PrimitiveKind::UInt16: return napi_uint16_array;
|
|
148
|
+
case PrimitiveKind::Int32: return napi_int32_array;
|
|
149
|
+
case PrimitiveKind::UInt32: return napi_uint32_array;
|
|
150
|
+
case PrimitiveKind::Float32: return napi_float32_array;
|
|
151
|
+
case PrimitiveKind::Float64: return napi_float64_array;
|
|
152
|
+
|
|
153
|
+
default: return -1;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
K_UNREACHABLE();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
static FORCE_INLINE napi_value NewStringUTF32(Napi::Env env, const char32_t *ptr, Size len)
|
|
160
|
+
{
|
|
161
|
+
static const char16_t ReplacementChar = 0xFFFD;
|
|
162
|
+
|
|
163
|
+
HeapArray<char16_t> buf;
|
|
164
|
+
buf.Reserve(len * 2);
|
|
165
|
+
|
|
166
|
+
for (Size i = 0; i < len; i++) {
|
|
167
|
+
char32_t uc = ptr[i];
|
|
168
|
+
|
|
169
|
+
if (uc <= 0xFFFF) {
|
|
170
|
+
if (uc < 0xD800 || uc > 0xDFFF) {
|
|
171
|
+
buf.Append((char16_t)uc);
|
|
172
|
+
} else {
|
|
173
|
+
buf.Append(ReplacementChar);
|
|
174
|
+
}
|
|
175
|
+
} else if (uc <= 0x10FFFF) {
|
|
176
|
+
uc -= 0x0010000UL;
|
|
177
|
+
|
|
178
|
+
buf.Append((char16_t)((uc >> 10) + 0xD800));
|
|
179
|
+
buf.Append((char16_t)((uc & 0x3FFul) + 0xDC00));
|
|
180
|
+
} else {
|
|
181
|
+
buf.Append(ReplacementChar);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return NewString(env, buf.ptr, buf.len);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
napi_value NewString(Napi::Env env, const char32_t *ptr, Size len)
|
|
189
|
+
{
|
|
190
|
+
if (ptr) {
|
|
191
|
+
return NewStringUTF32(env, ptr, len);
|
|
192
|
+
} else {
|
|
193
|
+
napi_value value;
|
|
194
|
+
NAPI_OK(napi_get_null(env, &value));
|
|
195
|
+
return value;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
napi_value NewString(Napi::Env env, const char32_t *ptr)
|
|
200
|
+
{
|
|
201
|
+
if (ptr) {
|
|
202
|
+
Size len = NullTerminatedLength(ptr);
|
|
203
|
+
return NewStringUTF32(env, ptr, len);
|
|
204
|
+
} else {
|
|
205
|
+
napi_value value;
|
|
206
|
+
NAPI_OK(napi_get_null(env, &value));
|
|
207
|
+
return value;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
static uint32_t DecodeDynamicLength(const uint8_t *origin, const RecordMember &by)
|
|
212
|
+
{
|
|
213
|
+
const uint8_t *src = origin + by.offset;
|
|
214
|
+
|
|
215
|
+
switch (by.type->primitive) {
|
|
216
|
+
case PrimitiveKind::Int8: {
|
|
217
|
+
int8_t i = *(int8_t *)src;
|
|
218
|
+
return (uint32_t)i;
|
|
219
|
+
} break;
|
|
220
|
+
case PrimitiveKind::UInt8: {
|
|
221
|
+
uint8_t u = *(uint8_t *)src;
|
|
222
|
+
return (uint32_t)u;
|
|
223
|
+
} break;
|
|
224
|
+
case PrimitiveKind::Int16: {
|
|
225
|
+
int16_t i;
|
|
226
|
+
memcpy(&i, src, 2);
|
|
227
|
+
return (uint32_t)i;
|
|
228
|
+
} break;
|
|
229
|
+
case PrimitiveKind::Int16S: {
|
|
230
|
+
int16_t i;
|
|
231
|
+
memcpy(&i, src, 2);
|
|
232
|
+
return (uint32_t)ReverseBytes(i);
|
|
233
|
+
} break;
|
|
234
|
+
case PrimitiveKind::UInt16: {
|
|
235
|
+
uint16_t u;
|
|
236
|
+
memcpy(&u, src, 2);
|
|
237
|
+
return (uint32_t)u;
|
|
238
|
+
} break;
|
|
239
|
+
case PrimitiveKind::UInt16S: {
|
|
240
|
+
uint16_t u;
|
|
241
|
+
memcpy(&u, src, 2);
|
|
242
|
+
return (uint32_t)ReverseBytes(u);
|
|
243
|
+
} break;
|
|
244
|
+
case PrimitiveKind::Int32: {
|
|
245
|
+
int32_t i;
|
|
246
|
+
memcpy(&i, src, 4);
|
|
247
|
+
return (uint32_t)i;
|
|
248
|
+
} break;
|
|
249
|
+
case PrimitiveKind::Int32S: {
|
|
250
|
+
int32_t i;
|
|
251
|
+
memcpy(&i, src, 4);
|
|
252
|
+
return (uint32_t)ReverseBytes(i);
|
|
253
|
+
} break;
|
|
254
|
+
case PrimitiveKind::UInt32: {
|
|
255
|
+
uint32_t u;
|
|
256
|
+
memcpy(&u, src, 4);
|
|
257
|
+
return (uint32_t)u;
|
|
258
|
+
} break;
|
|
259
|
+
case PrimitiveKind::UInt32S: {
|
|
260
|
+
uint32_t u;
|
|
261
|
+
memcpy(&u, src, 4);
|
|
262
|
+
return (uint32_t)ReverseBytes(u);
|
|
263
|
+
} break;
|
|
264
|
+
case PrimitiveKind::Int64: {
|
|
265
|
+
int64_t i;
|
|
266
|
+
memcpy(&i, src, 8);
|
|
267
|
+
return (uint32_t)i;
|
|
268
|
+
} break;
|
|
269
|
+
case PrimitiveKind::Int64S: {
|
|
270
|
+
int64_t i;
|
|
271
|
+
memcpy(&i, src, 8);
|
|
272
|
+
return (uint32_t)ReverseBytes(i);
|
|
273
|
+
} break;
|
|
274
|
+
case PrimitiveKind::UInt64: {
|
|
275
|
+
uint64_t u;
|
|
276
|
+
memcpy(&u, src, 8);
|
|
277
|
+
return (uint32_t)u;
|
|
278
|
+
} break;
|
|
279
|
+
case PrimitiveKind::UInt64S: {
|
|
280
|
+
uint64_t u;
|
|
281
|
+
memcpy(&u, src, 8);
|
|
282
|
+
return (uint32_t)ReverseBytes(u);
|
|
283
|
+
} break;
|
|
284
|
+
|
|
285
|
+
case PrimitiveKind::Void:
|
|
286
|
+
case PrimitiveKind::Bool:
|
|
287
|
+
case PrimitiveKind::String:
|
|
288
|
+
case PrimitiveKind::String16:
|
|
289
|
+
case PrimitiveKind::String32:
|
|
290
|
+
case PrimitiveKind::Pointer:
|
|
291
|
+
case PrimitiveKind::Callback:
|
|
292
|
+
case PrimitiveKind::Record:
|
|
293
|
+
case PrimitiveKind::Union:
|
|
294
|
+
case PrimitiveKind::Array:
|
|
295
|
+
case PrimitiveKind::Float32:
|
|
296
|
+
case PrimitiveKind::Float64:
|
|
297
|
+
case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
K_UNREACHABLE();
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
template <typename SetFunc>
|
|
304
|
+
static FORCE_INLINE void DecodeObject(InstanceData *instance, const uint8_t *origin, const TypeInfo *type, SetFunc set)
|
|
305
|
+
{
|
|
306
|
+
K_ASSERT(type->primitive == PrimitiveKind::Record);
|
|
307
|
+
|
|
308
|
+
Napi::Env env = instance->env;
|
|
309
|
+
Span<const RecordMember> members = type->members;
|
|
310
|
+
|
|
311
|
+
for (Size i = 0; i < members.len; i++) {
|
|
312
|
+
const RecordMember &member = members[i];
|
|
313
|
+
const uint8_t *src = origin + member.offset;
|
|
314
|
+
|
|
315
|
+
switch (member.type->primitive) {
|
|
316
|
+
case PrimitiveKind::Void: { K_UNREACHABLE(); } break;
|
|
317
|
+
|
|
318
|
+
case PrimitiveKind::Bool: {
|
|
319
|
+
bool b = *(bool *)src;
|
|
320
|
+
set(i, member, Napi::Boolean::New(env, b));
|
|
321
|
+
} break;
|
|
322
|
+
case PrimitiveKind::Int8: {
|
|
323
|
+
int8_t v = *(int8_t *)src;
|
|
324
|
+
set(i, member, NewInt(env, v));
|
|
325
|
+
} break;
|
|
326
|
+
case PrimitiveKind::UInt8: {
|
|
327
|
+
uint8_t v = *(uint8_t *)src;
|
|
328
|
+
set(i, member, NewInt(env, v));
|
|
329
|
+
} break;
|
|
330
|
+
case PrimitiveKind::Int16: {
|
|
331
|
+
int16_t v;
|
|
332
|
+
memcpy(&v, src, 2);
|
|
333
|
+
set(i, member, NewInt(env, v));
|
|
334
|
+
} break;
|
|
335
|
+
case PrimitiveKind::Int16S: {
|
|
336
|
+
int16_t v;
|
|
337
|
+
memcpy(&v, src, 2);
|
|
338
|
+
set(i, member, NewInt(env, ReverseBytes(v)));
|
|
339
|
+
} break;
|
|
340
|
+
case PrimitiveKind::UInt16: {
|
|
341
|
+
uint16_t v;
|
|
342
|
+
memcpy(&v, src, 2);
|
|
343
|
+
set(i, member, NewInt(env, v));
|
|
344
|
+
} break;
|
|
345
|
+
case PrimitiveKind::UInt16S: {
|
|
346
|
+
uint16_t v;
|
|
347
|
+
memcpy(&v, src, 2);
|
|
348
|
+
set(i, member, NewInt(env, ReverseBytes(v)));
|
|
349
|
+
} break;
|
|
350
|
+
case PrimitiveKind::Int32: {
|
|
351
|
+
int32_t v;
|
|
352
|
+
memcpy(&v, src, 4);
|
|
353
|
+
set(i, member, NewInt(env, v));
|
|
354
|
+
} break;
|
|
355
|
+
case PrimitiveKind::Int32S: {
|
|
356
|
+
int32_t v;
|
|
357
|
+
memcpy(&v, src, 4);
|
|
358
|
+
set(i, member, NewInt(env, ReverseBytes(v)));
|
|
359
|
+
} break;
|
|
360
|
+
case PrimitiveKind::UInt32: {
|
|
361
|
+
uint32_t v;
|
|
362
|
+
memcpy(&v, src, 4);
|
|
363
|
+
set(i, member, NewInt(env, v));
|
|
364
|
+
} break;
|
|
365
|
+
case PrimitiveKind::UInt32S: {
|
|
366
|
+
uint32_t v;
|
|
367
|
+
memcpy(&v, src, 4);
|
|
368
|
+
set(i, member, NewInt(env, ReverseBytes(v)));
|
|
369
|
+
} break;
|
|
370
|
+
case PrimitiveKind::Int64: {
|
|
371
|
+
int64_t v;
|
|
372
|
+
memcpy(&v, src, 8);
|
|
373
|
+
set(i, member, NewInt(env, v));
|
|
374
|
+
} break;
|
|
375
|
+
case PrimitiveKind::Int64S: {
|
|
376
|
+
int64_t v;
|
|
377
|
+
memcpy(&v, src, 8);
|
|
378
|
+
set(i, member, NewInt(env, ReverseBytes(v)));
|
|
379
|
+
} break;
|
|
380
|
+
case PrimitiveKind::UInt64: {
|
|
381
|
+
uint64_t v;
|
|
382
|
+
memcpy(&v, src, 8);
|
|
383
|
+
set(i, member, NewInt(env, v));
|
|
384
|
+
} break;
|
|
385
|
+
case PrimitiveKind::UInt64S: {
|
|
386
|
+
uint64_t v;
|
|
387
|
+
memcpy(&v, src, 8);
|
|
388
|
+
set(i, member, NewInt(env, ReverseBytes(v)));
|
|
389
|
+
} break;
|
|
390
|
+
case PrimitiveKind::String: {
|
|
391
|
+
const char *str;
|
|
392
|
+
memcpy(&str, src, K_SIZE(void *));
|
|
393
|
+
set(i, member, NewString(env, str));
|
|
394
|
+
|
|
395
|
+
if (member.type->dispose) {
|
|
396
|
+
member.type->dispose(instance, member.type, str);
|
|
397
|
+
}
|
|
398
|
+
} break;
|
|
399
|
+
case PrimitiveKind::String16: {
|
|
400
|
+
const char16_t *str16;
|
|
401
|
+
memcpy(&str16, src, K_SIZE(void *));
|
|
402
|
+
set(i, member, NewString(env, str16));
|
|
403
|
+
|
|
404
|
+
if (member.type->dispose) {
|
|
405
|
+
member.type->dispose(instance, member.type, str16);
|
|
406
|
+
}
|
|
407
|
+
} break;
|
|
408
|
+
case PrimitiveKind::String32: {
|
|
409
|
+
const char32_t *str32;
|
|
410
|
+
memcpy(&str32, src, K_SIZE(void *));
|
|
411
|
+
set(i, member, NewString(env, str32));
|
|
412
|
+
} break;
|
|
413
|
+
case PrimitiveKind::Pointer: {
|
|
414
|
+
void *ptr2;
|
|
415
|
+
memcpy(&ptr2, src, K_SIZE(void *));
|
|
416
|
+
|
|
417
|
+
if (member.countedby >= 0) {
|
|
418
|
+
const RecordMember &by = members[member.countedby];
|
|
419
|
+
uint32_t len = DecodeDynamicLength(origin, by);
|
|
420
|
+
|
|
421
|
+
napi_value value = DecodeArray(instance, (const uint8_t *)ptr2, member.type, len);
|
|
422
|
+
set(i, member, value);
|
|
423
|
+
} else {
|
|
424
|
+
napi_value p = WrapPointer(env, member.type->ref.type, ptr2);
|
|
425
|
+
set(i, member, p);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
if (member.type->dispose) {
|
|
429
|
+
member.type->dispose(instance, member.type, ptr2);
|
|
430
|
+
}
|
|
431
|
+
} break;
|
|
432
|
+
case PrimitiveKind::Callback: {
|
|
433
|
+
void *ptr2;
|
|
434
|
+
memcpy(&ptr2, src, K_SIZE(void *));
|
|
435
|
+
|
|
436
|
+
napi_value p = WrapPointer(env, member.type->ref.type, ptr2);
|
|
437
|
+
set(i, member, p);
|
|
438
|
+
|
|
439
|
+
if (member.type->dispose) {
|
|
440
|
+
member.type->dispose(instance, member.type, ptr2);
|
|
441
|
+
}
|
|
442
|
+
} break;
|
|
443
|
+
case PrimitiveKind::Record:
|
|
444
|
+
case PrimitiveKind::Union: {
|
|
445
|
+
napi_value obj2 = DecodeObject(instance, src, member.type);
|
|
446
|
+
set(i, member, obj2);
|
|
447
|
+
} break;
|
|
448
|
+
case PrimitiveKind::Array: {
|
|
449
|
+
if (member.countedby >= 0) {
|
|
450
|
+
const RecordMember &by = members[member.countedby];
|
|
451
|
+
|
|
452
|
+
uint32_t len = DecodeDynamicLength(origin, by);
|
|
453
|
+
uint32_t max = member.type->size / member.type->ref.type->size;
|
|
454
|
+
|
|
455
|
+
// Silently truncate result
|
|
456
|
+
len = std::min(len, max);
|
|
457
|
+
|
|
458
|
+
napi_value value = DecodeArray(instance, src, member.type, len);
|
|
459
|
+
set(i, member, value);
|
|
460
|
+
} else {
|
|
461
|
+
napi_value value = DecodeArray(instance, src, member.type);
|
|
462
|
+
set(i, member, value);
|
|
463
|
+
}
|
|
464
|
+
} break;
|
|
465
|
+
case PrimitiveKind::Float32: {
|
|
466
|
+
float f;
|
|
467
|
+
memcpy(&f, src, 4);
|
|
468
|
+
set(i, member, NewFloat(env, f));
|
|
469
|
+
} break;
|
|
470
|
+
case PrimitiveKind::Float64: {
|
|
471
|
+
double d;
|
|
472
|
+
memcpy(&d, src, 8);
|
|
473
|
+
set(i, member, NewFloat(env, d));
|
|
474
|
+
} break;
|
|
475
|
+
|
|
476
|
+
case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
napi_value DecodeObject(InstanceData *instance, const uint8_t *origin, const TypeInfo *type)
|
|
482
|
+
{
|
|
483
|
+
Napi::Env env = instance->env;
|
|
484
|
+
|
|
485
|
+
// We can't decode unions because we don't know which member is valid
|
|
486
|
+
if (type->primitive == PrimitiveKind::Union) {
|
|
487
|
+
Napi::Function construct;
|
|
488
|
+
{
|
|
489
|
+
napi_value value;
|
|
490
|
+
NAPI_OK(napi_get_reference_value(env, type->construct, &value));
|
|
491
|
+
|
|
492
|
+
construct = Napi::Function(env, value);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
Napi::External<void> external = Napi::External<void>::New(env, (void *)origin);
|
|
496
|
+
Napi::Object wrapper = construct.New({ external }).As<Napi::Object>();
|
|
497
|
+
SetValueTag(env, wrapper, &UnionValueMarker);
|
|
498
|
+
|
|
499
|
+
return wrapper;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// Only supported in recent Node versions, and still experimental at this time
|
|
503
|
+
if (node_api_create_object_with_properties && type->members.len <= 256) {
|
|
504
|
+
napi_value properties[256];
|
|
505
|
+
napi_value values[256];
|
|
506
|
+
|
|
507
|
+
DecodeObject(instance, origin, type, [&](Size i, const RecordMember &member, napi_value value) {
|
|
508
|
+
NAPI_OK(napi_get_reference_value(env, member.key, &properties[i]));
|
|
509
|
+
values[i] = value;
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
napi_value obj;
|
|
513
|
+
NAPI_OK(node_api_create_object_with_properties(env, instance->object_constructor.Value(), properties, values, type->members.len, &obj));
|
|
514
|
+
|
|
515
|
+
return obj;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
Napi::Object obj = Napi::Object::New(env);
|
|
519
|
+
DecodeObject(instance, obj, origin, type);
|
|
520
|
+
|
|
521
|
+
return obj;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
void DecodeObject(InstanceData *instance, napi_value obj, const uint8_t *origin, const TypeInfo *type)
|
|
525
|
+
{
|
|
526
|
+
Napi::Env env = instance->env;
|
|
527
|
+
|
|
528
|
+
if (node_api_create_property_key_utf8) {
|
|
529
|
+
DecodeObject(instance, origin, type, [&](Size, const RecordMember &member, napi_value value) {
|
|
530
|
+
napi_value key = nullptr;
|
|
531
|
+
napi_get_reference_value(env, member.key, &key);
|
|
532
|
+
|
|
533
|
+
NAPI_OK(napi_set_property(env, obj, key, value));
|
|
534
|
+
});
|
|
535
|
+
} else {
|
|
536
|
+
DecodeObject(instance, origin, type, [&](Size, const RecordMember &member, napi_value value) {
|
|
537
|
+
NAPI_OK(napi_set_named_property(env, obj, member.name, value));
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
napi_value DecodeArray(InstanceData *instance, const uint8_t *origin, const TypeInfo *type)
|
|
543
|
+
{
|
|
544
|
+
K_ASSERT(type->primitive == PrimitiveKind::Array);
|
|
545
|
+
|
|
546
|
+
uint32_t len = type->size / type->ref.stride;
|
|
547
|
+
return DecodeArray(instance, origin, type, len);
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
napi_value DecodeArray(InstanceData *instance, const uint8_t *origin, const TypeInfo *type, uint32_t len)
|
|
551
|
+
{
|
|
552
|
+
K_ASSERT(type->primitive == PrimitiveKind::Array || type->primitive == PrimitiveKind::Pointer);
|
|
553
|
+
|
|
554
|
+
Napi::Env env = instance->env;
|
|
555
|
+
const TypeInfo *ref = type->ref.type;
|
|
556
|
+
int32_t stride = type->ref.stride;
|
|
557
|
+
|
|
558
|
+
if (type->hint == ArrayHint::Typed) {
|
|
559
|
+
#define POP_TYPEDARRAY(TypedArrayType, CType) \
|
|
560
|
+
do { \
|
|
561
|
+
napi_value buffer = nullptr; \
|
|
562
|
+
napi_value array = nullptr; \
|
|
563
|
+
void *data; \
|
|
564
|
+
\
|
|
565
|
+
NAPI_OK(napi_create_arraybuffer(env, (size_t)len * K_SIZE(CType), &data, &buffer)); \
|
|
566
|
+
NAPI_OK(napi_create_typedarray(env, (TypedArrayType), (size_t)len, buffer, 0, &array)); \
|
|
567
|
+
\
|
|
568
|
+
Span<uint8_t> view = MakeSpan((uint8_t *)data, (Size)len * K_SIZE(CType)); \
|
|
569
|
+
DecodeBuffer(view, origin, type); \
|
|
570
|
+
\
|
|
571
|
+
return array; \
|
|
572
|
+
} while (false)
|
|
573
|
+
|
|
574
|
+
switch (ref->primitive) {
|
|
575
|
+
case PrimitiveKind::Int8: { POP_TYPEDARRAY(napi_int8_array, int8_t); } break;
|
|
576
|
+
case PrimitiveKind::UInt8: { POP_TYPEDARRAY(napi_uint8_array, uint8_t); } break;
|
|
577
|
+
case PrimitiveKind::Int16: { POP_TYPEDARRAY(napi_int16_array, int16_t); } break;
|
|
578
|
+
case PrimitiveKind::Int16S: { POP_TYPEDARRAY(napi_int16_array, int16_t); } break;
|
|
579
|
+
case PrimitiveKind::UInt16: { POP_TYPEDARRAY(napi_uint16_array, uint16_t); } break;
|
|
580
|
+
case PrimitiveKind::UInt16S: { POP_TYPEDARRAY(napi_uint16_array, uint16_t); } break;
|
|
581
|
+
case PrimitiveKind::Int32: { POP_TYPEDARRAY(napi_int32_array, int32_t); } break;
|
|
582
|
+
case PrimitiveKind::Int32S: { POP_TYPEDARRAY(napi_int32_array, int32_t); } break;
|
|
583
|
+
case PrimitiveKind::UInt32: { POP_TYPEDARRAY(napi_uint32_array, uint32_t); } break;
|
|
584
|
+
case PrimitiveKind::UInt32S: { POP_TYPEDARRAY(napi_uint32_array, uint32_t); } break;
|
|
585
|
+
case PrimitiveKind::Float32: { POP_TYPEDARRAY(napi_float32_array, float); } break;
|
|
586
|
+
case PrimitiveKind::Float64: { POP_TYPEDARRAY(napi_float64_array, double); } break;
|
|
587
|
+
|
|
588
|
+
case PrimitiveKind::Void:
|
|
589
|
+
case PrimitiveKind::Bool:
|
|
590
|
+
case PrimitiveKind::Int64:
|
|
591
|
+
case PrimitiveKind::Int64S:
|
|
592
|
+
case PrimitiveKind::UInt64:
|
|
593
|
+
case PrimitiveKind::UInt64S:
|
|
594
|
+
case PrimitiveKind::String:
|
|
595
|
+
case PrimitiveKind::String16:
|
|
596
|
+
case PrimitiveKind::String32:
|
|
597
|
+
case PrimitiveKind::Pointer:
|
|
598
|
+
case PrimitiveKind::Callback:
|
|
599
|
+
case PrimitiveKind::Record:
|
|
600
|
+
case PrimitiveKind::Union:
|
|
601
|
+
case PrimitiveKind::Array:
|
|
602
|
+
case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
#undef POP_TYPEDARRAY
|
|
606
|
+
} else if (type->hint == ArrayHint::Buffer) {
|
|
607
|
+
napi_value buffer;
|
|
608
|
+
void *data;
|
|
609
|
+
|
|
610
|
+
NAPI_OK(napi_create_buffer(env, (size_t)len * ref->size, &data, &buffer));
|
|
611
|
+
|
|
612
|
+
Span<uint8_t> view = MakeSpan((uint8_t *)data, (Size)len * ref->size);
|
|
613
|
+
DecodeBuffer(view, origin, type);
|
|
614
|
+
|
|
615
|
+
return buffer;
|
|
616
|
+
} else if (type->hint == ArrayHint::String) {
|
|
617
|
+
K_ASSERT(stride == ref->size);
|
|
618
|
+
|
|
619
|
+
switch (ref->primitive) {
|
|
620
|
+
case PrimitiveKind::Int8: {
|
|
621
|
+
const char *ptr = (const char *)origin;
|
|
622
|
+
size_t count = strnlen(ptr, (size_t)len);
|
|
623
|
+
|
|
624
|
+
return NewString(env, ptr, count);
|
|
625
|
+
} break;
|
|
626
|
+
case PrimitiveKind::Int16: {
|
|
627
|
+
const char16_t *ptr = (const char16_t *)origin;
|
|
628
|
+
Size count = NullTerminatedLength(ptr, len);
|
|
629
|
+
|
|
630
|
+
return NewString(env, ptr, count);
|
|
631
|
+
} break;
|
|
632
|
+
case PrimitiveKind::Int32: {
|
|
633
|
+
const char32_t *ptr = (const char32_t *)origin;
|
|
634
|
+
Size count = NullTerminatedLength(ptr, len);
|
|
635
|
+
|
|
636
|
+
return NewString(env, ptr, count);
|
|
637
|
+
} break;
|
|
638
|
+
|
|
639
|
+
case PrimitiveKind::Void:
|
|
640
|
+
case PrimitiveKind::Bool:
|
|
641
|
+
case PrimitiveKind::UInt8:
|
|
642
|
+
case PrimitiveKind::Int16S:
|
|
643
|
+
case PrimitiveKind::UInt16:
|
|
644
|
+
case PrimitiveKind::UInt16S:
|
|
645
|
+
case PrimitiveKind::Int32S:
|
|
646
|
+
case PrimitiveKind::UInt32:
|
|
647
|
+
case PrimitiveKind::UInt32S:
|
|
648
|
+
case PrimitiveKind::Int64:
|
|
649
|
+
case PrimitiveKind::Int64S:
|
|
650
|
+
case PrimitiveKind::UInt64:
|
|
651
|
+
case PrimitiveKind::UInt64S:
|
|
652
|
+
case PrimitiveKind::String:
|
|
653
|
+
case PrimitiveKind::String16:
|
|
654
|
+
case PrimitiveKind::String32:
|
|
655
|
+
case PrimitiveKind::Pointer:
|
|
656
|
+
case PrimitiveKind::Callback:
|
|
657
|
+
case PrimitiveKind::Record:
|
|
658
|
+
case PrimitiveKind::Union:
|
|
659
|
+
case PrimitiveKind::Array:
|
|
660
|
+
case PrimitiveKind::Float32:
|
|
661
|
+
case PrimitiveKind::Float64:
|
|
662
|
+
case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
|
|
663
|
+
}
|
|
664
|
+
} else {
|
|
665
|
+
K_ASSERT(type->hint == ArrayHint::Array);
|
|
666
|
+
|
|
667
|
+
Napi::Array array = Napi::Array::New(env);
|
|
668
|
+
DecodeElements(instance, array, origin, type, len);
|
|
669
|
+
|
|
670
|
+
return array;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
K_UNREACHABLE();
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
void DecodeElements(InstanceData *instance, napi_value array, const uint8_t *origin, const TypeInfo *type, uint32_t len)
|
|
677
|
+
{
|
|
678
|
+
K_ASSERT(IsArray(instance->env, array));
|
|
679
|
+
|
|
680
|
+
Napi::Env env = instance->env;
|
|
681
|
+
const TypeInfo *ref = type->ref.type;
|
|
682
|
+
int32_t stride = type->ref.stride;
|
|
683
|
+
|
|
684
|
+
Size offset = 0;
|
|
685
|
+
|
|
686
|
+
#define POP_ARRAY(SetCode) \
|
|
687
|
+
do { \
|
|
688
|
+
for (uint32_t i = 0; i < len; i++) { \
|
|
689
|
+
const uint8_t *src = origin + offset; \
|
|
690
|
+
SetCode \
|
|
691
|
+
offset += stride; \
|
|
692
|
+
} \
|
|
693
|
+
} while (false)
|
|
694
|
+
|
|
695
|
+
#define POP_INTEGERS(CType) \
|
|
696
|
+
do { \
|
|
697
|
+
POP_ARRAY({ \
|
|
698
|
+
CType v = *(CType *)src; \
|
|
699
|
+
napi_set_element(env, array, i, NewInt(env, v)); \
|
|
700
|
+
}); \
|
|
701
|
+
} while (false)
|
|
702
|
+
#define POP_INTEGERS_SWAP(CType) \
|
|
703
|
+
do { \
|
|
704
|
+
POP_ARRAY({ \
|
|
705
|
+
CType v = *(CType *)src; \
|
|
706
|
+
napi_set_element(env, array, i, NewInt(env, ReverseBytes(v))); \
|
|
707
|
+
}); \
|
|
708
|
+
} while (false)
|
|
709
|
+
|
|
710
|
+
switch (ref->primitive) {
|
|
711
|
+
case PrimitiveKind::Void: { K_UNREACHABLE(); } break;
|
|
712
|
+
|
|
713
|
+
case PrimitiveKind::Bool: {
|
|
714
|
+
POP_ARRAY({
|
|
715
|
+
bool b = *(bool *)src;
|
|
716
|
+
napi_set_element(env, array, i, Napi::Boolean::New(env, b));
|
|
717
|
+
});
|
|
718
|
+
} break;
|
|
719
|
+
case PrimitiveKind::Int8: { POP_INTEGERS(int8_t); } break;
|
|
720
|
+
case PrimitiveKind::UInt8: { POP_INTEGERS(uint8_t); } break;
|
|
721
|
+
case PrimitiveKind::Int16: { POP_INTEGERS(int16_t); } break;
|
|
722
|
+
case PrimitiveKind::Int16S: { POP_INTEGERS_SWAP(int16_t); } break;
|
|
723
|
+
case PrimitiveKind::UInt16: { POP_INTEGERS(uint16_t); } break;
|
|
724
|
+
case PrimitiveKind::UInt16S: { POP_INTEGERS_SWAP(uint16_t); } break;
|
|
725
|
+
case PrimitiveKind::Int32: { POP_INTEGERS(int32_t); } break;
|
|
726
|
+
case PrimitiveKind::Int32S: { POP_INTEGERS_SWAP(int32_t); } break;
|
|
727
|
+
case PrimitiveKind::UInt32: { POP_INTEGERS(uint32_t); } break;
|
|
728
|
+
case PrimitiveKind::UInt32S: { POP_INTEGERS_SWAP(uint32_t); } break;
|
|
729
|
+
case PrimitiveKind::Int64: { POP_INTEGERS(int64_t); } break;
|
|
730
|
+
case PrimitiveKind::Int64S: { POP_INTEGERS_SWAP(int64_t); } break;
|
|
731
|
+
case PrimitiveKind::UInt64: { POP_INTEGERS(uint64_t); } break;
|
|
732
|
+
case PrimitiveKind::UInt64S: { POP_INTEGERS_SWAP(uint64_t); } break;
|
|
733
|
+
case PrimitiveKind::String: {
|
|
734
|
+
POP_ARRAY({
|
|
735
|
+
const char *str = *(const char **)src;
|
|
736
|
+
napi_set_element(env, array, i, NewString(env, str));
|
|
737
|
+
|
|
738
|
+
if (ref->dispose) {
|
|
739
|
+
ref->dispose(instance, ref, str);
|
|
740
|
+
}
|
|
741
|
+
});
|
|
742
|
+
} break;
|
|
743
|
+
case PrimitiveKind::String16: {
|
|
744
|
+
POP_ARRAY({
|
|
745
|
+
const char16_t *str16 = *(const char16_t **)src;
|
|
746
|
+
napi_set_element(env, array, i, NewString(env, str16));
|
|
747
|
+
|
|
748
|
+
if (ref->dispose) {
|
|
749
|
+
ref->dispose(instance, ref, str16);
|
|
750
|
+
}
|
|
751
|
+
});
|
|
752
|
+
} break;
|
|
753
|
+
case PrimitiveKind::String32: {
|
|
754
|
+
POP_ARRAY({
|
|
755
|
+
const char32_t *str32 = *(const char32_t **)src;
|
|
756
|
+
napi_set_element(env, array, i, NewString(env, str32));
|
|
757
|
+
|
|
758
|
+
if (ref->dispose) {
|
|
759
|
+
ref->dispose(instance, ref, str32);
|
|
760
|
+
}
|
|
761
|
+
});
|
|
762
|
+
} break;
|
|
763
|
+
case PrimitiveKind::Pointer: {
|
|
764
|
+
POP_ARRAY({
|
|
765
|
+
void *ptr2 = *(void **)src;
|
|
766
|
+
|
|
767
|
+
napi_value p = WrapPointer(env, ref->ref.type, ptr2);
|
|
768
|
+
napi_set_element(env, array, i, p);
|
|
769
|
+
|
|
770
|
+
if (ref->dispose) {
|
|
771
|
+
ref->dispose(instance, ref, ptr2);
|
|
772
|
+
}
|
|
773
|
+
});
|
|
774
|
+
} break;
|
|
775
|
+
case PrimitiveKind::Callback: {
|
|
776
|
+
POP_ARRAY({
|
|
777
|
+
void *ptr2 = *(void **)src;
|
|
778
|
+
|
|
779
|
+
napi_value p = WrapPointer(env, ref->ref.type, ptr2);
|
|
780
|
+
napi_set_element(env, array, i, p);
|
|
781
|
+
|
|
782
|
+
if (ref->dispose) {
|
|
783
|
+
ref->dispose(instance, ref, ptr2);
|
|
784
|
+
}
|
|
785
|
+
});
|
|
786
|
+
} break;
|
|
787
|
+
case PrimitiveKind::Record:
|
|
788
|
+
case PrimitiveKind::Union: {
|
|
789
|
+
POP_ARRAY({
|
|
790
|
+
napi_value obj = DecodeObject(instance, src, ref);
|
|
791
|
+
napi_set_element(env, array, i, obj);
|
|
792
|
+
});
|
|
793
|
+
} break;
|
|
794
|
+
case PrimitiveKind::Array: {
|
|
795
|
+
POP_ARRAY({
|
|
796
|
+
napi_value value = DecodeArray(instance, src, ref);
|
|
797
|
+
napi_set_element(env, array, i, value);
|
|
798
|
+
});
|
|
799
|
+
} break;
|
|
800
|
+
case PrimitiveKind::Float32: {
|
|
801
|
+
POP_ARRAY({
|
|
802
|
+
float f;
|
|
803
|
+
memcpy(&f, src, 4);
|
|
804
|
+
|
|
805
|
+
napi_set_element(env, array, i, NewFloat(env, f));
|
|
806
|
+
});
|
|
807
|
+
} break;
|
|
808
|
+
case PrimitiveKind::Float64: {
|
|
809
|
+
POP_ARRAY({
|
|
810
|
+
double d;
|
|
811
|
+
memcpy(&d, src, 8);
|
|
812
|
+
|
|
813
|
+
napi_set_element(env, array, i, NewFloat(env, d));
|
|
814
|
+
});
|
|
815
|
+
} break;
|
|
816
|
+
|
|
817
|
+
case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
#undef POP_INTEGERS_SWAP
|
|
821
|
+
#undef POP_INTEGERS
|
|
822
|
+
|
|
823
|
+
#undef POP_ARRAY
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
void DecodeBuffer(Span<uint8_t> buffer, const uint8_t *origin, const TypeInfo *type)
|
|
827
|
+
{
|
|
828
|
+
const TypeInfo *ref = type->ref.type;
|
|
829
|
+
int32_t stride = type->ref.stride;
|
|
830
|
+
|
|
831
|
+
// Go fast if possible. Brrrrr!
|
|
832
|
+
if (stride == ref->size) {
|
|
833
|
+
MemCpy(buffer.ptr, origin, (size_t)buffer.len);
|
|
834
|
+
} else {
|
|
835
|
+
Size len = buffer.len / ref->size;
|
|
836
|
+
|
|
837
|
+
for (Size i = 0; i < len; i++) {
|
|
838
|
+
const uint8_t *src = origin + i * stride;
|
|
839
|
+
uint8_t *dest = buffer.ptr + i * ref->size;
|
|
840
|
+
|
|
841
|
+
memcpy(dest, src, ref->size);
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
#define SWAP(CType) \
|
|
846
|
+
do { \
|
|
847
|
+
CType *data = (CType *)buffer.ptr; \
|
|
848
|
+
Size len = buffer.len / K_SIZE(CType); \
|
|
849
|
+
\
|
|
850
|
+
for (Size i = 0; i < len; i++) { \
|
|
851
|
+
data[i] = ReverseBytes(data[i]); \
|
|
852
|
+
} \
|
|
853
|
+
} while (false)
|
|
854
|
+
|
|
855
|
+
if (ref->primitive == PrimitiveKind::Int16S || ref->primitive == PrimitiveKind::UInt16S) {
|
|
856
|
+
SWAP(uint16_t);
|
|
857
|
+
} else if (ref->primitive == PrimitiveKind::Int32S || ref->primitive == PrimitiveKind::UInt32S) {
|
|
858
|
+
SWAP(uint32_t);
|
|
859
|
+
} else if (ref->primitive == PrimitiveKind::Int64S || ref->primitive == PrimitiveKind::UInt64S) {
|
|
860
|
+
SWAP(uint64_t);
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
#undef SWAP
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
napi_value Decode(InstanceData *instance, const uint8_t *ptr, const TypeInfo *type)
|
|
867
|
+
{
|
|
868
|
+
Napi::Env env = instance->env;
|
|
869
|
+
|
|
870
|
+
#define RETURN_INT(Type, NewCall) \
|
|
871
|
+
do { \
|
|
872
|
+
Type v = *(Type *)ptr; \
|
|
873
|
+
return NewCall(env, v); \
|
|
874
|
+
} while (false)
|
|
875
|
+
#define RETURN_INT_SWAP(Type, NewCall) \
|
|
876
|
+
do { \
|
|
877
|
+
Type v = ReverseBytes(*(Type *)ptr); \
|
|
878
|
+
return NewCall(env, v); \
|
|
879
|
+
} while (false)
|
|
880
|
+
|
|
881
|
+
switch (type->primitive) {
|
|
882
|
+
case PrimitiveKind::Void: {
|
|
883
|
+
ThrowError<Napi::TypeError>(env, "Cannot decode value of type %1", type->name);
|
|
884
|
+
return env.Null();
|
|
885
|
+
} break;
|
|
886
|
+
|
|
887
|
+
case PrimitiveKind::Bool: {
|
|
888
|
+
bool v = *(bool *)ptr;
|
|
889
|
+
return Napi::Boolean::New(env, v);
|
|
890
|
+
} break;
|
|
891
|
+
case PrimitiveKind::Int8: { RETURN_INT(int8_t, NewInt); } break;
|
|
892
|
+
case PrimitiveKind::UInt8: { RETURN_INT(uint8_t, NewInt); } break;
|
|
893
|
+
case PrimitiveKind::Int16: { RETURN_INT(int16_t, NewInt); } break;
|
|
894
|
+
case PrimitiveKind::Int16S: { RETURN_INT_SWAP(int16_t, NewInt); } break;
|
|
895
|
+
case PrimitiveKind::UInt16: { RETURN_INT(uint16_t, NewInt); } break;
|
|
896
|
+
case PrimitiveKind::UInt16S: { RETURN_INT_SWAP(uint16_t, NewInt); } break;
|
|
897
|
+
case PrimitiveKind::Int32: { RETURN_INT(int32_t, NewInt); } break;
|
|
898
|
+
case PrimitiveKind::Int32S: { RETURN_INT_SWAP(int32_t, NewInt); } break;
|
|
899
|
+
case PrimitiveKind::UInt32: { RETURN_INT(uint32_t, NewInt); } break;
|
|
900
|
+
case PrimitiveKind::UInt32S: { RETURN_INT_SWAP(uint32_t, NewInt); } break;
|
|
901
|
+
case PrimitiveKind::Int64: { RETURN_INT(int64_t, NewInt); } break;
|
|
902
|
+
case PrimitiveKind::Int64S: { RETURN_INT_SWAP(int64_t, NewInt); } break;
|
|
903
|
+
case PrimitiveKind::UInt64: { RETURN_INT(uint64_t, NewInt); } break;
|
|
904
|
+
case PrimitiveKind::UInt64S: { RETURN_INT_SWAP(uint64_t, NewInt); } break;
|
|
905
|
+
case PrimitiveKind::String: {
|
|
906
|
+
const char *str = *(const char **)ptr;
|
|
907
|
+
return NewString(env, str);
|
|
908
|
+
} break;
|
|
909
|
+
case PrimitiveKind::String16: {
|
|
910
|
+
const char16_t *str16 = *(const char16_t **)ptr;
|
|
911
|
+
return NewString(env, str16);
|
|
912
|
+
} break;
|
|
913
|
+
case PrimitiveKind::String32: {
|
|
914
|
+
const char32_t *str32 = *(const char32_t **)ptr;
|
|
915
|
+
return NewString(env, str32);
|
|
916
|
+
} break;
|
|
917
|
+
case PrimitiveKind::Pointer: {
|
|
918
|
+
void *ptr2 = *(void **)ptr;
|
|
919
|
+
return WrapPointer(env, type->ref.type, ptr2);
|
|
920
|
+
} break;
|
|
921
|
+
case PrimitiveKind::Callback: {
|
|
922
|
+
void *ptr2 = *(void **)ptr;
|
|
923
|
+
return WrapPointer(env, type->ref.type, ptr2);
|
|
924
|
+
} break;
|
|
925
|
+
case PrimitiveKind::Record:
|
|
926
|
+
case PrimitiveKind::Union: { return DecodeObject(instance, ptr, type); } break;
|
|
927
|
+
case PrimitiveKind::Array: {
|
|
928
|
+
napi_value array = DecodeArray(instance, ptr, type);
|
|
929
|
+
return array;
|
|
930
|
+
} break;
|
|
931
|
+
case PrimitiveKind::Float32: {
|
|
932
|
+
float f;
|
|
933
|
+
memcpy(&f, ptr, 4);
|
|
934
|
+
|
|
935
|
+
return NewFloat(env, f);
|
|
936
|
+
} break;
|
|
937
|
+
case PrimitiveKind::Float64: {
|
|
938
|
+
double d;
|
|
939
|
+
memcpy(&d, ptr, 8);
|
|
940
|
+
|
|
941
|
+
return NewFloat(env, d);
|
|
942
|
+
} break;
|
|
943
|
+
|
|
944
|
+
case PrimitiveKind::Prototype: {
|
|
945
|
+
const FunctionInfo *proto = type->proto;
|
|
946
|
+
K_ASSERT(!proto->variadic);
|
|
947
|
+
K_ASSERT(!proto->lib);
|
|
948
|
+
|
|
949
|
+
FunctionInfo *func = new FunctionInfo();
|
|
950
|
+
K_DEFER { func->Unref(); };
|
|
951
|
+
|
|
952
|
+
memcpy((void *)func, proto, K_SIZE(*proto));
|
|
953
|
+
memset((void *)&func->parameters, 0, K_SIZE(func->parameters));
|
|
954
|
+
memset((void *)&func->sync, 0, K_SIZE(func->sync));
|
|
955
|
+
memset((void *)&func->async, 0, K_SIZE(func->async));
|
|
956
|
+
|
|
957
|
+
func->name = "<anonymous>";
|
|
958
|
+
func->native = (void *)ptr;
|
|
959
|
+
func->parameters = proto->parameters;
|
|
960
|
+
func->sync = proto->sync;
|
|
961
|
+
func->async = proto->async;
|
|
962
|
+
|
|
963
|
+
return WrapFunction(instance, func);
|
|
964
|
+
} break;
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
#undef RETURN_BIGINT
|
|
968
|
+
#undef RETURN_INT
|
|
969
|
+
|
|
970
|
+
return env.Null();
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
bool DetectCallConvention(Span<const char> name, CallConvention *out_convention)
|
|
974
|
+
{
|
|
975
|
+
if (name == "__cdecl") {
|
|
976
|
+
*out_convention = CallConvention::Cdecl;
|
|
977
|
+
return true;
|
|
978
|
+
} else if (name == "__stdcall") {
|
|
979
|
+
*out_convention = CallConvention::Stdcall;
|
|
980
|
+
return true;
|
|
981
|
+
} else if (name == "__fastcall") {
|
|
982
|
+
*out_convention = CallConvention::Fastcall;
|
|
983
|
+
return true;
|
|
984
|
+
} else if (name == "__thiscall") {
|
|
985
|
+
*out_convention = CallConvention::Thiscall;
|
|
986
|
+
return true;
|
|
987
|
+
} else {
|
|
988
|
+
return false;
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
static int AnalyseFlatRec(const TypeInfo *type, int offset, int count, FunctionRef<void(const TypeInfo *type, int offset, int count)> func)
|
|
993
|
+
{
|
|
994
|
+
if (type->primitive == PrimitiveKind::Record) {
|
|
995
|
+
for (int i = 0; i < count; i++) {
|
|
996
|
+
for (const RecordMember &member: type->members) {
|
|
997
|
+
offset = AnalyseFlatRec(member.type, offset, 1, func);
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
} else if (type->primitive == PrimitiveKind::Union) {
|
|
1001
|
+
for (int i = 0; i < count; i++) {
|
|
1002
|
+
for (const RecordMember &member: type->members) {
|
|
1003
|
+
AnalyseFlatRec(member.type, offset, 1, func);
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
offset += count;
|
|
1007
|
+
} else if (type->primitive == PrimitiveKind::Array) {
|
|
1008
|
+
count *= type->size / type->ref.type->size;
|
|
1009
|
+
offset = AnalyseFlatRec(type->ref.type, offset, count, func);
|
|
1010
|
+
} else {
|
|
1011
|
+
func(type, offset, count);
|
|
1012
|
+
offset += count;
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
return offset;
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
int AnalyseFlat(const TypeInfo *type, FunctionRef<void(const TypeInfo *type, int offset, int count)> func)
|
|
1019
|
+
{
|
|
1020
|
+
return AnalyseFlatRec(type, 0, 1, func);
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
void DumpMemory(const char *type, Span<const uint8_t> bytes)
|
|
1024
|
+
{
|
|
1025
|
+
if (bytes.len) {
|
|
1026
|
+
PrintLn(StdErr, "%1 at 0x%2 (%3):", type, bytes.ptr, FmtMemSize(bytes.len));
|
|
1027
|
+
|
|
1028
|
+
for (const uint8_t *ptr = bytes.begin(); ptr < bytes.end();) {
|
|
1029
|
+
Print(StdErr, " [0x%1 %2 %3] ", FmtHex((uintptr_t)ptr, 16),
|
|
1030
|
+
FmtInt((ptr - bytes.begin()) / sizeof(void *), 4, ' '),
|
|
1031
|
+
FmtInt(ptr - bytes.begin(), 4, ' '));
|
|
1032
|
+
for (int i = 0; ptr < bytes.end() && i < (int)sizeof(void *); i++, ptr++) {
|
|
1033
|
+
Print(StdErr, " %1", FmtHex(*ptr, 2));
|
|
1034
|
+
}
|
|
1035
|
+
PrintLn(StdErr);
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
}
|