1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
// Copyright (c) 2009-2015 Kris Maglione <maglione.k@gmail.com>
//
// This work is licensed for reuse under an MIT license. Details are
// given in the LICENSE.txt file included with this file.
"use strict";
try {
defineModule("main", {
exports: ["ModuleBase"],
require: ["config", "overlay", "services", "util"]
});
var BASE = "resource://dactyl-content/";
var global = this;
/**
* @class ModuleBase
* The base class for all modules.
*/
var ModuleBase = Class("ModuleBase", {
/**
* @property {[string]} A list of module prerequisites which
* must be initialized before this module is loaded.
*/
requires: [],
toString: function () "[module " + this.constructor.className + "]"
});
var _id = 0;
var Modules = function Modules(window) {
/**
* @constructor Module
*
* Constructs a new ModuleBase class and makes arrangements for its
* initialization. Arguments marked as optional must be either
* entirely elided, or they must have the exact type specified.
* Loading semantics are as follows:
*
* - A module is guaranteed not to be initialized before any of its
* prerequisites as listed in its {@see ModuleBase#requires} member.
* - A module is considered initialized once it's been instantiated,
* its {@see Class#init} method has been called, and its
* instance has been installed into the top-level {@see modules}
* object.
* - Once the module has been initialized, its module-dependent
* initialization functions will be called as described hereafter.
* @param {string} name The module's name as it will appear in the
* top-level {@see modules} object.
* @param {ModuleBase} base The base class for this module.
* @optional
* @param {Object} prototype The prototype for instances of this
* object. The object itself is copied and not used as a prototype
* directly.
* @param {Object} classProperties The class properties for the new
* module constructor.
* @optional
* @param {Object} moduleInit The module initialization functions
* for the new module. Each function is called as soon as the
* named module has been initialized. The constructors are
* guaranteed to be called in the same order that the dependent
* modules were initialized.
* @optional
*
* @returns {function} The constructor for the resulting module.
*/
function Module(name, ...args) {
var base = ModuleBase;
if (callable(args[0]))
base = args.shift();
let [prototype, classProperties, moduleInit] = args;
prototype._metaInit_ = function () {
delete module.prototype._metaInit_;
Class.replaceProperty(modules, module.className, this);
};
const module = Class(name, base, prototype, classProperties);
module.INIT = moduleInit || {};
module.modules = modules;
module.prototype.INIT = module.INIT;
module.requires = prototype.requires || [];
Module.list.push(module);
Module.constructors[name] = module;
return module;
}
Module.list = [];
Module.constructors = {};
function newContext(proto, normal, name) {
if (normal)
return create(proto);
let sandbox = Components.utils.Sandbox(window, { sandboxPrototype: proto || modules,
sandboxName: name || ("Dactyl Sandbox " + ++_id),
wantXrays: true });
// Hack:
// sandbox.Object = jsmodules.Object;
sandbox.File = global.File;
sandbox.Math = global.Math;
sandbox.Set = global.Set;
return sandbox;
};
const BASES = [BASE, "resource://dactyl-local-content/"];
let proxyCache = {};
var proxy = new Proxy(window, {
get: function window_get(target, prop) {
// `in`, not `hasOwnProperty`, because we want to return
// unbound methods in `Object.prototype`
if (prop in proxyCache)
return proxyCache[prop];
let p = target[prop];
if (callable(p))
return proxyCache[prop] = p.bind(target);
return p;
},
set: function window_set(target, prop, val, receiver) {
if (receiver !== target)
Object.defineProperty(receiver, prop, {
value: val,
enumerable: true, configurable: true, writable: true
});
else
target[prop] = val;
return true;
}
});
var jsmodules = newContext(proxy, false, "Dactyl `jsmodules`");
jsmodules.NAME = "jsmodules";
const create = bind("create", jsmodules.Object);
const modules = update(create(jsmodules), {
Symbol: Symbol,
yes_i_know_i_should_not_report_errors_in_these_branches_thanks: [],
jsmodules: jsmodules,
Module: Module,
load: function load(script) {
for (let base of BASES) {
try {
JSMLoader.loadSubScript(base + script + ".js", modules, "UTF-8");
return;
}
catch (e) {
if (typeof e !== "string") {
util.dump("Trying: " + (base + script + ".js") + ":");
util.reportError(e);
}
}
}
try {
require(script, jsmodules);
}
catch (e) {
util.dump("Loading script " + script + ":");
util.reportError(e);
}
},
newContext: newContext,
get ownPropertyValues() {
return Ary.compact(
Object.getOwnPropertyNames(this)
.map(name => Object.getOwnPropertyDescriptor(this, name).value));
},
get moduleList() {
return this.ownPropertyValues.filter(mod => (mod instanceof this.ModuleBase || mod.isLocalModule));
}
});
modules.plugins = create(modules);
modules.modules = modules;
return modules;
};
config.loadStyles();
overlay.overlayWindow(Object.keys(config.overlays),
function _overlay(window) ({
ready: function onInit(document) {
const modules = Modules(window);
modules.moduleManager = this;
this.modules = modules;
this.jsmodules = modules.jsmodules;
window.dactyl = { modules: modules };
defineModule.time("load", null, function _load() {
config.modules.global
.forEach(function (name) {
if (!isArray(name))
defineModule.time("load", name, require, null, name, modules.jsmodules);
else
lazyRequire(name[0], name.slice(1), modules.jsmodules);
});
config.modules.window
.forEach(name => { defineModule.time("load", name, modules.load, modules, name); });
}, this);
},
load: function onLoad(document) {
let self = this;
var { modules } = this.modules;
delete window.dactyl;
this.startTime = Date.now();
this.deferredInit = { load: {} };
this.seen = new RealSet;
this.loaded = new RealSet;
modules.loaded = this.loaded;
this.modules = modules;
this.scanModules();
this.initDependencies("init");
modules.config.scripts.forEach(modules.load);
this.scanModules();
defineModule.modules.forEach(function defModule({ lazyInit, constructor: { className } }) {
if (!lazyInit) {
Class.replaceProperty(modules, className, modules[className]);
this.initDependencies(className);
}
else
modules.__defineGetter__(className, () => {
let module = modules.jsmodules[className];
Class.replaceProperty(modules, className, module);
if (module.reallyInit)
module.reallyInit(); // :(
if (!module.lazyDepends)
self.initDependencies(className);
return module;
});
}, this);
},
cleanup: function cleanup(window) {
overlay.windows.delete(window);
JSMLoader.atexit(() => {
Cu.nukeSandbox(this.jsmodules);
});
},
unload: function unload(window) {
for (let mod of this.modules.moduleList.reverse()) {
mod.stale = true;
if ("destroy" in mod)
util.trapErrors("destroy", mod);
}
},
visible: function visible(window) {
// Module.list.forEach(load);
this.initDependencies("load");
this.modules.times = update({}, defineModule.times);
defineModule.loadLog.push("Loaded in " + (Date.now() - this.startTime) + "ms");
overlay.windows.add(window);
},
loadModule: function loadModule(module, prereq, frame) {
let { loaded, seen } = this;
let { Module, modules } = this.modules;
if (isString(module)) {
if (!Module.constructors.hasOwnProperty(module))
modules.load(module);
module = Module.constructors[module];
}
try {
if (loaded.has(module.className))
return;
if (seen.add(module.className))
throw Error("Module dependency loop.");
for (let dep of module.requires)
this.loadModule(Module.constructors[dep], module.className);
defineModule.loadLog.push(
"Load" + (isString(prereq) ? " " + prereq + " dependency: " : ": ")
+ module.className);
if (frame && frame.filename)
defineModule.loadLog.push(" from: " + util.fixURI(frame.filename) + ":" + frame.lineNumber);
let obj = defineModule.time(module.className, "init", module);
Class.replaceProperty(modules, module.className, obj);
loaded.add(module.className);
if (loaded.has("dactyl") && obj.signals)
modules.dactyl.registerObservers(obj);
if (!module.lazyDepends)
this.initDependencies(module.className);
}
catch (e) {
util.dump("Loading " + (module && module.className) + ":");
util.reportError(e);
}
return modules[module.className];
},
deferInit: function deferInit(name, INIT, mod) {
let { modules } = this.modules;
let init = this.deferredInit[name] || {};
this.deferredInit[name] = init;
let className = mod.className || mod.constructor.className;
if (!hasOwnProperty(init, className)) {
init[className] = function callee() {
function finish() {
this.currentDependency = className;
defineModule.time(className, name, INIT[name], mod,
modules.dactyl, modules, window);
}
if (!callee.frobbed) {
callee.frobbed = true;
if (modules[name] instanceof Class)
modules[name].withSavedValues(["currentDependency"], finish);
else
finish.call({});
}
};
INIT[name].require = name => { init[name](); };
}
},
scanModules: function scanModules() {
let { Module, modules } = this.modules;
defineModule.modules.forEach(mod => {
let names = new RealSet(Object.keys(mod.INIT));
if ("init" in mod.INIT)
names.add("init");
for (let name of names)
this.deferInit(name, mod.INIT, mod);
});
Module.list.forEach(mod => {
if (!mod.frobbed) {
modules.__defineGetter__(mod.className, () => {
delete modules[mod.className];
return this.loadModule(mod.className, null, Components.stack.caller);
});
Object.keys(mod.prototype.INIT)
.forEach(name => { this.deferInit(name, mod.prototype.INIT, mod); });
}
mod.frobbed = true;
});
},
initDependencies: function initDependencies(name, parents) {
for (let [k, v] of iter(this.deferredInit[name] || {}))
if (!parents || ~parents.indexOf(k))
util.trapErrors(v);
}
}));
endModule();
} catch(e){ if (!e.stack) e = Error(e); dump(e.fileName+":"+e.lineNumber+": "+e+"\n" + e.stack); }
// vim: set fdm=marker sw=4 sts=4 ts=8 et ft=javascript:
|