diff options
Diffstat (limited to 'src/library.js')
-rw-r--r-- | src/library.js | 845 |
1 files changed, 354 insertions, 491 deletions
diff --git a/src/library.js b/src/library.js index 5c2c858d..5e71b087 100644 --- a/src/library.js +++ b/src/library.js @@ -28,13 +28,7 @@ LibraryManager.library = { // dirent.h // ========================================================================== - __dirent_struct_layout: Runtime.generateStructInfo([ - ['i32', 'd_ino'], - ['i32', 'd_off'], - ['i16', 'd_reclen'], - ['i8', 'd_type'], - ['b256', 'd_name']]), - opendir__deps: ['$FS', '__setErrNo', '$ERRNO_CODES', '__dirent_struct_layout', 'open'], + opendir__deps: ['$FS', '__setErrNo', '$ERRNO_CODES', 'open'], opendir: function(dirname) { // DIR *opendir(const char *dirname); // http://pubs.opengroup.org/onlinepubs/007908799/xsh/opendir.html @@ -90,7 +84,7 @@ LibraryManager.library = { // http://pubs.opengroup.org/onlinepubs/007908799/xsh/rewinddir.html _seekdir(dirp, 0); }, - readdir_r__deps: ['$FS', '__setErrNo', '$ERRNO_CODES', '__dirent_struct_layout'], + readdir_r__deps: ['$FS', '__setErrNo', '$ERRNO_CODES'], readdir_r: function(dirp, entry, result) { // int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result); // http://pubs.opengroup.org/onlinepubs/007908799/xsh/readdir_r.html @@ -123,14 +117,14 @@ LibraryManager.library = { FS.isLink(child.mode) ? 10 : // DT_LNK, symbolic link. 8; // DT_REG, regular file. } - {{{ makeSetValue('entry', '___dirent_struct_layout.d_ino', 'id', 'i32') }}} - {{{ makeSetValue('entry', '___dirent_struct_layout.d_off', 'offset', 'i32') }}} - {{{ makeSetValue('entry', '___dirent_struct_layout.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 + ___dirent_struct_layout.d_name', 'i', 'name.charCodeAt(i)', 'i8') }}} + {{{ makeSetValue('entry + ' + C_STRUCTS.dirent.d_name, 'i', 'name.charCodeAt(i)', 'i8') }}} } - {{{ makeSetValue('entry + ___dirent_struct_layout.d_name', 'i', '0', 'i8') }}} - {{{ makeSetValue('entry', '___dirent_struct_layout.d_type', 'type', '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; @@ -145,7 +139,7 @@ LibraryManager.library = { return 0; } // TODO Is it supposed to be safe to execute multiple readdirs? - if (!_readdir.entry) _readdir.entry = _malloc(___dirent_struct_layout.__size__); + if (!_readdir.entry) _readdir.entry = _malloc({{{ C_STRUCTS.dirent.__size__ }}}); if (!_readdir.result) _readdir.result = _malloc(4); var err = _readdir_r(dirp, _readdir.entry, _readdir.result); if (err) { @@ -161,17 +155,14 @@ LibraryManager.library = { // utime.h // ========================================================================== - __utimbuf_struct_layout: Runtime.generateStructInfo([ - ['i32', 'actime'], - ['i32', 'modtime']]), - utime__deps: ['$FS', '__setErrNo', '$ERRNO_CODES', '__utimbuf_struct_layout'], + utime__deps: ['$FS', '__setErrNo', '$ERRNO_CODES'], utime: function(path, times) { // int utime(const char *path, const struct utimbuf *times); // http://pubs.opengroup.org/onlinepubs/009695399/basedefs/utime.h.html var time; if (times) { // NOTE: We don't keep track of access timestamps. - var offset = ___utimbuf_struct_layout.modtime; + var offset = {{{ C_STRUCTS.utimbuf.modtime }}}; time = {{{ makeGetValue('times', 'offset', 'i32') }}} time *= 1000; } else { @@ -253,27 +244,7 @@ LibraryManager.library = { // sys/stat.h // ========================================================================== - __stat_struct_layout: Runtime.generateStructInfo([ - ['i32', 'st_dev'], - ['i32', '__st_dev_padding'], - ['i32', '__st_ino_truncated'], - ['i32', 'st_mode'], - ['i32', 'st_nlink'], - ['i32', 'st_uid'], - ['i32', 'st_gid'], - ['i32', 'st_rdev'], - ['i32', '__st_rdev_padding'], - ['i32', 'st_size'], - ['i32', 'st_blksize'], - ['i32', 'st_blocks'], - ['i32', 'st_atim_secs'], - ['i32', 'st_atim_nsecs'], - ['i32', 'st_mtim_secs'], - ['i32', 'st_mtim_nsecs'], - ['i32', 'st_ctim_secs'], - ['i32', 'st_ctim_nsecs'], - ['i32', 'st_ino']]), - stat__deps: ['$FS', '__stat_struct_layout'], + stat__deps: ['$FS'], stat: function(path, buf, dontResolveLastLink) { // http://pubs.opengroup.org/onlinepubs/7908799/xsh/stat.html // int stat(const char *path, struct stat *buf); @@ -282,25 +253,25 @@ LibraryManager.library = { path = typeof path !== 'string' ? Pointer_stringify(path) : path; try { var stat = dontResolveLastLink ? FS.lstat(path) : FS.stat(path); - {{{ makeSetValue('buf', '___stat_struct_layout.st_dev', 'stat.dev', 'i32') }}}; - {{{ makeSetValue('buf', '___stat_struct_layout.__st_dev_padding', '0', 'i32') }}}; - {{{ makeSetValue('buf', '___stat_struct_layout.__st_ino_truncated', 'stat.ino', 'i32') }}}; - {{{ makeSetValue('buf', '___stat_struct_layout.st_mode', 'stat.mode', 'i32') }}} - {{{ makeSetValue('buf', '___stat_struct_layout.st_nlink', 'stat.nlink', 'i32') }}} - {{{ makeSetValue('buf', '___stat_struct_layout.st_uid', 'stat.uid', 'i32') }}} - {{{ makeSetValue('buf', '___stat_struct_layout.st_gid', 'stat.gid', 'i32') }}} - {{{ makeSetValue('buf', '___stat_struct_layout.st_rdev', 'stat.rdev', 'i32') }}} - {{{ makeSetValue('buf', '___stat_struct_layout.__st_rdev_padding', '0', 'i32') }}}; - {{{ makeSetValue('buf', '___stat_struct_layout.st_size', 'stat.size', 'i32') }}} - {{{ makeSetValue('buf', '___stat_struct_layout.st_blksize', '4096', 'i32') }}} - {{{ makeSetValue('buf', '___stat_struct_layout.st_blocks', 'stat.blocks', 'i32') }}} - {{{ makeSetValue('buf', '___stat_struct_layout.st_atim_secs', 'Math.floor(stat.atime.getTime() / 1000)', 'i32') }}} - {{{ makeSetValue('buf', '___stat_struct_layout.st_atim_nsecs', '0', 'i32') }}} - {{{ makeSetValue('buf', '___stat_struct_layout.st_mtim_secs', 'Math.floor(stat.mtime.getTime() / 1000)', 'i32') }}} - {{{ makeSetValue('buf', '___stat_struct_layout.st_mtim_nsecs', '0', 'i32') }}} - {{{ makeSetValue('buf', '___stat_struct_layout.st_ctim_secs', 'Math.floor(stat.ctime.getTime() / 1000)', 'i32') }}} - {{{ makeSetValue('buf', '___stat_struct_layout.st_ctim_nsecs', '0', 'i32') }}} - {{{ makeSetValue('buf', '___stat_struct_layout.st_ino', 'stat.ino', 'i32') }}} + {{{ 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_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') }}} return 0; } catch (e) { FS.handleFSError(e); @@ -436,43 +407,23 @@ LibraryManager.library = { // sys/statvfs.h // ========================================================================== - __statvfs_struct_layout: Runtime.generateStructInfo([ - ['i32', 'f_bsize'], - ['i32', 'f_frsize'], - ['i32', 'f_blocks'], - ['i32', 'f_bfree'], - ['i32', 'f_bavail'], - ['i32', 'f_files'], - ['i32', 'f_ffree'], - ['i32', 'f_favail'], - ['i32', 'f_fsid'], - ['i32', '__padding'], - ['i32', 'f_flag'], - ['i32', 'f_namemax'], - ['i32', '__reserved_1'], - ['i32', '__reserved_2'], - ['i32', '__reserved_3'], - ['i32', '__reserved_4'], - ['i32', '__reserved_5'], - ['i32', '__reserved_6']]), - statvfs__deps: ['$FS', '__statvfs_struct_layout'], + statvfs__deps: ['$FS'], statvfs: function(path, buf) { // http://pubs.opengroup.org/onlinepubs/009695399/functions/statvfs.html // int statvfs(const char *restrict path, struct statvfs *restrict buf); - var offsets = ___statvfs_struct_layout; // NOTE: None of the constants here are true. We're just returning safe and // sane values. - {{{ makeSetValue('buf', 'offsets.f_bsize', '4096', 'i32') }}} - {{{ makeSetValue('buf', 'offsets.f_frsize', '4096', 'i32') }}} - {{{ makeSetValue('buf', 'offsets.f_blocks', '1000000', 'i32') }}} - {{{ makeSetValue('buf', 'offsets.f_bfree', '500000', 'i32') }}} - {{{ makeSetValue('buf', 'offsets.f_bavail', '500000', 'i32') }}} - {{{ makeSetValue('buf', 'offsets.f_files', 'FS.nextInode', 'i32') }}} - {{{ makeSetValue('buf', 'offsets.f_ffree', '1000000', 'i32') }}} - {{{ makeSetValue('buf', 'offsets.f_favail', '1000000', 'i32') }}} - {{{ makeSetValue('buf', 'offsets.f_fsid', '42', 'i32') }}} - {{{ makeSetValue('buf', 'offsets.f_flag', '2', 'i32') }}} // ST_NOSUID - {{{ makeSetValue('buf', 'offsets.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'], @@ -488,13 +439,7 @@ LibraryManager.library = { // fcntl.h // ========================================================================== - __flock_struct_layout: Runtime.generateStructInfo([ - ['i16', 'l_type'], - ['i16', 'l_whence'], - ['i32', 'l_start'], - ['i32', 'l_len'], - ['i16', 'l_pid']]), - open__deps: ['$FS', '__setErrNo', '$ERRNO_CODES', '__dirent_struct_layout'], + open__deps: ['$FS', '__setErrNo', '$ERRNO_CODES'], open: function(path, oflag, varargs) { // int open(const char *path, int oflag, ...); // http://pubs.opengroup.org/onlinepubs/009695399/functions/open.html @@ -514,16 +459,19 @@ LibraryManager.library = { // http://pubs.opengroup.org/onlinepubs/009695399/functions/creat.html return _open(path, {{{ cDefine('O_WRONLY') }}} | {{{ cDefine('O_CREAT') }}} | {{{ cDefine('O_TRUNC') }}}, allocate([mode, 0, 0, 0], 'i32', ALLOC_STACK)); }, - mkstemp__deps: ['creat'], - mkstemp: function(template) { - if (!_mkstemp.counter) _mkstemp.counter = 0; - var c = (_mkstemp.counter++).toString(); + mktemp: function(template) { + if (!_mktemp.counter) _mktemp.counter = 0; + var c = (_mktemp.counter++).toString(); var rep = 'XXXXXX'; while (c.length < rep.length) c = '0' + c; writeArrayToMemory(intArrayFromString(c), template + Pointer_stringify(template).indexOf(rep)); - return _creat(template, 0600); + return template; + }, + mkstemp__deps: ['creat', 'mktemp'], + mkstemp: function(template) { + return _creat(_mktemp(template), 0600); }, - fcntl__deps: ['$FS', '__setErrNo', '$ERRNO_CODES', '__flock_struct_layout'], + fcntl__deps: ['$FS', '__setErrNo', '$ERRNO_CODES'], fcntl: function(fildes, cmd, varargs, dup2) { // int fcntl(int fildes, int cmd, ...); // http://pubs.opengroup.org/onlinepubs/009695399/functions/fcntl.html @@ -559,7 +507,7 @@ LibraryManager.library = { case {{{ cDefine('F_GETLK') }}}: case {{{ cDefine('F_GETLK64') }}}: var arg = {{{ makeGetValue('varargs', 0, 'i32') }}}; - var offset = ___flock_struct_layout.l_type; + var offset = {{{ C_STRUCTS.flock.l_type }}}; // We're always unlocked. {{{ makeSetValue('arg', 'offset', cDefine('F_UNLCK'), 'i16') }}} return 0; @@ -621,20 +569,15 @@ LibraryManager.library = { // ========================================================================== __DEFAULT_POLLMASK: {{{ cDefine('POLLIN') }}} | {{{ cDefine('POLLOUT') }}}, - __pollfd_struct_layout: Runtime.generateStructInfo([ - ['i32', 'fd'], - ['i16', 'events'], - ['i16', 'revents']]), - poll__deps: ['$FS', '__DEFAULT_POLLMASK', '__pollfd_struct_layout'], + poll__deps: ['$FS', '__DEFAULT_POLLMASK'], poll: function(fds, nfds, timeout) { // int poll(struct pollfd fds[], nfds_t nfds, int timeout); // http://pubs.opengroup.org/onlinepubs/009695399/functions/poll.html - var offsets = ___pollfd_struct_layout; var nonzero = 0; for (var i = 0; i < nfds; i++) { - var pollfd = fds + ___pollfd_struct_layout.__size__ * i; - var fd = {{{ makeGetValue('pollfd', 'offsets.fd', 'i32') }}}; - var events = {{{ makeGetValue('pollfd', 'offsets.events', 'i16') }}}; + var pollfd = fds + {{{ C_STRUCTS.pollfd.__size__ }}} * i; + var fd = {{{ makeGetValue('pollfd', C_STRUCTS.pollfd.fd, 'i32') }}}; + var events = {{{ makeGetValue('pollfd', C_STRUCTS.pollfd.events, 'i16') }}}; var mask = {{{ cDefine('POLLNVAL') }}}; var stream = FS.getStream(fd); if (stream) { @@ -645,7 +588,7 @@ LibraryManager.library = { } mask &= events | {{{ cDefine('POLLERR') }}} | {{{ cDefine('POLLHUP') }}}; if (mask) nonzero++; - {{{ makeSetValue('pollfd', 'offsets.revents', 'mask', 'i16') }}} + {{{ makeSetValue('pollfd', C_STRUCTS.pollfd.revents, 'mask', 'i16') }}} } return nonzero; }, @@ -688,24 +631,13 @@ LibraryManager.library = { // http://pubs.opengroup.org/onlinepubs/000095399/functions/chdir.html // NOTE: The path argument may be a string, to simplify fchdir(). if (typeof path !== 'string') path = Pointer_stringify(path); - var lookup; try { - lookup = FS.lookupPath(path, { follow: true }); + FS.chdir(path); + return 0; } catch (e) { FS.handleFSError(e); return -1; } - if (!FS.isDir(lookup.node.mode)) { - ___setErrNo(ERRNO_CODES.ENOTDIR); - return -1; - } - var err = FS.nodePermissions(lookup.node, 'x'); - if (err) { - ___setErrNo(err); - return -1; - } - FS.currentPath = lookup.path; - return 0; }, chown__deps: ['$FS', '__setErrNo', '$ERRNO_CODES'], chown: function(path, owner, group, dontResolveLastLink) { @@ -909,12 +841,14 @@ LibraryManager.library = { if (size == 0) { ___setErrNo(ERRNO_CODES.EINVAL); return 0; - } else if (size < FS.currentPath.length + 1) { + } + var cwd = FS.cwd(); + if (size < cwd.length + 1) { ___setErrNo(ERRNO_CODES.ERANGE); return 0; } else { - for (var i = 0; i < FS.currentPath.length; i++) { - {{{ makeSetValue('buf', 'i', 'FS.currentPath.charCodeAt(i)', 'i8') }}} + for (var i = 0; i < cwd.length; i++) { + {{{ makeSetValue('buf', 'i', 'cwd.charCodeAt(i)', 'i8') }}} } {{{ makeSetValue('buf', 'i', '0', 'i8') }}} return buf; @@ -1660,12 +1594,6 @@ LibraryManager.library = { __scanString.whiteSpace[{{{ charCode('\v') }}}] = 1; __scanString.whiteSpace[{{{ charCode('\f') }}}] = 1; __scanString.whiteSpace[{{{ charCode('\r') }}}] = 1; - __scanString.whiteSpace[' '] = 1; - __scanString.whiteSpace['\t'] = 1; - __scanString.whiteSpace['\n'] = 1; - __scanString.whiteSpace['\v'] = 1; - __scanString.whiteSpace['\f'] = 1; - __scanString.whiteSpace['\r'] = 1; } // Supports %x, %4x, %d.%d, %lld, %s, %f, %lf. // TODO: Support all format specifiers. @@ -1903,7 +1831,7 @@ LibraryManager.library = { break; } fields++; - } else if (format[formatIndex] in __scanString.whiteSpace) { + } else if (format[formatIndex].charCodeAt(0) in __scanString.whiteSpace) { next = get(); while (next in __scanString.whiteSpace) { if (next <= 0) break mainLoop; // End of input. @@ -1974,6 +1902,7 @@ LibraryManager.library = { var flagLeftAlign = false; var flagAlternative = false; var flagZeroPad = false; + var flagPadSign = false; flagsLoop: while (1) { switch (next) { case {{{ charCode('+') }}}: @@ -1992,6 +1921,9 @@ LibraryManager.library = { flagZeroPad = true; break; } + case {{{ charCode(' ') }}}: + flagPadSign = true; + break; default: break flagsLoop; } @@ -2158,14 +2090,20 @@ LibraryManager.library = { } // Add sign if needed - if (flagAlwaysSigned) { - if (currArg < 0) { - prefix = '-' + prefix; - } else { + if (currArg >= 0) { + if (flagAlwaysSigned) { prefix = '+' + prefix; + } else if (flagPadSign) { + prefix = ' ' + prefix; } } + // Move sign to prefix so we zero-pad after the sign + if (argText.charAt(0) == '-') { + prefix = '-' + prefix; + argText = argText.substr(1); + } + // Add padding. while (prefix.length + argText.length < width) { if (flagLeftAlign) { @@ -2248,8 +2186,12 @@ LibraryManager.library = { if (next == {{{ charCode('E') }}}) argText = argText.toUpperCase(); // Add sign. - if (flagAlwaysSigned && currArg >= 0) { - argText = '+' + argText; + if (currArg >= 0) { + if (flagAlwaysSigned) { + argText = '+' + argText; + } else if (flagPadSign) { + argText = ' ' + argText; + } } } @@ -2887,6 +2829,13 @@ LibraryManager.library = { asprintf: function(s, format, varargs) { return _sprintf(-s, format, varargs); }, + dprintf__deps: ['_formatString', 'write'], + dprintf: function(fd, format, varargs) { + var result = __formatString(format, varargs); + var stack = Runtime.stackSave(); + var ret = _write(fd, allocate(result, 'i8', ALLOC_STACK), result.length); + Runtime.stackRestore(stack); + }, #if TARGET_X86 // va_arg is just like our varargs @@ -2895,6 +2844,7 @@ LibraryManager.library = { vprintf: 'printf', vsprintf: 'sprintf', vasprintf: 'asprintf', + vdprintf: 'dprintf', vscanf: 'scanf', vfscanf: 'fscanf', vsscanf: 'sscanf', @@ -2922,6 +2872,10 @@ LibraryManager.library = { vasprintf: function(s, format, va_arg) { return _asprintf(s, format, {{{ makeGetValue('va_arg', 0, '*') }}}); }, + vdprintf__deps: ['dprintf'], + vdprintf: function (fd, format, va_arg) { + return _dprintf(fd, format, {{{ makeGetValue('va_arg', 0, '*') }}}); + }, vscanf__deps: ['scanf'], vscanf: function(format, va_arg) { return _scanf(format, {{{ makeGetValue('va_arg', 0, '*') }}}); @@ -3054,8 +3008,8 @@ LibraryManager.library = { return ret; }, - abs: 'Math.abs', - labs: 'Math.abs', + abs: 'Math_abs', + labs: 'Math_abs', #if USE_TYPED_ARRAYS == 2 llabs__deps: [function() { Types.preciseI64MathUsed = 1 }], llabs: function(lo, hi) { @@ -3792,6 +3746,7 @@ LibraryManager.library = { }, // We always assume ASCII locale. strcoll: 'strcmp', + strcoll_l: 'strcmp', strcasecmp__asm: true, strcasecmp__sig: 'iii', @@ -3847,8 +3802,8 @@ LibraryManager.library = { p1 = p1|0; p2 = p2|0; num = num|0; var i = 0, v1 = 0, v2 = 0; while ((i|0) < (num|0)) { - var v1 = {{{ makeGetValueAsm('p1', 'i', 'i8', true) }}}; - var v2 = {{{ makeGetValueAsm('p2', 'i', 'i8', true) }}}; + v1 = {{{ makeGetValueAsm('p1', 'i', 'i8', true) }}}; + v2 = {{{ makeGetValueAsm('p2', 'i', 'i8', true) }}}; if ((v1|0) != (v2|0)) return ((v1|0) > (v2|0) ? 1 : -1)|0; i = (i+1)|0; } @@ -4058,6 +4013,7 @@ LibraryManager.library = { } }, _toupper: 'toupper', + toupper_l: 'toupper', tolower__asm: true, tolower__sig: 'ii', @@ -4068,54 +4024,65 @@ LibraryManager.library = { return (chr - {{{ charCode('A') }}} + {{{ charCode('a') }}})|0; }, _tolower: 'tolower', + tolower_l: 'tolower', // The following functions are defined as macros in glibc. islower: function(chr) { return chr >= {{{ charCode('a') }}} && chr <= {{{ charCode('z') }}}; }, + islower_l: 'islower', isupper: function(chr) { return chr >= {{{ charCode('A') }}} && chr <= {{{ charCode('Z') }}}; }, + isupper_l: 'isupper', isalpha: function(chr) { return (chr >= {{{ charCode('a') }}} && chr <= {{{ charCode('z') }}}) || (chr >= {{{ charCode('A') }}} && chr <= {{{ charCode('Z') }}}); }, + isalpha_l: 'isalpha', isdigit: function(chr) { return chr >= {{{ charCode('0') }}} && chr <= {{{ charCode('9') }}}; }, - isdigit_l: 'isdigit', // no locale support yet + isdigit_l: 'isdigit', 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', // no locale support yet + isxdigit_l: 'isxdigit', 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', ispunct: function(chr) { return (chr >= {{{ charCode('!') }}} && chr <= {{{ charCode('/') }}}) || (chr >= {{{ charCode(':') }}} && chr <= {{{ charCode('@') }}}) || (chr >= {{{ charCode('[') }}} && chr <= {{{ charCode('`') }}}) || (chr >= {{{ charCode('{') }}} && chr <= {{{ charCode('~') }}}); }, + ispunct_l: 'ispunct', isspace: function(chr) { return (chr == 32) || (chr >= 9 && chr <= 13); }, + isspace_l: 'isspace', isblank: function(chr) { return chr == {{{ charCode(' ') }}} || chr == {{{ charCode('\t') }}}; }, + isblank_l: 'isblank', iscntrl: function(chr) { return (0 <= chr && chr <= 0x1F) || chr === 0x7F; }, + iscntrl_l: 'iscntrl', isprint: function(chr) { return 0x1F < chr && chr < 0x7F; }, + isprint_l: 'isprint', isgraph: function(chr) { return 0x20 < chr && chr < 0x7F; }, + isgraph_l: 'isgraph', // 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 @@ -4216,7 +4183,7 @@ LibraryManager.library = { llvm_va_end: function() {}, llvm_va_copy: function(ppdest, ppsrc) { - // copy the list start + // copy the list start {{{ makeCopyValues('ppdest', 'ppsrc', Runtime.QUANTUM_SIZE, 'null', null, 1) }}}; // copy the list's current offset (will be advanced with each call to va_arg) @@ -4260,9 +4227,9 @@ LibraryManager.library = { var ret = 0; ret = {{{ makeGetValueAsm('ctlz_i8', 'x >>> 24', 'i8') }}}; if ((ret|0) < 8) return ret|0; - var ret = {{{ makeGetValueAsm('ctlz_i8', '(x >> 16)&0xff', 'i8') }}}; + ret = {{{ makeGetValueAsm('ctlz_i8', '(x >> 16)&0xff', 'i8') }}}; if ((ret|0) < 8) return (ret + 8)|0; - var ret = {{{ makeGetValueAsm('ctlz_i8', '(x >> 8)&0xff', 'i8') }}}; + ret = {{{ makeGetValueAsm('ctlz_i8', '(x >> 8)&0xff', 'i8') }}}; if ((ret|0) < 8) return (ret + 16)|0; return ({{{ makeGetValueAsm('ctlz_i8', 'x&0xff', 'i8') }}} + 24)|0; }, @@ -4296,9 +4263,9 @@ LibraryManager.library = { var ret = 0; ret = {{{ makeGetValueAsm('cttz_i8', 'x & 0xff', 'i8') }}}; if ((ret|0) < 8) return ret|0; - var ret = {{{ makeGetValueAsm('cttz_i8', '(x >> 8)&0xff', 'i8') }}}; + ret = {{{ makeGetValueAsm('cttz_i8', '(x >> 8)&0xff', 'i8') }}}; if ((ret|0) < 8) return (ret + 8)|0; - var ret = {{{ makeGetValueAsm('cttz_i8', '(x >> 16)&0xff', 'i8') }}}; + ret = {{{ makeGetValueAsm('cttz_i8', '(x >> 16)&0xff', 'i8') }}}; if ((ret|0) < 8) return (ret + 16)|0; return ({{{ makeGetValueAsm('cttz_i8', 'x >>> 24', 'i8') }}} + 24)|0; }, @@ -4735,30 +4702,30 @@ LibraryManager.library = { // math.h // ========================================================================== - cos: 'Math.cos', - cosf: 'Math.cos', - cosl: 'Math.cos', - sin: 'Math.sin', - sinf: 'Math.sin', - sinl: 'Math.sin', - tan: 'Math.tan', - tanf: 'Math.tan', - tanl: 'Math.tan', - acos: 'Math.acos', - acosf: 'Math.acos', - acosl: 'Math.acos', - asin: 'Math.asin', - asinf: 'Math.asin', - asinl: 'Math.asin', - atan: 'Math.atan', - atanf: 'Math.atan', - atanl: 'Math.atan', - atan2: 'Math.atan2', - atan2f: 'Math.atan2', - atan2l: 'Math.atan2', - exp: 'Math.exp', - expf: 'Math.exp', - expl: 'Math.exp', + cos: 'Math_cos', + cosf: 'Math_cos', + cosl: 'Math_cos', + sin: 'Math_sin', + sinf: 'Math_sin', + sinl: 'Math_sin', + tan: 'Math_tan', + tanf: 'Math_tan', + tanl: 'Math_tan', + acos: 'Math_acos', + acosf: 'Math_acos', + acosl: 'Math_acos', + asin: 'Math_asin', + asinf: 'Math_asin', + asinl: 'Math_asin', + atan: 'Math_atan', + atanf: 'Math_atan', + atanl: 'Math_atan', + atan2: 'Math_atan2', + atan2f: 'Math_atan2', + atan2l: 'Math_atan2', + exp: 'Math_exp', + expf: 'Math_exp', + expl: 'Math_exp', // The erf and erfc functions are inspired from // http://www.digitalmars.com/archives/cplusplus/3634.html @@ -4820,32 +4787,32 @@ LibraryManager.library = { }, erff: 'erf', erfl: 'erf', - log: 'Math.log', - logf: 'Math.log', - logl: 'Math.log', - sqrt: 'Math.sqrt', - sqrtf: 'Math.sqrt', - sqrtl: 'Math.sqrt', - fabs: 'Math.abs', - fabsf: 'Math.abs', - fabsl: 'Math.abs', - ceil: 'Math.ceil', - ceilf: 'Math.ceil', - ceill: 'Math.ceil', - floor: 'Math.floor', - floorf: 'Math.floor', - floorl: 'Math.floor', - pow: 'Math.pow', - powf: 'Math.pow', - powl: 'Math.pow', - llvm_sqrt_f32: 'Math.sqrt', - llvm_sqrt_f64: 'Math.sqrt', - llvm_pow_f32: 'Math.pow', - llvm_pow_f64: 'Math.pow', - llvm_log_f32: 'Math.log', - llvm_log_f64: 'Math.log', - llvm_exp_f32: 'Math.exp', - llvm_exp_f64: 'Math.exp', + log: 'Math_log', + logf: 'Math_log', + logl: 'Math_log', + sqrt: 'Math_sqrt', + sqrtf: 'Math_sqrt', + sqrtl: 'Math_sqrt', + fabs: 'Math_abs', + fabsf: 'Math_abs', + fabsl: 'Math_abs', + ceil: 'Math_ceil', + ceilf: 'Math_ceil', + ceill: 'Math_ceil', + floor: 'Math_floor', + floorf: 'Math_floor', + floorl: 'Math_floor', + pow: 'Math_pow', + powf: 'Math_pow', + powl: 'Math_pow', + llvm_sqrt_f32: 'Math_sqrt', + llvm_sqrt_f64: 'Math_sqrt', + llvm_pow_f32: 'Math_pow', + llvm_pow_f64: 'Math_pow', + llvm_log_f32: 'Math_log', + llvm_log_f64: 'Math_log', + llvm_exp_f32: 'Math_exp', + llvm_exp_f64: 'Math_exp', ldexp: function(x, exp_) { return x * Math.pow(2, exp_); }, @@ -5068,17 +5035,11 @@ LibraryManager.library = { {{{ makeSetValue('cosine', '0', 'cosineVal', 'float') }}}; }, - __div_t_struct_layout: Runtime.generateStructInfo([ - ['i32', 'quot'], - ['i32', 'rem'], - ]), - div__deps: ['__div_t_struct_layout'], div: function(divt, numer, denom) { var quot = Math.floor(numer / denom); var rem = numer - quot * denom; - var offset = ___div_t_struct_layout.rem; - {{{ makeSetValue('divt', '0', 'quot', 'i32') }}}; - {{{ makeSetValue('divt', 'offset', 'rem', 'i32') }}}; + {{{ makeSetValue('divt', C_STRUCTS.div_t.quot, 'quot', 'i32') }}}; + {{{ makeSetValue('divt', C_STRUCTS.div_t.rem, 'rem', 'i32') }}}; return divt; }, @@ -5097,19 +5058,12 @@ LibraryManager.library = { // sys/utsname.h // ========================================================================== - __utsname_struct_layout: Runtime.generateStructInfo([ - ['b65', 'sysname'], - ['b65', 'nodename'], - ['b65', 'release'], - ['b65', 'version'], - ['b65', 'machine'], - ['b65', 'domainname']]), - uname__deps: ['__utsname_struct_layout'], uname: function(name) { // int uname(struct utsname *name); // http://pubs.opengroup.org/onlinepubs/009695399/functions/uname.html + var layout = {{{ JSON.stringify(C_STRUCTS.utsname) }}}; function copyString(element, value) { - var offset = ___utsname_struct_layout[element]; + var offset = layout[element]; for (var i = 0; i < value.length; i++) { {{{ makeSetValue('name', 'offset + i', 'value.charCodeAt(i)', 'i8') }}} } @@ -5405,62 +5359,48 @@ LibraryManager.library = { return time1 - time0; }, - __tm_struct_layout: Runtime.generateStructInfo([ - ['i32', 'tm_sec'], - ['i32', 'tm_min'], - ['i32', 'tm_hour'], - ['i32', 'tm_mday'], - ['i32', 'tm_mon'], - ['i32', 'tm_year'], - ['i32', 'tm_wday'], - ['i32', 'tm_yday'], - ['i32', 'tm_isdst'], - ['i32', 'tm_gmtoff'], - ['i8*', 'tm_zone']]), // Statically allocated time struct. - __tm_current: 'allocate({{{ Runtime.QUANTUM_SIZE }}}*26, "i8", ALLOC_STATIC)', + __tm_current: 'allocate({{{ C_STRUCTS.tm.__size__ }}}, "i8", ALLOC_STATIC)', // Statically allocated timezone string. We only use GMT as a timezone. __tm_timezone: 'allocate(intArrayFromString("GMT"), "i8", ALLOC_STATIC)', // Statically allocated time strings. - __tm_formatted: 'allocate({{{ Runtime.QUANTUM_SIZE }}}*26, "i8", ALLOC_STATIC)', + __tm_formatted: 'allocate({{{ C_STRUCTS.tm.__size__ }}}, "i8", ALLOC_STATIC)', - mktime__deps: ['__tm_struct_layout', 'tzset'], + mktime__deps: ['tzset'], mktime: function(tmPtr) { _tzset(); - var offsets = ___tm_struct_layout; - var year = {{{ makeGetValue('tmPtr', 'offsets.tm_year', 'i32') }}}; + var year = {{{ makeGetValue('tmPtr', C_STRUCTS.tm.tm_year, 'i32') }}}; var timestamp = new Date(year >= 1900 ? year : year + 1900, - {{{ makeGetValue('tmPtr', 'offsets.tm_mon', 'i32') }}}, - {{{ makeGetValue('tmPtr', 'offsets.tm_mday', 'i32') }}}, - {{{ makeGetValue('tmPtr', 'offsets.tm_hour', 'i32') }}}, - {{{ makeGetValue('tmPtr', 'offsets.tm_min', 'i32') }}}, - {{{ makeGetValue('tmPtr', 'offsets.tm_sec', 'i32') }}}, + {{{ makeGetValue('tmPtr', C_STRUCTS.tm.tm_mon, 'i32') }}}, + {{{ makeGetValue('tmPtr', C_STRUCTS.tm.tm_mday, 'i32') }}}, + {{{ makeGetValue('tmPtr', C_STRUCTS.tm.tm_hour, 'i32') }}}, + {{{ makeGetValue('tmPtr', C_STRUCTS.tm.tm_min, 'i32') }}}, + {{{ makeGetValue('tmPtr', C_STRUCTS.tm.tm_sec, 'i32') }}}, 0).getTime() / 1000; - {{{ makeSetValue('tmPtr', 'offsets.tm_wday', 'new Date(timestamp).getDay()', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_wday, 'new Date(timestamp).getDay()', 'i32') }}} var yday = Math.round((timestamp - (new Date(year, 0, 1)).getTime()) / (1000 * 60 * 60 * 24)); - {{{ makeSetValue('tmPtr', 'offsets.tm_yday', 'yday', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_yday, 'yday', 'i32') }}} return timestamp; }, timelocal: 'mktime', - gmtime__deps: ['malloc', '__tm_struct_layout', '__tm_current', 'gmtime_r'], + gmtime__deps: ['malloc', '__tm_current', 'gmtime_r'], gmtime: function(time) { return _gmtime_r(time, ___tm_current); }, - gmtime_r__deps: ['__tm_struct_layout', '__tm_timezone'], + gmtime_r__deps: ['__tm_timezone'], gmtime_r: function(time, tmPtr) { var date = new Date({{{ makeGetValue('time', 0, 'i32') }}}*1000); - var offsets = ___tm_struct_layout; - {{{ makeSetValue('tmPtr', 'offsets.tm_sec', 'date.getUTCSeconds()', 'i32') }}} - {{{ makeSetValue('tmPtr', 'offsets.tm_min', 'date.getUTCMinutes()', 'i32') }}} - {{{ makeSetValue('tmPtr', 'offsets.tm_hour', 'date.getUTCHours()', 'i32') }}} - {{{ makeSetValue('tmPtr', 'offsets.tm_mday', 'date.getUTCDate()', 'i32') }}} - {{{ makeSetValue('tmPtr', 'offsets.tm_mon', 'date.getUTCMonth()', 'i32') }}} - {{{ makeSetValue('tmPtr', 'offsets.tm_year', 'date.getUTCFullYear()-1900', 'i32') }}} - {{{ makeSetValue('tmPtr', 'offsets.tm_wday', 'date.getUTCDay()', 'i32') }}} - {{{ makeSetValue('tmPtr', 'offsets.tm_gmtoff', '0', 'i32') }}} - {{{ makeSetValue('tmPtr', 'offsets.tm_isdst', '0', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_sec, 'date.getUTCSeconds()', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_min, 'date.getUTCMinutes()', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_hour, 'date.getUTCHours()', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_mday, 'date.getUTCDate()', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_mon, 'date.getUTCMonth()', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_year, 'date.getUTCFullYear()-1900', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_wday, 'date.getUTCDay()', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_gmtoff, '0', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_isdst, '0', 'i32') }}} var start = new Date(date); // define date using UTC, start from Jan 01 00:00:00 UTC start.setUTCDate(1); start.setUTCMonth(0); @@ -5469,8 +5409,8 @@ LibraryManager.library = { start.setUTCSeconds(0); start.setUTCMilliseconds(0); var yday = Math.floor((date.getTime() - start.getTime()) / (1000 * 60 * 60 * 24)); - {{{ makeSetValue('tmPtr', 'offsets.tm_yday', 'yday', 'i32') }}} - {{{ makeSetValue('tmPtr', 'offsets.tm_zone', '___tm_timezone', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_yday, 'yday', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_zone, '___tm_timezone', 'i32') }}} return tmPtr; }, @@ -5484,33 +5424,32 @@ LibraryManager.library = { return ret; }, - localtime__deps: ['malloc', '__tm_struct_layout', '__tm_current', 'localtime_r'], + localtime__deps: ['malloc', '__tm_current', 'localtime_r'], localtime: function(time) { return _localtime_r(time, ___tm_current); }, - localtime_r__deps: ['__tm_struct_layout', '__tm_timezone', 'tzset'], + localtime_r__deps: ['__tm_timezone', 'tzset'], localtime_r: function(time, tmPtr) { _tzset(); - var offsets = ___tm_struct_layout; var date = new Date({{{ makeGetValue('time', 0, 'i32') }}}*1000); - {{{ makeSetValue('tmPtr', 'offsets.tm_sec', 'date.getSeconds()', 'i32') }}} - {{{ makeSetValue('tmPtr', 'offsets.tm_min', 'date.getMinutes()', 'i32') }}} - {{{ makeSetValue('tmPtr', 'offsets.tm_hour', 'date.getHours()', 'i32') }}} - {{{ makeSetValue('tmPtr', 'offsets.tm_mday', 'date.getDate()', 'i32') }}} - {{{ makeSetValue('tmPtr', 'offsets.tm_mon', 'date.getMonth()', 'i32') }}} - {{{ makeSetValue('tmPtr', 'offsets.tm_year', 'date.getFullYear()-1900', 'i32') }}} - {{{ makeSetValue('tmPtr', 'offsets.tm_wday', 'date.getDay()', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_sec, 'date.getSeconds()', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_min, 'date.getMinutes()', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_hour, 'date.getHours()', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_mday, 'date.getDate()', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_mon, 'date.getMonth()', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_year, 'date.getFullYear()-1900', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_wday, 'date.getDay()', 'i32') }}} var start = new Date(date.getFullYear(), 0, 1); var yday = Math.floor((date.getTime() - start.getTime()) / (1000 * 60 * 60 * 24)); - {{{ makeSetValue('tmPtr', 'offsets.tm_yday', 'yday', 'i32') }}} - {{{ makeSetValue('tmPtr', 'offsets.tm_gmtoff', 'start.getTimezoneOffset() * 60', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_yday, 'yday', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_gmtoff, 'start.getTimezoneOffset() * 60', 'i32') }}} var dst = Number(start.getTimezoneOffset() != date.getTimezoneOffset()); - {{{ makeSetValue('tmPtr', 'offsets.tm_isdst', 'dst', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_isdst, 'dst', 'i32') }}} - {{{ makeSetValue('tmPtr', 'offsets.tm_zone', '___tm_timezone', 'i32') }}} + {{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_zone, '___tm_timezone', 'i32') }}} return tmPtr; }, @@ -5621,21 +5560,21 @@ LibraryManager.library = { return newDate; }, - strftime__deps: ['__tm_struct_layout', '_isLeapYear', '_arraySum', '_addDays', '_MONTH_DAYS_REGULAR', '_MONTH_DAYS_LEAP'], + strftime__deps: ['_isLeapYear', '_arraySum', '_addDays', '_MONTH_DAYS_REGULAR', '_MONTH_DAYS_LEAP'], strftime: function(s, maxsize, format, tm) { // size_t strftime(char *restrict s, size_t maxsize, const char *restrict format, const struct tm *restrict timeptr); // http://pubs.opengroup.org/onlinepubs/009695399/functions/strftime.html var date = { - tm_sec: {{{ makeGetValue('tm', '___tm_struct_layout.tm_sec', 'i32') }}}, - tm_min: {{{ makeGetValue('tm', '___tm_struct_layout.tm_min', 'i32') }}}, - tm_hour: {{{ makeGetValue('tm', '___tm_struct_layout.tm_hour', 'i32') }}}, - tm_mday: {{{ makeGetValue('tm', '___tm_struct_layout.tm_mday', 'i32') }}}, - tm_mon: {{{ makeGetValue('tm', '___tm_struct_layout.tm_mon', 'i32') }}}, - tm_year: {{{ makeGetValue('tm', '___tm_struct_layout.tm_year', 'i32') }}}, - tm_wday: {{{ makeGetValue('tm', '___tm_struc |