Skip to content

Commit

Permalink
test: function reference call & construct
Browse files Browse the repository at this point in the history
PR-URL: nodejs#1005
Reviewed-By: Michael Dawson <[email protected]>
  • Loading branch information
legendecas authored and Deepak Rajamohan committed Oct 15, 2021
1 parent d837a89 commit a5456db
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 a5456db

Please sign in to comment.