Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | * http://creativecommons.org/licenses/publicdomain/ |
michael@0 | 4 | */ |
michael@0 | 5 | |
michael@0 | 6 | var BUGNUMBER = 640072; |
michael@0 | 7 | var summary = |
michael@0 | 8 | "Represent /a/.{lastIndex,global,source,multiline,sticky,ignoreCase} with " + |
michael@0 | 9 | "plain old data properties"; |
michael@0 | 10 | |
michael@0 | 11 | print(BUGNUMBER + ": " + summary); |
michael@0 | 12 | |
michael@0 | 13 | /************** |
michael@0 | 14 | * BEGIN TEST * |
michael@0 | 15 | **************/ |
michael@0 | 16 | |
michael@0 | 17 | function checkDataProperty(obj, p, expect, msg) |
michael@0 | 18 | { |
michael@0 | 19 | var d = Object.getOwnPropertyDescriptor(obj, p); |
michael@0 | 20 | |
michael@0 | 21 | assertEq(d.value, expect.value, msg + ": bad value for " + p); |
michael@0 | 22 | assertEq(d.writable, expect.writable, msg + ": bad writable for " + p); |
michael@0 | 23 | assertEq(d.enumerable, expect.enumerable, msg + ": bad enumerable for " + p); |
michael@0 | 24 | assertEq(d.configurable, expect.configurable, msg + ": bad configurable for " + p); |
michael@0 | 25 | |
michael@0 | 26 | // Try redefining the property using its initial values: these should all be |
michael@0 | 27 | // silent no-ops. |
michael@0 | 28 | Object.defineProperty(obj, p, { value: expect.value }); |
michael@0 | 29 | Object.defineProperty(obj, p, { writable: expect.writable }); |
michael@0 | 30 | Object.defineProperty(obj, p, { enumerable: expect.enumerable }); |
michael@0 | 31 | Object.defineProperty(obj, p, { configurable: expect.configurable }); |
michael@0 | 32 | |
michael@0 | 33 | var d2 = Object.getOwnPropertyDescriptor(obj, p); |
michael@0 | 34 | assertEq(d.value, d2.value, msg + ": value changed on redefinition of " + p + "?"); |
michael@0 | 35 | assertEq(d.writable, d2.writable, msg + ": writable changed on redefinition of " + p + "?"); |
michael@0 | 36 | assertEq(d.enumerable, d2.enumerable, msg + ": enumerable changed on redefinition of " + p + "?"); |
michael@0 | 37 | assertEq(d.configurable, d2.configurable, msg + ": configurable changed on redefinition of " + p + "?"); |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | |
michael@0 | 41 | // Check a bunch of "empty" regular expressions first. |
michael@0 | 42 | |
michael@0 | 43 | var choices = [{ msg: "RegExp.prototype", |
michael@0 | 44 | get: function() { return RegExp.prototype; } }, |
michael@0 | 45 | { msg: "new RegExp()", |
michael@0 | 46 | get: function() { return new RegExp(); } }, |
michael@0 | 47 | { msg: "/(?:)/", |
michael@0 | 48 | get: Function("return /(?:)/;") }]; |
michael@0 | 49 | |
michael@0 | 50 | function checkRegExp(r, msg, lastIndex, global, ignoreCase, multiline) |
michael@0 | 51 | { |
michael@0 | 52 | var expect; |
michael@0 | 53 | |
michael@0 | 54 | expect = { value: lastIndex, enumerable: false, configurable: false, writable: true }; |
michael@0 | 55 | checkDataProperty(r, "lastIndex", expect, msg); |
michael@0 | 56 | |
michael@0 | 57 | // check source specially: its value is under-defined in the spec |
michael@0 | 58 | var d = Object.getOwnPropertyDescriptor(r, "source"); |
michael@0 | 59 | assertEq(d.writable, false, "bad writable: " + msg); |
michael@0 | 60 | assertEq(d.enumerable, false, "bad enumerable: " + msg); |
michael@0 | 61 | assertEq(d.configurable, false, "bad configurable: " + msg); |
michael@0 | 62 | |
michael@0 | 63 | expect = { value: global, enumerable: false, configurable: false, writable: false }; |
michael@0 | 64 | checkDataProperty(r, "global", expect, msg); |
michael@0 | 65 | |
michael@0 | 66 | expect = { value: ignoreCase, enumerable: false, configurable: false, writable: false }; |
michael@0 | 67 | checkDataProperty(r, "ignoreCase", expect, msg); |
michael@0 | 68 | |
michael@0 | 69 | expect = { value: multiline, enumerable: false, configurable: false, writable: false }; |
michael@0 | 70 | checkDataProperty(r, "multiline", expect, msg); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | checkRegExp(RegExp.prototype, "RegExp.prototype", 0, false, false, false); |
michael@0 | 74 | checkRegExp(new RegExp(), "new RegExp()", 0, false, false, false); |
michael@0 | 75 | checkRegExp(/(?:)/, "/(?:)/", 0, false, false, false); |
michael@0 | 76 | checkRegExp(Function("return /(?:)/;")(), 'Function("return /(?:)/;")()', 0, false, false, false); |
michael@0 | 77 | |
michael@0 | 78 | for (var i = 0; i < choices.length; i++) |
michael@0 | 79 | { |
michael@0 | 80 | var choice = choices[i]; |
michael@0 | 81 | var msg = choice.msg; |
michael@0 | 82 | var r = choice.get(); |
michael@0 | 83 | |
michael@0 | 84 | checkRegExp(r, msg, 0, false, false, false); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | // Now test less generic regular expressions |
michael@0 | 88 | |
michael@0 | 89 | checkRegExp(/a/gim, "/a/gim", 0, true, true, true); |
michael@0 | 90 | |
michael@0 | 91 | var r; |
michael@0 | 92 | |
michael@0 | 93 | do |
michael@0 | 94 | { |
michael@0 | 95 | r = /abcd/mg; |
michael@0 | 96 | checkRegExp(r, "/abcd/mg initially", 0, true, false, true); |
michael@0 | 97 | r.exec("abcdefg"); |
michael@0 | 98 | checkRegExp(r, "/abcd/mg step 1", 4, true, false, true); |
michael@0 | 99 | r.exec("abcdabcd"); |
michael@0 | 100 | checkRegExp(r, "/abcd/mg step 2", 8, true, false, true); |
michael@0 | 101 | r.exec("abcdabcd"); |
michael@0 | 102 | checkRegExp(r, "/abcd/mg end", 0, true, false, true); |
michael@0 | 103 | |
michael@0 | 104 | r = /cde/ig; |
michael@0 | 105 | checkRegExp(r, "/cde/ig initially", 0, true, true, false); |
michael@0 | 106 | var obj = r.lastIndex = { valueOf: function() { return 2; } }; |
michael@0 | 107 | checkRegExp(r, "/cde/ig after lastIndex", obj, true, true, false); |
michael@0 | 108 | r.exec("aaacdef"); |
michael@0 | 109 | checkRegExp(r, "/cde/ig after exec", 6, true, true, false); |
michael@0 | 110 | Object.defineProperty(r, "lastIndex", { value: 3 }); |
michael@0 | 111 | checkRegExp(r, "/cde/ig after define 3", 3, true, true, false); |
michael@0 | 112 | Object.defineProperty(r, "lastIndex", { value: obj }); |
michael@0 | 113 | checkRegExp(r, "/cde/ig after lastIndex", obj, true, true, false); |
michael@0 | 114 | |
michael@0 | 115 | |
michael@0 | 116 | // Tricky bits of testing: make sure that redefining lastIndex doesn't change |
michael@0 | 117 | // the slot where the lastIndex property is initially stored, even if |
michael@0 | 118 | // the redefinition also changes writability. |
michael@0 | 119 | r = /a/g; |
michael@0 | 120 | checkRegExp(r, "/a/g initially", 0, true, false, false); |
michael@0 | 121 | Object.defineProperty(r, "lastIndex", { value: 2 }); |
michael@0 | 122 | r.exec("aabbbba"); |
michael@0 | 123 | checkRegExp(r, "/a/g after first exec", 7, true, false, false); |
michael@0 | 124 | assertEq(r.lastIndex, 7); |
michael@0 | 125 | r.lastIndex = 2; |
michael@0 | 126 | checkRegExp(r, "/a/g after assign", 2, true, false, false); |
michael@0 | 127 | r.exec("aabbbba"); |
michael@0 | 128 | assertEq(r.lastIndex, 7); // check in reverse order |
michael@0 | 129 | checkRegExp(r, "/a/g after second exec", 7, true, false, false); |
michael@0 | 130 | |
michael@0 | 131 | r = /c/g; |
michael@0 | 132 | r.lastIndex = 2; |
michael@0 | 133 | checkRegExp(r, "/c/g initially", 2, true, false, false); |
michael@0 | 134 | Object.defineProperty(r, "lastIndex", { writable: false }); |
michael@0 | 135 | assertEq(Object.getOwnPropertyDescriptor(r, "lastIndex").writable, false); |
michael@0 | 136 | try { r.exec("aabbbba"); } catch (e) { /* swallow error if thrown */ } |
michael@0 | 137 | assertEq(Object.getOwnPropertyDescriptor(r, "lastIndex").writable, false); |
michael@0 | 138 | assertEq(Object.getOwnPropertyDescriptor(r, "source").writable, false); |
michael@0 | 139 | assertEq(Object.getOwnPropertyDescriptor(r, "global").value, true); |
michael@0 | 140 | assertEq(Object.getOwnPropertyDescriptor(r, "global").writable, false); |
michael@0 | 141 | assertEq(Object.getOwnPropertyDescriptor(r, "ignoreCase").value, false); |
michael@0 | 142 | assertEq(Object.getOwnPropertyDescriptor(r, "ignoreCase").writable, false); |
michael@0 | 143 | assertEq(Object.getOwnPropertyDescriptor(r, "multiline").value, false); |
michael@0 | 144 | assertEq(Object.getOwnPropertyDescriptor(r, "multiline").writable, false); |
michael@0 | 145 | } |
michael@0 | 146 | while (Math.random() > 17); // fake loop to discourage RegExp object caching |
michael@0 | 147 | |
michael@0 | 148 | /******************************************************************************/ |
michael@0 | 149 | |
michael@0 | 150 | if (typeof reportCompare === "function") |
michael@0 | 151 | reportCompare(true, true); |
michael@0 | 152 | |
michael@0 | 153 | print("All tests passed!"); |