Skip to content

Commit ac9978d

Browse files
committed
Bug 1878457 - Part 1: ESMify performance-new modules. r=julienw,profiler-reviewers
Differential Revision: https://phabricator.services.mozilla.com/D200558
1 parent 603e42f commit ac9978d

File tree

10 files changed

+81
-100
lines changed

10 files changed

+81
-100
lines changed

devtools/client/performance-new/@types/gecko.d.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ declare namespace MockedExports {
2626
"resource:///modules/CustomizableUI.sys.mjs": typeof import("resource:///modules/CustomizableUI.sys.mjs");
2727
"resource:///modules/CustomizableWidgets.sys.mjs": typeof import("resource:///modules/CustomizableWidgets.sys.mjs");
2828
"resource://devtools/shared/loader/Loader.sys.mjs": typeof import("resource://devtools/shared/loader/Loader.sys.mjs");
29-
"resource://devtools/client/performance-new/shared/background.jsm.js": typeof import("resource://devtools/client/performance-new/shared/background.jsm.js");
30-
"resource://devtools/client/performance-new/shared/symbolication.jsm.js": typeof import("resource://devtools/client/performance-new/shared/symbolication.jsm.js");
29+
"resource://devtools/client/performance-new/shared/background.sys.mjs": typeof import("resource://devtools/client/performance-new/shared/background.sys.mjs");
30+
"resource://devtools/client/performance-new/shared/symbolication.sys.mjs": typeof import("resource://devtools/client/performance-new/shared/symbolication.sys.mjs");
3131
"resource://devtools/shared/loader/browser-loader.js": any;
32-
"resource://devtools/client/performance-new/popup/menu-button.jsm.js": typeof import("resource://devtools/client/performance-new/popup/menu-button.jsm.js");
33-
"resource://devtools/client/performance-new/shared/typescript-lazy-load.jsm.js": typeof import("resource://devtools/client/performance-new/shared/typescript-lazy-load.jsm.js");
34-
"resource://devtools/client/performance-new/popup/logic.jsm.js": typeof import("resource://devtools/client/performance-new/popup/logic.jsm.js");
32+
"resource://devtools/client/performance-new/popup/menu-button.sys.mjs": typeof import("resource://devtools/client/performance-new/popup/menu-button.sys.mjs");
33+
"resource://devtools/client/performance-new/shared/typescript-lazy-load.sys.mjs": typeof import("resource://devtools/client/performance-new/shared/typescript-lazy-load.sys.mjs");
34+
"resource://devtools/client/performance-new/popup/logic.sys.mjs": typeof import("resource://devtools/client/performance-new/popup/logic.sys.mjs");
3535
"resource:///modules/PanelMultiView.sys.mjs": typeof import("resource:///modules/PanelMultiView.sys.mjs");
3636
}
3737

@@ -339,13 +339,13 @@ declare module "resource://gre/modules/AppConstants.sys.mjs" {
339339
export = MockedExports.AppConstantsSYSMJS;
340340
}
341341

