From 6d59d2fe60fd7d1b7c76500998497e217cb7a3bf Mon Sep 17 00:00:00 2001 From: Matthew Gaudet Date: Tue, 7 Jul 2020 18:53:04 +0000 Subject: [PATCH] Bug 1642476 - Correctly name anonymous private functions bound to private fields. r=jorendorff Differential Revision: https://phabricator.services.mozilla.com/D77930 --- js/src/tests/non262/PrivateName/names.js | 31 ++++++++++++++++++++++++ js/src/vm/JSFunction.cpp | 16 +++++++++--- 2 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 js/src/tests/non262/PrivateName/names.js diff --git a/js/src/tests/non262/PrivateName/names.js b/js/src/tests/non262/PrivateName/names.js new file mode 100644 index 0000000000000..e44fc5935b9df --- /dev/null +++ b/js/src/tests/non262/PrivateName/names.js @@ -0,0 +1,31 @@ +// |reftest| skip-if(!xulRuntime.shell) shell-option(--enable-private-fields) + +var C = class { + static #field = () => 'Test262'; + static field = () => 'Test262'; + #instance = () => 'Test262'; + instance = () => 'Test262'; + + static accessPrivateField() { + return this.#field; + } + + accessPrivateInstanceField() { + return this.#instance; + } + + static accessField() { + return this.field; + } + + accessInstanceField() { + return this.instance; + } +} +assertEq(C.accessPrivateField().name, '#field') +assertEq(C.accessField().name, 'field'); +var c = new C; +assertEq(c.accessPrivateInstanceField().name, '#instance'); +assertEq(c.accessInstanceField().name, 'instance'); + +if (typeof reportCompare === 'function') reportCompare(0, 0); diff --git a/js/src/vm/JSFunction.cpp b/js/src/vm/JSFunction.cpp index ef1b9bb717eb7..ca33362c478b3 100644 --- a/js/src/vm/JSFunction.cpp +++ b/js/src/vm/JSFunction.cpp @@ -2362,9 +2362,19 @@ static JSAtom* SymbolToFunctionName(JSContext* cx, JS::Symbol* symbol, // Step 4.b. if (desc) { - // Step 4.c. - if (!sb.append('[') || !sb.append(desc) || !sb.append(']')) { - return nullptr; + // Note: Private symbols are wedged in, as implementation wise they're + // PrivateNameSymbols with a the source level name as a description + // i.e. obj.#f desugars to obj.[PrivateNameSymbol("#f")], however + // they don't use the symbol naming, but rather property naming. + if (symbol->isPrivateName()) { + if (!sb.append(desc)) { + return nullptr; + } + } else { + // Step 4.c. + if (!sb.append('[') || !sb.append(desc) || !sb.append(']')) { + return nullptr; + } } } return sb.finishAtom();