kernel.js

来自「《JavaScript王者归来》examples.rar」· JavaScript 代码 · 共 960 行 · 第 1/2 页

JS
960
字号
			/**			 * Sets the library home			 */			this.setLibHome = function(arg) {				libHome = arg;			}			/**			 * Returns the library home			 */			this.getLibHome = function() {				return libHome;			}		};		/**		 * Sets the classloader		 */		this.setClassloader = function(arg) {			classloader = arg;		}		/**		 * Returns the classloader		 */		this.getClassloader = function() {			return classloader;		}		/**		 * Defines default console		 */		this.console = {			/**			 * Writes			 */			"write" : function(msg) {				window.alert(msg);			},			/**			 * Reads			 */			"read" : function(pmt) {				window.prompt(pmt);			}};		/**		 * Sets the console of JSVM		 */		this.setConsole = function(arg) {			this.console = arg;		}		/**		 * Defines default container		 */		var container = new function() {			/**			 * class container			 */			var classes = {};			/**			 * Puts the class into container			 */			this.putClass = function(name, entity) {				classes[name] = entity;			}			/**			 * Returns the class by specified name			 */			this.getClass = function(name) {				return classes[name];			}			/**			 * class code container			 */			var classCodes = {};			/**			 * Puts the class code into container			 */			this.putClassCode = function(name, code) {				classCodes[name] = code;			}			/**			 * Returns the class code by specified classname			 */			this.getClassCode = function(name) {				return classCodes[name];			}			/**			 * Clears all classes and class codes			 */			this.clear = function () {				classes = {};				classCodes = {};			}			/**			 * Returns a list of all loaded classes that			 *  were cached by this container.			 */			this.getClasses = function () {				var o = {}, p;				for (p in classes) {					o[p] = classes[p];				}				return o;			}			/**			 * Returns a list of all loaded class codees that			 *  were cached by this container.			 */			this.getClassCodes = function () {				var o = {}, p;				for (p in classCodes) {					o[p] = classCodes[p];				}				return o;			}		};		/**		 * Sets container for JSVM		 */		this.setContainer = function(arg) {			container = arg;		}		/**		 * Returns the current container of JSVM		 */		this.getContainer = function() {			return container;		}		/**		 * Defines default compiler for JSVM		 */		var compiler = new function() {			/**			 * Defines parser container			 */			var parsers = {"native" : {parse : function(s){return s;}}};			/**			 * Sets the parser of the specified name			 */			this.setParser = function(name, parser) {				parsers[name.toLowerCase()] = parser;			}			/**			 * Retrieves the parser of the specified name			 */			this.getParser = function(name) {				return parsers[name.toLowerCase()];			}			var regexp_lang = /^(\s*)#( *)(language)( *):( *)(\S+)(\s*)/;			/**			 * Compiles the source code			 */			this.compile = function (sourceCode) {				var code = sourceCode;				if (regexp_lang.test(code)) {					code = code.replace(regexp_lang, "");					var lang = RegExp.$6;					var parser = this.getParser(lang);					if (parser != null) {						try {							code = parser.parse(code);						} catch(ex) {							throw new Exception("kernel.js/JSVM.compiler.compile()"								+ " Parse failed: [" + sourceCode + "]");						}					} else {						throw new Exception("kernel.js/JSVM.compiler.compile()"							+ " Not found parser: '" + lang + "'.");					}				}				return code;			}		};		/**		 * Sets the compiler by the specified compiler		 */		this.setCompiler = function(arg) {			compiler = arg;		}		/**		 * Retrieves the compiler by the specified name		 */		this.getCompiler = function() {			return compiler;		}		/**		 * Defines the default engine.		 */		var engine = new function() {			var engine = this;			/**			 * Defines API for all js class scope			 */			/**			 * Imports and returns the class by specified name			 */			var _$import = function(name) {				return JSVM.loadClass(name);			}, $import = _$import;			/**			 * Defines the package by the specified name			 */			var _$package = function(name) {				engine.definePackage(name);			}, $package = _$package;			/**			 * Defines the executor for the current engine			 */			this.executor = new function() {				// Mask these variables				var jsre, JSVM, engine, typeOf, isPackage, isClass;				var ExecuteException = Class.create("ExecuteException"					, function () {						return this.getSuperclass().apply(this, arguments);					}, Exception);				/**				 * Executes the js-class code.				 */				this.execute = function(code) {					try {						(function(){eval(code)})();					} catch (ex) {						var flag = false;						var cause = ex.cause;						while (cause != null) {							if (cause instanceof ExecuteException) {								flag = true;								break;							}							cause = cause.cause;						}						throw new ExecuteException("kernel.js/executor.execute() fail. "							+ (flag ? "" : "[code: " + code + "]") , ex);					}				}			}			/**			 * Retrieves the type of the variable			 * by the specified name.			 */			var typeOf = function(name) {				try {					return eval("typeof " + name);				} catch (ex) {					return "undefined";				}			}			/**			 * Determines whether the type of the variable			 *  is Package by the specified name			 */			var isPackage = function(name) {				return (typeOf(name) == "object");				}			/**			 * Determines whether the type of the variable			 *  is Class by the specified name			 */			var isClass = function(name) {				return (typeOf(name) != "undefined");				}			/**			 * Defines the container for these locked class			 */			var lockeds = {};			/**			 * Determines whether the class is			 * locked by the specified name			 */			var isLocked = function(name) {					return (lockeds[name] == 1);				}			/**			 * Locks the specified class			 */			var lock = function(name) {					lockeds[name] = 1;				}			/**			 * Unlocks the specified class			 */			var unlock = function(name) {					lockeds[name] = 0;				}			/**			 * Defines a class by name and code			 */			this.defineClass = function(name, code) {				try {					if (isLocked(name)) {						throw new Exception("kernel.js/JSVM.engine.defineClass()"							+ " Class: '" + name + "' is locked.");					}					lock(name);					this.executor.execute(code);					var c = eval(name);					if (typeof(c) == "function") {						c.prototype.$class = c;						c.$name = name;					} else {						logger.log("JSVM Warn: the class '" + name							+ "' is not a function instance !");					}					return c;				} catch (ex) {					throw new Exception("kernel.js/JSVM.engine.defineClass() Error.", ex);				} finally {					unlock(name);				}			}			/**			 * Defines a package by name.			 */			this.definePackage = function(name) {				if (isPackage(name) || typeOf(name) == "object") {					return;				} else if (typeOf(name) == "undefined") {					var idx = name.lastIndexOf(".");					if (idx > -1) {						this.definePackage(name.substring(0, idx));					}					eval("window." + name + "=new Package(name);");				} else {					throw new Exception("kernel.js/JSVM.engine.definePackage()"						+ " Var '" + name + "' always exists.");				}			}		};		/**		 * Sets the engine for JSVM		 */		this.setEngine = function(o) {			engine = o;		}		/**		 * Returns the engine of JSVM		 */		this.getEngine = function() {			return engine;		}		/**		 * Loads the class with the specified name.		 */		this.loadClass = function(name) {			try {				// get a class from container with name.				var clazz = container.getClass(name);				if (clazz == null) {					// determine whether this variable exists.					try {						var obj = eval(name), type = typeof(obj);						if (type == "object" || type == "function") {							logger.log("warn: the class '" + name 								+ "' already exists outside of JSVM container.");							return obj;						}					} catch (ex) {						// do nothing.					}					// get class code from container with name.					var code = container.getClassCode(name);					if (!code) {						// get class code from classloader with name.						code = classloader.loadClass(name);						// compile the resource code.						code = compiler.compile(code);						// put the class code into container.						container.putClassCode(name, code);					}					// define class by engine.					clazz = engine.defineClass(name, code);					// put the class into container.					container.putClass(name, clazz);				}				return clazz;			} catch (ex) {				var err = new Exception("kernel.js/JSVM.loadClass()"					+ " Not found class: " + name, ex);				logger.log(err);				throw err;			}		}		/**		 * Loads a package with the specified name.		 */		this.loadPackage = function(name) {			var err = new Exception("kernel.js/JSVM.loadPackage() Not implemented.");			logger.log(err);			throw err;		}		/**		 * Initializes JSVM		 */		this.initialize = function() {			classloader.setClasspath(jsre.classpath);		}		/**		 * Destroies JSVM		 */		this.destroy = function() {			container.clear();		}	}	// Creates and initialize an instance of JSVM	window.JSVM = jsre.JSVM = new _JSVM_Namespace.kernel.JSVM_V2_04_060820();	if (jsre.state == 4) {		JSVM.initialize();	}	//logger.log("JSVM initialized.");	/**	 * defines a link for Class	 */	window.Class = Class;	/**	 * Imports a class with specified name.	 */	window._import = window.$import = function (name) {		try {			var clazz = JSVM.loadClass(name);			var shortName = name.replace(/(.*)\./, "");			if ("undefined"	== typeof(window[shortName])) {				window[shortName] = clazz;			}		} catch(ex) {			ex.printStackTrace();			throw ex;		}	}});

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?