From cb92d243e8b3603cf4abd231abe83179950927fc Mon Sep 17 00:00:00 2001 From: Refael Ackermann Date: Thu, 30 May 2019 11:54:09 -0400 Subject: [PATCH] tools: fix js2c regression MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/27980 Reviewed-By: Michaƫl Zasso Reviewed-By: Ben Noordhuis Reviewed-By: Joyee Cheung --- node.gyp | 1 + src/node_native_module.h | 5 +++++ test/cctest/test_per_process.cc | 34 +++++++++++++++++++++++++++++++++ tools/js2c.py | 22 ++++++++++++++++++--- 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 test/cctest/test_per_process.cc diff --git a/node.gyp b/node.gyp index 8ece8060b84d64..56377d6a0c5e8e 100644 --- a/node.gyp +++ b/node.gyp @@ -1094,6 +1094,7 @@ 'test/cctest/test_node_postmortem_metadata.cc', 'test/cctest/test_environment.cc', 'test/cctest/test_linked_binding.cc', + 'test/cctest/test_per_process.cc', 'test/cctest/test_platform.cc', 'test/cctest/test_report_util.cc', 'test/cctest/test_traced_value.cc', diff --git a/src/node_native_module.h b/src/node_native_module.h index 5450c63c161cf2..fabaea75686161 100644 --- a/src/node_native_module.h +++ b/src/node_native_module.h @@ -11,6 +11,9 @@ #include "node_union_bytes.h" #include "v8.h" +// Forward declare test fixture for `friend` declaration. +class PerProcessTest; + namespace node { namespace native_module { @@ -82,6 +85,8 @@ class NativeModuleLoader { // Used to synchronize access to the code cache map Mutex code_cache_mutex_; + + friend class ::PerProcessTest; }; } // namespace native_module diff --git a/test/cctest/test_per_process.cc b/test/cctest/test_per_process.cc new file mode 100644 index 00000000000000..43af8dd65a72d0 --- /dev/null +++ b/test/cctest/test_per_process.cc @@ -0,0 +1,34 @@ +#include "node_native_module.h" + +#include "gtest/gtest.h" +#include "node_test_fixture.h" + +#include + + +using node::native_module::NativeModuleLoader; +using node::native_module::NativeModuleRecordMap; + +class PerProcessTest : public ::testing::Test { + protected: + static const NativeModuleRecordMap get_sources_for_test() { + return NativeModuleLoader::instance_.source_; + } +}; + +namespace { + +TEST_F(PerProcessTest, EmbeddedSources) { + const auto& sources = PerProcessTest::get_sources_for_test(); + ASSERT_TRUE( + std::any_of(sources.cbegin(), sources.cend(), + [](auto p){ return p.second.is_one_byte(); })) + << "NativeModuleLoader::source_ should have some 8bit items"; + + ASSERT_TRUE( + std::any_of(sources.cbegin(), sources.cend(), + [](auto p){ return !p.second.is_one_byte(); })) + << "NativeModuleLoader::source_ should have some 16bit items"; +} + +} // end namespace diff --git a/tools/js2c.py b/tools/js2c.py index 413158765551d3..c3ac53f14b7391 100755 --- a/tools/js2c.py +++ b/tools/js2c.py @@ -200,6 +200,12 @@ def ReadMacros(macro_files): }} // namespace node """ +ONE_BYTE_STRING = """ +static const uint8_t {0}[] = {{ +{1} +}}; +""" + TWO_BYTE_STRING = """ static const uint16_t {0}[] = {{ {1} @@ -215,15 +221,25 @@ def ReadMacros(macro_files): is_verbose = False def GetDefinition(var, source, step=30): - encoded_source = bytearray(source, 'utf-16le') - code_points = [encoded_source[i] + (encoded_source[i+1] * 256) for i in range(0, len(encoded_source), 2)] + template = ONE_BYTE_STRING + code_points = [ord(c) for c in source] + if any(c > 127 for c in code_points): + template = TWO_BYTE_STRING + # Treat non-ASCII as UTF-8 and encode as UTF-16 Little Endian. + encoded_source = bytearray(source, 'utf-16le') + code_points = [ + encoded_source[i] + (encoded_source[i + 1] * 256) + for i in range(0, len(encoded_source), 2) + ] + # For easier debugging, align to the common 3 char for code-points. elements_s = ['%3s' % x for x in code_points] # Put no more then `step` code-points in a line. slices = [elements_s[i:i + step] for i in range(0, len(elements_s), step)] lines = [','.join(s) for s in slices] array_content = ',\n'.join(lines) - definition = TWO_BYTE_STRING.format(var, array_content) + definition = template.format(var, array_content) + return definition, len(code_points)