-
Notifications
You must be signed in to change notification settings - Fork 61
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
Conversation
Field( | ||
"modifiers", | ||
IntegralType.INT, | ||
"Constraints used to control the number of generated events.", | ||
), | ||
Field("outModifiers", __SetCommand_outArray_Element, "Modifier array."), |
There was a problem hiding this comment.
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[ |
There was a problem hiding this comment.
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 :)
projects/jdwp/defs/schema.py
Outdated
description: str | ||
|
||
|
||
M = TypeVar("M", bound=ModifierKind) |
There was a problem hiding this comment.
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)
projects/jdwp/defs/schema.py
Outdated
"""Union tag class type.""" | ||
|
||
tag: IntegralType | ||
value: M |
There was a problem hiding this comment.
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]
projects/jdwp/defs/schema.py
Outdated
class TaggedUnion: | ||
"""Tagged Union class type""" | ||
|
||
pass | ||
tag: Field[UnionTag] | ||
cases: Dict[ModifierKind, Struct] |
There was a problem hiding this comment.
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
projects/jdwp/defs/schema.py
Outdated
class IntegralType(Enum): | ||
"""Integral type class.""" | ||
|
||
INT = "int" | ||
BYTE = "byte" | ||
|
||
|
||
@dataclass(frozen=True) | ||
class ArrayLength: | ||
"""Array length class.""" | ||
|
||
type: IntegralType | ||
|
There was a problem hiding this comment.
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
projects/jdwp/defs/schema.py
Outdated
"""Array class type.""" | ||
|
||
pass | ||
element_type: Struct | TaggedUnion |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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]
157dac4
to
0bd29f4
Compare
There was a problem hiding this 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 :)
__SetCommand_outArray_element_modifier, | ||
__SetCommand_out_modKind, |
There was a problem hiding this comment.
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 ?
projects/jdwp/defs/schema.py
Outdated
@@ -53,13 +53,27 @@ class Array: | |||
length: Field[ArrayLength] | |||
|
|||
|
|||
class TaggedUnion(Enum): | |||
@dataclass(frozen=True) | |||
class TaggedUnion: |
There was a problem hiding this comment.
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
0bd29f4
to
26ca2d5
Compare
This PR add the
SetCommand
from theEvent Request
Command Set.