Comparing version 0.0.8 to 0.0.9
0.0.9 / 2012-05-13 | ||
================== | ||
- allow `ref.alloc()` to not have a value being set with it | ||
- add the `coerceType()` function (get a proper "type" instance from a string) | ||
- add the Utf8String type back over from node-ffi | ||
- conditionally extend SlowBuffer.prototype for node >= v0.7.9 | ||
0.0.8 / 2012-05-12 | ||
@@ -3,0 +11,0 @@ ================== |
115
lib/ref.js
var debug = require('debug')('ref') | ||
, assert = require('assert') | ||
, SlowBuffer = require('buffer').SlowBuffer | ||
@@ -55,2 +54,31 @@ exports = module.exports = require('../build/Release/binding.node') | ||
/** | ||
* Coerces a "type" object from a String or a real "type" object. | ||
* So: | ||
* "int" gets coerced into `ref.types.int`. | ||
* "int *" gets translated into `ref.refType(ref.types.int)` | ||
* `ref.types.int` gets translated into `ref.types.int` (itself) | ||
*/ | ||
exports.coerceType = function coerceType (type) { | ||
var rtn = type | ||
if (typeof type === 'string') { | ||
if (type === 'pointer') { | ||
// legacy "pointer" being used :( | ||
//console.warn('type of "pointer" should not be used...') | ||
//console.trace() | ||
rtn = exports.refType(exports.types.void) // void * | ||
} else if (type === 'string') { | ||
rtn = exports.types.Utf8String // special char * type | ||
} else { | ||
assert(!/\*/.test(type), 'TODO: implement indirection coersion') | ||
// allow string names to be passed in | ||
rtn = exports.types[type] | ||
} | ||
} | ||
assert(rtn && 'size' in rtn && 'indirection' in rtn | ||
, 'could not determine a proper "type" from: ' + JSON.stringify(type)) | ||
return rtn | ||
} | ||
/** | ||
* Returns the "type" property of the given Buffer. | ||
@@ -131,2 +159,3 @@ * Creates a default type for the buffer when none exists. | ||
* ``` js | ||
* var intBuf = ref.alloc(ref.types.int) | ||
* var int_with_4 = ref.alloc(ref.types.int, 4) | ||
@@ -137,3 +166,3 @@ * ``` | ||
exports.alloc = function alloc (type, value) { | ||
debug('allocating type with "value"', value) | ||
debug('allocating Buffer for type with "size"', type.size) | ||
var size | ||
@@ -147,3 +176,6 @@ if (type.indirection === 1) { | ||
buffer.type = type | ||
exports.set(buffer, 0, value, type) | ||
if (arguments.length >= 2) { | ||
debug('setting value on allocated buffer', value) | ||
exports.set(buffer, 0, value, type) | ||
} | ||
return buffer | ||
@@ -260,4 +292,11 @@ } | ||
/** | ||
* Overwrite the native "reinterpret" function so that the reinterpreted buffer | ||
* retains a reference to the original buffer, so that it doesn't get GC'd while | ||
* the reinterpreted buffer is still watching it. | ||
*/ | ||
exports._reinterpret = exports.reinterpret | ||
exports.reinterpret = function reinterpret (buffer, size) { | ||
debug('reinterpreting buffer to "%d" bytes', size) | ||
var rtn = exports._reinterpret(buffer, size) | ||
@@ -396,5 +435,28 @@ exports._attach(rtn, buffer) | ||
/** | ||
* Utf8Strings are a kind of weird thing. We say it's a sizeof(char *), | ||
* so that means that we have to return a Buffer that is pointer sized, and points | ||
* to a some utf8 string data, so we have to create a 2nd "in-between" buffer. | ||
* | ||
* Really, people should just use a proper `char *` type. | ||
* This is only here for legacy purposes... | ||
*/ | ||
exports.types.Utf8String = { | ||
size: exports.sizeof.pointer | ||
, indirection: 1 | ||
, get: function get (buf, offset) { | ||
var _buf = buf.readPointer(offset) | ||
return _buf.readCString(0) | ||
} | ||
, set: function set (buf, offset, val) { | ||
var _buf = exports.allocCString(val) | ||
return buf.writePointer(_buf, offset) | ||
} | ||
} | ||
// "typedef"s for the variable-sized types | ||
;[ 'byte', 'char', 'uchar', 'short', 'ushort', 'int', 'uint', 'long', 'ulong' | ||
, 'longlong', 'ulonglong', 'size_t' ].forEach(function (name) { | ||
;[ 'byte', 'char', 'uchar', 'short', 'ushort', 'int', 'uint', 'long' | ||
, 'ulong', 'longlong', 'ulonglong', 'size_t' ].forEach(function (name) { | ||
var unsigned = name === 'byte' || name === 'size_t' || name[0] === 'u' | ||
@@ -502,20 +564,27 @@ var size = exports.sizeof[name] | ||
/** | ||
* SlowBuffer convenience methods. | ||
*/ | ||
// does SlowBuffer inherit from Buffer? (node >= v0.7.9) | ||
if (!(exports.NULL instanceof Buffer)) { | ||
debug('extending SlowBuffer\'s prototype since it doesn\'t inherit from Buffer.prototype') | ||
SlowBuffer.prototype.address = Buffer.prototype.address | ||
SlowBuffer.prototype.isNull = Buffer.prototype.isNull | ||
SlowBuffer.prototype.ref = Buffer.prototype.ref | ||
SlowBuffer.prototype.deref = Buffer.prototype.deref | ||
SlowBuffer.prototype.readObject = Buffer.prototype.readObject | ||
SlowBuffer.prototype.writeObject = Buffer.prototype.writeObject | ||
SlowBuffer.prototype.readPointer = Buffer.prototype.readPointer | ||
SlowBuffer.prototype.writePointer = Buffer.prototype.writePointer | ||
SlowBuffer.prototype.readCString = Buffer.prototype.readCString | ||
SlowBuffer.prototype.writeCString = Buffer.prototype.writeCString | ||
SlowBuffer.prototype.reinterpret = Buffer.prototype.reinterpret | ||
SlowBuffer.prototype['readInt64' + exports.endianness] = Buffer.prototype['readInt64' + exports.endianness] | ||
SlowBuffer.prototype['writeInt64' + exports.endianness] = Buffer.prototype['writeInt64' + exports.endianness] | ||
SlowBuffer.prototype['readUInt64' + exports.endianness] = Buffer.prototype['readUInt64' + exports.endianness] | ||
SlowBuffer.prototype['writeUInt64' + exports.endianness] = Buffer.prototype['writeUInt64' + exports.endianness] | ||
/** | ||
* SlowBuffer convenience methods. | ||
*/ | ||
var SlowBuffer = require('buffer').SlowBuffer | ||
SlowBuffer.prototype.address = Buffer.prototype.address | ||
SlowBuffer.prototype.isNull = Buffer.prototype.isNull | ||
SlowBuffer.prototype.ref = Buffer.prototype.ref | ||
SlowBuffer.prototype.deref = Buffer.prototype.deref | ||
SlowBuffer.prototype.readObject = Buffer.prototype.readObject | ||
SlowBuffer.prototype.writeObject = Buffer.prototype.writeObject | ||
SlowBuffer.prototype.readPointer = Buffer.prototype.readPointer | ||
SlowBuffer.prototype.writePointer = Buffer.prototype.writePointer | ||
SlowBuffer.prototype.readCString = Buffer.prototype.readCString | ||
SlowBuffer.prototype.writeCString = Buffer.prototype.writeCString | ||
SlowBuffer.prototype.reinterpret = Buffer.prototype.reinterpret | ||
SlowBuffer.prototype['readInt64' + exports.endianness] = Buffer.prototype['readInt64' + exports.endianness] | ||
SlowBuffer.prototype['writeInt64' + exports.endianness] = Buffer.prototype['writeInt64' + exports.endianness] | ||
SlowBuffer.prototype['readUInt64' + exports.endianness] = Buffer.prototype['readUInt64' + exports.endianness] | ||
SlowBuffer.prototype['writeUInt64' + exports.endianness] = Buffer.prototype['writeUInt64' + exports.endianness] | ||
} |
@@ -19,3 +19,3 @@ { "name": "ref" | ||
] | ||
, "version": "0.0.8" | ||
, "version": "0.0.9" | ||
, "author": "Nathan Rajlich <nathan@tootallnate.net> (http://tootallnate.net)" | ||
@@ -22,0 +22,0 @@ , "repository": { "type": "git", "url": "git://github.com/TooTallNate/ref.git" } |
59206
834