Skip to content

Commit cb92d24

Browse files
refackTrott
authored andcommitted
tools: fix js2c regression
PR-URL: #27980 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent db013e1 commit cb92d24

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,7 @@
10941094
'test/cctest/test_node_postmortem_metadata.cc',
10951095
'test/cctest/test_environment.cc',
10961096
'test/cctest/test_linked_binding.cc',
1097+
'test/cctest/test_per_process.cc',
10971098
'test/cctest/test_platform.cc',
10981099
'test/cctest/test_report_util.cc',
10991100
'test/cctest/test_traced_value.cc',

src/node_native_module.h

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#include "node_union_bytes.h"
1212
#include "v8.h"
1313

14+
// Forward declare test fixture for `friend` declaration.
15+
class PerProcessTest;
16+
1417
namespace node {
1518
namespace native_module {
1619

@@ -82,6 +85,8 @@ class NativeModuleLoader {
8285

8386
// Used to synchronize access to the code cache map
8487
Mutex code_cache_mutex_;
88+
89+
friend class ::PerProcessTest;
8590
};
8691
} // namespace native_module
8792

test/cctest/test_per_process.cc

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "node_native_module.h"
2+
3+
#include "gtest/gtest.h"
4+
#include "node_test_fixture.h"
5+
6+
#include <string>
7+
8+
9+
using node::native_module::NativeModuleLoader;
10+
using node::native_module::NativeModuleRecordMap;
11+
12+
class PerProcessTest : public ::testing::Test {
13+
protected:
14+
static const NativeModuleRecordMap get_sources_for_test() {
15+
return NativeModuleLoader::instance_.source_;
16+
}
17+
};
18+
19+
namespace {
20+
21+
TEST_F(PerProcessTest, EmbeddedSources) {
22+
const auto& sources = PerProcessTest::get_sources_for_test();
23+
ASSERT_TRUE(
24+
std::any_of(sources.cbegin(), sources.cend(),
25+
[](auto p){ return p.second.is_one_byte(); }))
26+
<< "NativeModuleLoader::source_ should have some 8bit items";
27+
28+
ASSERT_TRUE(
29+
std::any_of(sources.cbegin(), sources.cend(),
30+
[](auto p){ return !p.second.is_one_byte(); }))
31+
<< "NativeModuleLoader::source_ should have some 16bit items";
32+
}
33+
34+
} // end namespace

tools/js2c.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,12 @@ def ReadMacros(macro_files):
200200
}} // namespace node
201201
"""
202202

203+
ONE_BYTE_STRING = """
204+
static const uint8_t {0}[] = {{
205+
{1}
206+
}};
207+
"""
208+
203209
TWO_BYTE_STRING = """
204210
static const uint16_t {0}[] = {{
205211
{1}
@@ -215,15 +221,25 @@ def ReadMacros(macro_files):
215221
is_verbose = False
216222

217223
def GetDefinition(var, source, step=30):
218-
encoded_source = bytearray(source, 'utf-16le')
219-
code_points = [encoded_source[i] + (encoded_source[i+1] * 256) for i in range(0, len(encoded_source), 2)]
224+
template = ONE_BYTE_STRING
225+
code_points = [ord(c) for c in source]
226+
if any(c > 127 for c in code_points):
227+
template = TWO_BYTE_STRING
228+
# Treat non-ASCII as UTF-8 and encode as UTF-16 Little Endian.
229+
encoded_source = bytearray(source, 'utf-16le')
230+
code_points = [
231+
encoded_source[i] + (encoded_source[i + 1] * 256)
232+
for i in range(0, len(encoded_source), 2)
233+
]
234+
220235
# For easier debugging, align to the common 3 char for code-points.
221236
elements_s = ['%3s' % x for x in code_points]
222237
# Put no more then `step` code-points in a line.
223238
slices = [elements_s[i:i + step] for i in range(0, len(elements_s), step)]
224239
lines = [','.join(s) for s in slices]
225240
array_content = ',\n'.join(lines)
226-
definition = TWO_BYTE_STRING.format(var, array_content)
241+
definition = template.format(var, array_content)
242+
227243
return definition, len(code_points)
228244

229245

0 commit comments

Comments
 (0)