diff --git a/src/node_api.cc b/src/node_api.cc index fbf2087..5728ccc 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -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> @@ -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)); \ @@ -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; @@ -480,7 +484,7 @@ class CallbackWrapperBase : public CallbackWrapper { ->Value(); } - napi_value NewTarget() override { return nullptr; } + napi_value GetNewTarget() override { return nullptr; } protected: void InvokeCallback() { @@ -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 { @@ -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, @@ -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", @@ -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); } @@ -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), diff --git a/src/node_internals.cc b/src/node_internals.cc index ccdf880..2ff8afe 100644 --- a/src/node_internals.cc +++ b/src/node_internals.cc @@ -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); diff --git a/src/node_internals.h b/src/node_internals.h index 0ead169..43744fa 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -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 @@ -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(); @@ -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 {