test_bug793969.html 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=793969
  5. -->
  6. <head>
  7. <meta charset="utf-8">
  8. <title>Test for Bug 793969</title>
  9. <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  10. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  11. </head>
  12. <body>
  13. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=793969">Mozilla Bug 793969</a>
  14. <p id="display"></p>
  15. <div id="content" style="display: none">
  16. </div>
  17. <pre id="test">
  18. <script type="application/javascript">
  19. /** Test for Bug 793969 **/
  20. function checkThrows(f, desc, skipMessageCheck) {
  21. try {
  22. f();
  23. ok(false, "Should have thrown for " + desc);
  24. } catch (e) {
  25. ok(true, "threw correctly");
  26. if (!skipMessageCheck)
  27. ok(/denied/.exec(e) ||
  28. /can't redefine non-configurable property/.exec(e),
  29. "Correctly threw a security exception: " + e);
  30. }
  31. }
  32. // NB: These sets will be no-ops (throw in strict mode) because setting an inherited readonly value prop has those semantics.
  33. checkThrows(function() { "use strict"; location.valueOf = 'hah'; }, 'Shadow with string', /* skipMessageCheck = */ true);
  34. checkThrows(function() { "use strict"; location.valueOf = function() { return {a: 'hah'};} }, 'Shadow with function', /* skipMessageCheck = */ true);
  35. checkThrows(function() { Object.defineProperty(location, 'valueOf', { value: function() { return 'hah'; } }); }, 'defineProperty with value');
  36. checkThrows(function() { delete location.valueOf; Object.defineProperty(location, 'valueOf', { value: function() { return 'hah'; } }); }, 'delete + defineProperty with value');
  37. checkThrows(function() { Object.defineProperty(location, 'valueOf', { get: function() { return 'hah'; } }); }, 'defineProperty with getter');
  38. checkThrows(function() { delete location.valueOf; Object.defineProperty(location, 'valueOf', { get: function() { return 'hah'; } }); }, 'delete + defineProperty with getter');
  39. Object.prototype.valueOf = function() { return 'hah'; };
  40. is(({}).valueOf(), 'hah', "Shadowing on Object.prototype works for vanilla objects");
  41. is(location.valueOf(), location, "Shadowing on Object.prototype and Location.prototype doesn't for location objects");
  42. location[Symbol.toPrimitive] = function() { return 'hah'; }
  43. is(location + "", location.toString(), "Should't be able to shadow with toPrimitive");
  44. </script>
  45. </pre>
  46. </body>
  47. </html>