| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6d3f43c commit 6af5358
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,6 +41,7 @@ const { | |||
| 41 | 41 | StringPrototypeToUpperCase, | |
| 42 | 42 | Symbol, | |
| 43 | 43 | SymbolFor, | |
| 44 | + SymbolPrototypeGetDescription, | ||
| 44 | 45 | SymbolReplace, | |
| 45 | 46 | SymbolSplit, | |
| 46 | 47 | } = primordials; | |
@@ -67,6 +68,7 @@ const { | |||
| 67 | 68 | } = internalBinding('util'); | |
| 68 | 69 | const { isNativeError, isPromise } = internalBinding('types'); | |
| 69 | 70 | const { getOptionValue } = require('internal/options'); | |
| 71 | + const assert = require('internal/assert'); | ||
| 70 | 72 | const { encodings } = internalBinding('string_decoder'); | |
| 71 | 73 | ||
| 72 | 74 | const noCrypto = !process.versions.openssl; | |
@@ -897,10 +899,37 @@ const encodingsMap = { __proto__: null }; | |||
| 897 | 899 | for (let i = 0; i < encodings.length; ++i) | |
| 898 | 900 | encodingsMap[encodings[i]] = i; | |
| 899 | 901 | ||
| 902 | + /** | ||
| 903 | + * Reassigns the .name property of a function. | ||
| 904 | + * Should be used when function can't be initially defined with desired name | ||
| 905 | + * or when desired name should include `#`, `[`, `]`, etc. | ||
| 906 | + * @param {string | symbol} name | ||
| 907 | + * @param {Function} fn | ||
| 908 | + * @param {object} [descriptor] | ||
| 909 | + * @returns {Function} the same function, renamed | ||
| 910 | + */ | ||
| 911 | + function assignFunctionName(name, fn, descriptor = kEmptyObject) { | ||
| 912 | + if (typeof name !== 'string') { | ||
| 913 | + const symbolDescription = SymbolPrototypeGetDescription(name); | ||
| 914 | + assert(symbolDescription !== undefined, 'Attempted to name function after descriptionless Symbol'); | ||
| 915 | + name = `[${symbolDescription}]`; | ||
| 916 | + } | ||
| 917 | + return ObjectDefineProperty(fn, 'name', { | ||
| 918 | + __proto__: null, | ||
| 919 | + writable: false, | ||
| 920 | + enumerable: false, | ||
| 921 | + configurable: true, | ||
| 922 | + ...ObjectGetOwnPropertyDescriptor(fn, 'name'), | ||
| 923 | + ...descriptor, | ||
| 924 | + value: name, | ||
| 925 | + }); | ||
| 926 | + } | ||
| 927 | + | ||
| 900 | 928 | module.exports = { | |
| 901 | 929 | getLazy, | |
| 902 | 930 | assertCrypto, | |
| 903 | 931 | assertTypeScript, | |
| 932 | + assignFunctionName, | ||
| 904 | 933 | cachedResult, | |
| 905 | 934 | convertToValidSignal, | |
| 906 | 935 | createClassWrapper, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,7 +4,7 @@ | |||
| 4 | 4 | require('../common'); | |
| 5 | 5 | const assert = require('assert'); | |
| 6 | 6 | const { types } = require('util'); | |
| 7 | - const { isError } = require('internal/util'); | ||
| 7 | + const { assignFunctionName, isError } = require('internal/util'); | ||
| 8 | 8 | const vm = require('vm'); | |
| 9 | 9 | ||
| 10 | 10 | // Special cased errors. Test the internal function which is used in | |
@@ -35,3 +35,30 @@ const vm = require('vm'); | |||
| 35 | 35 | assert(!(differentRealmErr instanceof Error)); | |
| 36 | 36 | assert(isError(differentRealmErr)); | |
| 37 | 37 | } | |
| 38 | + | ||
| 39 | + { | ||
| 40 | + const nameMap = new Map([ | ||
| 41 | + [ 'meaningfulName', 'meaningfulName' ], | ||
| 42 | + [ '', '' ], | ||
| 43 | + [ Symbol.asyncIterator, '[Symbol.asyncIterator]' ], | ||
| 44 | + [ Symbol('notWellKnownSymbol'), '[notWellKnownSymbol]' ], | ||
| 45 | + ]); | ||
| 46 | + for (const fn of [ | ||
| 47 | + () => {}, | ||
| 48 | + function() {}, | ||
| 49 | + function value() {}, | ||
| 50 | + ({ value() {} }).value, | ||
| 51 | + new Function(), | ||
| 52 | + Function(), | ||
| 53 | + function() {}.bind(null), | ||
| 54 | + class {}, | ||
| 55 | + class value {}, | ||
| 56 | + ]) { | ||
| 57 | + for (const [ stringOrSymbol, expectedName ] of nameMap) { | ||
| 58 | + const namedFn = assignFunctionName(stringOrSymbol, fn); | ||
| 59 | + assert.strictEqual(fn, namedFn); | ||
| 60 | + assert.strictEqual(namedFn.name, expectedName); | ||
| 61 | + assert.strictEqual(namedFn.bind(null).name, `bound ${expectedName}`); | ||
| 62 | + } | ||
| 63 | + } | ||
| 64 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments