koffi 1.1.0-beta.3 → 1.1.0-beta.4

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/README.md CHANGED
@@ -3,6 +3,7 @@
3
3
  - [Introduction](#introduction)
4
4
  - [Get started](#get-started)
5
5
  - [Extra features](#extra-features)
6
+ * [Type information](#type-information)
6
7
  * [C arrays](#c-arrays)
7
8
  * [Variadic functions](#variadic-functions)
8
9
  * [Asynchronous calls](#asynchronous-calls)
@@ -218,6 +219,13 @@ while (!WindowShouldClose()) {
218
219
 
219
220
  # Extra features
220
221
 
222
+ ## Type information
223
+
224
+ Koffi exposes three functions to explore type information:
225
+ - `koffi.sizeof(type)` to get the size of a type
226
+ - `koffi.alignon(type)` to get the alignment of a type
227
+ - `koffi.introspect(type)` to get the definition of a type (only for structs for now)
228
+
221
229
  ## C arrays
222
230
 
223
231
  Fixed-size arrays are declared with `koffi.array(type, length)`. Just like in C, they cannot be passed
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koffi",
3
- "version": "1.1.0-beta.3",
3
+ "version": "1.1.0-beta.4",
4
4
  "description": "Fast and simple FFI (foreign function interface) for Node.js",
5
5
  "keywords": [
6
6
  "foreign",
package/src/ffi.cc CHANGED
@@ -72,6 +72,8 @@ static Napi::Value CreateStructType(const Napi::CallbackInfo &info, bool pad)
72
72
 
73
73
  type->name = DuplicateString(name.c_str(), &instance->str_alloc).ptr;
74
74
 
75
+ type->defn.Reset(obj, 1);
76
+
75
77
  type->primitive = PrimitiveKind::Record;
76
78
  type->align = 1;
77
79
 
@@ -296,6 +298,61 @@ static Napi::Value CreateArrayType(const Napi::CallbackInfo &info)
296
298
  return external;
297
299
  }
298
300
 
301
+ static Napi::Value GetTypeSize(const Napi::CallbackInfo &info)
302
+ {
303
+ Napi::Env env = info.Env();
304
+ InstanceData *instance = env.GetInstanceData<InstanceData>();
305
+
306
+ if (info.Length() < 1) {
307
+ ThrowError<Napi::TypeError>(env, "Expected 1 argument, got %1", info.Length());
308
+ return env.Null();
309
+ }
310
+
311
+ const TypeInfo *type = ResolveType(instance, info[0]);
312
+ if (!type)
313
+ return env.Null();
314
+
315
+ return Napi::Number::New(env, type->size);
316
+ }
317
+
318
+ static Napi::Value GetTypeAlign(const Napi::CallbackInfo &info)
319
+ {
320
+ Napi::Env env = info.Env();
321
+ InstanceData *instance = env.GetInstanceData<InstanceData>();
322
+
323
+ if (info.Length() < 1) {
324
+ ThrowError<Napi::TypeError>(env, "Expected 1 argument, got %1", info.Length());
325
+ return env.Null();
326
+ }
327
+
328
+ const TypeInfo *type = ResolveType(instance, info[0]);
329
+ if (!type)
330
+ return env.Null();
331
+
332
+ return Napi::Number::New(env, type->align);
333
+ }
334
+
335
+ static Napi::Value GetTypeDefinition(const Napi::CallbackInfo &info)
336
+ {
337
+ Napi::Env env = info.Env();
338
+ InstanceData *instance = env.GetInstanceData<InstanceData>();
339
+
340
+ if (info.Length() < 1) {
341
+ ThrowError<Napi::TypeError>(env, "Expected 1 argument, got %1", info.Length());
342
+ return env.Null();
343
+ }
344
+
345
+ const TypeInfo *type = ResolveType(instance, info[0]);
346
+ if (!type)
347
+ return env.Null();
348
+ if (type->defn.IsEmpty()) {
349
+ ThrowError<Napi::TypeError>(env, "Definition of type %1 is not available", type->name);
350
+ return env.Null();
351
+ }
352
+
353
+ return type->defn.Value();
354
+ }
355
+
299
356
  static Span<uint8_t> AllocateAndAlign16(Allocator *alloc, Size size)
300
357
  {
301
358
  RG_ASSERT(AlignLen(size, 16) == size);
@@ -870,6 +927,9 @@ static void SetExports(Napi::Env env, Func func)
870
927
  func("handle", Napi::Function::New(env, CreateHandleType));
871
928
  func("pointer", Napi::Function::New(env, CreatePointerType));
872
929
  func("array", Napi::Function::New(env, CreateArrayType));
930
+ func("sizeof", Napi::Function::New(env, GetTypeSize));
931
+ func("alignof", Napi::Function::New(env, GetTypeAlign));
932
+ func("introspect", Napi::Function::New(env, GetTypeDefinition));
873
933
  func("load", Napi::Function::New(env, LoadSharedLibrary));
874
934
  func("in", Napi::Function::New(env, MarkIn));
875
935
  func("out", Napi::Function::New(env, MarkOut));
package/src/ffi.hh CHANGED
@@ -70,6 +70,8 @@ struct TypeInfo {
70
70
  const char *name;
71
71
  napi_type_tag tag;
72
72
 
73
+ Napi::ObjectReference defn;
74
+
73
75
  PrimitiveKind primitive;
74
76
  int16_t size;
75
77
  int16_t align;