Skip to content

Commit

Permalink
Merge pull request nodejs#145 from boingoing/change_buffer_data_to_vo…
Browse files Browse the repository at this point in the history
…id_type

napi: Buffer data argument should be void* instead of char*
  • Loading branch information
boingoing authored Mar 15, 2017
2 parents df2a9e2 + 40d26cf commit e63b9ea
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
14 changes: 8 additions & 6 deletions src/node_jsvmapi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2020,7 +2020,7 @@ napi_status napi_get_and_clear_last_exception(napi_env e, napi_value* result) {
return napi_ok;
}

napi_status napi_create_buffer(napi_env e, size_t size, char** data,
napi_status napi_create_buffer(napi_env e, size_t size, void** data,
napi_value* result) {
NAPI_PREAMBLE(e);
CHECK_ARG(data);
Expand All @@ -2038,13 +2038,14 @@ napi_status napi_create_buffer(napi_env e, size_t size, char** data,
return GET_RETURN_STATUS();
}

napi_status napi_create_external_buffer(napi_env e, size_t size, char* data,
napi_status napi_create_external_buffer(napi_env e, size_t size, void* data,
napi_finalize finalize_cb,
napi_value* result) {
NAPI_PREAMBLE(e);
CHECK_ARG(result);

auto maybe = node::Buffer::New(v8impl::V8IsolateFromJsEnv(e), data, size,
auto maybe = node::Buffer::New(v8impl::V8IsolateFromJsEnv(e),
static_cast<char*>(data), size,
(node::Buffer::FreeCallback)finalize_cb, nullptr);

CHECK_MAYBE_EMPTY(maybe, napi_generic_failure);
Expand All @@ -2053,12 +2054,13 @@ napi_status napi_create_external_buffer(napi_env e, size_t size, char* data,
return GET_RETURN_STATUS();
}

napi_status napi_create_buffer_copy(napi_env e, const char* data,
napi_status napi_create_buffer_copy(napi_env e, const void* data,
size_t size, napi_value* result) {
NAPI_PREAMBLE(e);
CHECK_ARG(result);

auto maybe = node::Buffer::Copy(v8impl::V8IsolateFromJsEnv(e), data, size);
auto maybe = node::Buffer::Copy(v8impl::V8IsolateFromJsEnv(e),
static_cast<const char*>(data), size);

CHECK_MAYBE_EMPTY(maybe, napi_generic_failure);

Expand All @@ -2074,7 +2076,7 @@ napi_status napi_is_buffer(napi_env e, napi_value v, bool* result) {
return GET_RETURN_STATUS();
}

napi_status napi_get_buffer_info(napi_env e, napi_value v, char** data,
napi_status napi_get_buffer_info(napi_env e, napi_value v, void** data,
size_t* length) {
NAPI_PREAMBLE(e);

Expand Down
8 changes: 4 additions & 4 deletions src/node_jsvmapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -386,21 +386,21 @@ NODE_EXTERN napi_status napi_get_and_clear_last_exception(napi_env e,
// Methods to provide node::Buffer functionality with napi types
NODE_EXTERN napi_status napi_create_buffer(napi_env e,
size_t size,
char** data,
void** data,
napi_value* result);
NODE_EXTERN napi_status napi_create_external_buffer(napi_env e,
size_t size,
char* data,
void* data,
napi_finalize finalize_cb,
napi_value* result);
NODE_EXTERN napi_status napi_create_buffer_copy(napi_env e,
const char* data,
const void* data,
size_t size,
napi_value* result);
NODE_EXTERN napi_status napi_is_buffer(napi_env e, napi_value v, bool* result);
NODE_EXTERN napi_status napi_get_buffer_info(napi_env e,
napi_value v,
char** data,
void** data,
size_t* length);

// Methods to work with array buffers and typed arrays
Expand Down
6 changes: 3 additions & 3 deletions test/addons-abi/test_buffer/test_buffer.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <string.h>
#include <string.h>
#include <string>
#include <node_jsvmapi.h>

Expand Down Expand Up @@ -32,7 +32,7 @@ static void noopDeleter(void *data) {
void newBuffer(napi_env env, napi_callback_info info) {
napi_value theBuffer;
char *theCopy;
NAPI_CALL(env, napi_create_buffer(env, sizeof(theText), &theCopy,
NAPI_CALL(env, napi_create_buffer(env, sizeof(theText), (void**)&theCopy,
&theBuffer));
JS_ASSERT(env, theCopy, "Failed to copy static text for newBuffer");
strcpy(theCopy, theText);
Expand Down Expand Up @@ -88,7 +88,7 @@ void bufferInfo(napi_env env, napi_callback_info info) {
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,
NAPI_CALL(env, napi_get_buffer_info(env, theBuffer, (void**)&bufferData,
&bufferLength));
NAPI_CALL(env, napi_create_boolean(env,
!strcmp(bufferData, theText) && bufferLength == sizeof(theText),
Expand Down

0 comments on commit e63b9ea

Please sign in to comment.