Skip to content

Commit

Permalink
test: function reference call & construct
Browse files Browse the repository at this point in the history
PR-URL: #1005
Reviewed-By: Michael Dawson <midawson@redhat.com>
  • Loading branch information
legendecas authored and mhdawson committed May 31, 2021
1 parent a0c0492 commit b75afc4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Object InitDataViewReadWrite(Env env);
Object InitError(Env env);
Object InitExternal(Env env);
Object InitFunction(Env env);
Object InitFunctionReference(Env env);
Object InitHandleScope(Env env);
Object InitMovableCallbacks(Env env);
Object InitMemoryManagement(Env env);
Expand Down Expand Up @@ -105,6 +106,7 @@ Object Init(Env env, Object exports) {
exports.Set("error", InitError(env));
exports.Set("external", InitExternal(env));
exports.Set("function", InitFunction(env));
exports.Set("functionreference", InitFunctionReference(env));
exports.Set("name", InitName(env));
exports.Set("handlescope", InitHandleScope(env));
exports.Set("movable_callbacks", InitMovableCallbacks(env));
Expand Down
1 change: 1 addition & 0 deletions test/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
'error.cc',
'external.cc',
'function.cc',
'functionreference.cc',
'handlescope.cc',
'movable_callbacks.cc',
'memory_management.cc',
Expand Down
30 changes: 30 additions & 0 deletions test/functionreference.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "napi.h"

using namespace Napi;

namespace {
Value Call(const CallbackInfo& info) {
HandleScope scope(info.Env());
FunctionReference ref;
ref.Reset(info[0].As<Function>());

return ref.Call({});
}

Value Construct(const CallbackInfo& info) {
HandleScope scope(info.Env());
FunctionReference ref;
ref.Reset(info[0].As<Function>());

return ref.New({});
}
} // namespace

Object InitFunctionReference(Env env) {
Object exports = Object::New(env);

exports["call"] = Function::New(env, Call);
exports["construct"] = Function::New(env, Construct);

return exports;
}
20 changes: 20 additions & 0 deletions test/functionreference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const assert = require('assert');

module.exports = require('./common').runTest(binding => {
test(binding.functionreference);
});

function test(binding) {
const e = new Error('foobar');
const functionMayThrow = () => { throw e; };
const classMayThrow = class { constructor() { throw e; } };

assert.throws(() => {
binding.call(functionMayThrow);
}, /foobar/);
assert.throws(() => {
binding.construct(classMayThrow);
}, /foobar/);
}

0 comments on commit b75afc4

Please sign in to comment.