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 | // String.prototype[@@iterator] and StringIterator.prototype surface tests |
michael@0 | 2 | |
michael@0 | 3 | load(libdir + "array-compare.js"); |
michael@0 | 4 | load(libdir + "asserts.js"); |
michael@0 | 5 | load(libdir + "iteration.js"); |
michael@0 | 6 | |
michael@0 | 7 | function assertDataDescriptor(actual, expected) { |
michael@0 | 8 | assertEq(actual.value, expected.value); |
michael@0 | 9 | assertEq(actual.writable, expected.writable); |
michael@0 | 10 | assertEq(actual.enumerable, expected.enumerable); |
michael@0 | 11 | assertEq(actual.configurable, expected.configurable); |
michael@0 | 12 | } |
michael@0 | 13 | |
michael@0 | 14 | function isConstructor(o) { |
michael@0 | 15 | try { |
michael@0 | 16 | new (new Proxy(o, {construct: () => ({})})); |
michael@0 | 17 | return true; |
michael@0 | 18 | } catch(e) { |
michael@0 | 19 | return false; |
michael@0 | 20 | } |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | function assertBuiltinFunction(o, name, arity) { |
michael@0 | 24 | var fn = o[name]; |
michael@0 | 25 | assertDataDescriptor(Object.getOwnPropertyDescriptor(o, name), { |
michael@0 | 26 | value: fn, |
michael@0 | 27 | writable: true, |
michael@0 | 28 | enumerable: false, |
michael@0 | 29 | configurable: true, |
michael@0 | 30 | }); |
michael@0 | 31 | |
michael@0 | 32 | assertEq(typeof fn, "function"); |
michael@0 | 33 | assertEq(Object.getPrototypeOf(fn), Function.prototype); |
michael@0 | 34 | // FIXME: Proxy should only have [[Construct]] if target has [[Construct]] (bug 929467) |
michael@0 | 35 | // assertEq(isConstructor(fn), false); |
michael@0 | 36 | |
michael@0 | 37 | arraysEqual(Object.getOwnPropertyNames(fn).sort(), ["length", "name", "arguments", "caller"].sort()); |
michael@0 | 38 | |
michael@0 | 39 | // Also test "name", "arguments" and "caller" in addition to "length"? |
michael@0 | 40 | assertDataDescriptor(Object.getOwnPropertyDescriptor(fn, "length"), { |
michael@0 | 41 | value: arity, |
michael@0 | 42 | writable: false, |
michael@0 | 43 | enumerable: false, |
michael@0 | 44 | configurable: false, |
michael@0 | 45 | }); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | |
michael@0 | 49 | // String.prototype[@@iterator] is a built-in function |
michael@0 | 50 | assertBuiltinFunction(String.prototype, std_iterator, 0); |
michael@0 | 51 | |
michael@0 | 52 | // Test StringIterator.prototype surface |
michael@0 | 53 | var iter = ""[std_iterator](); |
michael@0 | 54 | var iterProto = Object.getPrototypeOf(iter); |
michael@0 | 55 | |
michael@0 | 56 | // StringIterator.prototype inherits from Object.prototype |
michael@0 | 57 | assertEq(Object.getPrototypeOf(iterProto), Object.prototype); |
michael@0 | 58 | |
michael@0 | 59 | // Own properties for StringIterator.prototype: "next" and @@iterator |
michael@0 | 60 | arraysEqual(Object.getOwnPropertyNames(iterProto).sort(), ["next", std_iterator].sort()); |
michael@0 | 61 | |
michael@0 | 62 | // StringIterator.prototype[@@iterator] is a built-in function |
michael@0 | 63 | assertBuiltinFunction(iterProto, std_iterator, 0); |
michael@0 | 64 | |
michael@0 | 65 | // StringIterator.prototype.next is a built-in function |
michael@0 | 66 | assertBuiltinFunction(iterProto, "next", 0); |
michael@0 | 67 | |
michael@0 | 68 | // StringIterator.prototype[@@iterator] is generic and returns |this| |
michael@0 | 69 | for (var v of [void 0, null, true, false, "", 0, 1, {}, [], iter, iterProto]) { |
michael@0 | 70 | assertEq(iterProto[std_iterator].call(v), v); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | // StringIterator.prototype.next is not generic |
michael@0 | 74 | for (var v of [void 0, null, true, false, "", 0, 1, {}, [], iterProto]) { |
michael@0 | 75 | assertThrowsInstanceOf(() => iterProto.next.call(v), TypeError); |
michael@0 | 76 | } |