preprocessor.js

来自「Browser independent JavaScript SDK. Clas」· JavaScript 代码 · 共 503 行 · 第 1/2 页

JS
503
字号

				for(var i=0;i<imports.length;i++)
				{
					if(imports[i].clazz == superClass)
					{
						fqSuperClass = imports[i].fqclazz;
						break;
					}
				}
			}
			if(implementsIndex > -1)
			{
				if(!clazz)
				{
					clazz = runtime.trimString(classDef.substring(0, implementsIndex));
				}

				var implementsClasses;
				if(mixinIndex > -1)
				{
					implementsClasses = runtime.trimString(classDef.substring((implementsIndex+this.IMPLEMENTS_TXT.length), mixinIndex)).split(",");
				}
				else
				{
					implementsClasses = runtime.trimString(classDef.substring((implementsIndex+this.IMPLEMENTS_TXT.length), classDef.length)).split(",");
				}

				for(var i=0;i<implementsClasses.length;i++)
				{
					var fndIndex = runtime.findIndexInArray(runtime.trimString(implementsClasses[i]), classImports);
					if(fndIndex > -1)
					{
						interfaces[interfaces.length] = imports[fndIndex].fqclazz;
					}
				}
			}
			if(mixinIndex > -1)
			{
				if(!clazz)
				{
					clazz = runtime.trimString(classDef.substring(0, mixinIndex));
				}

				var mixinClasses = runtime.trimString(classDef.substring((mixinIndex+this.MIXINS_TXT.length), classDef.length)).split(",");
				for(var i=0;i<mixinClasses.length;i++)
				{
					var fndIndex = runtime.findIndexInArray(runtime.trimString(mixinClasses[i]), classImports);
					if(fndIndex > -1)
					{
						mixins[mixins.length] = imports[fndIndex].fqclazz;
					}
				}
			}
		}
		else
		{
			clazz = runtime.trimString(classDef);
		}

		// Code Generation
		var newCode = "";

		var classCall = clazz+".prototype";

		if(runtime.findIndexInArray(clazz, this.CORE_CLASSES) == -1)
		{
			newCode += clazz+" = function(){this.initialize.apply(this, arguments);}\n";

			if(superClass)
			{
				newCode += classCall+" = new "+superClass+"();\n";
				newCode += classCall+".constructor = "+clazz+";\n";
				newCode += clazz+".superclass = "+superClass+".prototype;\n";

				var superCall = clazz+".superclass";
			}
		}

		// set fqc name
		fqClass = clazz;
		if(pakage)
		{
			fqClass = pakage+"."+clazz;
		}

		// need to remove the current class we're processing if it's in the imports. It could be in the array because of the wild card
		var newImports = [];
		for(var i=0;i<imports.length;i++)
		{
			if(fqClass != imports[i].fqclazz)
			{
				newImports[newImports.length] = imports[i];
			}
		}

		// code body
		var middleCode = _jsCode.substring(startIndex+1, (_jsCode.lastIndexOf("}")));

		var _classComments = "";

		// Replace the constructor
		// don't set up constructor if there is a new in front of the class
		var _constIndex = middleCode.indexOf(clazz+"(");
		var _testNew = middleCode.substring((_constIndex-5), _constIndex)
		var _testNew2 = middleCode.substring((_constIndex-3), _constIndex)
		if(_testNew != " new " && _testNew2 != "new")
		{
			// only time we need to do something special w/ comments
			var _classCommentsStart = middleCode.indexOf("/*");
			if(_classCommentsStart > -1 && _classCommentsStart < _constIndex)
			{
				var _classCommentsEnd = middleCode.indexOf("*/");

				// could have overview and class jsdoc tags
				var _another = middleCode.indexOf("*/", _classCommentsEnd+2);
				if(_another > 0 && _another < _constIndex)
				{
					_classCommentsEnd = _another;
				}

				_classComments = middleCode.substring(_classCommentsStart-2, _classCommentsEnd+2);

				middleCode = middleCode.substring(_classCommentsEnd+2, middleCode.length);
			}

			middleCode = middleCode.replace(clazz+"(", "/** @ignore */\n\t"+classCall+".initialize = function(");
		}

		// Function Replacing
		// #########################################
		// Replace super calls
		if(superCall)
		{
			// super call to init parent;
			middleCode = this.replaceCode(middleCode, this.SUPER_INIT, "(", function(_srcCode, _index, _searchFor, _endIndex)
						{
							var args = me.getFunctionArgs(_srcCode, _index, "super", "", _endIndex);

							var newCode = superCall+"."+"initialize"+".apply(this, ["+args+"])";
							var oldCode = _searchFor+args+")";

							return {oldCode : oldCode, newCode : newCode};
						});

			// look for other super method calls and replace ...
			// super.<method>(arg1, arg2, arg3) w/ <class>.superclass.<method>.apply(this, [arg1, arg2, arg3])
			middleCode = this.replaceCode(middleCode, this.SUPER_TXT, "(", function(_srcCode, _index, _searchFor, _endIndex)
						{
							var functionName = me.getName(_srcCode, _index, _searchFor, _endIndex);
							var args = me.getFunctionArgs(_srcCode, _index, _searchFor, functionName, _endIndex);

							var newCode = superCall+"."+functionName+".apply(this, ["+args+"])";
							var oldCode = _searchFor + functionName +"(" + args + ")";

							return {oldCode : oldCode, newCode : newCode};
						});
		}

		// look for instance(s) and replace ...
		// instance <method>(arg1, arg2, arg3) w/ <class>.prototype.<method> = function([arg1, arg2, arg3])
		middleCode = this.replaceCode(middleCode, this.INSTANCE_TXT, "(", function(_srcCode, _index, _searchFor, _endIndex)
					{
						var functionName = me.getName(_srcCode, _index, _searchFor, _endIndex);
						var args = me.getFunctionArgs(_srcCode, _index, _searchFor, functionName, _endIndex);

						var newCode = classCall+"."+functionName+" = function("+args+")";
						var oldCode = _searchFor + functionName +"(" + args + ")";

						return {oldCode : oldCode, newCode : newCode};
					});

		// look for abstract(s) and replace ...
		// abstract <method>(arg1, arg2, arg3) w/ <class>.prototype.<method> = function([arg1, arg2, arg3]){throw "method <class>.<method> is abstract";}
		middleCode = this.replaceCode(middleCode, this.ABSTRACT_TXT, "(", function(_srcCode, _index, _searchFor, _endIndex)
					{
						var functionName = me.getName(_srcCode, _index, _searchFor, _endIndex);
						var args = me.getFunctionArgs(_srcCode, _index, _searchFor, functionName, _endIndex);

						var newCode = classCall+"."+functionName+" = function("+args+"){throw '" + clazz + "." + functionName + " is abstract';}";
						var oldCode = _searchFor + functionName +"(" + args + ")";

						return {oldCode : oldCode, newCode : newCode};
					});

		// look for static(s) and replace ...
		// static <method>(arg1, arg2, arg3) w/ <class>.<method> = function([arg1, arg2, arg3])
		middleCode = this.replaceCode(middleCode, this.STATIC_TXT, "(", function(_srcCode, _index, _searchFor, _endIndex)
					{
						var functionName = me.getName(_srcCode, _index, _searchFor, _endIndex);
						var args = me.getFunctionArgs(_srcCode, _index, _searchFor, functionName, _endIndex);

						var newCode = clazz+"."+functionName+" = function("+args+")";
						var oldCode = _searchFor + functionName +"(" + args + ")";

						return {oldCode : oldCode, newCode : newCode};
					});

		// look for instance fields and replace
		// ##name w/ <class>.prototype.name
		middleCode = this.replaceCode(middleCode, "##", "=", function(_srcCode, _index, _searchFor, _endIndex)
					{
						var fieldName = me.getName(_srcCode, _index, _searchFor, _endIndex);

						var newCode = classCall+"."+fieldName;
						var oldCode = _searchFor + fieldName;

						return {oldCode : oldCode, newCode : newCode};
					});

		// append to
		newCode += middleCode;

		// ########################################
		// ########### Fix Namespacing ############
		// class
		if(pakage)
		{
			newCode = this.fixNamespacing(newCode, clazz, fqClass);
		}

		// imports
		var fqcImports = [];
		for(var i=0;i<newImports.length;i++)
		{
			newCode = this.fixNamespacing(newCode, newImports[i].clazz, newImports[i].fqclazz);

			fqcImports[fqcImports.length] = newImports[i].fqclazz;
		}
		// ########################################
		// ########################################

		// hack for keeping class comments
		if(_classComments)
		{
			newCode = _classComments +"\n\t"+ newCode;
		}

		return {
				pakage : pakage,
				clazz : clazz,
				fqClass : fqClass,
				fqSuperClass : fqSuperClass,
				imports : fqcImports,
				mixins : mixins,
				interfaces : interfaces,
				jsCode : newCode,
				nativeCode : _jsCode
			};
    }

};

⌨️ 快捷键说明

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