js/src/builtin/Iterator.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/builtin/Iterator.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,86 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +function IteratorIdentity() {
     1.9 +    return this;
    1.10 +}
    1.11 +
    1.12 +var LegacyIteratorWrapperMap = new std_WeakMap();
    1.13 +
    1.14 +function LegacyIteratorNext(arg) {
    1.15 +    var iter = callFunction(std_WeakMap_get, LegacyIteratorWrapperMap, this);
    1.16 +    try {
    1.17 +        return { value: iter.next(arg), done: false };
    1.18 +    } catch (e) {
    1.19 +        if (e instanceof std_StopIteration)
    1.20 +            return { value: undefined, done: true };
    1.21 +        throw e;
    1.22 +    }
    1.23 +}
    1.24 +
    1.25 +function LegacyIteratorThrow(exn) {
    1.26 +    var iter = callFunction(std_WeakMap_get, LegacyIteratorWrapperMap, this);
    1.27 +    try {
    1.28 +        return { value: iter.throw(exn), done: false };
    1.29 +    } catch (e) {
    1.30 +        if (e instanceof std_StopIteration)
    1.31 +            return { value: undefined, done: true };
    1.32 +        throw e;
    1.33 +    }
    1.34 +}
    1.35 +
    1.36 +function LegacyIterator(iter) {
    1.37 +    callFunction(std_WeakMap_set, LegacyIteratorWrapperMap, this, iter);
    1.38 +}
    1.39 +
    1.40 +function LegacyGeneratorIterator(iter) {
    1.41 +    callFunction(std_WeakMap_set, LegacyIteratorWrapperMap, this, iter);
    1.42 +}
    1.43 +
    1.44 +var LegacyIteratorsInitialized = std_Object_create(null);
    1.45 +
    1.46 +function InitLegacyIterators() {
    1.47 +    var props = std_Object_create(null);
    1.48 +
    1.49 +    props.next = std_Object_create(null);
    1.50 +    props.next.value = LegacyIteratorNext;
    1.51 +    props.next.enumerable = false;
    1.52 +    props.next.configurable = true;
    1.53 +    props.next.writable = true;
    1.54 +
    1.55 +    props[std_iterator] = std_Object_create(null);
    1.56 +    props[std_iterator].value = IteratorIdentity;
    1.57 +    props[std_iterator].enumerable = false;
    1.58 +    props[std_iterator].configurable = true;
    1.59 +    props[std_iterator].writable = true;
    1.60 +
    1.61 +    var LegacyIteratorProto = std_Object_create(GetIteratorPrototype(), props);
    1.62 +    MakeConstructible(LegacyIterator, LegacyIteratorProto);
    1.63 +
    1.64 +    props.throw = std_Object_create(null);
    1.65 +    props.throw.value = LegacyIteratorThrow;
    1.66 +    props.throw.enumerable = false;
    1.67 +    props.throw.configurable = true;
    1.68 +    props.throw.writable = true;
    1.69 +
    1.70 +    var LegacyGeneratorIteratorProto = std_Object_create(GetIteratorPrototype(), props);
    1.71 +    MakeConstructible(LegacyGeneratorIterator, LegacyGeneratorIteratorProto);
    1.72 +
    1.73 +    LegacyIteratorsInitialized.initialized = true;
    1.74 +}
    1.75 +
    1.76 +function NewLegacyIterator(iter, wrapper) {
    1.77 +    if (!LegacyIteratorsInitialized.initialized)
    1.78 +        InitLegacyIterators();
    1.79 +
    1.80 +    return new wrapper(iter);
    1.81 +}
    1.82 +
    1.83 +function LegacyIteratorShim() {
    1.84 +    return NewLegacyIterator(ToObject(this), LegacyIterator);
    1.85 +}
    1.86 +
    1.87 +function LegacyGeneratorIteratorShim() {
    1.88 +    return NewLegacyIterator(ToObject(this), LegacyGeneratorIterator);
    1.89 +}

mercurial