CodeSerializer.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /**
  2. * @author Kai Salmen / https://kaisalmen.de
  3. * Development repository: https://github.com/kaisalmen/WWOBJLoader
  4. */
  5. const CodeSerializer = {
  6. /**
  7. * Serialize an object without specific prototype definition.
  8. *
  9. * @param {String} fullObjectName complete object name
  10. * @param {Object} serializationTarget The object that should be serialized
  11. * @returns {String}
  12. */
  13. serializeObject: function ( fullObjectName, serializationTarget ) {
  14. let objectString = fullObjectName + ' = {\n\n';
  15. let part;
  16. for ( let name in serializationTarget ) {
  17. part = serializationTarget[ name ];
  18. if ( typeof ( part ) === 'string' || part instanceof String ) {
  19. part = part.replace( /\n/g, '\\n' );
  20. part = part.replace( /\r/g, '\\r' );
  21. objectString += '\t' + name + ': "' + part + '",\n';
  22. } else if ( part instanceof Array ) {
  23. objectString += '\t' + name + ': [' + part + '],\n';
  24. } else if ( typeof part === 'object' ) {
  25. console.log( 'Omitting object "' + name + '" and replace it with empty object.' );
  26. objectString += '\t' + name + ': {},\n';
  27. } else {
  28. objectString += '\t' + name + ': ' + part + ',\n';
  29. }
  30. }
  31. objectString += '}\n\n';
  32. return objectString;
  33. },
  34. /**
  35. * Serialize an object with specific prototype definition.
  36. *
  37. * @param {String} fullObjectName Specifies the complete object name
  38. * @param {Object} serializationTarget The object that should be serialized
  39. * @param {String} [basePrototypeName] Name of the prototype
  40. * @param {Object} [overrideFunctions} Array of {@Link CodeSerializationInstruction} allows to replace or remove function with provided content
  41. *
  42. * @returns {String}
  43. */
  44. serializeClass: function ( fullObjectName, serializationTarget, basePrototypeName, overrideFunctions ) {
  45. let objectPart, constructorString, i, funcInstructions, funcTemp;
  46. let prototypeFunctions = [];
  47. let objectProperties = [];
  48. let objectFunctions = [];
  49. let isExtended = ( basePrototypeName !== null && basePrototypeName !== undefined );
  50. if ( ! Array.isArray( overrideFunctions ) ) overrideFunctions = [];
  51. for ( let name in serializationTarget.prototype ) {
  52. objectPart = serializationTarget.prototype[ name ];
  53. funcInstructions = new CodeSerializationInstruction( name, fullObjectName + '.prototype.' + name );
  54. funcInstructions.setCode( objectPart.toString() );
  55. if ( name === 'constructor' ) {
  56. if ( ! funcInstructions.isRemoveCode() ) {
  57. constructorString = fullObjectName + ' = ' + funcInstructions.getCode() + ';\n\n';
  58. }
  59. } else if ( typeof objectPart === 'function' ) {
  60. funcTemp = overrideFunctions[ name ];
  61. if ( funcTemp instanceof CodeSerializationInstruction && funcTemp.getName() === funcInstructions.getName() ) {
  62. funcInstructions = funcTemp;
  63. }
  64. if ( ! funcInstructions.isRemoveCode() ) {
  65. if ( isExtended ) {
  66. prototypeFunctions.push( funcInstructions.getFullName() + ' = ' + funcInstructions.getCode() + ';\n\n' );
  67. } else {
  68. prototypeFunctions.push( '\t' + funcInstructions.getName() + ': ' + funcInstructions.getCode() + ',\n\n' );
  69. }
  70. }
  71. }
  72. }
  73. for ( let name in serializationTarget ) {
  74. objectPart = serializationTarget[ name ];
  75. funcInstructions = new CodeSerializationInstruction( name, fullObjectName + '.' + name );
  76. if ( typeof objectPart === 'function' ) {
  77. funcTemp = overrideFunctions[ name ];
  78. if ( funcTemp instanceof CodeSerializationInstruction && funcTemp.getName() === funcInstructions.getName() ) {
  79. funcInstructions = funcTemp;
  80. } else {
  81. funcInstructions.setCode( objectPart.toString() );
  82. }
  83. if ( ! funcInstructions.isRemoveCode() ) {
  84. objectFunctions.push( funcInstructions.getFullName() + ' = ' + funcInstructions.getCode() + ';\n\n' );
  85. }
  86. } else {
  87. if ( typeof ( objectPart ) === 'string' || objectPart instanceof String ) {
  88. funcInstructions.setCode( '\"' + objectPart.toString() + '\"' );
  89. } else if ( typeof objectPart === 'object' ) {
  90. console.log( 'Omitting object "' + funcInstructions.getName() + '" and replace it with empty object.' );
  91. funcInstructions.setCode( "{}" );
  92. } else {
  93. funcInstructions.setCode( objectPart );
  94. }
  95. if ( ! funcInstructions.isRemoveCode() ) {
  96. objectProperties.push( funcInstructions.getFullName() + ' = ' + funcInstructions.getCode() + ';\n' );
  97. }
  98. }
  99. }
  100. let objectString = constructorString + '\n\n';
  101. if ( isExtended ) {
  102. objectString += fullObjectName + '.prototype = Object.create( ' + basePrototypeName + '.prototype );\n';
  103. }
  104. objectString += fullObjectName + '.prototype.constructor = ' + fullObjectName + ';\n';
  105. objectString += '\n\n';
  106. for ( i = 0; i < objectProperties.length; i ++ ) {
  107. objectString += objectProperties[ i ];
  108. }
  109. objectString += '\n\n';
  110. for ( i = 0; i < objectFunctions.length; i ++ ) {
  111. objectString += objectFunctions[ i ];
  112. }
  113. objectString += '\n\n';
  114. if ( isExtended ) {
  115. for ( i = 0; i < prototypeFunctions.length; i ++ ) {
  116. objectString += prototypeFunctions[ i ];
  117. }
  118. } else {
  119. objectString += fullObjectName + '.prototype = {\n\n';
  120. for ( i = 0; i < prototypeFunctions.length; i ++ ) {
  121. objectString += prototypeFunctions[ i ];
  122. }
  123. objectString += '\n};';
  124. }
  125. objectString += '\n\n';
  126. return objectString;
  127. },
  128. };
  129. /**
  130. * Allows to define instructions to override or remove
  131. * @param {String} name Usually the name of a function
  132. * @param {String} fullName The name plus full object description
  133. * @constructor
  134. */
  135. const CodeSerializationInstruction = function ( name, fullName ) {
  136. this.name = name;
  137. this.fullName = fullName;
  138. this.code = null;
  139. this.removeCode = false;
  140. };
  141. CodeSerializationInstruction.prototype = {
  142. constructor: CodeSerializationInstruction,
  143. /**
  144. * Returns the name of the function
  145. * @return {String}
  146. */
  147. getName: function () {
  148. return this.name;
  149. },
  150. /**
  151. * Returns the full name of the function
  152. * @return {String}
  153. */
  154. getFullName: function () {
  155. return this.fullName;
  156. },
  157. /**
  158. * Set the string containing the serialized function
  159. * @param {String} code
  160. * @return {CodeSerializationInstruction}
  161. */
  162. setCode: function ( code ) {
  163. this.code = code;
  164. return this;
  165. },
  166. /**
  167. * Returns the serialized function code
  168. * @return {String}
  169. */
  170. getCode: function () {
  171. return this.code;
  172. },
  173. /**
  174. * Set if function should be removed
  175. * @param {boolean} removeCode
  176. * @return {CodeSerializationInstruction}
  177. */
  178. setRemoveCode: function ( removeCode ) {
  179. this.removeCode = removeCode;
  180. return this;
  181. },
  182. /**
  183. * If function should be completely removed
  184. * @return {boolean}
  185. */
  186. isRemoveCode: function () {
  187. return this.removeCode;
  188. }
  189. };
  190. export {
  191. CodeSerializer,
  192. CodeSerializationInstruction
  193. };