Comparing version 0.0.11 to 0.0.12
0.0.12 / 2012-05-30 | ||
=================== | ||
- make the "char" and "uchar" types accept JS String values | ||
- make the synonym types (i.e. longlong is a synonym for int64) be distinct | ||
objects, rather than simple JS references | ||
- fix coersion of a string value of "Object" | ||
- added the `reinterpretUntilZeros()` function | ||
0.0.11 / 2012-05-17 | ||
@@ -3,0 +12,0 @@ =================== |
@@ -64,4 +64,7 @@ | ||
if (typeof rtn === 'string') { | ||
rtn = exports.types[type] | ||
if (rtn) return rtn | ||
// strip whitespace | ||
rtn = rtn.replace(/\s+/g, '').toLowerCase() | ||
rtn = type.replace(/\s+/g, '').toLowerCase() | ||
if (rtn === 'pointer') { | ||
@@ -317,3 +320,14 @@ // legacy "pointer" being used :( | ||
/** | ||
*/ | ||
exports._reinterpretUntilZeros = exports.reinterpretUntilZeros | ||
exports.reinterpretUntilZeros = function reinterpretUntilZeros (buffer, size) { | ||
debug('reinterpreting buffer to until %d NULL bytes are found', size) | ||
var rtn = exports._reinterpretUntilZeros(buffer, size) | ||
exports._attach(rtn, buffer) | ||
return rtn | ||
} | ||
/** | ||
@@ -342,2 +356,5 @@ * Types. | ||
, set: function set (buf, offset, val) { | ||
if (typeof val === 'string') { | ||
val = val.charCodeAt(0) | ||
} | ||
return buf.writeInt8(val, offset || 0) | ||
@@ -353,2 +370,5 @@ } | ||
, set: function set (buf, offset, val) { | ||
if (typeof val === 'string') { | ||
val = val.charCodeAt(0) | ||
} | ||
return buf.writeUInt8(val, offset || 0) | ||
@@ -493,8 +513,9 @@ } | ||
assert(size >= 1 && size <= 8) | ||
var type = 'int' + (size * 8) | ||
var typeName = 'int' + (size * 8) | ||
if (unsigned) { | ||
type = 'u' + type | ||
typeName = 'u' + typeName | ||
} | ||
exports.types[name] = exports.types[type] | ||
assert(exports.types[name]) | ||
var type = exports.types[typeName] | ||
assert(type) | ||
exports.types[name] = Object.create(type) | ||
}) | ||
@@ -593,2 +614,6 @@ | ||
Buffer.prototype.reinterpretUntilZeros = function reinterpretUntilZeros (size) { | ||
return exports.reinterpretUntilZeros(this, size) | ||
} | ||
// does SlowBuffer inherit from Buffer? (node >= v0.7.9) | ||
@@ -615,2 +640,3 @@ if (!(exports.NULL instanceof Buffer)) { | ||
SlowBuffer.prototype.reinterpret = Buffer.prototype.reinterpret | ||
SlowBuffer.prototype.reinterpretUntilZeros = Buffer.prototype.reinterpretUntilZeros | ||
SlowBuffer.prototype['readInt64' + exports.endianness] = Buffer.prototype['readInt64' + exports.endianness] | ||
@@ -617,0 +643,0 @@ SlowBuffer.prototype['writeInt64' + exports.endianness] = Buffer.prototype['writeInt64' + exports.endianness] |
@@ -19,3 +19,3 @@ { "name": "ref" | ||
] | ||
, "version": "0.0.11" | ||
, "version": "0.0.12" | ||
, "author": "Nathan Rajlich <nathan@tootallnate.net> (http://tootallnate.net)" | ||
@@ -22,0 +22,0 @@ , "repository": { "type": "git", "url": "git://github.com/TooTallNate/ref.git" } |
@@ -37,3 +37,3 @@ ref | ||
#### references and derefencing | ||
#### referencing and derefencing | ||
@@ -120,2 +120,5 @@ ``` js | ||
`obj` gets "attached" to the buffer instance, so that the written object won't | ||
be garbage collected until the target buffer does. | ||
--- | ||
@@ -138,2 +141,5 @@ | ||
`pointer` gets "attached" to the buffer instance, so that the written pointer | ||
won't be garbage collected until the target buffer does. | ||
--- | ||
@@ -198,2 +204,5 @@ | ||
The original buffer instance gets "attached" to the new buffer instance, so that | ||
the original buffer won't be garbage collected until the new buffer does. | ||
__Warning:__ This function is potentially _dangerous_! There are only a small few | ||
@@ -204,2 +213,42 @@ use-cases where it _really_ needs to be used (i.e. resizing a Buffer returned from | ||
Built-in "types" | ||
---------------- | ||
`ref` comes with all the basic fixed-size C types that you are probably familiar with: | ||
| **Name** | **Description** | ||
|:-------------|:----------------------------------------------------- | ||
| `void` | A `void` type. Derefs to `null` | ||
| `int8` | Signed 8-bit Integer | ||
| `uint8` | Unsigned 8-bit Integer | ||
| `int16` | Signed 16-bit Integer | ||
| `uint16` | Unsigned 16-bit Integer | ||
| `int32` | Signed 32-bit Integer | ||
| `uint32` | Unsigned 32-bit Integer | ||
| `int64` | Signed 64-bit Integer | ||
| `uint64` | Unsigned 64-bit Integer | ||
| `float` | Single Precision Floating Point Number (float) | ||
| `double` | Double Precision Floating Point Number (double) | ||
| `Object` | A type capable of reading/writing references to JS objects | ||
| `Utf8String` | NULL-terminated String (char *) | ||
In addition to the basic types, there are type aliases for common C types. | ||
| **Name** | **Description** | ||
|:-------------|:----------------------------------------------------- | ||
| `bool` | bool. Returns/accepts JS `true`/`false` values | ||
| `byte` | unsigned char | ||
| `char` | char | ||
| `uchar` | unsigned char | ||
| `short` | short | ||
| `ushort` | unsigned short | ||
| `int` | int | ||
| `uint` | unsigned int | ||
| `long` | long | ||
| `ulong` | unsigned long | ||
| `longlong` | long long | ||
| `ulonglong` | unsigned long long | ||
| `size_t` | platform-dependent, usually pointer size | ||
The "type" interface | ||
@@ -212,12 +261,9 @@ -------------------- | ||
* `size` - Number - The size in bytes required to hold this type | ||
* `indirection` - Number - The current level of indirection of the buffer. | ||
Usually this would be _1_, and gets incremented on Buffers from `ref()` calls. | ||
A value of less than or equal to _0_ is invalid. | ||
* `get` - Function (buffer, offset) - the function to invoke when dereferencing | ||
this type when the indirection level is _1_. | ||
* `set` - Function (buffer, offset, value) - the function to invoke when | ||
setting a value to a buffer instance. | ||
* `alignment` - (optional) - Numer - The alignment of this type when placed in | ||
a struct | ||
| **Name** | **Data Type** | **Description** | ||
|:--------------|:---------------------------------|:---------------------------------- | ||
| `size` | Number | The size in bytes required to hold this type. | ||
| `indirection` | Number | The current level of indirection of the buffer. Usually this would be _1_, and gets incremented on Buffers from `ref()` calls. A value of less than or equal to _0_ is invalid. | ||
| `get` | Function (buffer, offset) | The function to invoke when dereferencing this type when the indirection level is _1_. | ||
| `set` | Function (buffer, offset, value) | The function to invoke when setting a value to a buffer instance. | ||
| `alignment` | Number | _(optional)_ The alignment of this type when placed in a struct. | ||
@@ -224,0 +270,0 @@ For example, you could define a "bigint" type that dereferences into a |
@@ -26,2 +26,6 @@ | ||
it('should coerce "Object" to `ref.types.Object`', function () { | ||
assert.strictEqual(ref.types.Object, ref.coerceType('Object')) | ||
}) | ||
}) |
@@ -45,2 +45,24 @@ | ||
describe('reinterpretUntilZeros()', function () { | ||
it('should return a new Buffer instance up until the first 0', function () { | ||
var buf = new Buffer('hello\0world') | ||
var buf2 = buf.reinterpretUntilZeros(1) | ||
assert.equal(buf2.length, 'hello'.length) | ||
assert.equal(buf2.toString(), 'hello') | ||
}) | ||
it('should return a new Buffer instance up until the first 2-byte sequence of 0s', function () { | ||
var str = 'hello world' | ||
var buf = new Buffer(50) | ||
var len = buf.write(str, 'ucs2') | ||
buf.writeInt16LE(0, len) // NULL terminate the string | ||
var buf2 = buf.reinterpretUntilZeros(2) | ||
assert.equal(str.length, buf2.length / 2) | ||
assert.equal(buf2.toString('ucs2'), str) | ||
}) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
68243
21
968
324