Projekt

Obecné

Profil

Stáhnout (1.25 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2

    
3
var implementation = require('./implementation');
4

    
5
var lacksProperEnumerationOrder = function () {
6
	if (!Object.assign) {
7
		return false;
8
	}
9
	// v8, specifically in node 4.x, has a bug with incorrect property enumeration order
10
	// note: this does not detect the bug unless there's 20 characters
11
	var str = 'abcdefghijklmnopqrst';
12
	var letters = str.split('');
13
	var map = {};
14
	for (var i = 0; i < letters.length; ++i) {
15
		map[letters[i]] = letters[i];
16
	}
17
	var obj = Object.assign({}, map);
18
	var actual = '';
19
	for (var k in obj) {
20
		actual += k;
21
	}
22
	return str !== actual;
23
};
24

    
25
var assignHasPendingExceptions = function () {
26
	if (!Object.assign || !Object.preventExtensions) {
27
		return false;
28
	}
29
	// Firefox 37 still has "pending exception" logic in its Object.assign implementation,
30
	// which is 72% slower than our shim, and Firefox 40's native implementation.
31
	var thrower = Object.preventExtensions({ 1: 2 });
32
	try {
33
		Object.assign(thrower, 'xy');
34
	} catch (e) {
35
		return thrower[1] === 'y';
36
	}
37
	return false;
38
};
39

    
40
module.exports = function getPolyfill() {
41
	if (!Object.assign) {
42
		return implementation;
43
	}
44
	if (lacksProperEnumerationOrder()) {
45
		return implementation;
46
	}
47
	if (assignHasPendingExceptions()) {
48
		return implementation;
49
	}
50
	return Object.assign;
51
};
(11-11/13)