/* Monster - an advanced game scripting language Copyright (C) 2007-2009 Nicolay Korslund Email: WWW: http://monster.snaptoad.com/ This file (codestream.d) is part of the Monster script language package. Monster is distributed as free software: you can redistribute it and/or modify it under the terms of the GNU General Public License version 3, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License version 3 along with this program. If not, see http://www.gnu.org/licenses/ . */ module monster.vm.codestream; import std.string; import std.stdio; import monster.vm.error; // CodeStream is a simple utility structure for reading data // sequentially. It holds a piece of byte compiled code, and keeps // track of the position within the code. struct CodeStream { private: ubyte[] data; int len; ubyte *pos; public: void setData(ubyte[] data) { this.data = data; len = data.length; pos = data.ptr; } // Called when the end of the stream was unexpectedly encountered void eos(char[] func) { char[] res = format("Premature end of input: %s() missing %s byte(s)", func, -len); fail(res); } // Jump to given position void jump(int newPos) { if(newPos<0 || newPos>=data.length) fail("Jump out of range"); len = data.length - newPos; pos = &data[newPos]; } // Get the current position int getPos() { return pos-data.ptr; } ubyte get() { if(len--) return *(pos++); eos("get"); } int getInt() { len -= 4; if(len < 0) eos("getInt"); int i = *(cast(int*)pos); pos+=4; return i; } // Get a slice of the 'size' next ints int[] getIntArray(uint size) { size *=4; // Convert size to bytes len -= size; if(len < 0) eos("getArray"); int[] res = cast(int[])pos[0..size]; pos += size; return res; } }