@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,493 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// SPDX-FileCopyrightText: 2026 Niels Martignène <niels.martignene@protonmail.com>
|
|
3
|
+
|
|
4
|
+
#pragma once
|
|
5
|
+
|
|
6
|
+
#include "lib/native/base/base.hh"
|
|
7
|
+
#include "ffi.hh"
|
|
8
|
+
|
|
9
|
+
#include <napi.h>
|
|
10
|
+
|
|
11
|
+
namespace K {
|
|
12
|
+
|
|
13
|
+
class UnionValue: public Napi::ObjectWrap<UnionValue> {
|
|
14
|
+
InstanceData *instance;
|
|
15
|
+
const TypeInfo *type;
|
|
16
|
+
|
|
17
|
+
Size active_idx = -1;
|
|
18
|
+
|
|
19
|
+
HeapArray<uint8_t> raw;
|
|
20
|
+
|
|
21
|
+
public:
|
|
22
|
+
static Napi::Function InitClass(InstanceData *instance, const TypeInfo *type);
|
|
23
|
+
|
|
24
|
+
UnionValue(const Napi::CallbackInfo &info);
|
|
25
|
+
|
|
26
|
+
void Finalize(Napi::BasicEnv env) override;
|
|
27
|
+
|
|
28
|
+
const TypeInfo *GetType() { return type; }
|
|
29
|
+
const RecordMember *GetMember() const { return (active_idx >= 0) ? &type->members[active_idx] : nullptr; }
|
|
30
|
+
|
|
31
|
+
void SetRaw(const uint8_t *ptr);
|
|
32
|
+
const uint8_t *GetRaw() const { return raw.ptr; }
|
|
33
|
+
|
|
34
|
+
private:
|
|
35
|
+
Napi::Value Getter(const Napi::CallbackInfo &info);
|
|
36
|
+
void Setter(const Napi::CallbackInfo &info, const Napi::Value &value);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
template <typename T, typename... Args>
|
|
40
|
+
void ThrowError(Napi::Env env, const char *msg, Args... args)
|
|
41
|
+
{
|
|
42
|
+
char buf[1024];
|
|
43
|
+
Fmt(buf, msg, args...);
|
|
44
|
+
|
|
45
|
+
auto err = T::New(env, buf);
|
|
46
|
+
err.ThrowAsJavaScriptException();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
static FORCE_INLINE napi_valuetype GetKindOf(napi_env env, napi_value value)
|
|
50
|
+
{
|
|
51
|
+
napi_valuetype kind = napi_undefined;
|
|
52
|
+
NAPI_OK(napi_typeof(env, value, &kind));
|
|
53
|
+
|
|
54
|
+
return kind;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
void SetValueTag(napi_env env, napi_value value, const void *marker);
|
|
58
|
+
bool CheckValueTag(napi_env env, napi_value value, const void *marker);
|
|
59
|
+
|
|
60
|
+
static FORCE_INLINE bool IsNullOrUndefined(napi_valuetype kind)
|
|
61
|
+
{
|
|
62
|
+
return kind == napi_null || kind == napi_undefined;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static FORCE_INLINE bool IsNullOrUndefined(napi_env env, napi_value value)
|
|
66
|
+
{
|
|
67
|
+
napi_valuetype kind = GetKindOf(env, value);
|
|
68
|
+
return IsNullOrUndefined(kind);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
static FORCE_INLINE bool IsArray(napi_env env, napi_value value)
|
|
72
|
+
{
|
|
73
|
+
bool array = false;
|
|
74
|
+
napi_is_array(env, value, &array);
|
|
75
|
+
|
|
76
|
+
return array;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static FORCE_INLINE bool IsObject(napi_env env, napi_value value)
|
|
80
|
+
{
|
|
81
|
+
if (GetKindOf(env, value) != napi_object)
|
|
82
|
+
return false;
|
|
83
|
+
if (IsArray(env, value))
|
|
84
|
+
return false;
|
|
85
|
+
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
static FORCE_INLINE bool IsTypedArray(napi_env env, napi_value value)
|
|
90
|
+
{
|
|
91
|
+
bool typedarray = false;
|
|
92
|
+
napi_is_typedarray(env, value, &typedarray);
|
|
93
|
+
return typedarray;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
static FORCE_INLINE bool IsArrayBuffer(napi_env env, napi_value value)
|
|
97
|
+
{
|
|
98
|
+
bool arraybuffer = false;
|
|
99
|
+
napi_is_arraybuffer(env, value, &arraybuffer);
|
|
100
|
+
return arraybuffer;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
static FORCE_INLINE bool IsBuffer(napi_env env, napi_value value)
|
|
104
|
+
{
|
|
105
|
+
bool buffer = false;
|
|
106
|
+
napi_is_buffer(env, value, &buffer);
|
|
107
|
+
return buffer;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
static FORCE_INLINE napi_value GetReferenceValue(napi_env env, napi_ref ref)
|
|
111
|
+
{
|
|
112
|
+
napi_value value;
|
|
113
|
+
NAPI_OK(napi_get_reference_value(env, ref, &value));
|
|
114
|
+
|
|
115
|
+
return value;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
static FORCE_INLINE uint32_t GetArrayLength(napi_env env, napi_value array)
|
|
119
|
+
{
|
|
120
|
+
uint32_t length = 0;
|
|
121
|
+
NAPI_OK(napi_get_array_length(env, array, &length));
|
|
122
|
+
|
|
123
|
+
return length;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
template <typename T>
|
|
127
|
+
static FORCE_INLINE bool TryNumber(napi_env env, napi_value value, T *out_value)
|
|
128
|
+
{
|
|
129
|
+
T v;
|
|
130
|
+
napi_status status;
|
|
131
|
+
|
|
132
|
+
// Assume number first
|
|
133
|
+
if constexpr (std::is_same_v<T, double>) {
|
|
134
|
+
status = napi_get_value_double(env, value, &v);
|
|
135
|
+
} else if constexpr (std::is_same_v<T, float>) {
|
|
136
|
+
double d;
|
|
137
|
+
status = napi_get_value_double(env, value, &d);
|
|
138
|
+
v = (float)d;
|
|
139
|
+
} else {
|
|
140
|
+
int64_t i64;
|
|
141
|
+
status = napi_get_value_int64(env, value, &i64);
|
|
142
|
+
v = (T)i64;
|
|
143
|
+
}
|
|
144
|
+
if (status == napi_ok) [[likely]] {
|
|
145
|
+
*out_value = v;
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Maybe a BigInt?
|
|
150
|
+
if constexpr (std::is_signed_v<T>) {
|
|
151
|
+
int64_t i64;
|
|
152
|
+
bool lossless;
|
|
153
|
+
status = napi_get_value_bigint_int64(env, value, &i64, &lossless);
|
|
154
|
+
v = (T)i64;
|
|
155
|
+
} else {
|
|
156
|
+
uint64_t u64;
|
|
157
|
+
bool lossless;
|
|
158
|
+
status = napi_get_value_bigint_uint64(env, value, &u64, &lossless);
|
|
159
|
+
v = (T)u64;
|
|
160
|
+
}
|
|
161
|
+
if (status == napi_ok) {
|
|
162
|
+
*out_value = v;
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
static FORCE_INLINE bool TryPointer(napi_env env, napi_value value, void **out_ptr)
|
|
170
|
+
{
|
|
171
|
+
// Fast path for BigInt
|
|
172
|
+
{
|
|
173
|
+
uint64_t u64;
|
|
174
|
+
bool lossless;
|
|
175
|
+
napi_status status = napi_get_value_bigint_uint64(env, value, &u64, &lossless);
|
|
176
|
+
|
|
177
|
+
if (status == napi_ok) {
|
|
178
|
+
*out_ptr = (void *)(uintptr_t)u64;
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (node_api_get_buffer_info(env, value, out_ptr, nullptr) == napi_ok)
|
|
184
|
+
return true;
|
|
185
|
+
|
|
186
|
+
napi_valuetype kind = GetKindOf(env, value);
|
|
187
|
+
|
|
188
|
+
if (IsNullOrUndefined(kind)) {
|
|
189
|
+
*out_ptr = nullptr;
|
|
190
|
+
return true;
|
|
191
|
+
} else if (kind == napi_number) {
|
|
192
|
+
int64_t i;
|
|
193
|
+
napi_status status = napi_get_value_int64(env, value, &i);
|
|
194
|
+
K_ASSERT(status == napi_ok);
|
|
195
|
+
|
|
196
|
+
*out_ptr = (void *)(uintptr_t)i;
|
|
197
|
+
return true;
|
|
198
|
+
#if defined(EXTERNAL_POINTERS)
|
|
199
|
+
} else if (kind == napi_external) {
|
|
200
|
+
Napi::External<void> external = Napi::External<void>(env, value);
|
|
201
|
+
|
|
202
|
+
*out_ptr = (void *)external.Data();
|
|
203
|
+
return true;
|
|
204
|
+
#endif
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (napi_get_arraybuffer_info(env, value, out_ptr, nullptr) == napi_ok)
|
|
208
|
+
return true;
|
|
209
|
+
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
static FORCE_INLINE bool TryPointer(napi_env env, napi_value value, void **out_ptr, Size *out_len)
|
|
214
|
+
{
|
|
215
|
+
// Fast path for BigInt
|
|
216
|
+
{
|
|
217
|
+
uint64_t u64;
|
|
218
|
+
bool lossless;
|
|
219
|
+
napi_status status = napi_get_value_bigint_uint64(env, value, &u64, &lossless);
|
|
220
|
+
|
|
221
|
+
if (status == napi_ok) {
|
|
222
|
+
*out_ptr = (void *)(uintptr_t)u64;
|
|
223
|
+
*out_len = -1;
|
|
224
|
+
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (size_t len = 0; node_api_get_buffer_info(env, value, out_ptr, &len) == napi_ok) {
|
|
230
|
+
*out_len = (Size)len;
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
napi_valuetype kind = GetKindOf(env, value);
|
|
235
|
+
|
|
236
|
+
if (IsNullOrUndefined(kind)) {
|
|
237
|
+
*out_ptr = nullptr;
|
|
238
|
+
*out_len = -1;
|
|
239
|
+
|
|
240
|
+
return true;
|
|
241
|
+
} else if (kind == napi_number) {
|
|
242
|
+
int64_t i;
|
|
243
|
+
napi_status status = napi_get_value_int64(env, value, &i);
|
|
244
|
+
K_ASSERT(status == napi_ok);
|
|
245
|
+
|
|
246
|
+
*out_ptr = (void *)(uintptr_t)i;
|
|
247
|
+
*out_len = -1;
|
|
248
|
+
|
|
249
|
+
return true;
|
|
250
|
+
#if defined(EXTERNAL_POINTERS)
|
|
251
|
+
} else if (kind == napi_external) {
|
|
252
|
+
Napi::External<void> external = Napi::External<void>(env, value);
|
|
253
|
+
|
|
254
|
+
*out_ptr = (void *)external.Data();
|
|
255
|
+
*out_len = -1;
|
|
256
|
+
|
|
257
|
+
return true;
|
|
258
|
+
#endif
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (size_t len = 0; napi_get_arraybuffer_info(env, value, out_ptr, &len) == napi_ok) {
|
|
262
|
+
*out_len = (Size)len;
|
|
263
|
+
return true;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
static FORCE_INLINE bool TryPointer(napi_env env, napi_value value, void **out_ptr, napi_valuetype *out_kind)
|
|
270
|
+
{
|
|
271
|
+
// Fast path for BigInt
|
|
272
|
+
{
|
|
273
|
+
uint64_t u64;
|
|
274
|
+
bool lossless;
|
|
275
|
+
napi_status status = napi_get_value_bigint_uint64(env, value, &u64, &lossless);
|
|
276
|
+
|
|
277
|
+
if (status == napi_ok) {
|
|
278
|
+
*out_ptr = (void *)(uintptr_t)u64;
|
|
279
|
+
*out_kind = napi_bigint;
|
|
280
|
+
|
|
281
|
+
return true;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (node_api_get_buffer_info(env, value, out_ptr, nullptr) == napi_ok) {
|
|
286
|
+
*out_kind = napi_object;
|
|
287
|
+
return true;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
napi_valuetype kind = GetKindOf(env, value);
|
|
291
|
+
|
|
292
|
+
if (IsNullOrUndefined(kind)) {
|
|
293
|
+
*out_ptr = nullptr;
|
|
294
|
+
*out_kind = kind;
|
|
295
|
+
|
|
296
|
+
return true;
|
|
297
|
+
} else if (kind == napi_number) {
|
|
298
|
+
int64_t i;
|
|
299
|
+
napi_status status = napi_get_value_int64(env, value, &i);
|
|
300
|
+
K_ASSERT(status == napi_ok);
|
|
301
|
+
|
|
302
|
+
*out_ptr = (void *)(uintptr_t)i;
|
|
303
|
+
*out_kind = napi_number;
|
|
304
|
+
|
|
305
|
+
return true;
|
|
306
|
+
#if defined(EXTERNAL_POINTERS)
|
|
307
|
+
} else if (kind == napi_external) {
|
|
308
|
+
Napi::External<void> external = Napi::External<void>(env, value);
|
|
309
|
+
|
|
310
|
+
*out_ptr = (void *)external.Data();
|
|
311
|
+
*out_kind = napi_external;
|
|
312
|
+
|
|
313
|
+
return true;
|
|
314
|
+
#endif
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (napi_get_arraybuffer_info(env, value, out_ptr, nullptr) == napi_ok) {
|
|
318
|
+
*out_kind = napi_object;
|
|
319
|
+
return true;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
*out_kind = kind;
|
|
323
|
+
return false;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
static FORCE_INLINE bool TryBuffer(napi_env env, napi_value value, Span<uint8_t> *out_buffer)
|
|
327
|
+
{
|
|
328
|
+
void *ptr = nullptr;
|
|
329
|
+
size_t len = 0;
|
|
330
|
+
|
|
331
|
+
if (node_api_get_buffer_info(env, value, &ptr, &len) == napi_ok) {
|
|
332
|
+
*out_buffer = MakeSpan((uint8_t *)ptr, (Size)len);
|
|
333
|
+
return true;
|
|
334
|
+
} else if (napi_get_arraybuffer_info(env, value, &ptr, &len) == napi_ok) {
|
|
335
|
+
*out_buffer = MakeSpan((uint8_t *)ptr, (Size)len);
|
|
336
|
+
return true;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
int GetTypedArrayType(const TypeInfo *type);
|
|
343
|
+
|
|
344
|
+
static FORCE_INLINE napi_value NewInt(Napi::Env env, char i) { napi_value value; napi_create_int32(env, (int32_t)i, &value); return value; }
|
|
345
|
+
static FORCE_INLINE napi_value NewInt(Napi::Env env, signed char i) { napi_value value; napi_create_int32(env, (int32_t)i, &value); return value; }
|
|
346
|
+
static FORCE_INLINE napi_value NewInt(Napi::Env env, unsigned char i) { napi_value value; napi_create_uint32(env, (uint32_t)i, &value); return value; }
|
|
347
|
+
static FORCE_INLINE napi_value NewInt(Napi::Env env, short i) { napi_value value; napi_create_int32(env, (int32_t)i, &value); return value; }
|
|
348
|
+
static FORCE_INLINE napi_value NewInt(Napi::Env env, unsigned short i) { napi_value value; napi_create_uint32(env, (uint32_t)i, &value); return value; }
|
|
349
|
+
static FORCE_INLINE napi_value NewInt(Napi::Env env, int i) { napi_value value; napi_create_int32(env, (int32_t)i, &value); return value; }
|
|
350
|
+
static FORCE_INLINE napi_value NewInt(Napi::Env env, unsigned int i) { napi_value value; napi_create_uint32(env, (uint32_t)i, &value); return value; }
|
|
351
|
+
#if LONG_MAX == INT32_MAX
|
|
352
|
+
static FORCE_INLINE napi_value NewInt(Napi::Env env, long i) { napi_value value; napi_create_int32(env, (int32_t)i, &value); return value; }
|
|
353
|
+
static FORCE_INLINE napi_value NewInt(Napi::Env env, unsigned long i) { napi_value value; napi_create_uint32(env, (uint32_t)i, &value); return value; }
|
|
354
|
+
#endif
|
|
355
|
+
|
|
356
|
+
template <typename T>
|
|
357
|
+
static FORCE_INLINE napi_value NewInt(Napi::Env env, T i)
|
|
358
|
+
{
|
|
359
|
+
static_assert(sizeof(T) == 8);
|
|
360
|
+
|
|
361
|
+
napi_value value;
|
|
362
|
+
|
|
363
|
+
if constexpr (std::is_signed_v<T>) {
|
|
364
|
+
if (i <= 9007199254740992ll && i >= -9007199254740992ll) {
|
|
365
|
+
NAPI_OK(napi_create_int64(env, (int64_t)i, &value));
|
|
366
|
+
return value;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
NAPI_OK(napi_create_bigint_int64(env, (int64_t)i, &value));
|
|
370
|
+
return value;
|
|
371
|
+
} else {
|
|
372
|
+
if (i <= 9007199254740992ull) {
|
|
373
|
+
NAPI_OK(napi_create_int64(env, (int64_t)i, &value));
|
|
374
|
+
return value;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
NAPI_OK(napi_create_bigint_uint64(env, (uint64_t)i, &value));
|
|
378
|
+
return value;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
static FORCE_INLINE napi_value NewFloat(Napi::Env env, float f) { napi_value value; napi_create_double(env, (double)f, &value); return value; }
|
|
383
|
+
static FORCE_INLINE napi_value NewFloat(Napi::Env env, double d) { napi_value value; napi_create_double(env, d, &value); return value; }
|
|
384
|
+
|
|
385
|
+
template <typename T>
|
|
386
|
+
static FORCE_INLINE Size NullTerminatedLength(const T *ptr)
|
|
387
|
+
{
|
|
388
|
+
Size len = 0;
|
|
389
|
+
while (ptr[len]) {
|
|
390
|
+
len++;
|
|
391
|
+
}
|
|
392
|
+
return len;
|
|
393
|
+
}
|
|
394
|
+
template <typename T>
|
|
395
|
+
static FORCE_INLINE Size NullTerminatedLength(const T *ptr, Size max)
|
|
396
|
+
{
|
|
397
|
+
Size len = 0;
|
|
398
|
+
while (len < max && ptr[len]) {
|
|
399
|
+
len++;
|
|
400
|
+
}
|
|
401
|
+
return len;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
static FORCE_INLINE napi_value NewString(Napi::Env env, const char *ptr, Size len)
|
|
405
|
+
{
|
|
406
|
+
napi_value value;
|
|
407
|
+
if (ptr) {
|
|
408
|
+
NAPI_OK(napi_create_string_utf8(env, ptr, (size_t)len, &value));
|
|
409
|
+
} else {
|
|
410
|
+
NAPI_OK(napi_get_null(env, &value));
|
|
411
|
+
}
|
|
412
|
+
return value;
|
|
413
|
+
}
|
|
414
|
+
static FORCE_INLINE napi_value NewString(Napi::Env env, const char *ptr)
|
|
415
|
+
{
|
|
416
|
+
napi_value value;
|
|
417
|
+
if (ptr) {
|
|
418
|
+
NAPI_OK(napi_create_string_utf8(env, ptr, NAPI_AUTO_LENGTH, &value));
|
|
419
|
+
} else {
|
|
420
|
+
NAPI_OK(napi_get_null(env, &value));
|
|
421
|
+
}
|
|
422
|
+
return value;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
static FORCE_INLINE napi_value NewString(Napi::Env env, const char16_t *ptr, Size len)
|
|
426
|
+
{
|
|
427
|
+
napi_value value;
|
|
428
|
+
if (ptr) {
|
|
429
|
+
NAPI_OK(napi_create_string_utf16(env, ptr, (size_t)len, &value));
|
|
430
|
+
} else {
|
|
431
|
+
NAPI_OK(napi_get_null(env, &value));
|
|
432
|
+
}
|
|
433
|
+
return value;
|
|
434
|
+
}
|
|
435
|
+
static FORCE_INLINE napi_value NewString(Napi::Env env, const char16_t *ptr)
|
|
436
|
+
{
|
|
437
|
+
napi_value value;
|
|
438
|
+
if (ptr) {
|
|
439
|
+
NAPI_OK(napi_create_string_utf16(env, ptr, NAPI_AUTO_LENGTH, &value));
|
|
440
|
+
} else {
|
|
441
|
+
NAPI_OK(napi_get_null(env, &value));
|
|
442
|
+
}
|
|
443
|
+
return value;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
napi_value NewString(Napi::Env env, const char32_t *ptr, Size len);
|
|
447
|
+
napi_value NewString(Napi::Env env, const char32_t *ptr);
|
|
448
|
+
|
|
449
|
+
napi_value DecodeObject(InstanceData *instance, const uint8_t *origin, const TypeInfo *type);
|
|
450
|
+
void DecodeObject(InstanceData *instance, napi_value obj, const uint8_t *origin, const TypeInfo *type);
|
|
451
|
+
|
|
452
|
+
napi_value DecodeArray(InstanceData *instance, const uint8_t *origin, const TypeInfo *type);
|
|
453
|
+
napi_value DecodeArray(InstanceData *instance, const uint8_t *origin, const TypeInfo *type, uint32_t len);
|
|
454
|
+
void DecodeElements(InstanceData *instance, napi_value array, const uint8_t *origin, const TypeInfo *type, uint32_t len);
|
|
455
|
+
INLINE_UNITY void DecodeBuffer(Span<uint8_t> buffer, const uint8_t *origin, const TypeInfo *type);
|
|
456
|
+
|
|
457
|
+
napi_value Decode(InstanceData *instance, const uint8_t *ptr, const TypeInfo *type);
|
|
458
|
+
|
|
459
|
+
static FORCE_INLINE Napi::Array GetOwnPropertyNames(napi_env env, napi_value obj)
|
|
460
|
+
{
|
|
461
|
+
K_ASSERT(IsObject(env, obj));
|
|
462
|
+
|
|
463
|
+
napi_value result;
|
|
464
|
+
napi_status status = napi_get_all_property_names(env, obj, napi_key_own_only,
|
|
465
|
+
(napi_key_filter)(napi_key_enumerable | napi_key_skip_symbols),
|
|
466
|
+
napi_key_numbers_to_strings, &result);
|
|
467
|
+
K_ASSERT(status == napi_ok);
|
|
468
|
+
|
|
469
|
+
return Napi::Array(env, result);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
static FORCE_INLINE napi_value WrapPointer(Napi::Env env, const TypeInfo *, void *ptr)
|
|
473
|
+
{
|
|
474
|
+
napi_value value;
|
|
475
|
+
|
|
476
|
+
if (ptr) {
|
|
477
|
+
#if defined(EXTERNAL_POINTERS)
|
|
478
|
+
NAPI_OK(napi_create_external(env, ptr, nullptr, nullptr, &value));
|
|
479
|
+
#else
|
|
480
|
+
NAPI_OK(napi_create_bigint_uint64(env, (uint64_t)(uintptr_t)ptr, &value));
|
|
481
|
+
#endif
|
|
482
|
+
} else {
|
|
483
|
+
NAPI_OK(napi_get_null(env, &value));
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
return value;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
int AnalyseFlat(const TypeInfo *type, FunctionRef<void(const TypeInfo *type, int offset, int count)> func);
|
|
490
|
+
|
|
491
|
+
void DumpMemory(const char *type, Span<const uint8_t> bytes);
|
|
492
|
+
|
|
493
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
|
|
3
|
+
#include "lib/native/base/base.hh"
|
|
4
|
+
#include "type.hh"
|
|
5
|
+
#include "util.hh"
|
|
6
|
+
#include "uv.hh"
|
|
7
|
+
|
|
8
|
+
#include <napi.h>
|
|
9
|
+
|
|
10
|
+
namespace K {
|
|
11
|
+
|
|
12
|
+
Napi::Function PollHandle::InitClass(InstanceData *instance)
|
|
13
|
+
{
|
|
14
|
+
Napi::Env env = instance->env;
|
|
15
|
+
|
|
16
|
+
// node-addon-api wants std::vector
|
|
17
|
+
std::vector<Napi::ClassPropertyDescriptor<PollHandle>> properties = {
|
|
18
|
+
InstanceMethod("start", &PollHandle::Start, napi_default, instance),
|
|
19
|
+
InstanceMethod("stop", &PollHandle::Stop, napi_default, instance),
|
|
20
|
+
InstanceMethod("close", &PollHandle::Close, napi_default, instance),
|
|
21
|
+
InstanceMethod("unref", &PollHandle::Unref, napi_default, instance),
|
|
22
|
+
InstanceMethod("ref", &PollHandle::Ref, napi_default, instance)
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
if (Napi::Value dispose = env.RunScript("Symbol.dispose"); !IsNullOrUndefined(env, dispose)) {
|
|
26
|
+
Napi::ClassPropertyDescriptor<PollHandle> prop = InstanceMethod(dispose.As<Napi::Symbol>(), &PollHandle::Close);
|
|
27
|
+
properties.push_back(prop);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
Napi::Function constructor = DefineClass(env, "PollHandle", properties, instance);
|
|
31
|
+
return constructor;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
PollHandle::PollHandle(const Napi::CallbackInfo &info)
|
|
35
|
+
: Napi::ObjectWrap<PollHandle>(info), env(info.Env())
|
|
36
|
+
{
|
|
37
|
+
if (info.Length() < 1 || !info[0].IsExternal()) {
|
|
38
|
+
ThrowError<Napi::Error>(env, "Poll handles cannot be constructed manually");
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
Napi::External<void> external = info[0].As<Napi::External<void>>();
|
|
43
|
+
int fd = (int)(intptr_t)external.Data();
|
|
44
|
+
|
|
45
|
+
uv_loop_t *loop = nullptr;
|
|
46
|
+
if (napi_get_uv_event_loop(env, &loop) != napi_ok || !loop) {
|
|
47
|
+
ThrowError<Napi::Error>(env, "napi_get_uv_event_loop() failed");
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// We would store it inside the class, but the definition of uv_poll_t involves windows.h...
|
|
52
|
+
// and we won't want that on Windows. Heap allocation it is!
|
|
53
|
+
// Also, it may have to outlive the object, because uv_close() is asynchronous.
|
|
54
|
+
{
|
|
55
|
+
size_t size = uv_handle_size(UV_POLL);
|
|
56
|
+
|
|
57
|
+
handle = (uv_poll_t *)AllocateRaw(nullptr, size);
|
|
58
|
+
MemSet(handle, 0, size);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (int ret = uv_poll_init_socket(loop, handle, (uv_os_sock_t)fd); ret != 0) {
|
|
62
|
+
ThrowError<Napi::Error>(env, "Failed to init UV poll: %1", uv_strerror(ret));
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
handle->data = this;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
void PollHandle::Start(const Napi::CallbackInfo &info)
|
|
70
|
+
{
|
|
71
|
+
InstanceData *instance = (InstanceData *)info.Data();
|
|
72
|
+
|
|
73
|
+
bool has_opts = (info.Length() >= 2 && info[0].IsObject());
|
|
74
|
+
|
|
75
|
+
if (info.Length() < 1u + has_opts) {
|
|
76
|
+
ThrowError<Napi::TypeError>(env, "Expected 1 to 2 arguments, got %1", info.Length());
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (!info[0u + has_opts].IsFunction()) {
|
|
80
|
+
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for callback, expected function", GetValueType(instance, info[0u + has_opts]));
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
int events = 0;
|
|
85
|
+
Napi::Function cb = info[0 + has_opts].As<Napi::Function>();
|
|
86
|
+
|
|
87
|
+
if (has_opts) {
|
|
88
|
+
Napi::Object opts = has_opts ? info[0].As<Napi::Object>() : Napi::Object::New(env);
|
|
89
|
+
|
|
90
|
+
events |= opts.Get("readable").ToBoolean() ? UV_READABLE : 0;
|
|
91
|
+
events |= opts.Get("writable").ToBoolean() ? UV_WRITABLE : 0;
|
|
92
|
+
events |= opts.Get("disconnect").ToBoolean() ? UV_DISCONNECT : 0;
|
|
93
|
+
} else {
|
|
94
|
+
events = UV_READABLE;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
callback = Napi::Persistent(cb);
|
|
98
|
+
|
|
99
|
+
if (int ret = uv_poll_start(handle, events, &PollHandle::OnPoll); ret != 0) {
|
|
100
|
+
callback.Reset();
|
|
101
|
+
ThrowError<Napi::Error>(env, "Failed to start UV poll: %1", uv_strerror(ret));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
void PollHandle::Finalize(Napi::BasicEnv env)
|
|
106
|
+
{
|
|
107
|
+
node_api_delete_reference(env, *this);
|
|
108
|
+
SuppressDestruct();
|
|
109
|
+
|
|
110
|
+
Close();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
void PollHandle::Stop(const Napi::CallbackInfo &)
|
|
114
|
+
{
|
|
115
|
+
uv_poll_stop(handle);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
void PollHandle::Close(const Napi::CallbackInfo &)
|
|
119
|
+
{
|
|
120
|
+
Close();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
void PollHandle::Ref(const Napi::CallbackInfo &)
|
|
124
|
+
{
|
|
125
|
+
uv_ref((uv_handle_t *)handle);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
void PollHandle::Unref(const Napi::CallbackInfo &)
|
|
129
|
+
{
|
|
130
|
+
uv_unref((uv_handle_t *)handle);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
void PollHandle::Close()
|
|
134
|
+
{
|
|
135
|
+
if (!handle)
|
|
136
|
+
return;
|
|
137
|
+
|
|
138
|
+
const auto release = [](uv_handle_t *ptr) {
|
|
139
|
+
uv_poll_t *handle = (uv_poll_t *)ptr;
|
|
140
|
+
ReleaseRaw(nullptr, handle, -1);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
uv_poll_stop(handle);
|
|
144
|
+
uv_close((uv_handle_t *)handle, release);
|
|
145
|
+
|
|
146
|
+
callback.Reset();
|
|
147
|
+
|
|
148
|
+
handle = nullptr;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
void PollHandle::OnPoll(uv_poll_t *h, int status, int events)
|
|
152
|
+
{
|
|
153
|
+
PollHandle *poll = (PollHandle *)h->data;
|
|
154
|
+
|
|
155
|
+
if (poll->callback.IsEmpty()) [[unlikely]]
|
|
156
|
+
return;
|
|
157
|
+
|
|
158
|
+
Napi::Env env = poll->env;
|
|
159
|
+
Napi::HandleScope scope(env);
|
|
160
|
+
|
|
161
|
+
Napi::Object obj = Napi::Object::New(env);
|
|
162
|
+
|
|
163
|
+
obj.Set("readable", !!(events & UV_READABLE));
|
|
164
|
+
obj.Set("writable", !!(events & UV_WRITABLE));
|
|
165
|
+
obj.Set("disconnect", !!(events & UV_DISCONNECT));
|
|
166
|
+
|
|
167
|
+
napi_value args[] = { NewInt(env, (int32_t)status), obj };
|
|
168
|
+
poll->callback.Call(poll->Value(), K_LEN(args), args);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
Napi::Value Poll(const Napi::CallbackInfo &info)
|
|
172
|
+
{
|
|
173
|
+
Napi::Env env = info.Env();
|
|
174
|
+
InstanceData *instance = (InstanceData *)info.Data();
|
|
175
|
+
|
|
176
|
+
bool has_opts = (info.Length() >= 3 && info[1].IsObject());
|
|
177
|
+
|
|
178
|
+
if (info.Length() < 2u + has_opts) {
|
|
179
|
+
ThrowError<Napi::TypeError>(env, "Expected 2 to 3 arguments, got %1", info.Length());
|
|
180
|
+
return env.Null();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (!info[0].IsNumber()) {
|
|
184
|
+
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for descriptor, expected number", GetValueType(instance, info[0]));
|
|
185
|
+
return env.Null();
|
|
186
|
+
}
|
|
187
|
+
if (!info[1 + has_opts].IsFunction()) {
|
|
188
|
+
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for callback, expected function", GetValueType(instance, info[1 + has_opts]));
|
|
189
|
+
return env.Null();
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
int fd = info[0].As<Napi::Number>().Int32Value();
|
|
193
|
+
|
|
194
|
+
Napi::External<void> external = Napi::External<void>::New(env, (void *)(intptr_t)fd);
|
|
195
|
+
Napi::Object inst = instance->construct_poll.New({ external });
|
|
196
|
+
Napi::Function start = inst.Get("start").As<Napi::Function>();
|
|
197
|
+
|
|
198
|
+
if (env.IsExceptionPending()) [[unlikely]]
|
|
199
|
+
return env.Null();
|
|
200
|
+
|
|
201
|
+
if (has_opts) {
|
|
202
|
+
Napi::Value opts = info[1];
|
|
203
|
+
Napi::Function cb = info[2].As<Napi::Function>();
|
|
204
|
+
|
|
205
|
+
start.Call(inst, { opts, cb });
|
|
206
|
+
} else {
|
|
207
|
+
Napi::Function cb = info[1].As<Napi::Function>();
|
|
208
|
+
start.Call(inst, { cb });
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return inst;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
}
|