<commit_before>// Many times I can't use 'this' to refer to the owning var's context, so I'm setting 'this' as 'self', 
// I can use 'self' from within functions, timers and listeners easily and to bind those functions to it as well
this.self = this;

// moduleAid - Helper to load subscripts into the context of "this"
// load(aModule, delayed) - loads aModule onto the context of self
//	aModule - (string) can be either module name which loads resource://objPathString/modules/aModule.jsm or full module path
//	(optional) delayed - true loads module 500ms later in an asychronous process, false loads immediatelly synchronously, defaults to false
// unload(aModule) - unloads aModule from the context of self
//	see load()
// loadIf(aModule, anIf, delayed) - conditionally load or unload aModule
//	anIf - true calls load(aModule, delayed), false calls unload(aModule)
//	see load()
// loaded(aModule) - returns (int) with corresponding module index in modules[] if aModule has been loaded, returns (bool) false otherwise
//	see load()
// subscript modules are run in the context of self, all objects should be set using this.whateverObject so they can be deleted on unload, moduleAid optionally expects these:
//	moduleAid.VERSION - (string) module version
//	moduleAid.VARSLIST - (array) list with all the objects the module inserts into the object when loaded, for easy unloading
//	moduleAid.LOADMODULE - (function) to be executed on module loading
//	moduleAid.UNLOADMODULE - (function) to be executed on module unloading
this.moduleAid = {
	version: '2.0.1',
	modules: [],
	moduleVars: {},
	
	loadIf: function(aModule, anIf, delayed) {
		if(anIf) {
			this.load(aModule, delayed);
		} else {
			this.unload(aModule);
		}
	},
	
	load: function(aModule, delayed) {
		var path = this.preparePath(aModule);
		if(this.loaded(path) != false) {
			return false;
		}
		
		Services.scriptloader.loadSubScript(path, self);
		
		var module = {
			name: aModule,
			path: path,
			load: (this.LOADMODULE) ? this.LOADMODULE : null,
			unload: (this.UNLOADMODULE) ? this.UNLOADMODULE : null,
			vars: (this.VARSLIST) ? this.VARSLIST : null,
			version: (this.VERSION) ? this.VERSION : null,
			loaded: false
		};
		var moduleIndex = this.modules.push(module) -1;
		
		if(this.VARSLIST) {
			this.createVars(this.VARSLIST);
		}
		if(this.LOADMODULE) {
			if(!delayed) {
				this.LOADMODULE();
				this.modules[moduleIndex].loaded = true;
			} else {
				aSync(function() {
					moduleAid.modules[moduleIndex].load.call(self);
					moduleAid.modules[moduleIndex].loaded = true; 
				}, 500);
			}
		}
		else {
			this.modules[moduleIndex].loaded = true;
		}
		
		delete this.VARSLIST;
		delete this.LOADMODULE;
		delete this.UNLOADMODULE;
		delete this.VERSION;
		return true;
	},
	
	unload: function(aModule) {
		var path = this.preparePath(aModule);
		
		var i = this.loaded(aModule);
		if(i !== false) {
			if(this.modules[i].unload && this.modules[i].loaded) {
				this.modules[i].unload();
			}
			if(this.modules[i].vars) {
				for(var o = 0; o < this.modules[i].vars.length; o++) {
					this.deleteVar(this.modules[i].vars[o]);
				}
			}
			this.modules.splice(i, 1);
			return true;
		}
		
		return false;
	},
	
	clean: function() {
		while(moduleAid.modules.length > 1) {
			moduleAid.unload(moduleAid.modules[1].name);
		}
	},
	
	loaded: function(aModule) {
		for(var i = 0; i < this.modules.length; i++) {
			if(this.modules[i].path == aModule || this.modules[i].name == aModule) {
				return i;
			}
		}
		return false;
	},
	
	createVars: function(aList) {
		for(var i=0; i<aList.length; i++) {
			if(this.moduleVars[aList[i]]) {
				this.moduleVars[aList[i]]++;
			} else {
				this.moduleVars[aList[i]] = 1;
			}
		}
	},
	
	deleteVar: function(aVar) {
		if(this.moduleVars[aVar]) {
			this.moduleVars[aVar]--;
			if(this.moduleVars[aVar] == 0) {
				delete self[aVar];
				delete this.moduleVars[aVar];
			}
			return true;
		}
		return false;
	},
	
	preparePath: function(aModule) {
		if(aModule.indexOf("resource://") === 0) {
			return aModule;
		}<commit_msg>failure to load a module shouldn't prevent the loading of consequent modules<commit_after>// Many times I can't use 'this' to refer to the owning var's context, so I'm setting 'this' as 'self', 
// I can use 'self' from within functions, timers and listeners easily and to bind those functions to it as well
this.self = this;

// moduleAid - Helper to load subscripts into the context of "this"
// load(aModule, delayed) - loads aModule onto the context of self
//	aModule - (string) can be either module name which loads resource://objPathString/modules/aModule.jsm or full module path
//	(optional) delayed - true loads module 500ms later in an asychronous process, false loads immediatelly synchronously, defaults to false
// unload(aModule) - unloads aModule from the context of self
//	see load()
// loadIf(aModule, anIf, delayed) - conditionally load or unload aModule
//	anIf - true calls load(aModule, delayed), false calls unload(aModule)
//	see load()
// loaded(aModule) - returns (int) with corresponding module index in modules[] if aModule has been loaded, returns (bool) false otherwise
//	see load()
// subscript modules are run in the context of self, all objects should be set using this.whateverObject so they can be deleted on unload, moduleAid optionally expects these:
//	moduleAid.VERSION - (string) module version
//	moduleAid.VARSLIST - (array) list with all the objects the module inserts into the object when loaded, for easy unloading
//	moduleAid.LOADMODULE - (function) to be executed on module loading
//	moduleAid.UNLOADMODULE - (function) to be executed on module unloading
this.moduleAid = {
	version: '2.0.2',
	modules: [],
	moduleVars: {},
	
	loadIf: function(aModule, anIf, delayed) {
		if(anIf) {
			this.load(aModule, delayed);
		} else {
			this.unload(aModule);
		}
	},
	
	load: function(aModule, delayed) {
		var path = this.preparePath(aModule);
		if(!path || this.loaded(path) !== false) {
			return false;
		}
		
		try { Services.scriptloader.loadSubScript(path, self); }
		catch(ex) {
			Cu.reportError(ex);
			return false;
		}
		
		var module = {
			name: aModule,
			path: path,
			load: (this.LOADMODULE) ? this.LOADMODULE : null,
			unload: (this.UNLOADMODULE) ? this.UNLOADMODULE : null,
			vars: (this.VARSLIST) ? this.VARSLIST : null,
			version: (this.VERSION) ? this.VERSION : null,
			loaded: false,
			failed: false
		};
		var i = this.modules.push(module) -1;
		
		delete this.VARSLIST;
		delete this.LOADMODULE;
		delete this.UNLOADMODULE;
		delete this.VERSION;
		
		try { this.createVars(this.modules[i].vars); }
		catch(ex) {
			Cu.reportError(ex);
			this.unload(aModule, true, true);
			return false;
		}
		
		if(this.modules[i].load) {
			if(!delayed) {
				try { this.modules[i].load(); }
				catch(ex) {
					Cu.reportError(ex);
					this.unload(aModule, true);
					return false;
				}
				this.modules[i].loaded = true;
			} else {
				aSync(function() {
					try { moduleAid.modules[i].load(); }
					catch(ex) {
						Cu.reportError(ex);
						this.unload(aModule, true);
						return false;
					}	
					moduleAid.modules[i].loaded = true; 
				}, 500);
			}
		}
		else {
			this.modules[i].loaded = true;
		}
		
		return true;
	},
	
	unload: function(aModule, force, justVars) {
		var path = this.preparePath(aModule);
		if(!path) { return false; }
		
		var i = this.loaded(aModule);
		if(i !== false) {
			if(!justVars && this.modules[i].unload && (this.modules[i].loaded || force)) {
				try { this.modules[i].unload(); }
				catch(ex) {
					if(!force) { Cu.reportError(ex); }
					this.modules[i].failed = true;
					return false;
				}
			}
			
			try { this.deleteVars(this.modules[i].vars); }
			catch(ex) {
				if(!force) { Cu.reportError(ex); }
				this.modules[i].failed = true;
				return false;
			}
			
			this.modules.splice(i, 1);
			return true;
		}
		
		return false;
	},
	
	clean: function() {
		while(moduleAid.modules.length > 1) {
			moduleAid.unload(moduleAid.modules[1].name);
		}
	},
	
	loaded: function(aModule) {
		for(var i = 0; i < this.modules.length; i++) {
			if(this.modules[i].path == aModule || this.modules[i].name == aModule) {
				return i;
			}
		}
		return false;
	},
	
	createVars: function(aList) {
		if(!Array.isArray(aList)) { return; }
		
		for(var i=0; i<aList.length; i++) {
			if(this.moduleVars[aList[i]]) {
				this.moduleVars[aList[i]]++;
			} else {
				this.moduleVars[aList[i]] = 1;
			}
		}
	},
	
	deleteVars: function(aList) {
		if(!Array.isArray(aList)) { return; }
		
		for(var o = 0; o < aList.length; o++) {
			if(this.moduleVars[aList[o]]) {
				this.moduleVars[aList[o]]--;
				if(this.moduleVars[aList[o]] == 0) {
					delete self[aList[o]];
					delete this.moduleVars[aList[o]];
				}
			}
		}
	},
	
	preparePath: function(aModule) {
		if(typeof(aModule) != 'string') { return null; }
		if(aModule.indexOf("resource://") === 0) { return aModule; }
{'fraction_non_alphanumeric': 0.14202769093345244, 'fraction_numerical': 0.004242965609647164, 'mean_word_length': 3.30625, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5500988', 'n_tokens_mistral': 3540, 'n_tokens_neox': 3056, 'n_words': 796}