Skip to content

Commit 32412a8

Browse files
Gabriel SchulhofMylesBorins
Gabriel Schulhof
authored andcommittedApr 16, 2018
n-api: make changes for source compatibility
These changes are necessary in order to retain source compatibility with older versions of node. The removal of `module_version` from `napi_module_register()` is reverted from the flag removal PR, because it should not have been a part of it. `CallbackWrapper::NewTarget()` is renamed to `CallbackWrapper::GetNewTarget()` to distinguish it from V8's native method, thus allowing for a macro which reduces `NewTarget()` to `This()` on V8 versions where it was not yet available. `AsyncResource` is constructed with a C string rather than a V8 string because the former is available on all versions of node. Backport-PR-URL: #19447 PR-URL: #16102 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
1 parent 5bba809 commit 32412a8

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed
 

‎src/node_api.cc

+10-6
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ class CallbackWrapper {
461461
CallbackWrapper(napi_value this_arg, size_t args_length, void* data)
462462
: _this(this_arg), _args_length(args_length), _data(data) {}
463463

464-
virtual napi_value NewTarget() = 0;
464+
virtual napi_value GetNewTarget() = 0;
465465
virtual void Args(napi_value* buffer, size_t bufferlength) = 0;
466466
virtual void SetReturnValue(napi_value value) = 0;
467467

@@ -490,7 +490,7 @@ class CallbackWrapperBase : public CallbackWrapper {
490490
->Value();
491491
}
492492

493-
napi_value NewTarget() override { return nullptr; }
493+
napi_value GetNewTarget() override { return nullptr; }
494494

495495
protected:
496496
void InvokeCallback() {
@@ -538,7 +538,7 @@ class FunctionCallbackWrapper
538538
const v8::FunctionCallbackInfo<v8::Value>& cbinfo)
539539
: CallbackWrapperBase(cbinfo, cbinfo.Length()) {}
540540

541-
napi_value NewTarget() override {
541+
napi_value GetNewTarget() override {
542542
if (_cbinfo.IsConstructCall()) {
543543
return v8impl::JsValueFromV8LocalValue(_cbinfo.NewTarget());
544544
} else {
@@ -858,8 +858,12 @@ void napi_module_register_cb(v8::Local<v8::Object> exports,
858858

859859
// Registers a NAPI module.
860860
void napi_module_register(napi_module* mod) {
861+
int module_version = -1;
862+
#ifdef EXTERNAL_NAPI
863+
module_version = NODE_MODULE_VERSION;
864+
#endif // EXTERNAL_NAPI
861865
node::node_module* nm = new node::node_module {
862-
-1,
866+
module_version,
863867
mod->nm_flags,
864868
nullptr,
865869
mod->nm_filename,
@@ -1912,7 +1916,7 @@ napi_status napi_get_new_target(napi_env env,
19121916
v8impl::CallbackWrapper* info =
19131917
reinterpret_cast<v8impl::CallbackWrapper*>(cbinfo);
19141918

1915-
*result = info->NewTarget();
1919+
*result = info->GetNewTarget();
19161920
return napi_clear_last_error(env);
19171921
}
19181922

@@ -3339,7 +3343,7 @@ class Work : public node::AsyncResource {
33393343
void* data = nullptr)
33403344
: AsyncResource(env->isolate,
33413345
async_resource,
3342-
async_resource_name),
3346+
*v8::String::Utf8Value(async_resource_name)),
33433347
_env(env),
33443348
_data(data),
33453349
_execute(execute),

‎src/node_api_backport.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ CallbackScope::~CallbackScope() {
7373

7474
AsyncResource::AsyncResource(v8::Isolate* _isolate,
7575
v8::Local<v8::Object> _object,
76-
v8::Local<v8::String> name) : isolate(_isolate) {
76+
char* name) : isolate(_isolate) {
7777
object.Reset(isolate, _object);
7878
}
7979

‎src/node_api_backport.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class AsyncResource {
5050
public:
5151
AsyncResource(v8::Isolate* _isolate,
5252
v8::Local<v8::Object> _object,
53-
v8::Local<v8::String> name);
53+
char* name);
5454
~AsyncResource();
5555
v8::Isolate* isolate;
5656
v8::Persistent<v8::Object> object;

0 commit comments

Comments
 (0)
Please sign in to comment.