| 1 | 'use strict';
|
|---|
| 2 | var $ = require('../internals/export');
|
|---|
| 3 | var aCallable = require('../internals/a-callable');
|
|---|
| 4 | var aWeakMap = require('../internals/a-weak-map');
|
|---|
| 5 | var aWeakKey = require('../internals/a-weak-key');
|
|---|
| 6 | var WeakMapHelpers = require('../internals/weak-map-helpers');
|
|---|
| 7 | var IS_PURE = require('../internals/is-pure');
|
|---|
| 8 |
|
|---|
| 9 | var get = WeakMapHelpers.get;
|
|---|
| 10 | var has = WeakMapHelpers.has;
|
|---|
| 11 | var set = WeakMapHelpers.set;
|
|---|
| 12 |
|
|---|
| 13 | var FORCED = IS_PURE || !function () {
|
|---|
| 14 | try {
|
|---|
| 15 | // eslint-disable-next-line es/no-weak-map, no-throw-literal -- testing
|
|---|
| 16 | if (WeakMap.prototype.getOrInsertComputed) new WeakMap().getOrInsertComputed(1, function () { throw 1; });
|
|---|
| 17 | } catch (error) {
|
|---|
| 18 | // FF144 Nightly - Beta 3 bug
|
|---|
| 19 | // https://bugzilla.mozilla.org/show_bug.cgi?id=1988369
|
|---|
| 20 | return error instanceof TypeError;
|
|---|
| 21 | }
|
|---|
| 22 | }();
|
|---|
| 23 |
|
|---|
| 24 | // `WeakMap.prototype.getOrInsertComputed` method
|
|---|
| 25 | // https://tc39.es/ecma262/#sec-weakmap.prototype.getorinsertcomputed
|
|---|
| 26 | $({ target: 'WeakMap', proto: true, real: true, forced: FORCED }, {
|
|---|
| 27 | getOrInsertComputed: function getOrInsertComputed(key, callbackfn) {
|
|---|
| 28 | if (!IS_PURE) aWeakMap(this);
|
|---|
| 29 | aWeakKey(key);
|
|---|
| 30 | aCallable(callbackfn);
|
|---|
| 31 | if (has(this, key)) return get(this, key);
|
|---|
| 32 | var value = callbackfn(key);
|
|---|
| 33 | set(this, key, value);
|
|---|
| 34 | return value;
|
|---|
| 35 | }
|
|---|
| 36 | });
|
|---|