diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/library.js | 11 | ||||
-rw-r--r-- | src/preamble.js | 96 |
2 files changed, 62 insertions, 45 deletions
diff --git a/src/library.js b/src/library.js index 166806ba..9a979ade 100644 --- a/src/library.js +++ b/src/library.js @@ -29,6 +29,10 @@ var Library = { __print__(Pointer_stringify(p) + '\n'); }, + fputc: function(chr, stream) { + __print__(String.fromCharCode(chr)); + }, + putchar: function(p) { __print__(String.fromCharCode(p)); }, @@ -55,8 +59,8 @@ var Library = { }, fwrite: function(ptr, size, count, stream) { - print('[fwrite] ' + Pointer_stringify(ptr).slice(0,count)); - return count; // or 0? + __print__(intArrayToString(Array_copy(ptr, count))); + return count; }, fclose: function(stream) { @@ -85,6 +89,9 @@ var Library = { return 0; // XXX }, + clearerr: function(stream) { + }, + // stdlib.h abs: 'Math.abs', diff --git a/src/preamble.js b/src/preamble.js index aa58e172..da6660ba 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -152,15 +152,11 @@ function Pointer_make(slab, pos, allocator) { } function Pointer_stringify(ptr) { - ptr = Pointer_niceify(ptr); - var ret = ""; var i = 0; var t; while (1) { -// if ((ptr.pos + i) >= ptr.slab.length) { return "<< Invalid read: " + (ptr.pos+i) + " : " + ptr.slab.length + " >>"; } else {} - if ((ptr.pos+i) >= ptr.slab.length) { break; } else {} - t = String.fromCharCode(ptr.slab[ptr.pos + i]); + t = String.fromCharCode(IHEAP[ptr + i]); if (t == "\0") { break; } else {} ret += t; i += 1; @@ -263,48 +259,54 @@ function __formatString() { while (curr) { // Note: should be curr != 0, technically. But this helps catch bugs with undefineds curr = IHEAP[textIndex]; next = IHEAP[textIndex+1]; - if (curr == '%'.charCodeAt(0) && ['d', 'u', 'f', '.'].indexOf(String.fromCharCode(next)) != -1) { - var currArg; - var argText; - // Handle very very simply formatting, namely only %.Xf - if (next == '.'.charCodeAt(0)) { - var limit = 0; - while(1) { - var limitChr = IHEAP[textIndex+2]; - if (!(limitChr >= '0'.charCodeAt(0) && limitChr <= '9'.charCodeAt(0))) break; - limit *= 10; - limit += limitChr - '0'.charCodeAt(0); - textIndex++; - } - textIndex--; + if (curr == '%'.charCodeAt(0)) { + if (next == 'l'.charCodeAt(0)) { + textIndex++; next = IHEAP[textIndex+1]; - currArg = getNextArg(next); - argText = String(+currArg); // +: boolean=>int - var dotIndex = argText.indexOf('.'); - if (dotIndex == -1) { - dotIndex = argText.length; - argText += '.'; + } + if (['d', 'u', 'f', '.'].indexOf(String.fromCharCode(next)) != -1) { + var currArg; + var argText; + // Handle very very simply formatting, namely only %.Xf + if (next == '.'.charCodeAt(0)) { + var limit = 0; + while(1) { + var limitChr = IHEAP[textIndex+2]; + if (!(limitChr >= '0'.charCodeAt(0) && limitChr <= '9'.charCodeAt(0))) break; + limit *= 10; + limit += limitChr - '0'.charCodeAt(0); + textIndex++; + } + textIndex--; + next = IHEAP[textIndex+1]; + currArg = getNextArg(next); + argText = String(+currArg); // +: boolean=>int + var dotIndex = argText.indexOf('.'); + if (dotIndex == -1) { + dotIndex = argText.length; + argText += '.'; + } + argText += '00000000000'; // padding + argText = argText.substr(0, dotIndex+1+limit); + textIndex += 2; + } else if (next == 'u'.charCodeAt(0)) { + currArg = getNextArg(next); + argText = String(unSign(currArg, 32)); + } else { + currArg = getNextArg(next); + argText = String(+currArg); // +: boolean=>int } - argText += '00000000000'; // padding - argText = argText.substr(0, dotIndex+1+limit); + argText.split('').forEach(function(chr) { + ret.push(chr.charCodeAt(0)); + }); + textIndex += 2; + } else if (next == 's'.charCodeAt(0)) { + ret = ret.concat(String_copy(getNextArg(next))); + textIndex += 2; + } else if (next == 'c'.charCodeAt(0)) { + ret = ret.concat(getNextArg(next)); textIndex += 2; - } else if (next == 'u'.charCodeAt(0)) { - currArg = getNextArg(next); - argText = String(unSign(currArg, 32)); - } else { - currArg = getNextArg(next); - argText = String(+currArg); // +: boolean=>int } - argText.split('').forEach(function(chr) { - ret.push(chr.charCodeAt(0)); - }); - textIndex += 2; - } else if (curr == '%'.charCodeAt(0) && next == 's'.charCodeAt(0)) { - ret = ret.concat(String_copy(getNextArg(next))); - textIndex += 2; - } else if (curr == '%'.charCodeAt(0) && next == 'c'.charCodeAt(0)) { - ret = ret.concat(getNextArg(next)); - textIndex += 2; } else { ret.push(curr); textIndex += 1; @@ -418,6 +420,14 @@ function intArrayFromString(stringy) { return ret; } +function intArrayToString(array) { + var ret = ''; + for (var i = 0; i < array.length; i++) { + ret += String.fromCharCode(array[i]); + } + return ret; +} + // Converts a value we have as signed, into an unsigned value. For // example, -1 in int32 would be a very large number as unsigned. function unSign(value, bits) { |