Skip to content

Commit

Permalink
napi: function may be called with any value as recv
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Schulhof committed Feb 15, 2017
1 parent 2328992 commit 654da16
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
3 changes: 1 addition & 2 deletions src/node_jsvmapi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1319,8 +1319,7 @@ napi_status napi_call_function(napi_env e,
v8::Isolate *isolate = v8impl::V8IsolateFromJsEnv(e);
v8::Local<v8::Context> context = isolate->GetCurrentContext();

v8::Handle<v8::Object> v8recv;
CHECK_TO_OBJECT(context, v8recv, recv);
v8::Handle<v8::Value> v8recv = v8impl::V8LocalValueFromJsValue(recv);

for (int i = 0; i < argc; i++) {
args[i] = v8impl::V8LocalValueFromJsValue(argv[i]);
Expand Down
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
21 changes: 19 additions & 2 deletions test/addons-abi/3_callbacks/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,27 @@ void RunCallback(napi_env env, const napi_callback_info info) {
if (status != napi_ok) return;
}

void RunCallbackWithRecv(napi_env env, const napi_callback_info info) {
napi_status status;

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 = { "exports", RunCallback };
status = napi_define_properties(env, module, 1, &desc);
napi_property_descriptor desc[2] = {
{ "RunCallback", RunCallback },
{ "RunCallbackWithRecv", RunCallbackWithRecv }
};
status = napi_define_properties(env, exports, 2, desc);
if (status != napi_ok) return;
}

Expand Down
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({});

0 comments on commit 654da16

Please sign in to comment.