11#include " node_native_module.h"
22#include " node_errors.h"
3+ #include " env.h"
4+
5+ #include < set>
36
47namespace node {
58
@@ -16,18 +19,15 @@ using v8::DEFAULT;
1619using v8::EscapableHandleScope;
1720using v8::Function;
1821using v8::FunctionCallbackInfo;
19- using v8::HandleScope;
2022using v8::Integer;
2123using v8::IntegrityLevel;
2224using v8::Isolate;
2325using v8::Local;
24- using v8::Maybe;
2526using v8::MaybeLocal;
2627using v8::Name;
2728using v8::None;
2829using v8::Object;
2930using v8::PropertyCallbackInfo;
30- using v8::Script;
3131using v8::ScriptCompiler;
3232using v8::ScriptOrigin;
3333using v8::Set;
@@ -36,6 +36,32 @@ using v8::String;
3636using v8::Uint8Array;
3737using 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+
3965void 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 =
0 commit comments