Commits

Mathias Panzenböck committed 5191483

added basic system module implementation (no support for ByteArrays. of course)

  • Participants
  • Parent commits daf049f

Comments (0)

Files changed (1)

+var output = document.getElementById('system.output');
+
+if (!output) {
+	output = document.createElement('pre');
+	output.id = 'system.output';
+
+	if (document.body) {
+		document.body.appendChild(output);
+	}
+	else {
+		document.addEventListener('load', function () {
+			document.body.appendChild(output);
+		}, false);
+	}
+}
+
+var text = document.createTextNode('');
+var lastWasStdout = true;
+output.appendChild(text);
+
+function illegalWrite () {
+	throw new Error('stream not writeable');
+}
+
+function illegalRead () {
+	throw new Error('stream not readeable');
+}
+
+function print () {
+	var buf = [];
+	for (var i = 0; i < arguments.length; ++ i) {
+		buf.push(arguments[i]);
+	}
+	this.writeLine(buf.join(this.delimiter));
+}
+
+function writeLine (line) {
+	this.write(line+'\n');
+}
+
+function substr (buf, begin, end) {
+	if (begin === undefined) {
+		if (end === undefined) {
+			return buf;
+		}
+		else {
+			return buf.substring(0, end);
+		}
+	}
+	else if (end === undefined) {
+		return buf.substring(begin);
+	}
+	else {
+		return bug.substring(begin, end);
+	}
+}
+
+exports.stdin = {
+	charset:   'UTF-8',
+	newline:   '\n',
+	delimiter: ' ',
+	buffer:    '',
+	_offset:   0,
+	read: function (n) {
+		if (n === undefined) {
+			var buf = [ this.buffer.substring(this._offset) ];
+
+			for (;;) {
+				var line = prompt('stdin:');
+
+				if (line == null)
+					break;
+
+				buf.push(line + this.newline);
+			}
+			return buf.join('');
+		}
+		else {
+			var buf = [ this.buffer.substring(this._offset) ];
+			var size = buf[0].length;
+
+			while (size < n) {
+				var line = prompt('stdin:');
+
+				if (line == null)
+					break;
+
+				line = line + this.newline;
+				buf.push(line);
+				size += line.length;
+			}
+
+			this.buffer  = buf = buf.join('');
+			this._offset = n;
+
+			return buf.substring(0, n);
+		}
+	},
+	readLine: function () {
+		var buf = this.buffer.substring(this._offset);
+		var pos = buf.indexOf(this.newline);
+
+		if (pos == -1) {
+			var line = prompt('stdin:');
+			this._offset = 0;
+			this.buffer  = '';
+
+			if (line == null) {
+				return '';
+			}
+			else {
+				return line + this.newline;
+			}
+		}
+		else {
+			var end = this._offset = pos + this.newline.length;
+			buf = buf.substring(0, end);
+			return buf;
+		}
+	},
+	readLines: function () {
+		var lines = [];
+
+		for (;;) {
+			var line = this.readLine();
+
+			if (!line)
+				break;
+			
+			lines.push(line);
+		}
+
+		return lines;
+	},
+	skip: function (n) {
+		this.read(n);
+	},
+	next: function () {
+		var line = this.readLine();
+
+		if (!line)
+			throw new StopIteration();
+
+		if (line[line.length-1] == '\n')
+			line = line.substring(0,line.length-1);
+
+		return line;
+	},
+	print:     illegalWrite,
+	write:     illegalWrite,
+	writeLine: illegalWrite,
+	iterator:  function () { return this; },
+	closed:    function () { return false; },
+	readable:  function () { return true; },
+	writeable: function () { return false; },
+	seekable:  function () { return false; },
+	close: function () {},
+	flush: function () {},
+};
+
+exports.stdout = {
+	charset:   'UTF-8',
+	newline:   '\n',
+	delimiter: ' ',
+	read:      illegalRead,
+	readLine:  illegalRead,
+	readLines: illegalRead,
+	skip:      illegalRead,
+	next:      illegalRead,
+	iterator:  illegalRead,
+	print:     print,
+	writeLine: writeLine,
+	write: function (buf, begin, end) {
+		if (!lastWasStdout) {
+			text = document.createTextNode(substr(buf,begin,end));
+			lastWasStdout = true;
+			output.appendChild(text);
+		}
+		else {
+			text.textContent += substr(buf,begin,end);
+		}
+	},
+	closed:    function () { return false; },
+	readable:  function () { return false; },
+	writeable: function () { return true; },
+	seekable:  function () { return false; },
+	close: function () {},
+	flush: function () {},
+};
+
+exports.stderr = {
+	charset:   'UTF-8',
+	newline:   '\n',
+	delimiter: ' ',
+	read:      illegalRead,
+	readLine:  illegalRead,
+	readLines: illegalRead,
+	skip:      illegalRead,
+	next:      illegalRead,
+	iterator:  illegalRead,
+	print:     print,
+	writeLine: writeLine,
+	write: function (buf, begin, end) {
+		if (lastWasStdout) {
+			var span = document.createElement('span');
+			span.className = 'stderr';
+			text = document.createTextNode(substr(buf,begin,end));
+			lastWasStdout = false;
+			span.appendChild(text);
+			output.appendChild(span);
+		}
+		else {
+			text.textContent += substr(buf,begin,end);
+		}
+	},
+	closed:    function () { return false; },
+	readable:  function () { return false; },
+	writeable: function () { return true; },
+	seekable:  function () { return false; },
+	close: function () {},
+	flush: function () {},
+};
+
+exports.env  = {};
+exports.args = [ window.location.href ];
+
+var search = window.location.search;
+if (search) {
+	if (search[0] == '?') {
+		search = search.substring(1);
+	}
+	var pairs = search.split('&');
+	for (i in pairs) {
+		var pair = pairs[i].split('=');
+		exports.env[pair[0]] = pair[1];
+	}
+}