Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/node_jsvmapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#ifndef SRC_NODE_JSVMAPI_H_
#define SRC_NODE_JSVMAPI_H_

#include <stddef.h>
#include "node_jsvmapi_types.h"

#ifndef NODE_EXTERN
Expand Down
25 changes: 22 additions & 3 deletions test/addons-abi/3_callbacks/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,30 @@ void RunCallback(napi_env env, const napi_callback_info info) {
if (status != napi_ok) return;
}

void Init(napi_env env, napi_value exports, napi_value module) {
void RunCallbackWithRecv(napi_env env, const napi_callback_info info) {
napi_status status;
napi_property_descriptor desc = { "exports", RunCallback };
status = napi_define_property(env, module, &desc);

napi_value args[2];
status = napi_get_cb_args(env, info, args, 2);
if (status != napi_ok) return;

napi_value cb = args[0];
napi_value recv = args[1];

status = napi_call_function(env, recv, cb, 0, nullptr, nullptr);
if (status != napi_ok) return;
}

void Init(napi_env env, napi_value exports, napi_value module) {
napi_status status;
napi_property_descriptor desc[2] = {
{ "RunCallback", RunCallback },
{ "RunCallbackWithRecv", RunCallbackWithRecv }
};
for (int index = 0; index < 2; index++) {
status = napi_define_property(env, exports, &desc[index]);
if (status != napi_ok) return;
}
}

NODE_MODULE_ABI(addon, Init)
19 changes: 18 additions & 1 deletion test/addons-abi/3_callbacks/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ require('../../common');
var assert = require('assert');
var addon = require('./build/Release/binding');

addon(function(msg) {
addon.RunCallback(function(msg) {
assert.equal(msg, 'hello world');
});

var global = ( function() { return this; } ).apply();

function testRecv(desiredRecv) {
addon.RunCallbackWithRecv(function() {
assert.equal(this,
( desiredRecv === undefined || desiredRecv === null ) ? global : desiredRecv );
}, desiredRecv);
}

testRecv(undefined);
testRecv(null);
testRecv(5);
testRecv(true);
testRecv("Hello");
testRecv([]);
testRecv({});