node-ctypes 2.1.1 → 2.2.0

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.
@@ -46,6 +46,8 @@
46
46
  * ```
47
47
  */
48
48
 
49
+ import { kMirrorBase, mirrorBase, mirroredPrototype, mirrorsActive } from "../memory/mirror.js";
50
+
49
51
  /**
50
52
  * Creates the SimpleCData base class with required dependencies injected.
51
53
  *
@@ -54,6 +56,16 @@
54
56
  * @private
55
57
  */
56
58
  export function createSimpleCDataClass(alloc, ptrToBuffer) {
59
+ // Istanza su un Buffer esistente. Su una copia di memoria nativa (Electron,
60
+ // memory/mirror.js) `value` resta sincronizzato con la memoria.
61
+ function withBuffer(Ctor, buf) {
62
+ const base = mirrorsActive ? mirrorBase(buf) : undefined;
63
+ const inst = Object.create(base === undefined ? Ctor.prototype : mirroredPrototype(Ctor));
64
+ inst._buffer = buf;
65
+ if (base !== undefined) inst[kMirrorBase] = base;
66
+ return inst;
67
+ }
68
+
57
69
  /**
58
70
  * SimpleCData base class for primitive C types.
59
71
  *
@@ -146,9 +158,7 @@ export function createSimpleCDataClass(alloc, ptrToBuffer) {
146
158
  if (!Buffer.isBuffer(buffer)) {
147
159
  throw new TypeError("from_buffer requires a Buffer");
148
160
  }
149
- const inst = Object.create(this.prototype);
150
- inst._buffer = buffer.subarray(offset, offset + this._size);
151
- return inst;
161
+ return withBuffer(this, buffer.subarray(offset, offset + this._size));
152
162
  }
153
163
 
154
164
  /**
@@ -178,10 +188,7 @@ export function createSimpleCDataClass(alloc, ptrToBuffer) {
178
188
  throw new Error("from_address requires ptrToBuffer injected at construction");
179
189
  }
180
190
  const addr = typeof address === "bigint" ? address : BigInt(address);
181
- const buf = ptrToBuffer(addr, this._size);
182
- const inst = Object.create(this.prototype);
183
- inst._buffer = buf;
184
- return inst;
191
+ return withBuffer(this, ptrToBuffer(addr, this._size));
185
192
  }
186
193
 
187
194
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-ctypes",
3
- "version": "2.1.1",
3
+ "version": "2.2.0",
4
4
  "description": "Python ctypes-like FFI for Node.js using libffi",
5
5
  "author": "Damiano Mazzella",
6
6
  "license": "MIT",