Turn Buffer instances into "pointers"
THIS IS A FORK OF THE ORIGINAL MODULE.
The intent is to provide prebuilt module for installation on Mozilla tbpl to run Gaia integration tests.
Use at your own risks.
This module is inspired by the old
Pointer class from node-ffi, but with the
intent of using Node's fast
Buffer instances instead of a slow C++
class. These two concepts were previously very similar, but now this module
brings over the functionality that Pointers had and Buffers are missing, so
now Buffers are a lot more powerful.
Bufferinstances' memory addresses to other
uint64_tdata values (Numbers or Strings)
int *, and reference/dereference at will.
$ npm install ref
var ref = require'ref'// so we can all agree that a buffer with the int value written// to it could be represented as an "int *"var buf = 4bufwriteInt32LE12345 0// first, what is the memory address of the buffer?console.logbufaddress // ← 140362165284824// using `ref`, you can set the "type", and gain magic abilities!buftype = reftypesint// now we can dereference to get the "meaningful" valueconsole.logbufderef // ← 12345// you can also get references to the original buffer if you need it.// this buffer could be thought of as an "int **"var one = bufref// and you can dereference all the way back down to an intconsole.logonederefderef // ← 12345
See the full API Docs for more examples.
You can easily define your own "type" objects at attach to
|Number||The size in bytes required to hold this type.|
|Number||The current level of indirection of the buffer. Usually this would be 1, and gets incremented on Buffers from |
|Function (buffer, offset)||The function to invoke when dereferencing this type when the indirection level is 1.|
|Function (buffer, offset, value)||The function to invoke when setting a value to a buffer instance.|
|String||(optional) The name to use during debugging for this type.|
|Number||(optional) The alignment of this type when placed in a struct. Defaults to the type's |
For example, you could define a "bigint" type that dereferences into a
var ref = require'ref'var bigint = require'bigint'// define the "type" instance according to the specvar BigintType =size: refsizeofint64indirection: 1// return a bigint instance from the bufferreturn bigintfromBufferbuffer// 'value' would be a bigint instancevar val = valuetoStringreturn refwriteInt64buffer offset || 0 val// now we can create instances of the type from existing buffers.// "buf" is some Buffer instance returned from some external data// source, which should contain "bigint" binary data.buftype = BigintType// and now you can create "bigint" instances using this generic "types" APIvar val = bufderefadd'1234'sqrtshiftLeft5
Install the dev dependencies:
$ npm install
Generate the docs:
$ npm run docs
(The MIT License)
Copyright (c) 2012 Nathan Rajlich <email@example.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.