aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/jsifier.js18
-rw-r--r--src/library.js59
-rw-r--r--src/preamble.js4
-rw-r--r--src/runtime.js2
4 files changed, 76 insertions, 7 deletions
diff --git a/src/jsifier.js b/src/jsifier.js
index ed42dae7..90b155e7 100644
--- a/src/jsifier.js
+++ b/src/jsifier.js
@@ -270,13 +270,21 @@ function JSify(data, functionsOnly, givenTypes, givenFunctions) {
processItem: function(item) {
var shortident = item.ident.substr(1);
if (shortident in Library) {
- var snippet = Library[shortident];
- if (typeof snippet === 'string') {
- if (Library[snippet]) {
- snippet = Library[snippet]; // redirection for aliases
+ function addFromLibrary(ident) {
+ var me = arguments.callee;
+ if (!me.added) me.added = {};
+ if (ident in me.added) return '';
+ me.added[ident] = true;
+ var snippet = Library[ident];
+ if (typeof snippet === 'string') {
+ if (Library[snippet]) {
+ snippet = Library[snippet]; // redirection for aliases
+ }
}
+ var deps = Library[ident + '__deps'];
+ return '_' + ident + ' = ' + snippet.toString() + (deps ? '\n' + deps.map(addFromLibrary).join('\n') : '');
}
- item.JS = item.ident + ' = ' + snippet.toString();
+ item.JS = addFromLibrary(shortident);
} else {
item.JS = '// stub for ' + item.ident;
}
diff --git a/src/library.js b/src/library.js
index 9a979ade..a4280a85 100644
--- a/src/library.js
+++ b/src/library.js
@@ -125,6 +125,7 @@ var Library = {
return 0; // TODO
},
+ strtod__deps: ['isspace', 'isdigit'],
strtod: function(str, endptr) {
// XXX handles only whitespace + |[0-9]+(.[0.9]+)?|, no e+
while (_isspace(str)) str++;
@@ -329,6 +330,15 @@ var Library = {
return Pointer_make(String_copy(ptr, true), 0, ALLOC_NORMAL);
},
+ strpbrk: function(ptr1, ptr2) {
+ var searchSet = set.apply(null, String_copy(ptr2));
+ while (IHEAP[ptr1]) {
+ if (IHEAP[ptr1] in searchSet) return ptr1;
+ ptr1++;
+ }
+ return 0;
+ },
+
// ctype.h
isdigit: function(chr) {
@@ -542,6 +552,41 @@ var Library = {
llvm_sqrt_f64: 'Math.sqrt',
llvm_pow_f32: 'Math.pow',
+ modf: function(x, intpart) {
+ FHEAP[intpart] = Math.floor(x);
+#if SAFE_HEAP
+ SAFE_HEAP_ACCESS(intpart, 'f32', true); // XXX f64?
+#endif
+ return x - FHEAP[intpart];
+ },
+
+ frexp: function(x, exp_addr) {
+ var sig = 0, exp_ = 0;
+ if (x !== 0) {
+ var raw_exp = Math.log(x)/Math.log(2);
+ exp_ = Math.ceil(raw_exp);
+ if (exp_ === raw_exp) exp_ += 1;
+ sig = x/Math.pow(2, exp_);
+ }
+ IHEAP[exp_addr] = exp_;
+#if SAFE_HEAP
+ SAFE_HEAP_ACCESS(exp_addr, 'i32', true);
+#endif
+ return sig;
+ },
+
+ __finite: function(x) {
+ return x !== Infinity && x !== -Infinity;
+ },
+
+ __isinf: function(x) {
+ return x === Infinity || x === -Infinity;
+ },
+
+ __isnan: function(x) {
+ return isNaN(x);
+ },
+
// unistd.h
sysconf: function(name_) {
@@ -640,6 +685,20 @@ var Library = {
return 0;
},
+ localeconv: function() {
+ // %struct.timeval = type { char* decimal point, other stuff... }
+ // var indexes = Runtime.calculateStructAlignment({ fields: ['i32', 'i32'] });
+ var me = arguments.callee;
+ if (!me.ret) {
+ me.ret = Pointer_make([Pointer_make(intArrayFromString('.'), null)], null); // just decimal point, for now
+#if SAFE_HEAP
+ SAFE_HEAP_ACCESS(me.ret, 'i32', true);
+ SAFE_HEAP_ACCESS(IHEAP[me.ret], 'i32', true);
+#endif
+ }
+ return me.ret;
+ },
+
// langinfo.h
nl_langinfo: function(item) {
diff --git a/src/preamble.js b/src/preamble.js
index 3494053a..e577ba3c 100644
--- a/src/preamble.js
+++ b/src/preamble.js
@@ -203,7 +203,7 @@ function __initializeRuntime__() {
FHEAP = HEAP; // fallback
}
#else
- IHEAP = HEAP; // We use that name in our runtime code that processes strings etc., see library.js
+ IHEAP = FHEAP = HEAP;
#endif
Module['HEAP'] = HEAP;
@@ -281,7 +281,7 @@ function __formatString() {
if (next == 'e'.charCodeAt(0) || next == 'g'.charCodeAt(0)) {
next = 'f'.charCodeAt(0); // no support for 'e'
}
- if (['d', 'u', 'p', 'f'].indexOf(String.fromCharCode(next)) != -1) {
+ if (['d', 'i', 'u', 'p', 'f'].indexOf(String.fromCharCode(next)) != -1) {
var currArg;
var argText;
currArg = getNextArg(next);
diff --git a/src/runtime.js b/src/runtime.js
index 455d8c0e..d6ce9c3d 100644
--- a/src/runtime.js
+++ b/src/runtime.js
@@ -107,6 +107,8 @@ Runtime = {
getNativeFieldSize: getNativeFieldSize,
dedup: dedup,
+ set: set,
+
// Calculate aligned size, just like C structs should be. TODO: Consider
// requesting that compilation be done with #pragma pack(push) /n #pragma pack(1),
// which would remove much of the complexity here.