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. copy: function (from, to){
  33. for(var prop in to){
  34. if (to.hasOwnProperty(prop)){
  35. var v = from[prop];
  36. if (v !== null && typeof v == "object")
  37. this.copy(v, to[prop]);
  38. else
  39. to[prop] = v;
  40. }
  41. }
  42. },
  43. clone: function (from){
  44. var to;
  45. var len;
  46. var i;
  47. var Ctr = from.constructor;
  48. if (Ctr == Uint16Array){
  49. len = from.length;
  50. to = this.__makeCharArray(len);
  51. for(i = 0; i < len; ++i)
  52. to[i] = from[i];
  53. }
  54. else {
  55. to = new Ctr();
  56. if (Ctr == Array)
  57. len = from.length;
  58. if (len){
  59. if (typeof from[0] != "object")
  60. for(i = 0; i < len; ++i)
  61. to[i] = from[i];
  62. else
  63. for(i = 0; i < len; ++i){
  64. var o = from[i];
  65. if (o !== null)
  66. to[i] = this.clone(o);
  67. }
  68. }
  69. else
  70. this.copy(from, to);
  71. }
  72. return to;
  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. RTL$.copy(static$, dynamic);
  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. }();