dynamic_array.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. cloneRecord: function (from){
  33. var Ctr = from.constructor;
  34. var result = new Ctr();
  35. this.copyRecord(from, result);
  36. return result;
  37. },
  38. copyRecord: function (from, to){
  39. for(var prop in to){
  40. if (to.hasOwnProperty(prop)){
  41. var v = from[prop];
  42. var isScalar = prop[0] != "$";
  43. if (isScalar)
  44. to[prop] = v;
  45. else
  46. to[prop] = v instanceof Array ? this.cloneArrayOfRecords(v)
  47. : this.cloneRecord(v);
  48. }
  49. }
  50. },
  51. assert: function (condition){
  52. if (!condition)
  53. throw new Error("assertion failed");
  54. }
  55. };
  56. var m = function (){
  57. var T = RTL$.extend({
  58. init: function T(){
  59. this.a = [];
  60. }
  61. });
  62. var r = new T();
  63. var a = RTL$.makeArray(3, 0);
  64. var dynamicInt = [];
  65. var dynamicString = [];
  66. var dynamicChar = [];
  67. var dynamicByte = [];
  68. var dynamicRecord = [];
  69. var dynamicArrayOfStaticArrayInt = [];
  70. var i = 0;
  71. var s = '';
  72. var byte = 0;
  73. function assignDynamicArrayFromStatic(){
  74. var static$ = RTL$.makeArray(3, 0);
  75. var dynamic = [];
  76. dynamic = static$.slice();
  77. }
  78. function returnOuterArray(){
  79. return a.slice();
  80. }
  81. dynamicInt.push(3);
  82. dynamicInt.push(i);
  83. dynamicInt.push(byte);
  84. dynamicString.push("abc");
  85. dynamicString.push("\"");
  86. dynamicString.push(s);
  87. dynamicChar.push(34);
  88. dynamicByte.push(byte);
  89. dynamicByte.push(i & 0xFF);
  90. dynamicRecord.push(RTL$.cloneRecord(r));
  91. dynamicArrayOfStaticArrayInt.push(a.slice());
  92. RTL$.assert(dynamicInt.indexOf(i) != -1);
  93. RTL$.assert(dynamicChar.indexOf(34) != -1);
  94. dynamicInt.splice(i, 1);
  95. dynamicInt.splice(0, Number.MAX_VALUE);
  96. }();