view engine-runner.js @ 31:5d7bf3684fc3

Implemented a terrible EraseWindow mechanism.
author Atul Varma <varmaa@toolness.com>
date Mon, 12 May 2008 10:43:51 -0700
parents 673f5be640e9
children 3a85b5915ea6
line wrap: on
line source

function Zui() {
}

Zui.prototype = {
    onLineInput: function(callback) {
    },
    onCharacterInput: function(callback) {
    },
    onSave: function(callback) {
    },
    onRestore: function(callback) {
    },
    onQuit: function() {
    },
    onRestart: function(callback) {
    },
    onWimpOut: function(callback) {
    },
    onBreakpoint: function(callback) {
    },
    onSetStyle: function(textStyle, foreground, background) {
    },
    onSplitWindow: function(window) {
    },
    onSetWindow: function(window) {
    },
    onEraseWindow: function(window) {
    },
    onEraseLine: function() {
    },
    onSetCursor: function(x, y) {
    },
    onSetBufferMode: function() {
    },
    onSetInputStream: function() {
    },
    onGetCursor: function() {
    },
    onPrint: function(output) {
    },
    onPrintTable: function(lines) {
    },
};

function EngineRunner(engine, zui) {
    this._engine = engine;
    this._zui = zui;
    this._isRunning = false;
    this._isInLoop = false;
    this._isWaitingForCallback = false;

    var self = this;

    this.__proto__ = {
        stop: function() {
            self._isRunning = false;
        },

        run: function() {
            self._isRunning = true;
            self._continueRunning();
        },

        _continueRunning: function() {
            while (self._isRunning && !self._isWaitingForCallback) {
                self._loop();
            }
        },

        _receiveLineInput: function(input) {
            self._isWaitingForCallback = false;
            self._engine.answer(1, input);
            if (!self._isInLoop) {
                self._continueRunning();
            } else {
                /* We're still inside _loop(), so just return. */
            }
        },

        _loop: function() {
            if (self._isInLoop)
                throw Error("Already in loop!");

            self._isInLoop = true;
            var engine = self._engine;

            engine.run();
            var effect = '"' + engine.effect(0) + '"';

            switch (effect) {
            case GNUSTO_EFFECT_INPUT:
                self._isWaitingForCallback = true;
                self._zui.onLineInput(self._receiveLineInput);
                break;
            case GNUSTO_EFFECT_INPUT_CHAR:
            case GNUSTO_EFFECT_SAVE:
            case GNUSTO_EFFECT_RESTORE:
                throw Error("Unimplemented effect: " + effect);
            case GNUSTO_EFFECT_QUIT:
                self.stop();
                self._zui.onQuit();
                break;
            case GNUSTO_EFFECT_RESTART:
            case GNUSTO_EFFECT_WIMP_OUT:
            case GNUSTO_EFFECT_BREAKPOINT:
            case GNUSTO_EFFECT_FLAGS_CHANGED:
            case GNUSTO_EFFECT_PIRACY:
                throw Error("Unimplemented effect: " + effect);
            case GNUSTO_EFFECT_STYLE:
                self._zui.onSetStyle(engine.effect(1),
                                     engine.effect(2),
                                     engine.effect(3));
                break;
            case GNUSTO_EFFECT_SOUND:
                throw Error("Unimplemented effect: " + effect);
            case GNUSTO_EFFECT_SPLITWINDOW:
                self._zui.onSplitWindow(engine.effect(1));
                break;
            case GNUSTO_EFFECT_SETWINDOW:
                self._zui.onSetWindow(engine.effect(1));
                break;
            case GNUSTO_EFFECT_ERASEWINDOW:
                self._zui.onEraseWindow(engine.effect(1));
                break;
            case GNUSTO_EFFECT_ERASELINE:
                throw Error("Unimplemented effect: " + effect);
            case GNUSTO_EFFECT_SETCURSOR:
                self._zui.onSetCursor(engine.effect(2),
                                      engine.effect(1));
                break;
            case GNUSTO_EFFECT_SETBUFFERMODE:
            case GNUSTO_EFFECT_SETINPUTSTREAM:
            case GNUSTO_EFFECT_GETCURSOR:
            case GNUSTO_EFFECT_PRINTTABLE:
                throw Error("Unimplemented effect: " + effect);
            }

            var text = engine.consoleText();
            if (text)
                self._zui.onPrint(text);

            self._isInLoop = false;
        }
    };
}