Skip to content

Commit b75afc4

Browse files
legendecasmhdawson
authored andcommitted
test: function reference call & construct
PR-URL: #1005 Reviewed-By: Michael Dawson <[email protected]>
1 parent a0c0492 commit b75afc4

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

test/binding.cc

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Object InitDataViewReadWrite(Env env);
3333
Object InitError(Env env);
3434
Object InitExternal(Env env);
3535
Object InitFunction(Env env);
36+
Object InitFunctionReference(Env env);
3637
Object InitHandleScope(Env env);
3738
Object InitMovableCallbacks(Env env);
3839
Object InitMemoryManagement(Env env);
@@ -105,6 +106,7 @@ Object Init(Env env, Object exports) {
105106
exports.Set("error", InitError(env));
106107
exports.Set("external", InitExternal(env));
107108
exports.Set("function", InitFunction(env));
109+
exports.Set("functionreference", InitFunctionReference(env));
108110
exports.Set("name", InitName(env));
109111
exports.Set("handlescope", InitHandleScope(env));
110112
exports.Set("movable_callbacks", InitMovableCallbacks(env));

test/binding.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
'error.cc',
2525
'external.cc',
2626
'function.cc',
27+
'functionreference.cc',
2728
'handlescope.cc',
2829
'movable_callbacks.cc',
2930
'memory_management.cc',

test/functionreference.cc

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "napi.h"
2+
3+
using namespace Napi;
4+
5+
namespace {
6+
Value Call(const CallbackInfo& info) {
7+
HandleScope scope(info.Env());
8+
FunctionReference ref;
9+
ref.Reset(info[0].As<Function>());
10+
11+
return ref.Call({});
12+
}
13+
14+
Value Construct(const CallbackInfo& info) {
15+
HandleScope scope(info.Env());
16+
FunctionReference ref;
17+
ref.Reset(info[0].As<Function>());
18+
19+
return ref.New({});
20+
}
21+
} // namespace
22+
23+
Object InitFunctionReference(Env env) {
24+
Object exports = Object::New(env);
25+
26+
exports["call"] = Function::New(env, Call);
27+
exports["construct"] = Function::New(env, Construct);
28+
29+
return exports;
30+
}

test/functionreference.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
5+
module.exports = require('./common').runTest(binding => {
6+
test(binding.functionreference);
7+
});
8+
9+
function test(binding) {
10+
const e = new Error('foobar');
11+
const functionMayThrow = () => { throw e; };
12+
const classMayThrow = class { constructor() { throw e; } };
13+
14+
assert.throws(() => {
15+
binding.call(functionMayThrow);
16+
}, /foobar/);
17+
assert.throws(() => {
18+
binding.construct(classMayThrow);
19+
}, /foobar/);
20+
}

0 commit comments

Comments
 (0)