third_party/blink/renderer/bindings/scripts/bind_gen/codegen_expr.py
def expr_from_exposure(exposure,
                       global_names=None,
                       may_use_feature_selector=False):
    """
    Returns an expression to determine whether this property should be exposed
    or not.

    Args:
        exposure: web_idl.Exposure of the target construct.
        global_names: When specified, it's taken into account that the global
            object implements |global_names|.
        may_use_feature_selector: True enables use of ${feature_selector} iff
            the exposure is context dependent.
    """
    assert isinstance(exposure, web_idl.Exposure)
    assert (global_names is None
            or (isinstance(global_names, (list, tuple))
                and all(isinstance(name, str) for name in global_names)))

    # The property exposures are categorized into three.
    # - Unconditional: Always exposed.
    # - Context-independent: Enabled per v8::Isolate.
    # - Context-dependent: Enabled per v8::Context, e.g. origin trials.
    #
    # Context-dependent properties can be installed in two phases.
    # - The first phase installs all the properties that are associated with the
    #   features enabled at the moment.  This phase is represented by
    #   FeatureSelector as FeatureSelector.IsAll().
    # - The second phase installs the properties associated with the specified
    #   feature.  This phase is represented as FeatureSelector.IsAny(feature).
    #
    # The exposure condition is represented as;
    #   (and feature_selector-independent-term
    #        (or
    #         feature_selector-1st-phase-term
    #         feature_selector-2nd-phase-term))
    # which can be represented in more details as:
    #   (and cross_origin_isolated_term
    #        direct_socket_enabled_term
    #        secure_context_term
    #        uncond_exposed_term
    #        (or
    #         (and feature_selector.IsAll()  # 1st phase; all enabled
    #              cond_exposed_term
    #              (or feature_enabled_term
    #                  context_enabled_term))
    #         (or exposed_selector_term      # 2nd phase; any selected
    #             feature_selector_term)))
    # where
    #   cross_origin_isolated_term represents [CrossOriginIsolated]
    #   direct_socket_enabled_term represents [DirectSocketEnabled]
    #   secure_context_term represents [SecureContext=F1]
    #   uncond_exposed_term represents [Exposed=(G1, G2)]
    #   cond_exposed_term represents [Exposed(G1 F1, G2 F2)]
    #   feature_enabled_term represents [RuntimeEnabled=(F1, F2)]
    #   context_enabled_term represents [ContextEnabled=F1]
    #   exposed_selector_term represents [Exposed(G1 F1, G2 F2)]
    #   feature_selector_term represents [RuntimeEnabled=(F1, F2)]
    uncond_exposed_terms = []
    cond_exposed_terms = []
    feature_enabled_terms = []
    context_enabled_terms = []
    exposed_selector_terms = []
    feature_selector_names = []  # Will turn into feature_selector.IsAnyOf(...)

    def ref_enabled(feature):
        arg = "${execution_context}" if feature.is_context_dependent else ""
        return _Expr("RuntimeEnabledFeatures::{}Enabled({})".format(
            feature, arg))

    def ref_selected(features):
        feature_tokens = map(
            lambda feature: "OriginTrialFeature::k{}".format(feature),
            features)
        return _Expr("${{feature_selector}}.IsAnyOf({})".format(
            ", ".join(feature_tokens)))

    # [CrossOriginIsolated]
    if exposure.only_in_coi_contexts:
        cross_origin_isolated_term = _Expr("${is_cross_origin_isolated}")
    else:
        cross_origin_isolated_term = _Expr(True)

    # [DirectSocketEnabled]
    if exposure.only_in_direct_socket_contexts:
        direct_socket_enabled_term = _Expr("${is_direct_socket_enabled}")
    else:
        direct_socket_enabled_term = _Expr(True)

    # [SecureContext]
    if exposure.only_in_secure_contexts is True:
        secure_context_term = _Expr("${is_in_secure_context}")
    elif exposure.only_in_secure_contexts is False:
        secure_context_term = _Expr(True)
    else:
        terms = list(map(ref_enabled, exposure.only_in_secure_contexts))
        secure_context_term = expr_or(
            [_Expr("${is_in_secure_context}"),
             expr_not(expr_and(terms))])

    # [Exposed]
    GLOBAL_NAME_TO_EXECUTION_CONTEXT_TEST = {
        "AnimationWorklet": "IsAnimationWorkletGlobalScope",
        "AudioWorklet": "IsAudioWorkletGlobalScope",
        "DedicatedWorker": "IsDedicatedWorkerGlobalScope",
        "LayoutWorklet": "IsLayoutWorkletGlobalScope",
        "PaintWorklet": "IsPaintWorkletGlobalScope",
        "ServiceWorker": "IsServiceWorkerGlobalScope",
        "SharedWorker": "IsSharedWorkerGlobalScope",
        "Window": "IsWindow",
        "Worker": "IsWorkerGlobalScope",
        "Worklet": "IsWorkletGlobalScope",
    }
    if global_names:
        matched_global_count = 0
        for entry in exposure.global_names_and_features:
            if entry.global_name not in global_names:
                continue
            matched_global_count += 1
            if entry.feature:
                cond_exposed_terms.append(ref_enabled(entry.feature))
                if entry.feature.is_context_dependent:
                    feature_selector_names.append(entry.feature)
        assert (not exposure.global_names_and_features
                or matched_global_count > 0)
    else:
        for entry in exposure.global_names_and_features:
            pred_term = _Expr("${{execution_context}}->{}()".format(
                GLOBAL_NAME_TO_EXECUTION_CONTEXT_TEST[entry.global_name]))
            if not entry.feature:
                uncond_exposed_terms.append(pred_term)
            else:
                cond_exposed_terms.append(
                    expr_and([pred_term, ref_enabled(entry.feature)]))
                if entry.feature.is_context_dependent:
                    exposed_selector_terms.append(
                        expr_and([pred_term,
                                  ref_selected([entry.feature])]))

    # [RuntimeEnabled]
    if exposure.runtime_enabled_features:
        feature_enabled_terms.extend(
            map(ref_enabled, exposure.runtime_enabled_features))
        feature_selector_names.extend(
            exposure.context_dependent_runtime_enabled_features)

    # [ContextEnabled]
    if exposure.context_enabled_features:
        terms = list(
            map(
                lambda feature: _Expr(
                    "${{context_feature_settings}}->is{}Enabled()".format(
                        feature)), exposure.context_enabled_features))
        context_enabled_terms.append(
            expr_and([_Expr("${context_feature_settings}"),
                      expr_or(terms)]))

    # Build an expression.
    top_level_terms = []
    top_level_terms.append(cross_origin_isolated_term)
    top_level_terms.append(direct_socket_enabled_term)
    top_level_terms.append(secure_context_term)
    if uncond_exposed_terms:
        top_level_terms.append(expr_or(uncond_exposed_terms))

    if not (may_use_feature_selector
            and exposure.is_context_dependent(global_names)):
        if cond_exposed_terms:
            top_level_terms.append(expr_or(cond_exposed_terms))
        if feature_enabled_terms:
            top_level_terms.append(expr_and(feature_enabled_terms))
        return expr_and(top_level_terms)

    all_enabled_terms = [_Expr("${feature_selector}.IsAll()")]
    if cond_exposed_terms:
        all_enabled_terms.append(expr_or(cond_exposed_terms))
    if feature_enabled_terms or context_enabled_terms:
        terms = []
        if feature_enabled_terms:
            terms.append(expr_and(feature_enabled_terms))
        if context_enabled_terms:
            terms.append(expr_or(context_enabled_terms))
        all_enabled_terms.append(expr_or(terms))

    selector_terms = []
    if exposed_selector_terms:
        selector_terms.append(expr_or(exposed_selector_terms))
    if feature_selector_names:
        # Remove duplicates
        selector_terms.append(ref_selected(sorted(
            set(feature_selector_names))))

    terms = []
    terms.append(expr_and(all_enabled_terms))
    if selector_terms:
        terms.append(expr_or(selector_terms))
    top_level_terms.append(expr_or(terms))

    return expr_and(top_level_terms)

    Returns an expression to determine whether this property should be exposed
    or not.

    Args:
        exposure: web_idl.Exposure of the target construct.
        global_names: When specified, it's taken into account that the global
            object implements |global_names|.
        may_use_feature_selector: True enables use of ${feature_selector} iff
            the exposure is context dependent.
    
