2
0

CodeSerializer.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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} fullName complete object name
  10. * @param {Object} object The object that should be serialized
  11. * @returns {String}
  12. */
  13. serializeObject: function ( fullName, object ) {
  14. let objectString = fullName + ' = {\n\n';
  15. let part;
  16. for ( let name in object ) {
  17. part = object[ name ];
  18. if ( typeof ( part ) === 'string' || part instanceof String ) {
  19. part = part.replace( '\n', '\\n' );
  20. part = part.replace( '\r', '\\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. // TODO: Short-cut for now. Recursion required?
  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} fullName complete object name
  38. * @param {Object} object The object that should be serialized
  39. * @param {String} constructorName
  40. * @param {String} basePrototypeName
  41. * @param {Object} overrideFunctions Object of {@Link OverrideFunctionDescription}
  42. * @returns {String}
  43. */
  44. serializeClass: function ( fullName, object, constructorName, basePrototypeName, overrideFunctions ) {
  45. let valueString, objectPart, constructorString, i, funcOverride, currentName;
  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 object.prototype ) {
  52. objectPart = object.prototype[ name ];
  53. valueString = objectPart.toString();
  54. if ( name === 'constructor' ) {
  55. constructorString = fullName + ' = ' + valueString + ';\n\n';
  56. } else if ( typeof objectPart === 'function' ) {
  57. currentName = fullName + '.prototype.' + name;
  58. funcOverride = overrideFunctions[ name ];
  59. if ( funcOverride instanceof OverrideFunctionInstruction && funcOverride.getFullName() === currentName ) {
  60. valueString = funcOverride.code;
  61. }
  62. if ( isExtended ) {
  63. prototypeFunctions.push( currentName + ' = ' + valueString + ';\n\n' );
  64. } else {
  65. prototypeFunctions.push( '\t' + name + ': ' + valueString + ',\n\n' );
  66. }
  67. }
  68. }
  69. for ( let name in object ) {
  70. objectPart = object[ name ];
  71. currentName = fullName + '.' + name;
  72. if ( typeof objectPart === 'function' ) {
  73. funcOverride = overrideFunctions[ name ];
  74. if ( funcOverride instanceof OverrideFunctionInstruction && funcOverride.getFullName() === currentName ) {
  75. valueString = funcOverride.getFunctionCode();
  76. } else {
  77. valueString = objectPart.toString();
  78. }
  79. objectFunctions.push( currentName + ' = ' + valueString + ';\n\n' );
  80. } else {
  81. if ( typeof ( objectPart ) === 'string' || objectPart instanceof String ) {
  82. valueString = '\"' + objectPart.toString() + '\"';
  83. } else if ( typeof objectPart === 'object' ) {
  84. // TODO: Short-cut for now. Recursion required?
  85. valueString = "{}";
  86. } else {
  87. valueString = objectPart;
  88. }
  89. objectProperties.push( currentName + ' = ' + valueString + ';\n' );
  90. }
  91. }
  92. if ( ( constructorString === undefined || constructorString === null ) && typeof object.prototype.constructor === 'function' ) {
  93. constructorString = fullName + ' = ' + object.prototype.constructor.toString().replace( constructorName, '' );
  94. }
  95. let objectString = constructorString + '\n\n';
  96. if ( isExtended ) {
  97. objectString += fullName + '.prototype = Object.create( ' + basePrototypeName + '.prototype );\n';
  98. }
  99. objectString += fullName + '.prototype.constructor = ' + fullName + ';\n';
  100. objectString += '\n\n';
  101. for ( i = 0; i < objectProperties.length; i ++ ) {
  102. objectString += objectProperties[ i ];
  103. }
  104. objectString += '\n\n';
  105. for ( i = 0; i < objectFunctions.length; i ++ ) {
  106. objectString += objectFunctions[ i ];
  107. }
  108. objectString += '\n\n';
  109. if ( isExtended ) {
  110. for ( i = 0; i < prototypeFunctions.length; i ++ ) {
  111. objectString += prototypeFunctions[ i ];
  112. }
  113. } else {
  114. objectString += fullName + '.prototype = {\n\n';
  115. for ( i = 0; i < prototypeFunctions.length; i ++ ) {
  116. objectString += prototypeFunctions[ i ];
  117. }
  118. objectString += '\n};';
  119. }
  120. objectString += '\n\n';
  121. return objectString;
  122. },
  123. };
  124. /**
  125. *
  126. * @param {String} fullName
  127. * @param {String} functionCode
  128. * @constructor
  129. */
  130. const OverrideFunctionInstruction = function ( fullName, functionCode ) {
  131. this.fullName = fullName;
  132. this.functionCode = functionCode;
  133. };
  134. OverrideFunctionInstruction.prototype = {
  135. constructor: OverrideFunctionInstruction,
  136. /**
  137. * Returns the full name of the function
  138. * @return {String}
  139. */
  140. getFullName: function () {
  141. return this.fullName;
  142. },
  143. /**
  144. * Returns the serialized function code
  145. * @return {String}
  146. */
  147. getFunctionCode: function() {
  148. return this.functionCode;
  149. }
  150. };
  151. export {
  152. CodeSerializer,
  153. OverrideFunctionInstruction
  154. };