Bug 1378173 - Remove Promise.jsm. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D128098
This commit is contained in:
Mark Banner 2021-10-14 08:28:28 +00:00
Родитель d00eca3132
Коммит 3633fd7457
37 изменённых файлов: 75 добавлений и 2432 удалений

Просмотреть файл

@ -67,7 +67,6 @@ const startupPhases = {
"resource://gre/modules/NewTabUtils.jsm",
"resource://gre/modules/PageThumbs.jsm",
"resource://gre/modules/PlacesUtils.jsm",
"resource://gre/modules/Promise.jsm", // imported by devtools during _delayedStartup
"resource://gre/modules/Preferences.jsm",
"resource://gre/modules/Sqlite.jsm",
]),

Просмотреть файл

@ -207,8 +207,6 @@ var whitelist = [
{ file: "resource://gre/modules/Manifest.jsm" },
// Bug 1356045
{ file: "chrome://global/content/test-ipc.xhtml" },
// Bug 1378173 (warning: still used by devtools)
{ file: "resource://gre/modules/Promise.jsm" },
// Bug 1494170
// (The references to these files are dynamically generated, so the test can't
// find the references)

Просмотреть файл

@ -13,11 +13,6 @@ const { PromiseTestUtils } = ChromeUtils.import(
PromiseTestUtils.disableUncaughtRejectionObserverForSelfTest();
add_task(async function test_globals() {
Assert.equal(
Promise.defer || undefined,
undefined,
"We are testing DOM Promise."
);
Assert.notEqual(
PromiseDebugging,
undefined,

Просмотреть файл

@ -24,7 +24,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=937317
SpecialPowers.pushPrefEnv({"set": [["security.allow_eval_with_system_principal",
true]]});
addLoadEvent(function() {
const {Promise} = ChromeUtils.import("resource://gre/modules/Promise.jsm");
const {PromiseUtils} = ChromeUtils.import("resource://gre/modules/PromiseUtils.jsm");
iwin = window[0];
// Smoketest.
@ -66,25 +66,25 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=937317
// Callbacks grab the incumbent script at the time of invocation.
//
// Note - We avoid calling the initial defer |d| so that it's not in-scope for everything below.
let initialDefer = Promise.defer();
let initialDefer = PromiseUtils.defer();
setTimeout(Cu.getIncumbentGlobal.bind(Cu, makeCallback(window, initialDefer, "same-global setTimeout")), 0);
initialDefer.promise.then(function() {
// Try cross-global setTimeout where |window| is the incumbent script when the callback is created.
let d = Promise.defer();
let d = PromiseUtils.defer();
iwin.setTimeout(Cu.getIncumbentGlobal.bind(Cu, makeCallback(window, d, "cross-global setTimeout by |window|")), 0);
return d.promise;
}).then(function() {
// Try cross-global setTimeout where |iwin| is the incumbent script when the callback is created.
let d = Promise.defer();
let d = PromiseUtils.defer();
iwin.wrappedJSObject.timeoutFun = Cu.getIncumbentGlobal.bind(Cu, makeCallback(iwin, d, "cross-global setTimeout by |iwin|"));
iwin.eval('setTimeout(timeoutFun, 0);');
return d.promise;
}).then(function() {
// The incumbent script override doesn't take effect if the callback is scripted.
let d = Promise.defer();
let d = PromiseUtils.defer();
iwin.wrappedJSObject.timeoutFun2 = Cu.getIncumbentGlobal.bind(Cu, makeCallback(iwin, d, "cross-global setTimeout of scripted function"));
iwin.eval('var timeoutFun2Wrapper = function() { timeoutFun2(); }');
setTimeout(iwin.wrappedJSObject.timeoutFun2Wrapper, 0);
@ -92,7 +92,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=937317
}).then(function() {
// Try event listeners.
let d = Promise.defer();
let d = PromiseUtils.defer();
let body = iwin.document.body;
body.addEventListener('click', Cu.getIncumbentGlobal.bind(Cu, makeCallback(window, d, "cross-global event listener")));
body.dispatchEvent(new iwin.MouseEvent('click'));
@ -101,7 +101,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=937317
}).then(function() {
// Try an event handler set by |iwin|.
let d = Promise.defer();
let d = PromiseUtils.defer();
let body = iwin.document.body;
iwin.wrappedJSObject.handler = Cu.getIncumbentGlobal.bind(Cu, makeCallback(iwin, d, "cross-global event handler"));
iwin.eval('document.body.onmousemove = handler');
@ -111,7 +111,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=937317
}).then(function() {
// Try an event handler compiled by a content attribute.
let d = Promise.defer();
let d = PromiseUtils.defer();
let body = iwin.document.body;
iwin.wrappedJSObject.innerHandler = Cu.getIncumbentGlobal.bind(Cu, makeCallback(iwin, d, "cross-global compiled event handler"));
iwin.eval("document.body.setAttribute('onmouseout', 'innerHandler()')");

Просмотреть файл

@ -16,7 +16,9 @@ const { MockRegistrar } = ChromeUtils.import(
"resource://testing-common/MockRegistrar.jsm"
);
const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
const { Promise } = ChromeUtils.import("resource://gre/modules/Promise.jsm");
const { PromiseUtils } = ChromeUtils.import(
"resource://gre/modules/PromiseUtils.jsm"
);
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
@ -519,7 +521,7 @@ async function asyncConnectTo(
function Connection(host) {
this.host = host;
this.thread = Services.tm.currentThread;
this.defer = Promise.defer();
this.defer = PromiseUtils.defer();
let sts = Cc["@mozilla.org/network/socket-transport-service;1"].getService(
Ci.nsISocketTransportService
);

Просмотреть файл

@ -10,7 +10,6 @@
<script src="chrome://mochikit/content/chrome-harness.js"></script>
<script type="application/javascript">
<![CDATA[
let {Promise} = ChromeUtils.import("resource://gre/modules/Promise.jsm");
const {OS} = ChromeUtils.import("resource://gre/modules/osfile.jsm");
let decoder = new TextDecoder();

Просмотреть файл

@ -3,17 +3,12 @@
"use strict";
setExpectedFailuresForSelfTest(8);
// Keep "JSMPromise" separate so "Promise" still refers to native Promises.
let JSMPromise = ChromeUtils.import("resource://gre/modules/Promise.jsm", {})
.Promise;
setExpectedFailuresForSelfTest(4);
async function rejectOnNextTick(error) {
await Promise.resolve();
Promise.reject(error);
JSMPromise.reject(error);
}
add_task(async function failWithoutError() {

Просмотреть файл

@ -1,19 +1,13 @@
setExpectedFailuresForSelfTest(3);
// Keep "JSMPromise" separate so "Promise" still refers to native Promises.
let JSMPromise = ChromeUtils.import("resource://gre/modules/Promise.jsm", {})
.Promise;
setExpectedFailuresForSelfTest(2);
function test() {
Promise.reject(new Error("Promise rejection."));
JSMPromise.reject(new Error("Promise.jsm rejection."));
(async () => {
throw new Error("Synchronous rejection from async function.");
})();
// The following rejections are caught, so they won't result in failures.
Promise.reject(new Error("Promise rejection.")).catch(() => {});
JSMPromise.reject(new Error("Promise.jsm rejection.")).catch(() => {});
(async () => {
throw new Error("Synchronous rejection from async function.");
})().catch(() => {});

Просмотреть файл

@ -1,13 +1,7 @@
// Keep "JSMPromise" separate so "Promise" still refers to native Promises.
let JSMPromise = ChromeUtils.import("resource://gre/modules/Promise.jsm", {})
.Promise;
const { PromiseTestUtils } = ChromeUtils.import(
"resource://testing-common/PromiseTestUtils.jsm"
);
PromiseTestUtils.allowMatchingRejectionsGlobally(/Allowed rejection./);
PromiseTestUtils.expectUncaughtRejection(/Promise.jsm rejection./);
PromiseTestUtils.expectUncaughtRejection(/Promise.jsm rejection./);
PromiseTestUtils.expectUncaughtRejection(/Promise rejection./);
PromiseTestUtils.expectUncaughtRejection(/Promise rejection./);
@ -15,7 +9,4 @@ function test() {
Promise.reject(new Error("Promise rejection."));
Promise.reject(new Error("Promise rejection."));
Promise.reject(new Error("Allowed rejection."));
JSMPromise.reject(new Error("Promise.jsm rejection."));
JSMPromise.reject(new Error("Promise.jsm rejection."));
JSMPromise.reject(new Error("Allowed rejection."));
}

Просмотреть файл

@ -40,16 +40,6 @@ function run_test() {
}
"""
SIMPLE_UNCAUGHT_REJECTION_JSM_TEST = """
Components.utils.import("resource://gre/modules/Promise.jsm");
Promise.reject(new Error("Test rejection."));
function run_test() {
Assert.ok(true);
}
"""
ADD_TEST_SIMPLE = """
function run_test() { run_next_test(); }
@ -77,17 +67,6 @@ add_test(function test_uncaught_rejection() {
});
"""
ADD_TEST_UNCAUGHT_REJECTION_JSM = """
Components.utils.import("resource://gre/modules/Promise.jsm");
function run_test() { run_next_test(); }
add_test(function test_uncaught_rejection() {
Promise.reject(new Error("Test rejection."));
run_next_test();
});
"""
CHILD_TEST_PASSING = """
function run_test () { run_next_test(); }
@ -148,8 +127,6 @@ add_test(function test_unicode_print () {
"""
ADD_TASK_SINGLE = """
Components.utils.import("resource://gre/modules/Promise.jsm");
function run_test() { run_next_test(); }
add_task(async function test_task() {
@ -159,8 +136,6 @@ add_task(async function test_task() {
"""
ADD_TASK_MULTIPLE = """
Components.utils.import("resource://gre/modules/Promise.jsm");
function run_test() { run_next_test(); }
add_task(async function test_task() {
@ -173,8 +148,6 @@ add_task(async function test_2() {
"""
ADD_TASK_REJECTED = """
Components.utils.import("resource://gre/modules/Promise.jsm");
function run_test() { run_next_test(); }
add_task(async function test_failing() {
@ -183,8 +156,6 @@ add_task(async function test_failing() {
"""
ADD_TASK_FAILURE_INSIDE = """
Components.utils.import("resource://gre/modules/Promise.jsm");
function run_test() { run_next_test(); }
add_task(async function test() {
@ -205,8 +176,6 @@ add_task(function () {
"""
ADD_TASK_STACK_TRACE = """
Components.utils.import("resource://gre/modules/Promise.jsm", this);
function run_test() { run_next_test(); }
add_task(async function this_test_will_fail() {
@ -326,7 +295,7 @@ function run_test () {
# A test for asynchronous cleanup functions
ASYNC_CLEANUP = """
function run_test() {
Components.utils.import("resource://gre/modules/Promise.jsm", this);
let { PromiseUtils } = Components.utils.import("resource://gre/modules/PromiseUtils.jsm", this);
// The list of checkpoints in the order we encounter them.
let checkpoints = [];
@ -353,7 +322,7 @@ function run_test() {
});
registerCleanupFunction(function async_cleanup_2() {
let deferred = Promise.defer();
let deferred = PromiseUtils.defer();
executeSoon(deferred.resolve);
return deferred.promise.then(function() {
checkpoints.push(3);
@ -365,7 +334,7 @@ function run_test() {
});
registerCleanupFunction(function async_cleanup() {
let deferred = Promise.defer();
let deferred = PromiseUtils.defer();
executeSoon(deferred.resolve);
return deferred.promise.then(function() {
checkpoints.push(1);
@ -422,8 +391,6 @@ add_task(function no_run_test_add_task_fail() {
"""
NO_RUN_TEST_ADD_TASK_MULTIPLE = """
Components.utils.import("resource://gre/modules/Promise.jsm");
add_task(async function test_task() {
await Promise.resolve(true);
});
@ -1020,23 +987,6 @@ add_test({
self.assertEquals(0, self.x.passCount)
self.assertEquals(1, self.x.failCount)
def testUncaughtRejectionJSM(self):
"""
Ensure a simple test with an uncaught rejection from Promise.jsm is reported.
"""
self.writeFile(
"test_simple_uncaught_rejection_jsm.js", SIMPLE_UNCAUGHT_REJECTION_JSM_TEST
)
self.writeManifest(["test_simple_uncaught_rejection_jsm.js"])
self.assertTestResult(False)
self.assertInLog(TEST_FAIL_STRING)
self.assertInLog("test_simple_uncaught_rejection_jsm.js:4:16")
self.assertInLog("Test rejection.")
self.assertEquals(1, self.x.testCount)
self.assertEquals(0, self.x.passCount)
self.assertEquals(1, self.x.failCount)
def testAddTestSimple(self):
"""
Ensure simple add_test() works.
@ -1102,20 +1052,6 @@ add_test({
self.assertEquals(0, self.x.passCount)
self.assertEquals(1, self.x.failCount)
def testAddTestUncaughtRejectionJSM(self):
"""
Ensure add_test() with an uncaught rejection from Promise.jsm is reported.
"""
self.writeFile(
"test_add_test_uncaught_rejection_jsm.js", ADD_TEST_UNCAUGHT_REJECTION_JSM
)
self.writeManifest(["test_add_test_uncaught_rejection_jsm.js"])
self.assertTestResult(False)
self.assertEquals(1, self.x.testCount)
self.assertEquals(0, self.x.passCount)
self.assertEquals(1, self.x.failCount)
def testAddTaskTestSingle(self):
"""
Ensure add_test_task() with a single passing test works.

Просмотреть файл

@ -5,7 +5,9 @@
"use strict";
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
const { Promise } = ChromeUtils.import("resource://gre/modules/Promise.jsm");
const { PromiseUtils } = ChromeUtils.import(
"resource://gre/modules/PromiseUtils.jsm"
);
function makeWatcher() {
let watcher = Cc[

Просмотреть файл

@ -48,7 +48,7 @@ add_task(async function test_watch_single_path_file_creation_no_error_cb() {
// Instantiate and initialize the native watcher.
let watcher = makeWatcher();
let deferred = Promise.defer();
let deferred = PromiseUtils.defer();
// Watch the profile directory but do not pass an error callback.
await promiseAddPath(watcher, watchedDir, deferred.resolve);

Просмотреть файл

@ -31,7 +31,7 @@ add_task(async function test_watch_with_shared_callback() {
// Instantiate and initialize the native watcher.
let watcher = makeWatcher();
let deferred = Promise.defer();
let deferred = PromiseUtils.defer();
// Watch both directories using the same callbacks.
await promiseAddPath(

Просмотреть файл

@ -29,7 +29,7 @@ add_task(async function test_watch_single_path_directory_creation() {
// Instantiate and initialize the native watcher.
let watcher = makeWatcher();
let deferred = Promise.defer();
let deferred = PromiseUtils.defer();
// Add the profile directory to the watch list and wait for the file watcher
// to start watching.

Просмотреть файл

@ -23,7 +23,7 @@ add_task(async function test_watch_single_path_directory_deletion() {
// Instantiate and initialize the native watcher.
let watcher = makeWatcher();
let deferred = Promise.defer();
let deferred = PromiseUtils.defer();
// Create a directory within the watched directory.
await OS.File.makeDir(tmpDirPath);

Просмотреть файл

@ -29,7 +29,7 @@ add_task(async function test_watch_single_path_file_creation() {
// Instantiate and initialize the native watcher.
let watcher = makeWatcher();
let deferred = Promise.defer();
let deferred = PromiseUtils.defer();
let tmpFilePath = OS.Path.join(watchedDir, tempFileName);

Просмотреть файл

@ -28,7 +28,7 @@ add_task(async function test_watch_single_path_file_deletion() {
// Instantiate and initialize the native watcher.
let watcher = makeWatcher();
let deferred = Promise.defer();
let deferred = PromiseUtils.defer();
// Create a file within the directory to be watched. We do this
// before watching the directory so we do not get the creation notification.

Просмотреть файл

@ -29,7 +29,7 @@ add_task(async function test_watch_single_path_file_modification() {
// Instantiate and initialize the native watcher.
let watcher = makeWatcher();
let deferred = Promise.defer();
let deferred = PromiseUtils.defer();
// Create a file within the directory to be watched. We do this
// before watching the directory so we do not get the creation notification.

Просмотреть файл

@ -40,7 +40,7 @@ add_task(async function test_fill_notification_buffer() {
// Instantiate the native watcher.
let watcher = makeWatcher();
let deferred = Promise.defer();
let deferred = PromiseUtils.defer();
// Initialise the change callback.
let changeCallback = function(changed) {

Просмотреть файл

@ -39,9 +39,9 @@ add_task(async function test_watch_multi_paths() {
let watchedResources = 0;
let unwatchedResources = 0;
let deferredChanges = Promise.defer();
let deferredSuccesses = Promise.defer();
let deferredShutdown = Promise.defer();
let deferredChanges = PromiseUtils.defer();
let deferredSuccesses = PromiseUtils.defer();
let deferredShutdown = PromiseUtils.defer();
// Define the change callback function.
let changeCallback = function(changed) {

Просмотреть файл

@ -33,7 +33,7 @@ add_task(async function test_watch_recursively() {
// Instantiate and initialize the native watcher.
let watcher = makeWatcher();
let deferred = Promise.defer();
let deferred = PromiseUtils.defer();
let tmpFilePath = OS.Path.join(subdirectory, tempFileName);

Просмотреть файл

@ -1,5 +1,7 @@
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { Promise } = ChromeUtils.import("resource://gre/modules/Promise.jsm");
const { PromiseUtils } = ChromeUtils.import(
"resource://gre/modules/PromiseUtils.jsm"
);
add_task(function init() {
do_get_profile();
@ -15,7 +17,7 @@ add_task(async function system_shutdown() {
// Test that closed directories do not cause warnings
function testLeaksOf(resource, topic) {
return (async function() {
let deferred = Promise.defer();
let deferred = PromiseUtils.defer();
// Register observer
Services.prefs.setBoolPref("toolkit.asyncshutdown.testing", true);

Просмотреть файл

@ -1,7 +1,7 @@
ChromeUtils.defineModuleGetter(
this,
"Promise",
"resource://gre/modules/Promise.jsm"
"PromiseUtils",
"resource://gre/modules/PromiseUtils.jsm"
);
/* exported promiseTabLoadEvent, is_element_visible, is_element_hidden */
@ -19,7 +19,7 @@ ChromeUtils.defineModuleGetter(
* @rejects if a valid load event is not received within a meaningful interval
*/
function promiseTabLoadEvent(tab, url) {
let deferred = Promise.defer();
let deferred = PromiseUtils.defer();
info("Wait tab event: load");
function handle(loadedUrl) {

Просмотреть файл

@ -5,7 +5,9 @@
var testnum = 0;
var dbConnection; // used for deleted table tests
const { Promise } = ChromeUtils.import("resource://gre/modules/Promise.jsm");
const { PromiseUtils } = ChromeUtils.import(
"resource://gre/modules/PromiseUtils.jsm"
);
function countDeletedEntries(expected) {
return new Promise((resolve, reject) => {
@ -108,7 +110,7 @@ add_task(async function() {
dbFile.append("formhistory.sqlite");
dbConnection = Services.storage.openUnsharedDatabase(dbFile);
let deferred = Promise.defer();
let deferred = PromiseUtils.defer();
let stmt = dbConnection.createAsyncStatement(
"DELETE FROM moz_deleted_formhistory"
@ -140,7 +142,7 @@ add_task(async function() {
// Cannot use promiseCountEntries when name and value are null
// because it treats null values as not set
// and here a search should be done explicity for null.
deferred = Promise.defer();
deferred = PromiseUtils.defer();
await FormHistory.count(
{ fieldname: null, value: null },
{

Просмотреть файл

@ -4,7 +4,7 @@
"use strict";
XPCOMUtils.defineLazyModuleGetters(this, {
Promise: "resource://gre/modules/Promise.jsm",
PromiseUtils: "resource://gre/modules/PromiseUtils.jsm",
SearchEngineSelector: "resource://gre/modules/SearchEngineSelector.jsm",
});
@ -104,7 +104,7 @@ add_task(async function test_selector_basic_get() {
add_task(async function test_selector_get_reentry() {
const listenerSpy = sinon.spy();
const engineSelector = new SearchEngineSelector(listenerSpy);
let promise = Promise.defer();
let promise = PromiseUtils.defer();
getStub.resetHistory();
getStub.onFirstCall().returns(promise.promise);
delete engineSelector._configuration;

Просмотреть файл

@ -14,7 +14,9 @@ function setup_crash() {
const { Services } = ChromeUtils.import(
"resource://gre/modules/Services.jsm"
);
const { Promise } = ChromeUtils.import("resource://gre/modules/Promise.jsm");
const { PromiseUtils } = ChromeUtils.import(
"resource://gre/modules/PromiseUtils.jsm"
);
Services.prefs.setBoolPref("toolkit.asyncshutdown.testing", true);
Services.prefs.setIntPref("toolkit.asyncshutdown.crash_timeout", 10);
@ -23,7 +25,7 @@ function setup_crash() {
let phase = AsyncShutdown._getPhase(TOPIC);
phase.addBlocker("A blocker that is never satisfied", function() {
dump("Installing blocker\n");
let deferred = Promise.defer();
let deferred = PromiseUtils.defer();
return deferred.promise;
});
@ -50,14 +52,16 @@ function setup_osfile_crash_noerror() {
"resource://gre/modules/Services.jsm"
);
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
const { Promise } = ChromeUtils.import("resource://gre/modules/Promise.jsm");
const { PromiseUtils } = ChromeUtils.import(
"resource://gre/modules/PromiseUtils.jsm"
);
Services.prefs.setIntPref("toolkit.asyncshutdown.crash_timeout", 1);
Services.prefs.setBoolPref("toolkit.osfile.native", false);
OS.File.profileBeforeChange.addBlocker(
"Adding a blocker that will never be resolved",
() => Promise.defer().promise
() => PromiseUtils.defer().promise
);
OS.File.getCurrentDirectory();
@ -88,14 +92,16 @@ function setup_osfile_crash_exn() {
"resource://gre/modules/Services.jsm"
);
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
const { Promise } = ChromeUtils.import("resource://gre/modules/Promise.jsm");
const { PromiseUtils } = ChromeUtils.import(
"resource://gre/modules/PromiseUtils.jsm"
);
Services.prefs.setIntPref("toolkit.asyncshutdown.crash_timeout", 1);
Services.prefs.setBoolPref("toolkit.osfile.native", false);
OS.File.profileBeforeChange.addBlocker(
"Adding a blocker that will never be resolved",
() => Promise.defer().promise
() => PromiseUtils.defer().promise
);
OS.File.read("I do not exist");

Просмотреть файл

@ -1,997 +0,0 @@
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
/**
* This implementation file is imported by the Promise.jsm module, and as a
* special case by the debugger server. To support chrome debugging, the
* debugger server needs to have all its code in one global, so it must use
* loadSubScript directly.
*
* In the general case, this script should be used by importing Promise.jsm:
*
* Components.utils.import("resource://gre/modules/Promise.jsm");
*
* More documentation can be found in the Promise.jsm module.
*/
// Globals
// Obtain an instance of Cu. How this instance is obtained depends on how this
// file is loaded.
//
// This file can be loaded in three different ways:
// 1. As a CommonJS module, by Loader.jsm, on the main thread.
// 2. As a CommonJS module, by worker-loader.js, on a worker thread.
// 3. As a subscript, by Promise.jsm, on the main thread.
//
// If require is defined, the file is loaded as a CommonJS module. Components
// will not be defined in that case, but we can obtain an instance of Cu from
// the chrome module. Otherwise, this file is loaded as a subscript, and we can
// obtain an instance of Cu from Components directly.
//
// If the file is loaded as a CommonJS module on a worker thread, the instance
// of Cu obtained from the chrome module will be null. The reason for this is
// that Components is not defined in worker threads, so no instance of Cu can
// be obtained.
// As this can be loaded in several ways, allow require and module to be defined.
/* global module:false require:false */
// This is allowed in workers.
/* global setImmediate:false */
/* eslint-disable mozilla/no-define-cc-etc */
/* eslint-disable mozilla/use-cc-etc */
var Cu = this.require ? require("chrome").Cu : Components.utils;
var Cc = this.require ? require("chrome").Cc : Components.classes;
var Ci = this.require ? require("chrome").Ci : Components.interfaces;
/* eslint-enable mozilla/use-cc-etc */
/* eslint-enable mozilla/no-define-cc-etc */
// If we can access Components, then we use it to capture an async
// parent stack trace; see scheduleWalkerLoop. However, as it might
// not be available (see above), users of this must check it first.
var Components_ = this.require ? require("chrome").components : Components;
// If Cu is defined, use it to lazily define the FinalizationWitnessService.
if (Cu) {
// If we're in a devtools module environment, ChromeUtils won't exist.
/* eslint "mozilla/use-chromeutils-import": ["error", {allowCu: true}] */
Cu.import("resource://gre/modules/Services.jsm", this);
Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);
XPCOMUtils.defineLazyServiceGetter(
this,
"FinalizationWitnessService",
"@mozilla.org/toolkit/finalizationwitness;1",
"nsIFinalizationWitnessService"
);
}
const STATUS_PENDING = 0;
const STATUS_RESOLVED = 1;
const STATUS_REJECTED = 2;
// This N_INTERNALS name allow internal properties of the Promise to be
// accessed only by this module, while still being visible on the object
// manually when using a debugger. This doesn't strictly guarantee that the
// properties are inaccessible by other code, but provide enough protection to
// avoid using them by mistake.
const salt = Math.floor(Math.random() * 100);
const N_INTERNALS = "{private:internals:" + salt + "}";
// We use DOM Promise for scheduling the walker loop.
const DOMPromise = Cu ? Promise : null;
// Warn-upon-finalization mechanism
//
// One of the difficult problems with promises is locating uncaught
// rejections. We adopt the following strategy: if a promise is rejected
// at the time of its garbage-collection *and* if the promise is at the
// end of a promise chain (i.e. |thatPromise.then| has never been
// called), then we print a warning.
//
// let deferred = Promise.defer();
// let p = deferred.promise.then();
// deferred.reject(new Error("I am un uncaught error"));
// deferred = null;
// p = null;
//
// In this snippet, since |deferred.promise| is not the last in the
// chain, no error will be reported for that promise. However, since
// |p| is the last promise in the chain, the error will be reported
// for |p|.
//
// Note that this may, in some cases, cause an error to be reported more
// than once. For instance, consider:
//
// let deferred = Promise.defer();
// let p1 = deferred.promise.then();
// let p2 = deferred.promise.then();
// deferred.reject(new Error("I am an uncaught error"));
// p1 = p2 = deferred = null;
//
// In this snippet, the error is reported both by p1 and by p2.
//
var PendingErrors = {
// An internal counter, used to generate unique id.
_counter: 0,
// Functions registered to be notified when a pending error
// is reported as uncaught.
_observers: new Set(),
_map: new Map(),
/**
* Initialize PendingErrors
*/
init() {
Services.obs.addObserver(function observe(aSubject, aTopic, aValue) {
PendingErrors.report(aValue);
}, "promise-finalization-witness");
},
/**
* Register an error as tracked.
*
* @return The unique identifier of the error.
*/
register(error) {
let id = "pending-error-" + this._counter++;
//
// At this stage, ideally, we would like to store the error itself
// and delay any treatment until we are certain that we will need
// to report that error. However, in the (unlikely but possible)
// case the error holds a reference to the promise itself, doing so
// would prevent the promise from being garbabe-collected, which
// would both cause a memory leak and ensure that we cannot report
// the uncaught error.
//
// To avoid this situation, we rather extract relevant data from
// the error and further normalize it to strings.
//
let value = {
date: new Date(),
message: "" + error,
fileName: null,
stack: null,
lineNumber: null,
};
try {
// Defend against non-enumerable values
if (error && error instanceof Ci.nsIException) {
// nsIException does things a little differently.
try {
// For starters |.toString()| does not only contain the message, but
// also the top stack frame, and we don't really want that.
value.message = error.message;
} catch (ex) {
// Ignore field
}
try {
// All lowercase filename. ;)
value.fileName = error.filename;
} catch (ex) {
// Ignore field
}
try {
value.lineNumber = error.lineNumber;
} catch (ex) {
// Ignore field
}
} else if (typeof error == "object" && error) {
for (let k of ["fileName", "stack", "lineNumber"]) {
try {
// Defend against fallible getters and string conversions
let v = error[k];
value[k] = v ? "" + v : null;
} catch (ex) {
// Ignore field
}
}
}
if (!value.stack) {
// |error| is not an Error (or Error-alike). Try to figure out the stack.
let stack = null;
if (
error &&
error.location &&
error.location instanceof Ci.nsIStackFrame
) {
// nsIException has full stack frames in the |.location| member.
stack = error.location;
} else {
// Components.stack to the rescue!
stack = Components_.stack;
// Remove those top frames that refer to Promise.jsm.
while (stack) {
if (!stack.filename.endsWith("/Promise.jsm")) {
break;
}
stack = stack.caller;
}
}
if (stack) {
let frames = [];
while (stack) {
frames.push(stack);
stack = stack.caller;
}
value.stack = frames.join("\n");
}
}
} catch (ex) {
// Ignore value
}
this._map.set(id, value);
return id;
},
/**
* Notify all observers that a pending error is now uncaught.
*
* @param id The identifier of the pending error, as returned by
* |register|.
*/
report(id) {
let value = this._map.get(id);
if (!value) {
return; // The error has already been reported
}
this._map.delete(id);
for (let obs of this._observers.values()) {
obs(value);
}
},
/**
* Mark all pending errors are uncaught, notify the observers.
*/
flush() {
// Since we are going to modify the map while walking it,
// let's copying the keys first.
for (let key of Array.from(this._map.keys())) {
this.report(key);
}
},
/**
* Stop tracking an error, as this error has been caught,
* eventually.
*/
unregister(id) {
this._map.delete(id);
},
/**
* Add an observer notified when an error is reported as uncaught.
*
* @param {function} observer A function notified when an error is
* reported as uncaught. Its arguments are
* {message, date, fileName, stack, lineNumber}
* All arguments are optional.
*/
addObserver(observer) {
this._observers.add(observer);
},
/**
* Remove an observer added with addObserver
*/
removeObserver(observer) {
this._observers.delete(observer);
},
/**
* Remove all the observers added with addObserver
*/
removeAllObservers() {
this._observers.clear();
},
};
// Initialize the warn-upon-finalization mechanism if and only if Cu is defined.
// Otherwise, FinalizationWitnessService won't be defined (see above).
if (Cu) {
PendingErrors.init();
}
// Default mechanism for displaying errors
PendingErrors.addObserver(function(details) {
const generalDescription =
"A promise chain failed to handle a rejection." +
" Did you forget to '.catch', or did you forget to 'return'?\nSee" +
" https://developer.mozilla.org/Mozilla/JavaScript_code_modules/Promise.jsm/Promise\n\n";
let error = Cc["@mozilla.org/scripterror;1"].createInstance(
Ci.nsIScriptError
);
if (!error || !Services.console) {
// Too late during shutdown to use the nsIConsole
dump("*************************\n");
dump(generalDescription);
dump("On: " + details.date + "\n");
dump("Full message: " + details.message + "\n");
dump("Full stack: " + (details.stack || "not available") + "\n");
dump("*************************\n");
return;
}
let message = details.message;
if (details.stack) {
message += "\nFull Stack: " + details.stack;
}
error.init(
/* message*/ generalDescription +
"Date: " +
details.date +
"\nFull Message: " +
message,
/* sourceName*/ details.fileName,
/* sourceLine*/ details.lineNumber ? "" + details.lineNumber : 0,
/* lineNumber*/ details.lineNumber || 0,
/* columnNumber*/ 0,
/* flags*/ Ci.nsIScriptError.errorFlag,
/* category*/ "chrome javascript"
);
Services.console.logMessage(error);
});
// Additional warnings for developers
//
// The following error types are considered programmer errors, which should be
// reported (possibly redundantly) so as to let programmers fix their code.
const ERRORS_TO_REPORT = [
"EvalError",
"RangeError",
"ReferenceError",
"TypeError",
];
// Promise
/**
* The Promise constructor. Creates a new promise given an executor callback.
* The executor callback is called with the resolve and reject handlers.
*
* @param aExecutor
* The callback that will be called with resolve and reject.
*/
this.Promise = function Promise(aExecutor) {
if (typeof aExecutor != "function") {
throw new TypeError("Promise constructor must be called with an executor.");
}
/*
* Object holding all of our internal values we associate with the promise.
*/
Object.defineProperty(this, N_INTERNALS, {
value: {
/*
* Internal status of the promise. This can be equal to STATUS_PENDING,
* STATUS_RESOLVED, or STATUS_REJECTED.
*/
status: STATUS_PENDING,
/*
* When the status property is STATUS_RESOLVED, this contains the final
* resolution value, that cannot be a promise, because resolving with a
* promise will cause its state to be eventually propagated instead. When the
* status property is STATUS_REJECTED, this contains the final rejection
* reason, that could be a promise, even if this is uncommon.
*/
value: undefined,
/*
* Array of Handler objects registered by the "then" method, and not processed
* yet. Handlers are removed when the promise is resolved or rejected.
*/
handlers: [],
/**
* When the status property is STATUS_REJECTED and until there is
* a rejection callback, this contains an array
* - {string} id An id for use with |PendingErrors|;
* - {FinalizationWitness} witness A witness broadcasting |id| on
* notification "promise-finalization-witness".
*/
witness: undefined,
},
});
Object.seal(this);
let resolve = PromiseWalker.completePromise.bind(
PromiseWalker,
this,
STATUS_RESOLVED
);
let reject = PromiseWalker.completePromise.bind(
PromiseWalker,
this,
STATUS_REJECTED
);
try {
aExecutor(resolve, reject);
} catch (ex) {
reject(ex);
}
};
/**
* Calls one of the provided functions as soon as this promise is either
* resolved or rejected. A new promise is returned, whose state evolves
* depending on this promise and the provided callback functions.
*
* The appropriate callback is always invoked after this method returns, even
* if this promise is already resolved or rejected. You can also call the
* "then" method multiple times on the same promise, and the callbacks will be
* invoked in the same order as they were registered.
*
* @param aOnResolve
* If the promise is resolved, this function is invoked with the
* resolution value of the promise as its only argument, and the
* outcome of the function determines the state of the new promise
* returned by the "then" method. In case this parameter is not a
* function (usually "null"), the new promise returned by the "then"
* method is resolved with the same value as the original promise.
*
* @param aOnReject
* If the promise is rejected, this function is invoked with the
* rejection reason of the promise as its only argument, and the
* outcome of the function determines the state of the new promise
* returned by the "then" method. In case this parameter is not a
* function (usually left "undefined"), the new promise returned by the
* "then" method is rejected with the same reason as the original
* promise.
*
* @return A new promise that is initially pending, then assumes a state that
* depends on the outcome of the invoked callback function:
* - If the callback returns a value that is not a promise, including
* "undefined", the new promise is resolved with this resolution
* value, even if the original promise was rejected.
* - If the callback throws an exception, the new promise is rejected
* with the exception as the rejection reason, even if the original
* promise was resolved.
* - If the callback returns a promise, the new promise will
* eventually assume the same state as the returned promise.
*
* @note If the aOnResolve callback throws an exception, the aOnReject
* callback is not invoked. You can register a rejection callback on
* the returned promise instead, to process any exception occurred in
* either of the callbacks registered on this promise.
*/
Promise.prototype.then = function(aOnResolve, aOnReject) {
let handler = new Handler(this, aOnResolve, aOnReject);
this[N_INTERNALS].handlers.push(handler);
// Ensure the handler is scheduled for processing if this promise is already
// resolved or rejected.
if (this[N_INTERNALS].status != STATUS_PENDING) {
// This promise is not the last in the chain anymore. Remove any watchdog.
if (this[N_INTERNALS].witness != null) {
let [id, witness] = this[N_INTERNALS].witness;
this[N_INTERNALS].witness = null;
witness.forget();
PendingErrors.unregister(id);
}
PromiseWalker.schedulePromise(this);
}
return handler.nextPromise;
};
/**
* Invokes `promise.then` with undefined for the resolve handler and a given
* reject handler.
*
* @param aOnReject
* The rejection handler.
*
* @return A new pending promise returned.
*
* @see Promise.prototype.then
*/
Promise.prototype.catch = function(aOnReject) {
return this.then(undefined, aOnReject);
};
/**
* Creates a new pending promise and provides methods to resolve or reject it.
*
* @return A new object, containing the new promise in the "promise" property,
* and the methods to change its state in the "resolve" and "reject"
* properties. See the Deferred documentation for details.
*/
Promise.defer = function() {
return new Deferred();
};
/**
* Creates a new promise resolved with the specified value, or propagates the
* state of an existing promise.
*
* @param aValue
* If this value is not a promise, including "undefined", it becomes
* the resolution value of the returned promise. If this value is a
* promise, then the returned promise will eventually assume the same
* state as the provided promise.
*
* @return A promise that can be pending, resolved, or rejected.
*/
Promise.resolve = function(aValue) {
if (aValue instanceof Promise) {
return aValue;
}
return new Promise(aResolve => aResolve(aValue));
};
/**
* Creates a new promise rejected with the specified reason.
*
* @param aReason
* The rejection reason for the returned promise. Although the reason
* can be "undefined", it is generally an Error object, like in
* exception handling.
*
* @return A rejected promise.
*
* @note The aReason argument should not be a promise. Using a rejected
* promise for the value of aReason would make the rejection reason
* equal to the rejected promise itself, and not its rejection reason.
*/
Promise.reject = function(aReason) {
return new Promise((_, aReject) => aReject(aReason));
};
/**
* Returns a promise that is resolved or rejected when all values are
* resolved or any is rejected.
*
* @param aValues
* Iterable of promises that may be pending, resolved, or rejected. When
* all are resolved or any is rejected, the returned promise will be
* resolved or rejected as well.
*
* @return A new promise that is fulfilled when all values are resolved or
* that is rejected when any of the values are rejected. Its
* resolution value will be an array of all resolved values in the
* given order, or undefined if aValues is an empty array. The reject
* reason will be forwarded from the first promise in the list of
* given promises to be rejected.
*/
Promise.all = function(aValues) {
if (aValues == null || typeof aValues[Symbol.iterator] != "function") {
throw new Error("Promise.all() expects an iterable.");
}
return new Promise((resolve, reject) => {
let values = Array.isArray(aValues) ? aValues : [...aValues];
let countdown = values.length;
let resolutionValues = new Array(countdown);
if (!countdown) {
resolve(resolutionValues);
return;
}
function checkForCompletion(aValue, aIndex) {
resolutionValues[aIndex] = aValue;
if (--countdown === 0) {
resolve(resolutionValues);
}
}
for (let i = 0; i < values.length; i++) {
let index = i;
let value = values[i];
let resolver = val => checkForCompletion(val, index);
if (value && typeof value.then == "function") {
value.then(resolver, reject);
} else {
// Given value is not a promise, forward it as a resolution value.
resolver(value);
}
}
});
};
/**
* Returns a promise that is resolved or rejected when the first value is
* resolved or rejected, taking on the value or reason of that promise.
*
* @param aValues
* Iterable of values or promises that may be pending, resolved, or
* rejected. When any is resolved or rejected, the returned promise will
* be resolved or rejected as to the given value or reason.
*
* @return A new promise that is fulfilled when any values are resolved or
* rejected. Its resolution value will be forwarded from the resolution
* value or rejection reason.
*/
Promise.race = function(aValues) {
if (aValues == null || typeof aValues[Symbol.iterator] != "function") {
throw new Error("Promise.race() expects an iterable.");
}
return new Promise((resolve, reject) => {
for (let value of aValues) {
Promise.resolve(value).then(resolve, reject);
}
});
};
Promise.Debugging = {
/**
* Add an observer notified when an error is reported as uncaught.
*
* @param {function} observer A function notified when an error is
* reported as uncaught. Its arguments are
* {message, date, fileName, stack, lineNumber}
* All arguments are optional.
*/
addUncaughtErrorObserver(observer) {
PendingErrors.addObserver(observer);
},
/**
* Remove an observer added with addUncaughtErrorObserver
*
* @param {function} An observer registered with
* addUncaughtErrorObserver.
*/
removeUncaughtErrorObserver(observer) {
PendingErrors.removeObserver(observer);
},
/**
* Remove all the observers added with addUncaughtErrorObserver
*/
clearUncaughtErrorObservers() {
PendingErrors.removeAllObservers();
},
/**
* Force all pending errors to be reported immediately as uncaught.
* Note that this may cause some false positives.
*/
flushUncaughtErrors() {
PendingErrors.flush();
},
};
Object.freeze(Promise.Debugging);
Object.freeze(Promise);
// If module is defined, this file is loaded as a CommonJS module. Make sure
// Promise is exported in that case.
if (this.module) {
module.exports = Promise;
}
// PromiseWalker
/**
* This singleton object invokes the handlers registered on resolved and
* rejected promises, ensuring that processing is not recursive and is done in
* the same order as registration occurred on each promise.
*
* There is no guarantee on the order of execution of handlers registered on
* different promises.
*/
this.PromiseWalker = {
/**
* Singleton array of all the unprocessed handlers currently registered on
* resolved or rejected promises. Handlers are removed from the array as soon
* as they are processed.
*/
handlers: [],
/**
* Called when a promise needs to change state to be resolved or rejected.
*
* @param aPromise
* Promise that needs to change state. If this is already resolved or
* rejected, this method has no effect.
* @param aStatus
* New desired status, either STATUS_RESOLVED or STATUS_REJECTED.
* @param aValue
* Associated resolution value or rejection reason.
*/
completePromise(aPromise, aStatus, aValue) {
// Do nothing if the promise is already resolved or rejected.
if (aPromise[N_INTERNALS].status != STATUS_PENDING) {
return;
}
// Resolving with another promise will cause this promise to eventually
// assume the state of the provided promise.
if (
aStatus == STATUS_RESOLVED &&
aValue &&
typeof aValue.then == "function"
) {
aValue.then(
this.completePromise.bind(this, aPromise, STATUS_RESOLVED),
this.completePromise.bind(this, aPromise, STATUS_REJECTED)
);
return;
}
// Change the promise status and schedule our handlers for processing.
aPromise[N_INTERNALS].status = aStatus;
aPromise[N_INTERNALS].value = aValue;
if (aPromise[N_INTERNALS].handlers.length) {
this.schedulePromise(aPromise);
} else if (Cu && aStatus == STATUS_REJECTED) {
// This is a rejection and the promise is the last in the chain.
// For the time being we therefore have an uncaught error.
let id = PendingErrors.register(aValue);
let witness = FinalizationWitnessService.make(
"promise-finalization-witness",
id
);
aPromise[N_INTERNALS].witness = [id, witness];
}
},
/**
* Sets up the PromiseWalker loop to start on the next tick of the event loop
*/
scheduleWalkerLoop() {
this.walkerLoopScheduled = true;
// If this file is loaded on a worker thread, DOMPromise will not behave as
// expected: because native promises are not aware of nested event loops
// created by the debugger, their respective handlers will not be called
// until after leaving the nested event loop. The debugger server relies
// heavily on the use promises, so this could cause the debugger to hang.
//
// To work around this problem, any use of native promises in the debugger
// server should be avoided when it is running on a worker thread. Because
// it is still necessary to be able to schedule runnables on the event
// queue, the worker loader defines the function setImmediate as a
// per-module global for this purpose.
//
// If Cu is defined, this file is loaded on the main thread. Otherwise, it
// is loaded on the worker thread.
if (Cu) {
let stack = Components_ ? Components_.stack : null;
if (stack) {
DOMPromise.resolve().then(() => {
Cu.callFunctionWithAsyncStack(
this.walkerLoop.bind(this),
stack,
"Promise"
);
});
} else {
DOMPromise.resolve().then(() => this.walkerLoop());
}
} else {
setImmediate(this.walkerLoop);
}
},
/**
* Schedules the resolution or rejection handlers registered on the provided
* promise for processing.
*
* @param aPromise
* Resolved or rejected promise whose handlers should be processed. It
* is expected that this promise has at least one handler to process.
*/
schedulePromise(aPromise) {
// Migrate the handlers from the provided promise to the global list.
for (let handler of aPromise[N_INTERNALS].handlers) {
this.handlers.push(handler);
}
aPromise[N_INTERNALS].handlers.length = 0;
// Schedule the walker loop on the next tick of the event loop.
if (!this.walkerLoopScheduled) {
this.scheduleWalkerLoop();
}
},
/**
* Indicates whether the walker loop is currently scheduled for execution on
* the next tick of the event loop.
*/
walkerLoopScheduled: false,
/**
* Processes all the known handlers during this tick of the event loop. This
* eager processing is done to avoid unnecessarily exiting and re-entering the
* JavaScript context for each handler on a resolved or rejected promise.
*
* This function is called with "this" bound to the PromiseWalker object.
*/
walkerLoop() {
// If there is more than one handler waiting, reschedule the walker loop
// immediately. Otherwise, use walkerLoopScheduled to tell schedulePromise()
// to reschedule the loop if it adds more handlers to the queue. This makes
// this walker resilient to the case where one handler does not return, but
// starts a nested event loop. In that case, the newly scheduled walker will
// take over. In the common case, the newly scheduled walker will be invoked
// after this one has returned, with no actual handler to process. This
// small overhead is required to make nested event loops work correctly, but
// occurs at most once per resolution chain, thus having only a minor
// impact on overall performance.
if (this.handlers.length > 1) {
this.scheduleWalkerLoop();
} else {
this.walkerLoopScheduled = false;
}
// Process all the known handlers eagerly.
while (this.handlers.length) {
this.handlers.shift().process();
}
},
};
// Bind the function to the singleton once.
PromiseWalker.walkerLoop = PromiseWalker.walkerLoop.bind(PromiseWalker);
// Deferred
/**
* Returned by "Promise.defer" to provide a new promise along with methods to
* change its state.
*/
function Deferred() {
this.promise = new Promise((aResolve, aReject) => {
this.resolve = aResolve;
this.reject = aReject;
});
Object.freeze(this);
}
Deferred.prototype = {
/**
* A newly created promise, initially in the pending state.
*/
promise: null,
/**
* Resolves the associated promise with the specified value, or propagates the
* state of an existing promise. If the associated promise has already been
* resolved or rejected, this method does nothing.
*
* This function is bound to its associated promise when "Promise.defer" is
* called, and can be called with any value of "this".
*
* @param aValue
* If this value is not a promise, including "undefined", it becomes
* the resolution value of the associated promise. If this value is a
* promise, then the associated promise will eventually assume the same
* state as the provided promise.
*
* @note Calling this method with a pending promise as the aValue argument,
* and then calling it again with another value before the promise is
* resolved or rejected, has unspecified behavior and should be avoided.
*/
resolve: null,
/**
* Rejects the associated promise with the specified reason. If the promise
* has already been resolved or rejected, this method does nothing.
*
* This function is bound to its associated promise when "Promise.defer" is
* called, and can be called with any value of "this".
*
* @param aReason
* The rejection reason for the associated promise. Although the
* reason can be "undefined", it is generally an Error object, like in
* exception handling.
*
* @note The aReason argument should not generally be a promise. In fact,
* using a rejected promise for the value of aReason would make the
* rejection reason equal to the rejected promise itself, not to the
* rejection reason of the rejected promise.
*/
reject: null,
};
// Handler
/**
* Handler registered on a promise by the "then" function.
*/
function Handler(aThisPromise, aOnResolve, aOnReject) {
this.thisPromise = aThisPromise;
this.onResolve = aOnResolve;
this.onReject = aOnReject;
this.nextPromise = new Promise(() => {});
}
Handler.prototype = {
/**
* Promise on which the "then" method was called.
*/
thisPromise: null,
/**
* Unmodified resolution handler provided to the "then" method.
*/
onResolve: null,
/**
* Unmodified rejection handler provided to the "then" method.
*/
onReject: null,
/**
* New promise that will be returned by the "then" method.
*/
nextPromise: null,
/**
* Called after thisPromise is resolved or rejected, invokes the appropriate
* callback and propagates the result to nextPromise.
*/
process() {
// The state of this promise is propagated unless a handler is defined.
let nextStatus = this.thisPromise[N_INTERNALS].status;
let nextValue = this.thisPromise[N_INTERNALS].value;
try {
// If a handler is defined for either resolution or rejection, invoke it
// to determine the state of the next promise, that will be resolved with
// the returned value, that can also be another promise.
if (nextStatus == STATUS_RESOLVED) {
if (typeof this.onResolve == "function") {
nextValue = this.onResolve.call(undefined, nextValue);
}
} else if (typeof this.onReject == "function") {
nextValue = this.onReject.call(undefined, nextValue);
nextStatus = STATUS_RESOLVED;
}
} catch (ex) {
// An exception has occurred in the handler.
if (
ex &&
typeof ex == "object" &&
"name" in ex &&
ERRORS_TO_REPORT.includes(ex.name)
) {
// We suspect that the exception is a programmer error, so we now
// display it using dump(). Note that we do not use Cu.reportError as
// we assume that this is a programming error, so we do not want end
// users to see it. Also, if the programmer handles errors correctly,
// they will either treat the error or log them somewhere.
dump("*************************\n");
dump(
"A coding exception was thrown in a Promise " +
(nextStatus == STATUS_RESOLVED ? "resolution" : "rejection") +
" callback.\n"
);
dump(
"See https://developer.mozilla.org/Mozilla/JavaScript_code_modules/Promise.jsm/Promise\n\n"
);
dump("Full message: " + ex + "\n");
dump(
"Full stack: " + ("stack" in ex ? ex.stack : "not available") + "\n"
);
dump("*************************\n");
}
// Additionally, reject the next promise.
nextStatus = STATUS_REJECTED;
nextValue = ex;
}
// Propagate the newly determined state to the next promise.
PromiseWalker.completePromise(this.nextPromise, nextStatus, nextValue);
},
};

Просмотреть файл

@ -1,93 +0,0 @@
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
var EXPORTED_SYMBOLS = ["Promise"];
/**
* This module implements the "promise" construct, according to the
* "Promises/A+" proposal as known in April 2013, documented here:
*
* <http://promises-aplus.github.com/promises-spec/>
*
* A promise is an object representing a value that may not be available yet.
* Internally, a promise can be in one of three states:
*
* - Pending, when the final value is not available yet. This is the only state
* that may transition to one of the other two states.
*
* - Resolved, when and if the final value becomes available. A resolution
* value becomes permanently associated with the promise. This may be any
* value, including "undefined".
*
* - Rejected, if an error prevented the final value from being determined. A
* rejection reason becomes permanently associated with the promise. This may
* be any value, including "undefined", though it is generally an Error
* object, like in exception handling.
*
* A reference to an existing promise may be received by different means, for
* example as the return value of a call into an asynchronous API. In this
* case, the state of the promise can be observed but not directly controlled.
*
* To observe the state of a promise, its "then" method must be used. This
* method registers callback functions that are called as soon as the promise is
* either resolved or rejected. The method returns a new promise, that in turn
* is resolved or rejected depending on the state of the original promise and on
* the behavior of the callbacks. For example, unhandled exceptions in the
* callbacks cause the new promise to be rejected, even if the original promise
* is resolved. See the documentation of the "then" method for details.
*
* Promises may also be created using the "Promise.defer" function, the main
* entry point of this module. The function, along with the new promise,
* returns separate methods to change its state to be resolved or rejected.
* See the documentation of the "Deferred" prototype for details.
*
* -----------------------------------------------------------------------------
*
* Cu.import("resource://gre/modules/Promise.jsm");
*
* // This function creates and returns a new promise.
* function promiseValueAfterTimeout(aValue, aTimeout)
* {
* let deferred = Promise.defer();
*
* try {
* // An asynchronous operation will trigger the resolution of the promise.
* // In this example, we don't have a callback that triggers a rejection.
* do_timeout(aTimeout, function () {
* deferred.resolve(aValue);
* });
* } catch (ex) {
* // Generally, functions returning promises propagate exceptions through
* // the returned promise, though they may also choose to fail early.
* deferred.reject(ex);
* }
*
* // We don't return the deferred to the caller, but only the contained
* // promise, so that the caller cannot accidentally change its state.
* return deferred.promise;
* }
*
* // This code uses the promise returned be the function above.
* let promise = promiseValueAfterTimeout("Value", 1000);
*
* let newPromise = promise.then(function onResolve(aValue) {
* do_print("Resolved with this value: " + aValue);
* }, function onReject(aReason) {
* do_print("Rejected with this reason: " + aReason);
* });
*
* // Unexpected errors should always be reported at the end of a promise chain.
* newPromise.then(null, Components.utils.reportError);
*
* -----------------------------------------------------------------------------
*/
// eslint-disable-next-line mozilla/use-services
Cc["@mozilla.org/moz/jssubscript-loader;1"]
.getService(Ci.mozIJSSubScriptLoader)
.loadSubScript("resource://gre/modules/Promise-backend.js", this);

Просмотреть файл

@ -152,8 +152,8 @@ Example 4: A service with both internal and external dependencies
// Module FooService2
Components.utils.import("resource://gre/modules/AsyncShutdown.jsm", this);
Components.utils.import("resource://gre/modules/Promise.jsm", this);
let { AsyncShutdown } = Components.utils.import("resource://gre/modules/AsyncShutdown.jsm");
let { PromiseUtils } = Components.utils.import("resource://gre/modules/PromiseUtils.jsm");
this.exports = ["FooService2"];
@ -172,7 +172,7 @@ Example 4: A service with both internal and external dependencies
throw new Error("FooService2 is closed");
}
let deferred = Promise.defer();
let deferred = PromiseUtils.defer();
connections.client.addBlocker("FooService2: Waiting for connection " + name + " to close", deferred.promise);
// ...

Просмотреть файл

@ -198,8 +198,6 @@ EXTRA_JS_MODULES += [
"PrivateBrowsingUtils.jsm",
"ProcessType.jsm",
"ProfileAge.jsm",
"Promise-backend.js",
"Promise.jsm",
"PromiseUtils.jsm",
"Region.jsm",
"RemotePageAccessManager.jsm",

Просмотреть файл

@ -13,10 +13,6 @@ var EXPORTED_SYMBOLS = ["PromiseTestUtils"];
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { Assert } = ChromeUtils.import("resource://testing-common/Assert.jsm");
// Keep "JSMPromise" separate so "Promise" still refers to DOM Promises.
let JSMPromise = ChromeUtils.import("resource://gre/modules/Promise.jsm", {})
.Promise;
var PromiseTestUtils = {
/**
* Array of objects containing the details of the Promise rejections that are
@ -65,12 +61,6 @@ var PromiseTestUtils = {
PromiseDebugging.addUncaughtRejectionObserver(this);
// Promise.jsm rejections are only reported to this observer when requested,
// so we don't have to store a key to remove them when consumed.
JSMPromise.Debugging.addUncaughtErrorObserver(rejection =>
this._rejections.push(rejection)
);
this._initialized = true;
},
_initialized: false,
@ -84,7 +74,6 @@ var PromiseTestUtils = {
}
PromiseDebugging.removeUncaughtRejectionObserver(this);
JSMPromise.Debugging.clearUncaughtErrorObservers();
this._initialized = false;
},
@ -256,9 +245,6 @@ var PromiseTestUtils = {
* This is called by the test suite at the end of each test function.
*/
assertNoUncaughtRejections() {
// Ask Promise.jsm to report all uncaught rejections to the observer now.
JSMPromise.Debugging.flushUncaughtErrors();
// If there is any uncaught rejection left at this point, the test fails.
while (this._rejections.length) {
let rejection = this._rejections.shift();

Просмотреть файл

@ -10,7 +10,7 @@ const kExtensionID = "searchignore@mozilla.com";
XPCOMUtils.defineLazyModuleGetters(this, {
IgnoreLists: "resource://gre/modules/IgnoreLists.jsm",
Promise: "resource://gre/modules/Promise.jsm",
PromiseUtils: "resource://gre/modules/PromiseUtils.jsm",
RemoteSettings: "resource://services-settings/remote-settings.js",
RemoteSettingsClient: "resource://services-settings/RemoteSettingsClient.jsm",
sinon: "resource://testing-common/Sinon.jsm",
@ -49,7 +49,7 @@ add_task(async function test_ignoreList_basic_get() {
});
add_task(async function test_ignoreList_reentry() {
let promise = Promise.defer();
let promise = PromiseUtils.defer();
getStub.resetHistory();
getStub.onFirstCall().returns(promise.promise);

Просмотреть файл

@ -419,8 +419,7 @@ add_task(async function format_errors() {
// lineNumber:columnNumber.
Assert.ok(str.includes(":1:11)"));
// Make sure that we use human-readable stack traces
// Check that the error doesn't contain any reference to "Promise.jsm" or "Task.jsm"
Assert.ok(!str.includes("Promise.jsm"));
// Check that the error doesn't contain any reference to "Task.jsm"
Assert.ok(!str.includes("Task.jsm"));
Assert.ok(str.includes("format_errors"));
}

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -2,7 +2,6 @@
const PROFILE_DIR = do_get_profile().path;
const { Promise } = ChromeUtils.import("resource://gre/modules/Promise.jsm");
const { PromiseUtils } = ChromeUtils.import(
"resource://gre/modules/PromiseUtils.jsm"
);
@ -464,7 +463,7 @@ add_task(async function test_execute_transaction_success() {
add_task(async function test_execute_transaction_rollback() {
let c = await getDummyDatabase("execute_transaction_rollback");
let deferred = Promise.defer();
let deferred = PromiseUtils.defer();
c.executeTransaction(async function transaction(conn) {
await conn.execute("INSERT INTO dirs (path) VALUES ('foo')");
@ -993,7 +992,7 @@ add_task(async function test_direct() {
let begin = db.createAsyncStatement("BEGIN DEFERRED TRANSACTION");
let end = db.createAsyncStatement("COMMIT TRANSACTION");
let deferred = Promise.defer();
let deferred = PromiseUtils.defer();
begin.executeAsync({
handleCompletion(reason) {
deferred.resolve();
@ -1003,7 +1002,7 @@ add_task(async function test_direct() {
statement.bindParameters(params);
deferred = Promise.defer();
deferred = PromiseUtils.defer();
print("Executing async.");
statement.executeAsync({
handleResult(resultSet) {},
@ -1024,7 +1023,7 @@ add_task(async function test_direct() {
await deferred.promise;
deferred = Promise.defer();
deferred = PromiseUtils.defer();
end.executeAsync({
handleCompletion(reason) {
deferred.resolve();
@ -1036,7 +1035,7 @@ add_task(async function test_direct() {
begin.finalize();
end.finalize();
deferred = Promise.defer();
deferred = PromiseUtils.defer();
db.asyncClose(function() {
deferred.resolve();
});
@ -1186,7 +1185,7 @@ add_task(async function test_warning_message_on_finalization() {
failTestsOnAutoClose(false);
let c = await getDummyDatabase("warning_message_on_finalization");
let identifier = c._connectionData._identifier;
let deferred = Promise.defer();
let deferred = PromiseUtils.defer();
let listener = {
observe(msg) {
@ -1214,7 +1213,7 @@ add_task(async function test_warning_message_on_finalization() {
add_task(async function test_error_message_on_unknown_finalization() {
failTestsOnAutoClose(false);
let deferred = Promise.defer();
let deferred = PromiseUtils.defer();
let listener = {
observe(msg) {

Просмотреть файл

@ -40,7 +40,6 @@ skip-if = toolkit == 'android'
skip-if = apple_silicon # bug 1729538
[test_PermissionsUtils.js]
[test_Preferences.js]
[test_Promise.js]
[test_PromiseUtils.js]
[test_propertyListsUtils.js]
skip-if = os != 'mac'

Просмотреть файл

@ -4,7 +4,9 @@
"use strict";
ChromeUtils.import("resource://gre/modules/Promise.jsm", this);
const { PromiseUtils } = ChromeUtils.import(
"resource://gre/modules/PromiseUtils.jsm"
);
const { AppConstants } = ChromeUtils.import(
"resource://gre/modules/AppConstants.jsm"
);
@ -370,7 +372,7 @@ add_task(async function testUpdateButton() {
let item = getAddonCard(win, addon.id);
gInstalledAddonId = "";
gInstallDeferred = Promise.defer();
gInstallDeferred = PromiseUtils.defer();
let loaded = waitForViewLoad(win);
item.querySelector("[action=expand]").click();