rtl.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. function Class(){}
  2. Class.extend = function extend(methods){
  3. methods.__proto__ = this.prototype; // make instanceof work
  4. // to see constructor name in diagnostic
  5. var result = methods.init;
  6. methods.constructor = result.prototype.constructor;
  7. result.prototype = methods;
  8. result.extend = extend;
  9. return result;
  10. };
  11. function RTLTypeGuard(from, to){
  12. if (!(from instanceof to))
  13. throw new Error("typeguard assertion failed");
  14. return from;
  15. }
  16. function RTLMakeArray(/*dimensions, initializer*/){
  17. var forward = Array.prototype.slice.call(arguments);
  18. var result = new Array(forward.shift());
  19. var i;
  20. if (forward.length == 1){
  21. var init = forward[0];
  22. if (typeof init == "function")
  23. for(i = 0; i < result.length; ++i)
  24. result[i] = init();
  25. else
  26. for(i = 0; i < result.length; ++i)
  27. result[i] = init;
  28. }
  29. else
  30. for(i = 0; i < result.length; ++i){
  31. result[i] = RTLMakeArray.apply(this, forward);
  32. return result;
  33. }
  34. }
  35. function RTLMakeSet(/*...*/){
  36. var result = 0;
  37. function checkBit(b){
  38. if (b < 0 || b > 31)
  39. throw new Error("integes between 0 and 31 expected, got " + b);
  40. }
  41. function setBit(b){
  42. checkBit(b);
  43. result |= 1 << b;
  44. }
  45. for(var i = 0; i < arguments.length; ++i){
  46. var b = arguments[i];
  47. if (b instanceof Array){
  48. var from = b[0];
  49. var to = b[1];
  50. if (from < to)
  51. throw new Error("invalid SET diapason: " + from + ".." + to);
  52. for(var bi = from; bi <= to; ++bi)
  53. setBit(bi);
  54. }
  55. else
  56. setBit(b);
  57. }
  58. return result;
  59. }
  60. function RTLMakeRef(obj, prop){
  61. return {set: function(v){ obj[prop] = v; },
  62. get: function(){ return obj[prop]; }};
  63. }
  64. function RTLSetInclL(l, r){
  65. return l & r == l;
  66. }
  67. function RTLSetInclR(l, r){
  68. return l & r == r;
  69. }
  70. exports.Class = Class;
  71. exports.RTL = Class.extend({
  72. init: function RTL(){
  73. this.__entries = {};
  74. this.__supportJS = false;
  75. },
  76. supportJS: function(){this.__supportJS = true;},
  77. baseClass: function(){
  78. if (!this.__entries["extend"])
  79. this.__entries.extend = Class.extend;
  80. return "RTL$";
  81. },
  82. genCast: function(obj, type){
  83. if (!this.__entries["typeGuard"])
  84. this.__entries.typeGuard = RTLTypeGuard;
  85. return "RTL$.typeGuard(" + obj + ", " + type.name() + ")";
  86. },
  87. makeRef: function(obj, prop){
  88. if (!this.__entries["makeRef"])
  89. this.__entries.makeRef = RTLMakeRef;
  90. return "RTL$.makeRef(" + obj + ", " + prop + ")";
  91. },
  92. makeArray: function(args){
  93. if (!this.__entries.makeArray)
  94. this.__entries.makeArray = RTLMakeArray;
  95. return "RTL$.makeArray(" + args + ")";
  96. },
  97. makeSet: function(args){
  98. if (!this.__entries["makeSet"])
  99. this.__entries.makeSet = RTLMakeSet;
  100. return "RTL$.makeSet(" + args + ")";
  101. },
  102. setInclL: function(args){
  103. if (!this.__entries.setInclL)
  104. this.__entries.setInclL = RTLSetInclL;
  105. return "RTL$.setInclL(" + args + ")";
  106. },
  107. setInclR: function(args){
  108. if (!this.__entries.setInclR)
  109. this.__entries.setInclR = RTLSetInclR;
  110. return "RTL$.setInclR(" + args + ")";
  111. },
  112. generate: function(){
  113. var result = "var RTL$ = {\n";
  114. var firstEntry = true;
  115. for (var name in this.__entries){
  116. if (!firstEntry)
  117. result += ",\n";
  118. else
  119. firstEntry = false;
  120. result += "\t" + name + ": " + this.__entries[name].toString().replace(/\n/g, "\n\t");
  121. }
  122. if (!firstEntry)
  123. result += "\n};\n";
  124. else
  125. result = "";
  126. if (this.__supportJS)
  127. result += "var JS = function(){return this;}();\n";
  128. return result;
  129. }
  130. });