aboutsummaryrefslogtreecommitdiff
path: root/src/library.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/library.js')
-rw-r--r--src/library.js1002
1 files changed, 735 insertions, 267 deletions
diff --git a/src/library.js b/src/library.js
index 077a2ebb..64da5444 100644
--- a/src/library.js
+++ b/src/library.js
@@ -23,6 +23,7 @@ LibraryManager.library = {
stdout: 'allocate(1, "i32*", ALLOC_STATIC)',
stderr: 'allocate(1, "i32*", ALLOC_STATIC)',
_impure_ptr: 'allocate(1, "i32*", ALLOC_STATIC)',
+ __dso_handle: 'allocate(1, "i32*", ALLOC_STATIC)',
// ==========================================================================
// dirent.h
@@ -99,7 +100,7 @@ LibraryManager.library = {
return FS.handleFSError(e);
}
if (stream.position < 0 || stream.position >= entries.length) {
- {{{ makeSetValue('result', '0', '0', 'i8*') }}}
+ {{{ makeSetValue('result', '0', '0', 'i8*') }}};
return 0;
}
var id;
@@ -117,15 +118,15 @@ LibraryManager.library = {
FS.isLink(child.mode) ? 10 : // DT_LNK, symbolic link.
8; // DT_REG, regular file.
}
- {{{ makeSetValue('entry', C_STRUCTS.dirent.d_ino, 'id', 'i32') }}}
- {{{ makeSetValue('entry', C_STRUCTS.dirent.d_off, 'offset', 'i32') }}}
- {{{ makeSetValue('entry', C_STRUCTS.dirent.d_reclen, 'name.length + 1', 'i32') }}}
+ {{{ makeSetValue('entry', C_STRUCTS.dirent.d_ino, 'id', 'i32') }}};
+ {{{ makeSetValue('entry', C_STRUCTS.dirent.d_off, 'offset', 'i32') }}};
+ {{{ makeSetValue('entry', C_STRUCTS.dirent.d_reclen, 'name.length + 1', 'i32') }}};
for (var i = 0; i < name.length; i++) {
- {{{ makeSetValue('entry + ' + C_STRUCTS.dirent.d_name, 'i', 'name.charCodeAt(i)', 'i8') }}}
+ {{{ makeSetValue('entry + ' + C_STRUCTS.dirent.d_name, 'i', 'name.charCodeAt(i)', 'i8') }}};
}
- {{{ makeSetValue('entry + ' + C_STRUCTS.dirent.d_name, 'i', '0', 'i8') }}}
- {{{ makeSetValue('entry', C_STRUCTS.dirent.d_type, 'type', 'i8') }}}
- {{{ makeSetValue('result', '0', 'entry', 'i8*') }}}
+ {{{ makeSetValue('entry + ' + C_STRUCTS.dirent.d_name, 'i', '0', 'i8') }}};
+ {{{ makeSetValue('entry', C_STRUCTS.dirent.d_type, 'type', 'i8') }}};
+ {{{ makeSetValue('result', '0', 'entry', 'i8*') }}};
stream.position++;
return 0;
},
@@ -148,8 +149,6 @@ LibraryManager.library = {
}
return {{{ makeGetValue(0, '_readdir.result', 'i8*') }}};
},
- __01readdir64_: 'readdir',
- // TODO: Check if we need to link any other aliases.
// ==========================================================================
// utime.h
@@ -163,7 +162,7 @@ LibraryManager.library = {
if (times) {
// NOTE: We don't keep track of access timestamps.
var offset = {{{ C_STRUCTS.utimbuf.modtime }}};
- time = {{{ makeGetValue('times', 'offset', 'i32') }}}
+ time = {{{ makeGetValue('times', 'offset', 'i32') }}};
time *= 1000;
} else {
time = Date.now();
@@ -206,13 +205,13 @@ LibraryManager.library = {
var length = i;
if (allSlashes) {
// All slashes result in a single slash.
- {{{ makeSetValue('path', '1', '0', 'i8') }}}
+ {{{ makeSetValue('path', '1', '0', 'i8') }}};
return [path, -1];
} else {
// Strip trailing slashes.
while (slashPositions.length &&
slashPositions[slashPositions.length - 1] == length - 1) {
- {{{ makeSetValue('path', 'slashPositions.pop(i)', '0', 'i8') }}}
+ {{{ makeSetValue('path', 'slashPositions.pop(i)', '0', 'i8') }}};
length--;
}
return [path, slashPositions.pop()];
@@ -226,16 +225,15 @@ LibraryManager.library = {
var result = ___libgenSplitName(path);
return result[0] + result[1] + 1;
},
- __xpg_basename: 'basename',
dirname__deps: ['__libgenSplitName'],
dirname: function(path) {
// char *dirname(char *path);
// http://pubs.opengroup.org/onlinepubs/007908799/xsh/dirname.html
var result = ___libgenSplitName(path);
if (result[1] == 0) {
- {{{ makeSetValue('result[0]', 1, '0', 'i8') }}}
+ {{{ makeSetValue('result[0]', 1, '0', 'i8') }}};
} else if (result[1] !== -1) {
- {{{ makeSetValue('result[0]', 'result[1]', '0', 'i8') }}}
+ {{{ makeSetValue('result[0]', 'result[1]', '0', 'i8') }}};
}
return result[0];
},
@@ -256,22 +254,22 @@ LibraryManager.library = {
{{{ makeSetValue('buf', C_STRUCTS.stat.st_dev, 'stat.dev', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.__st_dev_padding, '0', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.__st_ino_truncated, 'stat.ino', 'i32') }}};
- {{{ makeSetValue('buf', C_STRUCTS.stat.st_mode, 'stat.mode', 'i32') }}}
- {{{ makeSetValue('buf', C_STRUCTS.stat.st_nlink, 'stat.nlink', 'i32') }}}
- {{{ makeSetValue('buf', C_STRUCTS.stat.st_uid, 'stat.uid', 'i32') }}}
- {{{ makeSetValue('buf', C_STRUCTS.stat.st_gid, 'stat.gid', 'i32') }}}
- {{{ makeSetValue('buf', C_STRUCTS.stat.st_rdev, 'stat.rdev', 'i32') }}}
+ {{{ makeSetValue('buf', C_STRUCTS.stat.st_mode, 'stat.mode', 'i32') }}};
+ {{{ makeSetValue('buf', C_STRUCTS.stat.st_nlink, 'stat.nlink', 'i32') }}};
+ {{{ makeSetValue('buf', C_STRUCTS.stat.st_uid, 'stat.uid', 'i32') }}};
+ {{{ makeSetValue('buf', C_STRUCTS.stat.st_gid, 'stat.gid', 'i32') }}};
+ {{{ makeSetValue('buf', C_STRUCTS.stat.st_rdev, 'stat.rdev', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.__st_rdev_padding, '0', 'i32') }}};
- {{{ makeSetValue('buf', C_STRUCTS.stat.st_size, 'stat.size', 'i32') }}}
- {{{ makeSetValue('buf', C_STRUCTS.stat.st_blksize, '4096', 'i32') }}}
- {{{ makeSetValue('buf', C_STRUCTS.stat.st_blocks, 'stat.blocks', 'i32') }}}
- {{{ makeSetValue('buf', C_STRUCTS.stat.st_atim.tv_sec, 'Math.floor(stat.atime.getTime() / 1000)', 'i32') }}}
- {{{ makeSetValue('buf', C_STRUCTS.stat.st_atim.tv_nsec, '0', 'i32') }}}
- {{{ makeSetValue('buf', C_STRUCTS.stat.st_mtim.tv_sec, 'Math.floor(stat.mtime.getTime() / 1000)', 'i32') }}}
- {{{ makeSetValue('buf', C_STRUCTS.stat.st_mtim.tv_nsec, '0', 'i32') }}}
- {{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_sec, 'Math.floor(stat.ctime.getTime() / 1000)', 'i32') }}}
- {{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_nsec, '0', 'i32') }}}
- {{{ makeSetValue('buf', C_STRUCTS.stat.st_ino, 'stat.ino', 'i32') }}}
+ {{{ makeSetValue('buf', C_STRUCTS.stat.st_size, 'stat.size', 'i32') }}};
+ {{{ makeSetValue('buf', C_STRUCTS.stat.st_blksize, '4096', 'i32') }}};
+ {{{ makeSetValue('buf', C_STRUCTS.stat.st_blocks, 'stat.blocks', 'i32') }}};
+ {{{ makeSetValue('buf', C_STRUCTS.stat.st_atim.tv_sec, 'Math.floor(stat.atime.getTime() / 1000)', 'i32') }}};
+ {{{ makeSetValue('buf', C_STRUCTS.stat.st_atim.tv_nsec, '0', 'i32') }}};
+ {{{ makeSetValue('buf', C_STRUCTS.stat.st_mtim.tv_sec, 'Math.floor(stat.mtime.getTime() / 1000)', 'i32') }}};
+ {{{ makeSetValue('buf', C_STRUCTS.stat.st_mtim.tv_nsec, '0', 'i32') }}};
+ {{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_sec, 'Math.floor(stat.ctime.getTime() / 1000)', 'i32') }}};
+ {{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_nsec, '0', 'i32') }}};
+ {{{ makeSetValue('buf', C_STRUCTS.stat.st_ino, 'stat.ino', 'i32') }}};
return 0;
} catch (e) {
FS.handleFSError(e);
@@ -394,14 +392,6 @@ LibraryManager.library = {
_umask.cmask = newMask;
return oldMask;
},
- stat64: 'stat',
- fstat64: 'fstat',
- lstat64: 'lstat',
- __01fstat64_: 'fstat',
- __01stat64_: 'stat',
- __01lstat64_: 'lstat',
-
- // TODO: Check if other aliases are needed.
// ==========================================================================
// sys/statvfs.h
@@ -413,17 +403,17 @@ LibraryManager.library = {
// int statvfs(const char *restrict path, struct statvfs *restrict buf);
// NOTE: None of the constants here are true. We're just returning safe and
// sane values.
- {{{ makeSetValue('buf', C_STRUCTS.statvfs.f_bsize, '4096', 'i32') }}}
- {{{ makeSetValue('buf', C_STRUCTS.statvfs.f_frsize, '4096', 'i32') }}}
- {{{ makeSetValue('buf', C_STRUCTS.statvfs.f_blocks, '1000000', 'i32') }}}
- {{{ makeSetValue('buf', C_STRUCTS.statvfs.f_bfree, '500000', 'i32') }}}
- {{{ makeSetValue('buf', C_STRUCTS.statvfs.f_bavail, '500000', 'i32') }}}
- {{{ makeSetValue('buf', C_STRUCTS.statvfs.f_files, 'FS.nextInode', 'i32') }}}
- {{{ makeSetValue('buf', C_STRUCTS.statvfs.f_ffree, '1000000', 'i32') }}}
- {{{ makeSetValue('buf', C_STRUCTS.statvfs.f_favail, '1000000', 'i32') }}}
- {{{ makeSetValue('buf', C_STRUCTS.statvfs.f_fsid, '42', 'i32') }}}
- {{{ makeSetValue('buf', C_STRUCTS.statvfs.f_flag, '2', 'i32') }}} // ST_NOSUID
- {{{ makeSetValue('buf', C_STRUCTS.statvfs.f_namemax, '255', 'i32') }}}
+ {{{ makeSetValue('buf', C_STRUCTS.statvfs.f_bsize, '4096', 'i32') }}};
+ {{{ makeSetValue('buf', C_STRUCTS.statvfs.f_frsize, '4096', 'i32') }}};
+ {{{ makeSetValue('buf', C_STRUCTS.statvfs.f_blocks, '1000000', 'i32') }}};
+ {{{ makeSetValue('buf', C_STRUCTS.statvfs.f_bfree, '500000', 'i32') }}};
+ {{{ makeSetValue('buf', C_STRUCTS.statvfs.f_bavail, '500000', 'i32') }}};
+ {{{ makeSetValue('buf', C_STRUCTS.statvfs.f_files, 'FS.nextInode', 'i32') }}};
+ {{{ makeSetValue('buf', C_STRUCTS.statvfs.f_ffree, '1000000', 'i32') }}};
+ {{{ makeSetValue('buf', C_STRUCTS.statvfs.f_favail, '1000000', 'i32') }}};
+ {{{ makeSetValue('buf', C_STRUCTS.statvfs.f_fsid, '42', 'i32') }}};
+ {{{ makeSetValue('buf', C_STRUCTS.statvfs.f_flag, '2', 'i32') }}}; // ST_NOSUID
+ {{{ makeSetValue('buf', C_STRUCTS.statvfs.f_namemax, '255', 'i32') }}};
return 0;
},
fstatvfs__deps: ['statvfs'],
@@ -432,8 +422,6 @@ LibraryManager.library = {
// http://pubs.opengroup.org/onlinepubs/009604499/functions/statvfs.html
return _statvfs(0, buf);
},
- __01statvfs64_: 'statvfs',
- __01fstatvfs64_: 'fstatvfs',
// ==========================================================================
// fcntl.h
@@ -471,6 +459,11 @@ LibraryManager.library = {
mkstemp: function(template) {
return _creat(_mktemp(template), 0600);
},
+ mkdtemp__deps: ['mktemp', 'mkdir'],
+ mkdtemp: function(template) {
+ template = _mktemp(template);
+ return (_mkdir(template, 0700) === 0) ? template : 0;
+ },
fcntl__deps: ['$FS', '__setErrNo', '$ERRNO_CODES'],
fcntl: function(fildes, cmd, varargs, dup2) {
// int fcntl(int fildes, int cmd, ...);
@@ -509,7 +502,7 @@ LibraryManager.library = {
var arg = {{{ makeGetValue('varargs', 0, 'i32') }}};
var offset = {{{ C_STRUCTS.flock.l_type }}};
// We're always unlocked.
- {{{ makeSetValue('arg', 'offset', cDefine('F_UNLCK'), 'i16') }}}
+ {{{ makeSetValue('arg', 'offset', cDefine('F_UNLCK'), 'i16') }}};
return 0;
case {{{ cDefine('F_SETLK') }}}:
case {{{ cDefine('F_SETLKW') }}}:
@@ -535,7 +528,7 @@ LibraryManager.library = {
// Advise as much as you wish. We don't care.
return 0;
},
- posix_madvise: 'posix_fadvise',
+ posix_madvise: function(){ return 0 }, // ditto as fadvise
posix_fallocate__deps: ['$FS', '__setErrNo', '$ERRNO_CODES'],
posix_fallocate: function(fd, offset, len) {
// int posix_fallocate(int fd, off_t offset, off_t len);
@@ -565,6 +558,25 @@ LibraryManager.library = {
},
// ==========================================================================
+ // nl_types.h
+ // ==========================================================================
+
+ catopen: function(name, oflag) {
+ // nl_catd catopen (const char *name, int oflag)
+ return -1;
+ },
+
+ catgets: function(catd, set_id, msg_id, s) {
+ // char *catgets (nl_catd catd, int set_id, int msg_id, const char *s)
+ return s;
+ },
+
+ catclose: function(catd) {
+ // int catclose (nl_catd catd)
+ return 0;
+ },
+
+ // ==========================================================================
// poll.h
// ==========================================================================
@@ -588,7 +600,7 @@ LibraryManager.library = {
}
mask &= events | {{{ cDefine('POLLERR') }}} | {{{ cDefine('POLLHUP') }}};
if (mask) nonzero++;
- {{{ makeSetValue('pollfd', C_STRUCTS.pollfd.revents, 'mask', 'i16') }}}
+ {{{ makeSetValue('pollfd', C_STRUCTS.pollfd.revents, 'mask', 'i16') }}};
}
return nonzero;
},
@@ -1168,9 +1180,9 @@ LibraryManager.library = {
} else {
var length = Math.min(len, value.length);
for (var i = 0; i < length; i++) {
- {{{ makeSetValue('buf', 'i', 'value.charCodeAt(i)', 'i8') }}}
+ {{{ makeSetValue('buf', 'i', 'value.charCodeAt(i)', 'i8') }}};
}
- if (len > length) {{{ makeSetValue('buf', 'i++', '0', 'i8') }}}
+ if (len > length) {{{ makeSetValue('buf', 'i++', '0', 'i8') }}};
return i;
}
},
@@ -1217,9 +1229,9 @@ LibraryManager.library = {
// int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
// http://linux.die.net/man/2/getresuid
// We have just one process/group/user, all with ID 0.
- {{{ makeSetValue('ruid', '0', '0', 'i32') }}}
- {{{ makeSetValue('euid', '0', '0', 'i32') }}}
- {{{ makeSetValue('suid', '0', '0', 'i32') }}}
+ {{{ makeSetValue('ruid', '0', '0', 'i32') }}};
+ {{{ makeSetValue('euid', '0', '0', 'i32') }}};
+ {{{ makeSetValue('suid', '0', '0', 'i32') }}};
return 0;
},
getresgid: 'getresuid',
@@ -1231,7 +1243,7 @@ LibraryManager.library = {
___setErrNo(ERRNO_CODES.EINVAL);
return -1;
} else {
- {{{ makeSetValue('grouplist', '0', '0', 'i32') }}}
+ {{{ makeSetValue('grouplist', '0', '0', 'i32') }}};
return 1;
}
},
@@ -1264,10 +1276,10 @@ LibraryManager.library = {
}
var length = Math.min(namelen, host.length);
for (var i = 0; i < length; i++) {
- {{{ makeSetValue('name', 'i', 'host.charCodeAt(i)', 'i8') }}}
+ {{{ makeSetValue('name', 'i', 'host.charCodeAt(i)', 'i8') }}};
}
if (namelen > length) {
- {{{ makeSetValue('name', 'i', '0', 'i8') }}}
+ {{{ makeSetValue('name', 'i', '0', 'i8') }}};
return 0;
} else {
___setErrNo(ERRNO_CODES.ENAMETOOLONG);
@@ -1384,8 +1396,8 @@ LibraryManager.library = {
for (var i = 0; i < nbytes; i += 2) {
var first = {{{ makeGetValue('src', 'i', 'i8') }}};
var second = {{{ makeGetValue('src', 'i + 1', 'i8') }}};
- {{{ makeSetValue('dest', 'i', 'second', 'i8') }}}
- {{{ makeSetValue('dest', 'i + 1', 'first', 'i8') }}}
+ {{{ makeSetValue('dest', 'i', 'second', 'i8') }}};
+ {{{ makeSetValue('dest', 'i + 1', 'first', 'i8') }}};
}
},
tcgetpgrp: function(fildes) {
@@ -1559,14 +1571,6 @@ LibraryManager.library = {
if (bytes != 0) self.alloc(bytes);
return ret; // Previous break location.
},
- open64: 'open',
- lseek64: 'lseek',
- ftruncate64: 'ftruncate',
- __01open64_: 'open',
- __01lseek64_: 'lseek',
- __01truncate64_: 'truncate',
- __01ftruncate64_: 'ftruncate',
- // TODO: Check if any other aliases are needed.
// ==========================================================================
// stdio.h
@@ -1798,7 +1802,7 @@ LibraryManager.library = {
break;
case 'X':
case 'x':
- {{{ makeSetValue('argPtr', 0, 'parseInt(text, 16)', 'i32') }}}
+ {{{ makeSetValue('argPtr', 0, 'parseInt(text, 16)', 'i32') }}};
break;
case 'F':
case 'f':
@@ -1809,15 +1813,15 @@ LibraryManager.library = {
case 'E':
// fallthrough intended
if (long_) {
- {{{ makeSetValue('argPtr', 0, 'parseFloat(text)', 'double') }}}
+ {{{ makeSetValue('argPtr', 0, 'parseFloat(text)', 'double') }}};
} else {
- {{{ makeSetValue('argPtr', 0, 'parseFloat(text)', 'float') }}}
+ {{{ makeSetValue('argPtr', 0, 'parseFloat(text)', 'float') }}};
}
break;
case 's':
var array = intArrayFromString(text);
for (var j = 0; j < array.length; j++) {
- {{{ makeSetValue('argPtr', 'j', 'array[j]', 'i8') }}}
+ {{{ makeSetValue('argPtr', 'j', 'array[j]', 'i8') }}};
}
break;
}
@@ -1862,14 +1866,13 @@ LibraryManager.library = {
#endif
#if USE_TYPED_ARRAYS == 2
} else if (type == 'i64') {
-
-#if TARGET_LE32
+#if TARGET_LE32 == 1
ret = [{{{ makeGetValue('varargs', 'argIndex', 'i32', undefined, undefined, true) }}},
{{{ makeGetValue('varargs', 'argIndex+8', 'i32', undefined, undefined, true) }}}];
argIndex += {{{ STACK_ALIGN }}}; // each 32-bit chunk is in a 64-bit block
#else
- ret = [{{{ makeGetValue('varargs', 'argIndex', 'i32', undefined, undefined, true) }}},
- {{{ makeGetValue('varargs', 'argIndex+4', 'i32', undefined, undefined, true) }}}];
+ ret = [{{{ makeGetValue('varargs', 'argIndex', 'i32', undefined, undefined, true, 4) }}},
+ {{{ makeGetValue('varargs', 'argIndex+4', 'i32', undefined, undefined, true, 4) }}}];
#endif
#else
@@ -1945,9 +1948,9 @@ LibraryManager.library = {
}
// Handle precision.
- var precisionSet = false;
+ var precisionSet = false, precision = -1;
if (next == {{{ charCode('.') }}}) {
- var precision = 0;
+ precision = 0;
precisionSet = true;
textIndex++;
next = {{{ makeGetValue(0, 'textIndex+1', 'i8') }}};
@@ -1964,8 +1967,10 @@ LibraryManager.library = {
}
}
next = {{{ makeGetValue(0, 'textIndex+1', 'i8') }}};
- } else {
- var precision = 6; // Standard default.
+ }
+ if (precision === -1) {
+ precision = 6; // Standard default.
+ precisionSet = false;
}
// Handle integer sizes. WARNING: These assume a 32-bit architecture!
@@ -2252,7 +2257,7 @@ LibraryManager.library = {
case 'n': {
// Write the length written so far to the next parameter.
var ptr = getNextArg('i32*');
- {{{ makeSetValue('ptr', '0', 'ret.length', 'i32') }}}
+ {{{ makeSetValue('ptr', '0', 'ret.length', 'i32') }}};
break;
}
case '%': {
@@ -2379,9 +2384,9 @@ LibraryManager.library = {
___setErrNo(ERRNO_CODES.ESPIPE);
return -1;
}
- {{{ makeSetValue('pos', '0', 'stream.position', 'i32') }}}
+ {{{ makeSetValue('pos', '0', 'stream.position', 'i32') }}};
var state = (stream.eof ? 1 : 0) + (stream.error ? 2 : 0);
- {{{ makeSetValue('pos', Runtime.getNativeTypeSize('i32'), 'state', 'i32') }}}
+ {{{ makeSetValue('pos', Runtime.getNativeTypeSize('i32'), 'state', 'i32') }}};
return 0;
},
fgets__deps: ['fgetc'],
@@ -2398,9 +2403,9 @@ LibraryManager.library = {
if (streamObj.error || (streamObj.eof && i == 0)) return 0;
else if (streamObj.eof) break;
}
- {{{ makeSetValue('s', 'i', 'byte_', 'i8') }}}
+ {{{ makeSetValue('s', 'i', 'byte_', 'i8') }}};
}
- {{{ makeSetValue('s', 'i', '0', 'i8') }}}
+ {{{ makeSetValue('s', 'i', '0', 'i8') }}};
return s;
},
gets__deps: ['fgets'],
@@ -2464,7 +2469,7 @@ LibraryManager.library = {
// int fputc(int c, FILE *stream);
// http://pubs.opengroup.org/onlinepubs/000095399/functions/fputc.html
var chr = unSign(c & 0xFF);
- {{{ makeSetValue('_fputc.ret', '0', 'chr', 'i8') }}}
+ {{{ makeSetValue('_fputc.ret', '0', 'chr', 'i8') }}};
var ret = _write(stream, _fputc.ret, 1);
if (ret == -1) {
var streamObj = FS.getStream(stream);
@@ -2513,8 +2518,12 @@ LibraryManager.library = {
}
var bytesRead = 0;
var streamObj = FS.getStream(stream);
+ if (!streamObj) {
+ ___setErrNo(ERRNO_CODES.EBADF);
+ return 0;
+ }
while (streamObj.ungotten.length && bytesToRead > 0) {
- {{{ makeSetValue('ptr++', '0', 'streamObj.ungotten.pop()', 'i8') }}}
+ {{{ makeSetValue('ptr++', '0', 'streamObj.ungotten.pop()', 'i8') }}};
bytesToRead--;
bytesRead++;
}
@@ -2557,7 +2566,6 @@ LibraryManager.library = {
return 0;
},
fseeko: 'fseek',
- fseeko64: 'fseek',
fsetpos__deps: ['$FS', 'lseek', '__setErrNo', '$ERRNO_CODES'],
fsetpos: function(stream, pos) {
// int fsetpos(FILE *stream, const fpos_t *pos);
@@ -2594,7 +2602,6 @@ LibraryManager.library = {
}
},
ftello: 'ftell',
- ftello64: 'ftell',
fwrite__deps: ['$FS', 'write'],
fwrite: function(ptr, size, nitems, stream) {
// size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream);
@@ -2886,22 +2893,6 @@ LibraryManager.library = {
},
#endif
- fopen64: 'fopen',
- __01fopen64_: 'fopen',
- __01freopen64_: 'freopen',
- __01fseeko64_: 'fseek',
- __01ftello64_: 'ftell',
- __01tmpfile64_: 'tmpfile',
- __isoc99_fscanf: 'fscanf',
- // TODO: Check if any other aliases are needed.
- _IO_getc: 'getc',
- _IO_putc: 'putc',
- _ZNSo3putEc: 'putchar',
- _ZNSo5flushEv__deps: ['fflush', 'stdout'],
- _ZNSo5flushEv: function() {
- _fflush({{{ makeGetValue(makeGlobalUse('_stdout'), '0', 'void*') }}});
- },
-
// ==========================================================================
// sys/mman.h
// ==========================================================================
@@ -2939,7 +2930,6 @@ LibraryManager.library = {
_mmap.mappings[ptr] = { malloc: ptr, num: num, allocated: allocated };
return ptr;
},
- __01mmap64_: 'mmap',
munmap: function(start, num) {
if (!_mmap.mappings) _mmap.mappings = {};
@@ -3136,7 +3126,7 @@ LibraryManager.library = {
// Set end pointer.
if (endptr) {
- {{{ makeSetValue('endptr', 0, 'str', '*') }}}
+ {{{ makeSetValue('endptr', 0, 'str', '*') }}};
}
// Unsign if needed.
@@ -3222,7 +3212,7 @@ LibraryManager.library = {
// Set end pointer.
if (endptr) {
- {{{ makeSetValue('endptr', 0, 'str', '*') }}}
+ {{{ makeSetValue('endptr', 0, 'str', '*') }}};
}
try {
@@ -3239,22 +3229,34 @@ LibraryManager.library = {
strtoll: function(str, endptr, base) {
return __parseInt64(str, endptr, base, '-9223372036854775808', '9223372036854775807'); // LLONG_MIN, LLONG_MAX.
},
- strtoll_l: 'strtoll', // no locale support yet
+ strtoll_l__deps: ['strtoll'],
+ strtoll_l: function(str, endptr, base) {
+ return _strtoll(str, endptr, base); // no locale support yet
+ },
strtol__deps: ['_parseInt'],
strtol: function(str, endptr, base) {
return __parseInt(str, endptr, base, -2147483648, 2147483647, 32); // LONG_MIN, LONG_MAX.
},
- strtol_l: 'strtol', // no locale support yet
+ strtol_l__deps: ['strtol'],
+ strtol_l: function(str, endptr, base) {
+ return _strtol(str, endptr, base); // no locale support yet
+ },
strtoul__deps: ['_parseInt'],
strtoul: function(str, endptr, base) {
return __parseInt(str, endptr, base, 0, 4294967295, 32, true); // ULONG_MAX.
},
- strtoul_l: 'strtoul', // no locale support yet
+ strtoul_l__deps: ['strtoul'],
+ strtoul_l: function(str, endptr, base) {
+ return _strtoul(str, endptr, base); // no locale support yet
+ },
strtoull__deps: ['_parseInt64'],
strtoull: function(str, endptr, base) {
return __parseInt64(str, endptr, base, 0, '18446744073709551615', true); // ULONG_MAX.
},
- strtoull_l: 'strtoull', // no locale support yet
+ strtoull_l__deps: ['strtoull'],
+ strtoull_l: function(str, endptr, base) {
+ return _strtoull(str, endptr, base); // no locale support yet
+ },
atoi__deps: ['strtol'],
atoi: function(ptr) {
@@ -3316,7 +3318,7 @@ LibraryManager.library = {
poolPtr = allocate(TOTAL_ENV_SIZE, 'i8', ALLOC_STATIC);
envPtr = allocate(MAX_ENV_VALUES * {{{ Runtime.QUANTUM_SIZE }}},
'i8*', ALLOC_STATIC);
- {{{ makeSetValue('envPtr', '0', 'poolPtr', 'i8*') }}}
+ {{{ makeSetValue('envPtr', '0', 'poolPtr', 'i8*') }}};
{{{ makeSetValue(makeGlobalUse('_environ'), 0, 'envPtr', 'i8*') }}};
} else {
envPtr = {{{ makeGetValue(makeGlobalUse('_environ'), '0', 'i8**') }}};
@@ -3440,7 +3442,7 @@ LibraryManager.library = {
var limit = Math.min(nelem, 3);
var doubleSize = {{{ Runtime.getNativeTypeSize('double') }}};
for (var i = 0; i < limit; i++) {
- {{{ makeSetValue('loadavg', 'i * doubleSize', '0.1', 'double') }}}
+ {{{ makeSetValue('loadavg', 'i * doubleSize', '0.1', 'double') }}};
}
return limit;
},
@@ -3487,9 +3489,9 @@ LibraryManager.library = {
} else {
var size = Math.min(4095, absolute.path.length); // PATH_MAX - 1.
for (var i = 0; i < size; i++) {
- {{{ makeSetValue('resolved_name', 'i', 'absolute.path.charCodeAt(i)', 'i8') }}}
+ {{{ makeSetValue('resolved_name', 'i', 'absolute.path.charCodeAt(i)', 'i8') }}};
}
- {{{ makeSetValue('resolved_name', 'size', '0', 'i8') }}}
+ {{{ makeSetValue('resolved_name', 'size', '0', 'i8') }}};
return resolved_name;
}
},
@@ -3548,13 +3550,15 @@ LibraryManager.library = {
llvm_memcpy_p0i8_p0i8_i32: 'memcpy',
llvm_memcpy_p0i8_p0i8_i64: 'memcpy',
- memmove__sig: 'viii',
+ memmove__sig: 'iiii',
memmove__asm: true,
memmove__deps: ['memcpy'],
memmove: function(dest, src, num) {
dest = dest|0; src = src|0; num = num|0;
+ var ret = 0;
if (((src|0) < (dest|0)) & ((dest|0) < ((src + num)|0))) {
// Unlikely case: Copy backwards in a safe manner
+ ret = dest;
src = (src + num)|0;
dest = (dest + num)|0;
while ((num|0) > 0) {
@@ -3563,9 +3567,11 @@ LibraryManager.library = {
num = (num - 1)|0;
{{{ makeSetValueAsm('dest', 0, makeGetValueAsm('src', 0, 'i8'), 'i8') }}};
}
+ dest = ret;
} else {
_memcpy(dest, src, num) | 0;
}
+ return dest | 0;
},
llvm_memmove_i32: 'memmove',
llvm_memmove_i64: 'memmove',
@@ -3582,7 +3588,7 @@ LibraryManager.library = {
memset__inline: function(ptr, value, num, align) {
return makeSetValues(ptr, 0, value, 'null', num, align);
},
- memset__sig: 'viii',
+ memset__sig: 'iiii',
memset__asm: true,
memset: function(ptr, value, num) {
#if USE_TYPED_ARRAYS == 2
@@ -3611,8 +3617,10 @@ LibraryManager.library = {
{{{ makeSetValueAsm('ptr', 0, 'value', 'i8') }}};
ptr = (ptr+1)|0;
}
+ return (ptr-num)|0;
#else
{{{ makeSetValues('ptr', '0', 'value', 'null', 'num') }}};
+ return ptr;
#endif
},
llvm_memset_i32: 'memset',
@@ -3691,7 +3699,7 @@ LibraryManager.library = {
var padding = 0, curr = 0, i = 0;
while ((i|0) < (num|0)) {
curr = padding ? 0 : {{{ makeGetValueAsm('psrc', 'i', 'i8') }}};
- {{{ makeSetValue('pdest', 'i', 'curr', 'i8') }}}
+ {{{ makeSetValue('pdest', 'i', 'curr', 'i8') }}};
padding = padding ? 1 : ({{{ makeGetValueAsm('psrc', 'i', 'i8') }}} == 0);
i = (i+1)|0;
}
@@ -3744,7 +3752,7 @@ LibraryManager.library = {
if ({{{ makeGetValue('pdest', 'len+i', 'i8') }}} == 0) break;
i ++;
if (i == num) {
- {{{ makeSetValue('pdest', 'len+i', 0, 'i8') }}}
+ {{{ makeSetValue('pdest', 'len+i', 0, 'i8') }}};
break;
}
}
@@ -3757,7 +3765,10 @@ LibraryManager.library = {
},
// We always assume ASCII locale.
strcoll: 'strcmp',
- strcoll_l: 'strcmp',
+ strcoll_l__deps: ['strcoll'],
+ strcoll_l: function(px, py) {
+ return _strcoll(px, py); // no locale support yet
+ },
strcasecmp__asm: true,
strcasecmp__sig: 'iii',
@@ -3831,6 +3842,7 @@ LibraryManager.library = {
},
strnlen: function(ptr, num) {
+ num = num >>> 0;
for (var i = 0; i < num; i++) {
if ({{{ makeGetValue('ptr', 0, 'i8') }}} == 0) return i;
ptr++;
@@ -4021,7 +4033,10 @@ LibraryManager.library = {
}
},
_toupper: 'toupper',
- toupper_l: 'toupper',
+ toupper_l__deps: ['toupper'],
+ toupper_l: function(str, endptr, base) {
+ return _toupper(str, endptr, base); // no locale support yet
+ },
tolower__asm: true,
tolower__sig: 'ii',
@@ -4032,65 +4047,104 @@ LibraryManager.library = {
return (chr - {{{ charCode('A') }}} + {{{ charCode('a') }}})|0;
},
_tolower: 'tolower',
- tolower_l: 'tolower',
+ tolower_l__deps: ['tolower'],
+ tolower_l: function(chr) {
+ return _tolower(chr); // no locale support yet
+ },
// The following functions are defined as macros in glibc.
islower: function(chr) {
return chr >= {{{ charCode('a') }}} && chr <= {{{ charCode('z') }}};
},
- islower_l: 'islower',
+ islower_l__deps: ['islower'],
+ islower_l: function(chr) {
+ return _islower(chr); // no locale support yet
+ },
isupper: function(chr) {
return chr >= {{{ charCode('A') }}} && chr <= {{{ charCode('Z') }}};
},
- isupper_l: 'isupper',
+ isupper_l__deps: ['isupper'],
+ isupper_l: function(chr) {
+ return _isupper(chr); // no locale support yet
+ },
isalpha: function(chr) {
return (chr >= {{{ charCode('a') }}} && chr <= {{{ charCode('z') }}}) ||
(chr >= {{{ charCode('A') }}} && chr <= {{{ charCode('Z') }}});
},
- isalpha_l: 'isalpha',
+ isalpha_l__deps: ['isalpha'],
+ isalpha_l: function(chr) {
+ return _isalpha(chr); // no locale support yet
+ },
isdigit: function(chr) {
return chr >= {{{ charCode('0') }}} && chr <= {{{ charCode('9') }}};
},
- isdigit_l: 'isdigit',
+ isdigit_l__deps: ['isdigit'],
+ isdigit_l: function(chr) {
+ return _isdigit(chr); // no locale support yet
+ },
isxdigit: function(chr) {
return (chr >= {{{ charCode('0') }}} && chr <= {{{ charCode('9') }}}) ||
(chr >= {{{ charCode('a') }}} && chr <= {{{ charCode('f') }}}) ||
(chr >= {{{ charCode('A') }}} && chr <= {{{ charCode('F') }}});
},
- isxdigit_l: 'isxdigit',
+ isxdigit_l__deps: ['isxdigit'],
+ isxdigit_l: function(chr) {
+ return _isxdigit(chr); // no locale support yet
+ },
isalnum: function(chr) {
return (chr >= {{{ charCode('0') }}} && chr <= {{{ charCode('9') }}}) ||
(chr >= {{{ charCode('a') }}} && chr <= {{{ charCode('z') }}}) ||
(chr >= {{{ charCode('A') }}} && chr <= {{{ charCode('Z') }}});
},
- isalnum_l: 'isalnum',
+ isalnum_l__deps: ['isalnum'],
+ isalnum_l: function(chr) {
+ return _isalnum(chr); // no locale support yet
+ },
ispunct: function(chr) {
return (chr >= {{{ charCode('!') }}} && chr <= {{{ charCode('/') }}}) ||
(chr >= {{{ charCode(':') }}} && chr <= {{{ charCode('@') }}}) ||
(chr >= {{{ charCode('[') }}} && chr <= {{{ charCode('`') }}}) ||
(chr >= {{{ charCode('{') }}} && chr <= {{{ charCode('~') }}});
},
- ispunct_l: 'ispunct',
+ ispunct_l__deps: ['ispunct'],
+ ispunct_l: function(chr) {
+ return _ispunct(chr); // no locale support yet
+ },
isspace: function(chr) {
return (chr == 32) || (chr >= 9 && chr <= 13);
},
- isspace_l: 'isspace',
+ isspace_l__deps: ['isspace'],
+ isspace_l: function(chr) {
+ return _isspace(chr); // no locale support yet
+ },
isblank: function(chr) {
return chr == {{{ charCode(' ') }}} || chr == {{{ charCode('\t') }}};
},
- isblank_l: 'isblank',
+ isblank_l__deps: ['isblank'],
+ isblank_l: function(chr) {
+ return _isblank(chr); // no locale support yet
+ },
iscntrl: function(chr) {
return (0 <= chr && chr <= 0x1F) || chr === 0x7F;
},
- iscntrl_l: 'iscntrl',
+ iscntrl_l__deps: ['iscntrl'],
+ iscntrl_l: function(chr) {
+ return _iscntrl(chr); // no locale support yet
+ },
isprint: function(chr) {
return 0x1F < chr && chr < 0x7F;
},
- isprint_l: 'isprint',
+ isprint_l__deps: ['isprint'],
+ isprint_l: function(chr) {
+ return _isprint(chr); // no locale support yet
+ },
isgraph: function(chr) {
return 0x20 < chr && chr < 0x7F;
},
- isgraph_l: 'isgraph',
+ isgraph_l__deps: ['isgraph'],
+ isgraph_l: function(chr) {
+ return _isgraph(chr); // no locale support yet
+ },
// Lookup tables for glibc ctype implementation.
__ctype_b_loc: function() {
// http://refspecs.freestandards.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/baselib---ctype-b-loc.html
@@ -4111,7 +4165,7 @@ LibraryManager.library = {
var i16size = {{{ Runtime.getNativeTypeSize('i16') }}};
var arr = _malloc(values.length * i16size);
for (var i = 0; i < values.length; i++) {
- {{{ makeSetValue('arr', 'i * i16size', 'values[i]', 'i16') }}}
+ {{{ makeSetValue('arr', 'i * i16size', 'values[i]', 'i16') }}};
}
me.ret = allocate([arr + 128 * i16size], 'i16*', ALLOC_NORMAL);
}
@@ -4139,7 +4193,7 @@ LibraryManager.library = {
var i32size = {{{ Runtime.getNativeTypeSize('i32') }}};
var arr = _malloc(values.length * i32size);
for (var i = 0; i < values.length; i++) {
- {{{ makeSetValue('arr', 'i * i32size', 'values[i]', 'i32') }}}
+ {{{ makeSetValue('arr', 'i * i32size', 'values[i]', 'i32') }}};
}
me.ret = allocate([arr + 128 * i32size], 'i32*', ALLOC_NORMAL);
}
@@ -4166,7 +4220,7 @@ LibraryManager.library = {
var i32size = {{{ Runtime.getNativeTypeSize('i32') }}};
var arr = _malloc(values.length * i32size);
for (var i = 0; i < values.length; i++) {
- {{{ makeSetValue('arr', 'i * i32size', 'values[i]', 'i32') }}}
+ {{{ makeSetValue('arr', 'i * i32size', 'values[i]', 'i32') }}};
}
me.ret = allocate([arr + 128 * i32size], 'i32*', ALLOC_NORMAL);
}
@@ -4312,6 +4366,8 @@ LibraryManager.library = {
abort('trap!');
},
+ llvm_prefetch: function(){},
+
__assert_fail: function(condition, filename, line, func) {
ABORT = true;
throw 'Assertion failed: ' + Pointer_stringify(condition) + ', at: ' + [filename ? Pointer_stringify(filename) : 'unknown filename', line, func ? Pointer_stringify(func) : 'unknown function'] + ' at ' + stackTrace();
@@ -4337,21 +4393,36 @@ LibraryManager.library = {
_ZTVN10__cxxabiv120__si_class_type_infoE: [2], // yes inherited classes
#endif
+ // We store an extra header in front of the exception data provided
+ // by the user.
+ // This header is:
+ // * type
+ // * destructor function pointer
+ // This is then followed by the actual exception data.
+ __cxa_exception_header_size: 8,
+ __cxa_last_thrown_exception: 0,
+ __cxa_caught_exceptions: [],
+
// Exceptions
+ __cxa_allocate_exception__deps: ['__cxa_exception_header_size'],
__cxa_allocate_exception: function(size) {
- return _malloc(size);
+ var ptr = _malloc(size + ___cxa_exception_header_size);
+ return ptr + ___cxa_exception_header_size;
},
+ __cxa_free_exception__deps: ['__cxa_exception_header_size'],
__cxa_free_exception: function(ptr) {
try {
- return _free(ptr);
+ return _free(ptr - ___cxa_exception_header_size);
} catch(e) { // XXX FIXME
#if ASSERTIONS
Module.printErr('exception during cxa_free_exception: ' + e);
#endif
}
},
+ // Here, we throw an exception after recording a couple of values that we need to remember
+ // We also remember that it was the last exception thrown as we need to know that later.
__cxa_throw__sig: 'viii',
- __cxa_throw__deps: ['llvm_eh_exception', '_ZSt18uncaught_exceptionv', '__cxa_find_matching_catch'],
+ __cxa_throw__deps: ['_ZSt18uncaught_exceptionv', '__cxa_find_matching_catch', '__cxa_exception_header_size', '__cxa_last_thrown_exception'],
__cxa_throw: function(ptr, type, destructor) {
if (!___cxa_throw.initialized) {
try {
@@ -4368,28 +4439,34 @@ LibraryManager.library = {
#if EXCEPTION_DEBUG
Module.printErr('Compiled code throwing an exception, ' + [ptr,type,destructor] + ', at ' + stackTrace());
#endif
- {{{ makeSetValue('_llvm_eh_exception.buf', '0', 'ptr', 'void*') }}}
- {{{ makeSetValue('_llvm_eh_exception.buf', QUANTUM_SIZE, 'type', 'void*') }}}
- {{{ makeSetValue('_llvm_eh_exception.buf', 2 * QUANTUM_SIZE, 'destructor', 'void*') }}}
+ var header = ptr - ___cxa_exception_header_size;
+ {{{ makeSetValue('header', 0, 'type', 'void*') }}};
+ {{{ makeSetValue('header', 4, 'destructor', 'void*') }}};
+ ___cxa_last_thrown_exception = ptr;
if (!("uncaught_exception" in __ZSt18uncaught_exceptionv)) {
__ZSt18uncaught_exceptionv.uncaught_exception = 1;
} else {
__ZSt18uncaught_exceptionv.uncaught_exception++;
}
- {{{ makeThrow('ptr') }}};
+ {{{ makeThrow('ptr') }}}
},
- __cxa_rethrow__deps: ['llvm_eh_exception', '__cxa_end_catch'],
+ // This exception will be caught twice, but while begin_catch runs twice,
+ // we early-exit from end_catch when the exception has been rethrown, so
+ // pop that here from the caught exceptions.
+ __cxa_rethrow__deps: ['__cxa_end_catch', '__cxa_caught_exceptions'],
__cxa_rethrow: function() {
___cxa_end_catch.rethrown = true;
- {{{ makeThrow(makeGetValue('_llvm_eh_exception.buf', '0', 'void*')) }}};
+ var ptr = ___cxa_caught_exceptions.pop();
+ {{{ makeThrow('ptr') }}}
},
- llvm_eh_exception__postset: '_llvm_eh_exception.buf = allocate(12, "void*", ALLOC_STATIC);',
+ llvm_eh_exception__deps: ['__cxa_last_thrown_exception'],
llvm_eh_exception: function() {
- return {{{ makeGetValue('_llvm_eh_exception.buf', '0', 'void*') }}};
+ return ___cxa_last_thrown_exception;
},
llvm_eh_selector__jsargs: true,
+ llvm_eh_selector__deps: ['__cxa_last_thrown_exception'],
llvm_eh_selector: function(unused_exception_value, personality/*, varargs*/) {
- var type = {{{ makeGetValue('_llvm_eh_exception.buf', QUANTUM_SIZE, 'void*') }}}
+ var type = ___cxa_last_thrown_exception;
for (var i = 2; i < arguments.length; i++) {
if (arguments[i] == type) return type;
}
@@ -4398,12 +4475,22 @@ LibraryManager.library = {
llvm_eh_typeid_for: function(type) {
return type;
},
- __cxa_begin_catch__deps: ['_ZSt18uncaught_exceptionv'],
+ // Note that we push the last thrown exception here rather than the ptr.
+ // This is because if the exception is a pointer (as in test 3 of test_exceptions_typed),
+ // we don't actually get the value that we allocated, but something else. Easiest
+ // to remember that the last exception thrown is going to be the first to be caught,
+ // so just use that value ins