GeezerVison is showing github.com — n-api: napi_is_construct_call->napi_get_new_target · nodejs/node@a2d340f
File treeExpand file treeCollapse file tree Open diff view settings test/addons-napi/6_object_wrap
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/n-api.md +8-9Lines changed: 8 additions & 9 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -2918,25 +2918,24 @@ Returns `napi_ok` if the API succeeded. | 2918 | 2918 | This method is used within a callback function to retrieve details about the | 2919 | 2919 | call like the arguments and the `this` pointer from a given callback info. | 2920 | 2920 |
| 2921 | | -### *napi_is_construct_call* | | 2921 | +### *napi_get_new_target* | 2922 | 2922 | <!-- YAML | 2923 | | -added: v8.0.0 | | 2923 | +added: REPLACEME | 2924 | 2924 | --> | 2925 | 2925 | ```C | 2926 | | -napi_status napi_is_construct_call(napi_env env, | 2927 | | - napi_callback_info cbinfo, | 2928 | | - bool* result) | | 2926 | +napi_status napi_get_new_target(napi_env env, | | 2927 | + napi_callback_info cbinfo, | | 2928 | + napi_value* result) | 2929 | 2929 | ``` | 2930 | 2930 |
| 2931 | 2931 | - `[in] env`: The environment that the API is invoked under. | 2932 | 2932 | - `[in] cbinfo`: The callback info passed into the callback function. | 2933 | | -- `[out] result`: Whether the native function is being invoked as | 2934 | | -a constructor call. | | 2933 | +- `[out] result`: The `new.target` of the constructor call. | 2935 | 2934 |
| 2936 | 2935 | Returns `napi_ok` if the API succeeded. | 2937 | 2936 |
| 2938 | | -This API checks if the the current callback was due to a | 2939 | | -consructor call. | | 2937 | +This API returns the `new.target` of the constructor call. If the current | | 2938 | +callback is not a constructor call, the result is `nullptr`. | 2940 | 2939 |
| 2941 | 2940 | ### *napi_new_instance* | 2942 | 2941 | <!-- YAML | |
Collapse file Copy file name to clipboardExpand all lines: src/node_api.cc +13-10Lines changed: 13 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -446,7 +446,7 @@ class CallbackWrapper { | 446 | 446 | CallbackWrapper(napi_value this_arg, size_t args_length, void* data) | 447 | 447 | : _this(this_arg), _args_length(args_length), _data(data) {} | 448 | 448 |
| 449 | | - virtual bool IsConstructCall() = 0; | | 449 | + virtual napi_value NewTarget() = 0; | 450 | 450 | virtual void Args(napi_value* buffer, size_t bufferlength) = 0; | 451 | 451 | virtual void SetReturnValue(napi_value value) = 0; | 452 | 452 |
| @@ -475,8 +475,7 @@ class CallbackWrapperBase : public CallbackWrapper { | 475 | 475 | ->Value(); | 476 | 476 | } | 477 | 477 |
| 478 | | - /*virtual*/ | 479 | | - bool IsConstructCall() override { return false; } | | 478 | + napi_value NewTarget() override { return nullptr; } | 480 | 479 |
| 481 | 480 | protected: | 482 | 481 | void InvokeCallback() { | @@ -524,8 +523,13 @@ class FunctionCallbackWrapper | 524 | 523 | const v8::FunctionCallbackInfo<v8::Value>& cbinfo) | 525 | 524 | : CallbackWrapperBase(cbinfo, cbinfo.Length()) {} | 526 | 525 |
| 527 | | - /*virtual*/ | 528 | | - bool IsConstructCall() override { return _cbinfo.IsConstructCall(); } | | 526 | + napi_value NewTarget() override { | | 527 | + if (_cbinfo.IsConstructCall()) { | | 528 | + return v8impl::JsValueFromV8LocalValue(_cbinfo.NewTarget()); | | 529 | + } else { | | 530 | + return nullptr; | | 531 | + } | | 532 | + } | 529 | 533 |
| 530 | 534 | /*virtual*/ | 531 | 535 | void Args(napi_value* buffer, size_t buffer_length) override { | @@ -1874,18 +1878,17 @@ napi_status napi_get_cb_info( | 1874 | 1878 | return napi_clear_last_error(env); | 1875 | 1879 | } | 1876 | 1880 |
| 1877 | | -napi_status napi_is_construct_call(napi_env env, | 1878 | | - napi_callback_info cbinfo, | 1879 | | - bool* result) { | 1880 | | - // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because no V8 APIs are called. | | 1881 | +napi_status napi_get_new_target(napi_env env, | | 1882 | + napi_callback_info cbinfo, | | 1883 | + napi_value* result) { | 1881 | 1884 | CHECK_ENV(env); | 1882 | 1885 | CHECK_ARG(env, cbinfo); | 1883 | 1886 | CHECK_ARG(env, result); | 1884 | 1887 |
| 1885 | 1888 | v8impl::CallbackWrapper* info = | 1886 | 1889 | reinterpret_cast<v8impl::CallbackWrapper*>(cbinfo); | 1887 | 1890 |
| 1888 | | - *result = info->IsConstructCall(); | | 1891 | + *result = info->NewTarget(); | 1889 | 1892 | return napi_clear_last_error(env); | 1890 | 1893 | } | 1891 | 1894 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node_api.h +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -340,9 +340,9 @@ NAPI_EXTERN napi_status napi_get_cb_info( | 340 | 340 | napi_value* this_arg, // [out] Receives the JS 'this' arg for the call | 341 | 341 | void** data); // [out] Receives the data pointer for the callback. | 342 | 342 |
| 343 | | -NAPI_EXTERN napi_status napi_is_construct_call(napi_env env, | 344 | | - napi_callback_info cbinfo, | 345 | | - bool* result); | | 343 | +NAPI_EXTERN napi_status napi_get_new_target(napi_env env, | | 344 | + napi_callback_info cbinfo, | | 345 | + napi_value* result); | 346 | 346 | NAPI_EXTERN napi_status | 347 | 347 | napi_define_class(napi_env env, | 348 | 348 | const char* utf8name, | |
Collapse file Copy file name to clipboardExpand all lines: test/addons-napi/6_object_wrap/myobject.cc +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,8 +32,9 @@ void MyObject::Init(napi_env env, napi_value exports) { | 32 | 32 | } | 33 | 33 |
| 34 | 34 | napi_value MyObject::New(napi_env env, napi_callback_info info) { | 35 | | - bool is_constructor; | 36 | | - NAPI_CALL(env, napi_is_construct_call(env, info, &is_constructor)); | | 35 | + napi_value new_target; | | 36 | + NAPI_CALL(env, napi_get_new_target(env, info, &new_target)); | | 37 | + bool is_constructor = (new_target != nullptr); | 37 | 38 |
| 38 | 39 | size_t argc = 1; | 39 | 40 | napi_value args[1]; | |
|