aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/preamble.js42
-rw-r--r--src/snippets.js25
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;