Skip to content

Commit 986b6d0

Browse files
committed
enable only when c++ exceptions is enabled
1 parent 9e7d3ea commit 986b6d0

File tree

4 files changed

+53
-16
lines changed

4 files changed

+53
-16
lines changed

napi-inl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,7 @@ inline void Object::AddFinalizer(Finalizer finalizeCallback,
13961396
}
13971397
}
13981398

1399+
#ifdef NAPI_CPP_EXCEPTIONS
13991400
inline Object::const_iterator::const_iterator(const Object* object,
14001401
const Type type) {
14011402
_object = object;
@@ -1470,6 +1471,7 @@ Object::iterator::operator*() {
14701471
const PropertyLValue<Value> value = (*_object)[key];
14711472
return {key, value};
14721473
}
1474+
#endif // NAPI_CPP_EXCEPTIONS
14731475

14741476
#if NAPI_VERSION >= 8
14751477
inline bool Object::Freeze() {

napi.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,7 @@ namespace Napi {
781781
T* data,
782782
Hint* finalizeHint);
783783

784+
#ifdef NAPI_CPP_EXCEPTIONS
784785
class const_iterator;
785786

786787
inline const_iterator begin() const;
@@ -792,6 +793,7 @@ namespace Napi {
792793
inline iterator begin();
793794

794795
inline iterator end();
796+
#endif // NAPI_CPP_EXCEPTIONS
795797

796798
#if NAPI_VERSION >= 8
797799
bool Freeze();
@@ -833,6 +835,7 @@ namespace Napi {
833835
uint32_t Length() const;
834836
};
835837

838+
#ifdef NAPI_CPP_EXCEPTIONS
836839
class Object::const_iterator {
837840
private:
838841
enum class Type { BEGIN, END };
@@ -878,6 +881,7 @@ namespace Napi {
878881

879882
friend class Object;
880883
};
884+
#endif // NAPI_CPP_EXCEPTIONS
881885

882886
/// A JavaScript array buffer value.
883887
class ArrayBuffer : public Object {

test/object/object.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ Value CreateObjectUsingMagic(const CallbackInfo& info) {
252252
return obj;
253253
}
254254

255+
#ifdef NAPI_CPP_EXCEPTIONS
255256
Value Sum(const CallbackInfo& info) {
256257
Object object = info[0].As<Object>();
257258
int64_t sum = 0;
@@ -262,6 +263,7 @@ Value Sum(const CallbackInfo& info) {
262263

263264
return Number::New(info.Env(), sum);
264265
}
266+
#endif // NAPI_CPP_EXCEPTIONS
265267

266268
Value InstanceOf(const CallbackInfo& info) {
267269
Object obj = info[0].As<Object>();
@@ -309,7 +311,9 @@ Object InitObject(Env env) {
309311
exports["hasPropertyWithCppStyleString"] = Function::New(env, HasPropertyWithCppStyleString);
310312

311313
exports["createObjectUsingMagic"] = Function::New(env, CreateObjectUsingMagic);
314+
#ifdef NAPI_CPP_EXCEPTIONS
312315
exports["sum"] = Function::New(env, Sum);
316+
#endif // NAPI_CPP_EXCEPTIONS
313317

314318
exports["addFinalizer"] = Function::New(env, AddFinalizer);
315319
exports["addFinalizerWithHint"] = Function::New(env, AddFinalizerWithHint);

test/object/object.js

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
const assert = require('assert');
44

5-
module.exports = require('../common').runTest(test);
5+
test(require(`../build/${buildType}/binding.node`), true);
6+
test(require(`../build/${buildType}/binding_noexcept.node`));
67

7-
function test(binding) {
8+
function test(binding, NAPI_CPP_EXCEPTIONS = false) {
89
function assertPropertyIs(obj, key, attribute) {
910
const propDesc = Object.getOwnPropertyDescriptor(obj, key);
1011
assert.ok(propDesc);
@@ -158,20 +159,46 @@ function test(binding) {
158159
}
159160

160161
{
161-
const obj = {
162-
'-forbid': -0x4B1D,
163-
'-feedcode': -0xFEEDC0DE,
164-
'+office': +0x0FF1CE,
165-
'+forbid': +0x4B1D,
166-
'+deadbeef': +0xDEADBEEF,
167-
'+feedcode': +0xFEEDC0DE,
168-
};
169-
170-
let sum = 0;
171-
for (const key in obj) {
172-
sum += obj[key];
162+
if (NAPI_CPP_EXCEPTIONS) {
163+
{
164+
const obj = {
165+
'-forbid': -0x4B1D,
166+
'-feedcode': -0xFEEDC0DE,
167+
'+office': +0x0FF1CE,
168+
'+forbid': +0x4B1D,
169+
'+deadbeef': +0xDEADBEEF,
170+
'+feedcode': +0xFEEDC0DE,
171+
};
172+
173+
let sum = 0;
174+
for (const key in obj) {
175+
sum += obj[key];
176+
}
177+
178+
assert.strictEqual(binding.object.sum(obj), sum);
179+
}
180+
181+
{
182+
const obj = new Proxy({
183+
'-forbid': -0x4B1D,
184+
'-feedcode': -0xFEEDC0DE,
185+
'+office': +0x0FF1CE,
186+
'+forbid': +0x4B1D,
187+
'+deadbeef': +0xDEADBEEF,
188+
'+feedcode': +0xFEEDC0DE,
189+
}, {
190+
getOwnPropertyDescriptor(target, p) {
191+
throw new Error("getOwnPropertyDescriptor error");
192+
},
193+
ownKeys(target) {
194+
throw new Error("ownKeys error");
195+
},
196+
});
197+
198+
assert.throws(() => {
199+
binding.object.sum(obj);
200+
}, /ownKeys error/);
201+
}
173202
}
174-
175-
assert.strictEqual(binding.object.sum(obj), sum);
176203
}
177204
}

0 commit comments

Comments
 (0)