Skip to content

Commit 1a6d76e

Browse files
Gabriel Schulhofmhdawson
authored andcommitted
Add internals for async context and NewTarget
PR-URL: #154 Reviewed-By: Jason Ginchereau <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent b1c4cb9 commit 1a6d76e

File tree

3 files changed

+95
-7
lines changed

3 files changed

+95
-7
lines changed

src/node_api.cc

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <node_buffer.h>
1212
#include <node_object_wrap.h>
13+
#include <limits.h> // INT_MAX
1314
#include <string.h>
1415
#include <algorithm>
1516
#include <cmath>
@@ -125,6 +126,9 @@ struct napi_env__ {
125126
do { \
126127
static_assert(static_cast<int>(NAPI_AUTO_LENGTH) == -1, \
127128
"Casting NAPI_AUTO_LENGTH to int must result in -1"); \
129+
RETURN_STATUS_IF_FALSE((env), \
130+
(len == NAPI_AUTO_LENGTH) || len <= INT_MAX, \
131+
napi_invalid_arg); \
128132
auto str_maybe = v8::String::NewFromUtf8( \
129133
(env)->isolate, (str), v8::NewStringType::kInternalized, \
130134
static_cast<int>(len)); \
@@ -451,7 +455,7 @@ class CallbackWrapper {
451455
CallbackWrapper(napi_value this_arg, size_t args_length, void* data)
452456
: _this(this_arg), _args_length(args_length), _data(data) {}
453457

454-
virtual napi_value NewTarget() = 0;
458+
virtual napi_value GetNewTarget() = 0;
455459
virtual void Args(napi_value* buffer, size_t bufferlength) = 0;
456460
virtual void SetReturnValue(napi_value value) = 0;
457461

@@ -480,7 +484,7 @@ class CallbackWrapperBase : public CallbackWrapper {
480484
->Value();
481485
}
482486

483-
napi_value NewTarget() override { return nullptr; }
487+
napi_value GetNewTarget() override { return nullptr; }
484488

485489
protected:
486490
void InvokeCallback() {
@@ -528,7 +532,7 @@ class FunctionCallbackWrapper
528532
const v8::FunctionCallbackInfo<v8::Value>& cbinfo)
529533
: CallbackWrapperBase(cbinfo, cbinfo.Length()) {}
530534

531-
napi_value NewTarget() override {
535+
napi_value GetNewTarget() override {
532536
if (_cbinfo.IsConstructCall()) {
533537
return v8impl::JsValueFromV8LocalValue(_cbinfo.NewTarget());
534538
} else {
@@ -850,8 +854,12 @@ void napi_module_register_cb(v8::Local<v8::Object> exports,
850854

851855
// Registers a NAPI module.
852856
void napi_module_register(napi_module* mod) {
857+
int module_version = -1;
858+
#ifdef EXTERNAL_NAPI
859+
module_version = NODE_MODULE_VERSION;
860+
#endif // EXTERNAL_NAPI
853861
node::node_module* nm = new node::node_module {
854-
-1,
862+
module_version,
855863
mod->nm_flags,
856864
nullptr,
857865
mod->nm_filename,
@@ -866,7 +874,7 @@ void napi_module_register(napi_module* mod) {
866874

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

1907-
*result = info->NewTarget();
1915+
*result = info->GetNewTarget();
19081916
return napi_clear_last_error(env);
19091917
}
19101918

@@ -3331,7 +3339,7 @@ class Work : public node::AsyncResource {
33313339
void* data = nullptr)
33323340
: AsyncResource(env->isolate,
33333341
async_resource,
3334-
async_resource_name),
3342+
*v8::String::Utf8Value(async_resource_name)),
33353343
_env(env),
33363344
_data(data),
33373345
_execute(execute),

src/node_internals.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,42 @@
1010
#include <unistd.h> // getpid
1111
#endif
1212

13+
#if NODE_MAJOR_VERSION < 8
14+
CallbackScope::CallbackScope(void *work) {
15+
}
16+
#endif // NODE_MAJOR_VERSION < 8
17+
1318
namespace node {
1419

20+
#if NODE_MAJOR_VERSION < 8
21+
22+
async_context EmitAsyncInit(v8::Isolate* isolate,
23+
v8::Local<v8::Object> resource,
24+
v8::Local<v8::String> name,
25+
async_id trigger_async_id) {
26+
return async_context();
27+
}
28+
29+
void EmitAsyncDestroy(v8::Isolate* isolate,
30+
async_context asyncContext) {
31+
}
32+
33+
v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
34+
v8::Local<v8::Object> recv,
35+
v8::Local<v8::Function> callback,
36+
int argc,
37+
v8::Local<v8::Value>* argv,
38+
async_context asyncContext) {
39+
return node::MakeCallback(isolate, recv, callback, argc, argv);
40+
}
41+
42+
AsyncResource::AsyncResource(v8::Isolate* isolate,
43+
v8::Local<v8::Object> object,
44+
const char *name) {
45+
}
46+
47+
#endif // NODE_MAJOR_VERSION < 8
48+
1549
static void PrintErrorString(const char* format, ...) {
1650
va_list ap;
1751
va_start(ap, format);

src/node_internals.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <stdint.h>
1212
#include "uv.h"
1313
#include "node.h"
14+
#include <string>
1415

1516
// Windows 8+ does not like abort() in Release mode
1617
#ifdef _WIN32
@@ -60,8 +61,49 @@
6061
#define NODE_RELEASE "node"
6162
#endif
6263

64+
#if NODE_MAJOR_VERSION < 8 || NODE_MAJOR_VERSION == 8 && NODE_MINOR_VERSION < 6
65+
class CallbackScope {
66+
public:
67+
CallbackScope(void *work);
68+
};
69+
#endif // NODE_MAJOR_VERSION < 8
70+
6371
namespace node {
6472

73+
#if NODE_MAJOR_VERSION < 8 || NODE_MAJOR_VERSION == 8 && NODE_MINOR_VERSION < 6
74+
typedef int async_id;
75+
76+
typedef struct async_context {
77+
node::async_id async_id;
78+
node::async_id trigger_async_id;
79+
} async_context;
80+
81+
NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
82+
v8::Local<v8::Object> resource,
83+
v8::Local<v8::String> name,
84+
async_id trigger_async_id = -1);
85+
86+
NODE_EXTERN void EmitAsyncDestroy(v8::Isolate* isolate,
87+
async_context asyncContext);
88+
89+
v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
90+
v8::Local<v8::Object> recv,
91+
v8::Local<v8::Function> callback,
92+
int argc,
93+
v8::Local<v8::Value>* argv,
94+
async_context asyncContext);
95+
96+
#if NODE_MAJOR_VERSION < 8
97+
class AsyncResource {
98+
public:
99+
AsyncResource(v8::Isolate* isolate,
100+
v8::Local<v8::Object> object,
101+
const char *name);
102+
};
103+
#endif // node version below 8
104+
105+
#endif // node version below 8.6
106+
65107
// The slightly odd function signature for Assert() is to ease
66108
// instruction cache pressure in calls from ASSERT and CHECK.
67109
NO_RETURN void Abort();
@@ -75,6 +117,10 @@ NO_RETURN void FatalError(const char* location, const char* message);
75117

76118
} // namespace node
77119

120+
#if NODE_MAJOR_VERSION < 8
121+
#define NewTarget This
122+
#endif // NODE_MAJOR_VERSION < 8
123+
78124
#if NODE_MAJOR_VERSION < 6
79125
namespace v8 {
80126
namespace Private {

0 commit comments

Comments
 (0)