-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve collections polyfills - O(1) and prevent possible leaking wit…
…h frozen keys, correct observable state for object keys, close #134
- Loading branch information
Showing
20 changed files
with
237 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
var META = require('./_uid')('meta') | ||
, isObject = require('./_is-object') | ||
, has = require('./_has') | ||
, setDesc = require('./_').setDesc | ||
, id = 0; | ||
var isExtensible = Object.isExtensible || function(){ | ||
return true; | ||
}; | ||
var FREEZE = !require('./_fails')(function(){ | ||
return isExtensible(Object.preventExtensions({})); | ||
}); | ||
var setMeta = function(it){ | ||
setDesc(it, META, {value: { | ||
i: 'O' + ++id, // object ID | ||
w: {} // weak collections IDs | ||
}}); | ||
}; | ||
var fastKey = function(it, create){ | ||
// return primitive with prefix | ||
if(!isObject(it))return typeof it == 'symbol' ? it : (typeof it == 'string' ? 'S' : 'P') + it; | ||
if(!has(it, META)){ | ||
// can't set metadata to uncaught frozen object | ||
if(!isExtensible(it))return 'F'; | ||
// not necessary to add metadata | ||
if(!create)return 'E'; | ||
// add missing metadata | ||
setMeta(it); | ||
// return object ID | ||
} return it[META].i; | ||
}; | ||
var getWeak = function(it, create){ | ||
if(!has(it, META)){ | ||
// can't set metadata to uncaught frozen object | ||
if(!isExtensible(it))return true; | ||
// not necessary to add metadata | ||
if(!create)return false; | ||
// add missing metadata | ||
setMeta(it); | ||
// return hash weak collections IDs | ||
} return it[META].w; | ||
}; | ||
// add metadata on freeze-family methods calling | ||
var onFreeze = function(it){ | ||
if(FREEZE && meta.NEED && isExtensible(it) && !has(it, META))setMeta(it); | ||
return it; | ||
}; | ||
var meta = module.exports = { | ||
KEY: META, | ||
NEED: false, | ||
fastKey: fastKey, | ||
getWeak: getWeak, | ||
onFreeze: onFreeze | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
// 19.1.2.5 Object.freeze(O) | ||
var isObject = require('./_is-object'); | ||
var isObject = require('./_is-object') | ||
, meta = require('./_meta').onFreeze; | ||
|
||
require('./_object-sap')('freeze', function($freeze){ | ||
return function freeze(it){ | ||
return $freeze && isObject(it) ? $freeze(it) : it; | ||
return $freeze && isObject(it) ? $freeze(meta(it)) : it; | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
// 19.1.2.15 Object.preventExtensions(O) | ||
var isObject = require('./_is-object'); | ||
var isObject = require('./_is-object') | ||
, meta = require('./_meta').onFreeze; | ||
|
||
require('./_object-sap')('preventExtensions', function($preventExtensions){ | ||
return function preventExtensions(it){ | ||
return $preventExtensions && isObject(it) ? $preventExtensions(it) : it; | ||
return $preventExtensions && isObject(it) ? $preventExtensions(meta(it)) : it; | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
// 19.1.2.17 Object.seal(O) | ||
var isObject = require('./_is-object'); | ||
var isObject = require('./_is-object') | ||
, meta = require('./_meta').onFreeze; | ||
|
||
require('./_object-sap')('seal', function($seal){ | ||
return function seal(it){ | ||
return $seal && isObject(it) ? $seal(it) : it; | ||
return $seal && isObject(it) ? $seal(meta(it)) : it; | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.