Source code

Revision control

Copy as Markdown

Other Tools

Test Info:

/* Any copyright is dedicated to the Public Domain.
"use strict";
// Test that a lazy getter whose lambda throws can be retried.
const { loader } = ChromeUtils.importESModule(
"resource://devtools/shared/loader/Loader.sys.mjs"
);
add_task(function () {
const o = {};
let calls = 0;
loader.lazyGetter(o, "value", () => {
calls++;
if (calls === 1) {
throw new Error("first call fails");
}
return "value";
});
Assert.throws(() => o.value, /first call fails/);
Assert.ok("value" in o, "The getter is still defined after a failure");
Assert.equal(o.value, "value", "The second access calls the lambda again");
Assert.equal(o.value, "value", "The value is cached");
Assert.equal(calls, 2, "The lambda was called twice");
});
add_task(function test_recursive_access() {
const o = {};
let calls = 0;
let innerValue;
loader.lazyGetter(o, "value", () => {
calls++;
innerValue = o.value;
if (calls === 1) {
throw new Error("first call fails");
}
return "value";
});
Assert.throws(() => o.value, /first call fails/);
Assert.equal(innerValue, undefined, "The getter is not called recursively");
Assert.equal(calls, 1, "The lambda was called once");
Assert.equal(o.value, "value", "The second access calls the lambda again");
Assert.equal(innerValue, undefined, "The getter is not called recursively");
Assert.equal(calls, 2, "The lambda was called twice");
});