michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: function IteratorIdentity() { michael@0: return this; michael@0: } michael@0: michael@0: var LegacyIteratorWrapperMap = new std_WeakMap(); michael@0: michael@0: function LegacyIteratorNext(arg) { michael@0: var iter = callFunction(std_WeakMap_get, LegacyIteratorWrapperMap, this); michael@0: try { michael@0: return { value: iter.next(arg), done: false }; michael@0: } catch (e) { michael@0: if (e instanceof std_StopIteration) michael@0: return { value: undefined, done: true }; michael@0: throw e; michael@0: } michael@0: } michael@0: michael@0: function LegacyIteratorThrow(exn) { michael@0: var iter = callFunction(std_WeakMap_get, LegacyIteratorWrapperMap, this); michael@0: try { michael@0: return { value: iter.throw(exn), done: false }; michael@0: } catch (e) { michael@0: if (e instanceof std_StopIteration) michael@0: return { value: undefined, done: true }; michael@0: throw e; michael@0: } michael@0: } michael@0: michael@0: function LegacyIterator(iter) { michael@0: callFunction(std_WeakMap_set, LegacyIteratorWrapperMap, this, iter); michael@0: } michael@0: michael@0: function LegacyGeneratorIterator(iter) { michael@0: callFunction(std_WeakMap_set, LegacyIteratorWrapperMap, this, iter); michael@0: } michael@0: michael@0: var LegacyIteratorsInitialized = std_Object_create(null); michael@0: michael@0: function InitLegacyIterators() { michael@0: var props = std_Object_create(null); michael@0: michael@0: props.next = std_Object_create(null); michael@0: props.next.value = LegacyIteratorNext; michael@0: props.next.enumerable = false; michael@0: props.next.configurable = true; michael@0: props.next.writable = true; michael@0: michael@0: props[std_iterator] = std_Object_create(null); michael@0: props[std_iterator].value = IteratorIdentity; michael@0: props[std_iterator].enumerable = false; michael@0: props[std_iterator].configurable = true; michael@0: props[std_iterator].writable = true; michael@0: michael@0: var LegacyIteratorProto = std_Object_create(GetIteratorPrototype(), props); michael@0: MakeConstructible(LegacyIterator, LegacyIteratorProto); michael@0: michael@0: props.throw = std_Object_create(null); michael@0: props.throw.value = LegacyIteratorThrow; michael@0: props.throw.enumerable = false; michael@0: props.throw.configurable = true; michael@0: props.throw.writable = true; michael@0: michael@0: var LegacyGeneratorIteratorProto = std_Object_create(GetIteratorPrototype(), props); michael@0: MakeConstructible(LegacyGeneratorIterator, LegacyGeneratorIteratorProto); michael@0: michael@0: LegacyIteratorsInitialized.initialized = true; michael@0: } michael@0: michael@0: function NewLegacyIterator(iter, wrapper) { michael@0: if (!LegacyIteratorsInitialized.initialized) michael@0: InitLegacyIterators(); michael@0: michael@0: return new wrapper(iter); michael@0: } michael@0: michael@0: function LegacyIteratorShim() { michael@0: return NewLegacyIterator(ToObject(this), LegacyIterator); michael@0: } michael@0: michael@0: function LegacyGeneratorIteratorShim() { michael@0: return NewLegacyIterator(ToObject(this), LegacyGeneratorIterator); michael@0: }