rclnodejs 1.5.0 → 1.5.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.
- package/README.md +38 -110
- package/binding.gyp +2 -0
- package/index.js +1 -1
- package/lib/action/client.js +1 -1
- package/lib/action/graph.js +1 -1
- package/lib/action/server.js +1 -1
- package/lib/action/server_goal_handle.js +1 -1
- package/lib/client.js +1 -1
- package/lib/clock.js +1 -1
- package/lib/context.js +1 -1
- package/lib/duration.js +1 -1
- package/lib/event_handler.js +1 -1
- package/lib/guard_condition.js +1 -1
- package/lib/interface_loader.js +97 -1
- package/lib/lifecycle.js +1 -1
- package/lib/lifecycle_publisher.js +1 -1
- package/lib/logging.js +1 -1
- package/lib/native_loader.js +173 -0
- package/lib/node.js +1 -1
- package/lib/publisher.js +1 -1
- package/lib/serialization.js +1 -1
- package/lib/service.js +1 -1
- package/lib/subscription.js +1 -1
- package/lib/time.js +1 -1
- package/lib/time_source.js +1 -1
- package/lib/timer.js +1 -1
- package/lib/type_description_service.js +1 -1
- package/lib/utils.js +37 -0
- package/lib/validator.js +1 -1
- package/package.json +13 -12
- package/prebuilds/linux-arm64/humble-jammy-arm64-rclnodejs.node +0 -0
- package/prebuilds/linux-arm64/jazzy-noble-arm64-rclnodejs.node +0 -0
- package/prebuilds/linux-arm64/kilted-noble-arm64-rclnodejs.node +0 -0
- package/prebuilds/linux-x64/humble-jammy-x64-rclnodejs.node +0 -0
- package/prebuilds/linux-x64/jazzy-noble-x64-rclnodejs.node +0 -0
- package/prebuilds/linux-x64/kilted-noble-x64-rclnodejs.node +0 -0
- package/rosidl_gen/deallocator.js +1 -1
- package/rosidl_gen/generate_worker.js +63 -0
- package/rosidl_gen/generator.json +1 -1
- package/rosidl_gen/index.js +31 -0
- package/rosidl_gen/primitive_types.js +2 -2
- package/rosidl_gen/templates/message.dot +2 -2
- package/scripts/install.js +113 -0
- package/scripts/tag_prebuilds.js +70 -0
- package/src/addon.cpp +3 -0
- package/third_party/ref-napi/index.js +15 -0
- package/third_party/ref-napi/lib/ref.js +1741 -0
- package/third_party/ref-napi/src/ref_napi_bindings.cpp +736 -0
- package/third_party/ref-napi/src/ref_napi_bindings.h +26 -0
|
@@ -0,0 +1,1741 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const assert = require('assert');
|
|
3
|
+
const inspect = require('util').inspect;
|
|
4
|
+
const debug = require('debug')('ref');
|
|
5
|
+
const os = require('os');
|
|
6
|
+
const addon = require('../../../lib/native_loader.js');
|
|
7
|
+
|
|
8
|
+
if (!addon || !addon.ref) {
|
|
9
|
+
throw new Error('Failed to load ref bindings from rclnodejs addon');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
exports = module.exports = addon.ref;
|
|
13
|
+
|
|
14
|
+
exports.endianness = os.endianness();
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* A `Buffer` that references the C NULL pointer. That is, its memory address
|
|
18
|
+
* points to 0. Its `length` is 0 because accessing any data from this buffer
|
|
19
|
+
* would cause a _segmentation fault_.
|
|
20
|
+
*
|
|
21
|
+
* ```
|
|
22
|
+
* console.log(ref.NULL);
|
|
23
|
+
* <SlowBuffer@0x0 >
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @name NULL
|
|
27
|
+
* @type Buffer
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A string that represents the native endianness of the machine's processor.
|
|
32
|
+
* The possible values are either `"LE"` or `"BE"`.
|
|
33
|
+
*
|
|
34
|
+
* ```
|
|
35
|
+
* console.log(ref.endianness);
|
|
36
|
+
* 'LE'
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @name endianness
|
|
40
|
+
* @type String
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Accepts a `Buffer` instance and returns the memory address of the buffer
|
|
45
|
+
* instance. Returns a JavaScript Number, which can't hold 64-bit integers,
|
|
46
|
+
* so this function is unsafe on 64-bit systems.
|
|
47
|
+
* ```
|
|
48
|
+
* console.log(ref.address(new Buffer(1)));
|
|
49
|
+
* 4320233616
|
|
50
|
+
*
|
|
51
|
+
* console.log(ref.address(ref.NULL)));
|
|
52
|
+
* 0
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @param {Buffer} buffer The buffer to get the memory address of.
|
|
56
|
+
* @return {Number} The memory address the buffer instance.
|
|
57
|
+
* @name address
|
|
58
|
+
* @type method
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Accepts a `Buffer` instance and returns _true_ if the buffer represents the
|
|
63
|
+
* NULL pointer, _false_ otherwise.
|
|
64
|
+
*
|
|
65
|
+
* ```
|
|
66
|
+
* console.log(ref.isNull(new Buffer(1)));
|
|
67
|
+
* false
|
|
68
|
+
*
|
|
69
|
+
* console.log(ref.isNull(ref.NULL));
|
|
70
|
+
* true
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* @param {Buffer} buffer The buffer to check for NULL.
|
|
74
|
+
* @return {Boolean} true or false.
|
|
75
|
+
* @name isNull
|
|
76
|
+
* @type method
|
|
77
|
+
*/
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Reads a JavaScript Object that has previously been written to the given
|
|
81
|
+
* _buffer_ at the given _offset_.
|
|
82
|
+
*
|
|
83
|
+
* ```
|
|
84
|
+
* var obj = { foo: 'bar' };
|
|
85
|
+
* var buf = ref.alloc('Object', obj);
|
|
86
|
+
*
|
|
87
|
+
* var obj2 = ref.readObject(buf, 0);
|
|
88
|
+
* console.log(obj === obj2);
|
|
89
|
+
* true
|
|
90
|
+
* ```
|
|
91
|
+
*
|
|
92
|
+
* @param {Buffer} buffer The buffer to read an Object from.
|
|
93
|
+
* @param {Number} offset The offset to begin reading from.
|
|
94
|
+
* @return {Object} The Object that was read from _buffer_.
|
|
95
|
+
* @name readObject
|
|
96
|
+
* @type method
|
|
97
|
+
*/
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Reads a Buffer instance from the given _buffer_ at the given _offset_.
|
|
101
|
+
* The _size_ parameter specifies the `length` of the returned Buffer instance,
|
|
102
|
+
* which defaults to __0__.
|
|
103
|
+
*
|
|
104
|
+
* ```
|
|
105
|
+
* var buf = new Buffer('hello world');
|
|
106
|
+
* var pointer = ref.alloc('pointer', buf);
|
|
107
|
+
*
|
|
108
|
+
* var buf2 = ref.readPointer(pointer, 0, buf.length);
|
|
109
|
+
* console.log(buf2.toString());
|
|
110
|
+
* 'hello world'
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
* @param {Buffer} buffer The buffer to read a Buffer from.
|
|
114
|
+
* @param {Number} offset The offset to begin reading from.
|
|
115
|
+
* @param {Number} length (optional) The length of the returned Buffer. Defaults to 0.
|
|
116
|
+
* @return {Buffer} The Buffer instance that was read from _buffer_.
|
|
117
|
+
* @name readPointer
|
|
118
|
+
* @type method
|
|
119
|
+
*/
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Returns a JavaScript String read from _buffer_ at the given _offset_. The
|
|
123
|
+
* C String is read until the first NULL byte, which indicates the end of the
|
|
124
|
+
* String.
|
|
125
|
+
*
|
|
126
|
+
* This function can read beyond the `length` of a Buffer.
|
|
127
|
+
*
|
|
128
|
+
* ```
|
|
129
|
+
* var buf = new Buffer('hello\0world\0');
|
|
130
|
+
*
|
|
131
|
+
* var str = ref.readCString(buf, 0);
|
|
132
|
+
* console.log(str);
|
|
133
|
+
* 'hello'
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* @param {Buffer} buffer The buffer to read a Buffer from.
|
|
137
|
+
* @param {Number} offset The offset to begin reading from.
|
|
138
|
+
* @return {String} The String that was read from _buffer_.
|
|
139
|
+
* @name readCString
|
|
140
|
+
* @type method
|
|
141
|
+
*/
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Returns a big-endian signed 64-bit int read from _buffer_ at the given
|
|
145
|
+
* _offset_.
|
|
146
|
+
*
|
|
147
|
+
* If the returned value will fit inside a JavaScript Number without losing
|
|
148
|
+
* precision, then a Number is returned, otherwise a String is returned.
|
|
149
|
+
*
|
|
150
|
+
* ```
|
|
151
|
+
* var buf = ref.alloc('int64');
|
|
152
|
+
* ref.writeInt64BE(buf, 0, '9223372036854775807');
|
|
153
|
+
*
|
|
154
|
+
* var val = ref.readInt64BE(buf, 0)
|
|
155
|
+
* console.log(val)
|
|
156
|
+
* '9223372036854775807'
|
|
157
|
+
* ```
|
|
158
|
+
*
|
|
159
|
+
* @param {Buffer} buffer The buffer to read a Buffer from.
|
|
160
|
+
* @param {Number} offset The offset to begin reading from.
|
|
161
|
+
* @return {Number|String} The Number or String that was read from _buffer_.
|
|
162
|
+
* @name readInt64BE
|
|
163
|
+
* @type method
|
|
164
|
+
*/
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Returns a little-endian signed 64-bit int read from _buffer_ at the given
|
|
168
|
+
* _offset_.
|
|
169
|
+
*
|
|
170
|
+
* If the returned value will fit inside a JavaScript Number without losing
|
|
171
|
+
* precision, then a Number is returned, otherwise a String is returned.
|
|
172
|
+
*
|
|
173
|
+
* ```
|
|
174
|
+
* var buf = ref.alloc('int64');
|
|
175
|
+
* ref.writeInt64LE(buf, 0, '9223372036854775807');
|
|
176
|
+
*
|
|
177
|
+
* var val = ref.readInt64LE(buf, 0)
|
|
178
|
+
* console.log(val)
|
|
179
|
+
* '9223372036854775807'
|
|
180
|
+
* ```
|
|
181
|
+
*
|
|
182
|
+
* @param {Buffer} buffer The buffer to read a Buffer from.
|
|
183
|
+
* @param {Number} offset The offset to begin reading from.
|
|
184
|
+
* @return {Number|String} The Number or String that was read from _buffer_.
|
|
185
|
+
* @name readInt64LE
|
|
186
|
+
* @type method
|
|
187
|
+
*/
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Returns a big-endian unsigned 64-bit int read from _buffer_ at the given
|
|
191
|
+
* _offset_.
|
|
192
|
+
*
|
|
193
|
+
* If the returned value will fit inside a JavaScript Number without losing
|
|
194
|
+
* precision, then a Number is returned, otherwise a String is returned.
|
|
195
|
+
*
|
|
196
|
+
* ```
|
|
197
|
+
* var buf = ref.alloc('uint64');
|
|
198
|
+
* ref.writeUInt64BE(buf, 0, '18446744073709551615');
|
|
199
|
+
*
|
|
200
|
+
* var val = ref.readUInt64BE(buf, 0)
|
|
201
|
+
* console.log(val)
|
|
202
|
+
* '18446744073709551615'
|
|
203
|
+
* ```
|
|
204
|
+
*
|
|
205
|
+
* @param {Buffer} buffer The buffer to read a Buffer from.
|
|
206
|
+
* @param {Number} offset The offset to begin reading from.
|
|
207
|
+
* @return {Number|String} The Number or String that was read from _buffer_.
|
|
208
|
+
* @name readUInt64BE
|
|
209
|
+
* @type method
|
|
210
|
+
*/
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Returns a little-endian unsigned 64-bit int read from _buffer_ at the given
|
|
214
|
+
* _offset_.
|
|
215
|
+
*
|
|
216
|
+
* If the returned value will fit inside a JavaScript Number without losing
|
|
217
|
+
* precision, then a Number is returned, otherwise a String is returned.
|
|
218
|
+
*
|
|
219
|
+
* ```
|
|
220
|
+
* var buf = ref.alloc('uint64');
|
|
221
|
+
* ref.writeUInt64LE(buf, 0, '18446744073709551615');
|
|
222
|
+
*
|
|
223
|
+
* var val = ref.readUInt64LE(buf, 0)
|
|
224
|
+
* console.log(val)
|
|
225
|
+
* '18446744073709551615'
|
|
226
|
+
* ```
|
|
227
|
+
*
|
|
228
|
+
* @param {Buffer} buffer The buffer to read a Buffer from.
|
|
229
|
+
* @param {Number} offset The offset to begin reading from.
|
|
230
|
+
* @return {Number|String} The Number or String that was read from _buffer_.
|
|
231
|
+
* @name readUInt64LE
|
|
232
|
+
* @type method
|
|
233
|
+
*/
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Writes the _input_ Number or String as a big-endian signed 64-bit int into
|
|
237
|
+
* _buffer_ at the given _offset_.
|
|
238
|
+
*
|
|
239
|
+
* ```
|
|
240
|
+
* var buf = ref.alloc('int64');
|
|
241
|
+
* ref.writeInt64BE(buf, 0, '9223372036854775807');
|
|
242
|
+
* ```
|
|
243
|
+
*
|
|
244
|
+
* @param {Buffer} buffer The buffer to write to.
|
|
245
|
+
* @param {Number} offset The offset to begin writing from.
|
|
246
|
+
* @param {Number|String} input This String or Number which gets written.
|
|
247
|
+
* @name writeInt64BE
|
|
248
|
+
* @type method
|
|
249
|
+
*/
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Writes the _input_ Number or String as a little-endian signed 64-bit int into
|
|
253
|
+
* _buffer_ at the given _offset_.
|
|
254
|
+
*
|
|
255
|
+
* ```
|
|
256
|
+
* var buf = ref.alloc('int64');
|
|
257
|
+
* ref.writeInt64LE(buf, 0, '9223372036854775807');
|
|
258
|
+
* ```
|
|
259
|
+
*
|
|
260
|
+
* @param {Buffer} buffer The buffer to write to.
|
|
261
|
+
* @param {Number} offset The offset to begin writing from.
|
|
262
|
+
* @param {Number|String} input This String or Number which gets written.
|
|
263
|
+
* @name writeInt64LE
|
|
264
|
+
* @type method
|
|
265
|
+
*/
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Writes the _input_ Number or String as a big-endian unsigned 64-bit int into
|
|
269
|
+
* _buffer_ at the given _offset_.
|
|
270
|
+
*
|
|
271
|
+
* ```
|
|
272
|
+
* var buf = ref.alloc('uint64');
|
|
273
|
+
* ref.writeUInt64BE(buf, 0, '18446744073709551615');
|
|
274
|
+
* ```
|
|
275
|
+
*
|
|
276
|
+
* @param {Buffer} buffer The buffer to write to.
|
|
277
|
+
* @param {Number} offset The offset to begin writing from.
|
|
278
|
+
* @param {Number|String} input This String or Number which gets written.
|
|
279
|
+
* @name writeUInt64BE
|
|
280
|
+
* @type method
|
|
281
|
+
*/
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Writes the _input_ Number or String as a little-endian unsigned 64-bit int
|
|
285
|
+
* into _buffer_ at the given _offset_.
|
|
286
|
+
*
|
|
287
|
+
* ```
|
|
288
|
+
* var buf = ref.alloc('uint64');
|
|
289
|
+
* ref.writeUInt64LE(buf, 0, '18446744073709551615');
|
|
290
|
+
* ```
|
|
291
|
+
*
|
|
292
|
+
* @param {Buffer} buffer The buffer to write to.
|
|
293
|
+
* @param {Number} offset The offset to begin writing from.
|
|
294
|
+
* @param {Number|String} input This String or Number which gets written.
|
|
295
|
+
* @name writeUInt64LE
|
|
296
|
+
* @type method
|
|
297
|
+
*/
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Returns a new clone of the given "type" object, with its
|
|
301
|
+
* `indirection` level incremented by **1**.
|
|
302
|
+
*
|
|
303
|
+
* Say you wanted to create a type representing a `void *`:
|
|
304
|
+
*
|
|
305
|
+
* ```
|
|
306
|
+
* var voidPtrType = ref.refType(ref.types.void);
|
|
307
|
+
* ```
|
|
308
|
+
*
|
|
309
|
+
* @param {Object|String} type The "type" object to create a reference type from. Strings get coerced first.
|
|
310
|
+
* @return {Object} The new "type" object with its `indirection` incremented by 1.
|
|
311
|
+
*/
|
|
312
|
+
|
|
313
|
+
exports.refType = function refType(type) {
|
|
314
|
+
const _type = exports.coerceType(type);
|
|
315
|
+
const rtn = Object.create(_type);
|
|
316
|
+
rtn.indirection++;
|
|
317
|
+
if (_type.name) {
|
|
318
|
+
Object.defineProperty(rtn, 'name', {
|
|
319
|
+
value: _type.name + '*',
|
|
320
|
+
configurable: true,
|
|
321
|
+
enumerable: true,
|
|
322
|
+
writable: true,
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
return rtn;
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Returns a new clone of the given "type" object, with its
|
|
330
|
+
* `indirection` level decremented by 1.
|
|
331
|
+
*
|
|
332
|
+
* @param {Object|String} type The "type" object to create a dereference type from. Strings get coerced first.
|
|
333
|
+
* @return {Object} The new "type" object with its `indirection` decremented by 1.
|
|
334
|
+
*/
|
|
335
|
+
|
|
336
|
+
exports.derefType = function derefType(type) {
|
|
337
|
+
const _type = exports.coerceType(type);
|
|
338
|
+
if (_type.indirection === 1) {
|
|
339
|
+
throw new Error("Cannot create deref'd type for type with indirection 1");
|
|
340
|
+
}
|
|
341
|
+
let rtn = Object.getPrototypeOf(_type);
|
|
342
|
+
if (rtn.indirection !== _type.indirection - 1) {
|
|
343
|
+
// slow case
|
|
344
|
+
rtn = Object.create(_type);
|
|
345
|
+
rtn.indirection--;
|
|
346
|
+
}
|
|
347
|
+
return rtn;
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Coerces a "type" object from a String or an actual "type" object. String values
|
|
352
|
+
* are looked up from the `ref.types` Object. So:
|
|
353
|
+
*
|
|
354
|
+
* * `"int"` gets coerced into `ref.types.int`.
|
|
355
|
+
* * `"int *"` gets translated into `ref.refType(ref.types.int)`
|
|
356
|
+
* * `ref.types.int` gets translated into `ref.types.int` (returns itself)
|
|
357
|
+
*
|
|
358
|
+
* Throws an Error if no valid "type" object could be determined. Most `ref`
|
|
359
|
+
* functions use this function under the hood, so anywhere a "type" object is
|
|
360
|
+
* expected, a String may be passed as well, including simply setting the
|
|
361
|
+
* `buffer.type` property.
|
|
362
|
+
*
|
|
363
|
+
* ```
|
|
364
|
+
* var type = ref.coerceType('int **');
|
|
365
|
+
*
|
|
366
|
+
* console.log(type.indirection);
|
|
367
|
+
* 3
|
|
368
|
+
* ```
|
|
369
|
+
*
|
|
370
|
+
* @param {Object|String} type The "type" Object or String to coerce.
|
|
371
|
+
* @return {Object} A "type" object
|
|
372
|
+
*/
|
|
373
|
+
|
|
374
|
+
exports.coerceType = function coerceType(type) {
|
|
375
|
+
let rtn = type;
|
|
376
|
+
if (typeof rtn === 'string') {
|
|
377
|
+
rtn = exports.types[type];
|
|
378
|
+
if (rtn) return rtn;
|
|
379
|
+
|
|
380
|
+
// strip whitespace
|
|
381
|
+
rtn = type.replace(/\s+/g, '').toLowerCase();
|
|
382
|
+
if (rtn === 'pointer') {
|
|
383
|
+
// legacy "pointer" being used :(
|
|
384
|
+
rtn = exports.refType(exports.types.void); // void *
|
|
385
|
+
} else if (rtn === 'string') {
|
|
386
|
+
rtn = exports.types.CString; // special char * type
|
|
387
|
+
} else {
|
|
388
|
+
var refCount = 0;
|
|
389
|
+
rtn = rtn.replace(/\*/g, function () {
|
|
390
|
+
refCount++;
|
|
391
|
+
return '';
|
|
392
|
+
});
|
|
393
|
+
// allow string names to be passed in
|
|
394
|
+
rtn = exports.types[rtn];
|
|
395
|
+
if (refCount > 0) {
|
|
396
|
+
if (!(rtn && 'size' in rtn && 'indirection' in rtn)) {
|
|
397
|
+
throw new TypeError(
|
|
398
|
+
'could not determine a proper "type" from: ' + inspect(type)
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
for (let i = 0; i < refCount; i++) {
|
|
402
|
+
rtn = exports.refType(rtn);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
if (!(rtn && 'size' in rtn && 'indirection' in rtn)) {
|
|
408
|
+
throw new TypeError(
|
|
409
|
+
'could not determine a proper "type" from: ' + inspect(type)
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
return rtn;
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Returns the "type" property of the given Buffer.
|
|
417
|
+
* Creates a default type for the buffer when none exists.
|
|
418
|
+
*
|
|
419
|
+
* @param {Buffer} buffer The Buffer instance to get the "type" object from.
|
|
420
|
+
* @return {Object} The "type" object from the given Buffer.
|
|
421
|
+
*/
|
|
422
|
+
|
|
423
|
+
exports.getType = function getType(buffer) {
|
|
424
|
+
if (!buffer.type) {
|
|
425
|
+
debug('WARN: no "type" found on buffer, setting default "type"', buffer);
|
|
426
|
+
buffer.type = {};
|
|
427
|
+
buffer.type.size = buffer.length;
|
|
428
|
+
buffer.type.indirection = 1;
|
|
429
|
+
buffer.type.get = function get() {
|
|
430
|
+
throw new Error('unknown "type"; cannot get()');
|
|
431
|
+
};
|
|
432
|
+
buffer.type.set = function set() {
|
|
433
|
+
throw new Error('unknown "type"; cannot set()');
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
return exports.coerceType(buffer.type);
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Calls the `get()` function of the Buffer's current "type" (or the
|
|
441
|
+
* passed in _type_ if present) at the given _offset_.
|
|
442
|
+
*
|
|
443
|
+
* This function handles checking the "indirection" level and returning a
|
|
444
|
+
* proper "dereferenced" Bufffer instance when necessary.
|
|
445
|
+
*
|
|
446
|
+
* @param {Buffer} buffer The Buffer instance to read from.
|
|
447
|
+
* @param {Number} offset (optional) The offset on the Buffer to start reading from. Defaults to 0.
|
|
448
|
+
* @param {Object|String} type (optional) The "type" object to use when reading. Defaults to calling `getType()` on the buffer.
|
|
449
|
+
* @return {?} Whatever value the "type" used when reading returns.
|
|
450
|
+
*/
|
|
451
|
+
|
|
452
|
+
exports.get = function get(buffer, offset, type) {
|
|
453
|
+
if (!offset) {
|
|
454
|
+
offset = 0;
|
|
455
|
+
}
|
|
456
|
+
if (type) {
|
|
457
|
+
type = exports.coerceType(type);
|
|
458
|
+
} else {
|
|
459
|
+
type = exports.getType(buffer);
|
|
460
|
+
}
|
|
461
|
+
debug('get(): (offset: %d)', offset, buffer);
|
|
462
|
+
assert(
|
|
463
|
+
type.indirection > 0,
|
|
464
|
+
`"indirection" level must be at least 1, saw ${type.indirection}`
|
|
465
|
+
);
|
|
466
|
+
if (type.indirection === 1) {
|
|
467
|
+
// need to check "type"
|
|
468
|
+
return type.get(buffer, offset);
|
|
469
|
+
} else {
|
|
470
|
+
// need to create a deref'd Buffer
|
|
471
|
+
const size = type.indirection === 2 ? type.size : exports.sizeof.pointer;
|
|
472
|
+
const reference = exports.readPointer(buffer, offset, size);
|
|
473
|
+
reference.type = exports.derefType(type);
|
|
474
|
+
return reference;
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Calls the `set()` function of the Buffer's current "type" (or the
|
|
480
|
+
* passed in _type_ if present) at the given _offset_.
|
|
481
|
+
*
|
|
482
|
+
* This function handles checking the "indirection" level writing a pointer rather
|
|
483
|
+
* than calling the `set()` function if the indirection is greater than 1.
|
|
484
|
+
*
|
|
485
|
+
* @param {Buffer} buffer The Buffer instance to write to.
|
|
486
|
+
* @param {Number} offset The offset on the Buffer to start writing to.
|
|
487
|
+
* @param {?} value The value to write to the Buffer instance.
|
|
488
|
+
* @param {Object|String} type (optional) The "type" object to use when reading. Defaults to calling `getType()` on the buffer.
|
|
489
|
+
*/
|
|
490
|
+
|
|
491
|
+
exports.set = function set(buffer, offset, value, type) {
|
|
492
|
+
if (!offset) {
|
|
493
|
+
offset = 0;
|
|
494
|
+
}
|
|
495
|
+
if (type) {
|
|
496
|
+
type = exports.coerceType(type);
|
|
497
|
+
} else {
|
|
498
|
+
type = exports.getType(buffer);
|
|
499
|
+
}
|
|
500
|
+
debug('set(): (offset: %d)', offset, buffer, value);
|
|
501
|
+
assert(type.indirection >= 1, '"indirection" level must be at least 1');
|
|
502
|
+
if (type.indirection === 1) {
|
|
503
|
+
type.set(buffer, offset, value);
|
|
504
|
+
} else {
|
|
505
|
+
exports.writePointer(buffer, offset, value);
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* Returns a new Buffer instance big enough to hold `type`,
|
|
511
|
+
* with the given `value` written to it.
|
|
512
|
+
*
|
|
513
|
+
* ``` js
|
|
514
|
+
* var intBuf = ref.alloc(ref.types.int)
|
|
515
|
+
* var int_with_4 = ref.alloc(ref.types.int, 4)
|
|
516
|
+
* ```
|
|
517
|
+
*
|
|
518
|
+
* @param {Object|String} type The "type" object to allocate. Strings get coerced first.
|
|
519
|
+
* @param {?} value (optional) The initial value set on the returned Buffer, using _type_'s `set()` function.
|
|
520
|
+
* @return {Buffer} A new Buffer instance with it's `type` set to "type", and (optionally) "value" written to it.
|
|
521
|
+
*/
|
|
522
|
+
|
|
523
|
+
exports.alloc = function alloc(_type, value) {
|
|
524
|
+
var type = exports.coerceType(_type);
|
|
525
|
+
debug('allocating Buffer for type with "size"', type.size);
|
|
526
|
+
let size;
|
|
527
|
+
if (type.indirection === 1) {
|
|
528
|
+
size = type.size;
|
|
529
|
+
} else {
|
|
530
|
+
size = exports.sizeof.pointer;
|
|
531
|
+
}
|
|
532
|
+
const buffer = Buffer.alloc(size);
|
|
533
|
+
buffer.type = type;
|
|
534
|
+
if (arguments.length >= 2) {
|
|
535
|
+
debug('setting value on allocated buffer', value);
|
|
536
|
+
exports.set(buffer, 0, value, type);
|
|
537
|
+
}
|
|
538
|
+
return buffer;
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Returns a new `Buffer` instance with the given String written to it with the
|
|
543
|
+
* given encoding (defaults to __'utf8'__). The buffer is 1 byte longer than the
|
|
544
|
+
* string itself, and is NUL terminated.
|
|
545
|
+
*
|
|
546
|
+
* ```
|
|
547
|
+
* var buf = ref.allocCString('hello world');
|
|
548
|
+
*
|
|
549
|
+
* console.log(buf.toString());
|
|
550
|
+
* 'hello world\u0000'
|
|
551
|
+
* ```
|
|
552
|
+
*
|
|
553
|
+
* @param {String} string The JavaScript string to be converted to a C string.
|
|
554
|
+
* @param {String} encoding (optional) The encoding to use for the C string. Defaults to __'utf8'__.
|
|
555
|
+
* @return {Buffer} The new `Buffer` instance with the specified String written to it, and a trailing NULL byte.
|
|
556
|
+
*/
|
|
557
|
+
|
|
558
|
+
exports.allocCString = function allocCString(string, encoding) {
|
|
559
|
+
if (string == null || exports.isNull(string)) {
|
|
560
|
+
return exports.NULL;
|
|
561
|
+
}
|
|
562
|
+
const size = Buffer.byteLength(string, encoding) + 1;
|
|
563
|
+
const buffer = Buffer.allocUnsafe(size);
|
|
564
|
+
exports.writeCString(buffer, 0, string, encoding);
|
|
565
|
+
buffer.type = charPtrType;
|
|
566
|
+
return buffer;
|
|
567
|
+
};
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
* Writes the given string as a C String (NULL terminated) to the given buffer
|
|
571
|
+
* at the given offset. "encoding" is optional and defaults to __'utf8'__.
|
|
572
|
+
*
|
|
573
|
+
* Unlike `readCString()`, this function requires the buffer to actually have the
|
|
574
|
+
* proper length.
|
|
575
|
+
*
|
|
576
|
+
* @param {Buffer} buffer The Buffer instance to write to.
|
|
577
|
+
* @param {Number} offset The offset of the buffer to begin writing at.
|
|
578
|
+
* @param {String} string The JavaScript String to write that will be written to the buffer.
|
|
579
|
+
* @param {String} encoding (optional) The encoding to read the C string as. Defaults to __'utf8'__.
|
|
580
|
+
*/
|
|
581
|
+
|
|
582
|
+
exports.writeCString = function writeCString(buffer, offset, string, encoding) {
|
|
583
|
+
assert(Buffer.isBuffer(buffer), 'expected a Buffer as the first argument');
|
|
584
|
+
assert.strictEqual(
|
|
585
|
+
'string',
|
|
586
|
+
typeof string,
|
|
587
|
+
'expected a "string" as the third argument'
|
|
588
|
+
);
|
|
589
|
+
if (!offset) {
|
|
590
|
+
offset = 0;
|
|
591
|
+
}
|
|
592
|
+
if (!encoding) {
|
|
593
|
+
encoding = 'utf8';
|
|
594
|
+
}
|
|
595
|
+
const size = buffer.length - offset - 1;
|
|
596
|
+
const len = buffer.write(string, offset, size, encoding);
|
|
597
|
+
buffer.writeUInt8(0, offset + len); // NUL terminate
|
|
598
|
+
};
|
|
599
|
+
|
|
600
|
+
exports['readInt64' + exports.endianness] = exports.readInt64;
|
|
601
|
+
exports['readUInt64' + exports.endianness] = exports.readUInt64;
|
|
602
|
+
exports['writeInt64' + exports.endianness] = exports.writeInt64;
|
|
603
|
+
exports['writeUInt64' + exports.endianness] = exports.writeUInt64;
|
|
604
|
+
|
|
605
|
+
var opposite = exports.endianness == 'LE' ? 'BE' : 'LE';
|
|
606
|
+
var int64temp = Buffer.alloc(exports.sizeof.int64);
|
|
607
|
+
var uint64temp = Buffer.alloc(exports.sizeof.uint64);
|
|
608
|
+
|
|
609
|
+
exports['readInt64' + opposite] = function (buffer, offset) {
|
|
610
|
+
for (let i = 0; i < exports.sizeof.int64; i++) {
|
|
611
|
+
int64temp[i] = buffer[offset + exports.sizeof.int64 - i - 1];
|
|
612
|
+
}
|
|
613
|
+
return exports.readInt64(int64temp, 0);
|
|
614
|
+
};
|
|
615
|
+
exports['readUInt64' + opposite] = function (buffer, offset) {
|
|
616
|
+
for (let i = 0; i < exports.sizeof.uint64; i++) {
|
|
617
|
+
uint64temp[i] = buffer[offset + exports.sizeof.uint64 - i - 1];
|
|
618
|
+
}
|
|
619
|
+
return exports.readUInt64(uint64temp, 0);
|
|
620
|
+
};
|
|
621
|
+
exports['writeInt64' + opposite] = function (buffer, offset, value) {
|
|
622
|
+
exports.writeInt64(int64temp, 0, value);
|
|
623
|
+
for (let i = 0; i < exports.sizeof.int64; i++) {
|
|
624
|
+
buffer[offset + i] = int64temp[exports.sizeof.int64 - i - 1];
|
|
625
|
+
}
|
|
626
|
+
};
|
|
627
|
+
exports['writeUInt64' + opposite] = function (buffer, offset, value) {
|
|
628
|
+
exports.writeUInt64(uint64temp, 0, value);
|
|
629
|
+
for (let i = 0; i < exports.sizeof.uint64; i++) {
|
|
630
|
+
buffer[offset + i] = uint64temp[exports.sizeof.uint64 - i - 1];
|
|
631
|
+
}
|
|
632
|
+
};
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* `ref()` accepts a Buffer instance and returns a new Buffer
|
|
636
|
+
* instance that is "pointer" sized and has its data pointing to the given
|
|
637
|
+
* Buffer instance. Essentially the created Buffer is a "reference" to the
|
|
638
|
+
* original pointer, equivalent to the following C code:
|
|
639
|
+
*
|
|
640
|
+
* ``` c
|
|
641
|
+
* char *buf = buffer;
|
|
642
|
+
* char **ref = &buf;
|
|
643
|
+
* ```
|
|
644
|
+
*
|
|
645
|
+
* @param {Buffer} buffer A Buffer instance to create a reference to.
|
|
646
|
+
* @return {Buffer} A new Buffer instance pointing to _buffer_.
|
|
647
|
+
*/
|
|
648
|
+
|
|
649
|
+
exports.ref = function ref(buffer) {
|
|
650
|
+
debug('creating a reference to buffer', buffer);
|
|
651
|
+
var type = exports.refType(exports.getType(buffer));
|
|
652
|
+
return exports.alloc(type, buffer);
|
|
653
|
+
};
|
|
654
|
+
|
|
655
|
+
/**
|
|
656
|
+
* Accepts a Buffer instance and attempts to "dereference" it.
|
|
657
|
+
* That is, first it checks the `indirection` count of _buffer_'s "type", and if
|
|
658
|
+
* it's greater than __1__ then it merely returns another Buffer, but with one
|
|
659
|
+
* level less `indirection`.
|
|
660
|
+
*
|
|
661
|
+
* When _buffer_'s indirection is at __1__, then it checks for `buffer.type`
|
|
662
|
+
* which should be an Object with its own `get()` function.
|
|
663
|
+
*
|
|
664
|
+
* ```
|
|
665
|
+
* var buf = ref.alloc('int', 6);
|
|
666
|
+
*
|
|
667
|
+
* var val = ref.deref(buf);
|
|
668
|
+
* console.log(val);
|
|
669
|
+
* 6
|
|
670
|
+
* ```
|
|
671
|
+
*
|
|
672
|
+
*
|
|
673
|
+
* @param {Buffer} buffer A Buffer instance to dereference.
|
|
674
|
+
* @return {?} The returned value after dereferencing _buffer_.
|
|
675
|
+
*/
|
|
676
|
+
|
|
677
|
+
exports.deref = function deref(buffer) {
|
|
678
|
+
debug('dereferencing buffer', buffer);
|
|
679
|
+
return exports.get(buffer);
|
|
680
|
+
};
|
|
681
|
+
|
|
682
|
+
const kAttachedRefs = Symbol('attached');
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* Attaches _object_ to _buffer_ such that it prevents _object_ from being garbage
|
|
686
|
+
* collected until _buffer_ does.
|
|
687
|
+
*
|
|
688
|
+
* @param {Buffer} buffer A Buffer instance to attach _object_ to.
|
|
689
|
+
* @param {Object|Buffer} object An Object or Buffer to prevent from being garbage collected until _buffer_ does.
|
|
690
|
+
* @api private
|
|
691
|
+
*/
|
|
692
|
+
|
|
693
|
+
exports._attach = function _attach(buf, obj) {
|
|
694
|
+
if (!buf[kAttachedRefs]) {
|
|
695
|
+
buf[kAttachedRefs] = [];
|
|
696
|
+
}
|
|
697
|
+
buf[kAttachedRefs].push(obj);
|
|
698
|
+
};
|
|
699
|
+
|
|
700
|
+
/**
|
|
701
|
+
* @param {Buffer} buffer
|
|
702
|
+
* @param {Number} offset
|
|
703
|
+
* @param {Object} object
|
|
704
|
+
* @name _writeObject
|
|
705
|
+
* @api private
|
|
706
|
+
*/
|
|
707
|
+
|
|
708
|
+
/**
|
|
709
|
+
* Writes a pointer to _object_ into _buffer_ at the specified _offset.
|
|
710
|
+
*
|
|
711
|
+
* This function "attaches" _object_ to _buffer_ to prevent it from being garbage
|
|
712
|
+
* collected.
|
|
713
|
+
*
|
|
714
|
+
* ```
|
|
715
|
+
* var buf = ref.alloc('Object');
|
|
716
|
+
* ref.writeObject(buf, 0, { foo: 'bar' });
|
|
717
|
+
*
|
|
718
|
+
* ```
|
|
719
|
+
*
|
|
720
|
+
* @param {Buffer} buffer A Buffer instance to write _object_ to.
|
|
721
|
+
* @param {Number} offset The offset on the Buffer to start writing at.
|
|
722
|
+
* @param {Object} object The Object to be written into _buffer_.
|
|
723
|
+
*/
|
|
724
|
+
|
|
725
|
+
exports.writeObject = function writeObject(buf, offset, obj) {
|
|
726
|
+
debug('writing Object to buffer', buf, offset, obj);
|
|
727
|
+
exports._writeObject(buf, offset, obj);
|
|
728
|
+
exports._attach(buf, obj);
|
|
729
|
+
};
|
|
730
|
+
|
|
731
|
+
/**
|
|
732
|
+
* Same as `ref.writePointer()`, except that this version does not attach
|
|
733
|
+
* _pointer_ to _buffer_, which is potentially unsafe if the garbage collector
|
|
734
|
+
* runs.
|
|
735
|
+
*
|
|
736
|
+
* @param {Buffer} buffer A Buffer instance to write _pointer to.
|
|
737
|
+
* @param {Number} offset The offset on the Buffer to start writing at.
|
|
738
|
+
* @param {Buffer} pointer The Buffer instance whose memory address will be written to _buffer_.
|
|
739
|
+
* @name _writePointer
|
|
740
|
+
* @api private
|
|
741
|
+
*/
|
|
742
|
+
|
|
743
|
+
/**
|
|
744
|
+
* Writes the memory address of _pointer_ to _buffer_ at the specified _offset_.
|
|
745
|
+
*
|
|
746
|
+
* This function "attaches" _object_ to _buffer_ to prevent it from being garbage
|
|
747
|
+
* collected.
|
|
748
|
+
*
|
|
749
|
+
* ```
|
|
750
|
+
* var someBuffer = new Buffer('whatever');
|
|
751
|
+
* var buf = ref.alloc('pointer');
|
|
752
|
+
* ref.writePointer(buf, 0, someBuffer);
|
|
753
|
+
* ```
|
|
754
|
+
*
|
|
755
|
+
* @param {Buffer} buffer A Buffer instance to write _pointer to.
|
|
756
|
+
* @param {Number} offset The offset on the Buffer to start writing at.
|
|
757
|
+
* @param {Buffer} pointer The Buffer instance whose memory address will be written to _buffer_.
|
|
758
|
+
*/
|
|
759
|
+
|
|
760
|
+
exports.writePointer = function writePointer(buf, offset, ptr) {
|
|
761
|
+
debug('writing pointer to buffer', buf, offset, ptr);
|
|
762
|
+
// Passing true as a fourth parameter does an a stronger
|
|
763
|
+
// version of attach which ensures ptr is only collected after
|
|
764
|
+
// the finalizer for buf has run. See
|
|
765
|
+
// https://github.com/node-ffi-napi/ref-napi/issues/54
|
|
766
|
+
// for why this is necessary
|
|
767
|
+
exports._writePointer(buf, offset, ptr);
|
|
768
|
+
exports._attach(buf, ptr);
|
|
769
|
+
};
|
|
770
|
+
|
|
771
|
+
/**
|
|
772
|
+
* Same as `ref.reinterpret()`, except that this version does not attach
|
|
773
|
+
* _buffer_ to the returned Buffer, which is potentially unsafe if the
|
|
774
|
+
* garbage collector runs.
|
|
775
|
+
*
|
|
776
|
+
* @param {Buffer} buffer A Buffer instance to base the returned Buffer off of.
|
|
777
|
+
* @param {Number} size The `length` property of the returned Buffer.
|
|
778
|
+
* @param {Number} offset The offset of the Buffer to begin from.
|
|
779
|
+
* @return {Buffer} A new Buffer instance with the same memory address as _buffer_, and the requested _size_.
|
|
780
|
+
* @name _reinterpret
|
|
781
|
+
* @api private
|
|
782
|
+
*/
|
|
783
|
+
|
|
784
|
+
/**
|
|
785
|
+
* Returns a new Buffer instance with the specified _size_, with the same memory
|
|
786
|
+
* address as _buffer_.
|
|
787
|
+
*
|
|
788
|
+
* This function "attaches" _buffer_ to the returned Buffer to prevent it from
|
|
789
|
+
* being garbage collected.
|
|
790
|
+
*
|
|
791
|
+
* @param {Buffer} buffer A Buffer instance to base the returned Buffer off of.
|
|
792
|
+
* @param {Number} size The `length` property of the returned Buffer.
|
|
793
|
+
* @param {Number} offset The offset of the Buffer to begin from.
|
|
794
|
+
* @return {Buffer} A new Buffer instance with the same memory address as _buffer_, and the requested _size_.
|
|
795
|
+
*/
|
|
796
|
+
|
|
797
|
+
exports.reinterpret = function reinterpret(buffer, size, offset) {
|
|
798
|
+
debug('reinterpreting buffer to "%d" bytes', size);
|
|
799
|
+
const rtn = exports._reinterpret(buffer, size, offset || 0);
|
|
800
|
+
exports._attach(rtn, buffer);
|
|
801
|
+
return rtn;
|
|
802
|
+
};
|
|
803
|
+
|
|
804
|
+
/**
|
|
805
|
+
* Same as `ref.reinterpretUntilZeros()`, except that this version does not
|
|
806
|
+
* attach _buffer_ to the returned Buffer, which is potentially unsafe if the
|
|
807
|
+
* garbage collector runs.
|
|
808
|
+
*
|
|
809
|
+
* @param {Buffer} buffer A Buffer instance to base the returned Buffer off of.
|
|
810
|
+
* @param {Number} size The number of sequential, aligned `NULL` bytes that are required to terminate the buffer.
|
|
811
|
+
* @param {Number} offset The offset of the Buffer to begin from.
|
|
812
|
+
* @return {Buffer} A new Buffer instance with the same memory address as _buffer_, and a variable `length` that is terminated by _size_ NUL bytes.
|
|
813
|
+
* @name _reinterpretUntilZeros
|
|
814
|
+
* @api private
|
|
815
|
+
*/
|
|
816
|
+
|
|
817
|
+
/**
|
|
818
|
+
* Accepts a `Buffer` instance and a number of `NULL` bytes to read from the
|
|
819
|
+
* pointer. This function will scan past the boundary of the Buffer's `length`
|
|
820
|
+
* until it finds `size` number of aligned `NULL` bytes.
|
|
821
|
+
*
|
|
822
|
+
* This is useful for finding the end of NUL-termintated array or C string. For
|
|
823
|
+
* example, the `readCString()` function _could_ be implemented like:
|
|
824
|
+
*
|
|
825
|
+
* ```
|
|
826
|
+
* function readCString (buf) {
|
|
827
|
+
* return ref.reinterpretUntilZeros(buf, 1).toString('utf8')
|
|
828
|
+
* }
|
|
829
|
+
* ```
|
|
830
|
+
*
|
|
831
|
+
* This function "attaches" _buffer_ to the returned Buffer to prevent it from
|
|
832
|
+
* being garbage collected.
|
|
833
|
+
*
|
|
834
|
+
* @param {Buffer} buffer A Buffer instance to base the returned Buffer off of.
|
|
835
|
+
* @param {Number} size The number of sequential, aligned `NULL` bytes are required to terminate the buffer.
|
|
836
|
+
* @param {Number} offset The offset of the Buffer to begin from.
|
|
837
|
+
* @return {Buffer} A new Buffer instance with the same memory address as _buffer_, and a variable `length` that is terminated by _size_ NUL bytes.
|
|
838
|
+
*/
|
|
839
|
+
|
|
840
|
+
exports.reinterpretUntilZeros = function reinterpretUntilZeros(
|
|
841
|
+
buffer,
|
|
842
|
+
size,
|
|
843
|
+
offset
|
|
844
|
+
) {
|
|
845
|
+
debug('reinterpreting buffer to until "%d" NULL (0) bytes are found', size);
|
|
846
|
+
var rtn = exports._reinterpretUntilZeros(buffer, size, offset || 0);
|
|
847
|
+
exports._attach(rtn, buffer);
|
|
848
|
+
return rtn;
|
|
849
|
+
};
|
|
850
|
+
|
|
851
|
+
// the built-in "types"
|
|
852
|
+
const types = (exports.types = {});
|
|
853
|
+
|
|
854
|
+
/**
|
|
855
|
+
* The `void` type.
|
|
856
|
+
*
|
|
857
|
+
* @section types
|
|
858
|
+
*/
|
|
859
|
+
|
|
860
|
+
types.void = {
|
|
861
|
+
size: 0,
|
|
862
|
+
indirection: 1,
|
|
863
|
+
get: function get(buf, offset) {
|
|
864
|
+
debug('getting `void` type (returns `null`)');
|
|
865
|
+
return null;
|
|
866
|
+
},
|
|
867
|
+
set: function set(buf, offset, val) {
|
|
868
|
+
debug('setting `void` type (no-op)');
|
|
869
|
+
},
|
|
870
|
+
};
|
|
871
|
+
|
|
872
|
+
/**
|
|
873
|
+
* The `int8` type.
|
|
874
|
+
*/
|
|
875
|
+
|
|
876
|
+
types.int8 = {
|
|
877
|
+
size: exports.sizeof.int8,
|
|
878
|
+
indirection: 1,
|
|
879
|
+
get: function get(buf, offset) {
|
|
880
|
+
return buf.readInt8(offset || 0);
|
|
881
|
+
},
|
|
882
|
+
set: function set(buf, offset, val) {
|
|
883
|
+
if (typeof val === 'string') {
|
|
884
|
+
val = val.charCodeAt(0);
|
|
885
|
+
}
|
|
886
|
+
return buf.writeInt8(val, offset || 0);
|
|
887
|
+
},
|
|
888
|
+
};
|
|
889
|
+
|
|
890
|
+
/**
|
|
891
|
+
* The `uint8` type.
|
|
892
|
+
*/
|
|
893
|
+
|
|
894
|
+
types.uint8 = {
|
|
895
|
+
size: exports.sizeof.uint8,
|
|
896
|
+
indirection: 1,
|
|
897
|
+
get: function get(buf, offset) {
|
|
898
|
+
return buf.readUInt8(offset || 0);
|
|
899
|
+
},
|
|
900
|
+
set: function set(buf, offset, val) {
|
|
901
|
+
if (typeof val === 'string') {
|
|
902
|
+
val = val.charCodeAt(0);
|
|
903
|
+
}
|
|
904
|
+
return buf.writeUInt8(val, offset || 0);
|
|
905
|
+
},
|
|
906
|
+
};
|
|
907
|
+
|
|
908
|
+
/**
|
|
909
|
+
* The `int16` type.
|
|
910
|
+
*/
|
|
911
|
+
|
|
912
|
+
types.int16 = {
|
|
913
|
+
size: exports.sizeof.int16,
|
|
914
|
+
indirection: 1,
|
|
915
|
+
get: function get(buf, offset) {
|
|
916
|
+
return Buffer.isBuffer(buf)
|
|
917
|
+
? buf['readInt16' + exports.endianness](offset || 0)
|
|
918
|
+
: buf.readInt16(offset || 0);
|
|
919
|
+
},
|
|
920
|
+
set: function set(buf, offset, val) {
|
|
921
|
+
return buf['writeInt16' + exports.endianness](val, offset || 0);
|
|
922
|
+
},
|
|
923
|
+
};
|
|
924
|
+
|
|
925
|
+
/**
|
|
926
|
+
* The `uint16` type.
|
|
927
|
+
*/
|
|
928
|
+
|
|
929
|
+
types.uint16 = {
|
|
930
|
+
size: exports.sizeof.uint16,
|
|
931
|
+
indirection: 1,
|
|
932
|
+
get: function get(buf, offset) {
|
|
933
|
+
return Buffer.isBuffer(buf)
|
|
934
|
+
? buf['readUInt16' + exports.endianness](offset || 0)
|
|
935
|
+
: buf.readUInt16(offset || 0);
|
|
936
|
+
},
|
|
937
|
+
set: function set(buf, offset, val) {
|
|
938
|
+
return buf['writeUInt16' + exports.endianness](val, offset || 0);
|
|
939
|
+
},
|
|
940
|
+
};
|
|
941
|
+
|
|
942
|
+
/**
|
|
943
|
+
* The `int32` type.
|
|
944
|
+
*/
|
|
945
|
+
|
|
946
|
+
types.int32 = {
|
|
947
|
+
size: exports.sizeof.int32,
|
|
948
|
+
indirection: 1,
|
|
949
|
+
get: function get(buf, offset) {
|
|
950
|
+
return exports.readInt32(buf, offset || 0);
|
|
951
|
+
},
|
|
952
|
+
set: function set(buf, offset, val) {
|
|
953
|
+
return exports.writeInt32(buf, offset || 0, val);
|
|
954
|
+
},
|
|
955
|
+
};
|
|
956
|
+
|
|
957
|
+
/**
|
|
958
|
+
* The `uint32` type.
|
|
959
|
+
*/
|
|
960
|
+
|
|
961
|
+
types.uint32 = {
|
|
962
|
+
size: exports.sizeof.uint32,
|
|
963
|
+
indirection: 1,
|
|
964
|
+
get: function get(buf, offset) {
|
|
965
|
+
return Buffer.isBuffer(buf)
|
|
966
|
+
? buf['readUInt32' + exports.endianness](offset || 0)
|
|
967
|
+
: buf.readUInt32(offset || 0);
|
|
968
|
+
},
|
|
969
|
+
set: function set(buf, offset, val) {
|
|
970
|
+
return buf['writeUInt32' + exports.endianness](val, offset || 0);
|
|
971
|
+
},
|
|
972
|
+
};
|
|
973
|
+
|
|
974
|
+
/**
|
|
975
|
+
* The `int64` type.
|
|
976
|
+
*/
|
|
977
|
+
|
|
978
|
+
types.int64 = {
|
|
979
|
+
size: exports.sizeof.int64,
|
|
980
|
+
indirection: 1,
|
|
981
|
+
get: function get(buf, offset) {
|
|
982
|
+
return buf['readInt64' + exports.endianness](offset || 0);
|
|
983
|
+
},
|
|
984
|
+
set: function set(buf, offset, val) {
|
|
985
|
+
return buf['writeInt64' + exports.endianness](val, offset || 0);
|
|
986
|
+
},
|
|
987
|
+
};
|
|
988
|
+
|
|
989
|
+
/**
|
|
990
|
+
* The `uint64` type.
|
|
991
|
+
*/
|
|
992
|
+
|
|
993
|
+
types.uint64 = {
|
|
994
|
+
size: exports.sizeof.uint64,
|
|
995
|
+
indirection: 1,
|
|
996
|
+
get: function get(buf, offset) {
|
|
997
|
+
return buf['readUInt64' + exports.endianness](offset || 0);
|
|
998
|
+
},
|
|
999
|
+
set: function set(buf, offset, val) {
|
|
1000
|
+
return buf['writeUInt64' + exports.endianness](val, offset || 0);
|
|
1001
|
+
},
|
|
1002
|
+
};
|
|
1003
|
+
|
|
1004
|
+
/**
|
|
1005
|
+
* The `float` type.
|
|
1006
|
+
*/
|
|
1007
|
+
|
|
1008
|
+
types.float = {
|
|
1009
|
+
size: exports.sizeof.float,
|
|
1010
|
+
indirection: 1,
|
|
1011
|
+
get: function get(buf, offset) {
|
|
1012
|
+
return Buffer.isBuffer(buf)
|
|
1013
|
+
? buf['readFloat' + exports.endianness](offset || 0)
|
|
1014
|
+
: buf.readFloat(offset || 0);
|
|
1015
|
+
},
|
|
1016
|
+
set: function set(buf, offset, val) {
|
|
1017
|
+
return buf['writeFloat' + exports.endianness](val, offset || 0);
|
|
1018
|
+
},
|
|
1019
|
+
};
|
|
1020
|
+
|
|
1021
|
+
/**
|
|
1022
|
+
* The `double` type.
|
|
1023
|
+
*/
|
|
1024
|
+
|
|
1025
|
+
types.double = {
|
|
1026
|
+
size: exports.sizeof.double,
|
|
1027
|
+
indirection: 1,
|
|
1028
|
+
get: function get(buf, offset) {
|
|
1029
|
+
return Buffer.isBuffer(buf)
|
|
1030
|
+
? buf['readDouble' + exports.endianness](offset || 0)
|
|
1031
|
+
: buf.readDouble(offset || 0);
|
|
1032
|
+
},
|
|
1033
|
+
set: function set(buf, offset, val) {
|
|
1034
|
+
return buf['writeDouble' + exports.endianness](val, offset || 0);
|
|
1035
|
+
},
|
|
1036
|
+
};
|
|
1037
|
+
|
|
1038
|
+
/**
|
|
1039
|
+
* The `Object` type. This can be used to read/write regular JS Objects
|
|
1040
|
+
* into raw memory.
|
|
1041
|
+
*/
|
|
1042
|
+
|
|
1043
|
+
types.Object = {
|
|
1044
|
+
size: exports.sizeof.Object,
|
|
1045
|
+
indirection: 1,
|
|
1046
|
+
get: function get(buf, offset) {
|
|
1047
|
+
return buf.readObject(offset || 0);
|
|
1048
|
+
},
|
|
1049
|
+
set: function set(buf, offset, val) {
|
|
1050
|
+
return buf.writeObject(val, offset || 0);
|
|
1051
|
+
},
|
|
1052
|
+
};
|
|
1053
|
+
|
|
1054
|
+
/**
|
|
1055
|
+
* The `CString` (a.k.a `"string"`) type.
|
|
1056
|
+
*
|
|
1057
|
+
* CStrings are a kind of weird thing. We say it's `sizeof(char *)`, and
|
|
1058
|
+
* `indirection` level of 1, which means that we have to return a Buffer that
|
|
1059
|
+
* is pointer sized, and points to a some utf8 string data, so we have to create
|
|
1060
|
+
* a 2nd "in-between" buffer.
|
|
1061
|
+
*/
|
|
1062
|
+
|
|
1063
|
+
types.CString = {
|
|
1064
|
+
size: exports.sizeof.pointer,
|
|
1065
|
+
alignment: exports.alignof.pointer,
|
|
1066
|
+
indirection: 1,
|
|
1067
|
+
get: function get(buf, offset) {
|
|
1068
|
+
const _buf = exports.readPointer(buf, offset);
|
|
1069
|
+
if (exports.isNull(_buf)) {
|
|
1070
|
+
return null;
|
|
1071
|
+
}
|
|
1072
|
+
return exports.readCString(_buf, 0);
|
|
1073
|
+
},
|
|
1074
|
+
set: function set(buf, offset, val) {
|
|
1075
|
+
let _buf;
|
|
1076
|
+
if (Buffer.isBuffer(val)) {
|
|
1077
|
+
_buf = val;
|
|
1078
|
+
} else {
|
|
1079
|
+
// assume string
|
|
1080
|
+
_buf = exports.allocCString(val);
|
|
1081
|
+
}
|
|
1082
|
+
return exports.writePointer(buf, offset, _buf);
|
|
1083
|
+
},
|
|
1084
|
+
};
|
|
1085
|
+
|
|
1086
|
+
// alias Utf8String
|
|
1087
|
+
var utfstringwarned = false;
|
|
1088
|
+
Object.defineProperty(types, 'Utf8String', {
|
|
1089
|
+
enumerable: false,
|
|
1090
|
+
configurable: true,
|
|
1091
|
+
get: function () {
|
|
1092
|
+
if (!utfstringwarned) {
|
|
1093
|
+
utfstringwarned = true;
|
|
1094
|
+
console.error('"Utf8String" type is deprecated, use "CString" instead');
|
|
1095
|
+
}
|
|
1096
|
+
return types.CString;
|
|
1097
|
+
},
|
|
1098
|
+
});
|
|
1099
|
+
|
|
1100
|
+
/**
|
|
1101
|
+
* The `bool` type.
|
|
1102
|
+
*
|
|
1103
|
+
* Wrapper type around `types.uint8` that accepts/returns `true` or
|
|
1104
|
+
* `false` Boolean JavaScript values.
|
|
1105
|
+
*
|
|
1106
|
+
* @name bool
|
|
1107
|
+
*
|
|
1108
|
+
*/
|
|
1109
|
+
|
|
1110
|
+
/**
|
|
1111
|
+
* The `byte` type.
|
|
1112
|
+
*
|
|
1113
|
+
* @name byte
|
|
1114
|
+
*/
|
|
1115
|
+
|
|
1116
|
+
/**
|
|
1117
|
+
* The `char` type.
|
|
1118
|
+
*
|
|
1119
|
+
* @name char
|
|
1120
|
+
*/
|
|
1121
|
+
|
|
1122
|
+
/**
|
|
1123
|
+
* The `uchar` type.
|
|
1124
|
+
*
|
|
1125
|
+
* @name uchar
|
|
1126
|
+
*/
|
|
1127
|
+
|
|
1128
|
+
/**
|
|
1129
|
+
* The `short` type.
|
|
1130
|
+
*
|
|
1131
|
+
* @name short
|
|
1132
|
+
*/
|
|
1133
|
+
|
|
1134
|
+
/**
|
|
1135
|
+
* The `ushort` type.
|
|
1136
|
+
*
|
|
1137
|
+
* @name ushort
|
|
1138
|
+
*/
|
|
1139
|
+
|
|
1140
|
+
/**
|
|
1141
|
+
* The `int` type.
|
|
1142
|
+
*
|
|
1143
|
+
* @name int
|
|
1144
|
+
*/
|
|
1145
|
+
|
|
1146
|
+
/**
|
|
1147
|
+
* The `uint` type.
|
|
1148
|
+
*
|
|
1149
|
+
* @name uint
|
|
1150
|
+
*/
|
|
1151
|
+
|
|
1152
|
+
/**
|
|
1153
|
+
* The `long` type.
|
|
1154
|
+
*
|
|
1155
|
+
* @name long
|
|
1156
|
+
*/
|
|
1157
|
+
|
|
1158
|
+
/**
|
|
1159
|
+
* The `ulong` type.
|
|
1160
|
+
*
|
|
1161
|
+
* @name ulong
|
|
1162
|
+
*/
|
|
1163
|
+
|
|
1164
|
+
/**
|
|
1165
|
+
* The `longlong` type.
|
|
1166
|
+
*
|
|
1167
|
+
* @name longlong
|
|
1168
|
+
*/
|
|
1169
|
+
|
|
1170
|
+
/**
|
|
1171
|
+
* The `ulonglong` type.
|
|
1172
|
+
*
|
|
1173
|
+
* @name ulonglong
|
|
1174
|
+
*/
|
|
1175
|
+
|
|
1176
|
+
/**
|
|
1177
|
+
* The `size_t` type.
|
|
1178
|
+
*
|
|
1179
|
+
* @name size_t
|
|
1180
|
+
*/
|
|
1181
|
+
|
|
1182
|
+
// "typedef"s for the variable-sized types
|
|
1183
|
+
[
|
|
1184
|
+
'bool',
|
|
1185
|
+
'byte',
|
|
1186
|
+
'char',
|
|
1187
|
+
'uchar',
|
|
1188
|
+
'short',
|
|
1189
|
+
'ushort',
|
|
1190
|
+
'int',
|
|
1191
|
+
'uint',
|
|
1192
|
+
'long',
|
|
1193
|
+
'ulong',
|
|
1194
|
+
'longlong',
|
|
1195
|
+
'ulonglong',
|
|
1196
|
+
'size_t',
|
|
1197
|
+
].forEach((name) => {
|
|
1198
|
+
const unsigned =
|
|
1199
|
+
name === 'bool' || name === 'byte' || name === 'size_t' || name[0] === 'u';
|
|
1200
|
+
const size = exports.sizeof[name];
|
|
1201
|
+
assert(size >= 1 && size <= 8);
|
|
1202
|
+
let typeName = 'int' + size * 8;
|
|
1203
|
+
if (unsigned) {
|
|
1204
|
+
typeName = 'u' + typeName;
|
|
1205
|
+
}
|
|
1206
|
+
const type = exports.types[typeName];
|
|
1207
|
+
assert(type);
|
|
1208
|
+
exports.types[name] = Object.create(type);
|
|
1209
|
+
});
|
|
1210
|
+
|
|
1211
|
+
// set the "alignment" property on the built-in types
|
|
1212
|
+
Object.keys(exports.alignof).forEach((name) => {
|
|
1213
|
+
if (name === 'pointer') return;
|
|
1214
|
+
exports.types[name].alignment = exports.alignof[name];
|
|
1215
|
+
assert(exports.types[name].alignment > 0);
|
|
1216
|
+
});
|
|
1217
|
+
|
|
1218
|
+
// make the `bool` type work with JS true/false values
|
|
1219
|
+
exports.types.bool.get = (function (_get) {
|
|
1220
|
+
return function get(buf, offset) {
|
|
1221
|
+
return _get(buf, offset) ? true : false;
|
|
1222
|
+
};
|
|
1223
|
+
})(exports.types.bool.get);
|
|
1224
|
+
exports.types.bool.set = (function (_set) {
|
|
1225
|
+
return function set(buf, offset, val) {
|
|
1226
|
+
if (typeof val !== 'number') {
|
|
1227
|
+
val = val ? 1 : 0;
|
|
1228
|
+
}
|
|
1229
|
+
return _set(buf, offset, val);
|
|
1230
|
+
};
|
|
1231
|
+
})(exports.types.bool.set);
|
|
1232
|
+
|
|
1233
|
+
/*!
|
|
1234
|
+
* Set the `name` property of the types. Used for debugging...
|
|
1235
|
+
*/
|
|
1236
|
+
|
|
1237
|
+
Object.keys(exports.types).forEach((name) => {
|
|
1238
|
+
exports.types[name].name = name;
|
|
1239
|
+
});
|
|
1240
|
+
|
|
1241
|
+
/*!
|
|
1242
|
+
* This `char *` type is used by "allocCString()" above.
|
|
1243
|
+
*/
|
|
1244
|
+
|
|
1245
|
+
const charPtrType = exports.refType(exports.types.char);
|
|
1246
|
+
|
|
1247
|
+
/*!
|
|
1248
|
+
* Set the `type` property of the `NULL` pointer Buffer object.
|
|
1249
|
+
*/
|
|
1250
|
+
|
|
1251
|
+
exports.NULL.type = exports.types.void;
|
|
1252
|
+
|
|
1253
|
+
/**
|
|
1254
|
+
* `NULL_POINTER` is a pointer-sized `Buffer` instance pointing to `NULL`.
|
|
1255
|
+
* Conceptually, it's equivalent to the following C code:
|
|
1256
|
+
*
|
|
1257
|
+
* ``` c
|
|
1258
|
+
* char *null_pointer;
|
|
1259
|
+
* null_pointer = NULL;
|
|
1260
|
+
* ```
|
|
1261
|
+
*
|
|
1262
|
+
* @type Buffer
|
|
1263
|
+
*/
|
|
1264
|
+
|
|
1265
|
+
exports.NULL_POINTER = exports.ref(exports.NULL);
|
|
1266
|
+
|
|
1267
|
+
/**
|
|
1268
|
+
* All these '...' comment blocks below are for the documentation generator.
|
|
1269
|
+
*
|
|
1270
|
+
* @section buffer
|
|
1271
|
+
*/
|
|
1272
|
+
|
|
1273
|
+
Buffer.prototype.address = function address() {
|
|
1274
|
+
return exports.address(this, 0);
|
|
1275
|
+
};
|
|
1276
|
+
|
|
1277
|
+
/**
|
|
1278
|
+
* ...
|
|
1279
|
+
*/
|
|
1280
|
+
|
|
1281
|
+
Buffer.prototype.hexAddress = function hexAddress() {
|
|
1282
|
+
return exports.hexAddress(this, 0);
|
|
1283
|
+
};
|
|
1284
|
+
|
|
1285
|
+
/**
|
|
1286
|
+
* ...
|
|
1287
|
+
*/
|
|
1288
|
+
|
|
1289
|
+
Buffer.prototype.isNull = function isNull() {
|
|
1290
|
+
return exports.isNull(this, 0);
|
|
1291
|
+
};
|
|
1292
|
+
|
|
1293
|
+
/**
|
|
1294
|
+
* ...
|
|
1295
|
+
*/
|
|
1296
|
+
|
|
1297
|
+
Buffer.prototype.ref = function ref() {
|
|
1298
|
+
return exports.ref(this);
|
|
1299
|
+
};
|
|
1300
|
+
|
|
1301
|
+
/**
|
|
1302
|
+
* ...
|
|
1303
|
+
*/
|
|
1304
|
+
|
|
1305
|
+
Buffer.prototype.deref = function deref() {
|
|
1306
|
+
return exports.deref(this);
|
|
1307
|
+
};
|
|
1308
|
+
|
|
1309
|
+
/**
|
|
1310
|
+
* ...
|
|
1311
|
+
*/
|
|
1312
|
+
|
|
1313
|
+
Buffer.prototype.readObject = function readObject(offset) {
|
|
1314
|
+
return exports.readObject(this, offset);
|
|
1315
|
+
};
|
|
1316
|
+
|
|
1317
|
+
/**
|
|
1318
|
+
* ...
|
|
1319
|
+
*/
|
|
1320
|
+
|
|
1321
|
+
Buffer.prototype.writeObject = function writeObject(obj, offset) {
|
|
1322
|
+
return exports.writeObject(this, offset, obj);
|
|
1323
|
+
};
|
|
1324
|
+
|
|
1325
|
+
/**
|
|
1326
|
+
* ...
|
|
1327
|
+
*/
|
|
1328
|
+
|
|
1329
|
+
Buffer.prototype.readPointer = function readPointer(offset, size) {
|
|
1330
|
+
return exports.readPointer(this, offset, size);
|
|
1331
|
+
};
|
|
1332
|
+
|
|
1333
|
+
/**
|
|
1334
|
+
* ...
|
|
1335
|
+
*/
|
|
1336
|
+
|
|
1337
|
+
Buffer.prototype.writePointer = function writePointer(ptr, offset) {
|
|
1338
|
+
return exports.writePointer(this, offset, ptr);
|
|
1339
|
+
};
|
|
1340
|
+
|
|
1341
|
+
/**
|
|
1342
|
+
* ...
|
|
1343
|
+
*/
|
|
1344
|
+
|
|
1345
|
+
Buffer.prototype.readCString = function readCString(offset) {
|
|
1346
|
+
return exports.readCString(this, offset);
|
|
1347
|
+
};
|
|
1348
|
+
|
|
1349
|
+
/**
|
|
1350
|
+
* ...
|
|
1351
|
+
*/
|
|
1352
|
+
|
|
1353
|
+
Buffer.prototype.writeCString = function writeCString(
|
|
1354
|
+
string,
|
|
1355
|
+
offset,
|
|
1356
|
+
encoding
|
|
1357
|
+
) {
|
|
1358
|
+
return exports.writeCString(this, offset, string, encoding);
|
|
1359
|
+
};
|
|
1360
|
+
|
|
1361
|
+
/**
|
|
1362
|
+
* ...
|
|
1363
|
+
*/
|
|
1364
|
+
|
|
1365
|
+
Buffer.prototype.readInt64BE = function readInt64BE(offset) {
|
|
1366
|
+
return exports.readInt64BE(this, offset);
|
|
1367
|
+
};
|
|
1368
|
+
|
|
1369
|
+
/**
|
|
1370
|
+
* ...
|
|
1371
|
+
*/
|
|
1372
|
+
|
|
1373
|
+
Buffer.prototype.writeInt64BE = function writeInt64BE(val, offset) {
|
|
1374
|
+
return exports.writeInt64BE(this, offset, val);
|
|
1375
|
+
};
|
|
1376
|
+
|
|
1377
|
+
/**
|
|
1378
|
+
* ...
|
|
1379
|
+
*/
|
|
1380
|
+
|
|
1381
|
+
Buffer.prototype.readUInt64BE = function readUInt64BE(offset) {
|
|
1382
|
+
return exports.readUInt64BE(this, offset);
|
|
1383
|
+
};
|
|
1384
|
+
|
|
1385
|
+
/**
|
|
1386
|
+
* ...
|
|
1387
|
+
*/
|
|
1388
|
+
|
|
1389
|
+
Buffer.prototype.writeUInt64BE = function writeUInt64BE(val, offset) {
|
|
1390
|
+
return exports.writeUInt64BE(this, offset, val);
|
|
1391
|
+
};
|
|
1392
|
+
|
|
1393
|
+
/**
|
|
1394
|
+
* ...
|
|
1395
|
+
*/
|
|
1396
|
+
|
|
1397
|
+
Buffer.prototype.readInt64LE = function readInt64LE(offset) {
|
|
1398
|
+
return exports.readInt64LE(this, offset);
|
|
1399
|
+
};
|
|
1400
|
+
|
|
1401
|
+
/**
|
|
1402
|
+
* ...
|
|
1403
|
+
*/
|
|
1404
|
+
|
|
1405
|
+
Buffer.prototype.writeInt64LE = function writeInt64LE(val, offset) {
|
|
1406
|
+
return exports.writeInt64LE(this, offset, val);
|
|
1407
|
+
};
|
|
1408
|
+
|
|
1409
|
+
/**
|
|
1410
|
+
* ...
|
|
1411
|
+
*/
|
|
1412
|
+
|
|
1413
|
+
Buffer.prototype.readUInt64LE = function readUInt64LE(offset) {
|
|
1414
|
+
return exports.readUInt64LE(this, offset);
|
|
1415
|
+
};
|
|
1416
|
+
|
|
1417
|
+
/**
|
|
1418
|
+
* ...
|
|
1419
|
+
*/
|
|
1420
|
+
|
|
1421
|
+
Buffer.prototype.writeUInt64LE = function writeUInt64LE(val, offset) {
|
|
1422
|
+
return exports.writeUInt64LE(this, offset, val);
|
|
1423
|
+
};
|
|
1424
|
+
|
|
1425
|
+
/**
|
|
1426
|
+
* ...
|
|
1427
|
+
*/
|
|
1428
|
+
|
|
1429
|
+
Buffer.prototype.reinterpret = function reinterpret(size, offset) {
|
|
1430
|
+
return exports.reinterpret(this, size, offset);
|
|
1431
|
+
};
|
|
1432
|
+
|
|
1433
|
+
/**
|
|
1434
|
+
* ...
|
|
1435
|
+
*/
|
|
1436
|
+
|
|
1437
|
+
Buffer.prototype.reinterpretUntilZeros = function reinterpretUntilZeros(
|
|
1438
|
+
size,
|
|
1439
|
+
offset
|
|
1440
|
+
) {
|
|
1441
|
+
return exports.reinterpretUntilZeros(this, size, offset);
|
|
1442
|
+
};
|
|
1443
|
+
|
|
1444
|
+
/**
|
|
1445
|
+
* `ref` overwrites the default `Buffer#inspect()` function to include the
|
|
1446
|
+
* hex-encoded memory address of the Buffer instance when invoked.
|
|
1447
|
+
*
|
|
1448
|
+
* This is simply a nice-to-have.
|
|
1449
|
+
*
|
|
1450
|
+
* **Before**:
|
|
1451
|
+
*
|
|
1452
|
+
* ``` js
|
|
1453
|
+
* console.log(new Buffer('ref'));
|
|
1454
|
+
* <Buffer 72 65 66>
|
|
1455
|
+
* ```
|
|
1456
|
+
*
|
|
1457
|
+
* **After**:
|
|
1458
|
+
*
|
|
1459
|
+
* ``` js
|
|
1460
|
+
* console.log(new Buffer('ref'));
|
|
1461
|
+
* <Buffer@0x103015490 72 65 66>
|
|
1462
|
+
* ```
|
|
1463
|
+
*/
|
|
1464
|
+
|
|
1465
|
+
var inspectSym = inspect.custom || 'inspect';
|
|
1466
|
+
/**
|
|
1467
|
+
* in node 6.91, inspect.custom does not give a correct value; so in this case, don't torch the whole process.
|
|
1468
|
+
* fixed in >6.9.2
|
|
1469
|
+
*/
|
|
1470
|
+
if (Buffer.prototype[inspectSym]) {
|
|
1471
|
+
Buffer.prototype[inspectSym] = overwriteInspect(Buffer.prototype[inspectSym]);
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
// does SlowBuffer inherit from Buffer? (node >= v0.7.9)
|
|
1475
|
+
if (!(exports.NULL instanceof Buffer)) {
|
|
1476
|
+
debug(
|
|
1477
|
+
"extending SlowBuffer's prototype since it doesn't inherit from Buffer.prototype"
|
|
1478
|
+
);
|
|
1479
|
+
|
|
1480
|
+
/*!
|
|
1481
|
+
* SlowBuffer convenience methods.
|
|
1482
|
+
*/
|
|
1483
|
+
|
|
1484
|
+
var SlowBuffer = require('buffer').SlowBuffer;
|
|
1485
|
+
|
|
1486
|
+
SlowBuffer.prototype.address = Buffer.prototype.address;
|
|
1487
|
+
SlowBuffer.prototype.hexAddress = Buffer.prototype.hexAddress;
|
|
1488
|
+
SlowBuffer.prototype.isNull = Buffer.prototype.isNull;
|
|
1489
|
+
SlowBuffer.prototype.ref = Buffer.prototype.ref;
|
|
1490
|
+
SlowBuffer.prototype.deref = Buffer.prototype.deref;
|
|
1491
|
+
SlowBuffer.prototype.readObject = Buffer.prototype.readObject;
|
|
1492
|
+
SlowBuffer.prototype.writeObject = Buffer.prototype.writeObject;
|
|
1493
|
+
SlowBuffer.prototype.readPointer = Buffer.prototype.readPointer;
|
|
1494
|
+
SlowBuffer.prototype.writePointer = Buffer.prototype.writePointer;
|
|
1495
|
+
SlowBuffer.prototype.readCString = Buffer.prototype.readCString;
|
|
1496
|
+
SlowBuffer.prototype.writeCString = Buffer.prototype.writeCString;
|
|
1497
|
+
SlowBuffer.prototype.reinterpret = Buffer.prototype.reinterpret;
|
|
1498
|
+
SlowBuffer.prototype.reinterpretUntilZeros =
|
|
1499
|
+
Buffer.prototype.reinterpretUntilZeros;
|
|
1500
|
+
SlowBuffer.prototype.readInt64BE = Buffer.prototype.readInt64BE;
|
|
1501
|
+
SlowBuffer.prototype.writeInt64BE = Buffer.prototype.writeInt64BE;
|
|
1502
|
+
SlowBuffer.prototype.readUInt64BE = Buffer.prototype.readUInt64BE;
|
|
1503
|
+
SlowBuffer.prototype.writeUInt64BE = Buffer.prototype.writeUInt64BE;
|
|
1504
|
+
SlowBuffer.prototype.readInt64LE = Buffer.prototype.readInt64LE;
|
|
1505
|
+
SlowBuffer.prototype.writeInt64LE = Buffer.prototype.writeInt64LE;
|
|
1506
|
+
SlowBuffer.prototype.readUInt64LE = Buffer.prototype.readUInt64LE;
|
|
1507
|
+
SlowBuffer.prototype.writeUInt64LE = Buffer.prototype.writeUInt64LE;
|
|
1508
|
+
/**
|
|
1509
|
+
* in node 6.9.1, inspect.custom does not give a correct value; so in this case, don't torch the whole process.
|
|
1510
|
+
* fixed in >6.9.2
|
|
1511
|
+
*/
|
|
1512
|
+
if (SlowBuffer.prototype[inspectSym]) {
|
|
1513
|
+
SlowBuffer.prototype[inspectSym] = overwriteInspect(
|
|
1514
|
+
SlowBuffer.prototype[inspectSym]
|
|
1515
|
+
);
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
|
|
1519
|
+
/**
|
|
1520
|
+
* ...
|
|
1521
|
+
*/
|
|
1522
|
+
|
|
1523
|
+
exports.PointerBuffer.prototype.hexAddress = function hexAddress() {
|
|
1524
|
+
return exports.hexAddress(this, 0);
|
|
1525
|
+
};
|
|
1526
|
+
|
|
1527
|
+
/**
|
|
1528
|
+
* ...
|
|
1529
|
+
*/
|
|
1530
|
+
|
|
1531
|
+
exports.PointerBuffer.prototype.ref = function ref() {
|
|
1532
|
+
return exports.ref(this);
|
|
1533
|
+
};
|
|
1534
|
+
|
|
1535
|
+
/**
|
|
1536
|
+
* ...
|
|
1537
|
+
*/
|
|
1538
|
+
|
|
1539
|
+
exports.PointerBuffer.prototype.deref = function deref() {
|
|
1540
|
+
return exports.deref(this);
|
|
1541
|
+
};
|
|
1542
|
+
|
|
1543
|
+
/**
|
|
1544
|
+
* ...
|
|
1545
|
+
*/
|
|
1546
|
+
|
|
1547
|
+
exports.PointerBuffer.prototype.readObject = function readObject(offset) {
|
|
1548
|
+
return exports.readObject(this, offset);
|
|
1549
|
+
};
|
|
1550
|
+
|
|
1551
|
+
/**
|
|
1552
|
+
* ...
|
|
1553
|
+
*/
|
|
1554
|
+
|
|
1555
|
+
exports.PointerBuffer.prototype.writeObject = function writeObject(
|
|
1556
|
+
obj,
|
|
1557
|
+
offset
|
|
1558
|
+
) {
|
|
1559
|
+
return exports.writeObject(this, offset, obj);
|
|
1560
|
+
};
|
|
1561
|
+
|
|
1562
|
+
/**
|
|
1563
|
+
* ...
|
|
1564
|
+
*/
|
|
1565
|
+
|
|
1566
|
+
exports.PointerBuffer.prototype.readPointer = function readPointer(
|
|
1567
|
+
offset,
|
|
1568
|
+
size
|
|
1569
|
+
) {
|
|
1570
|
+
return exports.readPointer(this, offset, size);
|
|
1571
|
+
};
|
|
1572
|
+
|
|
1573
|
+
/**
|
|
1574
|
+
* ...
|
|
1575
|
+
*/
|
|
1576
|
+
|
|
1577
|
+
exports.PointerBuffer.prototype.writePointer = function writePointer(
|
|
1578
|
+
ptr,
|
|
1579
|
+
offset
|
|
1580
|
+
) {
|
|
1581
|
+
return exports.writePointer(this, offset, ptr);
|
|
1582
|
+
};
|
|
1583
|
+
|
|
1584
|
+
/**
|
|
1585
|
+
* ...
|
|
1586
|
+
*/
|
|
1587
|
+
|
|
1588
|
+
exports.PointerBuffer.prototype.readCString = function readCString(offset) {
|
|
1589
|
+
return exports.readCString(this, offset);
|
|
1590
|
+
};
|
|
1591
|
+
|
|
1592
|
+
/**
|
|
1593
|
+
* ...
|
|
1594
|
+
*/
|
|
1595
|
+
|
|
1596
|
+
exports.PointerBuffer.prototype.writeCString = function writeCString(
|
|
1597
|
+
string,
|
|
1598
|
+
offset,
|
|
1599
|
+
encoding
|
|
1600
|
+
) {
|
|
1601
|
+
return exports.writeCString(this, offset, string, encoding);
|
|
1602
|
+
};
|
|
1603
|
+
|
|
1604
|
+
/**
|
|
1605
|
+
* ...
|
|
1606
|
+
*/
|
|
1607
|
+
|
|
1608
|
+
exports.PointerBuffer.prototype.readInt64BE = function readInt64BE(offset) {
|
|
1609
|
+
return exports.readInt64BE(this, offset);
|
|
1610
|
+
};
|
|
1611
|
+
|
|
1612
|
+
/**
|
|
1613
|
+
* ...
|
|
1614
|
+
*/
|
|
1615
|
+
|
|
1616
|
+
exports.PointerBuffer.prototype.writeInt64BE = function writeInt64BE(
|
|
1617
|
+
val,
|
|
1618
|
+
offset
|
|
1619
|
+
) {
|
|
1620
|
+
return exports.writeInt64BE(this, offset, val);
|
|
1621
|
+
};
|
|
1622
|
+
|
|
1623
|
+
/**
|
|
1624
|
+
* ...
|
|
1625
|
+
*/
|
|
1626
|
+
|
|
1627
|
+
exports.PointerBuffer.prototype.readUInt64BE = function readUInt64BE(offset) {
|
|
1628
|
+
return exports.readUInt64BE(this, offset);
|
|
1629
|
+
};
|
|
1630
|
+
|
|
1631
|
+
/**
|
|
1632
|
+
* ...
|
|
1633
|
+
*/
|
|
1634
|
+
|
|
1635
|
+
exports.PointerBuffer.prototype.writeUInt64BE = function writeUInt64BE(
|
|
1636
|
+
val,
|
|
1637
|
+
offset
|
|
1638
|
+
) {
|
|
1639
|
+
return exports.writeUInt64BE(this, offset, val);
|
|
1640
|
+
};
|
|
1641
|
+
|
|
1642
|
+
/**
|
|
1643
|
+
* ...
|
|
1644
|
+
*/
|
|
1645
|
+
|
|
1646
|
+
exports.PointerBuffer.prototype.readInt64LE = function readInt64LE(offset) {
|
|
1647
|
+
return exports.readInt64LE(this, offset);
|
|
1648
|
+
};
|
|
1649
|
+
|
|
1650
|
+
/**
|
|
1651
|
+
* ...
|
|
1652
|
+
*/
|
|
1653
|
+
|
|
1654
|
+
exports.PointerBuffer.prototype.writeInt64LE = function writeInt64LE(
|
|
1655
|
+
val,
|
|
1656
|
+
offset
|
|
1657
|
+
) {
|
|
1658
|
+
return exports.writeInt64LE(this, offset, val);
|
|
1659
|
+
};
|
|
1660
|
+
|
|
1661
|
+
/**
|
|
1662
|
+
* ...
|
|
1663
|
+
*/
|
|
1664
|
+
|
|
1665
|
+
exports.PointerBuffer.prototype.readUInt64LE = function readUInt64LE(offset) {
|
|
1666
|
+
return exports.readUInt64LE(this, offset);
|
|
1667
|
+
};
|
|
1668
|
+
|
|
1669
|
+
/**
|
|
1670
|
+
* ...
|
|
1671
|
+
*/
|
|
1672
|
+
|
|
1673
|
+
exports.PointerBuffer.prototype.writeUInt64LE = function writeUInt64LE(
|
|
1674
|
+
val,
|
|
1675
|
+
offset
|
|
1676
|
+
) {
|
|
1677
|
+
return exports.writeUInt64LE(this, offset, val);
|
|
1678
|
+
};
|
|
1679
|
+
|
|
1680
|
+
exports.PointerBuffer.prototype.readUInt32 = function readUInt32(offset) {
|
|
1681
|
+
return exports.readUInt32(this, offset);
|
|
1682
|
+
};
|
|
1683
|
+
|
|
1684
|
+
exports.PointerBuffer.prototype.readInt8 = function readInt8(offset) {
|
|
1685
|
+
return exports.readInt8(this, offset);
|
|
1686
|
+
};
|
|
1687
|
+
|
|
1688
|
+
exports.PointerBuffer.prototype.readUInt8 = function readUInt8(offset) {
|
|
1689
|
+
return exports.readUInt8(this, offset);
|
|
1690
|
+
};
|
|
1691
|
+
|
|
1692
|
+
exports.PointerBuffer.prototype.readFloat = function readFloat(offset) {
|
|
1693
|
+
return exports.readFloat(this, offset);
|
|
1694
|
+
};
|
|
1695
|
+
|
|
1696
|
+
exports.PointerBuffer.prototype.readDouble = function readDouble(offset) {
|
|
1697
|
+
return exports.readDouble(this, offset);
|
|
1698
|
+
};
|
|
1699
|
+
|
|
1700
|
+
exports.PointerBuffer.prototype.readInt16 = function readInt16(offset) {
|
|
1701
|
+
return exports.readInt16(this, offset);
|
|
1702
|
+
};
|
|
1703
|
+
|
|
1704
|
+
exports.PointerBuffer.prototype.readUInt16 = function readUInt16(offset) {
|
|
1705
|
+
return exports.readUInt16(this, offset);
|
|
1706
|
+
};
|
|
1707
|
+
|
|
1708
|
+
/**
|
|
1709
|
+
* ...
|
|
1710
|
+
*/
|
|
1711
|
+
|
|
1712
|
+
exports.PointerBuffer.prototype.reinterpret = function reinterpret(
|
|
1713
|
+
size,
|
|
1714
|
+
offset
|
|
1715
|
+
) {
|
|
1716
|
+
return exports.reinterpret(this, size, offset);
|
|
1717
|
+
};
|
|
1718
|
+
|
|
1719
|
+
/**
|
|
1720
|
+
* ...
|
|
1721
|
+
*/
|
|
1722
|
+
|
|
1723
|
+
exports.PointerBuffer.prototype.reinterpretUntilZeros =
|
|
1724
|
+
function reinterpretUntilZeros(size, offset) {
|
|
1725
|
+
return exports.reinterpretUntilZeros(this, size, offset);
|
|
1726
|
+
};
|
|
1727
|
+
|
|
1728
|
+
exports.PointerBuffer.isPointerBuffer = function isPointerBuffer(buffer) {
|
|
1729
|
+
return buffer instanceof exports.PointerBuffer;
|
|
1730
|
+
};
|
|
1731
|
+
|
|
1732
|
+
function overwriteInspect(inspect) {
|
|
1733
|
+
if (inspect.name === 'refinspect') {
|
|
1734
|
+
return inspect;
|
|
1735
|
+
} else {
|
|
1736
|
+
return function refinspect() {
|
|
1737
|
+
var v = inspect.apply(this, arguments);
|
|
1738
|
+
return v.replace('Buffer', 'Buffer@0x' + this.hexAddress());
|
|
1739
|
+
};
|
|
1740
|
+
}
|
|
1741
|
+
}
|