koffi 3.0.1 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,53 +10,6 @@
10
10
 
11
11
  namespace K {
12
12
 
13
- #if defined(_MSC_VER)
14
- #define FORCE_INLINE __forceinline
15
- #else
16
- #define FORCE_INLINE __attribute__((always_inline)) inline
17
- #endif
18
- #if defined(UNITY_BUILD)
19
- #define INLINE_UNITY FORCE_INLINE
20
- #else
21
- #define INLINE_UNITY
22
- #endif
23
-
24
- #if defined(__GNUC__) || defined(__clang__)
25
- #if __has_attribute(musttail) && __has_attribute(preserve_none)
26
- #define MUST_TAIL __attribute__((musttail))
27
- #define PRESERVE_NONE __attribute__((preserve_none))
28
- #elif __has_attribute(musttail) && !defined(__clang__)
29
- // GCC regalloc seems better, so the generated code is not too bad even without preserve_none
30
- #define MUST_TAIL __attribute__((musttail))
31
- #define PRESERVE_NONE
32
- #endif
33
- #endif
34
-
35
- extern const napi_type_tag LibraryHandleMarker;
36
- extern const napi_type_tag TypeObjectMarker;
37
- extern const napi_type_tag DirectionMarker;
38
- extern const napi_type_tag UnionValueMarker;
39
- extern const napi_type_tag CastMarker;
40
-
41
- #if !defined(EXTERNAL_TYPES)
42
-
43
- class TypeObject: public Napi::ObjectWrap<TypeObject> {
44
- const TypeInfo *type;
45
-
46
- mutable Napi::Object members;
47
-
48
- public:
49
- static Napi::Function InitClass(Napi::Env env);
50
-
51
- TypeObject(const Napi::CallbackInfo &info);
52
-
53
- void Finalize(Napi::BasicEnv env) override;
54
-
55
- const TypeInfo *GetType() { return type; }
56
- };
57
-
58
- #endif
59
-
60
13
  class UnionValue: public Napi::ObjectWrap<UnionValue> {
61
14
  InstanceData *instance;
62
15
  const TypeInfo *type;
@@ -66,7 +19,7 @@ class UnionValue: public Napi::ObjectWrap<UnionValue> {
66
19
  HeapArray<uint8_t> raw;
67
20
 
68
21
  public:
69
- static Napi::Function InitClass(Napi::Env env, const TypeInfo *type);
22
+ static Napi::Function InitClass(InstanceData *instance, const TypeInfo *type);
70
23
 
71
24
  UnionValue(const Napi::CallbackInfo &info);
72
25
 
@@ -93,58 +46,14 @@ void ThrowError(Napi::Env env, const char *msg, Args... args)
93
46
  err.ThrowAsJavaScriptException();
94
47
  }
95
48
 
96
- static FORCE_INLINE bool IsInteger(const TypeInfo *type)
97
- {
98
- bool integer = ((int)type->primitive >= (int)PrimitiveKind::Int8 &&
99
- (int)type->primitive <= (int)PrimitiveKind::UInt64);
100
- return integer;
101
- }
102
-
103
- static FORCE_INLINE bool IsFloat(const TypeInfo *type)
104
- {
105
- bool fp = (type->primitive == PrimitiveKind::Float32 ||
106
- type->primitive == PrimitiveKind::Float64);
107
- return fp;
108
- }
109
-
110
- static FORCE_INLINE bool IsRegularSize(Size size, Size max)
111
- {
112
- bool regular = (size <= max && !(size & (size - 1)));
113
- return regular;
114
- }
115
-
116
- int ResolveDirections(Span<const char> str);
117
- const TypeInfo *ResolveType(Napi::Value value, int *out_directions = nullptr);
118
- const TypeInfo *ResolveType(Napi::Env env, Span<const char> str);
119
-
120
- TypeInfo *MakePointerType(InstanceData *instance, const TypeInfo *ref, int count = 1);
121
- TypeInfo *MakeArrayType(InstanceData *instance, const TypeInfo *ref, Size len);
122
- TypeInfo *MakeArrayType(InstanceData *instance, const TypeInfo *ref, Size len, ArrayHint hint);
123
-
124
- Napi::Value WrapType(Napi::Env env, const TypeInfo *type, bool freeze = true);
125
-
126
- const TypeInfo *ReshapeType(InstanceData *instance, const TypeInfo *type, int32_t stride, uint16_t flags);
127
-
128
- bool CanPassType(const TypeInfo *type, int directions);
129
- bool CanReturnType(const TypeInfo *type);
130
- bool CanStoreType(const TypeInfo *type);
131
-
132
49
  static FORCE_INLINE napi_valuetype GetKindOf(napi_env env, napi_value value)
133
50
  {
134
51
  napi_valuetype kind = napi_undefined;
135
- napi_typeof(env, value, &kind);
52
+ NAPI_OK(napi_typeof(env, value, &kind));
136
53
 
137
54
  return kind;
138
55
  }
139
56
 
140
- static FORCE_INLINE napi_valuetype GetKindOf(Napi::Value value)
141
- {
142
- return GetKindOf(value.Env(), value);
143
- }
144
-
145
- // Can be slow, only use for error messages
146
- const char *GetValueType(const InstanceData *instance, napi_value value);
147
-
148
57
  void SetValueTag(napi_env env, napi_value value, const void *marker);
149
58
  bool CheckValueTag(napi_env env, napi_value value, const void *marker);
150
59
 
@@ -203,9 +112,7 @@ static FORCE_INLINE bool IsBuffer(napi_env env, napi_value value)
203
112
  static FORCE_INLINE napi_value GetReferenceValue(napi_env env, napi_ref ref)
204
113
  {
205
114
  napi_value value;
206
-
207
- napi_status status = napi_get_reference_value(env, ref, &value);
208
- K_ASSERT(status == napi_ok);
115
+ NAPI_OK(napi_get_reference_value(env, ref, &value));
209
116
 
210
117
  return value;
211
118
  }
@@ -213,9 +120,7 @@ static FORCE_INLINE napi_value GetReferenceValue(napi_env env, napi_ref ref)
213
120
  static FORCE_INLINE uint32_t GetArrayLength(napi_env env, napi_value array)
214
121
  {
215
122
  uint32_t length = 0;
216
-
217
- napi_status status = napi_get_array_length(env, array, &length);
218
- K_ASSERT(status == napi_ok);
123
+ NAPI_OK(napi_get_array_length(env, array, &length));
219
124
 
220
125
  return length;
221
126
  }
@@ -307,6 +212,62 @@ static FORCE_INLINE bool TryPointer(napi_env env, napi_value value, void **out_p
307
212
  return false;
308
213
  }
309
214
 
215
+ static FORCE_INLINE bool TryPointer(napi_env env, napi_value value, void **out_ptr, Size *out_len)
216
+ {
217
+ // Fast path for BigInt
218
+ {
219
+ uint64_t u64;
220
+ bool lossless;
221
+ napi_status status = napi_get_value_bigint_uint64(env, value, &u64, &lossless);
222
+
223
+ if (status == napi_ok) {
224
+ *out_ptr = (void *)(uintptr_t)u64;
225
+ *out_len = -1;
226
+
227
+ return true;
228
+ }
229
+ }
230
+
231
+ if (size_t len = 0; node_api_get_buffer_info(env, value, out_ptr, &len) == napi_ok) {
232
+ *out_len = (Size)len;
233
+ return true;
234
+ }
235
+
236
+ napi_valuetype kind = GetKindOf(env, value);
237
+
238
+ if (IsNullOrUndefined(kind)) {
239
+ *out_ptr = nullptr;
240
+ *out_len = -1;
241
+
242
+ return true;
243
+ } else if (kind == napi_number) {
244
+ int64_t i;
245
+ napi_status status = napi_get_value_int64(env, value, &i);
246
+ K_ASSERT(status == napi_ok);
247
+
248
+ *out_ptr = (void *)(uintptr_t)i;
249
+ *out_len = -1;
250
+
251
+ return true;
252
+ #if defined(EXTERNAL_POINTERS)
253
+ } else if (kind == napi_external) {
254
+ Napi::External<void> external = Napi::External<void>(env, value);
255
+
256
+ *out_ptr = (void *)external.Data();
257
+ *out_len = -1;
258
+
259
+ return true;
260
+ #endif
261
+ }
262
+
263
+ if (size_t len = 0; napi_get_arraybuffer_info(env, value, out_ptr, &len) == napi_ok) {
264
+ *out_len = (Size)len;
265
+ return true;
266
+ }
267
+
268
+ return false;
269
+ }
270
+
310
271
  static FORCE_INLINE bool TryPointer(napi_env env, napi_value value, void **out_ptr, napi_valuetype *out_kind)
311
272
  {
312
273
  // Fast path for BigInt
@@ -413,11 +374,7 @@ napi_value DecodeArray(InstanceData *instance, const uint8_t *origin, const Type
413
374
  void DecodeElements(InstanceData *instance, napi_value array, const uint8_t *origin, const TypeInfo *type, uint32_t len);
414
375
  INLINE_UNITY void DecodeBuffer(Span<uint8_t> buffer, const uint8_t *origin, const TypeInfo *type);
415
376
 
416
- napi_value Decode(Napi::Value value, Size offset, const TypeInfo *type, const Size *len = nullptr);
417
- napi_value Decode(InstanceData *instance, const uint8_t *ptr, const TypeInfo *type, const Size *len = nullptr);
418
-
419
- bool Encode(Napi::Value ref, Size offset, Napi::Value value, const TypeInfo *type, const Size *len = nullptr);
420
- bool Encode(InstanceData *instance, uint8_t *ptr, Napi::Value value, const TypeInfo *type, const Size *len = nullptr);
377
+ napi_value Decode(InstanceData *instance, const uint8_t *ptr, const TypeInfo *type);
421
378
 
422
379
  static FORCE_INLINE napi_value NewInt(Napi::Env env, char i) { napi_value value; napi_create_int32(env, (int32_t)i, &value); return value; }
423
380
  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; }
@@ -436,22 +393,24 @@ static FORCE_INLINE napi_value NewInt(Napi::Env env, T i)
436
393
  {
437
394
  static_assert(sizeof(T) == 8);
438
395
 
396
+ napi_value value;
397
+
439
398
  if constexpr (std::is_signed_v<T>) {
440
399
  if (i <= 9007199254740992ll && i >= -9007199254740992ll) {
441
- napi_value value;
442
- napi_create_int64(env, (int64_t)i, &value);
443
- return Napi::Value(env, value);
400
+ NAPI_OK(napi_create_int64(env, (int64_t)i, &value));
401
+ return value;
444
402
  }
445
403
 
446
- return Napi::BigInt::New(env, (int64_t)i);
404
+ NAPI_OK(napi_create_bigint_int64(env, (int64_t)i, &value));
405
+ return value;
447
406
  } else {
448
407
  if (i <= 9007199254740992ull) {
449
- napi_value value;
450
- napi_create_int64(env, (int64_t)i, &value);
451
- return Napi::Value(env, value);
408
+ NAPI_OK(napi_create_int64(env, (int64_t)i, &value));
409
+ return value;
452
410
  }
453
411
 
454
- return Napi::BigInt::New(env, (uint64_t)i);
412
+ NAPI_OK(napi_create_bigint_uint64(env, (uint64_t)i, &value));
413
+ return value;
455
414
  }
456
415
  }
457
416
 
@@ -468,18 +427,20 @@ static FORCE_INLINE Napi::Array GetOwnPropertyNames(napi_env env, napi_value obj
468
427
  return Napi::Array(env, result);
469
428
  }
470
429
 
471
- Napi::Object DescribeFunction(Napi::Env env, const FunctionInfo *func);
472
- Napi::Function WrapFunction(Napi::Env env, const FunctionInfo *func);
430
+ napi_value DescribeFunction(Napi::Env env, const FunctionInfo *func);
431
+ napi_value WrapFunction(Napi::Env env, const FunctionInfo *func);
473
432
 
474
- static FORCE_INLINE Napi::Value WrapPointer(Napi::Env env, const TypeInfo *ref, void *ptr)
433
+ static FORCE_INLINE napi_value WrapPointer(Napi::Env env, const TypeInfo *ref, void *ptr)
475
434
  {
435
+ napi_value value;
436
+
476
437
  #if defined(EXTERNAL_POINTERS)
477
- Napi::External<void> external = Napi::External<void>::New(env, ptr);
478
- return external;
438
+ NAPI_OK(napi_create_external(env, ptr, nullptr, nullptr, &value));
479
439
  #else
480
- Napi::BigInt big = Napi::BigInt::New(env, (uint64_t)(uintptr_t)ptr);
481
- return big;
440
+ NAPI_OK(napi_create_bigint_uint64(env, (uint64_t)(uintptr_t)ptr, &value));
482
441
  #endif
442
+
443
+ return value;
483
444
  }
484
445
 
485
446
  bool DetectCallConvention(Span<const char> name, CallConvention *out_convention);
@@ -1,19 +1,25 @@
1
1
  // SPDX-License-Identifier: MIT
2
2
 
3
- #include "uv.hh"
3
+ #include "lib/native/base/base.hh"
4
+ #include "type.hh"
4
5
  #include "util.hh"
6
+ #include "uv.hh"
7
+
8
+ #include <napi.h>
5
9
 
6
10
  namespace K {
7
11
 
8
- Napi::Function PollHandle::InitClass(Napi::Env env)
12
+ Napi::Function PollHandle::InitClass(InstanceData *instance)
9
13
  {
14
+ Napi::Env env = instance->env;
15
+
10
16
  // node-addon-api wants std::vector
11
17
  std::vector<Napi::ClassPropertyDescriptor<PollHandle>> properties = {
12
- InstanceMethod("start", &PollHandle::Start),
13
- InstanceMethod("stop", &PollHandle::Stop),
14
- InstanceMethod("close", &PollHandle::Close),
15
- InstanceMethod("unref", &PollHandle::Unref),
16
- InstanceMethod("ref", &PollHandle::Ref)
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)
17
23
  };
18
24
 
19
25
  if (Napi::Value dispose = env.RunScript("Symbol.dispose"); !IsNullOrUndefined(env, dispose)) {
@@ -21,7 +27,7 @@ Napi::Function PollHandle::InitClass(Napi::Env env)
21
27
  properties.push_back(prop);
22
28
  }
23
29
 
24
- Napi::Function constructor = DefineClass(env, "PollHandle", properties);
30
+ Napi::Function constructor = DefineClass(env, "PollHandle", properties, instance);
25
31
  return constructor;
26
32
  }
27
33
 
@@ -62,7 +68,7 @@ PollHandle::PollHandle(const Napi::CallbackInfo &info)
62
68
 
63
69
  void PollHandle::Start(const Napi::CallbackInfo &info)
64
70
  {
65
- InstanceData *instance = env.GetInstanceData<InstanceData>();
71
+ InstanceData *instance = (InstanceData *)info.Data();
66
72
 
67
73
  bool has_opts = (info.Length() >= 2 && info[0].IsObject());
68
74
 
@@ -165,7 +171,7 @@ void PollHandle::OnPoll(uv_poll_t *h, int status, int events)
165
171
  Napi::Value Poll(const Napi::CallbackInfo &info)
166
172
  {
167
173
  Napi::Env env = info.Env();
168
- InstanceData *instance = env.GetInstanceData<InstanceData>();
174
+ InstanceData *instance = (InstanceData *)info.Data();
169
175
 
170
176
  bool has_opts = (info.Length() >= 3 && info[1].IsObject());
171
177
 
@@ -3,6 +3,7 @@
3
3
  #pragma once
4
4
 
5
5
  #include "lib/native/base/base.hh"
6
+ #include "ffi.hh"
6
7
 
7
8
  #include <napi.h>
8
9
 
@@ -88,7 +89,7 @@ class PollHandle: public Napi::ObjectWrap<PollHandle> {
88
89
  Napi::FunctionReference callback;
89
90
 
90
91
  public:
91
- static Napi::Function InitClass(Napi::Env env);
92
+ static Napi::Function InitClass(InstanceData *instance);
92
93
 
93
94
  PollHandle(const Napi::CallbackInfo &info);
94
95
 
package/indirect.d.ts DELETED
@@ -1,322 +0,0 @@
1
- // SPDX-License-Identifier: MIT
2
- // SPDX-FileCopyrightText: 2026 Niels Martignène <niels.martignene@protonmail.com>
3
-
4
- declare module "koffi/indirect" {
5
- type PrimitiveKind = 'Void' | 'Bool' | 'Int8' | 'UInt8' | 'Int16' | 'Int16S' | 'UInt16' | 'UInt16S' |
6
- 'Int32' | 'Int32S' | 'UInt32' | 'UInt32S' | 'Int64' | 'Int64S' | 'UInt64' | 'UInt64S' |
7
- 'String' | 'String16' | 'Pointer' | 'Record' | 'Union' | 'Array' | 'Float32' | 'Float64' |
8
- 'Prototype' | 'Callback';
9
- type ArrayHint = 'Array' | 'Typed' | 'String';
10
-
11
- type PrototypeInfo = {
12
- name: string;
13
- arguments: TypeObject[];
14
- result: TypeObject
15
- };
16
-
17
- interface IKoffiDirectionMarker { __brand: 'IKoffiDirectionMarker' }
18
- interface IKoffiPointerCast { __brand: 'IKoffiPointerCast' }
19
-
20
- export type TypeObject = {
21
- name: string;
22
- primitive: PrimitiveKind;
23
- size: number;
24
- alignment: number;
25
- disposable: boolean;
26
- length?: number;
27
- hint?: ArrayHint;
28
- ref?: TypeObject;
29
- members?: Record<string, { name: string, type: TypeObject, offset: number }>;
30
- proto?: PrototypeInfo;
31
- values?: Record<string, number | bigint>;
32
- };
33
-
34
- type TypeSpec = string | TypeObject | IKoffiDirectionMarker;
35
- type TypeSpecWithAlignment = TypeSpec | [number, TypeSpec];
36
-
37
- type KoffiFunction = {
38
- (...args: any[]) : any;
39
- async: (...args: any[]) => any;
40
- info: PrototypeInfo;
41
- };
42
- export type KoffiFunc<T extends (...args: any) => any> = T & {
43
- async: (...args: [...Parameters<T>, (err: any, result: ReturnType<T>) => void]) => void;
44
- info: PrototypeInfo;
45
- };
46
-
47
- type LoadOptions = {
48
- lazy?: boolean,
49
- global?: boolean,
50
- deep?: boolean
51
- };
52
-
53
- export type LibraryHandle = {
54
- func(definition: string): KoffiFunction;
55
- func(name: string | number, result: TypeSpec, arguments: TypeSpec[]): KoffiFunction;
56
- func(convention: string, name: string | number, result: TypeSpec, arguments: TypeSpec[]): KoffiFunction;
57
-
58
- /** @deprecated */ cdecl(definition: string): KoffiFunction;
59
- /** @deprecated */ cdecl(name: string | number, result: TypeSpec, arguments: TypeSpec[]): KoffiFunction;
60
- /** @deprecated */ stdcall(definition: string): KoffiFunction;
61
- /** @deprecated */ stdcall(name: string | number, result: TypeSpec, arguments: TypeSpec[]): KoffiFunction;
62
- /** @deprecated */ fastcall(definition: string): KoffiFunction;
63
- /** @deprecated */ fastcall(name: string | number, result: TypeSpec, arguments: TypeSpec[]): KoffiFunction;
64
- /** @deprecated */ thiscall(definition: string): KoffiFunction;
65
- /** @deprecated */ thiscall(name: string | number, result: TypeSpec, arguments: TypeSpec[]): KoffiFunction;
66
-
67
- symbol(name: string, type: TypeSpec): any;
68
-
69
- unload(): void;
70
- };
71
-
72
- export function load(path: string | null, options?: LoadOptions): LibraryHandle;
73
-
74
- export function struct(name: string | null | undefined, def: Record<string, TypeSpecWithAlignment>): TypeObject;
75
- export function struct(ref: TypeObject, def: Record<string, TypeSpecWithAlignment>): TypeObject;
76
- export function struct(def: Record<string, TypeSpecWithAlignment>): TypeObject;
77
- export function pack(name: string | null | undefined, def: Record<string, TypeSpecWithAlignment>): TypeObject;
78
- export function pack(ref: TypeObject, def: Record<string, TypeSpecWithAlignment>): TypeObject;
79
- export function pack(def: Record<string, TypeSpecWithAlignment>): TypeObject;
80
-
81
- export function union(name: string | null | undefined, def: Record<string, TypeSpecWithAlignment>): TypeObject;
82
- export function union(ref: TypeObject, def: Record<string, TypeSpecWithAlignment>): TypeObject;
83
- export function union(def: Record<string, TypeSpecWithAlignment>): TypeObject;
84
-
85
- export class Union {
86
- constructor(type: TypeSpec);
87
- [s: string]: any;
88
- }
89
-
90
- export function enumeration(name: string | null | undefined, def: Record<string, number | bigint>, storage?: TypeSpec | null) : TypeObject;
91
- export function enumeration(def: Record<string, number | bigint>, storage?: TypeSpec | null) : TypeObject;
92
-
93
- export function array(ref: TypeSpec, len: number, hint?: ArrayHint | null): TypeObject;
94
- export function array(ref: TypeSpec, countedBy: string, maxLen: number, hint?: ArrayHint | null): TypeObject;
95
-
96
- export function opaque(name: string | null | undefined): TypeObject;
97
- export function opaque(): TypeObject;
98
-
99
- export function pointer(ref: TypeSpec): TypeObject;
100
- export function pointer(ref: TypeSpec, count: number): TypeObject;
101
- export function pointer(name: string | null | undefined, ref: TypeSpec, countedBy?: string | null): TypeObject;
102
- export function pointer(name: string | null | undefined, ref: TypeSpec, count: number): TypeObject;
103
-
104
- export function out(type: TypeSpec): IKoffiDirectionMarker;
105
- export function inout(type: TypeSpec): IKoffiDirectionMarker;
106
-
107
- export function disposable(type: TypeSpec): TypeObject;
108
- export function disposable(type: TypeSpec, freeFunction: Function): TypeObject;
109
- export function disposable(name: string | null | undefined, type: TypeSpec): TypeObject;
110
- export function disposable(name: string | null | undefined, type: TypeSpec, freeFunction: Function): TypeObject;
111
-
112
- export function proto(definition: string): TypeObject;
113
- export function proto(result: TypeSpec, arguments: TypeSpec[]): TypeObject;
114
- export function proto(convention: string, result: TypeSpec, arguments: TypeSpec[]): TypeObject;
115
- export function proto(name: string | null | undefined, result: TypeSpec, arguments: TypeSpec[]): TypeObject;
116
- export function proto(convention: string, name: string | null | undefined, result: TypeSpec, arguments: TypeSpec[]): TypeObject;
117
-
118
- export function register(callback: Function, type: TypeSpec): bigint;
119
- /** @deprecated */ export function register(thisValue: any, callback: Function, type: TypeSpec): bigint;
120
- export function unregister(callback: bigint): void;
121
-
122
- export function as(value: any, type: TypeSpec): IKoffiPointerCast;
123
- export function address(value: any): bigint;
124
- export function call(value: any, type: TypeSpec, ...args: any[]): any;
125
- export function view(ref: any, len: number): ArrayBuffer;
126
-
127
- export const decode: {
128
- (value: any, type: TypeSpec): any;
129
- (value: any, type: TypeSpec, len: number): any;
130
- (value: any, offset: number, type: TypeSpec): any;
131
- (value: any, offset: number, type: TypeSpec, len: number): any;
132
-
133
- char(ptr: any): number;
134
- short(ptr: any): number;
135
- int(ptr: any): number;
136
- long(ptr: any): number | bigint;
137
- longlong(ptr: any): number | bigint;
138
- uchar(ptr: any): number;
139
- ushort(ptr: any): number;
140
- uint(ptr: any): number;
141
- ulong(ptr: any): number | bigint;
142
- ulonglong(ptr: any): number | bigint;
143
-
144
- int8(ptr: any): number;
145
- int16(ptr: any): number;
146
- int32(ptr: any): number;
147
- int64(ptr: any): number | bigint;
148
- uint8(ptr: any): number;
149
- uint16(ptr: any): number;
150
- uint32(ptr: any): number;
151
- uint64(ptr: any): number | bigint;
152
-
153
- float(ptr: any): number;
154
- double(ptr: any): number;
155
-
156
- string(ptr: any, length?: number | bigint | null): string;
157
- string16(ptr: any, length?: number | bigint | null): string;
158
- string32(ptr: any, length?: number | bigint | null): string;
159
- };
160
-
161
- export function encode(ref: any, type: TypeSpec, value: any): void;
162
- export function encode(ref: any, type: TypeSpec, value: any, len: number): void;
163
- export function encode(ref: any, offset: number, type: TypeSpec): void;
164
- export function encode(ref: any, offset: number, type: TypeSpec, value: any): void;
165
- export function encode(ref: any, offset: number, type: TypeSpec, value: any, len: number): void;
166
-
167
- export function type(type: TypeSpec): TypeObject;
168
- export function sizeof(type: TypeSpec): number;
169
- export function alignof(type: TypeSpec): number;
170
- export function offsetof(type: TypeSpec, member_name: string): number;
171
- /** @deprecated */ export function resolve(type: TypeSpec): TypeObject;
172
- /** @deprecated */ export function introspect(type: TypeSpec): TypeObject;
173
-
174
- export function alias(name: string, type: TypeSpec): TypeObject;
175
-
176
- type KoffiConfig = {
177
- sync_stack_size?: number;
178
- sync_heap_size?: number;
179
- async_stack_size?: number;
180
- async_heap_size?: number;
181
- resident_async_pools?: number;
182
- max_async_calls?: number;
183
- max_type_size?: number;
184
- };
185
- type KoffiStats = {
186
- disposed: number
187
- };
188
-
189
- export function config(): KoffiConfig;
190
- export function config(cfg: KoffiConfig): KoffiConfig;
191
- export function stats(): KoffiStats;
192
-
193
- export function alloc(type: TypeSpec, length: number): any;
194
- export function free(value: any): void;
195
-
196
- export function errno(): number;
197
- export function errno(value: number): number;
198
-
199
- export function reset(): void;
200
-
201
- export const internal: boolean;
202
- export const extension: string;
203
-
204
- export const os: {
205
- errno: Record<string, number>
206
- };
207
-
208
- // https://koffi.dev/input#standard-types
209
- type PrimitiveTypes =
210
- | 'bool'
211
- | 'char'
212
- | 'char16_t'
213
- | 'char16'
214
- | 'char32_t'
215
- | 'char32'
216
- | 'double'
217
- | 'float'
218
- | 'float32'
219
- | 'float64'
220
- | 'int'
221
- | 'int8_t'
222
- | 'int8'
223
- | 'int16_be_t'
224
- | 'int16_be'
225
- | 'int16_le_t'
226
- | 'int16_le'
227
- | 'int16_t'
228
- | 'int16'
229
- | 'int32_be_t'
230
- | 'int32_be'
231
- | 'int32_le_t'
232
- | 'int32_le'
233
- | 'int32_t'
234
- | 'int32'
235
- | 'int64_be_t'
236
- | 'int64_be'
237
- | 'int64_le_t'
238
- | 'int64_le'
239
- | 'int64_t'
240
- | 'int64'
241
- | 'intptr_t'
242
- | 'intptr'
243
- | 'long long'
244
- | 'long'
245
- | 'longlong'
246
- | 'short'
247
- | 'size_t'
248
- | 'str'
249
- | 'str16'
250
- | 'str32'
251
- | 'string'
252
- | 'string16'
253
- | 'string32'
254
- | 'uchar'
255
- | 'uint'
256
- | 'uint8_t'
257
- | 'uint8'
258
- | 'uint16_be_t'
259
- | 'uint16_be'
260
- | 'uint16_le_t'
261
- | 'uint16_le'
262
- | 'uint16_t'
263
- | 'uint16'
264
- | 'uint32_be_t'
265
- | 'uint32_be'
266
- | 'uint32_le_t'
267
- | 'uint32_le'
268
- | 'uint32_t'
269
- | 'uint32'
270
- | 'uint64_be_t'
271
- | 'uint64_be'
272
- | 'uint64_le_t'
273
- | 'uint64_le'
274
- | 'uint64_t'
275
- | 'uint64'
276
- | 'uintptr_t'
277
- | 'uintptr'
278
- | 'ulong'
279
- | 'ulonglong'
280
- | 'unsigned char'
281
- | 'unsigned int'
282
- | 'unsigned long long'
283
- | 'unsigned long'
284
- | 'unsigned short'
285
- | 'ushort'
286
- | 'void'
287
- | 'wchar'
288
- | 'wchar_t';
289
- export const types: Record<PrimitiveTypes, TypeObject>;
290
-
291
- export namespace node {
292
- export const env: { __brand: 'IKoffiNodeEnv' };
293
-
294
- type PollOptions = {
295
- readable?: boolean;
296
- writable?: boolean;
297
- disconnect?: boolean;
298
- };
299
-
300
- type PollEvents = {
301
- readable: boolean;
302
- writable: boolean;
303
- disconnect: boolean;
304
- };
305
-
306
- export class PollHandle {
307
- start(opts: PollOptions, callback: (ev: PollEvents) => void): void;
308
- start(callback: (ev: PollEvents) => void): void;
309
- stop(): void;
310
-
311
- close(): void;
312
-
313
- unref(): void;
314
- ref(): void;
315
- }
316
-
317
- export function poll(fd: number, opts: PollOptions, callback: (ev: PollEvents) => void): PollHandle;
318
- export function poll(fd: number, callback: (ev: PollEvents) => void): PollHandle;
319
- }
320
-
321
- export const version: string;
322
- }