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

add for checker for command ids order in a command set and union types #90

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions projects/jdwp/defs/command_sets/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.

from projects.jdwp.defs.command_sets.virtual_machine import VirtualMachine
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 add the copyright here ?

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 added the line

from projects.jdwp.defs.command_sets.reference_type import ReferenceType
from projects.jdwp.defs.command_sets.event_request import EventRequest

ALL = [
VirtualMachine,
ReferenceType,
EventRequest,
]
2 changes: 1 addition & 1 deletion projects/jdwp/defs/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def __post_init__(self):
class UnionTag(Generic[EnumT]):
"""Union tag class type."""

tag: IntegralType
type: IntegralType
value: TypeAlias[EnumT]


Expand Down
28 changes: 28 additions & 0 deletions projects/jdwp/tools/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.

python_binary(
name = "check-uniontypes",
deps = [
":jdwp-tools"
],
main_module = "projects.jdwp.tools.check_union_types",

)

python_binary(
name = "cmd-order-check",
deps = [
":jdwp-tools"
],
main_module = "projects.jdwp.tools.check_command_ids_order",

)

python_library(
name="jdwp-tools",
srcs=glob(["**/*.py"]),
deps=["//projects/jdwp/defs:defs"],
visibility=[
"PUBLIC",
],
)
34 changes: 34 additions & 0 deletions projects/jdwp/tools/check_command_ids_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.

from argparse import ArgumentParser
wekesa360 marked this conversation as resolved.
Show resolved Hide resolved
from projects.jdwp.defs.schema import CommandSet
from projects.jdwp.defs.command_sets import ALL


def check_command_ids(command_set: CommandSet) -> None:
try:
sorted_command_ids = [command.id for command in command_set.commands]
if sorted_command_ids != sorted(sorted_command_ids):
print(f"Command IDs in {command_set.name} are NOT in ascending order.")
exit(1)
except Exception as e:
print(f"Error checking command IDs in {command_set.name}: {e}")
exit(1)
Comment on lines +14 to +16
Copy link
Contributor

Choose a reason for hiding this comment

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

what are we catching here ?



def main() -> None:
parser = ArgumentParser(description="Check order of command IDs in command sets")
parser.add_argument("--command-set", type=str, help="Specific command set to check")
args = parser.parse_args()

arg_command_set: str = args.command_set

for command_set in ALL:
if arg_command_set:
if arg_command_set != command_set.name:
continue
check_command_ids(command_set)


if __name__ == "__main__":
main()
73 changes: 73 additions & 0 deletions projects/jdwp/tools/check_union_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.

from argparse import ArgumentParser
from projects.jdwp.defs.command_sets import ALL
from projects.jdwp.defs.schema import (
Command,
CommandSet,
Struct,
TaggedUnion,
Type,
Array,
UnionTag,
)


def check_command_set(command_set: CommandSet) -> None:
for command in command_set.commands:
check_command(command)


def check_command(command: Command) -> None:
if command.out:
check_struct(command.out)
if command.reply:
check_struct(command.reply)


def check_struct(struct: Struct) -> None:
for field in struct.fields:
check_type(field.type)


def check_type(type: Type) -> None:
match type:
case Struct():
check_struct(type)
case Array():
check_struct(type.element_type)
case TaggedUnion():
check_tagged_union(type)


def check_tagged_union(union: TaggedUnion) -> None:
tagged_union: UnionTag = union.tag.type
for enum_value in tagged_union.value:
try:
union.cases[enum_value]
Copy link
Contributor

Choose a reason for hiding this comment

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

cases is not a dictionary any more, it's a sequence of key-values: https://github.com/facebookexperimental/ExtendedAndroidTools/blob/main/projects/jdwp/defs/schema.py#L88. The check needs to look a bit different now.

except KeyError:
print(
f"Error in tagged union '{union.tag.type}': Missing case for enum value '{enum_value}'"
)
exit(1)

for value in union.cases[1]:
check_struct(value)


def main() -> None:
parser = ArgumentParser(description="Check tagged union mappings in command sets")
parser.add_argument("--command-set", type=str, help="Specific command set to check")
args = parser.parse_args()

arg_command_set: str = args.command_set

for command_set in ALL:
if arg_command_set:
if arg_command_set != command_set.name:
continue
check_command_set(command_set)


if __name__ == "__main__":
main()