summaryrefslogtreecommitdiffstats
path: root/dom/bindings/test/test_proxy_expandos.html
blob: bfe6ab598ced40900d72200573f2c1386f293d6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=965992
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 965992</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=965992">Mozilla Bug 965992</a>
<p id="display"></p>
<form id="theform"></form>
<pre id="test">
<script type="application/javascript">

// Ensure we are in JIT code and attach IC stubs.
const iterations = 50;

function testFoo(obj, kind, expected) {
  for (var i = 0; i < iterations; i++) {
    obj.foo = i;
    is(obj.foo, (expected === undefined) ? i : expected,
       "Looking up an expando should work - " + kind);
  }
}

function getPropTests(obj) {
  // Start with a plain data property.
  obj.foo = "bar";
  testFoo(obj, "plain");

  // Now change it to a scripted getter/setter.
  var count = 0;
  var getterSetterVal = 0;
  Object.defineProperty(obj, "foo", {configurable: true, get() {
    is(this, obj, "Getter should have the proxy as |this|");
    is(arguments.length, 0, "Shouldn't pass arguments to getters");
    count++;
    return getterSetterVal;
  }, set(v) {
    is(this, obj, "Setter should have the proxy as |this|");
    is(arguments.length, 1, "Should pass 1 argument to setters");
    getterSetterVal = v;
    count++;
  }});
  testFoo(obj, "scripted getter/setter");
  is(count, iterations * 2, "Should have called the getter/setter enough times");

  // Now try a native getter/setter.
  Object.defineProperty(obj, "foo", {get: Math.abs, set: Math.abs, configurable: true});
  testFoo(obj, "native getter/setter", NaN);
}

function getElemTests(obj) {
  // Define two expando properties, then test inline caches for obj[prop]
  // correctly guard on prop being the same.
  var count = 0, getterSetterVal = 0;
  obj.elem1 = 1;
  Object.defineProperty(obj, "elem2", {
                        get() { count++; return getterSetterVal; },
                        set(v) { getterSetterVal = v; count++; },
  });
  for (var i = 0; i < iterations; i++) {
    var prop = ((i & 1) == 0) ? "elem1" : "elem2";
    obj[prop] = i;
    is(obj[prop], i, "Should return correct property value");
  }
  is(count, iterations, "Should have called the getter/setter enough times");
}

var directExpando = document.getElementsByTagName("*");
var indirectExpando = document.getElementById("theform");

getPropTests(directExpando);
getPropTests(indirectExpando);

getElemTests(indirectExpando);
getElemTests(directExpando);

</script>
</pre>
</body>
</html>