Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: function reference call & construct #1005

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/);
}