koffi 2.3.12 → 2.3.13

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.
Files changed (38) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/build/2.3.13/koffi_darwin_arm64/koffi.node +0 -0
  3. package/build/2.3.13/koffi_darwin_x64/koffi.node +0 -0
  4. package/build/2.3.13/koffi_freebsd_arm64/koffi.node +0 -0
  5. package/build/2.3.13/koffi_freebsd_ia32/koffi.node +0 -0
  6. package/build/2.3.13/koffi_freebsd_x64/koffi.node +0 -0
  7. package/build/2.3.13/koffi_linux_arm32hf/koffi.node +0 -0
  8. package/build/{2.3.12 → 2.3.13}/koffi_linux_arm64/koffi.node +0 -0
  9. package/build/2.3.13/koffi_linux_ia32/koffi.node +0 -0
  10. package/build/2.3.13/koffi_linux_riscv64hf64/koffi.node +0 -0
  11. package/build/{2.3.12 → 2.3.13}/koffi_linux_x64/koffi.node +0 -0
  12. package/build/2.3.13/koffi_openbsd_ia32/koffi.node +0 -0
  13. package/build/2.3.13/koffi_openbsd_x64/koffi.node +0 -0
  14. package/build/2.3.13/koffi_win32_arm64/koffi.node +0 -0
  15. package/build/2.3.13/koffi_win32_ia32/koffi.node +0 -0
  16. package/build/{2.3.12 → 2.3.13}/koffi_win32_x64/koffi.node +0 -0
  17. package/doc/pointers.md +4 -0
  18. package/package.json +2 -2
  19. package/src/index.d.ts +2 -2
  20. package/src/koffi/src/ffi.cc +36 -4
  21. package/build/2.3.12/koffi_darwin_arm64/koffi.node +0 -0
  22. package/build/2.3.12/koffi_darwin_x64/koffi.node +0 -0
  23. package/build/2.3.12/koffi_freebsd_arm64/koffi.node +0 -0
  24. package/build/2.3.12/koffi_freebsd_ia32/koffi.node +0 -0
  25. package/build/2.3.12/koffi_freebsd_x64/koffi.node +0 -0
  26. package/build/2.3.12/koffi_linux_arm32hf/koffi.node +0 -0
  27. package/build/2.3.12/koffi_linux_ia32/koffi.node +0 -0
  28. package/build/2.3.12/koffi_linux_riscv64hf64/koffi.node +0 -0
  29. package/build/2.3.12/koffi_openbsd_ia32/koffi.node +0 -0
  30. package/build/2.3.12/koffi_openbsd_x64/koffi.node +0 -0
  31. package/build/2.3.12/koffi_win32_arm64/koffi.node +0 -0
  32. package/build/2.3.12/koffi_win32_ia32/koffi.node +0 -0
  33. /package/build/{2.3.12 → 2.3.13}/koffi_win32_arm64/koffi.exp +0 -0
  34. /package/build/{2.3.12 → 2.3.13}/koffi_win32_arm64/koffi.lib +0 -0
  35. /package/build/{2.3.12 → 2.3.13}/koffi_win32_ia32/koffi.exp +0 -0
  36. /package/build/{2.3.12 → 2.3.13}/koffi_win32_ia32/koffi.lib +0 -0
  37. /package/build/{2.3.12 → 2.3.13}/koffi_win32_x64/koffi.exp +0 -0
  38. /package/build/{2.3.12 → 2.3.13}/koffi_win32_x64/koffi.lib +0 -0
package/CHANGELOG.md CHANGED
@@ -4,6 +4,12 @@
4
4
 
5
5
  ### Koffi 2.3
6
6
 
7
+ #### Koffi 2.3.13
8
+
9
+ **Main changes:**
10
+
11
+ - Add `koffi.address()` to get the raw value of a wrapper pointer
12
+
7
13
  #### Koffi 2.3.12
8
14
 
9
15
  **Main fixes:**
package/doc/pointers.md CHANGED
@@ -133,3 +133,7 @@ By default, just like for objects, array arguments are copied from JS to C but n
133
133
  Disposable types allow you to register a function that will automatically called after each C to JS conversion performed by Koffi. This can be used to avoid leaking heap-allocated strings, for example.
134
134
 