342-
declare module "resource://devtools/client/performance-new/shared/background.jsm.js" {
343-
import * as Background from "devtools/client/performance-new/shared/background.jsm.js";
342+
declare module "resource://devtools/client/performance-new/shared/background.sys.mjs" {
343+
import * as Background from "devtools/client/performance-new/shared/background.sys.mjs";
344344
export = Background;
345345
}
346346

347-
declare module "resource://devtools/client/performance-new/shared/symbolication.jsm.js" {
348-
import * as PerfSymbolication from "devtools/client/performance-new/shared/symbolication.jsm.js";
347+
declare module "resource://devtools/client/performance-new/shared/symbolication.sys.mjs" {
348+
import * as PerfSymbolication from "devtools/client/performance-new/shared/symbolication.sys.mjs";
349349
export = PerfSymbolication;
350350
}
351351

devtools/client/performance-new/popup/logic.jsm.js renamed to devtools/client/performance-new/popup/logic.sys.mjs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44
// @ts-check
5-
"use strict";
65

76
/**
87
* This file controls the logic of the profiler popup view.
@@ -309,21 +308,9 @@ function addPopupEventHandlers(state, elements, view) {
309308
* @param {State} panelState
310309
* @param {XULElement} panelview
311310
*/
312-
function initializePopup(panelState, panelview) {
311+
export function initializePopup(panelState, panelview) {
313312
const panelElements = selectElementsInPanelview(panelview);
314313
const panelviewControllers = createViewControllers(panelState, panelElements);
315314
addPopupEventHandlers(panelState, panelElements, panelviewControllers);
316315
initializeView(panelState, panelElements, panelviewControllers);
317316
}
318-
319-
// Provide an exports object for the JSM to be properly read by TypeScript.
320-
/** @type {any} */
321-
var module = {};
322-
323-
module.exports = {
324-
initializePopup,
325-
};
326-
327-
// Object.keys() confuses the linting which expects a static array expression.
328-
// eslint-disable-next-line
329-
var EXPORTED_SYMBOLS = Object.keys(module.exports);

devtools/client/performance-new/popup/menu-button.jsm.js renamed to devtools/client/performance-new/popup/menu-button.sys.mjs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44
// @ts-check
5-
"use strict";
65

76
/**
87
* This file controls the enabling and disabling of the menu button for the profiler.
98
* Care should be taken to keep it minimal as it can be run with browser initialization.
109
*/
1110

12-
// Provide an exports object for the JSM to be properly read by TypeScript.
13-
/** @type {any} */
14-
var exports = {};
15-
1611
const { createLazyLoaders } = ChromeUtils.import(
1712
"resource://devtools/client/performance-new/shared/typescript-lazy-load.jsm.js"
1813
);
@@ -112,7 +107,7 @@ function initialize(toggleProfilerKeyShortcuts) {
112107
/**
113108
* This is mutable state that will be shared between panel displays.
114109
*
115-
* @type {import("devtools/client/performance-new/popup/logic.jsm.js").State}
110+
* @type {import("devtools/client/performance-new/popup/logic.sys.mjs").State}
116111
*/
117112
const panelState = {
118113
cleanup: [],
@@ -318,16 +313,10 @@ function initialize(toggleProfilerKeyShortcuts) {
318313
CustomizableWidgets.push(item);
319314
}
320315

321-
const ProfilerMenuButton = {
316+
export const ProfilerMenuButton = {
322317
initialize,
323318
addToNavbar,
324319
isInNavbar,
325320
openPopup,
326321
remove,
327322
};
328-
329-
exports.ProfilerMenuButton = ProfilerMenuButton;
330-
331-
// Object.keys() confuses the linting which expects a static array expression.
332-
// eslint-disable-next-line
333-
var EXPORTED_SYMBOLS = Object.keys(exports);

devtools/client/performance-new/popup/moz.build

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
55

66
DevToolsModules(
7-
"logic.jsm.js",
8-
"menu-button.jsm.js",
7+
"logic.sys.mjs",
8+
"menu-button.sys.mjs",
99
)
1010

1111
with Files("**"):

devtools/client/performance-new/shared/background.jsm.js renamed to devtools/client/performance-new/shared/background.sys.mjs

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44
// @ts-check
5-
"use strict";
65

76
/**
87
* This file contains all of the background logic for controlling the state and
@@ -122,7 +121,7 @@ const lazy = createLazyLoaders({
122121
// https://github.com/mozilla-mobile/firefox-android/blob/1d177e7e78d027e8ab32cedf0fc68316787d7454/fenix/app/src/main/java/org/mozilla/fenix/perf/ProfilerUtils.kt
123122

124123
/** @type {Presets} */
125-
const presets = {
124+
export const presets = {
126125
"web-developer": {
127126
entries: 128 * 1024 * 1024,
128127
interval: 1,
@@ -311,7 +310,7 @@ const presets = {
311310
* @param {PageContext} pageContext
312311
* @return {ProfilerViewMode | undefined}
313312
*/
314-
function getProfilerViewModeForCurrentPreset(pageContext) {
313+
export function getProfilerViewModeForCurrentPreset(pageContext) {
315314
const prefPostfix = getPrefPostfix(pageContext);
316315
const presetName = Services.prefs.getCharPref(PRESET_PREF + prefPostfix);
317316

@@ -334,7 +333,7 @@ function getProfilerViewModeForCurrentPreset(pageContext) {
334333
* @param {PageContext} pageContext
335334
* @return {Promise<void>}
336335
*/
337-
async function captureProfile(pageContext) {
336+
export async function captureProfile(pageContext) {
338337
if (!Services.profiler.IsActive()) {
339338
// The profiler is not active, ignore.
340339
return;
@@ -397,7 +396,7 @@ async function captureProfile(pageContext) {
397396
* popup.
398397
* @param {PageContext} pageContext
399398
*/
400-
function startProfiler(pageContext) {
399+
export function startProfiler(pageContext) {
401400
const { entries, interval, features, threads, duration } =
402401
getRecordingSettings(pageContext, Services.profiler.GetFeatures());
403402

@@ -420,7 +419,7 @@ function startProfiler(pageContext) {
420419
* using the shortcut keys to capture a profile.
421420
* @type {() => void}
422421
*/
423-
function stopProfiler() {
422+
export function stopProfiler() {
424423
Services.profiler.StopProfiler();
425424
}
426425

@@ -430,7 +429,7 @@ function stopProfiler() {
430429
* @param {PageContext} pageContext
431430
* @return {void}
432431
*/
433-
function toggleProfiler(pageContext) {
432+
export function toggleProfiler(pageContext) {
434433
if (Services.profiler.IsPaused()) {
435434
// The profiler is currently paused, which means that the user is already
436435
// attempting to capture a profile. Ignore this request.
@@ -446,7 +445,7 @@ function toggleProfiler(pageContext) {
446445
/**
447446
* @param {PageContext} pageContext
448447
*/
449-
function restartProfiler(pageContext) {
448+
export function restartProfiler(pageContext) {
450449
stopProfiler();
451450
startProfiler(pageContext);
452451
}
@@ -528,7 +527,7 @@ function getObjdirPrefValue() {
528527
* @param {string[]} supportedFeatures
529528
* @returns {RecordingSettings}
530529
*/
531-
function getRecordingSettings(pageContext, supportedFeatures) {
530+
export function getRecordingSettings(pageContext, supportedFeatures) {
532531
const objdirs = getObjdirPrefValue();
533532
const prefPostfix = getPrefPostfix(pageContext);
534533
const presetName = Services.prefs.getCharPref(PRESET_PREF + prefPostfix);
@@ -616,7 +615,7 @@ function getRecordingSettingsFromPrefs(
616615
* @param {PageContext} pageContext
617616
* @param {RecordingSettings} prefs
618617
*/
619-
function setRecordingSettings(pageContext, prefs) {
618+
export function setRecordingSettings(pageContext, prefs) {
620619
const prefPostfix = getPrefPostfix(pageContext);
621620
Services.prefs.setCharPref(PRESET_PREF + prefPostfix, prefs.presetName);
622621
Services.prefs.setIntPref(ENTRIES_PREF + prefPostfix, prefs.entries);
@@ -637,13 +636,13 @@ function setRecordingSettings(pageContext, prefs) {
637636
setObjdirPrefValue(prefs.objdirs);
638637
}
639638

640-
const platform = AppConstants.platform;
639+
export const platform = AppConstants.platform;
641640

642641
/**
643642
* Revert the recording prefs for both local and remote profiling.
644643
* @return {void}
645644
*/
646-
function revertRecordingSettings() {
645+
export function revertRecordingSettings() {
647646
for (const prefPostfix of ["", ".remote"]) {
648647
Services.prefs.clearUserPref(PRESET_PREF + prefPostfix);
649648
Services.prefs.clearUserPref(ENTRIES_PREF + prefPostfix);
@@ -664,7 +663,7 @@ function revertRecordingSettings() {
664663
* @param {string[]} supportedFeatures
665664
* @return {void}
666665
*/
667-
function changePreset(pageContext, presetName, supportedFeatures) {
666+
export function changePreset(pageContext, presetName, supportedFeatures) {
668667
const prefPostfix = getPrefPostfix(pageContext);
669668
const objdirs = getObjdirPrefValue();
670669
let recordingSettings = getRecordingSettingsFromPreset(
@@ -689,7 +688,7 @@ function changePreset(pageContext, presetName, supportedFeatures) {
689688
* @param {PrefObserver} observer
690689
* @return {void}
691690
*/
692-
function addPrefObserver(observer) {
691+
export function addPrefObserver(observer) {
693692
Services.prefs.addObserver(PREF_PREFIX, observer);
694693
}
695694

@@ -698,7 +697,7 @@ function addPrefObserver(observer) {
698697
* @param {PrefObserver} observer
699698
* @return {void}
700699
*/
701-
function removePrefObserver(observer) {
700+
export function removePrefObserver(observer) {
702701
Services.prefs.removeObserver(PREF_PREFIX, observer);
703702
}
704703

@@ -867,7 +866,7 @@ function getSymbolicationServiceForBrowser(browser) {
867866
* @param {any} message
868867
* @param {MockedExports.WebChannelTarget} target
869868
*/
870-
async function handleWebChannelMessage(channel, id, message, target) {
869+
export async function handleWebChannelMessage(channel, id, message, target) {
871870
if (typeof message !== "object" || typeof message.type !== "string") {
872871
console.error(
873872
"An malformed message was received by the profiler's WebChannel handler.",
@@ -918,7 +917,7 @@ async function handleWebChannelMessage(channel, id, message, target) {
918917
* method should obtain a symbol table for the requested binary and resolve the
919918
* returned promise with it.
920919
*/
921-
function registerProfileCaptureForBrowser(
920+
export function registerProfileCaptureForBrowser(
922921
browser,
923922
profileCaptureResult,
924923
symbolicationService
@@ -928,30 +927,3 @@ function registerProfileCaptureForBrowser(
928927
symbolicationService,
929928
});
930929
}
931-
932-
// Provide a fake module.exports for the JSM to be properly read by TypeScript.
933-
/** @type {any} */
934-
var module = { exports: {} };
935-
936-
module.exports = {
937-
presets,
938-
captureProfile,
939-
startProfiler,
940-
stopProfiler,
941-
restartProfiler,
942-
toggleProfiler,
943-
platform,
944-
getRecordingSettings,
945-
setRecordingSettings,
946-
revertRecordingSettings,
947-
changePreset,
948-
handleWebChannelMessage,
949-
registerProfileCaptureForBrowser,
950-
addPrefObserver,
951-
removePrefObserver,
952-
getProfilerViewModeForCurrentPreset,
953-
};
954-
955-
// Object.keys() confuses the linting which expects a static array expression.
956-
// eslint-disable-next-line
957-
var EXPORTED_SYMBOLS = Object.keys(module.exports);

devtools/client/performance-new/shared/moz.build

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
55

66
DevToolsModules(
7-
"background.jsm.js",
7+
"background.sys.mjs",
88
"browser.js",
99
"profiler_get_symbols.js",
10-
"symbolication.jsm.js",
10+
"symbolication.sys.mjs",
1111
"symbolication.worker.js",
12-
"typescript-lazy-load.jsm.js",
12+
"typescript-lazy-load.sys.mjs",
1313
"utils.js",
1414
)
1515

devtools/client/performance-new/shared/symbolication.jsm.js renamed to devtools/client/performance-new/shared/symbolication.sys.mjs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44
// @ts-check
5-
"use strict";
65

76
/** @type {any} */
87
const lazy = {};
@@ -346,7 +345,11 @@ class LocalSymbolicationServiceWithRemoteSymbolTableFallback {
346345
* tables from remote targets
347346
* @return {SymbolicationService}
348347
*/
349-
function createLocalSymbolicationService(sharedLibraries, objdirs, perfFront) {
348+
export function createLocalSymbolicationService(
349+
sharedLibraries,
350+
objdirs,
351+
perfFront
352+
) {
350353
const service = new LocalSymbolicationService(sharedLibraries, objdirs);
351354
if (perfFront) {
352355
return new LocalSymbolicationServiceWithRemoteSymbolTableFallback(
@@ -357,15 +360,3 @@ function createLocalSymbolicationService(sharedLibraries, objdirs, perfFront) {
357360
}
358361
return service;
359362
}
360-
361-
// Provide an exports object for the JSM to be properly read by TypeScript.
362-
/** @type {any} */
363-
var module = {};
364-
365-
module.exports = {
366-
createLocalSymbolicationService,
367-
};
368-
369-
// Object.keys() confuses the linting which expects a static array expression.
370-
// eslint-disable-next-line
371-
var EXPORTED_SYMBOLS = Object.keys(module.exports);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
// @ts-check
5+
6+
/**
7+
* TypeScript can't understand the lazyRequireGetter mechanism, due to how it defines
8+
* properties as a getter. This function, instead provides lazy loading in a
9+
* TypeScript-friendly manner. It applies the lazy load memoization to each property
10+
* of the provided object.
11+
*
12+
* Example usage:
13+
*
14+
* const lazy = createLazyLoaders({
15+
* moduleA: () => require("module/a"),
16+
* moduleB: () => require("module/b"),
17+
* });
18+
*
19+
* Later:
20+
*
21+
* const moduleA = lazy.moduleA();
22+
* const { objectInModuleB } = lazy.moduleB();
23+
*
24+
* @template {{ [key: string]: any }} T
25+
* @param {T} definition - An object where each property has a function that loads a module.
26+
* @returns {T} - The load memoized version of T.
27+
*/
28+
export function createLazyLoaders(definition) {
29+
/** @type {any} */
30+
const result = {};
31+
for (const [key, callback] of Object.entries(definition)) {
32+
/** @type {any} */
33+
let cache;
34+
result[key] = () => {
35+
if (cache === undefined) {
36+
cache = callback();
37+
}
38+
return cache;
39+
};
40+
}
41+
return result;
42+
}

0 commit comments

Comments
 (0)