Returns an expression to determine whether this property should be exposed
or not.
Returns an expression to determine whether this property should be exposed or not .
def expr_from_exposure(exposure,
                       global_names=None,
                       may_use_feature_selector=False):
    assert isinstance(exposure, web_idl.Exposure)
    assert (global_names is None
            or (isinstance(global_names, (list, tuple))
                and all(isinstance(name, str) for name in global_names)))
     Context-dependent properties can be installed in two phases.
     - The first phase installs all the properties that are associated with the
       features enabled at the moment.  This phase is represented by
       FeatureSelector as FeatureSelector.IsAll().
     - The second phase installs the properties associated with the specified
       feature.  This phase is represented as FeatureSelector.IsAny(feature).
     The exposure condition is represented as;
       (and feature_selector-independent-term
            (or
             feature_selector-1st-phase-term
             feature_selector-2nd-phase-term))
     which can be represented in more details as:
       (and cross_origin_isolated_term
            direct_socket_enabled_term
            secure_context_term
            uncond_exposed_term
            (or
             (and feature_selector.IsAll()   1st phase; all enabled
                  cond_exposed_term
                  (or feature_enabled_term
                      context_enabled_term))
             (or exposed_selector_term       2nd phase; any selected
                 feature_selector_term)))
     where
       cross_origin_isolated_term represents [CrossOriginIsolated]
       direct_socket_enabled_term represents [DirectSocketEnabled]
       secure_context_term represents [SecureContext=F1]
       uncond_exposed_term represents [Exposed=(G1, G2)]
       cond_exposed_term represents [Exposed(G1 F1, G2 F2)]
       feature_enabled_term represents [RuntimeEnabled=(F1, F2)]
       context_enabled_term represents [ContextEnabled=F1]
       exposed_selector_term represents [Exposed(G1 F1, G2 F2)]
       feature_selector_term represents [RuntimeEnabled=(F1, F2)]
    uncond_exposed_terms = []
    cond_exposed_terms = []
    feature_enabled_terms = []
    context_enabled_terms = []
    exposed_selector_terms = []
    feature_selector_names = []   Will turn into feature_selector.IsAnyOf(...)
    def ref_enabled(feature):
        arg = "${execution_context}" if feature.is_context_dependent else ""
        return _Expr("RuntimeEnabledFeatures::{}Enabled({})".format(
            feature, arg))
    def ref_selected(features):
        feature_tokens = map(
            lambda feature: "OriginTrialFeature::k{}".format(feature),
            features)
        return _Expr("${{feature_selector}}.IsAnyOf({})".format(
            ", ".join(feature_tokens)))
     [CrossOriginIsolated]
    if exposure.only_in_coi_contexts:
        cross_origin_isolated_term = _Expr("${is_cross_origin_isolated}")
    else:
        cross_origin_isolated_term = _Expr(True)
     [DirectSocketEnabled]
    if exposure.only_in_direct_socket_contexts:
        direct_socket_enabled_term = _Expr("${is_direct_socket_enabled}")
    else:
        direct_socket_enabled_term = _Expr(True)
     [SecureContext]
    if exposure.only_in_secure_contexts is True:
        secure_context_term = _Expr("${is_in_secure_context}")
    elif exposure.only_in_secure_contexts is False:
        secure_context_term = _Expr(True)
    else:
        terms = list(map(ref_enabled, exposure.only_in_secure_contexts))
        secure_context_term = expr_or(
            [_Expr("${is_in_secure_context}"),
             expr_not(expr_and(terms))])
     [Exposed]
    GLOBAL_NAME_TO_EXECUTION_CONTEXT_TEST = {
        "AnimationWorklet": "IsAnimationWorkletGlobalScope",
        "AudioWorklet": "IsAudioWorkletGlobalScope",
        "DedicatedWorker": "IsDedicatedWorkerGlobalScope",
        "LayoutWorklet": "IsLayoutWorkletGlobalScope",
        "PaintWorklet": "IsPaintWorkletGlobalScope",
        "ServiceWorker": "IsServiceWorkerGlobalScope",
        "SharedWorker": "IsSharedWorkerGlobalScope",
        "Window": "IsWindow",
        "Worker": "IsWorkerGlobalScope",
        "Worklet": "IsWorkletGlobalScope",
    }
    if global_names:
        matched_global_count = 0
        for entry in exposure.global_names_and_features:
            if entry.global_name not in global_names:
                continue
            matched_global_count += 1
            if entry.feature:
                cond_exposed_terms.append(ref_enabled(entry.feature))
                if entry.feature.is_context_dependent:
                    feature_selector_names.append(entry.feature)
        assert (not exposure.global_names_and_features
                or matched_global_count > 0)
    else:
        for entry in exposure.global_names_and_features:
            pred_term = _Expr("${{execution_context}}->{}()".format(
                GLOBAL_NAME_TO_EXECUTION_CONTEXT_TEST[entry.global_name]))
            if not entry.feature:
                uncond_exposed_terms.append(pred_term)
            else:
                cond_exposed_terms.append(
                    expr_and([pred_term, ref_enabled(entry.feature)]))
                if entry.feature.is_context_dependent:
                    exposed_selector_terms.append(
                        expr_and([pred_term,
                                  ref_selected([entry.feature])]))
     [RuntimeEnabled]
    if exposure.runtime_enabled_features:
        feature_enabled_terms.extend(
            map(ref_enabled, exposure.runtime_enabled_features))
        feature_selector_names.extend(
            exposure.context_dependent_runtime_enabled_features)
     [ContextEnabled]
    if exposure.context_enabled_features:
        terms = list(
            map(
                lambda feature: _Expr(
                    "${{context_feature_settings}}->is{}Enabled()".format(
                        feature)), exposure.context_enabled_features))
        context_enabled_terms.append(
            expr_and([_Expr("${context_feature_settings}"),
                      expr_or(terms)]))
     Build an expression.
    top_level_terms = []
    top_level_terms.append(cross_origin_isolated_term)
    top_level_terms.append(direct_socket_enabled_term)
    top_level_terms.append(secure_context_term)
    if uncond_exposed_terms:
        top_level_terms.append(expr_or(uncond_exposed_terms))
    if not (may_use_feature_selector
            and exposure.is_context_dependent(global_names)):
        if cond_exposed_terms:
            top_level_terms.append(expr_or(cond_exposed_terms))
        if feature_enabled_terms:
            top_level_terms.append(expr_and(feature_enabled_terms))
        return expr_and(top_level_terms)
    all_enabled_terms = [_Expr("${feature_selector}.IsAll()")]
    if cond_exposed_terms:
        all_enabled_terms.append(expr_or(cond_exposed_terms))
    if feature_enabled_terms or context_enabled_terms:
        terms = []
        if feature_enabled_terms:
            terms.append(expr_and(feature_enabled_terms))
        if context_enabled_terms:
            terms.append(expr_or(context_enabled_terms))
        all_enabled_terms.append(expr_or(terms))
    selector_terms = []
    if exposed_selector_terms:
        selector_terms.append(expr_or(exposed_selector_terms))
    if feature_selector_names:
         Remove duplicates
        selector_terms.append(ref_selected(sorted(
            set(feature_selector_names))))
    terms = []
    terms.append(expr_and(all_enabled_terms))
    if selector_terms:
        terms.append(expr_or(selector_terms))
    top_level_terms.append(expr_or(terms))
    return expr_and(top_level_terms)
