gpu.js/lib/jison/es5.js
2016-01-23 23:38:46 +08:00

233 lines
6.9 KiB
JavaScript

if (!Object.keys) {
Object.keys = function (object) {
var keys = [];
for (var name in object) {
if (Object.prototype.hasOwnProperty.call(object, name)) {
keys.push(name);
}
}
return keys;
};
}
if (!Object.defineProperty)
Object.defineProperty = function(object, property, descriptor) {
var has = Object.prototype.hasOwnProperty;
if (typeof descriptor == "object") {
if (has.call(descriptor, "value")) {
if (!object.__lookupGetter__(property) && !object.__lookupSetter__(property))
// data property defined and no pre-existing accessors
object[property] = descriptor.value;
if ((has.call(descriptor, "get") || has.call(descriptor, "set")))
// descriptor has a value property but accessor already exists
throw new TypeError("Object doesn't support this action");
}
if ( // can't implement these features; allow false but not true
!(has.call(descriptor, "writable") ? descriptor.writable : true) ||
!(has.call(descriptor, "enumerable") ? descriptor.enumerable : true) ||
!(has.call(descriptor, "configurable") ? descriptor.configurable : true)
)
throw new RangeError(
"This implementation of Object.defineProperty does not " +
"support configurable, enumerable, or writable."
);
else if (typeof descriptor.get == "function")
object.__defineGetter__(property, descriptor.get);
if (typeof descriptor.set == "function")
object.__defineSetter__(property, descriptor.set);
}
return object;
};
if (!Object.defineProperties) {
Object.defineProperties = function(object, properties) {
for (var property in properties) {
if (Object.prototype.hasOwnProperty.call(properties, property))
Object.defineProperty(object, property, properties[property]);
}
return object;
};
}
if (!Object.create) {
Object.create = function(prototype, properties) {
function Type() {};
Type.prototype = prototype;
var object = new Type();
if (typeof properties !== "undefined")
Object.defineProperties(object, properties);
return object;
};
}
// ES5 15.5.4.20
if (!String.prototype.trim) {
// http://blog.stevenlevithan.com/archives/faster-trim-javascript
var trimBeginRegexp = /^\s\s*/;
var trimEndRegexp = /\s\s*$/;
String.prototype.trim = function () {
return String(this).replace(trimBeginRegexp, '').replace(trimEndRegexp, '');
};
}
// Array additions.
// ES5 draft:
// http://www.ecma-international.org/publications/files/drafts/tc39-2009-025.pdf
// ES5 15.4.3.2
if (!Array.isArray) {
Array.isArray = function(obj) {
return Object.prototype.toString.call(obj) == "[object Array]";
};
}
// ES5 15.4.4.18
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(block, thisObject) {
var len = this.length >>> 0;
for (var i = 0; i < len; i++) {
if (i in this) {
block.call(thisObject, this[i], i, this);
}
}
};
}
// ES5 15.4.4.19
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/map
if (!Array.prototype.map) {
Array.prototype.map = function(fun /*, thisp*/) {
var len = this.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in this)
res[i] = fun.call(thisp, this[i], i, this);
}
return res;
};
}
// filter
if (!Array.prototype.filter) {
Array.prototype.filter = function (block /*, thisp */) {
var values = [];
var thisp = arguments[1];
for (var i = 0; i < this.length; i++)
if (block.call(thisp, this[i]))
values.push(this[i]);
return values;
};
}
// every
if (!Array.prototype.every) {
Array.prototype.every = function (block /*, thisp */) {
var thisp = arguments[1];
for (var i = 0; i < this.length; i++)
if (!block.call(thisp, this[i]))
return false;
return true;
};
}
// some
if (!Array.prototype.some) {
Array.prototype.some = function (block /*, thisp */) {
var thisp = arguments[1];
for (var i = 0; i < this.length; i++)
if (block.call(thisp, this[i]))
return true;
return false;
};
}
// reduce
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduce
if (!Array.prototype.reduce) {
Array.prototype.reduce = function(fun /*, initial*/) {
var len = this.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
// no value to return if no initial value and an empty array
if (len == 0 && arguments.length == 1)
throw new TypeError();
var i = 0;
if (arguments.length >= 2) {
var rv = arguments[1];
} else {
do {
if (i in this) {
rv = this[i++];
break;
}
// if array contains no values, no initial value to return
if (++i >= len)
throw new TypeError();
} while (true);
}
for (; i < len; i++) {
if (i in this)
rv = fun.call(null, rv, this[i], i, this);
}
return rv;
};
}
// reduceRight
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduceRight
if (!Array.prototype.reduceRight) {
Array.prototype.reduceRight = function(fun /*, initial*/) {
var len = this.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
// no value to return if no initial value, empty array
if (len == 0 && arguments.length == 1)
throw new TypeError();
var i = len - 1;
if (arguments.length >= 2) {
var rv = arguments[1];
} else {
do {
if (i in this) {
rv = this[i--];
break;
}
// if array contains no values, no initial value to return
if (--i < 0)
throw new TypeError();
} while (true);
}
for (; i >= 0; i--) {
if (i in this)
rv = fun.call(null, rv, this[i], i, this);
}
return rv;
};
}
if(!Array.prototype.indexOf){
Array.prototype.indexOf = function(obj){
for(var i=0; i<this.length; i++){
if(this[i]==obj){
return i;
}
}
return -1;
}
}