Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GPII-4140: Update to node12 and renamed as scoped package @gpii/ref #1

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
54 changes: 0 additions & 54 deletions lib/ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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_.
*
Expand All @@ -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_.
Expand All @@ -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`
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "ref",
"name": "@gpii/ref",
"description": "Turn Buffer instances into \"pointers\"",
"keywords": [
"native",
Expand All @@ -17,17 +17,17 @@
"byte",
"64"
],
"version": "1.3.5",
"version": "2.0.0",
"license": "MIT",
"author": "Nathan Rajlich <[email protected]> (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",
Expand All @@ -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"
}
}
139 changes: 70 additions & 69 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ NAN_METHOD(WriteObject) {
Nan::Persistent<Object>* pptr = reinterpret_cast<Nan::Persistent<Object>*>(ptr);
Local<Object> val = info[2].As<Object>();

bool persistent = info[3]->BooleanValue();
bool persistent = Nan::To<bool>(info[3]).FromJust();
if (persistent) {
(*pptr).Reset(val);
} else {
Expand Down Expand Up @@ -250,7 +250,7 @@ NAN_METHOD(ReadPointer) {

int64_t offset = GetInt64(info[1]);
char *ptr = Buffer::Data(buf.As<Object>()) + offset;
size_t size = info[2]->Uint32Value();
size_t size = Nan::To<uint32_t>(info[2]).FromJust();

if (ptr == NULL) {
return Nan::ThrowError("readPointer: Cannot read from NULL pointer");
Expand Down Expand Up @@ -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<v8::String>(in).ToLocalChecked());
str = *_str;

errno = 0; /* To distinguish success/failure after call */
Expand Down Expand Up @@ -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<v8::String>(in).ToLocalChecked());
str = *_str;

errno = 0; /* To distinguish success/failure after call */
Expand Down Expand Up @@ -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<uint32_t>(info[1]).FromJust();

info.GetReturnValue().Set(WrapPointer(ptr, size));
}
Expand Down Expand Up @@ -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<uint32_t>(info[1]).FromJust();
uint32_t i = 0;
size_t size = 0;
bool end = false;
Expand All @@ -573,90 +573,91 @@ NAN_METHOD(ReinterpretBufferUntilZeros) {

NAN_MODULE_INIT(init) {
Nan::HandleScope scope;
v8::Local<v8::Context> ctx = Nan::GetCurrentContext();

// "sizeof" map
Local<Object> smap = Nan::New<v8::Object>();
// fixed sizes
#define SET_SIZEOF(name, type) \
smap->Set(Nan::New<v8::String>( #name ).ToLocalChecked(), Nan::New<v8::Uint32>(static_cast<uint32_t>(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<v8::Context>(context), Nan::New<v8::String>( #name ).ToLocalChecked(), Nan::New<v8::Uint32>(static_cast<uint32_t>(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<Object>);
SET_SIZEOF(ctx, Object, Nan::Persistent<Object>);

// "alignof" map
Local<Object> amap = Nan::New<v8::Object>();
#define SET_ALIGNOF(name, type) \
#define SET_ALIGNOF(context, name, type) \
struct s_##name { type a; }; \
amap->Set(Nan::New<v8::String>( #name ).ToLocalChecked(), Nan::New<v8::Uint32>(static_cast<uint32_t>(__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<Object>);
amap->Set(v8::Local<v8::Context>(context), Nan::New<v8::String>( #name ).ToLocalChecked(), Nan::New<v8::Uint32>(static_cast<uint32_t>(__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<Object>);

// exports
target->Set(Nan::New<v8::String>("sizeof").ToLocalChecked(), smap);
target->Set(Nan::New<v8::String>("alignof").ToLocalChecked(), amap);
Nan::ForceSet(target, Nan::New<v8::String>("endianness").ToLocalChecked(), Nan::New<v8::String>(CheckEndianness()).ToLocalChecked(), static_cast<PropertyAttribute>(ReadOnly|DontDelete));
Nan::ForceSet(target, Nan::New<v8::String>("NULL").ToLocalChecked(), WrapNullPointer(), static_cast<PropertyAttribute>(ReadOnly|DontDelete));
target->Set(ctx, Nan::New<v8::String>("sizeof").ToLocalChecked(), smap);
target->Set(ctx, Nan::New<v8::String>("alignof").ToLocalChecked(), amap);
Nan::DefineOwnProperty(target, Nan::New<v8::String>("endianness").ToLocalChecked(), Nan::New<v8::String>(CheckEndianness()).ToLocalChecked(), static_cast<PropertyAttribute>(ReadOnly|DontDelete));
Nan::DefineOwnProperty(target, Nan::New<v8::String>("NULL").ToLocalChecked(), WrapNullPointer(), static_cast<PropertyAttribute>(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);