def expr_from_exposure ( exposure , global_names = None , may_use_feature_selector = False ) : assert isinstance ( exposure , web_idl . Exposure ) assert ( global_names is None or ( isinstance ( global_names , ( list , tuple ) ) and all ( isinstance ( name , str ) for name in global_names ) ) ) uncond_exposed_terms = [ ] cond_exposed_terms = [ ] feature_enabled_terms = [ ] context_enabled_terms = [ ] exposed_selector_terms = [ ] feature_selector_names = [ ] def ref_enabled ( feature ) : arg = "${execution_context}" if feature . is_context_dependent else "" return _Expr ( "RuntimeEnabledFeatures::{}Enabled({})" . format ( feature , arg ) ) def ref_selected ( features ) : feature_tokens = map ( lambda feature : "OriginTrialFeature::k{}" . format ( feature ) , features ) return _Expr ( "${{feature_selector}}.IsAnyOf({})" . format ( ", " . join ( feature_tokens ) ) ) if exposure . only_in_coi_contexts : cross_origin_isolated_term = _Expr ( "${is_cross_origin_isolated}" ) else : cross_origin_isolated_term = _Expr ( True ) if exposure . only_in_direct_socket_contexts : direct_socket_enabled_term = _Expr ( "${is_direct_socket_enabled}" ) else : direct_socket_enabled_term = _Expr ( True ) if exposure . only_in_secure_contexts is True : secure_context_term = _Expr ( "${is_in_secure_context}" ) elif exposure . only_in_secure_contexts is False : secure_context_term = _Expr ( True ) else : terms = list ( map ( ref_enabled , exposure . only_in_secure_contexts ) ) secure_context_term = expr_or ( [ _Expr ( "${is_in_secure_context}" ) , expr_not ( expr_and ( terms ) ) ] ) GLOBAL_NAME_TO_EXECUTION_CONTEXT_TEST = { "AnimationWorklet" : "IsAnimationWorkletGlobalScope" , "AudioWorklet" : "IsAudioWorkletGlobalScope" , "DedicatedWorker" : "IsDedicatedWorkerGlobalScope" , "LayoutWorklet" : "IsLayoutWorkletGlobalScope" , "PaintWorklet" : "IsPaintWorkletGlobalScope" , "ServiceWorker" : "IsServiceWorkerGlobalScope" , "SharedWorker" : "IsSharedWorkerGlobalScope" , "Window" : "IsWindow" , "Worker" : "IsWorkerGlobalScope" , "Worklet" : "IsWorkletGlobalScope" , } if global_names : matched_global_count = 0 for entry in exposure . global_names_and_features : if entry . global_name not in global_names : continue matched_global_count += 1 if entry . feature : cond_exposed_terms . append ( ref_enabled ( entry . feature ) ) if entry . feature . is_context_dependent : feature_selector_names . append ( entry . feature ) assert ( not exposure . global_names_and_features or matched_global_count > 0 ) else : for entry in exposure . global_names_and_features : pred_term = _Expr ( "${{execution_context}}->{}()" . format ( GLOBAL_NAME_TO_EXECUTION_CONTEXT_TEST [ entry . global_name ] ) ) if not entry . feature : uncond_exposed_terms . append ( pred_term ) else : cond_exposed_terms . append ( expr_and ( [ pred_term , ref_enabled ( entry . feature ) ] ) ) if entry . feature . is_context_dependent : exposed_selector_terms . append ( expr_and ( [ pred_term , ref_selected ( [ entry . feature ] ) ] ) ) if exposure . runtime_enabled_features : feature_enabled_terms . extend ( map ( ref_enabled , exposure . runtime_enabled_features ) ) feature_selector_names . extend ( exposure . context_dependent_runtime_enabled_features ) if exposure . context_enabled_features : terms = list ( map ( lambda feature : _Expr ( "${{context_feature_settings}}->is{}Enabled()" . format ( feature ) ) , exposure . context_enabled_features ) ) context_enabled_terms . append ( expr_and ( [ _Expr ( "${context_feature_settings}" ) , expr_or ( terms ) ] ) ) top_level_terms = [ ] top_level_terms . append ( cross_origin_isolated_term ) top_level_terms . append ( direct_socket_enabled_term ) top_level_terms . append ( secure_context_term ) if uncond_exposed_terms : top_level_terms . append ( expr_or ( uncond_exposed_terms ) ) if not ( may_use_feature_selector and exposure . is_context_dependent ( global_names ) ) : if cond_exposed_terms : top_level_terms . append ( expr_or ( cond_exposed_terms ) ) if feature_enabled_terms : top_level_terms . append ( expr_and ( feature_enabled_terms ) ) return expr_and ( top_level_terms ) all_enabled_terms = [ _Expr ( "${feature_selector}.IsAll()" ) ] if cond_exposed_terms : all_enabled_terms . append ( expr_or ( cond_exposed_terms ) ) if feature_enabled_terms or context_enabled_terms : terms = [ ] if feature_enabled_terms : terms . append ( expr_and ( feature_enabled_terms ) ) if context_enabled_terms : terms . append ( expr_or ( context_enabled_terms ) ) all_enabled_terms . append ( expr_or ( terms ) ) selector_terms = [ ] if exposed_selector_terms : selector_terms . append ( expr_or ( exposed_selector_terms ) ) if feature_selector_names : selector_terms . append ( ref_selected ( sorted ( set ( feature_selector_names ) ) ) ) terms = [ ] terms . append ( expr_and ( all_enabled_terms ) ) if selector_terms : terms . append ( expr_or ( selector_terms ) ) top_level_terms . append ( expr_or ( terms ) ) return expr_and ( top_level_terms )
Returns an expression to determine whether this property should be exposed
or not.
Returns an expression to determine whether this property should be exposed or not .
"""
    Returns an expression to determine whether this property should be exposed
    or not.

    Args:
        exposure: web_idl.Exposure of the target construct.
        global_names: When specified, it's taken into account that the global
            object implements |global_names|.
        may_use_feature_selector: True enables use of ${feature_selector} iff
            the exposure is context dependent.
    """ # The property exposures are categorized into three. # - Unconditional: Always exposed. # - Context-independent: Enabled per v8::Isolate. # - Context-dependent: Enabled per v8::Context, e.g. origin trials. # # Context-dependent properties can be installed in two phases. # - The first phase installs all the properties that are associated with the #   features enabled at the moment.  This phase is represented by #   FeatureSelector as FeatureSelector.IsAll(). # - The second phase installs the properties associated with the specified #   feature.  This phase is represented as FeatureSelector.IsAny(feature). # # The exposure condition is represented as; #   (and feature_selector-independent-term #        (or #         feature_selector-1st-phase-term #         feature_selector-2nd-phase-term)) # which can be represented in more details as: #   (and cross_origin_isolated_term #        direct_socket_enabled_term #        secure_context_term #        uncond_exposed_term #        (or #         (and feature_selector.IsAll()  # 1st phase; all enabled #              cond_exposed_term #              (or feature_enabled_term #                  context_enabled_term)) #         (or exposed_selector_term      # 2nd phase; any selected #             feature_selector_term))) # where #   cross_origin_isolated_term represents [CrossOriginIsolated] #   direct_socket_enabled_term represents [DirectSocketEnabled] #   secure_context_term represents [SecureContext=F1] #   uncond_exposed_term represents [Exposed=(G1, G2)] #   cond_exposed_term represents [Exposed(G1 F1, G2 F2)] #   feature_enabled_term represents [RuntimeEnabled=(F1, F2)] #   context_enabled_term represents [ContextEnabled=F1] #   exposed_selector_term represents [Exposed(G1 F1, G2 F2)] #   feature_selector_term represents [RuntimeEnabled=(F1, F2)] # Will turn into feature_selector.IsAnyOf(...) # [CrossOriginIsolated] # [DirectSocketEnabled] # [SecureContext] # [Exposed] # [RuntimeEnabled] # [ContextEnabled] # Build an expression. # Remove duplicates
exposure
web_idl.Exposure of the target construct.
web_idl . Exposure of the target construct .
global_names
When specified, it's taken into account that the global
object implements |global_names|.
When specified it ' s taken into account that the global object implements |global_names| .
may_use_feature_selector
True enables use of ${feature_selector} iff
the exposure is context dependent.
True enables use of $ { feature_selector } iff the exposure is context dependent .