writable3.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. //
  6. // Test changing [writable] attribute should trigger Type transition.
  7. //
  8. var echo = this.WScript ? WScript.Echo : function () { console.log([].join.apply(arguments, [", "])); };
  9. function assert(value, msg) { if (!value) { throw new Error("Failed: " + msg); } }
  10. function endTest() { echo("pass"); }
  11. //
  12. // Win8: 713428
  13. //
  14. // When SET writable on a NON-shared SimpleTypeHandler, we fail to ChangeType, thus fail to invalidate cache.
  15. // This test exploits the bug with inline cache.
  16. //
  17. function changePrototype(f, expectedSucceed, msg) {
  18. var tmp = new Object();
  19. // This exploits inline cache
  20. f.prototype = tmp;
  21. var succeeded = (f.prototype === tmp);
  22. assert(succeeded === expectedSucceed, msg);
  23. }
  24. // Make f to use a NON-shared SimpleTypeHandler
  25. var f = new Boolean(true); // NullTypeHandler
  26. f.prototype = 12; // Evolve to Non-shared SimpleTypeHandler
  27. Object.defineProperty(f, "prototype", { writable: false }); // Clear writable
  28. changePrototype(f, false, "Should not be able to change f.prototype, writable false");
  29. Object.defineProperty(f, "prototype", { writable: true }); // SET writable
  30. changePrototype(f, true, "f.prototype is now writable, should be changed");
  31. endTest();