From 83e18ae9a19f11de7df543d30724acd6c02ef567 Mon Sep 17 00:00:00 2001 From: legendecas Date: Tue, 25 May 2021 00:01:22 +0800 Subject: [PATCH] test: function reference call & construct PR-URL: https://github.com/nodejs/node-addon-api/pull/1005 Reviewed-By: Michael Dawson --- test/binding.cc | 2 ++ test/binding.gyp | 1 + test/functionreference.cc | 30 ++++++++++++++++++++++++++++++ test/functionreference.js | 20 ++++++++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 test/functionreference.cc create mode 100644 test/functionreference.js 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/); +}