profiledataobject.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. function constantLookup(val, table) {
  6. for (var prop in table) {
  7. if (table[prop] == val)
  8. return prop;
  9. }
  10. // Special case for ImplicitCallFlags
  11. if(table.Accessor)
  12. {
  13. var flags = [];
  14. if(val & table.None) flags.push("None");
  15. if((val & table.ToPrimitive) != table.None) flags.push("ToPrimitive");
  16. if((val & table.Accessor) != table.None) flags.push("Accessor");
  17. if((val & table.External) != table.None) flags.push("External");
  18. if((val & table.Exception) != table.None) flags.push("Exception");
  19. if(val == table.All) flags.push("All");
  20. if(val & table.Dispose) flags.push("Dispose");
  21. return flags.join(" | ");
  22. }
  23. // Special case for ValueType - unset all but the most significant type bit, preserve the 'likely' bit, exclude the array
  24. // detail bits, and look it up again
  25. if(table.hasOwnProperty("LikelyBit")) {
  26. var highestTypeBitIndex = table.VALUE_TYPE_COMMON_BIT_COUNT - 1;
  27. if(val & table.ObjectBit)
  28. val &= ~(((1 << table.VALUE_TYPE_OBJECT_BIT_COUNT) - 1) ^ ((1 << table.VALUE_TYPE_COMMON_BIT_COUNT) - 1))
  29. else
  30. highestTypeBitIndex += table.VALUE_TYPE_NONOBJECT_BIT_COUNT;
  31. for(var i = highestTypeBitIndex; i >= 0; --i) {
  32. if(val & (1 << i)) {
  33. var generalizedVal = val & (~((1 << i) - 1) | table.LikelyBit);
  34. for (var prop in table) {
  35. if (table[prop] == generalizedVal)
  36. return prop;
  37. }
  38. break;
  39. }
  40. }
  41. }
  42. return "ERROR: constant not found";
  43. }
  44. function dumpProfileData(f, msg) {
  45. WScript.Echo("Profile data for " + msg);
  46. var pdo;
  47. try {
  48. pdo = Debug.getProfileDataObject(f);
  49. }
  50. catch (ex) {
  51. WScript.Echo("No profile data found.");
  52. WScript.Echo("");
  53. WScript.Echo("");
  54. return;
  55. }
  56. if(pdo.returnTypeInfo.length)
  57. WScript.Echo("Return type info:");
  58. for (var i = 0; i < pdo.returnTypeInfo.length; ++i) {
  59. WScript.Echo(" #" + i + ": " + pdo.returnTypeInfo[i] + " (" + constantLookup(pdo.returnTypeInfo[i], pdo.ValueType) + ")");
  60. }
  61. if (pdo.elemInfo.length)
  62. WScript.Echo("Elem info:");
  63. for (var i = 0; i < pdo.elemInfo.length; ++i) {
  64. WScript.Echo(" #" + i + ": " + pdo.elemInfo[i] + " (" + constantLookup(pdo.elemInfo[i], pdo.ValueType) + ")");
  65. }
  66. if(pdo.parameterInfo.length)
  67. WScript.Echo("Param info:")
  68. for (var i = 0; i < pdo.parameterInfo.length; ++i) {
  69. WScript.Echo(" #" + i + ": " + pdo.parameterInfo[i] + " (" + constantLookup(pdo.parameterInfo[i], pdo.ValueType) + ")");
  70. }
  71. WScript.Echo("Implicit call flags:");
  72. WScript.Echo(" #" + i + ": " + pdo.implicitCallFlags + " (" + constantLookup(pdo.implicitCallFlags, pdo.ImplicitCallFlags) + ")");
  73. if(pdo.loopImplicitCallFlags.length)
  74. WScript.Echo("Loop implicit call flags:");
  75. for (var i = 0; i < pdo.loopImplicitCallFlags.length; ++i) {
  76. WScript.Echo(" #" + i + ": " + pdo.loopImplicitCallFlags[i] + " (" + constantLookup(pdo.loopImplicitCallFlags[i], pdo.ImplicitCallFlags) + ")");
  77. }
  78. WScript.Echo("");
  79. WScript.Echo("");
  80. }
  81. function I1(x) {
  82. return x;
  83. }
  84. function I2(x) {
  85. return x;
  86. }
  87. function I3(x) {
  88. return x;
  89. }
  90. function I4(x) {
  91. return x;
  92. }
  93. function test1() {
  94. var sum = I1("test") + I2(123) + I3(0.5) + I4({});
  95. }
  96. test1();
  97. dumpProfileData(test1, "test1");
  98. function test2(a,b,c,d,e,f) {
  99. var sum = 0;
  100. for (var i = 0; i < a.length; ++i) {
  101. sum += a[i];
  102. }
  103. for (var i = 0; i < b.length; ++i) {
  104. sum += b[i];
  105. }
  106. for (var i = 0; i < c.length; ++i) {
  107. sum += c[i];
  108. }
  109. for (var i = 0; i < d.length; ++i) {
  110. sum += d[i];
  111. }
  112. for (var i = 0; i < e.length; ++i) {
  113. sum += e[i];
  114. }
  115. for (var i = 0; i < f.length; ++i) {
  116. sum += f[i];
  117. }
  118. }
  119. test2(
  120. [1, 2, 3, 4, 5],
  121. [-0x80000000, 0x7fffffff],
  122. new Uint8Array(10),
  123. new Float64Array(10),
  124. new Int16Array(10),
  125. [0.3, 0.4, 0.5, 0.6, 0.7]
  126. );
  127. dumpProfileData(test2, "test2");
  128. test2(
  129. [1, 2, 3.5, 4.2, 5],
  130. [0, 0x7fffffff],
  131. new Uint8Array(10),
  132. "a string",
  133. new Int16Array(10),
  134. [0.3, 0.4, 0.5, 0.6, 0.7]
  135. );
  136. dumpProfileData(test2, "test2 - second call");
  137. function test3(a, b, c, d) {
  138. var sum = 0;
  139. for (var i = 0; i < a.p.length; ++i) {
  140. sum += a.p[i];
  141. }
  142. for (var i = 0; i < b.p.length; ++i) {
  143. sum += b.p[i];
  144. }
  145. if (/* false */typeof (c) === "blah") {
  146. sum += c.p[i];
  147. }
  148. }
  149. test3(
  150. { p: new Uint32Array(10) },
  151. { p: [null,,,,] },
  152. 0
  153. );
  154. dumpProfileData(test3, "test3");
  155. // Try manipulating the profile data.
  156. var pdo = Debug.getProfileDataObject(test3);
  157. pdo.parameterInfo[0] = pdo.ValueType.Uninitialized;
  158. pdo.parameterInfo[1] = pdo.ValueType.LikelyTaggedInt;
  159. pdo.parameterInfo[2] = pdo.ValueType.LikelyNumber;
  160. pdo.parameterInfo[3] = pdo.ValueType.LikelyString;
  161. pdo.elemInfo[2] = pdo.ValueType.LikelyFloat64Array;
  162. pdo.loopImplicitCallFlags[0] = pdo.ImplicitCallFlags.ToPrimitive;
  163. dumpProfileData(test3, "test3 - after writing profile data");
  164. function test4(a, b, c) {
  165. var sum = a + b + c;
  166. }
  167. dumpProfileData(test4, "test4 - before call");
  168. test4(
  169. "a string",
  170. 5.3,
  171. 3
  172. );
  173. dumpProfileData(test4, "test4");
  174. test4(
  175. "a string",
  176. 3,
  177. 3
  178. );
  179. dumpProfileData(test4, "test4 - second call");
  180. function test5(a,b) {
  181. var x1 = { valueOf: function () { return 7; } };
  182. var x2 = { get prop() { return 8; } };
  183. var sum = 0;
  184. for (var i = 0; i < 5; ++i) {
  185. }
  186. for (var i = 0; i < 5; ++i) {
  187. sum += x1;
  188. }
  189. for (var i = 0; i < 5; ++i) {
  190. sum += x2.prop;
  191. }
  192. for (var i = 0; i < 5; ++i) {
  193. sum += x1 + x2.prop;
  194. }
  195. }
  196. test5();
  197. dumpProfileData(test5, "test5");