forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* There are now three creation functions: 1. Create such that node owns the pointer 2. Create such that the caller owns the pointer 3. Create such that node owns the pointer and copies the data into it * napi_buffer_has_instance() is now called napi_is_buffer() * napi_buffer_data() and napi_buffer_length() are now merged into napi_get_buffer_info() * There is now a test for this api under test/addons-abi Fixes nodejs/abi-stable-node#108 Closes nodejs/abi-stable-node#109
- Loading branch information
Gabriel Schulhof
committed
Feb 22, 2017
1 parent
654da16
commit 5042fc7
Showing
26 changed files
with
252 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
'use strict'; | ||
require('../../common'); | ||
var common = require('../../common'); | ||
var assert = require('assert'); | ||
var addon = require('./build/Release/binding'); | ||
var addon = require(`./build/${common.buildType}/binding`); | ||
|
||
assert.equal(addon.hello(), 'world'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
'use strict'; | ||
require('../../common'); | ||
var common = require('../../common'); | ||
var assert = require('assert'); | ||
var addon = require('./build/Release/binding'); | ||
var addon = require(`./build/${common.buildType}/binding`); | ||
|
||
assert.equal(addon.add(3, 5), 8); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
'use strict'; | ||
require('../../common'); | ||
var common = require('../../common'); | ||
var assert = require('assert'); | ||
var addon = require('./build/Release/binding'); | ||
var addon = require(`./build/${common.buildType}/binding`); | ||
|
||
var fn = addon(); | ||
assert.equal(fn(), 'hello world'); // 'hello world' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"targets": [ | ||
{ | ||
"target_name": "test_buffer", | ||
"sources": [ "test_buffer.cc" ] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
// Flags: --expose-gc | ||
|
||
var common = require('../../common'); | ||
var binding = require(`./build/${common.buildType}/test_buffer`); | ||
var assert = require( "assert" ); | ||
|
||
assert( binding.newBuffer().toString() === binding.theText, | ||
"buffer returned by newBuffer() has wrong contents" ); | ||
assert( binding.newExternalBuffer().toString() === binding.theText, | ||
"buffer returned by newExternalBuffer() has wrong contents" ); | ||
console.log( "gc1" ); | ||
global.gc(); | ||
assert( binding.getDeleterCallCount(), 1, "deleter was not called" ); | ||
assert( binding.copyBuffer().toString() === binding.theText, | ||
"buffer returned by copyBuffer() has wrong contents" ); | ||
|
||
var buffer = binding.staticBuffer(); | ||
assert( binding.bufferHasInstance( buffer ), true, "buffer type checking fails" ); | ||
assert( binding.bufferInfo( buffer ), true, "buffer data is accurate" ); | ||
buffer = null; | ||
global.gc(); | ||
console.log( "gc2" ); | ||
assert( binding.getDeleterCallCount(), 2, "deleter was not called" ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
#include <string.h> | ||
#include <string> | ||
#include <node_api_helpers.h> | ||
|
||
#define JS_ASSERT(env, assertion, message) \ | ||
if (!(assertion)) { \ | ||
napi_throw_error((env), (std::string("assertion (" #assertion ") failed: ") + message).c_str()); \ | ||
return; \ | ||
} | ||
|
||
#define NAPI_CALL(env, theCall) \ | ||
if (theCall != napi_ok) { \ | ||
const char *errorMessage = napi_get_last_error_info()->error_message; \ | ||
errorMessage = errorMessage ? errorMessage: "empty error message"; \ | ||
napi_throw_error((env), errorMessage); \ | ||
return; \ | ||
} | ||
|
||
static const char theText[] = | ||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit."; | ||
|
||
static int deleterCallCount = 0; | ||
static void deleteTheText(void *data) { | ||
delete (char *)data; | ||
deleterCallCount++; | ||
} | ||
|
||
static void noopDeleter(void *data) { | ||
deleterCallCount++; | ||
} | ||
|
||
NAPI_METHOD(newBuffer) { | ||
napi_value theBuffer; | ||
char *theCopy; | ||
NAPI_CALL(env, napi_create_buffer(env, sizeof(theText), &theCopy, | ||
&theBuffer)); | ||
JS_ASSERT(env, theCopy, "Failed to copy static text for newBuffer"); | ||
strcpy(theCopy, theText); | ||
NAPI_CALL(env, napi_set_return_value(env, info, theBuffer)); | ||
} | ||
|
||
NAPI_METHOD(newExternalBuffer) { | ||
napi_value theBuffer; | ||
char *theCopy = strdup(theText); | ||
JS_ASSERT(env, theCopy, | ||
"Failed to copy static text for newExternalBuffer"); | ||
NAPI_CALL(env, napi_create_external_buffer(env, sizeof(theText), theCopy, | ||
deleteTheText, &theBuffer)); | ||
NAPI_CALL(env, napi_set_return_value(env, info, theBuffer)); | ||
} | ||
|
||
NAPI_METHOD(getDeleterCallCount) { | ||
napi_value callCount; | ||
NAPI_CALL(env, napi_create_number(env, deleterCallCount, &callCount)); | ||
NAPI_CALL(env, napi_set_return_value(env, info, callCount)); | ||
} | ||
|
||
NAPI_METHOD(copyBuffer) { | ||
napi_value theBuffer; | ||
NAPI_CALL(env, napi_create_buffer_copy(env, theText, sizeof(theText), | ||
&theBuffer)); | ||
NAPI_CALL(env, napi_set_return_value(env, info, theBuffer)); | ||
} | ||
|
||
NAPI_METHOD(bufferHasInstance) { | ||
int argc; | ||
NAPI_CALL(env, napi_get_cb_args_length(env, info, &argc)); | ||
JS_ASSERT(env, argc == 1, "Wrong number of arguments"); | ||
napi_value theBuffer; | ||
NAPI_CALL(env, napi_get_cb_args(env, info, &theBuffer, 1)); | ||
bool hasInstance; | ||
napi_valuetype theType; | ||
NAPI_CALL(env, napi_get_type_of_value(env, theBuffer, &theType)); | ||
JS_ASSERT(env, theType == napi_object, | ||
"bufferHasInstance: instance is not an object"); | ||
NAPI_CALL(env, napi_is_buffer(env, theBuffer, &hasInstance)); | ||
JS_ASSERT(env, hasInstance, "bufferHasInstance: instance is not a buffer"); | ||
napi_value returnValue; | ||
NAPI_CALL(env, napi_create_boolean(env, hasInstance, &returnValue)); | ||
NAPI_CALL(env, napi_set_return_value(env, info, returnValue)); | ||
} | ||
|
||
NAPI_METHOD(bufferInfo) { | ||
int argc; | ||
NAPI_CALL(env, napi_get_cb_args_length(env, info, &argc)); | ||
JS_ASSERT(env, argc == 1, "Wrong number of arguments"); | ||
napi_value theBuffer, returnValue; | ||
NAPI_CALL(env, napi_get_cb_args(env, info, &theBuffer, 1)); | ||
char *bufferData; | ||
size_t bufferLength; | ||
NAPI_CALL(env, napi_get_buffer_info(env, theBuffer, &bufferData, | ||
&bufferLength)); | ||
NAPI_CALL(env, napi_create_boolean(env, | ||
!strcmp(bufferData, theText) && bufferLength == sizeof(theText), | ||
&returnValue)); | ||
NAPI_CALL(env, napi_set_return_value(env, info, returnValue)); | ||
} | ||
|
||
NAPI_METHOD(staticBuffer) { | ||
napi_value theBuffer; | ||
NAPI_CALL(env, napi_create_external_buffer(env, sizeof(theText), | ||
(char *)theText, noopDeleter, &theBuffer)); | ||
NAPI_CALL(env, napi_set_return_value(env, info, theBuffer)); | ||
} | ||
|
||
void Init(napi_env env, napi_value exports, napi_value module) { | ||
napi_propertyname propName; | ||
napi_value theValue; | ||
|
||
NAPI_CALL(env, napi_property_name(env, "theText", &propName)); | ||
NAPI_CALL(env, napi_create_string_utf8(env, theText, sizeof(theText), &theValue)); | ||
NAPI_CALL(env, napi_set_property(env, exports, propName, theValue)); | ||
|
||
napi_property_descriptor methods[] = { | ||
{ "newBuffer", newBuffer }, | ||
{ "newExternalBuffer", newExternalBuffer }, | ||
{ "getDeleterCallCount", getDeleterCallCount }, | ||
{ "copyBuffer", copyBuffer }, | ||
{ "bufferHasInstance", bufferHasInstance }, | ||
{ "bufferInfo", bufferInfo }, | ||
{ "staticBuffer", staticBuffer } | ||
}; | ||
|
||
NAPI_CALL(env, napi_define_properties(env, exports, | ||
sizeof(methods)/sizeof(methods[0]), methods)); | ||
} | ||
|
||
NODE_MODULE_ABI(addon, Init) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.