forked from nodejs/node-addon-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: function reference call & construct
PR-URL: nodejs#1005 Reviewed-By: Michael Dawson <midawson@redhat.com>
- Loading branch information
1 parent
d837a89
commit a5456db
Showing
4 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/); | ||
} |