Skip to content

Commit d5fd4e7

Browse files
committed
src: simplify Javascript code embedding
* use `string_view` instead of `UnionBytes`
1 parent 38a3362 commit d5fd4e7

9 files changed

Lines changed: 126 additions & 172 deletions

File tree

common.gypi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@
231231
'SuppressStartupBanner': 'true',
232232
'WarnAsError': 'false',
233233
'WarningLevel': 3, # /W3
234+
'AdditionalOptions': [ '/std:c++17' ],
235+
234236
},
235237
'VCLinkerTool': {
236238
'conditions': [
@@ -334,7 +336,7 @@
334336
}],
335337
[ 'OS in "linux freebsd openbsd solaris android aix cloudabi"', {
336338
'cflags': [ '-Wall', '-Wextra', '-Wno-unused-parameter', ],
337-
'cflags_cc': [ '-fno-rtti', '-fno-exceptions', '-std=gnu++1y' ],
339+
'cflags_cc': [ '-fno-rtti', '-fno-exceptions', '-std=gnu++1z' ],
338340
'ldflags': [ '-rdynamic' ],
339341
'target_conditions': [
340342
# The 1990s toolchain on SmartOS can't handle thin archives.
@@ -461,7 +463,7 @@
461463
['clang==1', {
462464
'xcode_settings': {
463465
'GCC_VERSION': 'com.apple.compilers.llvm.clang.1_0',
464-
'CLANG_CXX_LANGUAGE_STANDARD': 'gnu++1y', # -std=gnu++1y
466+
'CLANG_CXX_LANGUAGE_STANDARD': 'gnu++1z', # -std=gnu++1z
465467
'CLANG_CXX_LIBRARY': 'libc++',
466468
},
467469
}],

deps/gtest/gtest.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'direct_dependent_settings': {
88
'include_dirs': ['include'],
99
},
10+
'defines': [ 'GTEST_LANG_CXX11' ],
1011
'include_dirs': ['.', 'include'],
1112
'sources': [
1213
'src/gtest-death-test.cc',

node.gyp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@
523523
'src/node_http2_state.h',
524524
'src/node_i18n.h',
525525
'src/node_internals.h',
526+
'src/node_javascript.h',
526527
'src/node_messaging.h',
527528
'src/node_metadata.h',
528529
'src/node_mutex.h',
@@ -538,7 +539,6 @@
538539
'src/node_revert.h',
539540
'src/node_root_certs.h',
540541
'src/node_stat_watcher.h',
541-
'src/node_union_bytes.h',
542542
'src/node_url.h',
543543
'src/node_version.h',
544544
'src/node_v8_platform-inl.h',
@@ -1031,7 +1031,10 @@
10311031
'<(SHARED_INTERMEDIATE_DIR)', # for node_natives.h
10321032
],
10331033

1034-
'defines': [ 'NODE_WANT_INTERNALS=1' ],
1034+
'defines': [
1035+
'NODE_WANT_INTERNALS=1',
1036+
'GTEST_LANG_CXX11'
1037+
],
10351038

10361039
'sources': [
10371040
'test/cctest/node_test_fixture.cc',

node.gypi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@
7878
'<(_msvs_precompiled_header)',
7979
'<(_msvs_precompiled_source)',
8080
],
81+
'include_dirs': [
82+
'tools/msvs/pch',
83+
],
8184
}, { # POSIX
8285
'defines': [ '__POSIX__' ],
8386
}],

src/node_javascript.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
3+
#include <map>
4+
#include <string>
5+
6+
#if __has_include(<string_view>)
7+
#include <string_view>
8+
using std::string_view;
9+
#else
10+
#include <experimental/string_view>
11+
using std::experimental::string_view;
12+
#endif
13+
14+
15+
16+
namespace node {
17+
18+
namespace native_module {
19+
20+
using NativeModuleRecordMap = std::map<std::string, string_view>;
21+
22+
class JavascriptEmbeddedCode {
23+
public:
24+
JavascriptEmbeddedCode();
25+
protected:
26+
string_view config_;
27+
NativeModuleRecordMap source_;
28+
};
29+
30+
} // namespace native_module
31+
32+
} // namespace node

src/node_native_module.cc

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "node_native_module.h"
22
#include "node_errors.h"
3+
#include "env.h"
4+
5+
#include <set>
36

47
namespace node {
58

@@ -16,18 +19,15 @@ using v8::DEFAULT;
1619
using v8::EscapableHandleScope;
1720
using v8::Function;
1821
using v8::FunctionCallbackInfo;
19-
using v8::HandleScope;
2022
using v8::Integer;
2123
using v8::IntegrityLevel;
2224
using v8::Isolate;
2325
using v8::Local;
24-
using v8::Maybe;
2526
using v8::MaybeLocal;
2627
using v8::Name;
2728
using v8::None;
2829
using v8::Object;
2930
using v8::PropertyCallbackInfo;
30-
using v8::Script;
3131
using v8::ScriptCompiler;
3232
using v8::ScriptOrigin;
3333
using v8::Set;
@@ -36,6 +36,32 @@ using v8::String;
3636
using v8::Uint8Array;
3737
using v8::Value;
3838

39+
class UInt8SpanResource
40+
: public String::ExternalOneByteStringResource {
41+
public:
42+
explicit UInt8SpanResource(string_view span) : span_(span) {}
43+
UInt8SpanResource(const UInt8SpanResource&) = delete;
44+
UInt8SpanResource(const UInt8SpanResource&&) = delete;
45+
UInt8SpanResource& operator=(const UInt8SpanResource&) = delete;
46+
UInt8SpanResource& operator=(const UInt8SpanResource&&) = delete;
47+
48+
~UInt8SpanResource() override = default;
49+
void Dispose() override { delete this; }
50+
51+
const char* data() const override { return span_.data(); }
52+
size_t length() const override { return span_.size(); }
53+
54+
static v8::Local<v8::String> ToStringChecked(v8::Isolate* isolate,
55+
string_view span) {
56+
return
57+
v8::String::NewExternalOneByte(isolate, new UInt8SpanResource{span})
58+
.ToLocalChecked();
59+
}
60+
61+
private:
62+
const string_view span_;
63+
};
64+
3965
void NativeModuleLoader::InitializeModuleCategories() {
4066
if (module_categories_.is_initialized) {
4167
DCHECK(!module_categories_.can_be_required.empty());
@@ -83,22 +109,18 @@ void NativeModuleLoader::InitializeModuleCategories() {
83109
"internal/v8_prof_processor",
84110
};
85111

86-
for (auto const& x : source_) {
112+
for (const auto& x : source_) {
87113
const std::string& id = x.first;
88114
for (auto const& prefix : prefixes) {
89115
if (prefix.length() > id.length()) {
90116
continue;
91117
}
92118
if (id.find(prefix) == 0) {
93-
module_categories_.cannot_be_required.emplace(id);
119+
module_categories_.cannot_be_required.insert(id);
94120
}
95121
}
96-
}
97-
98-
for (auto const& x : source_) {
99-
const std::string& id = x.first;
100122
if (0 == module_categories_.cannot_be_required.count(id)) {
101-
module_categories_.can_be_required.emplace(id);
123+
module_categories_.can_be_required.insert(id);
102124
}
103125
}
104126

@@ -111,8 +133,11 @@ Local<Object> MapToObject(Local<Context> context,
111133
Isolate* isolate = context->GetIsolate();
112134
Local<Object> out = Object::New(isolate);
113135
for (auto const& x : in) {
114-
Local<String> key = OneByteString(isolate, x.first.c_str(), x.first.size());
115-
out->Set(context, key, x.second.ToStringChecked(isolate)).FromJust();
136+
const Local<String> key = OneByteString(
137+
isolate, x.first.c_str(), x.first.size());
138+
const Local<String> val = UInt8SpanResource::ToStringChecked(
139+
isolate, x.second);
140+
out->Set(context, key, val).Check();
116141
}
117142
return out;
118143
}
@@ -206,18 +231,14 @@ void NativeModuleLoader::ConfigStringGetter(
206231
per_process::native_module_loader.GetConfigString(info.GetIsolate()));
207232
}
208233

209-
Local<Object> NativeModuleLoader::GetSourceObject(
210-
Local<Context> context) const {
234+
Local<Object> NativeModuleLoader::GetSourceObject(Local<Context> context)
235+
const {
211236
return MapToObject(context, source_);
212237
}
213238

214-
Local<String> NativeModuleLoader::GetConfigString(Isolate* isolate) const {
215-
return config_.ToStringChecked(isolate);
216-
}
217-
218-
NativeModuleLoader::NativeModuleLoader() : config_(GetConfig()) {
219-
LoadJavaScriptSource();
220-
LoadCodeCache();
239+
Local<String> NativeModuleLoader::GetConfigString(Isolate* isolate)
240+
const {
241+
return UInt8SpanResource::ToStringChecked(isolate, config_);;
221242
}
222243

223244
// This is supposed to be run only by the main thread in
@@ -294,9 +315,8 @@ MaybeLocal<Function> NativeModuleLoader::LookupAndCompile(
294315
Isolate* isolate = context->GetIsolate();
295316
EscapableHandleScope scope(isolate);
296317

297-
const auto source_it = source_.find(id);
298-
CHECK_NE(source_it, source_.end());
299-
Local<String> source = source_it->second.ToStringChecked(isolate);
318+
const Local<String> source = UInt8SpanResource::ToStringChecked(
319+
isolate, source_.at(id));
300320

301321
std::string filename_s = id + std::string(".js");
302322
Local<String> filename =

src/node_native_module.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33

44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

6-
#include <map>
7-
#include <set>
8-
#include <string>
96
#include "env.h"
7+
#include "node_javascript.h"
108
#include "node_mutex.h"
11-
#include "node_union_bytes.h"
129
#include "v8.h"
1310

11+
#include <memory>
12+
#include <set>
13+
#include <string>
14+
1415
namespace node {
16+
1517
namespace native_module {
1618

17-
using NativeModuleRecordMap = std::map<std::string, UnionBytes>;
1819
using NativeModuleCacheMap =
1920
std::unordered_map<std::string,
2021
std::unique_ptr<v8::ScriptCompiler::CachedData>>;
@@ -26,13 +27,16 @@ using NativeModuleCacheMap =
2627
// This class should not depend on a particular isolate, context, or
2728
// environment. Rather it should take them as arguments when necessary.
2829
// The instances of this class are per-process.
29-
class NativeModuleLoader {
30+
class NativeModuleLoader : public JavascriptEmbeddedCode {
3031
public:
31-
NativeModuleLoader();
32+
NativeModuleLoader() = default;
33+
~NativeModuleLoader() = default;
3234
// TODO(joyeecheung): maybe we should make this a singleton, instead of
3335
// putting it in per_process.
3436
NativeModuleLoader(const NativeModuleLoader&) = delete;
37+
NativeModuleLoader(const NativeModuleLoader&&) = delete;
3538
NativeModuleLoader& operator=(const NativeModuleLoader&) = delete;
39+
NativeModuleLoader& operator=(const NativeModuleLoader&&) = delete;
3640

3741
static void Initialize(v8::Local<v8::Object> target,
3842
v8::Local<v8::Value> unused,
@@ -75,12 +79,11 @@ class NativeModuleLoader {
7579

7680
// Generated by tools/js2c.py as node_javascript.cc
7781
void LoadJavaScriptSource(); // Loads data into source_
78-
UnionBytes GetConfig(); // Return data for config.gypi
7982

8083
// Generated by tools/generate_code_cache.js as node_code_cache.cc when
8184
// the build is configured with --code-cache-path=.... They are noops
8285
// in node_code_cache_stub.cc
83-
void LoadCodeCache(); // Loads data into code_cache_
86+
void LoadCodeCache(); // Loads data into code_cache_
8487

8588
// Compile a script as a NativeModule that can be loaded via
8689
// NativeModule.p.require in JS land.
@@ -96,9 +99,7 @@ class NativeModuleLoader {
9699

97100
ModuleCategories module_categories_;
98101

99-
NativeModuleRecordMap source_;
100102
NativeModuleCacheMap code_cache_;
101-
UnionBytes config_;
102103

103104
// Used to synchronize access to the code cache map
104105
Mutex code_cache_mutex_;

0 commit comments

Comments
 (0)