From a9d3377b16e1af850d6b59f489dd83b8a901e6ae Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Mon, 23 Aug 2021 12:56:29 -0700 Subject: [PATCH] Implement optional packetbuffer function --- burger/toppings/packetinstructions.py | 30 +++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/burger/toppings/packetinstructions.py b/burger/toppings/packetinstructions.py index 17d8211..a38ad37 100644 --- a/burger/toppings/packetinstructions.py +++ b/burger/toppings/packetinstructions.py @@ -663,8 +663,34 @@ def _handle_2_arg_buffer_call(classloader, classes, instruction, verbose, # the endloop past everything. operations.append(Operation(instruction.pos + 1 - SUB_INS_EPSILON, "endloop")) return operations + elif desc.args[0].name == "java/util/Optional" and \ + desc.args[1].name == "java/util/function/BiConsumer": + # Write a boolean indicating whether the optional is present. + # Call the consumer with the packetbuffer and value if the optional is present. + operations = [] + field = args[0] + assert isinstance(field, StackOperand) + operations.append(Operation(instruction.pos, "write", type="boolean", + field=field.value + ".isPresent()")) + operations.append(Operation(instruction.pos, "if", + condition=field.value + ".isPresent()")) + info = args[1] + assert isinstance(info, InvokeDynamicInfo) + operations += _PIT._lambda_operations( + classloader, classes, instruction, verbose, + info, [instance, field.value + ".get()"] + ) + # Jank: the part of the program that converts loop+endloop + # to a nested setup sorts the operations. + # Thus, if instruction.pos is used, _sub_operations + # adds epsilon to each sub-instruction, making them + # come after the endloop. + # Assume that 1 - SUB_INS_EPSILON (e.g. .99) will put + # the endloop past everything. + operations.append(Operation(instruction.pos + 1 - SUB_INS_EPSILON, "endif")) + return operations else: - raise Exception("Unexpected descriptor " + desc) + raise Exception("Unexpected descriptor " + desc.descriptor) @staticmethod def _handle_3_arg_buffer_call(classloader, classes, instruction, verbose, @@ -711,7 +737,7 @@ def _handle_3_arg_buffer_call(classloader, classes, instruction, verbose, operations.append(Operation(instruction.pos + 1 - SUB_INS_EPSILON, "endloop")) return operations else: - raise Exception("Unexpected descriptor " + desc) + raise Exception("Unexpected descriptor " + desc.descriptor) @staticmethod def _handle_foreach(classloader, classes, instruction, verbose,