8000 addons, refactor: add init order test. · fibjs/fibjs@749785e · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 749785e

Browse files
committed
addons, refactor: add init order test.
1 parent d42a7f2 commit 749785e

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

fibjs/addons/build.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ if(NOT "${CLEAN_BUILD}" STREQUAL "true")
2828
test_function
2929
test_general
3030
test_handle_scope
31+
test_init_order
3132
test_new_target
3233
test_number
3334
test_object
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cmake_minimum_required(VERSION 2.6)
2+
3+
include(../common.cmake)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <node_api.h>
2+
#include <memory>
3+
#include <string>
4+
#include "../common.h"
5+
6+
// This test verifies that use of the NAPI_MODULE in C++ code does not
7+
// interfere with the C++ dynamic static initializers.
8+
9+
namespace {
10+
11+
// This class uses dynamic static initializers for the test.
12+
// In production code developers must avoid dynamic static initializers because
13+
// they affect the start up time. They must prefer static initialization such as
14+
// use of constexpr functions or classes with constexpr constructors. E.g.
15+
// instead of using std::string, it is preferrable to use const char[], or
16+
// constexpr std::string_view starting with C++17, or even constexpr
17+
// std::string starting with C++20.
18+
struct MyClass {
19+
static const std::unique_ptr<int> valueHolder;
20+
static const std::string testString;
21+
};
22+
23+
const std::unique_ptr<int> MyClass::valueHolder =
24+
std::unique_ptr<int>(new int(42));
25+
// NOLINTNEXTLINE(runtime/string)
26+
const std::string MyClass::testString = std::string("123");
27+
28+
} // namespace
29+
30+
EXTERN_C_START
31+
napi_value Init(napi_env env, napi_value exports) {
32+
napi_value cppIntValue, cppStringValue;
33+
NODE_API_CALL(env,
34+
napi_create_int32(env, *MyClass::valueHolder, &cppIntValue));
35+
NODE_API_CALL(
36+
env,
37+
napi_create_string_utf8(
38+
env, MyClass::testString.c_str(), NAPI_AUTO_LENGTH, &cppStringValue));
39+
40+
napi_property_descriptor descriptors[] = {
41+
DECLARE_NODE_API_PROPERTY_VALUE("cppIntValue", cppIntValue),
42+
DECLARE_NODE_API_PROPERTY_VALUE("cppStringValue", cppStringValue)};
43+
44+
NODE_API_CALL(env,
45+
napi_define_properties(
46+
env, exports, std::size(descriptors), descriptors));
47+
48+
return exports;
49+
}
50+
EXTERN_C_END
51+
52+
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)

test/addons_test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,17 @@ describe('addons api', () => {
10571057
RangeError);
10581058
});
10591059

1060+
it('test_init_order', () => {
1061+
var module = {
1062+
exports: {}
1063+
}
1064+
process.dlopen(module, path.join(bin_path, 'test_init_order.node'));
1065+
const test_init_order = module.exports;
1066+
1067+
assert.strictEqual(test_init_order.cppIntValue, 42);
1068+
assert.strictEqual(test_init_order.cppStringValue, '123');
1069+
});
1070+
10601071
it('test_new_target', () => {
10611072
var module = {
10621073
exports: {}

0 commit comments

Comments
 (0)
0