Ignore:
Timestamp:
Feb 18, 2021, 3:48:28 PM (4 years ago)
Author:
Caio Lima
Message:

[JSC] Implement private static method
https://bugs.webkit.org/show_bug.cgi?id=219181

Reviewed by Yusuke Suzuki.

JSTests:

  • stress/class-private-method-access.js: Added.
  • stress/private-accessor-static-non-static.js: Added.
  • stress/private-getter-inner-class.js:
  • stress/static-private-methods-and-accessor-inner-class.js: Added.
  • stress/static-private-methods-and-accessor-multiple-evaluation.js: Added.
  • stress/static-private-methods-and-accessors-postfix-node.js: Added.
  • stress/static-private-methods-and-accessors-prefix-node.js: Added.
  • test262/config.yaml:

Source/JavaScriptCore:

This patch is implementing static private methods and accessors
proposal based on https://github.com/tc39/proposal-static-class-features.
This implementation diverge a bit from private methods&accessors on the
brand check, because we are using a simpler way to perform static
brand checks. Since only the class constructor is allowed to access
its private methods and accessors, we save it on @privateClassBrand
on class lexical scope and compare it with the receiver of the static
private method (and accessors) using === operation.
While this genenrates more bytecodes than check_private_brand, we
don't need to perform a Structure transition to install a brand,
and avoid allocation of a private symbol. Since each evaluation of a
class generates a different constructor object, we preserve the semantics
that private methods are lexically scoped.

  • builtins/BuiltinNames.h:
  • bytecompiler/BytecodeGenerator.cpp:

(JSC::BytecodeGenerator::emitCreatePrivateBrand):
(JSC::BytecodeGenerator::emitInstallPrivateBrand):
(JSC::BytecodeGenerator::emitInstallPrivateClassBrand):
(JSC::BytecodeGenerator::emitGetPrivateBrand):
(JSC::BytecodeGenerator::emitCheckPrivateBrand):

  • bytecompiler/BytecodeGenerator.h:
  • bytecompiler/NodesCodegen.cpp:

(JSC::BaseDotNode::emitGetPropertyValue):
(JSC::BaseDotNode::emitPutProperty):
(JSC::PostfixNode::emitDot):
(JSC::PrefixNode::emitDot):
(JSC::ClassExprNode::emitBytecode):

  • parser/Parser.cpp:

(JSC::Parser<LexerType>::parseClass):
(JSC::Parser<LexerType>::parseGetterSetter):

  • parser/Parser.h:

(JSC::Scope::declarePrivateMethod):
(JSC::Scope::declarePrivateAccessor):
(JSC::Scope::declarePrivateSetter):
(JSC::Scope::declarePrivateGetter):

  • parser/VariableEnvironment.cpp:

(JSC::VariableEnvironment::declarePrivateAccessor):
(JSC::VariableEnvironment::declarePrivateSetter):
(JSC::VariableEnvironment::declarePrivateGetter):
(JSC::VariableEnvironment::declarePrivateMethod):

  • parser/VariableEnvironment.h:

