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