Skip to content

Commit 8377cfe

Browse files
committed
Bug 1916277 - Part 10: Add a debug slot [[IsPreload]] in ModuleObject. r=jonco
This will allow us to identify the module is loaded during preloading phase. Differential Revision: https://phabricator.services.mozilla.com/D281155
1 parent 14a1867 commit 8377cfe

6 files changed

Lines changed: 50 additions & 3 deletions

File tree

js/loader/LoadedScript.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,13 @@ void ModuleScript::SetModuleRecord(Handle<JSObject*> aModuleRecord) {
456456
SetModulePrivate(mModuleRecord, PrivateValue(this));
457457
}
458458

459+
#ifdef DEBUG
460+
// Sync the [[PreloadSlot]] in ModuleObject.
461+
if (mModuleRecord) {
462+
SetModulePreload(mModuleRecord, mForPreload);
463+
}
464+
#endif
465+
459466
mozilla::HoldJSObjects(this);
460467
}
461468

@@ -479,7 +486,14 @@ void ModuleScript::SetErrorToRethrow(const Value& aError) {
479486
mErrorToRethrow = aError;
480487
}
481488

482-
void ModuleScript::SetForPreload(bool aValue) { mForPreload = aValue; }
489+
void ModuleScript::SetForPreload(bool aValue) {
490+
mForPreload = aValue;
491+
#ifdef DEBUG
492+
if (ModuleRecord()) {
493+
SetModulePreload(ModuleRecord(), aValue);
494+
}
495+
#endif
496+
}
483497
void ModuleScript::SetHadImportMap(bool aValue) { mHadImportMap = aValue; }
484498

485499
ResolvedModuleSet* ModuleScript::GetPreloadedResolvedSet() {

js/loader/ModuleLoaderBase.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,8 +1055,8 @@ nsresult ModuleLoaderBase::CreateModuleScript(ModuleLoadRequest* aRequest) {
10551055
moduleScript->SetModuleRecord(module);
10561056
}
10571057

1058-
LOG(("ScriptLoadRequest (%p): module script == %p", aRequest,
1059-
aRequest->mModuleScript.get()));
1058+
LOG(("ScriptLoadRequest (%p): module script == %p ForPreload %d", aRequest,
1059+
aRequest->mModuleScript.get(), aRequest->mModuleScript->ForPreload()));
10601060

10611061
return rv;
10621062
}

js/public/Modules.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,14 @@ extern JS_PUBLIC_API Value GetModulePrivate(JSObject* module);
246246
*/
247247
extern JS_PUBLIC_API bool IsCyclicModule(JSObject* module);
248248

249+
#ifdef DEBUG
250+
/**
251+
* A helper function to set the isPreload flag on the ModuleObject.
252+
* The flag will be verified later when ResetPreloadedModule is called.
253+
*/
254+
extern JS_PUBLIC_API void SetModulePreload(JSObject* module, bool isPreload);
255+
#endif
256+
249257
/*
250258
* Perform the ModuleLink operation on the given source text module record.
251259
*

js/src/builtin/ModuleObject.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,16 @@ void ModuleObject::setMetaObject(JSObject* obj) {
13421342
cyclicModuleFields()->metaObject = obj;
13431343
}
13441344

1345+
#ifdef DEBUG
1346+
void ModuleObject::setPreload(bool isPreload) {
1347+
setReservedSlot(PreloadSlot, BooleanValue(isPreload));
1348+
}
1349+
1350+
bool ModuleObject::isPreload() const {
1351+
return getReservedSlot(PreloadSlot).toBoolean();
1352+
}
1353+
#endif
1354+
13451355
/* static */
13461356
void ModuleObject::trace(JSTracer* trc, JSObject* obj) {
13471357
ModuleObject& module = obj->as<ModuleObject>();

js/src/builtin/ModuleObject.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,9 @@ class ModuleObject : public NativeObject {
395395
CyclicModuleFieldsSlot,
396396
// `SyntheticModuleFields` if a synthetic module. Otherwise `undefined`.
397397
SyntheticModuleFieldsSlot,
398+
#ifdef DEBUG
399+
PreloadSlot,
400+
#endif
398401
SlotCount
399402
};
400403

@@ -501,6 +504,11 @@ class ModuleObject : public NativeObject {
501504
void initAsyncSlots(JSContext* cx, bool hasTopLevelAwait,
502505
Handle<ListObject*> asyncParentModules);
503506

507+
#ifdef DEBUG
508+
void setPreload(bool isPreload);
509+
bool isPreload() const;
510+
#endif
511+
504512
private:
505513
static const JSClassOps classOps_;
506514

js/src/vm/Modules.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,13 @@ JS_PUBLIC_API bool JS::IsCyclicModule(JSObject* module) {
338338
return module->as<ModuleObject>().hasCyclicModuleFields();
339339
}
340340

341+
#ifdef DEBUG
342+
JS_PUBLIC_API void JS::SetModulePreload(JSObject* module, bool isPreload) {
343+
MOZ_ASSERT(module->is<ModuleObject>());
344+
module->as<ModuleObject>().setPreload(isPreload);
345+
}
346+
#endif
347+
341348
JS_PUBLIC_API bool JS::ModuleLink(JSContext* cx, Handle<JSObject*> moduleArg) {
342349
AssertHeapIsIdle();
343350
CHECK_THREAD(cx);

0 commit comments

Comments
 (0)