Bug 907077 - Fix up jetpack for for-of changes. r=rFobic

This commit is contained in:
Andy Wingo 2013-10-01 13:05:15 -04:00
parent 5c89d8f6ea
commit 289443ff0b
4 changed files with 66 additions and 32 deletions

View File

@ -8,6 +8,7 @@ module.metadata = {
};
const { Trait } = require('../deprecated/traits');
const { iteratorSymbol } = require('../util/iteration');
/**
* @see https://jetpack.mozillalabs.com/sdk/latest/docs/#module/api-utils/list
@ -38,7 +39,7 @@ exports.Iterable = Iterable;
* elements of the list). List is a base trait and is meant to be a part of
* composition, since all of it's API is private except length property.
*/
const List = Trait.resolve({ toString: null }).compose({
const listOptions = {
_keyValueMap: null,
/**
* List constructor can take any number of element to populate itself.
@ -114,12 +115,12 @@ const List = Trait.resolve({ toString: null }).compose({
for (let element of array)
yield onKeyValue ? [++i, element] : onKeys ? ++i : element;
},
iterator: function iterator() {
let array = this._keyValueMap.slice(0);
};
listOptions[iteratorSymbol] = function* iterator() {
let array = this._keyValueMap.slice(0);
for (let element of array)
yield element;
}
});
for (let element of array)
yield element;
}
const List = Trait.resolve({ toString: null }).compose(listOptions);
exports.List = List;

View File

@ -22,7 +22,8 @@ const { Ci, Cc } = require("chrome"),
{ getTabs, getTabContentWindow, getTabForContentWindow,
getAllTabContentWindows } = require('./tabs/utils'),
winUtils = require("./window/utils"),
events = require("./system/events");
events = require("./system/events"),
{ iteratorSymbol, forInIterator } = require("./util/iteration");
// The selection types
const HTML = 0x01,
@ -99,25 +100,26 @@ const selectionListener = {
* is returned because the text field selection APIs doesn't support
* multiple selections.
*/
function iterator() {
let selection = getSelection(DOM);
let count = 0;
function* forOfIterator() {
let selection = getSelection(DOM);
let count = 0;
if (selection)
count = selection.rangeCount || (getElementWithSelection() ? 1 : 0);
if (selection)
count = selection.rangeCount || (getElementWithSelection() ? 1 : 0);
for (let i = 0; i < count; i++) {
let sel = Selection(i);
for (let i = 0; i < count; i++) {
let sel = Selection(i);
if (sel.text)
yield Selection(i);
}
if (sel.text)
yield Selection(i);
}
}
const selectionIterator = obscure({
__iterator__: iterator, // for...in; for each...in
iterator: iterator // for....of
});
const selectionIteratorOptions = {
__iterator__: forInIterator
}
selectionIteratorOptions[iteratorSymbol] = forOfIterator;
const selectionIterator = obscure(selectionIteratorOptions);
/**
* Returns the most recent focused window.

View File

@ -0,0 +1,33 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';
module.metadata = {
"stability": "experimental"
};
// This is known as @@iterator in the ES6 spec. Until it is bound to
// some well-known name, find the @@iterator object by expecting it as
// the first property accessed on a for-of iterable.
const iteratorSymbol = (function() {
try {
for (var _ of Proxy.create({get: function(_, name) { throw name; } }))
break;
} catch (name) {
return name;
}
throw new TypeError;
})();
exports.iteratorSymbol = iteratorSymbol;
// An adaptor that, given an object that is iterable with for-of, is
// suitable for being bound to __iterator__ in order to make the object
// iterable in the same way via for-in.
function forInIterator() {
for (let item of this)
yield item;
}
exports.forInIterator = forInIterator;

View File

@ -9,8 +9,9 @@ module.metadata = {
const { Class } = require('../core/heritage');
const listNS = require('../core/namespace').ns();
const { iteratorSymbol } = require('../util/iteration');
const List = Class({
const listOptions = {
/**
* List constructor can take any number of element to populate itself.
* @params {Object|String|Number} element
@ -46,14 +47,11 @@ const List = Class({
for each(let element in array)
yield onKeyValue ? [++i, element] : onKeys ? ++i : element;
},
iterator: function iterator() {
let array = listNS(this).keyValueMap.slice(0),
i = -1;
for (let element of array)
yield element;
}
});
};
listOptions[iteratorSymbol] = function iterator() {
return listNS(this).keyValueMap.slice(0)[iteratorSymbol]();
};
const List = Class(listOptions);
exports.List = List;
function addListItem(that, value) {