1 | var $ = require('../internals/export');
|
---|
2 | var hiddenKeys = require('../internals/hidden-keys');
|
---|
3 | var isObject = require('../internals/is-object');
|
---|
4 | var has = require('../internals/has');
|
---|
5 | var defineProperty = require('../internals/object-define-property').f;
|
---|
6 | var getOwnPropertyNamesModule = require('../internals/object-get-own-property-names');
|
---|
7 | var getOwnPropertyNamesExternalModule = require('../internals/object-get-own-property-names-external');
|
---|
8 | var uid = require('../internals/uid');
|
---|
9 | var FREEZING = require('../internals/freezing');
|
---|
10 |
|
---|
11 | var REQUIRED = false;
|
---|
12 | var METADATA = uid('meta');
|
---|
13 | var id = 0;
|
---|
14 |
|
---|
15 | // eslint-disable-next-line es/no-object-isextensible -- safe
|
---|
16 | var isExtensible = Object.isExtensible || function () {
|
---|
17 | return true;
|
---|
18 | };
|
---|
19 |
|
---|
20 | var setMetadata = function (it) {
|
---|
21 | defineProperty(it, METADATA, { value: {
|
---|
22 | objectID: 'O' + id++, // object ID
|
---|
23 | weakData: {} // weak collections IDs
|
---|
24 | } });
|
---|
25 | };
|
---|
26 |
|
---|
27 | var fastKey = function (it, create) {
|
---|
28 | // return a primitive with prefix
|
---|
29 | if (!isObject(it)) return typeof it == 'symbol' ? it : (typeof it == 'string' ? 'S' : 'P') + it;
|
---|
30 | if (!has(it, METADATA)) {
|
---|
31 | // can't set metadata to uncaught frozen object
|
---|
32 | if (!isExtensible(it)) return 'F';
|
---|
33 | // not necessary to add metadata
|
---|
34 | if (!create) return 'E';
|
---|
35 | // add missing metadata
|
---|
36 | setMetadata(it);
|
---|
37 | // return object ID
|
---|
38 | } return it[METADATA].objectID;
|
---|
39 | };
|
---|
40 |
|
---|
41 | var getWeakData = function (it, create) {
|
---|
42 | if (!has(it, METADATA)) {
|
---|
43 | // can't set metadata to uncaught frozen object
|
---|
44 | if (!isExtensible(it)) return true;
|
---|
45 | // not necessary to add metadata
|
---|
46 | if (!create) return false;
|
---|
47 | // add missing metadata
|
---|
48 | setMetadata(it);
|
---|
49 | // return the store of weak collections IDs
|
---|
50 | } return it[METADATA].weakData;
|
---|
51 | };
|
---|
52 |
|
---|
53 | // add metadata on freeze-family methods calling
|
---|
54 | var onFreeze = function (it) {
|
---|
55 | if (FREEZING && REQUIRED && isExtensible(it) && !has(it, METADATA)) setMetadata(it);
|
---|
56 | return it;
|
---|
57 | };
|
---|
58 |
|
---|
59 | var enable = function () {
|
---|
60 | meta.enable = function () { /* empty */ };
|
---|
61 | REQUIRED = true;
|
---|
62 | var getOwnPropertyNames = getOwnPropertyNamesModule.f;
|
---|
63 | var splice = [].splice;
|
---|
64 | var test = {};
|
---|
65 | test[METADATA] = 1;
|
---|
66 |
|
---|
67 | // prevent exposing of metadata key
|
---|
68 | if (getOwnPropertyNames(test).length) {
|
---|
69 | getOwnPropertyNamesModule.f = function (it) {
|
---|
70 | var result = getOwnPropertyNames(it);
|
---|
71 | for (var i = 0, length = result.length; i < length; i++) {
|
---|
72 | if (result[i] === METADATA) {
|
---|
73 | splice.call(result, i, 1);
|
---|
74 | break;
|
---|
75 | }
|
---|
76 | } return result;
|
---|
77 | };
|
---|
78 |
|
---|
79 | $({ target: 'Object', stat: true, forced: true }, {
|
---|
80 | getOwnPropertyNames: getOwnPropertyNamesExternalModule.f
|
---|
81 | });
|
---|
82 | }
|
---|
83 | };
|
---|
84 |
|
---|
85 | var meta = module.exports = {
|
---|
86 | enable: enable,
|
---|
87 | fastKey: fastKey,
|
---|
88 | getWeakData: getWeakData,
|
---|
89 | onFreeze: onFreeze
|
---|
90 | };
|
---|
91 |
|
---|
92 | hiddenKeys[METADATA] = true;
|
---|