michael@0: // String.prototype[@@iterator] and StringIterator.prototype surface tests michael@0: michael@0: load(libdir + "array-compare.js"); michael@0: load(libdir + "asserts.js"); michael@0: load(libdir + "iteration.js"); michael@0: michael@0: function assertDataDescriptor(actual, expected) { michael@0: assertEq(actual.value, expected.value); michael@0: assertEq(actual.writable, expected.writable); michael@0: assertEq(actual.enumerable, expected.enumerable); michael@0: assertEq(actual.configurable, expected.configurable); michael@0: } michael@0: michael@0: function isConstructor(o) { michael@0: try { michael@0: new (new Proxy(o, {construct: () => ({})})); michael@0: return true; michael@0: } catch(e) { michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: function assertBuiltinFunction(o, name, arity) { michael@0: var fn = o[name]; michael@0: assertDataDescriptor(Object.getOwnPropertyDescriptor(o, name), { michael@0: value: fn, michael@0: writable: true, michael@0: enumerable: false, michael@0: configurable: true, michael@0: }); michael@0: michael@0: assertEq(typeof fn, "function"); michael@0: assertEq(Object.getPrototypeOf(fn), Function.prototype); michael@0: // FIXME: Proxy should only have [[Construct]] if target has [[Construct]] (bug 929467) michael@0: // assertEq(isConstructor(fn), false); michael@0: michael@0: arraysEqual(Object.getOwnPropertyNames(fn).sort(), ["length", "name", "arguments", "caller"].sort()); michael@0: michael@0: // Also test "name", "arguments" and "caller" in addition to "length"? michael@0: assertDataDescriptor(Object.getOwnPropertyDescriptor(fn, "length"), { michael@0: value: arity, michael@0: writable: false, michael@0: enumerable: false, michael@0: configurable: false, michael@0: }); michael@0: } michael@0: michael@0: michael@0: // String.prototype[@@iterator] is a built-in function michael@0: assertBuiltinFunction(String.prototype, std_iterator, 0); michael@0: michael@0: // Test StringIterator.prototype surface michael@0: var iter = ""[std_iterator](); michael@0: var iterProto = Object.getPrototypeOf(iter); michael@0: michael@0: // StringIterator.prototype inherits from Object.prototype michael@0: assertEq(Object.getPrototypeOf(iterProto), Object.prototype); michael@0: michael@0: // Own properties for StringIterator.prototype: "next" and @@iterator michael@0: arraysEqual(Object.getOwnPropertyNames(iterProto).sort(), ["next", std_iterator].sort()); michael@0: michael@0: // StringIterator.prototype[@@iterator] is a built-in function michael@0: assertBuiltinFunction(iterProto, std_iterator, 0); michael@0: michael@0: // StringIterator.prototype.next is a built-in function michael@0: assertBuiltinFunction(iterProto, "next", 0); michael@0: michael@0: // StringIterator.prototype[@@iterator] is generic and returns |this| michael@0: for (var v of [void 0, null, true, false, "", 0, 1, {}, [], iter, iterProto]) { michael@0: assertEq(iterProto[std_iterator].call(v), v); michael@0: } michael@0: michael@0: // StringIterator.prototype.next is not generic michael@0: for (var v of [void 0, null, true, false, "", 0, 1, {}, [], iterProto]) { michael@0: assertThrowsInstanceOf(() => iterProto.next.call(v), TypeError); michael@0: }