From b89a7ee8b956c96a1dcee995ea840feddc5d4b27 Mon Sep 17 00:00:00 2001 From: Pierre Frisch Date: Thu, 22 Dec 2011 07:25:50 -0800 Subject: First commit of Ninja to ninja-internal Signed-off-by: Valerio Virgillito --- node_modules/montage/core/shim/array.js | 78 ++++ node_modules/montage/core/shim/string.js | 66 ++++ node_modules/montage/core/shim/structures.js | 552 +++++++++++++++++++++++++++ node_modules/montage/core/shim/timers.js | 87 +++++ node_modules/montage/core/shim/weak-map.js | 454 ++++++++++++++++++++++ 5 files changed, 1237 insertions(+) create mode 100755 node_modules/montage/core/shim/array.js create mode 100755 node_modules/montage/core/shim/string.js create mode 100755 node_modules/montage/core/shim/structures.js create mode 100755 node_modules/montage/core/shim/timers.js create mode 100755 node_modules/montage/core/shim/weak-map.js (limited to 'node_modules/montage/core/shim') diff --git a/node_modules/montage/core/shim/array.js b/node_modules/montage/core/shim/array.js new file mode 100755 index 00000000..ff5d5d37 --- /dev/null +++ b/node_modules/montage/core/shim/array.js @@ -0,0 +1,78 @@ +/* + This file contains proprietary software owned by Motorola Mobility, Inc.
+ No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+ (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. +
*/ +/** + Defines extensions to native Array object. + @see [Array class]{@link external:Array} + @module montage/core/shim/array +*/ +/** + @external Array +*/ + +/** + @function external:Array#equals + @param {object} right The object to compare. + @returns {Boolean} true or false +*/ +if (!Array.prototype.equals) { + Object.defineProperty(Array.prototype, "equals", { + value: function (right) { + var i = 0, + length = this.length, + lhs, + rhs; + + if (this === right) { + return true; + } + + if (!right || !Array.isArray(right)) { + return false; + } + + if (length !== right.length) { + return false; + } else { + for (; i < length; ++i) { + if (i in this) { + lhs = this[i], + rhs = right[i]; + + if (lhs !== rhs && (lhs && rhs && !lhs.equals(rhs))) { + return false; + } + } else { + if (i in right) { + return false; + } + } + } + } + return true; + } + }); +} +/** + Shim implementation of Array.isArray() for browsers that don't yet support it. + @function external:Array.isArray + @param {object} obj The object to determine if its an array. + @returns {Array} Object.prototype.toString.call(obj) === "[object Array]" +*/ +if (!Array.isArray) { + Object.defineProperty(Array, "isArray", { + value: function(obj) { + return Object.prototype.toString.call(obj) === "[object Array]"; + } + }); +} + +if (!Array.isCanvasPixelArray) { + Object.defineProperty(Array, "isCanvasPixelArray", { + value: function(obj) { + return Object.prototype.toString.call(obj) === "[object CanvasPixelArray]"; + } + }); +} diff --git a/node_modules/montage/core/shim/string.js b/node_modules/montage/core/shim/string.js new file mode 100755 index 00000000..1b71cec3 --- /dev/null +++ b/node_modules/montage/core/shim/string.js @@ -0,0 +1,66 @@ +/* + This file contains proprietary software owned by Motorola Mobility, Inc.
+ No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+ (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. +
*/ +/** + Defines extensions to native String object. + @see [String class]{@link external:String} + @module montage/core/shim/string +*/ + +/** + @external String + */ +Object.defineProperties(String.prototype, /** @lends external:String.prototype#*/{ + /** + @function external:String.addEventListener + @param {Listener} type The type of event listener. + @param {Listener} listener The event listener. + @param {Function} useCapture The capturing function. + */ + addEventListener: { + value: function(type, listener, useCapture) { + //NO OP, on purpose + } + + }, + /** + Capitalizes the first letter in the string. + @function external:String.toCapitalized + @returns {String} The original string with its first letter capitalized. + @example + var fname = "abe"; + var lname = "lincoln"; + var name = fname.toCapitalized() + " " + lname.toCapitalized(); + // name == "Abe Lincoln" + */ + toCapitalized: { + value: function() { + return this.charAt(0).toUpperCase() + this.slice(1); + } + }, + + /** + Returns true if the two strings are equal, otherwise returns false. + @function external:String.equals + @param {Object} anObject The object to compare to the string. + @returns {Boolean} Returns true if the string is equal to anObject. + */ + equals: { + value: function(anObject) { + if (this.toString() === anObject) { + return true; + } + + if (!anObject || !(anObject instanceof String)) { + return false; + } + + return (this == anObject); + } + } + +}); + + diff --git a/node_modules/montage/core/shim/structures.js b/node_modules/montage/core/shim/structures.js new file mode 100755 index 00000000..2f3d7d05 --- /dev/null +++ b/node_modules/montage/core/shim/structures.js @@ -0,0 +1,552 @@ +/* + This file contains proprietary software owned by Motorola Mobility, Inc.
+ No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+ (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. +
*/ + +// Specification: +// http://wiki.ecmascript.org/doku.php?id=harmony:simple_maps_and_sets + +/** + This module provides common data structure utililties, such as maps and sets. + @module montage/core/shim/structures + @see [Map class]{@link module:montage/core/shim/structures.Map} + */ +/** + @class module:montage/core/shim/structures.Map + @classdesc Provides a Map data structure for managing key/value pairs, including methods for querying and manipulating map elements. A map cannot contain duplicate keys; each key can map to at most one value. + */ +exports.Map = Map; +function Map(ignored, options) { + if (!(this instanceof Map)) { + return new Map(ignored, options); + } + options = options || {}; + var eq = options.eq || Set.eq; + var hash = options.hash || Set.hash; + this._set = Set( + undefined, + { + eq: function (a, b) { + return eq(a.key, b.key); + }, + hash: function (pair) { + return hash(pair.key); + } + } + ); +} + +Object.defineProperties(Map.prototype, /** @lends module:montage/core/shim/structures.Map# */ { +/** + @function + @returns this._set.empty() + */ + empty: { + value: function () { + return this._set.empty(); + } + }, + + /** + Returns the value associated with the key parameter, if it exists. + @function + @param {String} key The name of the key. + @returns {Object} The value of the specified key, if it exists; otherwise returns undefined. + */ + get: { + value: function (key) { + var pair = this._set.get({ + key: key + }); + return pair ? pair.value : undefined; + } + }, + + /** + Adds a new key/value pair to the map. + @function + @param {Object} key The key to use for the new pair. + @param {Object} value The value to associate with the key. + @returns key, value + */ + set: { + value: function (key, value) { + return this._set.add({ + key: key, + value: value + }); + } + }, + + /** + Deletes the element from the map specified by the key parameter. + @function + @param {Object} key The key of the map element to remove. + @returns key + */ + + "delete": { // née del + value: function (key) { + return this._set["delete"]({ + key: key + }); + } + }, + + /** + Returns true if the map contains an element with the specified key, otherwise returns false. + @function + @param {Object} key The key of the element you want to query for. + @returns {Boolean} Returns true if the map contains the specified key, otherwise returns false. + */ + has: { + value: function (key) { + return this._set.has({ + key: key + }); + } + }, + + /** + Executes a function once per map element, passing the current element to each function as a parameter. + @function + @param {Function} callback The function to execute for each element. + */ + + forEach: { + value: function (callback /*, thisp*/) { + var self = Object(this), + thisp = arguments[1]; + return this._set.forEach(function (pair) { + callback.call(thisp, pair.value, pair.key, self); + }); + } + } +}); + +/** + Provides a set data structure and methods to query and modify the set.
+ A set is a collection that contains no duplicate elements. It stores
+ @class module:montage/core/shim/structures.Set + */ +exports.Set = Set; +function Set(ignored, options) { + if (!(this instanceof Set)) { + return new Set(ignored, options); + } + options = options || {}; + var eq = options.eq || Set.eq; + var hash = options.hash || Set.hash; + this._buckets = {}; + this._Bucket = function () { + return OrderedSet(undefined, { + eq: eq + }); + }; + this._eq = eq; + this._hash = hash; +} + +Object.defineProperties(Set.prototype, /** @lends module:montage/core/shim/structures.Set# */ { + /** + Determines if the set is empty or not. + @function + @returns {Boolean} Returns true if the set is empty, otherwise returns false. + */ + empty: { + value: function () { + return !Object.keys(this._buckets).length; + } + }, + + /** + Retrieves the value of the specified set element. + @function + @param {Object} value The key to an element in the set. + */ + get: { + value: function (value) { + var hash = this._hash(value); + var buckets = this._buckets; + return buckets[hash] ? buckets[hash].get(value) : undefined; + } + }, + + /** + Determines if the set contains a specified item. + @function + @param {Number} value + @returns {Object} The value of the set element. + */ + has: { + value: function (value) { + var hash = this._hash(value); + var buckets = this._buckets; + return buckets[hash] ? buckets[hash].has(value) : false; + } + }, + + /** + Inserts a new element into the set with value as the hash key. + @function + @param {String} value The new element's hash key. + */ + add: { // née insert + value: function (value) { + var hash = this._hash(value); + var buckets = this._buckets; + var bucket = buckets[hash] = buckets[hash] || this._Bucket(); + bucket.add(value); + } + }, + + /** + Removes an element from the set identified by the specified hash key. + @function + @param {String} value The hash key of the element to remove. + */ + "delete": { // née remove + value: function (value) { + var hash = this._hash(value); + var buckets = this._buckets; + var bucket = buckets[hash] = buckets[hash] || this._Bucket(); + bucket["delete"](value); + if (bucket.empty()) { + delete buckets[hash]; + } + } + }, + + /** + Executes a function once per set element. + @function + @param {Function} callback The function to execute on each element. + @returns {Object} object + */ + + forEach: { + value: function (callback /*, thisp*/) { + var self = Object(this); + var thisp = arguments[1]; + var buckets = self._buckets; + return Object.keys(buckets).forEach(function (hash) { + buckets[hash].forEach(callback, thisp); + }); + } + } +}); +/** + @class module:montage/core/shim/structures.OrderedSet + @param {boolean} ignored + @param {object} options +*/ +exports.OrderedSet = OrderedSet; +function OrderedSet(ignored, options) { + if (!(this instanceof OrderedSet)) { + return new OrderedSet(ignored, options); + } + options = options || {}; + var eq = options.eq || OrderedSet.eq; + var head = {}; + head.next = head; + head.prev = head; + this._head = head; + this._eq = eq; +} +; +/** + @private +*/ +Object.defineProperties(OrderedSet.prototype, /** @lends module:montage/core/shim/structures.OrderedSet */{ + _delete: { + value: function (node) { + node.prev.next = node.next; + node.next.prev = node.prev; + } + }, + + _add: { + value: function (node) { + var head = this._head; + var prev = head.prev; + head.prev = node; + node.prev = prev; + prev.next = node; + node.next = head; + } + }, + + _find: { + value: function (value) { + var head = this._head; + var at = head.next; + while (at !== head) { + if (this._eq(at.data, value)) { + return at; + } + at = at.next; + } + } + }, + + /** + @function + @returns {boolean} Returns true if empty, otherwise returns false + */ + empty: { + value: function () { + var head = this._head; + return head.next === head; + } + }, + /** + @function + @param {Number} value + @returns !!this._find(value) + */ + has: { + value: function (value) { + return !!this._find(value); + } + }, + /** + @function + @param {Number} value + @returns found.data + */ + get: { + value: function (value) { + var found = this._find(value); + if (found) { + return found.data; + } + } + }, + /** + @function + @param {Number} value + */ + add: { // née insert + value: function (value) { + if (!this._find(value)) { + this._add({ + data: value + }); + } + } + }, + /** + @function + @param {Number} value + */ + "delete": { // née remove + value: function (value) { + var found = this._find(value); + if (found) { + this._delete(found); + } + } + }, + /** + @function + @param {Function} callback The callback function. + @param {String} context The context string. + */ + forEach: { + value: function (callback, context) { + var head = this._head; + var at = head.next; + while (at !== head) { + callback.call(context, at.data); + at = at.next; + } + } + } +}); + +Set.eq = + OrderedSet.eq = function (a, b) { + return a === b; + }; + +/** + @function + @param {String} value + @returns "~" + */ +Set.hash = function (value) { + return "~" + ( + value && typeof value.hash === "function" ? + value.hash() : + value + ); +}; + +// A least-recently-used cache map contains an entagled +// linked list and mapping. The mapping serves to provide +// constant-time access to any of the linked list nodes. +// When nodes are accessed, they float to the top of the +// list. The least recently accessed node will be collected +// when the collection size exceeds the maximum length. +// +// A WeakMap is almost always a superior alternative to +// a CacheMap, but a CacheMap will suffice in some cases +// if the former is not available. + +/** + @exports CacheMap + @function + @param {Boolean} ignored + @param {String} options + @returns new CacheMap(ignored, options) + */ +exports.CacheMap = CacheMap; +function CacheMap(ignored, options) { + if (!(this instanceof CacheMap)) { + return new CacheMap(ignored, options); + } + options = options || {}; + + this._set = new OrderedSet(undefined, options); + this._map = new Map(undefined, options); + + this._length = 0; + this._maxLength = options.maxLength || Infinity; + this._ondrop = options.ondrop; +} + +/** + @class module:montage/core/shim/structures.CacheMap +*/ +CacheMap.prototype = Object.create(Object.prototype,/** @lends module:montage/core/shim/structures.CacheMap# */ { + /** + @type {Constructor} + @default CacheMap + */ + constructor: { + value: CacheMap + }, +/** + @private +*/ + _add: { + value: function (node) { + this._map.set(node.key, node); + this._set.add(node); + this._length++; + if (this._length > this._maxLength) { + // delete least recently accessed node + // /!\ dives deep into set structure + var node = this._set._head.next; + if (this._ondrop) { + this._ondrop(node.data); + } + this._set._delete(node); + this._map["delete"](node.key); + this._length--; + } + } + }, + /** + @private +*/ + _delete: { + value: function (node) { + this._map["delete"](node.key); + this._set["delete"](node); + this._length--; + } + }, + /** + @function + @returns this._set.empty() + */ + empty: { + value: function () { + return this._set.empty(); + } + }, + /** + @function + @param {Function} key + @returns node.value + */ + get: { + value: function (key) { + var node = this._map.get(key); + if (!node) { + return; + } + // push node to tail + this._set["delete"](node); + this._set.add(node); + return node.value; + } + }, + /** + @function + @param {String} key + @param {Number} value + */ + set: { + value: function (key, value) { + var node = this._map.get(key); + if (node) { + node.value = value; + this._delete(node); + } else { + node = { + key: key, + value: value + }; + } + this._add(node); + } + }, + /** + @function + @param {String} key + */ + "delete": { + value: function (key) { + var node = this._map.get(key); + if (node) { + this._delete(node); + } + } + }, + /** + @function + @param {String} key + @returns this._map.has(key) + */ + has: { + value: function (key) { + return this._map.has(key); + } + }, + /** + @function + @returns {Array} keys + */ + keys: { + value: function () { + var keys = []; + this._set.forEach(function (node) { + keys.push(node.key); + }); + return keys; + } + }, + /** + @function + @returns this._set.forEach.apply(this._set, arguments) + */ + forEach: { + value: function () { + return this._set.forEach.apply(this._set, arguments); + } + } +}); + diff --git a/node_modules/montage/core/shim/timers.js b/node_modules/montage/core/shim/timers.js new file mode 100755 index 00000000..859a7aec --- /dev/null +++ b/node_modules/montage/core/shim/timers.js @@ -0,0 +1,87 @@ +/* + This file contains proprietary software owned by Motorola Mobility, Inc.
+ No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+ (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. +
*/ + + /** + Defines [setImmediate()]{@link setImmediate} and [clearImmediate()]{@link clearImmediate} shim functions. + @see setImmediate + @see clearImmediate + @module montage/core/shim/timers + */ + +/** + @function + @name setImmediate + @global +*/ + +/** + @function + @name clearImmediate + @global +*/ + +(function (global) { + +var nextTick; +if (typeof process !== "undefined") { + nextTick = process.nextTick; +} else if (typeof MessageChannel !== "undefined") { + // http://www.nonblocking.io/2011/06/windownexttick.html + var channel = new MessageChannel(); + // linked list of tasks (single, with head node) + var head = {}, tail = head; + + channel.port1.onmessage = function () { + var next = head.next; + var task = next.task; + head = next; + task(); + }; + + nextTick = function (task) { + tail = tail.next = {task: task}; + channel.port2.postMessage(void 0); + } +} else if (typeof setTimeout !== "undefined") { + + nextTick = function (callback) { + setTimeout(callback, 0); + }; +} else { + throw new Error("Can't shim setImmediate."); +} + +if (typeof setImmediate === "undefined") { + var nextHandle = 0; + var handles = {}; + + + global.setImmediate = function setImmediate(callback) { + var handle = nextHandle++; + var args = arguments.length > 1 ? + Array.prototype.slice.call(arguments, 1) : + void 0; + handles[handle] = true; + nextTick(function () { + if (handles[handle]) { + callback.apply(void 0, args); + delete handles[handle]; + } + }); + return handle; + }; + + global.clearImmediate = function clearImmediate(handle) { + delete handles[handle]; + }; +} + +// Make this work as a