dynamic_array.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. assert: function (condition){
  80. if (!condition)
  81. throw new Error("assertion failed");
  82. }
  83. };
  84. var m = function (){
  85. var T = RTL$.extend({
  86. init: function T(){
  87. this.a = [];
  88. }
  89. });
  90. var r = new T();
  91. var a = RTL$.makeArray(3, 0);
  92. var dynamicInt = [];
  93. var dynamicString = [];
  94. var dynamicChar = [];
  95. var dynamicByte = [];
  96. var dynamicRecord = [];
  97. var dynamicArrayOfStaticArrayInt = [];
  98. var i = 0;
  99. var s = '';
  100. var byte = 0;
  101. function assignDynamicArrayFromStatic(){
  102. var static$ = RTL$.makeArray(3, 0);
  103. var dynamic = [];
  104. dynamic = RTL$.clone(static$);
  105. }
  106. function returnOuterArray(){
  107. return RTL$.clone(a);
  108. }
  109. dynamicInt.push(3);
  110. dynamicInt.push(i);
  111. dynamicInt.push(byte);
  112. dynamicString.push("abc");
  113. dynamicString.push("\"");
  114. dynamicString.push(s);
  115. dynamicChar.push(34);
  116. dynamicByte.push(byte);
  117. dynamicByte.push(i & 0xFF);
  118. dynamicRecord.push(RTL$.clone(r));
  119. dynamicArrayOfStaticArrayInt.push(RTL$.clone(a));
  120. RTL$.assert(dynamicInt.indexOf(i) != -1);
  121. RTL$.assert(dynamicChar.indexOf(34) != -1);
  122. dynamicInt.splice(i, 1);
  123. dynamicInt.splice(0, Number.MAX_VALUE);
  124. }();