Skip to content

Commit 148ecfa

Browse files
Gabriel Schulhofmhdawson
authored andcommitted
Catch segfaults, fix a segfault, track build type
PR-URL: #161 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Michael Dawson <[email protected]> * Establish the build type by looking at whether test/build has a subdirectory named "Release" or whether it has one named "Debug". Thus, `npm test --debug` will also work. * Correctly process the child exit status, examining both the exit code *and* the signal it threw, if any. * In `objectwrap` we must delete the reference to the constructor sooner than the C++ library destructor for global data, because by the time the library constructor gets called the V8 machinery has been decommissioned and causes a segfault when attempting the belated deletion of a reference. PR-URL: #161 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 988c9a9 commit 148ecfa

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

test/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
'use strict';
22

3+
process.config.target_defaults.default_configuration =
4+
require('fs')
5+
.readdirSync(require('path').join(__dirname, 'build'))
6+
.filter((item) => (item === 'Debug' || item === 'Release'))[0];
7+
38
let testModules = [
49
'arraybuffer',
510
'asyncworker',
@@ -30,9 +35,10 @@ if (typeof global.gc === 'function') {
3035
});
3136

3237
if (child.signal) {
33-
console.log(`Tests aborted with ${child.signal}`);
38+
console.error(`Tests aborted with ${child.signal}`);
3439
process.exitCode = 1;
3540
} else {
3641
process.exitCode = child.status;
3742
}
43+
process.exit(process.exitCode);
3844
}

test/objectwrap.cc

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,18 @@ class TestIter : public Napi::ObjectWrap<TestIter> {
1010
return object;
1111
}
1212

13-
static void Initialize(Napi::Env env, Napi::Object exports) {
14-
Constructor = Napi::Persistent(DefineClass(env, "TestIter", {
13+
static Napi::FunctionReference Initialize(Napi::Env env) {
14+
return Napi::Persistent(DefineClass(env, "TestIter", {
1515
InstanceMethod("next", &TestIter::Next),
1616
}));
17-
18-
Constructor.SuppressDestruct();
1917
}
20-
21-
static Napi::FunctionReference Constructor;
2218
};
2319

24-
Napi::FunctionReference TestIter::Constructor;
25-
2620
class Test : public Napi::ObjectWrap<Test> {
2721
public:
2822
Test(const Napi::CallbackInfo& info) :
29-
Napi::ObjectWrap<Test>(info) {
23+
Napi::ObjectWrap<Test>(info),
24+
Constructor(TestIter::Initialize(info.Env())) {
3025
}
3126

3227
void SetMethod(const Napi::CallbackInfo& info) {
@@ -38,7 +33,7 @@ class Test : public Napi::ObjectWrap<Test> {
3833
}
3934

4035
Napi::Value Iter(const Napi::CallbackInfo& info) {
41-
return TestIter::Constructor.New({});
36+
return Constructor.New({});
4237
}
4338

4439
void Setter(const Napi::CallbackInfo& info, const Napi::Value& new_value) {
@@ -62,11 +57,11 @@ class Test : public Napi::ObjectWrap<Test> {
6257

6358
private:
6459
uint32_t value;
60+
Napi::FunctionReference Constructor;
6561
};
6662

6763
Napi::Object InitObjectWrap(Napi::Env env) {
6864
Napi::Object exports = Napi::Object::New(env);
6965
Test::Initialize(env, exports);
70-
TestIter::Initialize(env, exports);
7166
return exports;
7267
}

0 commit comments

Comments
 (0)