diff --git a/napi-inl.h b/napi-inl.h index 9ffc082ce..69b720578 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -3451,6 +3451,10 @@ inline CallbackInfo::~CallbackInfo() { } } +inline CallbackInfo::operator napi_callback_info() const { + return _info; +} + inline Value CallbackInfo::NewTarget() const { napi_value newTarget; napi_status status = napi_get_new_target(_env, _info, &newTarget); diff --git a/napi.h b/napi.h index 3fa4df653..831b3b6df 100644 --- a/napi.h +++ b/napi.h @@ -1791,6 +1791,7 @@ class CallbackInfo { Value This() const; void* Data() const; void SetData(void* data); + operator napi_callback_info() const; private: const size_t _staticArgCount = 6; diff --git a/test/binding.cc b/test/binding.cc index b3d9265d4..5f393a43c 100644 --- a/test/binding.cc +++ b/test/binding.cc @@ -28,6 +28,7 @@ Object InitCallbackScope(Env env); #if (NAPI_VERSION > 4) Object InitDate(Env env); #endif +Object InitCallbackInfo(Env env); Object InitDataView(Env env); Object InitDataViewReadWrite(Env env); Object InitEnvCleanup(Env env); @@ -108,6 +109,7 @@ Object Init(Env env, Object exports) { #if (NAPI_VERSION > 2) exports.Set("callbackscope", InitCallbackScope(env)); #endif + exports.Set("callbackInfo", InitCallbackInfo(env)); exports.Set("dataview", InitDataView(env)); exports.Set("dataview_read_write", InitDataView(env)); exports.Set("dataview_read_write", InitDataViewReadWrite(env)); diff --git a/test/binding.gyp b/test/binding.gyp index 9222fe014..23dab743e 100644 --- a/test/binding.gyp +++ b/test/binding.gyp @@ -17,6 +17,7 @@ 'basic_types/number.cc', 'basic_types/value.cc', 'bigint.cc', + 'callbackInfo.cc', 'date.cc', 'binding.cc', 'buffer.cc', diff --git a/test/callbackInfo.cc b/test/callbackInfo.cc new file mode 100644 index 000000000..ad5f72356 --- /dev/null +++ b/test/callbackInfo.cc @@ -0,0 +1,27 @@ +#include +#include "napi.h" +using namespace Napi; + +struct TestCBInfoSetData { + static void Test(napi_env env, napi_callback_info info) { + Napi::CallbackInfo cbInfo(env, info); + int valuePointer = 1220202; + cbInfo.SetData(&valuePointer); + + int* placeHolder = static_cast(cbInfo.Data()); + assert(*(placeHolder) == valuePointer); + assert(placeHolder == &valuePointer); + } +}; + +void TestCallbackInfoSetData(const Napi::CallbackInfo& info) { + napi_callback_info cb_info = static_cast(info); + TestCBInfoSetData::Test(info.Env(), cb_info); +} + +Object InitCallbackInfo(Env env) { + Object exports = Object::New(env); + + exports["testCbSetData"] = Function::New(env, TestCallbackInfoSetData); + return exports; +} diff --git a/test/callbackInfo.js b/test/callbackInfo.js new file mode 100644 index 000000000..ea671a986 --- /dev/null +++ b/test/callbackInfo.js @@ -0,0 +1,9 @@ +'use strict'; + +const common = require('./common'); + +module.exports = common.runTest(test); + +async function test (binding) { + binding.callbackInfo.testCbSetData(); +}