diff options
author | alon@honor <none@none> | 2010-09-23 20:21:03 -0700 |
---|---|---|
committer | alon@honor <none@none> | 2010-09-23 20:21:03 -0700 |
commit | de2a556fcd00d772b112b0105eee7e74f4c1d7b7 (patch) | |
tree | df7efd36872f8d7e9ef85dc06d823140868e4614 /src | |
parent | 1dd1b85c8462a76388686b4fdeb4ec15f420a30a (diff) |
proper print buffering, + cleanup
Diffstat (limited to 'src')
-rw-r--r-- | src/preamble.js | 42 | ||||
-rw-r--r-- | src/snippets.js | 25 |
2 files changed, 36 insertions, 31 deletions
diff --git a/src/preamble.js b/src/preamble.js index 9dfacca7..a2ceaa0c 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -154,33 +154,6 @@ function __formatString() { return Pointer_make(ret); } -function _printf() { - var text = Pointer_stringify(__formatString.apply(null, arguments)); - // Our print() will print a \n anyhow... remove dupes - if (text[text.length-1] == '\n') { - text = text.substr(0, text.length-1); - } - print(text); -} - -function _puts(p) { - _printf(p); -// print("\n"); // XXX print already always adds one -} - -function _putchar(p) { - print(String.fromCharCode(p)); -} - -function _strlen(p) { - // XXX hardcoded ptr impl - var q = p; - while (HEAP[q] != 0) q++; - return q - p; -// p = Pointer_niceify(p); -// return p.slab.length; // XXX might want to find the null terminator... -} - // Copies a list of num items on the HEAP into a // a normal JavaScript array of numbers function Array_copy(ptr, num) { @@ -218,13 +191,20 @@ function _llvm_memcpy_i32(dest, src, num, idunno) { _llvm_memcpy_i64 = _llvm_memcpy_i32; // Tools -// println((new Error).stack); // for stack traces -function println(text) { - print(text);// + "\n"); // XXX print already always adds one +PRINTBUFFER = ''; +function __print__(text) { + // We print only when we see a '\n', as console JS engines always add + // one anyhow. + PRINTBUFFER = PRINTBUFFER + text; + var endIndex; + while ((endIndex = PRINTBUFFER.indexOf('\n')) != -1) { + print(PRINTBUFFER.substr(0, endIndex)); + PRINTBUFFER = PRINTBUFFER.substr(endIndex + 1); + } } -function jrint(label, obj) { +function jrint(label, obj) { // XXX manual debugging if (!obj) { obj = label; label = ''; diff --git a/src/snippets.js b/src/snippets.js index ddcb6d37..ec29f371 100644 --- a/src/snippets.js +++ b/src/snippets.js @@ -1,4 +1,20 @@ var Snippets = { + // stdio.h + + printf: function() { + __print__(Pointer_stringify(__formatString.apply(null, arguments))); + }, + + puts: function(p) { + __print__(Pointer_stringify(p) + '\n'); + }, + + putchar: function(p) { + __print__(String.fromCharCode(p)); + }, + + // ? + vsnprintf: function(dst, num, src, ptr) { var args = Array_copy(ptr+1, HEAP[ptr]); // # of args in in first place var text = __formatString.apply(null, [src].concat(args)); @@ -12,6 +28,14 @@ var Snippets = { __ATEXIT__.push(func); }, + // string.h + + strlen: function(p) { + var q = p; + while (HEAP[q] != 0) q++; + return q - p; + }, + strspn: function(pstr, pset) { var str = String_copy(pstr, true); var set = String_copy(pset); @@ -119,4 +143,5 @@ var Snippets = { Snippets.__cxa_atexit = Snippets.atexit; // iostream Snippets._ZNSolsEi = Snippets._ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc = Snippets._ZNSolsEd = Snippets._ZNSolsEPFRSoS_E = function(stream, data) { print(data) }; +Snippets._ZNSt8ios_base4InitD1Ev = Snippets._ZNSt8ios_base4InitC1Ev; |