test_proxy_expandos.html (2635B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=965992 5 --> 6 <head> 7 <meta charset="utf-8"> 8 <title>Test for Bug 965992</title> 9 <script 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=965992">Mozilla Bug 965992</a> 14 <p id="display"></p> 15 <form id="theform"></form> 16 <pre id="test"> 17 <script type="application/javascript"> 18 19 // Ensure we are in JIT code and attach IC stubs. 20 const iterations = 50; 21 22 function testFoo(obj, kind, expected) { 23 for (var i = 0; i < iterations; i++) { 24 obj.foo = i; 25 is(obj.foo, (expected === undefined) ? i : expected, 26 "Looking up an expando should work - " + kind); 27 } 28 } 29 30 function getPropTests(obj) { 31 // Start with a plain data property. 32 obj.foo = "bar"; 33 testFoo(obj, "plain"); 34 35 // Now change it to a scripted getter/setter. 36 var count = 0; 37 var getterSetterVal = 0; 38 Object.defineProperty(obj, "foo", {configurable: true, get() { 39 is(this, obj, "Getter should have the proxy as |this|"); 40 is(arguments.length, 0, "Shouldn't pass arguments to getters"); 41 count++; 42 return getterSetterVal; 43 }, set(v) { 44 is(this, obj, "Setter should have the proxy as |this|"); 45 is(arguments.length, 1, "Should pass 1 argument to setters"); 46 getterSetterVal = v; 47 count++; 48 }}); 49 testFoo(obj, "scripted getter/setter"); 50 is(count, iterations * 2, "Should have called the getter/setter enough times"); 51 52 // Now try a native getter/setter. 53 Object.defineProperty(obj, "foo", {get: Math.abs, set: Math.abs, configurable: true}); 54 testFoo(obj, "native getter/setter", NaN); 55 } 56 57 function getElemTests(obj) { 58 // Define two expando properties, then test inline caches for obj[prop] 59 // correctly guard on prop being the same. 60 var count = 0, getterSetterVal = 0; 61 obj.elem1 = 1; 62 Object.defineProperty(obj, "elem2", { 63 get() { count++; return getterSetterVal; }, 64 set(v) { getterSetterVal = v; count++; }, 65 }); 66 for (var i = 0; i < iterations; i++) { 67 var prop = ((i & 1) == 0) ? "elem1" : "elem2"; 68 obj[prop] = i; 69 is(obj[prop], i, "Should return correct property value"); 70 } 71 is(count, iterations, "Should have called the getter/setter enough times"); 72 } 73 74 var directExpando = document.getElementsByTagName("*"); 75 var indirectExpando = document.getElementById("theform"); 76 77 getPropTests(directExpando); 78 getPropTests(indirectExpando); 79 80 getElemTests(indirectExpando); 81 getElemTests(directExpando); 82 83 </script> 84 </pre> 85 </body> 86 </html>