dynamic_array.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. var RTL$ = {
  2. extend: function extend(methods){
  3. function Type(){
  4. for(var m in methods)
  5. this[m] = methods[m];
  6. }
  7. Type.prototype = this.prototype;
  8. var result = methods.init;
  9. result.prototype = new Type(); // inherit this.prototype
  10. result.prototype.constructor = result; // to see constructor name in diagnostic
  11. result.extend = extend;
  12. return result;
  13. },
  14. makeArray: function (/*dimensions, initializer*/){
  15. var forward = Array.prototype.slice.call(arguments);
  16. var result = new Array(forward.shift());
  17. var i;
  18. if (forward.length == 1){
  19. var init = forward[0];
  20. if (typeof init == "function")
  21. for(i = 0; i < result.length; ++i)
  22. result[i] = init();
  23. else
  24. for(i = 0; i < result.length; ++i)
  25. result[i] = init;
  26. }
  27. else
  28. for(i = 0; i < result.length; ++i)
  29. result[i] = this.makeArray.apply(this, forward);
  30. return result;
  31. },
  32. clone: function (from){
  33. var to;
  34. var len;
  35. var i;
  36. var Ctr = from.constructor;
  37. if (Ctr == Uint16Array){
  38. len = from.length;
  39. to = this.__makeCharArray(len);
  40. for(i = 0; i < len; ++i)
  41. to[i] = from[i];
  42. }
  43. else {
  44. to = new Ctr();
  45. if (Ctr == Array)
  46. len = from.length;
  47. if (len){
  48. if (typeof from[0] != "object")
  49. for(i = 0; i < len; ++i)
  50. to[i] = from[i];
  51. else
  52. for(i = 0; i < len; ++i){
  53. var o = from[i];
  54. if (o !== null)
  55. to[i] = this.clone(o);
  56. }
  57. }
  58. else
  59. this.copy(from, to);
  60. }
  61. return to;
  62. },
  63. copy: function (from, to){
  64. for(var prop in to){
  65. if (to.hasOwnProperty(prop)){
  66. var v = from[prop];
  67. if (v !== null && typeof v == "object")
  68. this.copy(v, to[prop]);
  69. else
  70. to[prop] = v;
  71. }
  72. }
  73. },
  74. __makeCharArray: function (length){
  75. var result = new Uint16Array(length);
  76. result.charCodeAt = function(i){return this[i];};
  77. return result;
  78. }
  79. };
  80. var m = function (){
  81. var T = RTL$.extend({
  82. init: function T(){
  83. this.a = [];
  84. }
  85. });
  86. var r = new T();
  87. var a = RTL$.makeArray(3, 0);
  88. var dynamicInt = [];
  89. var dynamicString = [];
  90. var dynamicByte = [];
  91. var dynamicRecord = [];
  92. var dynamicArrayOfStaticArrayInt = [];
  93. var i = 0;
  94. var s = '';
  95. var byte = 0;
  96. function assignDynamicArrayFromStatic(){
  97. var static$ = RTL$.makeArray(3, 0);
  98. var dynamic = [];
  99. dynamic = RTL$.clone(static$);
  100. }
  101. dynamicInt.push(3);
  102. dynamicInt.push(i);
  103. dynamicInt.push(byte);
  104. dynamicString.push("abc");
  105. dynamicString.push("\"");
  106. dynamicString.push(s);
  107. dynamicByte.push(byte);
  108. dynamicByte.push(i & 0xFF);
  109. dynamicRecord.push(RTL$.clone(r));
  110. dynamicArrayOfStaticArrayInt.push(RTL$.clone(a));
  111. dynamicInt.splice(i, 1);
  112. }();