Skip to content

Commit 3e0ffa3

Browse files
authored
Add CallbackInfo.IsConstructCall() (#69)
1 parent b1ef1b7 commit 3e0ffa3

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

napi-inl.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1977,7 +1977,7 @@ inline Object FunctionReference::New(const std::vector<napi_value>& args) const
19771977
////////////////////////////////////////////////////////////////////////////////
19781978

19791979
inline CallbackInfo::CallbackInfo(napi_env env, napi_callback_info info)
1980-
: _env(env), _this(nullptr), _dynamicArgs(nullptr), _data(nullptr) {
1980+
: _env(env), _info(info), _this(nullptr), _dynamicArgs(nullptr), _data(nullptr) {
19811981
_argc = _staticArgCount;
19821982
_argv = _staticArgs;
19831983
napi_status status = napi_get_cb_info(env, info, &_argc, _argv, &_this, &_data);
@@ -2000,6 +2000,13 @@ inline CallbackInfo::~CallbackInfo() {
20002000
}
20012001
}
20022002

2003+
inline bool CallbackInfo::IsConstructCall() const {
2004+
bool isConstructCall;
2005+
napi_status status = napi_is_construct_call(_env, _info, &isConstructCall);
2006+
NAPI_THROW_IF_FAILED(_env, status, false);
2007+
return isConstructCall;
2008+
}
2009+
20032010
inline Napi::Env CallbackInfo::Env() const {
20042011
return Napi::Env(_env);
20052012
}

napi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,7 @@ namespace Napi {
11181118
~CallbackInfo();
11191119

11201120
Napi::Env Env() const;
1121+
bool IsConstructCall() const;
11211122
size_t Length() const;
11221123
const Value operator [](size_t index) const;
11231124
Value This() const;
@@ -1127,6 +1128,7 @@ namespace Napi {
11271128
private:
11281129
const size_t _staticArgCount = 6;
11291130
napi_env _env;
1131+
napi_callback_info _info;
11301132
napi_value _this;
11311133
size_t _argc;
11321134
napi_value* _argv;

test/function.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ Value CallConstructorWithVector(const CallbackInfo& info) {
9696
return func.New(args);
9797
}
9898

99+
void IsConstructCall(const CallbackInfo& info) {
100+
Function callback = info[0].As<Function>();
101+
bool isConstructCall = info.IsConstructCall();
102+
callback({Napi::Boolean::New(info.Env(), isConstructCall)});
103+
}
104+
99105
} // end anonymous namespace
100106

101107
Object InitFunction(Env env) {
@@ -113,5 +119,6 @@ Object InitFunction(Env env) {
113119
exports["callWithInvalidReceiver"] = Function::New(env, CallWithInvalidReceiver);
114120
exports["callConstructorWithArgs"] = Function::New(env, CallConstructorWithArgs);
115121
exports["callConstructorWithVector"] = Function::New(env, CallConstructorWithVector);
122+
exports["isConstructCall"] = Function::New(env, IsConstructCall);
116123
return exports;
117124
}

test/function.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,11 @@ function test(binding) {
6565
assert.equal(binding.function.voidCallback.name, 'voidCallback');
6666
assert.equal(binding.function.valueCallback.name, 'valueCallback');
6767

68+
let testConstructCall = undefined;
69+
binding.function.isConstructCall((result) => { testConstructCall = result; });
70+
assert.ok(!testConstructCall);
71+
new binding.function.isConstructCall((result) => { testConstructCall = result; });
72+
assert.ok(testConstructCall);
73+
6874
// TODO: Function::MakeCallback tests
6975
}

0 commit comments

Comments
 (0)