2
0

HasOnlyWritableDataPropertiesCache.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. var InvalidationScheme = {
  6. ReadOnlyData: 0,
  7. ReadOnlyAccessor: 1,
  8. ReadWriteAccessors: 2,
  9. CollectGarbage: 3
  10. };
  11. function GenerateTest(
  12. useIndexProperties,
  13. numSetsBeforeCacheInvalidation,
  14. invalidationScheme,
  15. numSetsAfterCacheInvalidation) {
  16. function PropertyName(propertyIndex) {
  17. var propertyName = "";
  18. if(!useIndexProperties)
  19. propertyName += "p";
  20. return propertyName + propertyIndex;
  21. }
  22. f = "var proto = {};";
  23. for(var i = 0; i < numSetsBeforeCacheInvalidation + numSetsAfterCacheInvalidation; ++i) {
  24. var propertyName = PropertyName(i);
  25. f += "proto[" + propertyName + "] = " + i + ";";
  26. }
  27. f += "var Construct = function(){};";
  28. f += "Construct.prototype = proto;";
  29. f += "var o = new Construct;";
  30. for(var i = 0; i < numSetsBeforeCacheInvalidation; ++i) {
  31. var propertyName = PropertyName(i);
  32. f += "o[" + propertyName + "] = " + i + ";";
  33. }
  34. var propertyName = PropertyName(numSetsBeforeCacheInvalidation);
  35. if(invalidationScheme == InvalidationScheme.ReadOnlyData)
  36. f += "Object.defineProperty(proto, \"" + propertyName + "\", {writable: false});";
  37. else if(invalidationScheme == InvalidationScheme.ReadOnlyAccessor)
  38. f += "Object.defineProperty(proto, \"" + propertyName + "\", {get: function(){return 0;}});";
  39. else if(invalidationScheme == InvalidationScheme.ReadWriteAccessors) {
  40. f += "Object.defineProperty(proto, \"" + propertyName + "\", {";
  41. f += "get: function(){return 0;},";
  42. f += "set: function(v){}";
  43. f += "});";
  44. } else if(invalidationScheme == InvalidationScheme.CollectGarbage)
  45. f += "CollectGarbage();";
  46. for(var i = numSetsBeforeCacheInvalidation; i < numSetsAfterCacheInvalidation; ++i) {
  47. var propertyName = PropertyName(i);
  48. f += "o[" + propertyName + "] = " + i + ";";
  49. }
  50. f += "echo(\"proto: \", proto);";
  51. f += "echo(\"o: \", o);";
  52. echo(f);
  53. return f;
  54. }
  55. var bools = [false, true];
  56. for(var useIndexPropertiesPropertyName in bools) {
  57. var useIndexProperties = bools[useIndexPropertiesPropertyName];
  58. for(var numSetsBeforeCacheInvalidation = 0; numSetsBeforeCacheInvalidation <= 3; ++numSetsBeforeCacheInvalidation) {
  59. for(var invalidationSchemePropertyName in InvalidationScheme) {
  60. var invalidationScheme = InvalidationScheme[invalidationSchemePropertyName];
  61. for(var numSetsAfterCacheInvalidation = 0;
  62. numSetsAfterCacheInvalidation <= 3;
  63. ++numSetsAfterCacheInvalidation) {
  64. safeCall(
  65. eval,
  66. GenerateTest(
  67. useIndexProperties,
  68. numSetsBeforeCacheInvalidation,
  69. invalidationScheme,
  70. numSetsAfterCacheInvalidation));
  71. }
  72. }
  73. }
  74. }
  75. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  76. function myToString(o, quoteStrings) {
  77. switch(o) {
  78. case null:
  79. case undefined:
  80. return "" + o;
  81. }
  82. switch(typeof o) {
  83. case "boolean":
  84. return "" + o;
  85. case "number":
  86. {
  87. var s = "" + o;
  88. var i = s.indexOf("e");
  89. var end = i === -1 ? s.length : e;
  90. i = s.indexOf(".");
  91. var start = i === -1 ? 0 : i + 1;
  92. if(start !== 0) {
  93. if(end % 3 !== 0)
  94. end += 3 - end % 3;
  95. for(i = end - 3; i > start; i -= 3)
  96. s = s.substring(0, i) + "," + s.substring(i);
  97. end = start - 1;
  98. start = 0;
  99. }
  100. for(i = end - 3; i > start; i -= 3)
  101. s = s.substring(0, i) + "," + s.substring(i);
  102. return s;
  103. }
  104. case "string":
  105. {
  106. var hex = "0123456789abcdef";
  107. var s = "";
  108. for(var i = 0; i < o.length; ++i) {
  109. var c = o.charCodeAt(i);
  110. switch(c) {
  111. case 0x0:
  112. s += "\\0";
  113. continue;
  114. case 0x8:
  115. s += "\\b";
  116. continue;
  117. case 0xb:
  118. s += "\\v";
  119. continue;
  120. case 0xc:
  121. s += "\\f";
  122. continue;
  123. }
  124. if(quoteStrings) {
  125. switch(c) {
  126. case 0x9:
  127. s += "\\t";
  128. continue;
  129. case 0xa:
  130. s += "\\n";
  131. continue;
  132. case 0xd:
  133. s += "\\r";
  134. continue;
  135. case 0x22:
  136. s += "\\\"";
  137. continue;
  138. case 0x5c:
  139. s += "\\\\";
  140. continue;
  141. }
  142. }
  143. if(c >= 0x20 && c < 0x7f)
  144. s += o.charAt(i);
  145. else if(c <= 0xff)
  146. s += "\\x" + hex.charAt((c >> 4) & 0xf) + hex.charAt(c & 0xf);
  147. else
  148. s += "\\u" + hex.charAt((c >> 12) & 0xf) + hex.charAt((c >> 8) & 0xf) + hex.charAt((c >> 4) & 0xf) + hex.charAt(c & 0xf);
  149. }
  150. if(quoteStrings)
  151. s = "\"" + s + "\"";
  152. return s;
  153. }
  154. case "object":
  155. case "function":
  156. break;
  157. default:
  158. return "<unknown type '" + typeof o + "'>";
  159. }
  160. if(o instanceof Array) {
  161. var s = "[";
  162. for(var i = 0; i < o.length; ++i) {
  163. if(i)
  164. s += ", ";
  165. s += myToString(o[i], true);
  166. }
  167. return s + "]";
  168. }
  169. if(o instanceof Error)
  170. return o.name + ": " + o.message;
  171. if(o instanceof RegExp)
  172. return o.toString() + (o.lastIndex === 0 ? "" : " (lastIndex: " + o.lastIndex + ")");
  173. if(o instanceof Object) {
  174. var s = "";
  175. for(var p in o)
  176. s += myToString(p) + ": " + myToString(o[p], true) + ", ";
  177. if(s.length !== 0)
  178. s = s.substring(0, s.length - ", ".length);
  179. return "{" + s + "}";
  180. }
  181. return "" + o;
  182. }
  183. function echo() {
  184. var doEcho;
  185. if(this.WScript)
  186. doEcho = function (s) { this.WScript.Echo(s); };
  187. else if(this.document)
  188. doEcho =
  189. function (s) {
  190. s = s
  191. .replace(/&/g, "&&")
  192. .replace(/</g, "&lt;")
  193. .replace(/>/g, "&gt;");
  194. this.document.write(s + "<br/>");
  195. };
  196. else
  197. doEcho = function (s) { this.print(s); };
  198. echo =
  199. function () {
  200. var s = "";
  201. for(var i = 0; i < arguments.length; ++i)
  202. s += myToString(arguments[i]);
  203. doEcho(s);
  204. };
  205. echo.apply(this, arguments);
  206. }
  207. function safeCall(f) {
  208. var args = [];
  209. for(var a = 1; a < arguments.length; ++a)
  210. args.push(arguments[a]);
  211. try {
  212. return f.apply(this, args);
  213. } catch(ex) {
  214. echo(ex);
  215. }
  216. }