@@ -18,6 +18,7 @@ using v8::EscapableHandleScope;
18
18
using v8::Function;
19
19
using v8::FunctionCallbackInfo;
20
20
using v8::FunctionTemplate;
21
+ using v8::HandleScope;
21
22
using v8::Integer;
22
23
using v8::IntegrityLevel;
23
24
using v8::Isolate;
@@ -26,32 +27,31 @@ using v8::Local;
26
27
using v8::MaybeLocal;
27
28
using v8::Module;
28
29
using v8::Object;
29
- using v8::Persistent;
30
30
using v8::Promise;
31
31
using v8::ScriptCompiler;
32
32
using v8::ScriptOrigin;
33
33
using v8::String;
34
34
using v8::Value;
35
35
36
- static const char * EXTENSIONS[] = {" .mjs" , " .js" , " .json" , " .node" };
37
- std::map<int , std::vector<ModuleWrap*>*> ModuleWrap::module_map_;
36
+ static const char * const EXTENSIONS[] = {" .mjs" , " .js" , " .json" , " .node" };
38
37
39
38
ModuleWrap::ModuleWrap (Environment* env,
40
39
Local<Object> object,
41
40
Local<Module> module ,
42
41
Local<String> url) : BaseObject(env, object) {
43
- Isolate* iso = Isolate::GetCurrent ();
44
- module_.Reset (iso, module );
45
- url_.Reset (iso, url);
42
+ module_.Reset (env->isolate (), module );
43
+ url_.Reset (env->isolate (), url);
46
44
}
47
45
48
46
ModuleWrap::~ModuleWrap () {
49
- Local<Module> module = module_.Get (Isolate::GetCurrent ());
50
- std::vector<ModuleWrap*>* same_hash = module_map_[module ->GetIdentityHash ()];
51
- auto it = std::find (same_hash->begin (), same_hash->end (), this );
52
-
53
- if (it != same_hash->end ()) {
54
- same_hash->erase (it);
47
+ HandleScope scope (env ()->isolate ());
48
+ Local<Module> module = module_.Get (env ()->isolate ());
49
+ auto range = env ()->module_map .equal_range (module ->GetIdentityHash ());
50
+ for (auto it = range.first ; it != range.second ; ++it) {
51
+ if (it->second == this ) {
52
+ env ()->module_map .erase (it);
53
+ break ;
54
+ }
55
55
}
56
56
57
57
module_.Reset ();
@@ -119,12 +119,7 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
119
119
ModuleWrap* obj =
120
120
new ModuleWrap (Environment::GetCurrent (ctx), that, mod, url);
121
121
122
- if (ModuleWrap::module_map_.count (mod->GetIdentityHash ()) == 0 ) {
123
- ModuleWrap::module_map_[mod->GetIdentityHash ()] =
124
- new std::vector<ModuleWrap*>();
125
- }
126
-
127
- ModuleWrap::module_map_[mod->GetIdentityHash ()]->push_back (obj);
122
+ env->module_map .emplace (mod->GetIdentityHash (), obj);
128
123
Wrap (that, obj);
129
124
130
125
that->SetIntegrityLevel (ctx, IntegrityLevel::kFrozen );
@@ -170,8 +165,7 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
170
165
env->ThrowError (" linking error, expected resolver to return a promise" );
171
166
}
172
167
Local<Promise> resolve_promise = resolve_return_value.As <Promise>();
173
- obj->resolve_cache_ [specifier_std] = new Persistent<Promise>();
174
- obj->resolve_cache_ [specifier_std]->Reset (iso, resolve_promise);
168
+ obj->resolve_cache_ [specifier_std].Reset (env->isolate (), resolve_promise);
175
169
}
176
170
177
171
args.GetReturnValue ().Set (handle_scope.Escape (that));
@@ -187,6 +181,8 @@ void ModuleWrap::Instantiate(const FunctionCallbackInfo<Value>& args) {
187
181
bool ok = mod->Instantiate (ctx, ModuleWrap::ResolveCallback);
188
182
189
183
// clear resolve cache on instantiate
184
+ for (auto & entry : obj->resolve_cache_ )
185
+ entry.second .Reset ();
190
186
obj->resolve_cache_ .clear ();
191
187
192
188
if (!ok) {
@@ -214,18 +210,17 @@ MaybeLocal<Module> ModuleWrap::ResolveCallback(Local<Context> context,
214
210
Local<Module> referrer) {
215
211
Environment* env = Environment::GetCurrent (context);
216
212
Isolate* iso = Isolate::GetCurrent ();
217
- if (ModuleWrap::module_map_ .count (referrer->GetIdentityHash ()) == 0 ) {
213
+ if (env-> module_map .count (referrer->GetIdentityHash ()) == 0 ) {
218
214
env->ThrowError (" linking error, unknown module" );
219
215
return MaybeLocal<Module>();
220
216
}
221
217
222
- std::vector<ModuleWrap*>* possible_deps =
223
- ModuleWrap::module_map_[referrer->GetIdentityHash ()];
224
218
ModuleWrap* dependent = nullptr ;
225
-
226
- for (auto possible_dep : *possible_deps) {
227
- if (possible_dep->module_ == referrer) {
228
- dependent = possible_dep;
219
+ auto range = env->module_map .equal_range (referrer->GetIdentityHash ());
220
+ for (auto it = range.first ; it != range.second ; ++it) {
221
+ if (it->second ->module_ == referrer) {
222
+ dependent = it->second ;
223
+ break ;
229
224
}
230
225
}
231
226
@@ -243,7 +238,7 @@ MaybeLocal<Module> ModuleWrap::ResolveCallback(Local<Context> context,
243
238
}
244
239
245
240
Local<Promise> resolve_promise =
246
- dependent->resolve_cache_ [specifier_std]-> Get (iso);
241
+ dependent->resolve_cache_ [specifier_std]. Get (iso);
247
242
248
243
if (resolve_promise->State () != Promise::kFulfilled ) {
249
244
env->ThrowError (" linking error, dependency promises must be resolved on "
0 commit comments