package com.ninty.cmd;

import com.ninty.cmd.base.ICmdBase;
import com.ninty.cmd.base.Index8Cmd;
import com.ninty.runtime.NiFrame;

import java.nio.ByteBuffer;

import static com.ninty.utils.VMUtils.toUInt;

/**
 * Created by ninty on 2017/7/16.
 */
public class CmdExtended {

    static class WIDE implements ICmdBase {
        private ICmdBase cmd;

        private void initIndex8Cmd(Index8Cmd cmd, ByteBuffer bb){
            cmd.index = bb.getChar();
            this.cmd = cmd;
        }

        @Override
        public void init(ByteBuffer bb) {
            int code = toUInt(bb.get());
            switch (code) {
                case 0x15:
                    initIndex8Cmd(new CmdLoads.ILOAD(), bb);
                    break;
                case 0x16:
                    initIndex8Cmd(new CmdLoads.LLOAD(), bb);
                    break;
                case 0x17:
                    initIndex8Cmd(new CmdLoads.FLOAD(), bb);
                    break;
                case 0x18:
                    initIndex8Cmd(new CmdLoads.DLOAD(), bb);
                    break;
                case 0x19:
                    initIndex8Cmd(new CmdStores.ISTORE(), bb);
                    break;
                case 0x36:
                    initIndex8Cmd(new CmdStores.LSTORE(), bb);
                    break;
                case 0x37:
                    initIndex8Cmd(new CmdStores.FSTORE(), bb);
                    break;
                case 0x38:
                    initIndex8Cmd(new CmdStores.DSTORE(), bb);
                    break;
                case 0x39:
                    initIndex8Cmd(new CmdStores.ASTORE(), bb);
                    break;
                case 0x3a:
                    initIndex8Cmd(new CmdLoads.ILOAD(), bb);
                    break;
                case 0x84:
                    CmdMath.IINC iinc = new CmdMath.IINC();
                    iinc.index = bb.getChar();
                    iinc.inc = bb.getShort();
                    cmd = iinc;
                    break;
                case 0xa9:
                default:
                    throw new UnsupportedOperationException("unsupport opcode:" + code);
            }
        }

        @Override
        public void exec(NiFrame frame) {
            cmd.exec(frame);
        }
    }
}

@@ -17,16 +17,88 @@
 y.cmd;%0A%0A
+import com.ninty.cmd.base.BranchCmd;%0Aimport com.ninty.cmd.base.DataCmd;%0A
 import c
@@ -2359,11 +2359,821 @@
  %7D%0A    %7D
+%0A%0A    static class IFNULL extends BranchCmd %7B%0A        @Override%0A        public void exec(NiFrame frame) %7B%0A            Object ref = frame.getOperandStack().popRef();%0A            if(ref == null)%7B%0A                branch();%0A            %7D%0A        %7D%0A    %7D%0A%0A    static class IFNONNULL extends BranchCmd %7B%0A        @Override%0A        public void exec(NiFrame frame) %7B%0A            Object ref = frame.getOperandStack().popRef();%0A            if(ref != null)%7B%0A                branch();%0A            %7D%0A        %7D%0A    %7D%0A%0A    static class GOTO_W extends DataCmd %7B%0A        private int offset;%0A%0A        @Override%0A        public void init(ByteBuffer bb) %7B%0A            super.init(bb);%0A            offset = bb.getInt();%0A        %7D%0A%0A        @Override%0A        public void exec(NiFrame frame) %7B%0A            jumpTo(offset);%0A        %7D%0A    %7D
 %0A%7D%0A

complete extend cmd