Skip to content

Commit 972bfe1

Browse files
jasonginmhdawson
authored andcommitted
n-api: Sync with back-compat changes
Background: To enable N-API support for node versions back to v4, the N-API code can also be built as an external addon. To make maintenance easier, a single codebase needs to support both built-in and external scenarios, along with Node versions >= 4 (and corresponding V8 versions). This change includes several minor fixes to avoid using node internal APIs and support older V8 versions: - Expand node::arraysize - In the CHECK_ENV() macro, return an error code instead of calling node::FatalError(). This is more consistent with how other invalid arguments to N-API functions are handled. - In v8impl::SetterCallbackWrapper::SetReturnValue(), do nothing instead of calling node::FatalError(). This is more consistent with JavaScript setter callbacks, where any returned value is silently ignored. - When queueing async work items, get the uv default loop instead of getting the loop from node::Environment::GetCurrent(). Currently that returns the same loop anyway. If/when node supports multiple environments, it should have a public API for getting the environment & event loop, and we can update this implementation then. - Use v8::Maybe::FromJust() instead of the newer alias ToChecked() PR-URL: #12674 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent a4fd9e5 commit 972bfe1

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

src/node_api.cc

+16-13
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
#include <node_object_wrap.h>
1313
#include <string.h>
1414
#include <algorithm>
15+
#include <cassert>
1516
#include <cmath>
1617
#include <vector>
18+
#include "uv.h"
1719
#include "node_api.h"
18-
#include "env-inl.h"
1920

2021
static
2122
napi_status napi_set_last_error(napi_env env, napi_status error_code,
@@ -45,9 +46,9 @@ struct napi_env__ {
4546
} \
4647
} while (0)
4748

48-
#define CHECK_ENV(env) \
49-
if ((env) == nullptr) { \
50-
node::FatalError(__func__, "environment(env) must not be null"); \
49+
#define CHECK_ENV(env) \
50+
if ((env) == nullptr) { \
51+
return napi_invalid_arg; \
5152
}
5253

5354
#define CHECK_ARG(env, arg) \
@@ -578,8 +579,7 @@ class SetterCallbackWrapper
578579

579580
/*virtual*/
580581
void SetReturnValue(napi_value value) override {
581-
node::FatalError("napi_set_return_value",
582-
"Cannot return a value from a setter callback.");
582+
// Ignore any value returned from a setter callback.
583583
}
584584

585585
private:
@@ -744,7 +744,8 @@ napi_status napi_get_last_error_info(napi_env env,
744744
CHECK_ENV(env);
745745
CHECK_ARG(env, result);
746746

747-
static_assert(node::arraysize(error_messages) == napi_status_last,
747+
static_assert(
748+
(sizeof (error_messages) / sizeof (*error_messages)) == napi_status_last,
748749
"Count of error messages must match count of error values");
749750
assert(env->last_error.error_code < napi_status_last);
750751

@@ -1696,7 +1697,7 @@ napi_status napi_get_value_int32(napi_env env,
16961697

16971698
v8::Isolate* isolate = env->isolate;
16981699
v8::Local<v8::Context> context = isolate->GetCurrentContext();
1699-
*result = val->Int32Value(context).ToChecked();
1700+
*result = val->Int32Value(context).FromJust();
17001701

17011702
return napi_clear_last_error(env);
17021703
}
@@ -1715,7 +1716,7 @@ napi_status napi_get_value_uint32(napi_env env,
17151716

17161717
v8::Isolate* isolate = env->isolate;
17171718
v8::Local<v8::Context> context = isolate->GetCurrentContext();
1718-
*result = val->Uint32Value(context).ToChecked();
1719+
*result = val->Uint32Value(context).FromJust();
17191720

17201721
return napi_clear_last_error(env);
17211722
}
@@ -1740,7 +1741,7 @@ napi_status napi_get_value_int64(napi_env env,
17401741
} else {
17411742
v8::Isolate* isolate = env->isolate;
17421743
v8::Local<v8::Context> context = isolate->GetCurrentContext();
1743-
*result = val->IntegerValue(context).ToChecked();
1744+
*result = val->IntegerValue(context).FromJust();
17441745
}
17451746

17461747
return napi_clear_last_error(env);
@@ -2821,9 +2822,11 @@ napi_status napi_queue_async_work(napi_env env, napi_async_work work) {
28212822
CHECK_ENV(env);
28222823
CHECK_ARG(env, work);
28232824

2824-
// Consider: Encapsulate the uv_loop_t into an opaque pointer parameter
2825-
uv_loop_t* event_loop =
2826-
node::Environment::GetCurrent(env->isolate)->event_loop();
2825+
// Consider: Encapsulate the uv_loop_t into an opaque pointer parameter.
2826+
// Currently the environment event loop is the same as the UV default loop.
2827+
// Someday (if node ever supports multiple isolates), it may be better to get
2828+
// the loop from node::Environment::GetCurrent(env->isolate)->event_loop();
2829+
uv_loop_t* event_loop = uv_default_loop();
28272830

28282831
uvimpl::Work* w = reinterpret_cast<uvimpl::Work*>(work);
28292832

0 commit comments

Comments
 (0)