CodeSerializer.js 5.7 KB

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