dynamic_array.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. copyArrayOfRecords: function (from, to){
  33. to.splice(0, to.length);
  34. var length = from.length;
  35. if (!length)
  36. return;
  37. var method = from[0] instanceof Array
  38. ? this.cloneArrayOfRecords // this is array of arrays, go deeper
  39. : this.cloneRecord;
  40. for(var i = 0; i < length; ++i)
  41. to.push(method.call(this, from[i]));
  42. },
  43. cloneArrayOfRecords: function (from){
  44. var length = from.length;
  45. var result = new Array(length);
  46. if (length){
  47. var method = from[0] instanceof Array
  48. ? this.cloneArrayOfRecords // this is array of arrays, go deeper
  49. : this.cloneRecord;
  50. for(var i = 0; i < result.length; ++i)
  51. result[i] = method.call(this, from[i]);
  52. }
  53. return result;
  54. },
  55. cloneRecord: function (from){
  56. var Ctr = from.constructor;
  57. var result = new Ctr();
  58. this.copyRecord(from, result);
  59. return result;
  60. },
  61. copyRecord: function (from, to){
  62. for(var prop in to){
  63. if (to.hasOwnProperty(prop)){
  64. var v = from[prop];
  65. var isScalar = prop[0] != "$";
  66. if (isScalar)
  67. to[prop] = v;
  68. else
  69. to[prop] = v instanceof Array ? this.cloneArrayOfRecords(v)
  70. : this.cloneRecord(v);
  71. }
  72. }
  73. },
  74. copyArrayOfScalars: function (from, to){
  75. to.splice(0, to.length);
  76. for(var i = 0; i < from.length; ++i)
  77. to.push(this.cloneArrayOfScalars(from[i]));
  78. },
  79. cloneArrayOfScalars: function (from){
  80. var length = from.length;
  81. if (!length)
  82. return [];
  83. if (!(from[0] instanceof Array))
  84. return from.slice();
  85. // this is array of arrays, go deeper
  86. var result = new Array(length);
  87. for(var i = 0; i < length; ++i)
  88. result[i] = this.cloneArrayOfScalars(from[i]);
  89. return result;
  90. },
  91. assert: function (condition){
  92. if (!condition)
  93. throw new Error("assertion failed");
  94. }
  95. };
  96. var m = function (){
  97. var T = RTL$.extend({
  98. init: function T(){
  99. this.a = [];
  100. }
  101. });
  102. var r = new T();
  103. var a = RTL$.makeArray(3, 0);
  104. var dynamicInt = [];
  105. var dynamicString = [];
  106. var dynamicChar = [];
  107. var dynamicByte = [];
  108. var dynamicRecord = [];
  109. var dynamicArrayOfStaticArrayInt = [];
  110. var i = 0;
  111. var s = '';
  112. var byte = 0;
  113. function assignDynamicArrayFromStatic(){
  114. var static$ = RTL$.makeArray(3, 0);
  115. var dynamic = [];
  116. Array.prototype.splice.apply(dynamic, [0, Number.MAX_VALUE].concat(static$));
  117. }
  118. function returnOuterArray(){
  119. return a.slice();
  120. }
  121. function passArrayBeRef(a/*VAR ARRAY * OF INTEGER*/){
  122. var static$ = RTL$.makeArray(3, 0);
  123. a[0] = 1;
  124. a[0] = a[1];
  125. Array.prototype.splice.apply(a, [0, Number.MAX_VALUE].concat(static$));
  126. Array.prototype.splice.apply(a, [0, Number.MAX_VALUE].concat(dynamicInt));
  127. }
  128. function passArrayOfRecordsByRef(a/*VAR ARRAY * OF T*/){
  129. var result = [];
  130. RTL$.copyArrayOfRecords(result, a);
  131. }
  132. function passArrayOfArraysByRef(a/*VAR ARRAY *, 3 OF INTEGER*/){
  133. var result = [];
  134. RTL$.copyArrayOfScalars(result, a);
  135. }
  136. dynamicInt.push(3);
  137. dynamicInt.push(i);
  138. dynamicInt.push(byte);
  139. dynamicString.push("abc");
  140. dynamicString.push("\"");
  141. dynamicString.push(s);
  142. dynamicChar.push(34);
  143. dynamicByte.push(byte);
  144. dynamicByte.push(i & 0xFF);
  145. dynamicRecord.push(RTL$.cloneRecord(r));
  146. dynamicArrayOfStaticArrayInt.push(a.slice());
  147. RTL$.assert(dynamicInt.indexOf(i) != -1);
  148. RTL$.assert(dynamicChar.indexOf(34) != -1);
  149. dynamicInt.splice(i, 1);
  150. dynamicInt.splice(0, Number.MAX_VALUE);
  151. passArrayBeRef(dynamicInt);
  152. passArrayOfRecordsByRef(dynamicRecord);
  153. passArrayOfArraysByRef(dynamicArrayOfStaticArrayInt);
  154. }();