2
0

dynamic_array.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. };
  44. var m = function (){
  45. var T = RTL$.extend({
  46. init: function T(){
  47. this.a = [];
  48. }
  49. });
  50. var a = [];
  51. var r = new T();
  52. function assignDynamicArrayFromStatic(){
  53. var static$ = RTL$.makeArray(3, 0);
  54. var dynamic = [];
  55. RTL$.copy(static$, dynamic);
  56. dynamic.push(3);
  57. }
  58. }();