// An implementation of a libc for the web. Basically, implementations of
// the various standard C libraries, that can be called from compiled code,
// and work using the actual JavaScript environment.
//
// We search the Library object when there is an external function. If the
// entry in the Library is a function, we insert it. If it is a string, we
// do another lookup in the library (a simple way to write a function once,
// if it can be called by different names). We also allow dependencies,
// using __deps. Initialization code to be run after allocating all
// global constants can be defined by __postset.
//
// Note that the full function name will be '_' + the name in the Library
// object. For convenience, the short name appears here. Note that if you add a
// new function with an '_', it will not be found.
var Library = {
// ==========================================================================
// stdio.h
// ==========================================================================
_scanString: function() {
// Supports %x, %4x, %d.%d
var str = Pointer_stringify(arguments[0]);
var stri = 0;
var fmt = Pointer_stringify(arguments[1]);
var fmti = 0;
var args = Array.prototype.slice.call(arguments, 2);
var argsi = 0;
var read = 0;
while (fmti < fmt.length) {
if (fmt[fmti] === '%') {
fmti++;
var max_ = parseInt(fmt[fmti]);
if (!isNaN(max_)) fmti++;
var type = fmt[fmti];
fmti++;
var curr = 0;
while ((curr < max_