Skip to content
forked from nodejs/node

Commit 0341910

Browse files
committed
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() These changes were copied directly from nodejs/node-addon-api@c7d4df4 where they were reviewed as part of nodejs/node-addon-api#25
1 parent d7ba2a6 commit 0341910

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

@@ -1694,7 +1695,7 @@ napi_status napi_get_value_int32(napi_env env,
16941695

16951696
v8::Isolate* isolate = env->isolate;
16961697
v8::Local<v8::Context> context = isolate->GetCurrentContext();
1697-
*result = val->Int32Value(context).ToChecked();
1698+
*result = val->Int32Value(context).FromJust();
16981699

16991700
return napi_clear_last_error(env);
17001701
}
@@ -1713,7 +1714,7 @@ napi_status napi_get_value_uint32(napi_env env,
17131714

17141715
v8::Isolate* isolate = env->isolate;
17151716
v8::Local<v8::Context> context = isolate->GetCurrentContext();
1716-
*result = val->Uint32Value(context).ToChecked();
1717+
*result = val->Uint32Value(context).FromJust();
17171718

17181719
return napi_clear_last_error(env);
17191720
}
@@ -1738,7 +1739,7 @@ napi_status napi_get_value_int64(napi_env env,
17381739
} else {
17391740
v8::Isolate* isolate = env->isolate;
17401741
v8::Local<v8::Context> context = isolate->GetCurrentContext();
1741-
*result = val->IntegerValue(context).ToChecked();
1742+
*result = val->IntegerValue(context).FromJust();
17421743
}
17431744

17441745
return napi_clear_last_error(env);
@@ -2819,9 +2820,11 @@ napi_status napi_queue_async_work(napi_env env, napi_async_work work) {
28192820
CHECK_ENV(env);
28202821
CHECK_ARG(env, work);
28212822

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

28262829
uvimpl::Work* w = reinterpret_cast<uvimpl::Work*>(work);
28272830

0 commit comments

Comments
 (0)