135
135
  Read the documentation for [disposable types](calls.md#heap-allocated-values) on the page about function calls.
136
+
137
+ ## Unwrap pointers
138
+
139
+ You can use `koffi.address(ptr)` on a pointer to get the numeric value as a [BigInt object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt).
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koffi",
3
- "version": "2.3.12",
4
- "stable": "2.3.12",
3
+ "version": "2.3.13",
4
+ "stable": "2.3.13",
5
5
  "description": "Fast and simple C FFI (foreign function interface) for Node.js",
6
6
  "keywords": [
7
7
  "foreign",
package/src/index.d.ts CHANGED
@@ -101,8 +101,6 @@ declare module 'koffi' {
101
101
  export function out(value: TypeSpec): IKoffiCType;
102
102
  export function inout(value: TypeSpec): IKoffiCType;
103
103
 
104
- export function as(value: any, type: TypeSpec): IKoffiPointerCast;
105
-
106
104
  export function disposable(type: TypeSpec): IKoffiCType;
107
105
  export function disposable(name: string, type: TypeSpec): IKoffiCType;
108
106
  export function disposable(name: string, type: TypeSpec, freeFunction: Function): IKoffiCType;
@@ -114,10 +112,12 @@ declare module 'koffi' {
114
112
  export function register(thisValue: any, callback: Function, type: TypeSpec): IKoffiRegisteredCallback;
115
113
  export function unregister(callback: IKoffiRegisteredCallback): void;
116
114
 
115
+ export function as(value: any, type: TypeSpec): IKoffiPointerCast;
117
116
  export function decode(value: any, type: TypeSpec): any;
118
117
  export function decode(value: any, type: TypeSpec, len: number): any;
119
118
  export function decode(value: any, offset: number, type: TypeSpec): any;
120
119
  export function decode(value: any, offset: number, type: TypeSpec, len: number): any;
120
+ export function address(value: any): bigint;
121
121
 
122
122
  export function sizeof(type: TypeSpec): number;
123
123
  export function alignof(type: TypeSpec): number;
@@ -722,19 +722,30 @@ static Napi::Value CreateDisposableType(const Napi::CallbackInfo &info)
722
722
  return WrapType(env, instance, type);
723
723
  }
724
724
 
725
+ static inline bool CheckExternalPointer(Napi::Env env, Napi::Value value)
726
+ {
727
+ InstanceData *instance = env.GetInstanceData<InstanceData>();
728
+
729
+ if (!value.IsExternal() || CheckValueTag(instance, value, &TypeInfoMarker) ||
730
+ CheckValueTag(instance, value, &CastMarker) ||
731
+ CheckValueTag(instance, value, &MagicUnionMarker)) {
732
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value for ptr, expected external pointer", GetValueType(instance, value));
733
+ return false;
734
+ }
735
+
736
+ return true;
737
+ }
738
+
725
739
  static Napi::Value CallFree(const Napi::CallbackInfo &info)
726
740
  {
727
741
  Napi::Env env = info.Env();
728
- InstanceData *instance = env.GetInstanceData<InstanceData>();
729
742
 
730
743
  if (info.Length() < 1) {
731
744
  ThrowError<Napi::TypeError>(env, "Expected 1 argument, got %1", info.Length());
732
745
  return env.Null();
733
746
  }
734
- if (!info[0].IsExternal() || CheckValueTag(instance, info[0], &TypeInfoMarker)) {
735
- ThrowError<Napi::TypeError>(env, "Unexpected %1 value for ptr, expected external", GetValueType(instance, info[0]));
747
+ if (!CheckExternalPointer(env, info[0]))
736
748
  return env.Null();
737
- }
738
749
 
739
750
  Napi::External<void> external = info[0].As<Napi::External<void>>();
740
751
  void *ptr = external.Data();
@@ -1973,6 +1984,26 @@ static Napi::Value DecodeValue(const Napi::CallbackInfo &info)
1973
1984
  return ret;
1974
1985
  }
1975
1986
 
1987
+ static Napi::Value GetPointerAddress(const Napi::CallbackInfo &info)
1988
+ {
1989
+ Napi::Env env = info.Env();
1990
+
1991
+ if (info.Length() < 1) {
1992
+ ThrowError<Napi::TypeError>(env, "Expected 1 argument, got %1", info.Length());
1993
+ return env.Null();
1994
+ }
1995
+ if (!CheckExternalPointer(env, info[0]))
1996
+ return env.Null();
1997
+
1998
+ Napi::External<void> external = info[0].As<Napi::External<void>>();
1999
+ void *ptr = external.Data();
2000
+
2001
+ uint64_t ptr64 = (uint64_t)(uintptr_t)ptr;
2002
+ Napi::BigInt bigint = Napi::BigInt::New(env, ptr64);
2003
+
2004
+ return bigint;
2005
+ }
2006
+
1976
2007
  extern "C" void RelayCallback(Size idx, uint8_t *own_sp, uint8_t *caller_sp, BackRegisters *out_reg)
1977
2008
  {
1978
2009
  if (RG_LIKELY(exec_call)) {
@@ -2038,6 +2069,7 @@ static void SetExports(Napi::Env env, Func func)
2038
2069
 
2039
2070
  func("as", Napi::Function::New(env, CastValue));
2040
2071
  func("decode", Napi::Function::New(env, DecodeValue));
2072
+ func("address", Napi::Function::New(env, GetPointerAddress));
2041
2073
 
2042
2074
  #if defined(_WIN32)
2043
2075
  func("extension", Napi::String::New(env, ".dll"));