Skip to content

Commit 55bd08e

Browse files
JckXiamhdawson
authored andcommitted
src: api to get callback_info from CallBackInfo
- Exposing conversion api to fetch underlying napi_callback_info from CallBackInfo - Add test coverage for CallbackInfo SetData method PR-URL: #1253 Reviewed-By: Michael Dawson <[email protected] Reviewed-By: Chengzhong Wu <[email protected]>
1 parent ad76256 commit 55bd08e

File tree

6 files changed

+44
-0
lines changed

6 files changed

+44
-0
lines changed

napi-inl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3451,6 +3451,10 @@ inline CallbackInfo::~CallbackInfo() {
34513451
}
34523452
}
34533453

3454+
inline CallbackInfo::operator napi_callback_info() const {
3455+
return _info;
3456+
}
3457+
34543458
inline Value CallbackInfo::NewTarget() const {
34553459
napi_value newTarget;
34563460
napi_status status = napi_get_new_target(_env, _info, &newTarget);

napi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,7 @@ class CallbackInfo {
17911791
Value This() const;
17921792
void* Data() const;
17931793
void SetData(void* data);
1794+
operator napi_callback_info() const;
17941795

17951796
private:
17961797
const size_t _staticArgCount = 6;

test/binding.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Object InitCallbackScope(Env env);
2828
#if (NAPI_VERSION > 4)
2929
Object InitDate(Env env);
3030
#endif
31+
Object InitCallbackInfo(Env env);
3132
Object InitDataView(Env env);
3233
Object InitDataViewReadWrite(Env env);
3334
Object InitEnvCleanup(Env env);
@@ -108,6 +109,7 @@ Object Init(Env env, Object exports) {
108109
#if (NAPI_VERSION > 2)
109110
exports.Set("callbackscope", InitCallbackScope(env));
110111
#endif
112+
exports.Set("callbackInfo", InitCallbackInfo(env));
111113
exports.Set("dataview", InitDataView(env));
112114
exports.Set("dataview_read_write", InitDataView(env));
113115
exports.Set("dataview_read_write", InitDataViewReadWrite(env));

test/binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
'basic_types/number.cc',
1818
'basic_types/value.cc',
1919
'bigint.cc',
20+
'callbackInfo.cc',
2021
'date.cc',
2122
'binding.cc',
2223
'buffer.cc',

test/callbackInfo.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <assert.h>
2+
#include "napi.h"
3+
using namespace Napi;
4+
5+
struct TestCBInfoSetData {
6+
static void Test(napi_env env, napi_callback_info info) {
7+
Napi::CallbackInfo cbInfo(env, info);
8+
int valuePointer = 1220202;
9+
cbInfo.SetData(&valuePointer);
10+
11+
int* placeHolder = static_cast<int*>(cbInfo.Data());
12+
assert(*(placeHolder) == valuePointer);
13+
assert(placeHolder == &valuePointer);
14+
}
15+
};
16+
17+
void TestCallbackInfoSetData(const Napi::CallbackInfo& info) {
18+
napi_callback_info cb_info = static_cast<napi_callback_info>(info);
19+
TestCBInfoSetData::Test(info.Env(), cb_info);
20+
}
21+
22+
Object InitCallbackInfo(Env env) {
23+
Object exports = Object::New(env);
24+
25+
exports["testCbSetData"] = Function::New(env, TestCallbackInfoSetData);
26+
return exports;
27+
}

test/callbackInfo.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
const common = require('./common');
4+
5+
module.exports = common.runTest(test);
6+
7+
async function test (binding) {
8+
binding.callbackInfo.testCbSetData();
9+
}

0 commit comments

Comments
 (0)