diff options
Diffstat (limited to 'tools/addon-sdk-1.12/lib/sdk/util')
-rw-r--r-- | tools/addon-sdk-1.12/lib/sdk/util/array.js | 89 | ||||
-rw-r--r-- | tools/addon-sdk-1.12/lib/sdk/util/collection.js | 112 | ||||
-rw-r--r-- | tools/addon-sdk-1.12/lib/sdk/util/deprecate.js | 27 | ||||
-rw-r--r-- | tools/addon-sdk-1.12/lib/sdk/util/list.js | 78 | ||||
-rw-r--r-- | tools/addon-sdk-1.12/lib/sdk/util/object.js | 57 | ||||
-rw-r--r-- | tools/addon-sdk-1.12/lib/sdk/util/registry.js | 61 | ||||
-rw-r--r-- | tools/addon-sdk-1.12/lib/sdk/util/uuid.js | 17 |
7 files changed, 441 insertions, 0 deletions
diff --git a/tools/addon-sdk-1.12/lib/sdk/util/array.js b/tools/addon-sdk-1.12/lib/sdk/util/array.js new file mode 100644 index 0000000..764eeca --- /dev/null +++ b/tools/addon-sdk-1.12/lib/sdk/util/array.js @@ -0,0 +1,89 @@ +/* vim:set ts=2 sw=2 sts=2 et: */ +/* 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" +}; + +/** + * Returns `true` if given `array` contain given `element` or `false` + * otherwise. + * @param {Array} array + * Target array. + * @param {Object|String|Number|Boolean} element + * Element being looked up. + * @returns {Boolean} + */ +var has = exports.has = function has(array, element) { + // shorter and faster equivalent of `array.indexOf(element) >= 0` + return !!~array.indexOf(element); +}; +var hasAny = exports.hasAny = function hasAny(array, elements) { + if (arguments.length < 2) + return false; + if (!Array.isArray(elements)) + elements = [ elements ]; + return array.some(function (element) { + return has(elements, element); + }); +}; + +/** + * Adds given `element` to the given `array` if it does not contain it yet. + * `true` is returned if element was added otherwise `false` is returned. + * @param {Array} array + * Target array. + * @param {Object|String|Number|Boolean} element + * Element to be added. + * @returns {Boolean} + */ +var add = exports.add = function add(array, element) { + var result; + if ((result = !has(array, element))) + array.push(element); + + return result; +}; + +/** + * Removes first occurrence of the given `element` from the given `array`. If + * `array` does not contain given `element` `false` is returned otherwise + * `true` is returned. + * @param {Array} array + * Target array. + * @param {Object|String|Number|Boolean} element + * Element to be removed. + * @returns {Boolean} + */ +exports.remove = function remove(array, element) { + var result; + if ((result = has(array, element))) + array.splice(array.indexOf(element), 1); + + return result; +}; + +/** + * Produces a duplicate-free version of the given `array`. + * @param {Array} array + * Source array. + * @returns {Array} + */ +exports.unique = function unique(array) { + var value = []; + return array.forEach(function(element) { + add(value, element); + }); + return value; +}; + +exports.flatten = function flatten(array){ + var flat = []; + for (var i = 0, l = array.length; i < l; i++) { + flat = flat.concat(Array.isArray(array[i]) ? flatten(array[i]) : array[i]); + } + return flat; +}; diff --git a/tools/addon-sdk-1.12/lib/sdk/util/collection.js b/tools/addon-sdk-1.12/lib/sdk/util/collection.js new file mode 100644 index 0000000..54e7313 --- /dev/null +++ b/tools/addon-sdk-1.12/lib/sdk/util/collection.js @@ -0,0 +1,112 @@ +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et: */ +/* 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" +}; + +exports.Collection = Collection; + +/** + * Adds a collection property to the given object. Setting the property to a + * scalar value empties the collection and adds the value. Setting it to an + * array empties the collection and adds all the items in the array. + * + * @param obj + * The property will be defined on this object. + * @param propName + * The name of the property. + * @param array + * If given, this will be used as the collection's backing array. + */ +exports.addCollectionProperty = function addCollProperty(obj, propName, array) { + array = array || []; + let publicIface = new Collection(array); + + obj.__defineSetter__(propName, function (itemOrItems) { + array.splice(0, array.length); + publicIface.add(itemOrItems); + }); + + obj.__defineGetter__(propName, function () { + return publicIface; + }); +}; + +/** + * A collection is ordered, like an array, but its items are unique, like a set. + * + * @param array + * The collection is backed by an array. If this is given, it will be + * used as the backing array. This way the caller can fully control the + * collection. Otherwise a new empty array will be used, and no one but + * the collection will have access to it. + */ +function Collection(array) { + array = array || []; + + /** + * Provides iteration over the collection. Items are yielded in the order + * they were added. + */ + this.__iterator__ = function Collection___iterator__() { + let items = array.slice(); + for (let i = 0; i < items.length; i++) + yield items[i]; + }; + + /** + * The number of items in the collection. + */ + this.__defineGetter__("length", function Collection_get_length() { + return array.length; + }); + + /** + * Adds a single item or an array of items to the collection. Any items + * already contained in the collection are ignored. + * + * @param itemOrItems + * An item or array of items. + * @return The collection. + */ + this.add = function Collection_add(itemOrItems) { + let items = toArray(itemOrItems); + for (let i = 0; i < items.length; i++) { + let item = items[i]; + if (array.indexOf(item) < 0) + array.push(item); + } + return this; + }; + + /** + * Removes a single item or an array of items from the collection. Any items + * not contained in the collection are ignored. + * + * @param itemOrItems + * An item or array of items. + * @return The collection. + */ + this.remove = function Collection_remove(itemOrItems) { + let items = toArray(itemOrItems); + for (let i = 0; i < items.length; i++) { + let idx = array.indexOf(items[i]); + if (idx >= 0) + array.splice(idx, 1); + } + return this; + }; +}; + +function toArray(itemOrItems) { + let isArr = itemOrItems && + itemOrItems.constructor && + itemOrItems.constructor.name === "Array"; + return isArr ? itemOrItems : [itemOrItems]; +} diff --git a/tools/addon-sdk-1.12/lib/sdk/util/deprecate.js b/tools/addon-sdk-1.12/lib/sdk/util/deprecate.js new file mode 100644 index 0000000..b38606a --- /dev/null +++ b/tools/addon-sdk-1.12/lib/sdk/util/deprecate.js @@ -0,0 +1,27 @@ +/* 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" +}; + +const traceback = require("../console/traceback"); + +function deprecateUsage(msg) { + // Print caller stacktrace in order to help figuring out which code + // does use deprecated thing + let stack = traceback.get().slice(0, -2); + console.error("DEPRECATED: " + msg + "\n" + traceback.format(stack)); +} +exports.deprecateUsage = deprecateUsage; + +function deprecateFunction(fun, msg) { + return function deprecated() { + deprecateUsage(msg); + return fun.apply(this, arguments); + }; +} +exports.deprecateFunction = deprecateFunction; diff --git a/tools/addon-sdk-1.12/lib/sdk/util/list.js b/tools/addon-sdk-1.12/lib/sdk/util/list.js new file mode 100644 index 0000000..0a143d9 --- /dev/null +++ b/tools/addon-sdk-1.12/lib/sdk/util/list.js @@ -0,0 +1,78 @@ +/* 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'; + +const { Class } = require('../core/heritage'); +const listNS = require('../core/namespace').ns(); + +const List = Class({ + /** + * List constructor can take any number of element to populate itself. + * @params {Object|String|Number} element + * @example + * List(1,2,3).length == 3 // true + */ + initialize: function List() { + listNS(this).keyValueMap = []; + + for (let i = 0, ii = arguments.length; i < ii; i++) + addListItem(this, arguments[i]); + }, + /** + * Number of elements in this list. + * @type {Number} + */ + get length() listNS(this).keyValueMap.length, + /** + * Returns a string representing this list. + * @returns {String} + */ + toString: function toString() 'List(' + listNS(this).keyValueMap + ')', + /** + * Custom iterator providing `List`s enumeration behavior. + * We cant reuse `_iterator` that is defined by `Iterable` since it provides + * iteration in an arbitrary order. + * @see https://developer.mozilla.org/en/JavaScript/Reference/Statements/for...in + * @param {Boolean} onKeys + */ + __iterator__: function __iterator__(onKeys, onKeyValue) { + let array = listNS(this).keyValueMap.slice(0), + i = -1; + for each(let element in array) + yield onKeyValue ? [++i, element] : onKeys ? ++i : element; + } +}); +exports.List = List; + +function addListItem(that, value) { + let list = listNS(that).keyValueMap, + index = list.indexOf(value); + + if (-1 === index) { + try { + that[that.length] = value; + } + catch (e) {} + list.push(value); + } +} +exports.addListItem = addListItem; + +function removeListItem(that, element) { + let list = listNS(that).keyValueMap, + index = list.indexOf(element); + + if (0 <= index) { + list.splice(index, 1); + try { + for (let length = list.length; index < length; index++) + that[index] = list[index]; + that[list.length] = undefined; + } + catch(e){} + } +} +exports.removeListItem = removeListItem; + +exports.listNS = listNS; diff --git a/tools/addon-sdk-1.12/lib/sdk/util/object.js b/tools/addon-sdk-1.12/lib/sdk/util/object.js new file mode 100644 index 0000000..4b1a2d4 --- /dev/null +++ b/tools/addon-sdk-1.12/lib/sdk/util/object.js @@ -0,0 +1,57 @@ +/* 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": "unstable" +}; + +/** + * Merges all the properties of all arguments into first argument. If two or + * more argument objects have own properties with the same name, the property + * is overridden, with precedence from right to left, implying, that properties + * of the object on the left are overridden by a same named property of the + * object on the right. + * + * Any argument given with "falsy" value - commonly `null` and `undefined` in + * case of objects - are skipped. + * + * @examples + * var a = { bar: 0, a: 'a' } + * var b = merge(a, { foo: 'foo', bar: 1 }, { foo: 'bar', name: 'b' }); + * b === a // true + * b.a // 'a' + * b.foo // 'bar' + * b.bar // 1 + * b.name // 'b' + */ +function merge(source) { + let descriptor = {}; + // `Boolean` converts the first parameter to a boolean value. Any object is + // converted to `true` where `null` and `undefined` becames `false`. Therefore + // the `filter` method will keep only objects that are defined and not null. + Array.slice(arguments, 1).filter(Boolean).forEach(function onEach(properties) { + Object.getOwnPropertyNames(properties).forEach(function(name) { + descriptor[name] = Object.getOwnPropertyDescriptor(properties, name); + }); + }); + return Object.defineProperties(source, descriptor); +} +exports.merge = merge; + +/** + * Returns an object that inherits from the first argument and contains all the + * properties from all following arguments. + * `extend(source1, source2, source3)` is equivalent of + * `merge(Object.create(source1), source2, source3)`. + */ +function extend(source) { + let rest = Array.slice(arguments, 1); + rest.unshift(Object.create(source)); + return merge.apply(null, rest); +} +exports.extend = extend; + + diff --git a/tools/addon-sdk-1.12/lib/sdk/util/registry.js b/tools/addon-sdk-1.12/lib/sdk/util/registry.js new file mode 100644 index 0000000..7d385f0 --- /dev/null +++ b/tools/addon-sdk-1.12/lib/sdk/util/registry.js @@ -0,0 +1,61 @@ +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et: */ +/* 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": "unstable" +}; + +const { EventEmitter } = require('../deprecated/events'); +const unload = require('../system/unload'); + +const Registry = EventEmitter.compose({ + _registry: null, + _constructor: null, + constructor: function Registry(constructor) { + this._registry = []; + this._constructor = constructor; + this.on('error', this._onError = this._onError.bind(this)); + unload.ensure(this, "_destructor"); + }, + _destructor: function _destructor() { + let _registry = this._registry.slice(0); + for each (let instance in _registry) + this._emit('remove', instance); + this._registry.splice(0); + }, + _onError: function _onError(e) { + if (!this._listeners('error').length) + console.error(e); + }, + has: function has(instance) { + let _registry = this._registry; + return ( + (0 <= _registry.indexOf(instance)) || + (instance && instance._public && 0 <= _registry.indexOf(instance._public)) + ); + }, + add: function add(instance) { + let { _constructor, _registry } = this; + if (!(instance instanceof _constructor)) + instance = new _constructor(instance); + if (0 > _registry.indexOf(instance)) { + _registry.push(instance); + this._emit('add', instance); + } + return instance; + }, + remove: function remove(instance) { + let _registry = this._registry; + let index = _registry.indexOf(instance) + if (0 <= index) { + this._emit('remove', instance); + _registry.splice(index, 1); + } + } +}); +exports.Registry = Registry; + diff --git a/tools/addon-sdk-1.12/lib/sdk/util/uuid.js b/tools/addon-sdk-1.12/lib/sdk/util/uuid.js new file mode 100644 index 0000000..0ebf981 --- /dev/null +++ b/tools/addon-sdk-1.12/lib/sdk/util/uuid.js @@ -0,0 +1,17 @@ +/* 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": "unstable" +}; + +const { Cc, Ci, components: { ID: parseUUID } } = require('chrome'); +const { generateUUID } = Cc['@mozilla.org/uuid-generator;1']. + getService(Ci.nsIUUIDGenerator); + +// Returns `uuid`. If `id` is passed then it's parsed to `uuid` and returned +// if not then new one is generated. +exports.uuid = function uuid(id) id ? parseUUID(id) : generateUUID() |