Comparing version 0.0.4 to 0.0.5
@@ -75,2 +75,40 @@ | ||
exports.get = function get (buffer, offset, type) { | ||
if (!offset) { | ||
offset = 0 | ||
} | ||
if (!type) { | ||
type = exports.getType(buffer) | ||
} | ||
debug('get(): (offset: %d)', offset, buffer) | ||
assert(type.indirection > 0, '"indirection" level must be at least 1') | ||
if (type.indirection === 1) { | ||
// need to check "type" | ||
return type.get(buffer, offset) | ||
} else { | ||
// need to create a deref'd Buffer | ||
var size = type.indirection === 2 ? type.size : exports.sizeof.pointer | ||
var reference = exports.readPointer(buffer, offset, size) | ||
reference.type = exports.derefType(type) | ||
return reference | ||
} | ||
} | ||
exports.set = function set (buffer, offset, value, type) { | ||
if (!offset) { | ||
offset = 0 | ||
} | ||
if (!type) { | ||
type = exports.getType(buffer) | ||
} | ||
debug('set(): (offset: %d)', offset, buffer, value) | ||
assert(type.indirection > 0, '"indirection" level must be at least 1') | ||
if (type.indirection === 1) { | ||
type.set(buffer, offset, value) | ||
} else { | ||
exports.writePointer(buffer, offset, value) | ||
} | ||
} | ||
/** | ||
@@ -86,13 +124,12 @@ * Returns a new Buffer instance big enough to hold `type`, | ||
exports.alloc = function alloc (type, value) { | ||
var buffer | ||
assert(type.indirection > 0, '"indirection" level must be at lest 1') | ||
debug('allocating type with "value"', value) | ||
var size | ||
if (type.indirection === 1) { | ||
buffer = new Buffer(type.size) | ||
type.set(buffer, 0, value) | ||
size = type.size | ||
} else { | ||
buffer = new Buffer(exports.sizeof.pointer) | ||
exports.writePointer(buffer, 0, value) | ||
size = exports.sizeof.pointer | ||
} | ||
var buffer = new Buffer(size) | ||
buffer.type = type | ||
exports.set(buffer, 0, value, type) | ||
return buffer | ||
@@ -160,3 +197,3 @@ } | ||
* `deref()` acceps a Buffer instance and attempts to "dereference" it. | ||
* That is, first it checks the "_indirection" count, and if it's greater than | ||
* That is, first it checks the "indirection" count, and if it's greater than | ||
* 0 then it merely returns another Buffer, but with one level less indirection. | ||
@@ -168,16 +205,4 @@ * When the buffer is at indirection 0, or undefined, then it checks for "type" | ||
exports.deref = function deref (buffer) { | ||
var type = exports.getType(buffer) | ||
var indirection = type.indirection | ||
assert(type.indirection > 0, '"indirection" level must be at lest 1') | ||
debug('dereferencing buffer', buffer, type, indirection) | ||
if (indirection === 1) { | ||
// need to check "type" | ||
return type.get(buffer) | ||
} else { | ||
// need to create a deref'd Buffer | ||
var size = indirection === 2 ? type.size : exports.sizeof.pointer | ||
var reference = exports.readPointer(buffer, 0, size) | ||
reference.type = exports.derefType(type) | ||
return reference | ||
} | ||
debug('dereferencing buffer', buffer) | ||
return exports.get(buffer) | ||
} | ||
@@ -184,0 +209,0 @@ |
@@ -19,3 +19,3 @@ { "name": "ref" | ||
] | ||
, "version": "0.0.4" | ||
, "version": "0.0.5" | ||
, "author": "Nathan Rajlich <nathan@tootallnate.net> (http://tootallnate.net)" | ||
@@ -22,0 +22,0 @@ , "repository": { "type": "git", "url": "git://github.com/TooTallNate/ref.git" } |
@@ -15,3 +15,3 @@ | ||
var address = ref.address(buf) | ||
assert(typeof address === 'number') | ||
assert.equal(typeof address, 'number') | ||
assert(isFinite(address)) | ||
@@ -18,0 +18,0 @@ assert(address > 0) |
@@ -11,3 +11,3 @@ | ||
it('should allow simple ints to be written and read', function () { | ||
var buf = new Buffer(ref.sizeof.longlong) | ||
var buf = new Buffer(ref.sizeof.int64) | ||
var val = 123456789 | ||
@@ -20,3 +20,3 @@ ref.writeInt64(buf, 0, val) | ||
it('should allow INT64_MAX to be written and read', function () { | ||
var buf = new Buffer(ref.sizeof.longlong) | ||
var buf = new Buffer(ref.sizeof.int64) | ||
var val = '9223372036854775807' | ||
@@ -29,3 +29,3 @@ ref.writeInt64(buf, 0, val) | ||
it('should return a Number when reading JS_MIN_INT', function () { | ||
var buf = new Buffer(ref.sizeof.longlong) | ||
var buf = new Buffer(ref.sizeof.int64) | ||
ref.writeInt64(buf, 0, JS_MIN_INT) | ||
@@ -38,3 +38,3 @@ var rtn = ref.readInt64(buf, 0) | ||
it('should return a Number when reading JS_MAX_INT', function () { | ||
var buf = new Buffer(ref.sizeof.longlong) | ||
var buf = new Buffer(ref.sizeof.int64) | ||
ref.writeInt64(buf, 0, JS_MAX_INT) | ||
@@ -47,3 +47,3 @@ var rtn = ref.readInt64(buf, 0) | ||
it('should return a String when reading JS_MAX_INT+1', function () { | ||
var buf = new Buffer(ref.sizeof.longlong) | ||
var buf = new Buffer(ref.sizeof.int64) | ||
var plus_one = '9007199254740993' | ||
@@ -57,3 +57,3 @@ ref.writeInt64(buf, 0, plus_one) | ||
it('should return a String when reading JS_MIN_INT-1', function () { | ||
var buf = new Buffer(ref.sizeof.longlong) | ||
var buf = new Buffer(ref.sizeof.int64) | ||
var minus_one = '-9007199254740993' | ||
@@ -67,3 +67,3 @@ ref.writeInt64(buf, 0, minus_one) | ||
it('should return a Number when reading 0, even when written as a String', function () { | ||
var buf = new Buffer(ref.sizeof.longlong) | ||
var buf = new Buffer(ref.sizeof.int64) | ||
var zero = '0' | ||
@@ -70,0 +70,0 @@ ref.writeInt64(buf, 0, zero) |
@@ -7,4 +7,2 @@ | ||
var buf = new Buffer('hello') | ||
it('should return "true" for the NULL pointer', function () { | ||
@@ -15,2 +13,3 @@ assert.strictEqual(true, ref.isNull(ref.NULL)) | ||
it('should return "false" for a valid Buffer', function () { | ||
var buf = new Buffer('hello') | ||
assert.strictEqual(false, ref.isNull(buf)) | ||
@@ -17,0 +16,0 @@ }) |
@@ -34,3 +34,3 @@ | ||
ref.deref(test) | ||
}, 'unknown "type"') | ||
}, /unknown "type"/) | ||
}) | ||
@@ -45,3 +45,3 @@ | ||
ref.deref(_test) | ||
}, 'unknown "type"') | ||
}, /unknown "type"/) | ||
}) | ||
@@ -48,0 +48,0 @@ |
Sorry, the diff of this file is not supported yet
48585
683