Skip to content

Commit

Permalink
Fix uv_work_t memory abi compatibility with node 0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
sampsongao committed Nov 3, 2016
1 parent 7474efa commit 8413f2a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 22 deletions.
37 changes: 37 additions & 0 deletions src/node_jsvmapi.cc
Expand Up @@ -863,6 +863,43 @@ size_t napi_buffer_length(napi_env e, napi_value v) {
return node::Buffer::Length(v8impl::V8LocalValueFromJsValue(v));
}

uv_work_t* napi_create_uv_work_t() {
uv_work_t* req = (uv_work_t*) malloc(sizeof(uv_work_t));
return req;
}

void napi_delete_uv_work_t(uv_work_t* w) {
if (w != NULL) {
delete w;
w = NULL;
}
}


///////////////////////////////////////////
// AsyncWorker Methods
///////////////////////////////////////////
namespace Napi {
void AsyncExecute(uv_work_t* req) {
AsyncWorker *worker = static_cast<AsyncWorker*>(req->data);
worker->Execute();
}

void AsyncExecuteComplete(uv_work_t* req) {
AsyncWorker* worker = static_cast<AsyncWorker*>(req->data);
worker->WorkComplete();
worker->Destroy();
}

void AsyncQueueWorker(AsyncWorker* worker) {
uv_queue_work(
uv_default_loop(),
worker->request,
AsyncExecute,
reinterpret_cast<uv_after_work_cb>(AsyncExecuteComplete));
}
} // namespace Napi


///////////////////////////////////////////
// WILL GO AWAY
Expand Down
39 changes: 17 additions & 22 deletions src/node_jsvmapi.h
Expand Up @@ -213,7 +213,14 @@ NODE_EXTERN napi_value napi_buffer_copy(napi_env e,
NODE_EXTERN bool napi_buffer_has_instance(napi_env e, napi_value v);
NODE_EXTERN char* napi_buffer_data(napi_env e, napi_value v);
NODE_EXTERN size_t napi_buffer_length(napi_env e, napi_value v);
} // extern "C"


#include<uv.h>

extern "C" {
NODE_EXTERN uv_work_t* napi_create_uv_work_t();
NODE_EXTERN void napi_delete_uv_work_t(uv_work_t* w);
} // extern "C"

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -227,7 +234,6 @@ NODE_EXTERN size_t napi_buffer_length(napi_env e, napi_value v);
////////////////////////////////////////////////////////////////////////////////
#include <limits.h>
#include <string.h>
#include <uv.h>

#define NAPI_METHOD(name) \
void name(napi_env env, napi_func_cb_info info)
Expand Down Expand Up @@ -461,7 +467,8 @@ namespace Napi {
public:
explicit AsyncWorker(Callback *callback_)
: callback(callback_), errmsg_(nullptr) {
request.data = this;
request = napi_create_uv_work_t();
request->data = this;
napi_env env = napi_get_current_env();

HandleScope scope;
Expand All @@ -479,6 +486,8 @@ namespace Napi {
}
delete callback;
delete[] errmsg_;

napi_delete_uv_work_t(request);
}

virtual void WorkComplete() {
Expand Down Expand Up @@ -545,7 +554,7 @@ namespace Napi {

virtual void Execute() = 0;

uv_work_t request;
uv_work_t* request;

virtual void Destroy() {
delete this;
Expand Down Expand Up @@ -586,25 +595,11 @@ namespace Napi {
char *errmsg_;
};

inline void AsyncExecute(uv_work_t* req) {
AsyncWorker *worker = static_cast<AsyncWorker*>(req->data);
worker->Execute();
}

inline void AsyncExecuteComplete(uv_work_t* req) {
AsyncWorker* worker = static_cast<AsyncWorker*>(req->data);
worker->WorkComplete();
worker->Destroy();
}

inline void AsyncQueueWorker(AsyncWorker* worker) {
uv_queue_work(
uv_default_loop(),
&worker->request,
AsyncExecute,
reinterpret_cast<uv_after_work_cb>(AsyncExecuteComplete));
}
} // namespace Napi
void AsyncExecute(uv_work_t* req);
void AsyncExecuteComplete(uv_work_t* req);
void AsyncQueueWorker(AsyncWorker* worker);
} // namespace Napi


////////////////////////////////////////////////////////////////////////////////
// WILL GO AWAY (these can't be extern "C" because they work with C++ types)
Expand Down

0 comments on commit 8413f2a

Please sign in to comment.