cmake-js/ref-array

Name: ref-array

Owner: CMake.js

Description: Create C "array" instances on top of Buffers

Forked from: TooTallNate/ref-array

Created: 2016-11-02 17:56:04.0

Updated: 2016-11-02 17:56:05.0

Pushed: 2016-11-07 22:38:06.0

Homepage: null

Size: 42

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

ref-array

Create C typed “array” instances on top of Buffers

Build Status Build Status

This module offers an “array” implementation on top of Node.js Buffers using the ref “type” interface.

Installation

Install with npm:

m install ref-array
Examples
Basic usage
ref = require('ref')
ArrayType = require('ref-array')

ypedef
int = ref.types.int

efine the "int[]" type
IntArray = ArrayType(int)

ow we can create array instances; the constructor takes the same arguments
he native JS Array class

a = new IntArray(5) // by length
ngth // 5
 = 0
 = 1
 = -1
 = 2
 = -2

b = new IntArray([1, 2, 3, 4, 5]) // with an existing Array
ngth // 5
 // 1
 // 2
 // 3
 // 4
 // 5
Reading a NULL-terminated Array
ometimes you get a variable length array that is terminated by a NULL byte.
buf = new Buffer(int.size * 3)
set(buf, int.size * 0, 5)
set(buf, int.size * 1, 8)
set(buf, int.size * 2, 0) // <- terminate with 0s

ou can create an array instance with the length automatically determined
array = IntArray.untilZeros(buf)
ole.log(array.length)

ole.log(array)
 5, 8 ]
With node-ffi
ffi = require('ffi')

he "int[]" type may be used as a "type" in FFI'd functions or callbacks
func = ffi.ForeignFunction(funcPointer, int, [ IntArray, int ])

arg = new IntArray(3)
0] = 1234
1] = -9999
2] = 1

rtn = func(arg, arg.length)
License

(The MIT License)

Copyright (c) 2012 Nathan Rajlich <nathan@tootallnate.net>

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.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.