2
* This file contains polyfills, implementing functionality that exists in WebEngine,
3
* but isn't yet supported by PhantomJS.
5
* These polyfills have been taken from Mozilla Developer Network.
7
// Production steps of ECMA-262, Edition 6, 22.1.2.1
9
Array.from = (function () {
10
var toStr = Object.prototype.toString;
11
var isCallable = function (fn) {
12
return typeof fn === 'function' || toStr.call(fn) === '[object Function]';
14
var toInteger = function (value) {
15
var number = Number(value);
16
if (isNaN(number)) { return 0; }
17
if (number === 0 || !isFinite(number)) { return number; }
18
return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));
20
var maxSafeInteger = Math.pow(2, 53) - 1;
21
var toLength = function (value) {
22
var len = toInteger(value);
23
return Math.min(Math.max(len, 0), maxSafeInteger);
26
// The length property of the from method is 1.
27
return function from(arrayLike/*, mapFn, thisArg */) {
28
// 1. Let C be the this value.
31
// 2. Let items be ToObject(arrayLike).
32
var items = Object(arrayLike);
34
// 3. ReturnIfAbrupt(items).
35
if (arrayLike == null) {
36
throw new TypeError('Array.from requires an array-like object - not null or undefined');
39
// 4. If mapfn is undefined, then let mapping be false.
40
var mapFn = arguments.length > 1 ? arguments[1] : void undefined;
42
if (typeof mapFn !== 'undefined') {
44
// 5. a If IsCallable(mapfn) is false, throw a TypeError exception.
45
if (!isCallable(mapFn)) {
46
throw new TypeError('Array.from: when provided, the second argument must be a function');
49
// 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined.
50
if (arguments.length > 2) {
55
// 10. Let lenValue be Get(items, "length").
56
// 11. Let len be ToLength(lenValue).
57
var len = toLength(items.length);
59
// 13. If IsConstructor(C) is true, then
60
// 13. a. Let A be the result of calling the [[Construct]] internal method
61
// of C with an argument list containing the single item len.
62
// 14. a. Else, Let A be ArrayCreate(len).
63
var A = isCallable(C) ? Object(new C(len)) : new Array(len);
67
// 17. Repeat, while k < len… (also steps a - h)
72
A[k] = typeof T === 'undefined' ? mapFn(kValue, k) : mapFn.call(T, kValue, k);
78
// 18. Let putStatus be Put(A, "length", len, true).