diff --git a/lib/ref.js b/lib/ref.js index 77530a6..79a992c 100644 --- a/lib/ref.js +++ b/lib/ref.js @@ -676,19 +676,6 @@ exports._attach = function _attach (buf, obj) { buf._refs.push(obj) } -/** - * Same as `ref.writeObject()`, except that this version does not _attach_ the - * Object to the Buffer, which is potentially unsafe if the garbage collector - * runs. - * - * @param {Buffer} buffer A Buffer instance to write _object_ to. - * @param {Number} offset The offset on the Buffer to start writing at. - * @param {Object} object The Object to be written into _buffer_. - * @api private - */ - -exports._writeObject = exports.writeObject - /** * Writes a pointer to _object_ into _buffer_ at the specified _offset. * @@ -712,19 +699,6 @@ exports.writeObject = function writeObject (buf, offset, obj, persistent) { exports._attach(buf, obj) } -/** - * Same as `ref.writePointer()`, except that this version does not attach - * _pointer_ to _buffer_, which is potentially unsafe if the garbage collector - * runs. - * - * @param {Buffer} buffer A Buffer instance to write _pointer to. - * @param {Number} offset The offset on the Buffer to start writing at. - * @param {Buffer} pointer The Buffer instance whose memory address will be written to _buffer_. - * @api private - */ - -exports._writePointer = exports.writePointer - /** * Writes the memory address of _pointer_ to _buffer_ at the specified _offset_. * @@ -748,20 +722,6 @@ exports.writePointer = function writePointer (buf, offset, ptr) { exports._attach(buf, ptr) } -/** - * Same as `ref.reinterpret()`, except that this version does not attach - * _buffer_ to the returned Buffer, which is potentially unsafe if the - * garbage collector runs. - * - * @param {Buffer} buffer A Buffer instance to base the returned Buffer off of. - * @param {Number} size The `length` property of the returned Buffer. - * @param {Number} offset The offset of the Buffer to begin from. - * @return {Buffer} A new Buffer instance with the same memory address as _buffer_, and the requested _size_. - * @api private - */ - -exports._reinterpret = exports.reinterpret - /** * Returns a new Buffer instance with the specified _size_, with the same memory * address as _buffer_. @@ -782,20 +742,6 @@ exports.reinterpret = function reinterpret (buffer, size, offset) { return rtn } -/** - * Same as `ref.reinterpretUntilZeros()`, except that this version does not - * attach _buffer_ to the returned Buffer, which is potentially unsafe if the - * garbage collector runs. - * - * @param {Buffer} buffer A Buffer instance to base the returned Buffer off of. - * @param {Number} size The number of sequential, aligned `NULL` bytes that are required to terminate the buffer. - * @param {Number} offset The offset of the Buffer to begin from. - * @return {Buffer} A new Buffer instance with the same memory address as _buffer_, and a variable `length` that is terminated by _size_ NUL bytes. - * @api private - */ - -exports._reinterpretUntilZeros = exports.reinterpretUntilZeros - /** * Accepts a `Buffer` instance and a number of `NULL` bytes to read from the * pointer. This function will scan past the boundary of the Buffer's `length` diff --git a/package.json b/package.json index 503e10b..c746ce5 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "ref", + "name": "@gpii/ref", "description": "Turn Buffer instances into \"pointers\"", "keywords": [ "native", @@ -17,17 +17,17 @@ "byte", "64" ], - "version": "1.3.5", + "version": "2.0.0", "license": "MIT", "author": "Nathan Rajlich (http://tootallnate.net)", "repository": { "type": "git", - "url": "git://github.com/TooTallNate/ref.git" + "url": "git://github.com/GPII/ref.git" }, "main": "./lib/ref.js", "scripts": { "docs": "node docs/compile", - "test": "mocha -gc --reporter spec --use_strict" + "test": "mocha --gc-global --expose-gc --reporter spec --use_strict" }, "dependencies": { "bindings": "1", @@ -36,10 +36,10 @@ }, "devDependencies": { "dox": "0.9.0", - "highlight.js": "1", + "highlight.js": "9", "jade": "1", - "marked": "0.5.2", + "marked": "0.6.2", "mocha": "*", - "weak": "1" + "weak": "github:lxe/node-weak#node-12" } } diff --git a/src/binding.cc b/src/binding.cc index 7075be1..ed0c74c 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -219,7 +219,7 @@ NAN_METHOD(WriteObject) { Nan::Persistent* pptr = reinterpret_cast*>(ptr); Local val = info[2].As(); - bool persistent = info[3]->BooleanValue(); + bool persistent = Nan::To(info[3]).FromJust(); if (persistent) { (*pptr).Reset(val); } else { @@ -250,7 +250,7 @@ NAN_METHOD(ReadPointer) { int64_t offset = GetInt64(info[1]); char *ptr = Buffer::Data(buf.As()) + offset; - size_t size = info[2]->Uint32Value(); + size_t size = Nan::To(info[2]).FromJust(); if (ptr == NULL) { return Nan::ThrowError("readPointer: Cannot read from NULL pointer"); @@ -357,7 +357,7 @@ NAN_METHOD(WriteInt64) { } else if (in->IsString()) { char *endptr, *str; int base = 0; - String::Utf8Value _str(in); + String::Utf8Value _str(v8::Isolate::GetCurrent(), Nan::To(in).ToLocalChecked()); str = *_str; errno = 0; /* To distinguish success/failure after call */ @@ -444,7 +444,7 @@ NAN_METHOD(WriteUInt64) { } else if (in->IsString()) { char *endptr, *str; int base = 0; - String::Utf8Value _str(in); + String::Utf8Value _str(v8::Isolate::GetCurrent(), Nan::To(in).ToLocalChecked()); str = *_str; errno = 0; /* To distinguish success/failure after call */ @@ -518,7 +518,7 @@ NAN_METHOD(ReinterpretBuffer) { return Nan::ThrowError("reinterpret: Cannot reinterpret from NULL pointer"); } - size_t size = info[1]->Uint32Value(); + size_t size = Nan::To(info[1]).FromJust(); info.GetReturnValue().Set(WrapPointer(ptr, size)); } @@ -547,7 +547,7 @@ NAN_METHOD(ReinterpretBufferUntilZeros) { return Nan::ThrowError("reinterpretUntilZeros: Cannot reinterpret from NULL pointer"); } - uint32_t numZeros = info[1]->Uint32Value(); + uint32_t numZeros = Nan::To(info[1]).FromJust(); uint32_t i = 0; size_t size = 0; bool end = false; @@ -573,90 +573,91 @@ NAN_METHOD(ReinterpretBufferUntilZeros) { NAN_MODULE_INIT(init) { Nan::HandleScope scope; + v8::Local ctx = Nan::GetCurrentContext(); // "sizeof" map Local smap = Nan::New(); // fixed sizes -#define SET_SIZEOF(name, type) \ - smap->Set(Nan::New( #name ).ToLocalChecked(), Nan::New(static_cast(sizeof(type)))); - SET_SIZEOF(int8, int8_t); - SET_SIZEOF(uint8, uint8_t); - SET_SIZEOF(int16, int16_t); - SET_SIZEOF(uint16, uint16_t); - SET_SIZEOF(int32, int32_t); - SET_SIZEOF(uint32, uint32_t); - SET_SIZEOF(int64, int64_t); - SET_SIZEOF(uint64, uint64_t); - SET_SIZEOF(float, float); - SET_SIZEOF(double, double); +#define SET_SIZEOF(context, name, type) \ + smap->Set(v8::Local(context), Nan::New( #name ).ToLocalChecked(), Nan::New(static_cast(sizeof(type)))); + SET_SIZEOF(ctx, int8, int8_t); + SET_SIZEOF(ctx, uint8, uint8_t); + SET_SIZEOF(ctx, int16, int16_t); + SET_SIZEOF(ctx, uint16, uint16_t); + SET_SIZEOF(ctx, int32, int32_t); + SET_SIZEOF(ctx, uint32, uint32_t); + SET_SIZEOF(ctx, int64, int64_t); + SET_SIZEOF(ctx, uint64, uint64_t); + SET_SIZEOF(ctx, float, float); + SET_SIZEOF(ctx, double, double); // (potentially) variable sizes - SET_SIZEOF(bool, bool); - SET_SIZEOF(byte, unsigned char); - SET_SIZEOF(char, char); - SET_SIZEOF(uchar, unsigned char); - SET_SIZEOF(short, short); - SET_SIZEOF(ushort, unsigned short); - SET_SIZEOF(int, int); - SET_SIZEOF(uint, unsigned int); - SET_SIZEOF(long, long); - SET_SIZEOF(ulong, unsigned long); - SET_SIZEOF(longlong, long long); - SET_SIZEOF(ulonglong, unsigned long long); - SET_SIZEOF(pointer, char *); - SET_SIZEOF(size_t, size_t); - SET_SIZEOF(wchar_t, wchar_t); + SET_SIZEOF(ctx, bool, bool); + SET_SIZEOF(ctx, byte, unsigned char); + SET_SIZEOF(ctx, char, char); + SET_SIZEOF(ctx, uchar, unsigned char); + SET_SIZEOF(ctx, short, short); + SET_SIZEOF(ctx, ushort, unsigned short); + SET_SIZEOF(ctx, int, int); + SET_SIZEOF(ctx, uint, unsigned int); + SET_SIZEOF(ctx, long, long); + SET_SIZEOF(ctx, ulong, unsigned long); + SET_SIZEOF(ctx, longlong, long long); + SET_SIZEOF(ctx, ulonglong, unsigned long long); + SET_SIZEOF(ctx, pointer, char *); + SET_SIZEOF(ctx, size_t, size_t); + SET_SIZEOF(ctx, wchar_t, wchar_t); // size of a Persistent handle to a JS object - SET_SIZEOF(Object, Nan::Persistent); + SET_SIZEOF(ctx, Object, Nan::Persistent); // "alignof" map Local amap = Nan::New(); -#define SET_ALIGNOF(name, type) \ +#define SET_ALIGNOF(context, name, type) \ struct s_##name { type a; }; \ - amap->Set(Nan::New( #name ).ToLocalChecked(), Nan::New(static_cast(__alignof__(struct s_##name)))); - SET_ALIGNOF(int8, int8_t); - SET_ALIGNOF(uint8, uint8_t); - SET_ALIGNOF(int16, int16_t); - SET_ALIGNOF(uint16, uint16_t); - SET_ALIGNOF(int32, int32_t); - SET_ALIGNOF(uint32, uint32_t); - SET_ALIGNOF(int64, int64_t); - SET_ALIGNOF(uint64, uint64_t); - SET_ALIGNOF(float, float); - SET_ALIGNOF(double, double); - SET_ALIGNOF(bool, bool); - SET_ALIGNOF(char, char); - SET_ALIGNOF(uchar, unsigned char); - SET_ALIGNOF(short, short); - SET_ALIGNOF(ushort, unsigned short); - SET_ALIGNOF(int, int); - SET_ALIGNOF(uint, unsigned int); - SET_ALIGNOF(long, long); - SET_ALIGNOF(ulong, unsigned long); - SET_ALIGNOF(longlong, long long); - SET_ALIGNOF(ulonglong, unsigned long long); - SET_ALIGNOF(pointer, char *); - SET_ALIGNOF(size_t, size_t); - SET_ALIGNOF(wchar_t, wchar_t); - SET_ALIGNOF(Object, Nan::Persistent); + amap->Set(v8::Local(context), Nan::New( #name ).ToLocalChecked(), Nan::New(static_cast(__alignof__(struct s_##name)))); + SET_ALIGNOF(ctx, int8, int8_t); + SET_ALIGNOF(ctx, uint8, uint8_t); + SET_ALIGNOF(ctx, int16, int16_t); + SET_ALIGNOF(ctx, uint16, uint16_t); + SET_ALIGNOF(ctx, int32, int32_t); + SET_ALIGNOF(ctx, uint32, uint32_t); + SET_ALIGNOF(ctx, int64, int64_t); + SET_ALIGNOF(ctx, uint64, uint64_t); + SET_ALIGNOF(ctx, float, float); + SET_ALIGNOF(ctx, double, double); + SET_ALIGNOF(ctx, bool, bool); + SET_ALIGNOF(ctx, char, char); + SET_ALIGNOF(ctx, uchar, unsigned char); + SET_ALIGNOF(ctx, short, short); + SET_ALIGNOF(ctx, ushort, unsigned short); + SET_ALIGNOF(ctx, int, int); + SET_ALIGNOF(ctx, uint, unsigned int); + SET_ALIGNOF(ctx, long, long); + SET_ALIGNOF(ctx, ulong, unsigned long); + SET_ALIGNOF(ctx, longlong, long long); + SET_ALIGNOF(ctx, ulonglong, unsigned long long); + SET_ALIGNOF(ctx, pointer, char *); + SET_ALIGNOF(ctx, size_t, size_t); + SET_ALIGNOF(ctx, wchar_t, wchar_t); + SET_ALIGNOF(ctx, Object, Nan::Persistent); // exports - target->Set(Nan::New("sizeof").ToLocalChecked(), smap); - target->Set(Nan::New("alignof").ToLocalChecked(), amap); - Nan::ForceSet(target, Nan::New("endianness").ToLocalChecked(), Nan::New(CheckEndianness()).ToLocalChecked(), static_cast(ReadOnly|DontDelete)); - Nan::ForceSet(target, Nan::New("NULL").ToLocalChecked(), WrapNullPointer(), static_cast(ReadOnly|DontDelete)); + target->Set(ctx, Nan::New("sizeof").ToLocalChecked(), smap); + target->Set(ctx, Nan::New("alignof").ToLocalChecked(), amap); + Nan::DefineOwnProperty(target, Nan::New("endianness").ToLocalChecked(), Nan::New(CheckEndianness()).ToLocalChecked(), static_cast(ReadOnly|DontDelete)); + Nan::DefineOwnProperty(target, Nan::New("NULL").ToLocalChecked(), WrapNullPointer(), static_cast(ReadOnly|DontDelete)); Nan::SetMethod(target, "address", Address); Nan::SetMethod(target, "hexAddress", HexAddress); Nan::SetMethod(target, "isNull", IsNull); Nan::SetMethod(target, "readObject", ReadObject); - Nan::SetMethod(target, "writeObject", WriteObject); + Nan::SetMethod(target, "_writeObject", WriteObject); Nan::SetMethod(target, "readPointer", ReadPointer); - Nan::SetMethod(target, "writePointer", WritePointer); + Nan::SetMethod(target, "_writePointer", WritePointer); Nan::SetMethod(target, "readInt64", ReadInt64); Nan::SetMethod(target, "writeInt64", WriteInt64); Nan::SetMethod(target, "readUInt64", ReadUInt64); Nan::SetMethod(target, "writeUInt64", WriteUInt64); Nan::SetMethod(target, "readCString", ReadCString); - Nan::SetMethod(target, "reinterpret", ReinterpretBuffer); - Nan::SetMethod(target, "reinterpretUntilZeros", ReinterpretBufferUntilZeros); + Nan::SetMethod(target, "_reinterpret", ReinterpretBuffer); + Nan::SetMethod(target, "_reinterpretUntilZeros", ReinterpretBufferUntilZeros); } NODE_MODULE(binding, init);