show-vars.nut 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. local base_class_key = " [base class] -> ";
  2. local function getMembersNotInBaseClass(klass, klass_members){
  3. local bklass = klass.getbase();
  4. local bklass_members = {};
  5. if(bklass) foreach(k,v in bklass) bklass_members[k] <- true;
  6. foreach(k,v in klass) {
  7. if(table_get(bklass_members, k, klass) == klass) { //not found
  8. klass_members.push([k,v]);
  9. }
  10. }
  11. klass_members.push([base_class_key, gettypetag(bklass)]);
  12. }
  13. /** Make a human readable representation of a bit encoded integer.
  14. @param pc is an array of bit encoded integers
  15. @return string representing the parameters
  16. */
  17. local function getParamsCheck(pc){
  18. local mask_values = {
  19. ["NULL"] = 0x00000001,
  20. INTEGER = 0x00000002,
  21. FLOAT = 0x00000004,
  22. BOOL = 0x00000008,
  23. STRING = 0x00000010,
  24. TABLE = 0x00000020,
  25. ARRAY = 0x00000040,
  26. USERDATA = 0x00000080,
  27. CLOSURE = 0x00000100,
  28. NATIVECLOSURE = 0x00000200,
  29. GENERATOR = 0x00000400,
  30. USERPOINTER = 0x00000800,
  31. THREAD = 0x00001000,
  32. FUNCPROTO = 0x00002000,
  33. CLASS = 0x00004000,
  34. INSTANCE = 0x00008000,
  35. WEAKREF = 0x00010000,
  36. OUTER = 0x00020000,
  37. }
  38. local ANY = -1;
  39. local tmp = [];
  40. foreach(v in pc){
  41. if(v == ANY) {
  42. tmp.push("ANY");
  43. continue;
  44. }
  45. local m = [];
  46. foreach(k, mv in mask_values){
  47. if(mv & v) m.push(k);
  48. }
  49. tmp.push(m.concat("|"));
  50. }
  51. return "(" + tmp.concat(" , ") + ")";
  52. }
  53. /*local do not allow recursion*/
  54. local function showVars(avar, myvars, prefix=null){
  55. local isClass = type(avar) == "class";
  56. local isTable = type(avar) == "table";
  57. if(isClass) getMembersNotInBaseClass(avar, myvars);
  58. else
  59. {
  60. foreach(k,v in avar) {
  61. /* class and instance do not work with this code
  62. if(isTable){
  63. if(avar.rawin(k)) {
  64. myvars.push([k,v]);
  65. }
  66. }
  67. else */
  68. myvars.push([k,v]);
  69. }
  70. }
  71. myvars.sort(@(a,b) a[0] <=> b[0]);
  72. foreach(v in myvars) {
  73. local vtype = type(v[1]);
  74. if(prefix) print1(prefix);
  75. local vvalue = "";
  76. try {
  77. if(vtype == "function"){
  78. local infos = v[1].getinfos();
  79. if(infos.native){
  80. vvalue = getParamsCheck(infos.typecheck);
  81. }
  82. }
  83. else
  84. {
  85. vvalue = v[1].tostring();
  86. if(vvalue[0] == '(') vvalue = "";
  87. }
  88. } catch(e) {}
  89. if(v[0] == base_class_key) {
  90. vtype = "";
  91. if(vvalue == "") vvalue = "NONE";
  92. }
  93. print(v[0], vtype, vvalue);
  94. if(vtype == "class" || vtype == "table") showVars(v[1], [], prefix ? prefix + "\t" : "\t");
  95. }
  96. }
  97. //showVars(this, []);
  98. local function hideFromGlobals(){
  99. /*
  100. There is a bug in the language that when a class implements the metamethod _get
  101. that expects other types than string make impossible to call default delegate functions
  102. like here blob/file/std_stream implements _get expecting a number (integer|float).
  103. local instBlob = blob();
  104. local weakv = instBlob.weakref();
  105. */
  106. local inst_klass = Decimal(); //SlaveVM(1024);
  107. local weakv = inst_klass.weakref();
  108. local intv = 2;
  109. local tblv = {};
  110. local arrayv = [];
  111. local str = "";
  112. local coro = ::newthread(showVars);
  113. local function geny(n){
  114. for(local i=0;i<n;i+=1)
  115. yield i;
  116. return null;
  117. }
  118. local gtor=geny(10);
  119. local myvars = [
  120. ["array_delegate", arrayv.getdelegate()],
  121. ["class_delegate", getdefaultdelegate(blob)],
  122. ["instance_delegate", getdefaultdelegate(inst_klass)],
  123. ["closure_delegate", getdefaultdelegate(showVars)],
  124. ["generator_delegate", getdefaultdelegate(gtor)],
  125. ["number_delegate", getdefaultdelegate(intv)],
  126. ["string_delegate", str.getdelegate()],
  127. ["table_delegate", getdefaultdelegate(tblv)],
  128. ["thread_delegate", getdefaultdelegate(coro)],
  129. ["weakref_delegate", getdefaultdelegate(weakv)],
  130. ];
  131. showVars(this, myvars);
  132. print("\n<<<Constants>>>\n")
  133. local const_array = [];
  134. foreach(k,v in getconsttable()){
  135. const_array.push([k,v]);
  136. }
  137. const_array.sort(@(a,b) a[0] <=> b[0]);
  138. foreach(v in const_array){
  139. print(v[0], v[1]);
  140. }
  141. }
  142. hideFromGlobals();