Skip to content

Commit 895246c

Browse files
authored
test: nan return (#172)
1 parent f5913ba commit 895246c

File tree

9 files changed

+180
-0
lines changed

9 files changed

+180
-0
lines changed

packages/test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,10 @@ if(NOT IS_WASM32)
453453
add_test_v8("nan_maybe" "./nan/maybe.cpp" ON)
454454
add_test_v8("nan_methodswithdata" "./nan/methodswithdata.cpp" ON)
455455
add_test_v8("nan_persistent" "./nan/persistent.cpp" ON)
456+
add_test_v8("nan_returnemptystring" "./nan/returnemptystring.cpp" ON)
457+
add_test_v8("nan_returnnull" "./nan/returnnull.cpp" ON)
458+
add_test_v8("nan_returnundefined" "./nan/returnundefined.cpp" ON)
459+
add_test_v8("nan_returnvalue" "./nan/returnvalue.cpp" ON)
456460
add_test_v8("nan_strings" "./nan/strings.cpp" ON)
457461
add_test_v8("nan_symbols" "./nan/symbols.cpp" ON)
458462
add_test_v8("nan_trycatch" "./nan/trycatch.cpp" ON)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*********************************************************************
2+
* NAN - Native Abstractions for Node.js
3+
*
4+
* Copyright (c) 2018 NAN contributors
5+
*
6+
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
7+
********************************************************************/
8+
9+
#include <nan.h>
10+
11+
using namespace Nan; // NOLINT(build/namespaces)
12+
13+
NAN_METHOD(ReturnEmptyString) {
14+
info.GetReturnValue().SetEmptyString();
15+
}
16+
17+
NAN_MODULE_INIT(Init) {
18+
Set(target
19+
, New<v8::String>("r").ToLocalChecked()
20+
, GetFunction(New<v8::FunctionTemplate>(ReturnEmptyString)).ToLocalChecked()
21+
);
22+
}
23+
24+
NODE_MODULE(returnemptystring, Init)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
const assert = require('assert')
3+
const { load } = require('../util')
4+
5+
const p = load('nan_returnemptystring')
6+
module.exports = p.then(bindings => {
7+
assert.strictEqual(typeof bindings.r, 'function');
8+
assert.strictEqual(bindings.r('a string value'), '');
9+
assert.strictEqual(bindings.r(), '');
10+
})

packages/test/nan/returnnull.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*********************************************************************
2+
* NAN - Native Abstractions for Node.js
3+
*
4+
* Copyright (c) 2018 NAN contributors
5+
*
6+
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
7+
********************************************************************/
8+
9+
#include <nan.h>
10+
11+
NAN_METHOD(ReturnNull) {
12+
info.GetReturnValue().SetNull();
13+
}
14+
15+
NAN_MODULE_INIT(Init) {
16+
Nan::Set(target
17+
, Nan::New<v8::String>("r").ToLocalChecked()
18+
, Nan::GetFunction(Nan::New<v8::FunctionTemplate>(ReturnNull))
19+
.ToLocalChecked()
20+
);
21+
}
22+
23+
NODE_MODULE(returnnull, Init)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
const assert = require('assert')
3+
const { load } = require('../util')
4+
5+
const p = load('nan_returnnull')
6+
module.exports = p.then(bindings => {
7+
assert.strictEqual(typeof bindings.r, 'function');
8+
assert.strictEqual(bindings.r('a string value'), null);
9+
assert.strictEqual(bindings.r(), null);
10+
})
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*********************************************************************
2+
* NAN - Native Abstractions for Node.js
3+
*
4+
* Copyright (c) 2018 NAN contributors
5+
*
6+
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
7+
********************************************************************/
8+
9+
#include <nan.h>
10+
11+
using namespace Nan; // NOLINT(build/namespaces)
12+
13+
NAN_METHOD(ReturnUndefined) {
14+
info.GetReturnValue().SetUndefined();
15+
}
16+
17+
NAN_MODULE_INIT(Init) {
18+
Set(target
19+
, New<v8::String>("r").ToLocalChecked()
20+
, GetFunction(New<v8::FunctionTemplate>(ReturnUndefined)).ToLocalChecked()
21+
);
22+
}
23+
24+
NODE_MODULE(returnundefined, Init)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
const assert = require('assert')
3+
const { load } = require('../util')
4+
5+
const p = load('nan_returnundefined')
6+
module.exports = p.then(bindings => {
7+
assert.strictEqual(typeof bindings.r, 'function');
8+
assert.strictEqual(bindings.r('a string value'), undefined);
9+
assert.strictEqual(bindings.r(), undefined);
10+
})

packages/test/nan/returnvalue.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*********************************************************************
2+
* NAN - Native Abstractions for Node.js
3+
*
4+
* Copyright (c) 2018 NAN contributors
5+
*
6+
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
7+
********************************************************************/
8+
9+
#include <nan.h>
10+
11+
using namespace Nan; // NOLINT(build/namespaces)
12+
13+
static Global<v8::Boolean> global;
14+
15+
NAN_METHOD(ReturnAValue) {
16+
const FunctionCallbackInfo<v8::Value> &cbinfo = info;
17+
ReturnValue<v8::Value> ret = cbinfo.GetReturnValue();
18+
if (cbinfo.Length() == 1) {
19+
ret.Set(To<v8::String>(info[0]).ToLocalChecked());
20+
} else {
21+
ret.Set(New("default").ToLocalChecked());
22+
}
23+
}
24+
25+
NAN_METHOD(ReturnPrimitive) {
26+
info.GetReturnValue().Set(true);
27+
}
28+
29+
NAN_METHOD(ReturnGlobal) {
30+
info.GetReturnValue().Set(global);
31+
global.Reset();
32+
}
33+
34+
NAN_METHOD(ReturnUnsigned) {
35+
info.GetReturnValue().Set(0x80000000u);
36+
}
37+
38+
NAN_MODULE_INIT(Init) {
39+
global.Reset(New(true));
40+
41+
Set(target
42+
, New<v8::String>("r").ToLocalChecked()
43+
, GetFunction(New<v8::FunctionTemplate>(ReturnAValue)).ToLocalChecked()
44+
);
45+
Set(target
46+
, New<v8::String>("p").ToLocalChecked()
47+
, GetFunction(New<v8::FunctionTemplate>(ReturnPrimitive)).ToLocalChecked()
48+
);
49+
Set(target
50+
, New<v8::String>("q").ToLocalChecked()
51+
, GetFunction(New<v8::FunctionTemplate>(ReturnGlobal)).ToLocalChecked()
52+
);
53+
Set(target
54+
, New<v8::String>("u").ToLocalChecked()
55+
, GetFunction(New<v8::FunctionTemplate>(ReturnUnsigned)).ToLocalChecked()
56+
);
57+
}
58+
59+
NODE_MODULE(returnvalue, Init)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
const assert = require('assert')
3+
const { load } = require('../util')
4+
5+
const p = load('nan_returnvalue')
6+
module.exports = p.then(bindings => {
7+
assert.strictEqual(typeof bindings.r, 'function');
8+
assert.strictEqual(typeof bindings.p, 'function');
9+
assert.strictEqual(typeof bindings.q, 'function');
10+
assert.strictEqual(typeof bindings.u, 'function');
11+
assert.strictEqual(bindings.r('a string value'), 'a string value');
12+
assert.strictEqual(bindings.r(), 'default');
13+
assert.ok(bindings.p());
14+
assert.ok(bindings.q());
15+
assert.strictEqual(bindings.u(), 0x80000000);
16+
})

0 commit comments

Comments
 (0)