Skip to content

Commit

Permalink
Add internals for async context and NewTarget
Browse files Browse the repository at this point in the history
PR-URL: nodejs/node-addon-api#154
Reviewed-By: Jason Ginchereau <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
  • Loading branch information
wroy7860 committed Oct 11, 2017
1 parent b62a373 commit 153d868
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <node_buffer.h>
#include <node_object_wrap.h>
#include <limits.h> // INT_MAX
#include <string.h>
#include <algorithm>
#include <cmath>
Expand Down Expand Up @@ -125,6 +126,9 @@ struct napi_env__ {
do { \
static_assert(static_cast<int>(NAPI_AUTO_LENGTH) == -1, \
"Casting NAPI_AUTO_LENGTH to int must result in -1"); \
RETURN_STATUS_IF_FALSE((env), \
(len == NAPI_AUTO_LENGTH) || len <= INT_MAX, \
napi_invalid_arg); \
auto str_maybe = v8::String::NewFromUtf8( \
(env)->isolate, (str), v8::NewStringType::kInternalized, \
static_cast<int>(len)); \
Expand Down Expand Up @@ -451,7 +455,7 @@ class CallbackWrapper {
CallbackWrapper(napi_value this_arg, size_t args_length, void* data)
: _this(this_arg), _args_length(args_length), _data(data) {}

virtual napi_value NewTarget() = 0;
virtual napi_value GetNewTarget() = 0;
virtual void Args(napi_value* buffer, size_t bufferlength) = 0;
virtual void SetReturnValue(napi_value value) = 0;

Expand Down Expand Up @@ -480,7 +484,7 @@ class CallbackWrapperBase : public CallbackWrapper {
->Value();
}

napi_value NewTarget() override { return nullptr; }
napi_value GetNewTarget() override { return nullptr; }

protected:
void InvokeCallback() {
Expand Down Expand Up @@ -528,7 +532,7 @@ class FunctionCallbackWrapper
const v8::FunctionCallbackInfo<v8::Value>& cbinfo)
: CallbackWrapperBase(cbinfo, cbinfo.Length()) {}

napi_value NewTarget() override {
napi_value GetNewTarget() override {
if (_cbinfo.IsConstructCall()) {
return v8impl::JsValueFromV8LocalValue(_cbinfo.NewTarget());
} else {
Expand Down Expand Up @@ -850,8 +854,12 @@ void napi_module_register_cb(v8::Local<v8::Object> exports,

// Registers a NAPI module.
void napi_module_register(napi_module* mod) {
int module_version = -1;
#ifdef EXTERNAL_NAPI
module_version = NODE_MODULE_VERSION;
#endif // EXTERNAL_NAPI
node::node_module* nm = new node::node_module {
-1,
module_version,
mod->nm_flags,
nullptr,
mod->nm_filename,
Expand All @@ -866,7 +874,7 @@ void napi_module_register(napi_module* mod) {

// Warning: Keep in-sync with napi_status enum
const char* error_messages[] = {nullptr,
"Invalid pointer passed as argument",
"Invalid argument",
"An object was expected",
"A string was expected",
"A string or symbol was expected",
Expand Down Expand Up @@ -1904,7 +1912,7 @@ napi_status napi_get_new_target(napi_env env,
v8impl::CallbackWrapper* info =
reinterpret_cast<v8impl::CallbackWrapper*>(cbinfo);

*result = info->NewTarget();
*result = info->GetNewTarget();
return napi_clear_last_error(env);
}

Expand Down Expand Up @@ -3331,7 +3339,7 @@ class Work : public node::AsyncResource {
void* data = nullptr)
: AsyncResource(env->isolate,
async_resource,
async_resource_name),
*v8::String::Utf8Value(async_resource_name)),
_env(env),
_data(data),
_execute(execute),
Expand Down
34 changes: 34 additions & 0 deletions src/node_internals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,42 @@
#include <unistd.h> // getpid
#endif

#if NODE_MAJOR_VERSION < 8
CallbackScope::CallbackScope(void *work) {
}
#endif // NODE_MAJOR_VERSION < 8

namespace node {

#if NODE_MAJOR_VERSION < 8

async_context EmitAsyncInit(v8::Isolate* isolate,
v8::Local<v8::Object> resource,
v8::Local<v8::String> name,
async_id trigger_async_id) {
return async_context();
}

void EmitAsyncDestroy(v8::Isolate* isolate,
async_context asyncContext) {
}

v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
v8::Local<v8::Object> recv,
v8::Local<v8::Function> callback,
int argc,
v8::Local<v8::Value>* argv,
async_context asyncContext) {
return node::MakeCallback(isolate, recv, callback, argc, argv);
}

AsyncResource::AsyncResource(v8::Isolate* isolate,
v8::Local<v8::Object> object,
const char *name) {
}

#endif // NODE_MAJOR_VERSION < 8

static void PrintErrorString(const char* format, ...) {
va_list ap;
va_start(ap, format);
Expand Down
46 changes: 46 additions & 0 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <stdint.h>
#include "uv.h"
#include "node.h"
#include <string>

// Windows 8+ does not like abort() in Release mode
#ifdef _WIN32
Expand Down Expand Up @@ -60,8 +61,49 @@
#define NODE_RELEASE "node"
#endif

#if NODE_MAJOR_VERSION < 8 || NODE_MAJOR_VERSION == 8 && NODE_MINOR_VERSION < 6
class CallbackScope {
public:
CallbackScope(void *work);
};
#endif // NODE_MAJOR_VERSION < 8

namespace node {

#if NODE_MAJOR_VERSION < 8 || NODE_MAJOR_VERSION == 8 && NODE_MINOR_VERSION < 6
typedef int async_id;

typedef struct async_context {
node::async_id async_id;
node::async_id trigger_async_id;
} async_context;

NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
v8::Local<v8::Object> resource,
v8::Local<v8::String> name,
async_id trigger_async_id = -1);

NODE_EXTERN void EmitAsyncDestroy(v8::Isolate* isolate,
async_context asyncContext);

v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
v8::Local<v8::Object> recv,
v8::Local<v8::Function> callback,
int argc,
v8::Local<v8::Value>* argv,
async_context asyncContext);

#if NODE_MAJOR_VERSION < 8
class AsyncResource {
public:
AsyncResource(v8::Isolate* isolate,
v8::Local<v8::Object> object,
const char *name);
};
#endif // node version below 8

#endif // node version below 8.6

// The slightly odd function signature for Assert() is to ease
// instruction cache pressure in calls from ASSERT and CHECK.
NO_RETURN void Abort();
Expand All @@ -75,6 +117,10 @@ NO_RETURN void FatalError(const char* location, const char* message);

} // namespace node

#if NODE_MAJOR_VERSION < 8
#define NewTarget This
#endif // NODE_MAJOR_VERSION < 8

#if NODE_MAJOR_VERSION < 6
namespace v8 {
namespace Private {
Expand Down

0 comments on commit 153d868

Please sign in to comment.