(JSC::PrivateNameEntry::isStatic const):
(JSC::VariableEnvironment::isEmpty const):
(JSC::VariableEnvironment::declareStaticPrivateMethod):
(JSC::VariableEnvironment::declarePrivateSetter):
(JSC::VariableEnvironment::declareStaticPrivateSetter):
(JSC::VariableEnvironment::declarePrivateGetter):
(JSC::VariableEnvironment::declareStaticPrivateGetter):
(JSC::VariableEnvironment::hasStaticPrivateMethodOrAccessor const):
(JSC::VariableEnvironment::hasInstancePrivateMethodOrAccessor const):
(JSC::VariableEnvironment::hasPrivateMethodOrAccessor const): Deleted.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp

    r272883 r273107  
    983983            RefPtr<RegisterID> scope = generator.emitResolveScope(nullptr, var);
    984984
    985             RefPtr<RegisterID> privateBrandSymbol = generator.emitGetPrivateBrand(generator.newTemporary(), scope.get());
    986             generator.emitCheckPrivateBrand(base, privateBrandSymbol.get());
     985            RefPtr<RegisterID> privateBrandSymbol = generator.emitGetPrivateBrand(generator.newTemporary(), scope.get(), privateTraits.isStatic());
     986            generator.emitCheckPrivateBrand(base, privateBrandSymbol.get(), privateTraits.isStatic());
    987987
    988988            return generator.emitGetFromScope(dst, scope.get(), var, ThrowIfNotFound);
     
    993993            RefPtr<RegisterID> scope = generator.emitResolveScope(nullptr, var);
    994994
    995             RefPtr<RegisterID> privateBrandSymbol = generator.emitGetPrivateBrand(generator.newTemporary(), scope.get());
    996             generator.emitCheckPrivateBrand(base, privateBrandSymbol.get());
     995            RefPtr<RegisterID> privateBrandSymbol = generator.emitGetPrivateBrand(generator.newTemporary(), scope.get(), privateTraits.isStatic());
     996            generator.emitCheckPrivateBrand(base, privateBrandSymbol.get(), privateTraits.isStatic());
    997997
    998998            RefPtr<RegisterID> getterSetterObj = generator.emitGetFromScope(generator.newTemporary(), scope.get(), var, ThrowIfNotFound);
     
    10081008            RefPtr<RegisterID> scope = generator.emitResolveScope(nullptr, var);
    10091009
    1010             RefPtr<RegisterID> privateBrandSymbol = generator.emitGetPrivateBrand(generator.newTemporary(), scope.get());
    1011             generator.emitCheckPrivateBrand(base, privateBrandSymbol.get());
     1010            RefPtr<RegisterID> privateBrandSymbol = generator.emitGetPrivateBrand(generator.newTemporary(), scope.get(), privateTraits.isStatic());
     1011            generator.emitCheckPrivateBrand(base, privateBrandSymbol.get(), privateTraits.isStatic());
    10121012            generator.emitThrowTypeError("Trying to access an undefined private getter");
    10131013            return dst;
     
    10481048            RefPtr<RegisterID> scope = generator.emitResolveScope(nullptr, var);
    10491049
    1050             RefPtr<RegisterID> privateBrandSymbol = generator.emitGetPrivateBrand(generator.newTemporary(), scope.get());
    1051             generator.emitCheckPrivateBrand(base, privateBrandSymbol.get());
     1050            RefPtr<RegisterID> privateBrandSymbol = generator.emitGetPrivateBrand(generator.newTemporary(), scope.get(), privateTraits.isStatic());
     1051            generator.emitCheckPrivateBrand(base, privateBrandSymbol.get(), privateTraits.isStatic());
    10521052
    10531053            RefPtr<RegisterID> getterSetterObj = generator.emitGetFromScope(generator.newTemporary(), scope.get(), var, ThrowIfNotFound);
     
    10651065            RefPtr<RegisterID> scope = generator.emitResolveScope(nullptr, var);
    10661066
    1067             RefPtr<RegisterID> privateBrandSymbol = generator.emitGetPrivateBrand(generator.newTemporary(), scope.get());
    1068             generator.emitCheckPrivateBrand(base, privateBrandSymbol.get());
     1067            RefPtr<RegisterID> privateBrandSymbol = generator.emitGetPrivateBrand(generator.newTemporary(), scope.get(), privateTraits.isStatic());
     1068            generator.emitCheckPrivateBrand(base, privateBrandSymbol.get(), privateTraits.isStatic());
    10691069
    10701070            generator.emitThrowTypeError("Trying to access an undefined private setter");
     
    24132413            RefPtr<RegisterID> scope = generator.emitResolveScope(nullptr, var);
    24142414
    2415             RefPtr<RegisterID> privateBrandSymbol = generator.emitGetPrivateBrand(generator.newTemporary(), scope.get());
    2416             generator.emitCheckPrivateBrand(base.get(), privateBrandSymbol.get());
     2415            RefPtr<RegisterID> privateBrandSymbol = generator.emitGetPrivateBrand(generator.newTemporary(), scope.get(), privateTraits.isStatic());
     2416            generator.emitCheckPrivateBrand(base.get(), privateBrandSymbol.get(), privateTraits.isStatic());
    24172417
    24182418            generator.emitExpressionInfo(divot(), divotStart(), divotEnd());
     
    24242424        RefPtr<RegisterID> scope = generator.emitResolveScope(nullptr, var);
    24252425
    2426         RefPtr<RegisterID> privateBrandSymbol = generator.emitGetPrivateBrand(generator.newTemporary(), scope.get());
    2427         generator.emitCheckPrivateBrand(base.get(), privateBrandSymbol.get());
     2426        RefPtr<RegisterID> privateBrandSymbol = generator.emitGetPrivateBrand(generator.newTemporary(), scope.get(), privateTraits.isStatic());
     2427        generator.emitCheckPrivateBrand(base.get(), privateBrandSymbol.get(), privateTraits.isStatic());
    24282428
    24292429        RefPtr<RegisterID> value;
     
    27022702            RefPtr<RegisterID> scope = generator.emitResolveScope(nullptr, var);
    27032703
    2704             RefPtr<RegisterID> privateBrandSymbol = generator.emitGetPrivateBrand(generator.newTemporary(), scope.get());
    2705             generator.emitCheckPrivateBrand(base.get(), privateBrandSymbol.get());
     2704            RefPtr<RegisterID> privateBrandSymbol = generator.emitGetPrivateBrand(generator.newTemporary(), scope.get(), privateTraits.isStatic());
     2705            generator.emitCheckPrivateBrand(base.get(), privateBrandSymbol.get(), privateTraits.isStatic());
    27062706
    27072707            generator.emitExpressionInfo(divot(), divotStart(), divotEnd());
     
    27132713        RefPtr<RegisterID> scope = generator.emitResolveScope(nullptr, var);
    27142714
    2715         RefPtr<RegisterID> privateBrandSymbol = generator.emitGetPrivateBrand(generator.newTemporary(), scope.get());
    2716         generator.emitCheckPrivateBrand(base.get(), privateBrandSymbol.get());
     2715        RefPtr<RegisterID> privateBrandSymbol = generator.emitGetPrivateBrand(generator.newTemporary(), scope.get(), privateTraits.isStatic());
     2716        generator.emitCheckPrivateBrand(base.get(), privateBrandSymbol.get(), privateTraits.isStatic());
    27172717
    27182718        if (privateTraits.isGetter()) {
     
    51535153
    51545154    bool hasPrivateNames = !!m_lexicalVariables.privateNamesSize();
    5155     bool shouldEmitPrivateBrand = m_lexicalVariables.hasPrivateMethodOrAccessor();
     5155    bool shouldEmitPrivateBrand = m_lexicalVariables.hasInstancePrivateMethodOrAccessor();
     5156    bool shouldInstallBrandOnConstructor = m_lexicalVariables.hasStaticPrivateMethodOrAccessor();
    51565157    if (hasPrivateNames)
    51575158        generator.pushPrivateAccessNames(m_lexicalVariables.privateNameEnvironment());
     
    52445245        generator.emitPutToScope(scope.get(), classNameVar, constructor.get(), ThrowIfNotFound, InitializationMode::Initialization);
    52455246    }
     5247
     5248    if (shouldInstallBrandOnConstructor)
     5249        generator.emitInstallPrivateClassBrand(constructor.get());
    52465250
    52475251    if (!staticFieldLocations.isEmpty()) {
Note: See TracChangeset for help on using the changeset viewer.