Bug 1323845: Part 3 - Auto-create intermediate API namespace objects when necessary. r?aswan
MozReview-Commit-ID: HA8WpynPOqp
--- a/toolkit/components/extensions/ExtensionCommon.jsm
+++ b/toolkit/components/extensions/ExtensionCommon.jsm
@@ -826,28 +826,32 @@ class CanOfAPIs {
findAPIPath(path) {
if (this.apiPaths.has(path)) {
return this.apiPaths.get(path);
}
let obj = this.root;
let modules = this.apiManager.modulePaths;
- for (let key of path.split(".")) {
+ let parts = path.split(".");
+ for (let [i, key] of parts.entries()) {
if (!obj) {
return;
}
modules = getChild(modules, key);
for (let name of modules.modules) {
if (!this.apis.has(name)) {
this.loadAPI(name);
}
}
+ if (!(key in obj) && i < parts.length - 1) {
+ obj[key] = {};
+ }
obj = obj[key];
}
this.apiPaths.set(path, obj);
return obj;
}
/**
@@ -862,28 +866,33 @@ class CanOfAPIs {
async asyncFindAPIPath(path) {
if (this.apiPaths.has(path)) {
return this.apiPaths.get(path);
}
let obj = this.root;
let modules = this.apiManager.modulePaths;
- for (let key of path.split(".")) {
+ let parts = path.split(".");
+ for (let [i, key] of parts.entries()) {
if (!obj) {
return;
}
modules = getChild(modules, key);
for (let name of modules.modules) {
if (!this.apis.has(name)) {
await this.asyncLoadAPI(name);
}
}
+ if (!(key in obj) && i < parts.length - 1) {
+ obj[key] = {};
+ }
+
if (typeof obj[key] === "function") {
obj = obj[key].bind(obj);
} else {
obj = obj[key];
}
}
this.apiPaths.set(path, obj);