ソースを参照

CodeSerializer: Clean-up/Remove functionality no longer needed. Make override clearer via CodeSerializationInstruction
Fixed comments and made minor ts adjustments

Kai Salmen 6 年 前
コミット
945149e3b1

+ 1 - 1
examples/jsm/loaders/OBJLoader2.js

@@ -269,7 +269,7 @@ OBJLoader2.prototype = {
 	 * @param {function} onLoad A function to be called after loading is successfully completed. The function receives loaded Object3D as an argument.
 	 * @param {function} [onFileLoadProgress] A function to be called while the loading is in progress. The argument will be the XMLHttpRequest instance, which contains total and Integer bytes.
 	 * @param {function} [onError] A function to be called if an error occurs during loading. The function receives the error as an argument.
-	 * @param {function} [onMeshAlter] Called after worker successfully delivered a single mesh
+	 * @param {function} [onMeshAlter] Called after every single mesh is made available by the parser
 	 */
 	load: function ( url, onLoad, onFileLoadProgress, onError, onMeshAlter ) {
 

+ 0 - 2
examples/jsm/loaders/OBJLoader2Parallel.js

@@ -116,8 +116,6 @@ OBJLoader2Parallel.prototype.buildWorkerCode = function () {
 		codeBuilderInstructions.addCodeFragment( codeParserPayloadHandler );
 		codeBuilderInstructions.addCodeFragment( codeWorkerRunner );
 
-		// allows to include full libraries as importScripts
-		//		codeBuilderInstructions.addLibraryImport( '../../node_modules/three/build/three.js' );
 		codeBuilderInstructions.addStartCode( 'new WorkerRunner( new DefaultWorkerPayloadHandler( new OBJLoader2Parser() ) );' );
 
 	}

+ 17 - 2
examples/jsm/loaders/obj2/utils/CodeSerializer.d.ts

@@ -1,4 +1,19 @@
 export namespace CodeSerializer {
-  export function serializeObject(fullName: string, object: object): string;
-  export function serializeClass(fullName: string, object: object, constructorName?: string, basePrototypeName?: string, ignoreFunctions?: string[], includeFunctions?: string[], overrideFunctions?: string[]): string;
+  export function serializeObject(fullName: string, serializationTarget: object): string;
+  export function serializeClass(fullObjectName: string, serializationTarget: object, basePrototypeName?: string, overrideFunctions?: CodeSerializationInstruction[]): string;
+}
+
+export class CodeSerializationInstruction {
+	constructor(name: string, fullName: string);
+	name: string;
+	fullName: string;
+	code: string;
+	removeCode: boolean;
+
+	getName(): string;
+	getFullName(): string;
+	setCode(code: string): this;
+	getCode(): string;
+	setRemoveCode(removeCode: boolean): this;
+	isRemoveCode(): boolean;
 }

+ 114 - 57
examples/jsm/loaders/obj2/utils/CodeSerializer.js

@@ -8,17 +8,17 @@ const CodeSerializer = {
 	/**
 	 * Serialize an object without specific prototype definition.
 	 *
-	 * @param {String} fullName complete object name
-	 * @param {Object} object The object that should be serialized
+	 * @param {String} fullObjectName complete object name
+	 * @param {Object} serializationTarget The object that should be serialized
 	 * @returns {String}
 	 */
-	serializeObject: function ( fullName, object ) {
+	serializeObject: function ( fullObjectName, serializationTarget ) {
 
-		let objectString = fullName + ' = {\n\n';
+		let objectString = fullObjectName + ' = {\n\n';
 		let part;
-		for ( let name in object ) {
+		for ( let name in serializationTarget ) {
 
-			part = object[ name ];
+			part = serializationTarget[ name ];
 			if ( typeof ( part ) === 'string' || part instanceof String ) {
 
 				part = part.replace( '\n', '\\n' );
@@ -31,7 +31,7 @@ const CodeSerializer = {
 
 			} else if ( typeof part === 'object' ) {
 
-				// TODO: Short-cut for now. Recursion required?
+				console.log( 'Omitting object "' + name + '" and replace it with empty object.');
 				objectString += '\t' + name + ': {},\n';
 
 			} else {
@@ -50,16 +50,16 @@ const CodeSerializer = {
 	/**
 	 * Serialize an object with specific prototype definition.
 	 *
-	 * @param {String} fullName complete object name
-	 * @param {Object} object The object that should be serialized
-	 * @param {String} constructorName
-	 * @param {String} basePrototypeName
-	 * @param {Object} overrideFunctions Object of {@Link OverrideFunctionDescription}
+	 * @param {String} fullObjectName Specifies the complete object name
+	 * @param {Object} serializationTarget The object that should be serialized
+	 * @param {String} [basePrototypeName] Name of the prototype
+	 * @param {Object} [overrideFunctions} Array of {@Link CodeSerializationInstruction} allows to replace or remove function with provided content
+	 *
 	 * @returns {String}
 	 */
-	serializeClass: function ( fullName, object, constructorName, basePrototypeName, overrideFunctions ) {
+	serializeClass: function ( fullObjectName, serializationTarget, basePrototypeName, overrideFunctions ) {
 
-		let valueString, objectPart, constructorString, i, funcOverride, currentName;
+		let objectPart, constructorString, i, funcInstructions, funcTemp;
 		let prototypeFunctions = [];
 		let objectProperties = [];
 		let objectFunctions = [];
@@ -67,88 +67,99 @@ const CodeSerializer = {
 
 		if ( ! Array.isArray( overrideFunctions ) ) overrideFunctions = [];
 
-		for ( let name in object.prototype ) {
+		for ( let name in serializationTarget.prototype ) {
+
+			objectPart = serializationTarget.prototype[ name ];
+			funcInstructions = new CodeSerializationInstruction( name, fullObjectName + '.prototype.' + name );
+			funcInstructions.setCode( objectPart.toString() );
 
-			objectPart = object.prototype[ name ];
-			valueString = objectPart.toString();
 			if ( name === 'constructor' ) {
 
-				constructorString = fullName + ' = ' + valueString + ';\n\n';
+				if ( !funcInstructions.isRemoveCode() ) {
+
+					constructorString = fullObjectName + ' = ' + funcInstructions.getCode() + ';\n\n';
+
+				}
 
 			} else if ( typeof objectPart === 'function' ) {
 
-				currentName = fullName + '.prototype.' + name;
-				funcOverride = overrideFunctions[ name ];
-				if ( funcOverride instanceof OverrideFunctionInstruction && funcOverride.getFullName() === currentName ) {
+				funcTemp = overrideFunctions[ name ];
+				if ( funcTemp instanceof CodeSerializationInstruction && funcTemp.getName() === funcInstructions.getName() ) {
 
-					valueString = funcOverride.code;
+					funcInstructions = funcTemp;
 
 				}
-				if ( isExtended ) {
+				if ( !funcInstructions.isRemoveCode() ) {
 
-					prototypeFunctions.push( currentName + ' = ' + valueString + ';\n\n' );
+					if ( isExtended ) {
 
-				} else {
+						prototypeFunctions.push( funcInstructions.getFullName() + ' = ' + funcInstructions.getCode() + ';\n\n' );
 
-					prototypeFunctions.push( '\t' + name + ': ' + valueString + ',\n\n' );
+					} else {
+
+						prototypeFunctions.push( '\t' + funcInstructions.getName() + ': ' + funcInstructions.getCode() + ',\n\n' );
+
+					}
 
 				}
 
 			}
 
 		}
-		for ( let name in object ) {
-
-			objectPart = object[ name ];
-			currentName = fullName + '.' + name;
+		for ( let name in serializationTarget ) {
 
+			objectPart = serializationTarget[ name ];
+			funcInstructions = new CodeSerializationInstruction( name, fullObjectName + '.' + name );
 			if ( typeof objectPart === 'function' ) {
 
-				funcOverride = overrideFunctions[ name ];
-				if ( funcOverride instanceof OverrideFunctionInstruction && funcOverride.getFullName() === currentName ) {
+				funcTemp = overrideFunctions[ name ];
+				if ( funcTemp instanceof CodeSerializationInstruction && funcTemp.getName() === funcInstructions.getName() ) {
 
-					valueString = funcOverride.getFunctionCode();
+					funcInstructions = funcTemp;
 
 				} else {
 
-					valueString = objectPart.toString();
+					funcInstructions.setCode( objectPart.toString() );
+
+				}
+				if ( ! funcInstructions.isRemoveCode() ) {
+
+					objectFunctions.push( funcInstructions.getFullName() + ' = ' + funcInstructions.getCode() + ';\n\n' );
 
 				}
-				objectFunctions.push( currentName + ' = ' + valueString + ';\n\n' );
 
 			} else {
 
 				if ( typeof ( objectPart ) === 'string' || objectPart instanceof String ) {
 
-					valueString = '\"' + objectPart.toString() + '\"';
+					funcInstructions.setCode( '\"' + objectPart.toString() + '\"' );
 
 				} else if ( typeof objectPart === 'object' ) {
 
-					// TODO: Short-cut for now. Recursion required?
-					valueString = "{}";
+					console.log( 'Omitting object "' + funcInstructions.getName() + '" and replace it with empty object.');
+					funcInstructions.setCode( "{}" );
 
 				} else {
 
-					valueString = objectPart;
+					funcInstructions.setCode( objectPart );
 
 				}
-				objectProperties.push( currentName + ' = ' + valueString + ';\n' );
+				if ( ! funcInstructions.isRemoveCode() ) {
 
-			}
+					objectProperties.push( funcInstructions.getFullName() + ' = ' + funcInstructions.getCode() + ';\n' );
 
-		}
-		if ( ( constructorString === undefined || constructorString === null ) && typeof object.prototype.constructor === 'function' ) {
+				}
 
-			constructorString = fullName + ' = ' + object.prototype.constructor.toString().replace( constructorName, '' );
+			}
 
 		}
 		let objectString = constructorString + '\n\n';
 		if ( isExtended ) {
 
-			objectString += fullName + '.prototype = Object.create( ' + basePrototypeName + '.prototype );\n';
+			objectString += fullObjectName + '.prototype = Object.create( ' + basePrototypeName + '.prototype );\n';
 
 		}
-		objectString += fullName + '.prototype.constructor = ' + fullName + ';\n';
+		objectString += fullObjectName + '.prototype.constructor = ' + fullObjectName + ';\n';
 		objectString += '\n\n';
 
 		for ( i = 0; i < objectProperties.length; i ++ ) {
@@ -175,7 +186,7 @@ const CodeSerializer = {
 
 		} else {
 
-			objectString += fullName + '.prototype = {\n\n';
+			objectString += fullObjectName + '.prototype = {\n\n';
 			for ( i = 0; i < prototypeFunctions.length; i ++ ) {
 
 				objectString += prototypeFunctions[ i ];
@@ -192,21 +203,33 @@ const CodeSerializer = {
 };
 
 /**
- *
- * @param {String} fullName
- * @param {String} functionCode
+ * Allows to define instructions to override or remove
+ * @param {String} name Usually the name of a function
+ * @param {String} fullName The name plus full object description
  * @constructor
  */
-const OverrideFunctionInstruction = function ( fullName, functionCode ) {
+const CodeSerializationInstruction = function ( name, fullName ) {
 
+	this.name = name;
 	this.fullName = fullName;
-	this.functionCode = functionCode;
+	this.code = null;
+	this.removeCode = false;
 
 };
 
-OverrideFunctionInstruction.prototype = {
+CodeSerializationInstruction.prototype = {
 
-	constructor: OverrideFunctionInstruction,
+	constructor: CodeSerializationInstruction,
+
+	/**
+	 * Returns the name of the function
+	 * @return {String}
+	 */
+	getName: function () {
+
+		return this.name;
+
+	},
 
 	/**
 	 * Returns the full name of the function
@@ -218,13 +241,47 @@ OverrideFunctionInstruction.prototype = {
 
 	},
 
+	/**
+	 * Set the string containing the serialized function
+	 * @param {String} code
+	 * @return {CodeSerializationInstruction}
+	 */
+	setCode: function ( code ) {
+
+		this.code = code;
+		return this;
+
+	},
+
 	/**
 	 * Returns the serialized function code
 	 * @return {String}
 	 */
-	getFunctionCode: function() {
+	getCode: function() {
+
+		return this.code;
+
+	},
+
+	/**
+	 * Set if function should be removed
+	 * @param {boolean} removeCode
+	 * @return {CodeSerializationInstruction}
+	 */
+	setRemoveCode: function ( removeCode ) {
+
+		this.removeCode = removeCode;
+		return this;
+
+	},
+
+	/**
+	 * If function should be completely removed
+	 * @return {boolean}
+	 */
+	isRemoveCode: function () {
 
-		return this.functionCode;
+		return this.removeCode;
 
 	}
 
@@ -232,5 +289,5 @@ OverrideFunctionInstruction.prototype = {
 
 export {
 	CodeSerializer,
-	OverrideFunctionInstruction
+	CodeSerializationInstruction
 };

+ 1 - 1
examples/jsm/loaders/obj2/utils/ObjectManipulator.js

@@ -16,7 +16,7 @@ const ObjectManipulator = {
 		// fast-fail
 		if ( objToAlter === undefined || objToAlter === null || params === undefined || params === null ) return;
 
-		var property, funcName, values;
+		let property, funcName, values;
 		for ( property in params ) {
 
 			funcName = 'set' + property.substring( 0, 1 ).toLocaleUpperCase() + property.substring( 1 );

+ 3 - 3
examples/jsm/loaders/obj2/worker/main/WorkerExecutionSupport.d.ts

@@ -30,7 +30,7 @@ export class WorkerExecutionSupport {
   };
 
   worker: {
-    native: null;
+    native: Worker;
     jsmWorker: boolean;
     logging: boolean;
     workerRunner: {
@@ -41,13 +41,13 @@ export class WorkerExecutionSupport {
     terminateWorkerOnLoad: boolean;
     forceWorkerDataCopy: boolean;
     started: boolean;
-    queuedMessage: null;
+    queuedMessage: object;
     callbacks: {
       onAssetAvailable: Function;
       onLoad: Function;
       terminate: Function;
     };
-  }
+  };
 
   setLogging(enabled: boolean, debug: boolean): this;
   setForceWorkerDataCopy(forceWorkerDataCopy: boolean): this;

+ 2 - 2
examples/jsm/loaders/obj2/worker/parallel/OBJLoader2Parser.js

@@ -1,10 +1,10 @@
 /**
- * @author Kai Salmen / www.kaisalmen.de
+ * @author Kai Salmen / https://kaisalmen.de
+ * Development repository: https://github.com/kaisalmen/WWOBJLoader
  */
 
 /**
  * Parse OBJ data either from ArrayBuffer or string
- * @class
  */
 const OBJLoader2Parser = function () {
 

+ 2 - 1
examples/jsm/loaders/obj2/worker/parallel/WorkerRunner.js

@@ -1,5 +1,6 @@
 /**
- * @author Kai Salmen / www.kaisalmen.de
+ * @author Kai Salmen / https://kaisalmen.de
+ * Development repository: https://github.com/kaisalmen/WWOBJLoader
  */
 
 import { ObjectManipulator } from "../../utils/ObjectManipulator.js";

+ 2 - 1
examples/jsm/loaders/obj2/worker/parallel/jsm/OBJLoader2Worker.js

@@ -1,5 +1,6 @@
 /**
- * @author Kai Salmen / www.kaisalmen.de
+ * @author Kai Salmen / https://kaisalmen.de
+ * Development repository: https://github.com/kaisalmen/WWOBJLoader
  */
 
 import { OBJLoader2Parser } from "../OBJLoader2Parser.js";