Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: Add an operator to retrieve raw napi_callback_info from CallbackInfo #1253

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions napi-inl.h
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

const common = require('./common');

module.exports = common.runTest(test);

async function test (binding) {
binding.callbackInfo.testCbSetData();
}