Skip to content

Commit 367e82a

Browse files
committed
Fix Global::Pass for old versions
Fixes: #517 Reviewed-By: bnoordhuis
1 parent 8d5289a commit 367e82a

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

nan_persistent_pre_12_inl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ class Global : public PersistentBase<T> {
210210
* Move constructor.
211211
*/
212212
NAN_INLINE Global(RValue rvalue)
213-
: PersistentBase<T>(rvalue.object.persistent) {
213+
: PersistentBase<T>(rvalue.object->persistent) {
214214
rvalue.object->Reset();
215215
}
216216
NAN_INLINE ~Global() { this->Reset(); }

test/cpp/persistent.cpp

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ static Persistent<v8::String> persistentTest1;
1515

1616
NAN_METHOD(Save1) {
1717
persistentTest1.Reset(info[0].As<v8::String>());
18-
info.GetReturnValue().SetUndefined();
1918
}
2019

2120
NAN_METHOD(Get1) {
@@ -24,7 +23,6 @@ NAN_METHOD(Get1) {
2423

2524
NAN_METHOD(Dispose1) {
2625
persistentTest1.Reset();
27-
info.GetReturnValue().SetUndefined();
2826
}
2927

3028
NAN_METHOD(ToPersistentAndBackAgain) {
@@ -39,8 +37,8 @@ NAN_METHOD(PersistentToPersistent) {
3937
Persistent<v8::String> persistent(info[0].As<v8::String>());
4038
persistentTest1.Reset(persistent);
4139
persistent.Reset();
40+
info.GetReturnValue().Set(New(persistentTest1));
4241
persistentTest1.Reset();
43-
info.GetReturnValue().SetUndefined();
4442
}
4543

4644
NAN_METHOD(CopyablePersistent) {
@@ -49,30 +47,46 @@ NAN_METHOD(CopyablePersistent) {
4947
info.GetReturnValue().Set(New(p));
5048
}
5149

50+
template<typename T> Global<T> passer(v8::Local<T> handle) {
51+
return Global<T>(handle).Pass();
52+
}
53+
54+
NAN_METHOD(PassGlobal) {
55+
info.GetReturnValue().Set(passer(New(42)));
56+
}
57+
5258
NAN_MODULE_INIT(Init) {
5359
Set(target
5460
, New<v8::String>("save1").ToLocalChecked()
55-
, New<v8::FunctionTemplate>(Save1)->GetFunction()
61+
, GetFunction(New<v8::FunctionTemplate>(Save1)).ToLocalChecked()
5662
);
5763
Set(target
5864
, New<v8::String>("get1").ToLocalChecked()
59-
, New<v8::FunctionTemplate>(Get1)->GetFunction()
65+
, GetFunction(New<v8::FunctionTemplate>(Get1)).ToLocalChecked()
6066
);
6167
Set(target
6268
, New<v8::String>("dispose1").ToLocalChecked()
63-
, New<v8::FunctionTemplate>(Dispose1)->GetFunction()
69+
, GetFunction(New<v8::FunctionTemplate>(Dispose1)).ToLocalChecked()
6470
);
6571
Set(target
6672
, New<v8::String>("toPersistentAndBackAgain").ToLocalChecked()
67-
, New<v8::FunctionTemplate>(ToPersistentAndBackAgain)->GetFunction()
73+
, GetFunction(New<v8::FunctionTemplate>(ToPersistentAndBackAgain))
74+
.ToLocalChecked()
6875
);
6976
Set(target
7077
, New<v8::String>("persistentToPersistent").ToLocalChecked()
71-
, New<v8::FunctionTemplate>(PersistentToPersistent)->GetFunction()
78+
, GetFunction(New<v8::FunctionTemplate>(PersistentToPersistent))
79+
.ToLocalChecked()
7280
);
7381
Set(target
7482
, New<v8::String>("copyablePersistent").ToLocalChecked()
75-
, New<v8::FunctionTemplate>(CopyablePersistent)->GetFunction()
83+
, GetFunction(New<v8::FunctionTemplate>(CopyablePersistent))
84+
.ToLocalChecked()
85+
);
86+
Set(target
87+
, New<v8::String>("passGlobal").ToLocalChecked()
88+
, GetFunction(New<v8::FunctionTemplate>(PassGlobal))
89+
.ToLocalChecked()
7690
);
7791
}
7892

test/js/persistent-test.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const test = require('tap').test
1111
, bindings = require('bindings')({ module_root: testRoot, bindings: 'persistent' });
1212

1313
test('persistent', function (t) {
14-
t.plan(14);
14+
t.plan(15);
1515

1616
var persistent = bindings;
1717
t.type(persistent.save1, 'function');
@@ -20,26 +20,22 @@ test('persistent', function (t) {
2020
t.type(persistent.toPersistentAndBackAgain, 'function');
2121
t.type(persistent.persistentToPersistent, 'function');
2222
t.type(persistent.copyablePersistent, 'function');
23+
t.type(persistent.passGlobal, 'function');
24+
2325
t.deepEqual(persistent.toPersistentAndBackAgain({ x: 42 }), { x: 42 });
2426

25-
t.ok(persistent.persistentToPersistent('any string') || true);
27+
t.equal(persistent.persistentToPersistent('any string'), 'any string');
2628

2729
persistent.save1('a string to save');
2830
t.equal(persistent.get1(), 'a string to save');
31+
t.equal(persistent.copyablePersistent(), 'a string to save');
32+
33+
t.equal(persistent.passGlobal(), 42, 'pass global');
34+
2935
setTimeout(function () {
3036
t.equal(persistent.get1(), 'a string to save');
31-
}, 25);
32-
setTimeout(function () {
33-
t.equal(persistent.get1(), 'a string to save');
34-
}, 50);
35-
setTimeout(function () {
36-
t.equal(persistent.copyablePersistent(), 'a string to save');
37-
}, 75);
38-
setTimeout(function () {
3937
persistent.dispose1();
40-
}, 75);
41-
setTimeout(function () {
4238
t.ok(persistent.get1() === undefined, 'no more persistent');
4339
t.ok(persistent.copyablePersistent() === undefined, 'no more persistent');
44-
}, 100);
40+
}, 25);
4541
});

0 commit comments

Comments
 (0)