mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-24 09:25:15 +00:00
93 lines
2.7 KiB
JavaScript
93 lines
2.7 KiB
JavaScript
|
var $ = require('../internals/export');
|
||
|
var hiddenKeys = require('../internals/hidden-keys');
|
||
|
var isObject = require('../internals/is-object');
|
||
|
var has = require('../internals/has');
|
||
|
var defineProperty = require('../internals/object-define-property').f;
|
||
|
var getOwnPropertyNamesModule = require('../internals/object-get-own-property-names');
|
||
|
var getOwnPropertyNamesExternalModule = require('../internals/object-get-own-property-names-external');
|
||
|
var uid = require('../internals/uid');
|
||
|
var FREEZING = require('../internals/freezing');
|
||
|
|
||
|
var REQUIRED = false;
|
||
|
var METADATA = uid('meta');
|
||
|
var id = 0;
|
||
|
|
||
|
// eslint-disable-next-line es/no-object-isextensible -- safe
|
||
|
var isExtensible = Object.isExtensible || function () {
|
||
|
return true;
|
||
|
};
|
||
|
|
||
|
var setMetadata = function (it) {
|
||
|
defineProperty(it, METADATA, { value: {
|
||
|
objectID: 'O' + id++, // object ID
|
||
|
weakData: {} // weak collections IDs
|
||
|
} });
|
||
|
};
|
||
|
|
||
|
var fastKey = function (it, create) {
|
||
|
// return a primitive with prefix
|
||
|
if (!isObject(it)) return typeof it == 'symbol' ? it : (typeof it == 'string' ? 'S' : 'P') + it;
|
||
|
if (!has(it, METADATA)) {
|
||
|
// 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
|
||
|
setMetadata(it);
|
||
|
// return object ID
|
||
|
} return it[METADATA].objectID;
|
||
|
};
|
||
|
|
||
|
var getWeakData = function (it, create) {
|
||
|
if (!has(it, METADATA)) {
|
||
|
// 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
|
||
|
setMetadata(it);
|
||
|
// return the store of weak collections IDs
|
||
|
} return it[METADATA].weakData;
|
||
|
};
|
||
|
|
||
|
// add metadata on freeze-family methods calling
|
||
|
var onFreeze = function (it) {
|
||
|
if (FREEZING && REQUIRED && isExtensible(it) && !has(it, METADATA)) setMetadata(it);
|
||
|
return it;
|
||
|
};
|
||
|
|
||
|
var enable = function () {
|
||
|
meta.enable = function () { /* empty */ };
|
||
|
REQUIRED = true;
|
||
|
var getOwnPropertyNames = getOwnPropertyNamesModule.f;
|
||
|
var splice = [].splice;
|
||
|
var test = {};
|
||
|
test[METADATA] = 1;
|
||
|
|
||
|
// prevent exposing of metadata key
|
||
|
if (getOwnPropertyNames(test).length) {
|
||
|
getOwnPropertyNamesModule.f = function (it) {
|
||
|
var result = getOwnPropertyNames(it);
|
||
|
for (var i = 0, length = result.length; i < length; i++) {
|
||
|
if (result[i] === METADATA) {
|
||
|
splice.call(result, i, 1);
|
||
|
break;
|
||
|
}
|
||
|
} return result;
|
||
|
};
|
||
|
|
||
|
$({ target: 'Object', stat: true, forced: true }, {
|
||
|
getOwnPropertyNames: getOwnPropertyNamesExternalModule.f
|
||
|
});
|
||
|
}
|
||
|
};
|
||
|
|
||
|
var meta = module.exports = {
|
||
|
enable: enable,
|
||
|
fastKey: fastKey,
|
||
|
getWeakData: getWeakData,
|
||
|
onFreeze: onFreeze
|
||
|
};
|
||
|
|
||
|
hiddenKeys[METADATA] = true;
|