writable2.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 CLEAR writable on a SHARED SimpleTypeHandler, we fail to ChangeType, thus fail to invalidate cache.
  15. // This test exploits the bug with PropertyString cache.
  16. //
  17. // Get a property string to manipulate with it
  18. function getPropertyString(x, name) {
  19. var names = Object.getOwnPropertyNames(f);
  20. var i = names.indexOf(name);
  21. return names[i];
  22. }
  23. function changePrototype(f, expectedSucceed, msg) {
  24. var tmp = new Object();
  25. // This exploits the PropertyString fast path in OP_SetElementI
  26. var proto = getPropertyString(f, "prototype");
  27. f[proto] = tmp;
  28. var succeeded = (f.prototype === tmp);
  29. assert(succeeded === expectedSucceed, msg);
  30. }
  31. // Initially we use a shared SimpleTypeHandler with "prototype" property for a function.
  32. function f() { }
  33. changePrototype(f, true, "Should be able to change f.prototype initially");
  34. Object.defineProperty(f, "prototype", { writable: false });
  35. changePrototype(f, false, "f.prototype is now not writable, shouldn't be changed");
  36. endTest();