Skip to content

Commit 490d42e

Browse files
author
Gabriel Schulhof
committed
napi: synchronize function call context test
Closes #107
1 parent 871c1bf commit 490d42e

3 files changed

Lines changed: 41 additions & 4 deletions

File tree

src/node_jsvmapi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef SRC_NODE_JSVMAPI_H_
1919
#define SRC_NODE_JSVMAPI_H_
2020

21+
#include <stddef.h>
2122
#include "node_jsvmapi_types.h"
2223

2324
#ifndef NODE_EXTERN

test/addons-abi/3_callbacks/binding.cc

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,30 @@ void RunCallback(napi_env env, const napi_callback_info info) {
2121
if (status != napi_ok) return;
2222
}
2323

24-
void Init(napi_env env, napi_value exports, napi_value module) {
24+
void RunCallbackWithRecv(napi_env env, const napi_callback_info info) {
2525
napi_status status;
26-
napi_property_descriptor desc = { "exports", RunCallback };
27-
status = napi_define_property(env, module, &desc);
26+
27+
napi_value args[2];
28+
status = napi_get_cb_args(env, info, args, 2);
29+
if (status != napi_ok) return;
30+
31+
napi_value cb = args[0];
32+
napi_value recv = args[1];
33+
34+
status = napi_call_function(env, recv, cb, 0, nullptr, nullptr);
2835
if (status != napi_ok) return;
2936
}
3037

38+
void Init(napi_env env, napi_value exports, napi_value module) {
39+
napi_status status;
40+
napi_property_descriptor desc[2] = {
41+
{ "RunCallback", RunCallback },
42+
{ "RunCallbackWithRecv", RunCallbackWithRecv }
43+
};
44+
for (int index = 0; index < 2; index++) {
45+
status = napi_define_property(env, exports, &desc[index]);
46+
if (status != napi_ok) return;
47+
}
48+
}
49+
3150
NODE_MODULE_ABI(addon, Init)

test/addons-abi/3_callbacks/test.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@ require('../../common');
33
var assert = require('assert');
44
var addon = require('./build/Release/binding');
55

6-
addon(function(msg) {
6+
addon.RunCallback(function(msg) {
77
assert.equal(msg, 'hello world');
88
});
9+
10+
var global = ( function() { return this; } ).apply();
11+
12+
function testRecv(desiredRecv) {
13+
addon.RunCallbackWithRecv(function() {
14+
assert.equal(this,
15+
( desiredRecv === undefined || desiredRecv === null ) ? global : desiredRecv );
16+
}, desiredRecv);
17+
}
18+
19+
testRecv(undefined);
20+
testRecv(null);
21+
testRecv(5);
22+
testRecv(true);
23+
testRecv("Hello");
24+
testRecv([]);
25+
testRecv({});

0 commit comments

Comments
 (0)