Mercurial > web-gnusto
view engine-runner.js @ 34:cf2303c41d8c
Added logging functionality to engine-runner.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Tue, 13 May 2008 22:18:32 -0700 |
parents | 3a85b5915ea6 |
children | be2a410b8518 |
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, logfunc) { this._engine = engine; this._zui = zui; this._isRunning = false; this._isInLoop = false; this._isWaitingForCallback = false; if (logfunc) { this._log = logfunc; } else { this._log = function() {}; } 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. */ } }, _receiveCharacterInput: function(input) { self._isWaitingForCallback = false; self._engine.answer(0, 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) + '"'; var logString = "[ " + engine.effect(0); for (var i = 1; engine.effect(i) != undefined; i++) { logString += ", " + engine.effect(i); } self._log(logString + " ]"); switch (effect) { case GNUSTO_EFFECT_INPUT: self._isWaitingForCallback = true; self._zui.onLineInput(self._receiveLineInput); break; case GNUSTO_EFFECT_INPUT_CHAR: self._isWaitingForCallback = true; self._zui.onCharacterInput(self._receiveCharacterInput); break; 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; } }; }