Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Union types in JDWP commands #77

Merged
merged 1 commit into from
Nov 21, 2023

Conversation

wekesa360
Copy link
Contributor

This PR add the SetCommand from the Event Request Command Set.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 9, 2023
Comment on lines 92 to 97
Field(
"modifiers",
IntegralType.INT,
"Constraints used to control the number of generated events.",
),
Field("outModifiers", __SetCommand_outArray_Element, "Modifier array."),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The modifiers field needs to be of type ArrayLength and outModifiers needs to be of an array type

])


__Modifier = Union[
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to use TaggedUnion type from schema.py to represent the union. This should look something like:

__SetCommand_outArray_Element_modKind = Field(
    "modKind",
    UnionTag(IntegralType.BYTE, ModifierKind),
    "Modifier kind",
)

__SetCommand_outArray_Element_Modifier = TaggedUnion(
    tag=__SetCommand_outArray_Element_modKind,
    cases={
        ModifierKind.COUNT: CountModifier,
        ModifierKind.CONDITIONAL: ConditionalModifier,
        ...
    },
)

In another PR we will want to enforce that the cases dict is total, for every possible enum value it has a corresponding Struct value. It can be done via a unit test :)

description: str


M = TypeVar("M", bound=ModifierKind)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make this generic we want this to be a TypeVar that can be an arbitrary enum, not just ModifierKind. Would the following work ?

EnumT = TypeVar("M", bound=enum.Enum)

"""Union tag class type."""

tag: IntegralType
value: M
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of specifying a particular value we should specify accepted range of values, ideally by specifying the enum class instead. Would this work ?

    value: Type[EnumT]

Comment on lines 57 to 61
class TaggedUnion:
"""Tagged Union class type"""

pass
tag: Field[UnionTag]
cases: Dict[ModifierKind, Struct]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make TaggedUnion generic over M/EnumT as well. How about something like this:

class TaggedUnion[Generic[EnumT]]:
    """Tagged Union class type"""

    tag: Field[UnionTag[EnumT]]
    cases: Mapping[EnumT, Struct]

Note that it's better to use collections.abc.Mapping than typing.Dict

Comment on lines 34 to 46
class IntegralType(Enum):
"""Integral type class."""

INT = "int"
BYTE = "byte"


@dataclass(frozen=True)
class ArrayLength:
"""Array length class."""

type: IntegralType

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you rebase your PR ? These changes should not be in this PR

"""Array class type."""

pass
element_type: Struct | TaggedUnion
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need TaggedUnion here ?

Copy link
Contributor Author

@wekesa360 wekesa360 Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have worked on that, initially it was for the Array to accept the type TaggedUnion

Field(
"outModifiers",
Array(
__SetCommand_outArray_element_modifier,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type of the element of the array is a struct consisting of two fields: tag and tagged union

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have implemented the that, however the pyre checker is failing with:
Too many arguments [19]: Call `TaggedUnion.__init__` expects 0 positional arguments, 2 were provided.

looking at the TaggedUnion dataclass:

@dataclass(frozen=True)
class TaggedUnion:
    """Tagged Union class type"""

    tag: Field[UnionTag[EnumT]]
    cases: Mapping[EnumT, Struct]

And how tagged union is used is as:

TaggedUnion(
    tag=__SetCommand_out_modKind,
    cases={
        ModifierKind.COUNT: CountModifier,
        ModifierKind.CONDITIONAL: ConditionalModifier,
        ModifierKind.THREAD_ONLY: ThreadOnlyModifier,
        ModifierKind.CLASS_ONLY: ClassOnlyModifier,
        ModifierKind.CLASS_MATCH: ClassMatchModifier,
        ModifierKind.CLASS_EXCLUDE: ClassExcludeModifier,
        ModifierKind.STEP: StepModifier,
        ModifierKind.LOCATION_ONLY: LocationOnlyModifier,
        ModifierKind.EXCEPTION_ONLY: ExceptionOnlyModifier,
        ModifierKind.FIELD_ONLY: FieldOnlyModifier,
        ModifierKind.INSTANCE_ONLY: InstanceOnlyModifier,
        ModifierKind.SOURCE_NAME_MATCH: SourceNameMatchModifier,
    }
)

Would you kindly advice on how to resolve this fail.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I am not sure pyre got the definition of TaggedUnion right - I believe EnumT is not an allowed type unless you extend Generic[EnumT]

Copy link
Contributor

@michalgr michalgr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great already ! But there are three tiny comments left :)

Comment on lines 112 to 114
__SetCommand_outArray_element_modifier,
__SetCommand_out_modKind,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the order should be the other way around ?

@@ -53,13 +53,27 @@ class Array:
length: Field[ArrayLength]


class TaggedUnion(Enum):
@dataclass(frozen=True)
class TaggedUnion:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to extend Generic[EnumT] as well

@michalgr michalgr merged commit 8243f67 into facebookexperimental:main Nov 21, 2023
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants