diff --git a/test/binding.cc b/test/binding.cc index 92f6c77df..f450c71d2 100644 --- a/test/binding.cc +++ b/test/binding.cc @@ -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); @@ -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)); diff --git a/test/binding.gyp b/test/binding.gyp index 8ecf91b83..7e6864535 100644 --- a/test/binding.gyp +++ b/test/binding.gyp @@ -24,6 +24,7 @@ 'error.cc', 'external.cc', 'function.cc', + 'functionreference.cc', 'handlescope.cc', 'movable_callbacks.cc', 'memory_management.cc', diff --git a/test/functionreference.cc b/test/functionreference.cc new file mode 100644 index 000000000..44dec3ce7 --- /dev/null +++ b/test/functionreference.cc @@ -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()); + + return ref.Call({}); +} + +Value Construct(const CallbackInfo& info) { + HandleScope scope(info.Env()); + FunctionReference ref; + ref.Reset(info[0].As()); + + 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; +} diff --git a/test/functionreference.js b/test/functionreference.js new file mode 100644 index 000000000..3266f0031 --- /dev/null +++ b/test/functionreference.js @@ -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/); +}