Skip to content

Commit cf6e213

Browse files
Gabriel Schulhoftargos
Gabriel Schulhof
authored andcommitted
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. PR-URL: #16102 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
1 parent 014a48b commit cf6e213

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/node_api.cc

+10-6
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ class CallbackWrapper {
455455
CallbackWrapper(napi_value this_arg, size_t args_length, void* data)
456456
: _this(this_arg), _args_length(args_length), _data(data) {}
457457

458-
virtual napi_value NewTarget() = 0;
458+
virtual napi_value GetNewTarget() = 0;
459459
virtual void Args(napi_value* buffer, size_t bufferlength) = 0;
460460
virtual void SetReturnValue(napi_value value) = 0;
461461

@@ -484,7 +484,7 @@ class CallbackWrapperBase : public CallbackWrapper {
484484
->Value();
485485
}
486486

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

489489
protected:
490490
void InvokeCallback() {
@@ -532,7 +532,7 @@ class FunctionCallbackWrapper
532532
const v8::FunctionCallbackInfo<v8::Value>& cbinfo)
533533
: CallbackWrapperBase(cbinfo, cbinfo.Length()) {}
534534

535-
napi_value NewTarget() override {
535+
napi_value GetNewTarget() override {
536536
if (_cbinfo.IsConstructCall()) {
537537
return v8impl::JsValueFromV8LocalValue(_cbinfo.NewTarget());
538538
} else {
@@ -854,8 +854,12 @@ void napi_module_register_cb(v8::Local<v8::Object> exports,
854854

855855
// Registers a NAPI module.
856856
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
857861
node::node_module* nm = new node::node_module {
858-
-1,
862+
module_version,
859863
mod->nm_flags,
860864
nullptr,
861865
mod->nm_filename,
@@ -1908,7 +1912,7 @@ napi_status napi_get_new_target(napi_env env,
19081912
v8impl::CallbackWrapper* info =
19091913
reinterpret_cast<v8impl::CallbackWrapper*>(cbinfo);
19101914

1911-
*result = info->NewTarget();
1915+
*result = info->GetNewTarget();
19121916
return napi_clear_last_error(env);
19131917
}
19141918

@@ -3335,7 +3339,7 @@ class Work : public node::AsyncResource {
33353339
void* data = nullptr)
33363340
: AsyncResource(env->isolate,
33373341
async_resource,
3338-
async_resource_name),
3342+
*v8::String::Utf8Value(async_resource_name)),
33393343
_env(env),
33403344
_data(data),
33413345
_execute(execute),

0 commit comments

Comments
 (0)