Skip to content

Commit

Permalink
src: api to get callback_info from CallBackInfo
Browse files Browse the repository at this point in the history
- 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 <midawson@redhat.com
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
  • Loading branch information
JckXia authored and mhdawson committed Jan 3, 2023
1 parent ad76256 commit 55bd08e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions napi-inl.h
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions napi.h
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions test/binding.cc
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions test/binding.gyp
Expand Up @@ -17,6 +17,7 @@
'basic_types/number.cc',
'basic_types/value.cc',
'bigint.cc',
'callbackInfo.cc',
'date.cc',
'binding.cc',
'buffer.cc',
Expand Down
27 changes: 27 additions & 0 deletions test/callbackInfo.cc
@@ -0,0 +1,27 @@
#include <assert.h>
#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<int*>(cbInfo.Data());
assert(*(placeHolder) == valuePointer);
assert(placeHolder == &valuePointer);
}
};

void TestCallbackInfoSetData(const Napi::CallbackInfo& info) {
napi_callback_info cb_info = static_cast<napi_callback_info>(info);
TestCBInfoSetData::Test(info.Env(), cb_info);
}

Object InitCallbackInfo(Env env) {
Object exports = Object::New(env);

exports["testCbSetData"] = Function::New(env, TestCallbackInfoSetData);
return exports;
}
9 changes: 9 additions & 0 deletions 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();
}

0 comments on commit 55bd08e

Please sign in to comment.