Question

I asked a similar question before I haven't gotten any good feedback/help there. So I am rewriting this question with more information.

Here is a partially converted Javascript Function to Java.. It's nearly complete. I know I cannot use Anonymous Functions in arrays in Java let alone Anonymous Functions, only thing closest to this functionality would be inner-classes, not what I want.

I know this code can still be converted maybe into multiple functions and instead of using Anonymous Function Arrays something like if statements or a mix of a switch(...)

The function below just initalizes the LINECONTROL array of anonymous functions using recursion calls as well.

Later in this javscript emulator It's used like this

LCDCONTROL = (LCDisOn) ? LINECONTROL : DISPLAYOFFCONTROL;

DISPLAYOFFCONTROL is a anonymous function that's empty. declared like this

 DISPLAYOFFCONTROL = [function() {}];

You never call the LINECONTROL array outside the initalizeLCDController, you only call the LCDCONTROL object.

Like so

//LCDCONTROL[actualScanLine]();

I just need some advise how to to convert to function below maybe just a head start that's about it.

Here is the partially translated function from JavaScript -> Java

public void initializeLCDController() {
    long a = 0;
    while (a < 154) {
        if (a < 143) {
            LINECONTROL[a] = function () {
                if (LCDTicks < 80) {
                    scanLineMode2();
                } else if (LCDTicks < 252) {
                    scanLineMode3();
                } else if (LCDTicks < 456) {
                    scanLineMode0();
                } else {
                    LCDTicks -= 456;
                    if (STATTracker != 3) {
                        if (STATTracker != 2) {
                            if (STATTracker == 0 && mode2TriggerSTAT) {
                                interruptsRequested |= 2;
                            }
                            incrementScanLineQueue();
                        }
                        if (hdmaRunning) {
                            executeHDMA();
                        }
                        if (mode0TriggerSTAT) {
                            interruptsRequested |= 2;
                        }
                    }
                    actualScanLine = ++memory[65348];
                    if (actualScanLine == memory[65349]) {
                        memory[65345] |= 4;
                        if (LYCMatchTriggerSTAT) {
                            interruptsRequested |= 2;
                        }
                    } else {
                        memory[65345] &= 123;
                    }
                    checkIRQMatching();
                    STATTracker = 0;
                    modeSTAT = 2;
                    LINECONTROL[actualScanLine]();
                }
            }
        } else if (a == 143) {
            LINECONTROL[143] = function () {
                if (LCDTicks < 80) {
                    scanLineMode2();
                } else if (LCDTicks < 252) {
                    scanLineMode3();
                } else if (LCDTicks < 456) {
                    scanLineMode0();
                } else {
                    LCDTicks -= 456;
                    if (STATTracker != 3) {
                        if (STATTracker != 2) {
                            if (STATTracker == 0 && mode2TriggerSTAT) {
                                interruptsRequested |= 2;
                            }
                            incrementScanLineQueue();
                        }
                        if (hdmaRunning) {
                            executeHDMA();
                        }
                        if (mode0TriggerSTAT) {
                            interruptsRequested |= 2;
                        }
                    }
                    actualScanLine = memory[65348] = 144;
                    if (memory[65349] == 144) {
                        memory[65345] |= 4;
                        if (LYCMatchTriggerSTAT) {
                            interruptsRequested |= 2;
                        }
                    } else {
                        memory[65345] &= 123;
                    }
                    STATTracker = 0;
                    modeSTAT = 1;
                    interruptsRequested |= (mode1TriggerSTAT) ? 3 : 1;
                    checkIRQMatching();
                    if (drewBlank == 0) {
                        if (totalLinesPassed < 144 || (totalLinesPassed == 144 && midScanlineOffset > -1)) {
                            graphicsJITVBlank();
                            prepareFrame();
                        }
                    } else {
                        --drewBlank;
                    }
                    LINECONTROL[144]();
                }
            }
        } else if (a < 153) {
            LINECONTROL[a] = function () {
                if (LCDTicks >= 456) {
                    LCDTicks -= 456;
                    actualScanLine = ++memory[65348];
                    if (actualScanLine == memory[65349]) {
                        memory[65345] |= 4;
                        if (LYCMatchTriggerSTAT) {
                            interruptsRequested |= 2;
                            checkIRQMatching();
                        }
                    } else {
                        memory[65345] &= 123;
                    }
                    LINECONTROL[actualScanLine]();
                }
            }
        } else {
            LINECONTROL[153] = function () {
                if (LCDTicks >= 8) {
                    if (STATTracker != 4 && memory[65348] == 153) {
                        memory[65348] = 0;
                        if (memory[65349] == 0) {
                            memory[65345] |= 4;
                            if (LYCMatchTriggerSTAT) {
                                interruptsRequested |= 2;
                                checkIRQMatching();
                            }
                        } else {
                            memory[65345] &= 123;
                        }
                        STATTracker = 4;
                    }
                    if (LCDTicks >= 456) {
                        LCDTicks -= 456;
                        STATTracker = actualScanLine = 0;
                        LINECONTROL[0]();
                    }
                }
            }
        }
        ++a;
    }
}
Was it helpful?

Solution

Here's a clever enum approach. You can use it by doing ACTIONS[x].act(this). It's essentially a type-safe jump table.

Edit: added getAction. To use it, do getAction(a).act(this), but at this point, you're better off losing the enum and dispatching directly from a renamed getAction(). The only benefits you get from the enum are having only a single instance per enum (only a benefit over non-static inner classes) and the jump table.

class Foo {

    protected enum Action {
        FUNCTION_0 {
            public void act(Foo foo) { ... }
        },

        FUNCTION_1 {
            public void act(Foo foo) { ... }
        },

        FUNCTION_2 {
            public void act(Foo foo) { ... }
        },

        FUNCTION_3 {
            public void act(Foo foo) { ... }
        },

        ;

        public abstract void act(Foo foo);
    }

    protected static final Action[] ACTIONS = new Action[154];
    static {
        Arrays.fill(ACTIONS, 0, 143, Action.FUNCTION_0);
        Arrays.fill(ACTIONS, 143, 144, Action.FUNCTION_1);
        Arrays.fill(ACTIONS, 144, 153, Action.FUNCTION_2);
        Arrays.fill(ACTIONS, 153, 154, Action.FUNCTION_3);
    }

        protected Action getAction(int a) {
            if (a < 0) { throw new IllegalArgumentError(); }
            else if (a < 143) { return Action.FUNCTION_0; }
            else if (a < 144) { return Action.FUNCTION_1; }
            else if (a < 153) { return Action.FUNCTION_2; }
            else if (a < 154) { return Action.FUNCTION_3; }
            else { throw new IllegalArgumentError(); }
        }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top