11<?php
22namespace PHPJava \Kernel \Mnemonics ;
33
4- use PHPJava \Exceptions \NotImplementedException ;
4+ use PHPJava \Kernel \Types \_Array \Collection ;
5+ use PHPJava \Utilities \Extractor ;
6+ use PHPJava \Utilities \Formatter ;
57
68final class _multianewarray implements OperationInterface
79{
@@ -10,6 +12,54 @@ final class _multianewarray implements OperationInterface
1012
1113 public function execute (): void
1214 {
13- throw new NotImplementedException (__CLASS__ );
15+ $ cp = $ this ->getConstantPool ();
16+ $ index = $ this ->readUnsignedShort ();
17+ $ dimensions = $ this ->readByte ();
18+
19+ $ descriptor = Formatter::parseSignature (
20+ $ cp [$ cp [$ index ]->getClassIndex ()]->getString ()
21+ );
22+
23+ $ counts = array_fill (0 , $ dimensions , 0 );
24+
25+ for ($ i = $ dimensions - 1 ; $ i >= 0 ; $ i --) {
26+ $ counts [$ i ] = Extractor::getRealValue (
27+ $ this ->popFromOperandStack ()
28+ );
29+ }
30+
31+ $ data = null ;
32+ $ multiDimensionArray = $ this ->makeMultiDimensionArray (
33+ $ data ,
34+ $ counts ,
35+ $ descriptor [0 ]['class_name ' ] ?? $ descriptor [0 ]['type ' ],
36+ 0 ,
37+ $ dimensions
38+ );
39+
40+ $ this ->pushToOperandStackByReference ($ multiDimensionArray );
41+ }
42+
43+ /**
44+ * @param $array
45+ * @return $this
46+ */
47+ private function makeMultiDimensionArray ($ array , array $ counts , string $ type , int $ currentDimension , int $ maxDimension )
48+ {
49+ if ($ currentDimension >= $ maxDimension ) {
50+ return $ array ;
51+ }
52+ $ newArray = (new Collection ())->setType ($ type );
53+ for ($ i = 0 ; $ i < $ counts [$ currentDimension ]; $ i ++) {
54+ $ collection = (new Collection ())->setType ($ type );
55+ $ newArray [$ i ] = $ this ->makeMultiDimensionArray (
56+ $ collection ,
57+ $ counts ,
58+ $ type ,
59+ $ currentDimension + 1 ,
60+ $ maxDimension
61+ );
62+ }
63+ return $ newArray ;
1464 }
1565}
0 commit comments