|
1 | 1 | <?php |
2 | 2 | namespace PHPJava\Kernel\Mnemonics; |
3 | 3 |
|
| 4 | +use PHPJava\Core\JVM\Parameters\Runtime; |
4 | 5 | use PHPJava\Exceptions\NotImplementedException; |
| 6 | +use PHPJava\Packages\java\lang\invoke\MethodHandles; |
| 7 | +use PHPJava\Packages\java\lang\invoke\MethodHandles\Lookup; |
| 8 | +use PHPJava\Kernel\Attributes\BootstrapMethodsAttribute; |
| 9 | +use PHPJava\Kernel\Maps\MethodHandleKind; |
| 10 | +use PHPJava\Kernel\Structures\_BootstrapMethod; |
| 11 | +use PHPJava\Kernel\Structures\_MethodHandle; |
| 12 | +use PHPJava\Kernel\Structures\_NameAndType; |
| 13 | +use PHPJava\Packages\java\lang\invoke\MethodType; |
| 14 | +use PHPJava\Utilities\AttributionResolver; |
5 | 15 | use PHPJava\Utilities\BinaryTool; |
| 16 | +use PHPJava\Utilities\Formatter; |
| 17 | +use PHPJava\Utilities\Normalizer; |
6 | 18 |
|
7 | 19 | final class _invokedynamic implements OperationInterface |
8 | 20 | { |
9 | 21 | use \PHPJava\Kernel\Core\Accumulator; |
10 | 22 | use \PHPJava\Kernel\Core\ConstantPool; |
11 | 23 |
|
| 24 | + /** |
| 25 | + * `invokedynamic` is a trial implementation. |
| 26 | + * |
| 27 | + * @throws NotImplementedException |
| 28 | + * @throws \PHPJava\Exceptions\NormalizerException |
| 29 | + * @throws \PHPJava\Exceptions\TypeException |
| 30 | + * @throws \PHPJava\Exceptions\UnableToFindAttributionException |
| 31 | + * @see https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-6.html#jvms-6.5.invokedynamic |
| 32 | + */ |
12 | 33 | public function execute(): void |
13 | 34 | { |
14 | | - throw new NotImplementedException(__CLASS__); |
| 35 | + $cp = $this->getConstantPool(); |
| 36 | + /** |
| 37 | + * @var \PHPJava\Kernel\Structures\_InvokeDynamic $invokeDynamicStructure |
| 38 | + */ |
| 39 | + $invokeDynamicStructure = $cp[$this->readUnsignedShort()]; |
| 40 | + |
| 41 | + // NOTE: The values of the third and fourth operand bytes must always be zero. |
| 42 | + $thirdByte = $this->read(); |
| 43 | + $forthByte = $this->read(); |
| 44 | + |
| 45 | + /** |
| 46 | + * @var _NameAndType $nameAndTypeIndex |
| 47 | + */ |
| 48 | + $nameAndTypeIndex = $cp[$invokeDynamicStructure->getNameAndTypeIndex()]; |
| 49 | + |
| 50 | + $name = $cp[$nameAndTypeIndex->getNameIndex()]->getString(); |
| 51 | + $signature = Formatter::parseSignature($cp[$nameAndTypeIndex->getDescriptorIndex()]->getString()); |
| 52 | + |
| 53 | + $arguments = []; |
| 54 | + if (($length = $signature['arguments_count'] - 1) >= 0) { |
| 55 | + $arguments = array_fill(0, $length, null); |
| 56 | + for ($i = $length; $i >= 0; $i--) { |
| 57 | + $arguments[$i] = $this->popFromOperandStack(); |
| 58 | + } |
| 59 | + |
| 60 | + $arguments = Normalizer::normalizeValues( |
| 61 | + $arguments, |
| 62 | + $signature['arguments'] |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @var BootstrapMethodsAttribute $bootstrapMethodAttribute |
| 68 | + */ |
| 69 | + $bootstrapMethodAttribute = AttributionResolver::resolve( |
| 70 | + // NOTE: bootstrapMethods attribution is defined in JavaClass class. |
| 71 | + $this->javaClass->getAttributes(), |
| 72 | + BootstrapMethodsAttribute::class |
| 73 | + ); |
| 74 | + |
| 75 | + /** |
| 76 | + * @var _BootstrapMethod $bootstrapMethod |
| 77 | + */ |
| 78 | + $bootstrapMethod = $bootstrapMethodAttribute->getBootstrapMethods()[$invokeDynamicStructure->getBootstrapMethodAttrIndex()]; |
| 79 | + $bootstrapMethodArguments = $bootstrapMethod->getBootstrapArguments(); |
| 80 | + |
| 81 | + /** |
| 82 | + * @var _MethodHandle $methodHandle |
| 83 | + */ |
| 84 | + $methodHandle = $cp[$bootstrapMethod->getBootstrapMethodRef()]; |
| 85 | + switch ($methodHandle->getReferenceKind()) { |
| 86 | + case MethodHandleKind::REF_getField: |
| 87 | + throw new NotImplementedException($methodHandle->getReferenceKind() . ' does not implemented in ' . __METHOD__); |
| 88 | + case MethodHandleKind::REF_getStatic: |
| 89 | + throw new NotImplementedException($methodHandle->getReferenceKind() . ' does not implemented in ' . __METHOD__); |
| 90 | + case MethodHandleKind::REF_putField: |
| 91 | + throw new NotImplementedException($methodHandle->getReferenceKind() . ' does not implemented in ' . __METHOD__); |
| 92 | + case MethodHandleKind::REF_putStatic: |
| 93 | + throw new NotImplementedException($methodHandle->getReferenceKind() . ' does not implemented in ' . __METHOD__); |
| 94 | + case MethodHandleKind::REF_invokeVirtual: |
| 95 | + throw new NotImplementedException($methodHandle->getReferenceKind() . ' does not implemented in ' . __METHOD__); |
| 96 | + case MethodHandleKind::REF_invokeStatic: |
| 97 | + $factory = Runtime::PHP_PACKAGES_DIRECTORY . '\\' . str_replace( |
| 98 | + '/', |
| 99 | + '\\', |
| 100 | + $cp[$cp[$cp[$methodHandle->getReferenceIndex()]->getClassIndex()]->getClassIndex()]->getString() |
| 101 | + ); |
| 102 | + $methodHandleLookup = new Lookup(); |
| 103 | + |
| 104 | + $methodHandleNameAndTypeConstant = $cp[$cp[$methodHandle->getReferenceIndex()]->getNameAndTypeIndex()]; |
| 105 | + $methodHandledName = $cp[$methodHandleNameAndTypeConstant->getNameIndex()]->getString(); |
| 106 | + $methodHandledDescriptor = $cp[$methodHandleNameAndTypeConstant->getDescriptorIndex()]->getString(); |
| 107 | + |
| 108 | + // NOTE: Must be a class name. |
| 109 | + $className = Formatter::convertJavaNamespaceToPHP($signature[0]['class_name']); |
| 110 | + |
| 111 | + $methodHandleType = MethodType::methodType($className); |
| 112 | + |
| 113 | + $result = forward_static_call_array( |
| 114 | + [$factory, $name], |
| 115 | + array_merge( |
| 116 | + [ |
| 117 | + MethodHandles::lookup(), |
| 118 | + $methodHandledName, |
| 119 | + $methodHandleType, |
| 120 | + $bootstrapMethodArguments[0]->getStringObject() |
| 121 | + ], |
| 122 | + $arguments |
| 123 | + ) |
| 124 | + ); |
| 125 | + |
| 126 | + if ($signature[0]['type'] !== 'void') { |
| 127 | + $this->pushToOperandStack($result); |
| 128 | + } |
| 129 | + break; |
| 130 | + case MethodHandleKind::REF_invokeSpecial: |
| 131 | + throw new NotImplementedException($methodHandle->getReferenceKind() . ' does not implemented in ' . __METHOD__); |
| 132 | + case MethodHandleKind::REF_newInvokeSpecial: |
| 133 | + throw new NotImplementedException($methodHandle->getReferenceKind() . ' does not implemented in ' . __METHOD__); |
| 134 | + case MethodHandleKind::REF_invokeInterface: |
| 135 | + throw new NotImplementedException($methodHandle->getReferenceKind() . ' does not implemented in ' . __METHOD__); |
| 136 | + } |
15 | 137 | } |
16 | 138 | } |
0 commit comments