1// Modified embenchen to direct to asm-wasm. 2// Flags: --expose-wasm 3 4var EXPECTED_OUTPUT = 5 '123456789\n' + 6 '213456789\n' + 7 '231456789\n' + 8 '321456789\n' + 9 '312456789\n' + 10 '132456789\n' + 11 '234156789\n' + 12 '324156789\n' + 13 '342156789\n' + 14 '432156789\n' + 15 '423156789\n' + 16 '243156789\n' + 17 '341256789\n' + 18 '431256789\n' + 19 '413256789\n' + 20 '143256789\n' + 21 '134256789\n' + 22 '314256789\n' + 23 '412356789\n' + 24 '142356789\n' + 25 '124356789\n' + 26 '214356789\n' + 27 '241356789\n' + 28 '421356789\n' + 29 '234516789\n' + 30 '324516789\n' + 31 '342516789\n' + 32 '432516789\n' + 33 '423516789\n' + 34 '243516789\n' + 35 'Pfannkuchen(9) = 30.\n'; 36var Module = { 37 arguments: [1], 38 print: function(x) {Module.printBuffer += x + '\n';}, 39 preRun: [function() {Module.printBuffer = ''}], 40 postRun: [function() { 41 assertEquals(EXPECTED_OUTPUT, Module.printBuffer); 42 }], 43}; 44// The Module object: Our interface to the outside world. We import 45// and export values on it, and do the work to get that through 46// closure compiler if necessary. There are various ways Module can be used: 47// 1. Not defined. We create it here 48// 2. A function parameter, function(Module) { ..generated code.. } 49// 3. pre-run appended it, var Module = {}; ..generated code.. 50// 4. External script tag defines var Module. 51// We need to do an eval in order to handle the closure compiler 52// case, where this code here is minified but Module was defined 53// elsewhere (e.g. case 4 above). We also need to check if Module 54// already exists (e.g. case 3 above). 55// Note that if you want to run closure, and also to use Module 56// after the generated code, you will need to define var Module = {}; 57// before the code. Then that object will be used in the code, and you 58// can continue to use Module afterwards as well. 59var Module; 60if (!Module) Module = (typeof Module !== 'undefined' ? Module : null) || {}; 61 62// Sometimes an existing Module object exists with properties 63// meant to overwrite the default module functionality. Here 64// we collect those properties and reapply _after_ we configure 65// the current environment's defaults to avoid having to be so 66// defensive during initialization. 67var moduleOverrides = {}; 68for (var key in Module) { 69 if (Module.hasOwnProperty(key)) { 70 moduleOverrides[key] = Module[key]; 71 } 72} 73 74// The environment setup code below is customized to use Module. 75// *** Environment setup code *** 76var ENVIRONMENT_IS_NODE = typeof process === 'object' && typeof require === 'function'; 77var ENVIRONMENT_IS_WEB = typeof window === 'object'; 78var ENVIRONMENT_IS_WORKER = typeof importScripts === 'function'; 79var ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER; 80 81if (ENVIRONMENT_IS_NODE) { 82 // Expose functionality in the same simple way that the shells work 83 // Note that we pollute the global namespace here, otherwise we break in node 84 if (!Module['print']) Module['print'] = function print(x) { 85 process['stdout'].write(x + '\n'); 86 }; 87 if (!Module['printErr']) Module['printErr'] = function printErr(x) { 88 process['stderr'].write(x + '\n'); 89 }; 90 91 var nodeFS = require('fs'); 92 var nodePath = require('path'); 93 94 Module['read'] = function read(filename, binary) { 95 filename = nodePath['normalize'](filename); 96 var ret = nodeFS['readFileSync'](filename); 97 // The path is absolute if the normalized version is the same as the resolved. 98 if (!ret && filename != nodePath['resolve'](filename)) { 99 filename = path.join(__dirname, '..', 'src', filename); 100 ret = nodeFS['readFileSync'](filename); 101 } 102 if (ret && !binary) ret = ret.toString(); 103 return ret; 104 }; 105 106 Module['readBinary'] = function readBinary(filename) { return Module['read'](filename, true) }; 107 108 Module['load'] = function load(f) { 109 globalEval(read(f)); 110 }; 111 112 Module['arguments'] = process['argv'].slice(2); 113 114 module['exports'] = Module; 115} 116else if (ENVIRONMENT_IS_SHELL) { 117 if (!Module['print']) Module['print'] = print; 118 if (typeof printErr != 'undefined') Module['printErr'] = printErr; // not present in v8 or older sm 119 120 if (typeof read != 'undefined') { 121 Module['read'] = read; 122 } else { 123 Module['read'] = function read() { throw 'no read() available (jsc?)' }; 124 } 125 126 Module['readBinary'] = function readBinary(f) { 127 return read(f, 'binary'); 128 }; 129 130 if (typeof scriptArgs != 'undefined') { 131 Module['arguments'] = scriptArgs; 132 } else if (typeof arguments != 'undefined') { 133 Module['arguments'] = arguments; 134 } 135 136 this['Module'] = Module; 137 138 eval("if (typeof gc === 'function' && gc.toString().indexOf('[native code]') > 0) var gc = undefined"); // wipe out the SpiderMonkey shell 'gc' function, which can confuse closure (uses it as a minified name, and it is then initted to a non-falsey value unexpectedly) 139} 140else if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) { 141 Module['read'] = function read(url) { 142 var xhr = new XMLHttpRequest(); 143 xhr.open('GET', url, false); 144 xhr.send(null); 145 return xhr.responseText; 146 }; 147 148 if (typeof arguments != 'undefined') { 149 Module['arguments'] = arguments; 150 } 151 152 if (typeof console !== 'undefined') { 153 if (!Module['print']) Module['print'] = function print(x) { 154 console.log(x); 155 }; 156 if (!Module['printErr']) Module['printErr'] = function printErr(x) { 157 console.log(x); 158 }; 159 } else { 160 // Probably a worker, and without console.log. We can do very little here... 161 var TRY_USE_DUMP = false; 162 if (!Module['print']) Module['print'] = (TRY_USE_DUMP && (typeof(dump) !== "undefined") ? (function(x) { 163 dump(x); 164 }) : (function(x) { 165 // self.postMessage(x); // enable this if you want stdout to be sent as messages 166 })); 167 } 168 169 if (ENVIRONMENT_IS_WEB) { 170 window['Module'] = Module; 171 } else { 172 Module['load'] = importScripts; 173 } 174} 175else { 176 // Unreachable because SHELL is dependant on the others 177 throw 'Unknown runtime environment. Where are we?'; 178} 179 180function globalEval(x) { 181 eval.call(null, x); 182} 183if (!Module['load'] == 'undefined' && Module['read']) { 184 Module['load'] = function load(f) { 185 globalEval(Module['read'](f)); 186 }; 187} 188if (!Module['print']) { 189 Module['print'] = function(){}; 190} 191if (!Module['printErr']) { 192 Module['printErr'] = Module['print']; 193} 194if (!Module['arguments']) { 195 Module['arguments'] = []; 196} 197// *** Environment setup code *** 198 199// Closure helpers 200Module.print = Module['print']; 201Module.printErr = Module['printErr']; 202 203// Callbacks 204Module['preRun'] = []; 205Module['postRun'] = []; 206 207// Merge back in the overrides 208for (var key in moduleOverrides) { 209 if (moduleOverrides.hasOwnProperty(key)) { 210 Module[key] = moduleOverrides[key]; 211 } 212} 213 214 215 216// === Auto-generated preamble library stuff === 217 218//======================================== 219// Runtime code shared with compiler 220//======================================== 221 222var Runtime = { 223 stackSave: function () { 224 return STACKTOP; 225 }, 226 stackRestore: function (stackTop) { 227 STACKTOP = stackTop; 228 }, 229 forceAlign: function (target, quantum) { 230 quantum = quantum || 4; 231 if (quantum == 1) return target; 232 if (isNumber(target) && isNumber(quantum)) { 233 return Math.ceil(target/quantum)*quantum; 234 } else if (isNumber(quantum) && isPowerOfTwo(quantum)) { 235 return '(((' +target + ')+' + (quantum-1) + ')&' + -quantum + ')'; 236 } 237 return 'Math.ceil((' + target + ')/' + quantum + ')*' + quantum; 238 }, 239 isNumberType: function (type) { 240 return type in Runtime.INT_TYPES || type in Runtime.FLOAT_TYPES; 241 }, 242 isPointerType: function isPointerType(type) { 243 return type[type.length-1] == '*'; 244}, 245 isStructType: function isStructType(type) { 246 if (isPointerType(type)) return false; 247 if (isArrayType(type)) return true; 248 if (/<?\{ ?[^}]* ?\}>?/.test(type)) return true; // { i32, i8 } etc. - anonymous struct types 249 // See comment in isStructPointerType() 250 return type[0] == '%'; 251}, 252 INT_TYPES: {"i1":0,"i8":0,"i16":0,"i32":0,"i64":0}, 253 FLOAT_TYPES: {"float":0,"double":0}, 254 or64: function (x, y) { 255 var l = (x | 0) | (y | 0); 256 var h = (Math.round(x / 4294967296) | Math.round(y / 4294967296)) * 4294967296; 257 return l + h; 258 }, 259 and64: function (x, y) { 260 var l = (x | 0) & (y | 0); 261 var h = (Math.round(x / 4294967296) & Math.round(y / 4294967296)) * 4294967296; 262 return l + h; 263 }, 264 xor64: function (x, y) { 265 var l = (x | 0) ^ (y | 0); 266 var h = (Math.round(x / 4294967296) ^ Math.round(y / 4294967296)) * 4294967296; 267 return l + h; 268 }, 269 getNativeTypeSize: function (type) { 270 switch (type) { 271 case 'i1': case 'i8': return 1; 272 case 'i16': return 2; 273 case 'i32': return 4; 274 case 'i64': return 8; 275 case 'float': return 4; 276 case 'double': return 8; 277 default: { 278 if (type[type.length-1] === '*') { 279 return Runtime.QUANTUM_SIZE; // A pointer 280 } else if (type[0] === 'i') { 281 var bits = parseInt(type.substr(1)); 282 assert(bits % 8 === 0); 283 return bits/8; 284 } else { 285 return 0; 286 } 287 } 288 } 289 }, 290 getNativeFieldSize: function (type) { 291 return Math.max(Runtime.getNativeTypeSize(type), Runtime.QUANTUM_SIZE); 292 }, 293 dedup: function dedup(items, ident) { 294 var seen = {}; 295 if (ident) { 296 return items.filter(function(item) { 297 if (seen[item[ident]]) return false; 298 seen[item[ident]] = true; 299 return true; 300 }); 301 } else { 302 return items.filter(function(item) { 303 if (seen[item]) return false; 304 seen[item] = true; 305 return true; 306 }); 307 } 308}, 309 set: function set() { 310 var args = typeof arguments[0] === 'object' ? arguments[0] : arguments; 311 var ret = {}; 312 for (var i = 0; i < args.length; i++) { 313 ret[args[i]] = 0; 314 } 315 return ret; 316}, 317 STACK_ALIGN: 8, 318 getAlignSize: function (type, size, vararg) { 319 // we align i64s and doubles on 64-bit boundaries, unlike x86 320 if (!vararg && (type == 'i64' || type == 'double')) return 8; 321 if (!type) return Math.min(size, 8); // align structures internally to 64 bits 322 return Math.min(size || (type ? Runtime.getNativeFieldSize(type) : 0), Runtime.QUANTUM_SIZE); 323 }, 324 calculateStructAlignment: function calculateStructAlignment(type) { 325 type.flatSize = 0; 326 type.alignSize = 0; 327 var diffs = []; 328 var prev = -1; 329 var index = 0; 330 type.flatIndexes = type.fields.map(function(field) { 331 index++; 332 var size, alignSize; 333 if (Runtime.isNumberType(field) || Runtime.isPointerType(field)) { 334 size = Runtime.getNativeTypeSize(field); // pack char; char; in structs, also char[X]s. 335 alignSize = Runtime.getAlignSize(field, size); 336 } else if (Runtime.isStructType(field)) { 337 if (field[1] === '0') { 338 // this is [0 x something]. When inside another structure like here, it must be at the end, 339 // and it adds no size 340 // XXX this happens in java-nbody for example... assert(index === type.fields.length, 'zero-length in the middle!'); 341 size = 0; 342 if (Types.types[field]) { 343 alignSize = Runtime.getAlignSize(null, Types.types[field].alignSize); 344 } else { 345 alignSize = type.alignSize || QUANTUM_SIZE; 346 } 347 } else { 348 size = Types.types[field].flatSize; 349 alignSize = Runtime.getAlignSize(null, Types.types[field].alignSize); 350 } 351 } else if (field[0] == 'b') { 352 // bN, large number field, like a [N x i8] 353 size = field.substr(1)|0; 354 alignSize = 1; 355 } else if (field[0] === '<') { 356 // vector type 357 size = alignSize = Types.types[field].flatSize; // fully aligned 358 } else if (field[0] === 'i') { 359 // illegal integer field, that could not be legalized because it is an internal structure field 360 // it is ok to have such fields, if we just use them as markers of field size and nothing more complex 361 size = alignSize = parseInt(field.substr(1))/8; 362 assert(size % 1 === 0, 'cannot handle non-byte-size field ' + field); 363 } else { 364 assert(false, 'invalid type for calculateStructAlignment'); 365 } 366 if (type.packed) alignSize = 1; 367 type.alignSize = Math.max(type.alignSize, alignSize); 368 var curr = Runtime.alignMemory(type.flatSize, alignSize); // if necessary, place this on aligned memory 369 type.flatSize = curr + size; 370 if (prev >= 0) { 371 diffs.push(curr-prev); 372 } 373 prev = curr; 374 return curr; 375 }); 376 if (type.name_ && type.name_[0] === '[') { 377 // arrays have 2 elements, so we get the proper difference. then we scale here. that way we avoid 378 // allocating a potentially huge array for [999999 x i8] etc. 379 type.flatSize = parseInt(type.name_.substr(1))*type.flatSize/2; 380 } 381 type.flatSize = Runtime.alignMemory(type.flatSize, type.alignSize); 382 if (diffs.length == 0) { 383 type.flatFactor = type.flatSize; 384 } else if (Runtime.dedup(diffs).length == 1) { 385 type.flatFactor = diffs[0]; 386 } 387 type.needsFlattening = (type.flatFactor != 1); 388 return type.flatIndexes; 389 }, 390 generateStructInfo: function (struct, typeName, offset) { 391 var type, alignment; 392 if (typeName) { 393 offset = offset || 0; 394 type = (typeof Types === 'undefined' ? Runtime.typeInfo : Types.types)[typeName]; 395 if (!type) return null; 396 if (type.fields.length != struct.length) { 397 printErr('Number of named fields must match the type for ' + typeName + ': possibly duplicate struct names. Cannot return structInfo'); 398 return null; 399 } 400 alignment = type.flatIndexes; 401 } else { 402 var type = { fields: struct.map(function(item) { return item[0] }) }; 403 alignment = Runtime.calculateStructAlignment(type); 404 } 405 var ret = { 406 __size__: type.flatSize 407 }; 408 if (typeName) { 409 struct.forEach(function(item, i) { 410 if (typeof item === 'string') { 411 ret[item] = alignment[i] + offset; 412 } else { 413 // embedded struct 414 var key; 415 for (var k in item) key = k; 416 ret[key] = Runtime.generateStructInfo(item[key], type.fields[i], alignment[i]); 417 } 418 }); 419 } else { 420 struct.forEach(function(item, i) { 421 ret[item[1]] = alignment[i]; 422 }); 423 } 424 return ret; 425 }, 426 dynCall: function (sig, ptr, args) { 427 if (args && args.length) { 428 if (!args.splice) args = Array.prototype.slice.call(args); 429 args.splice(0, 0, ptr); 430 return Module['dynCall_' + sig].apply(null, args); 431 } else { 432 return Module['dynCall_' + sig].call(null, ptr); 433 } 434 }, 435 functionPointers: [], 436 addFunction: function (func) { 437 for (var i = 0; i < Runtime.functionPointers.length; i++) { 438 if (!Runtime.functionPointers[i]) { 439 Runtime.functionPointers[i] = func; 440 return 2*(1 + i); 441 } 442 } 443 throw 'Finished up all reserved function pointers. Use a higher value for RESERVED_FUNCTION_POINTERS.'; 444 }, 445 removeFunction: function (index) { 446 Runtime.functionPointers[(index-2)/2] = null; 447 }, 448 getAsmConst: function (code, numArgs) { 449 // code is a constant string on the heap, so we can cache these 450 if (!Runtime.asmConstCache) Runtime.asmConstCache = {}; 451 var func = Runtime.asmConstCache[code]; 452 if (func) return func; 453 var args = []; 454 for (var i = 0; i < numArgs; i++) { 455 args.push(String.fromCharCode(36) + i); // $0, $1 etc 456 } 457 var source = Pointer_stringify(code); 458 if (source[0] === '"') { 459 // tolerate EM_ASM("..code..") even though EM_ASM(..code..) is correct 460 if (source.indexOf('"', 1) === source.length-1) { 461 source = source.substr(1, source.length-2); 462 } else { 463 // something invalid happened, e.g. EM_ASM("..code($0)..", input) 464 abort('invalid EM_ASM input |' + source + '|. Please use EM_ASM(..code..) (no quotes) or EM_ASM({ ..code($0).. }, input) (to input values)'); 465 } 466 } 467 try { 468 var evalled = eval('(function(' + args.join(',') + '){ ' + source + ' })'); // new Function does not allow upvars in node 469 } catch(e) { 470 Module.printErr('error in executing inline EM_ASM code: ' + e + ' on: \n\n' + source + '\n\nwith args |' + args + '| (make sure to use the right one out of EM_ASM, EM_ASM_ARGS, etc.)'); 471 throw e; 472 } 473 return Runtime.asmConstCache[code] = evalled; 474 }, 475 warnOnce: function (text) { 476 if (!Runtime.warnOnce.shown) Runtime.warnOnce.shown = {}; 477 if (!Runtime.warnOnce.shown[text]) { 478 Runtime.warnOnce.shown[text] = 1; 479 Module.printErr(text); 480 } 481 }, 482 funcWrappers: {}, 483 getFuncWrapper: function (func, sig) { 484 assert(sig); 485 if (!Runtime.funcWrappers[func]) { 486 Runtime.funcWrappers[func] = function dynCall_wrapper() { 487 return Runtime.dynCall(sig, func, arguments); 488 }; 489 } 490 return Runtime.funcWrappers[func]; 491 }, 492 UTF8Processor: function () { 493 var buffer = []; 494 var needed = 0; 495 this.processCChar = function (code) { 496 code = code & 0xFF; 497 498 if (buffer.length == 0) { 499 if ((code & 0x80) == 0x00) { // 0xxxxxxx 500 return String.fromCharCode(code); 501 } 502 buffer.push(code); 503 if ((code & 0xE0) == 0xC0) { // 110xxxxx 504 needed = 1; 505 } else if ((code & 0xF0) == 0xE0) { // 1110xxxx 506 needed = 2; 507 } else { // 11110xxx 508 needed = 3; 509 } 510 return ''; 511 } 512 513 if (needed) { 514 buffer.push(code); 515 needed--; 516 if (needed > 0) return ''; 517 } 518 519 var c1 = buffer[0]; 520 var c2 = buffer[1]; 521 var c3 = buffer[2]; 522 var c4 = buffer[3]; 523 var ret; 524 if (buffer.length == 2) { 525 ret = String.fromCharCode(((c1 & 0x1F) << 6) | (c2 & 0x3F)); 526 } else if (buffer.length == 3) { 527 ret = String.fromCharCode(((c1 & 0x0F) << 12) | ((c2 & 0x3F) << 6) | (c3 & 0x3F)); 528 } else { 529 // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae 530 var codePoint = ((c1 & 0x07) << 18) | ((c2 & 0x3F) << 12) | 531 ((c3 & 0x3F) << 6) | (c4 & 0x3F); 532 ret = String.fromCharCode( 533 Math.floor((codePoint - 0x10000) / 0x400) + 0xD800, 534 (codePoint - 0x10000) % 0x400 + 0xDC00); 535 } 536 buffer.length = 0; 537 return ret; 538 } 539 this.processJSString = function processJSString(string) { 540 /* TODO: use TextEncoder when present, 541 var encoder = new TextEncoder(); 542 encoder['encoding'] = "utf-8"; 543 var utf8Array = encoder['encode'](aMsg.data); 544 */ 545 string = unescape(encodeURIComponent(string)); 546 var ret = []; 547 for (var i = 0; i < string.length; i++) { 548 ret.push(string.charCodeAt(i)); 549 } 550 return ret; 551 } 552 }, 553 getCompilerSetting: function (name) { 554 throw 'You must build with -s RETAIN_COMPILER_SETTINGS=1 for Runtime.getCompilerSetting or emscripten_get_compiler_setting to work'; 555 }, 556 stackAlloc: function (size) { var ret = STACKTOP;STACKTOP = (STACKTOP + size)|0;STACKTOP = (((STACKTOP)+7)&-8); return ret; }, 557 staticAlloc: function (size) { var ret = STATICTOP;STATICTOP = (STATICTOP + size)|0;STATICTOP = (((STATICTOP)+7)&-8); return ret; }, 558 dynamicAlloc: function (size) { var ret = DYNAMICTOP;DYNAMICTOP = (DYNAMICTOP + size)|0;DYNAMICTOP = (((DYNAMICTOP)+7)&-8); if (DYNAMICTOP >= TOTAL_MEMORY) enlargeMemory();; return ret; }, 559 alignMemory: function (size,quantum) { var ret = size = Math.ceil((size)/(quantum ? quantum : 8))*(quantum ? quantum : 8); return ret; }, 560 makeBigInt: function (low,high,unsigned) { var ret = (unsigned ? ((+((low>>>0)))+((+((high>>>0)))*(+4294967296))) : ((+((low>>>0)))+((+((high|0)))*(+4294967296)))); return ret; }, 561 GLOBAL_BASE: 8, 562 QUANTUM_SIZE: 4, 563 __dummy__: 0 564} 565 566 567Module['Runtime'] = Runtime; 568 569 570 571 572 573 574 575 576 577//======================================== 578// Runtime essentials 579//======================================== 580 581var __THREW__ = 0; // Used in checking for thrown exceptions. 582 583var ABORT = false; // whether we are quitting the application. no code should run after this. set in exit() and abort() 584var EXITSTATUS = 0; 585 586var undef = 0; 587// tempInt is used for 32-bit signed values or smaller. tempBigInt is used 588// for 32-bit unsigned values or more than 32 bits. TODO: audit all uses of tempInt 589var tempValue, tempInt, tempBigInt, tempInt2, tempBigInt2, tempPair, tempBigIntI, tempBigIntR, tempBigIntS, tempBigIntP, tempBigIntD, tempDouble, tempFloat; 590var tempI64, tempI64b; 591var tempRet0, tempRet1, tempRet2, tempRet3, tempRet4, tempRet5, tempRet6, tempRet7, tempRet8, tempRet9; 592 593function assert(condition, text) { 594 if (!condition) { 595 abort('Assertion failed: ' + text); 596 } 597} 598 599var globalScope = this; 600 601// C calling interface. A convenient way to call C functions (in C files, or 602// defined with extern "C"). 603// 604// Note: LLVM optimizations can inline and remove functions, after which you will not be 605// able to call them. Closure can also do so. To avoid that, add your function to 606// the exports using something like 607// 608// -s EXPORTED_FUNCTIONS='["_main", "_myfunc"]' 609// 610// @param ident The name of the C function (note that C++ functions will be name-mangled - use extern "C") 611// @param returnType The return type of the function, one of the JS types 'number', 'string' or 'array' (use 'number' for any C pointer, and 612// 'array' for JavaScript arrays and typed arrays; note that arrays are 8-bit). 613// @param argTypes An array of the types of arguments for the function (if there are no arguments, this can be ommitted). Types are as in returnType, 614// except that 'array' is not possible (there is no way for us to know the length of the array) 615// @param args An array of the arguments to the function, as native JS values (as in returnType) 616// Note that string arguments will be stored on the stack (the JS string will become a C string on the stack). 617// @return The return value, as a native JS value (as in returnType) 618function ccall(ident, returnType, argTypes, args) { 619 return ccallFunc(getCFunc(ident), returnType, argTypes, args); 620} 621Module["ccall"] = ccall; 622 623// Returns the C function with a specified identifier (for C++, you need to do manual name mangling) 624function getCFunc(ident) { 625 try { 626 var func = Module['_' + ident]; // closure exported function 627 if (!func) func = eval('_' + ident); // explicit lookup 628 } catch(e) { 629 } 630 assert(func, 'Cannot call unknown function ' + ident + ' (perhaps LLVM optimizations or closure removed it?)'); 631 return func; 632} 633 634// Internal function that does a C call using a function, not an identifier 635function ccallFunc(func, returnType, argTypes, args) { 636 var stack = 0; 637 function toC(value, type) { 638 if (type == 'string') { 639 if (value === null || value === undefined || value === 0) return 0; // null string 640 value = intArrayFromString(value); 641 type = 'array'; 642 } 643 if (type == 'array') { 644 if (!stack) stack = Runtime.stackSave(); 645 var ret = Runtime.stackAlloc(value.length); 646 writeArrayToMemory(value, ret); 647 return ret; 648 } 649 return value; 650 } 651 function fromC(value, type) { 652 if (type == 'string') { 653 return Pointer_stringify(value); 654 } 655 assert(type != 'array'); 656 return value; 657 } 658 var i = 0; 659 var cArgs = args ? args.map(function(arg) { 660 return toC(arg, argTypes[i++]); 661 }) : []; 662 var ret = fromC(func.apply(null, cArgs), returnType); 663 if (stack) Runtime.stackRestore(stack); 664 return ret; 665} 666 667// Returns a native JS wrapper for a C function. This is similar to ccall, but 668// returns a function you can call repeatedly in a normal way. For example: 669// 670// var my_function = cwrap('my_c_function', 'number', ['number', 'number']); 671// alert(my_function(5, 22)); 672// alert(my_function(99, 12)); 673// 674function cwrap(ident, returnType, argTypes) { 675 var func = getCFunc(ident); 676 return function() { 677 return ccallFunc(func, returnType, argTypes, Array.prototype.slice.call(arguments)); 678 } 679} 680Module["cwrap"] = cwrap; 681 682// Sets a value in memory in a dynamic way at run-time. Uses the 683// type data. This is the same as makeSetValue, except that 684// makeSetValue is done at compile-time and generates the needed 685// code then, whereas this function picks the right code at 686// run-time. 687// Note that setValue and getValue only do *aligned* writes and reads! 688// Note that ccall uses JS types as for defining types, while setValue and 689// getValue need LLVM types ('i8', 'i32') - this is a lower-level operation 690function setValue(ptr, value, type, noSafe) { 691 type = type || 'i8'; 692 if (type.charAt(type.length-1) === '*') type = 'i32'; // pointers are 32-bit 693 switch(type) { 694 case 'i1': HEAP8[(ptr)]=value; break; 695 case 'i8': HEAP8[(ptr)]=value; break; 696 case 'i16': HEAP16[((ptr)>>1)]=value; break; 697 case 'i32': HEAP32[((ptr)>>2)]=value; break; 698 case 'i64': (tempI64 = [value>>>0,(tempDouble=value,(+(Math_abs(tempDouble))) >= (+1) ? (tempDouble > (+0) ? ((Math_min((+(Math_floor((tempDouble)/(+4294967296)))), (+4294967295)))|0)>>>0 : (~~((+(Math_ceil((tempDouble - +(((~~(tempDouble)))>>>0))/(+4294967296))))))>>>0) : 0)],HEAP32[((ptr)>>2)]=tempI64[0],HEAP32[(((ptr)+(4))>>2)]=tempI64[1]); break; 699 case 'float': HEAPF32[((ptr)>>2)]=value; break; 700 case 'double': HEAPF64[((ptr)>>3)]=value; break; 701 default: abort('invalid type for setValue: ' + type); 702 } 703} 704Module['setValue'] = setValue; 705 706// Parallel to setValue. 707function getValue(ptr, type, noSafe) { 708 type = type || 'i8'; 709 if (type.charAt(type.length-1) === '*') type = 'i32'; // pointers are 32-bit 710 switch(type) { 711 case 'i1': return HEAP8[(ptr)]; 712 case 'i8': return HEAP8[(ptr)]; 713 case 'i16': return HEAP16[((ptr)>>1)]; 714 case 'i32': return HEAP32[((ptr)>>2)]; 715 case 'i64': return HEAP32[((ptr)>>2)]; 716 case 'float': return HEAPF32[((ptr)>>2)]; 717 case 'double': return HEAPF64[((ptr)>>3)]; 718 default: abort('invalid type for setValue: ' + type); 719 } 720 return null; 721} 722Module['getValue'] = getValue; 723 724var ALLOC_NORMAL = 0; // Tries to use _malloc() 725var ALLOC_STACK = 1; // Lives for the duration of the current function call 726var ALLOC_STATIC = 2; // Cannot be freed 727var ALLOC_DYNAMIC = 3; // Cannot be freed except through sbrk 728var ALLOC_NONE = 4; // Do not allocate 729Module['ALLOC_NORMAL'] = ALLOC_NORMAL; 730Module['ALLOC_STACK'] = ALLOC_STACK; 731Module['ALLOC_STATIC'] = ALLOC_STATIC; 732Module['ALLOC_DYNAMIC'] = ALLOC_DYNAMIC; 733Module['ALLOC_NONE'] = ALLOC_NONE; 734 735// allocate(): This is for internal use. You can use it yourself as well, but the interface 736// is a little tricky (see docs right below). The reason is that it is optimized 737// for multiple syntaxes to save space in generated code. So you should 738// normally not use allocate(), and instead allocate memory using _malloc(), 739// initialize it with setValue(), and so forth. 740// @slab: An array of data, or a number. If a number, then the size of the block to allocate, 741// in *bytes* (note that this is sometimes confusing: the next parameter does not 742// affect this!) 743// @types: Either an array of types, one for each byte (or 0 if no type at that position), 744// or a single type which is used for the entire block. This only matters if there 745// is initial data - if @slab is a number, then this does not matter at all and is 746// ignored. 747// @allocator: How to allocate memory, see ALLOC_* 748function allocate(slab, types, allocator, ptr) { 749 var zeroinit, size; 750 if (typeof slab === 'number') { 751 zeroinit = true; 752 size = slab; 753 } else { 754 zeroinit = false; 755 size = slab.length; 756 } 757 758 var singleType = typeof types === 'string' ? types : null; 759 760 var ret; 761 if (allocator == ALLOC_NONE) { 762 ret = ptr; 763 } else { 764 ret = [_malloc, Runtime.stackAlloc, Runtime.staticAlloc, Runtime.dynamicAlloc][allocator === undefined ? ALLOC_STATIC : allocator](Math.max(size, singleType ? 1 : types.length)); 765 } 766 767 if (zeroinit) { 768 var ptr = ret, stop; 769 assert((ret & 3) == 0); 770 stop = ret + (size & ~3); 771 for (; ptr < stop; ptr += 4) { 772 HEAP32[((ptr)>>2)]=0; 773 } 774 stop = ret + size; 775 while (ptr < stop) { 776 HEAP8[((ptr++)|0)]=0; 777 } 778 return ret; 779 } 780 781 if (singleType === 'i8') { 782 if (slab.subarray || slab.slice) { 783 HEAPU8.set(slab, ret); 784 } else { 785 HEAPU8.set(new Uint8Array(slab), ret); 786 } 787 return ret; 788 } 789 790 var i = 0, type, typeSize, previousType; 791 while (i < size) { 792 var curr = slab[i]; 793 794 if (typeof curr === 'function') { 795 curr = Runtime.getFunctionIndex(curr); 796 } 797 798 type = singleType || types[i]; 799 if (type === 0) { 800 i++; 801 continue; 802 } 803 804 if (type == 'i64') type = 'i32'; // special case: we have one i32 here, and one i32 later 805 806 setValue(ret+i, curr, type); 807 808 // no need to look up size unless type changes, so cache it 809 if (previousType !== type) { 810 typeSize = Runtime.getNativeTypeSize(type); 811 previousType = type; 812 } 813 i += typeSize; 814 } 815 816 return ret; 817} 818Module['allocate'] = allocate; 819 820function Pointer_stringify(ptr, /* optional */ length) { 821 // TODO: use TextDecoder 822 // Find the length, and check for UTF while doing so 823 var hasUtf = false; 824 var t; 825 var i = 0; 826 while (1) { 827 t = HEAPU8[(((ptr)+(i))|0)]; 828 if (t >= 128) hasUtf = true; 829 else if (t == 0 && !length) break; 830 i++; 831 if (length && i == length) break; 832 } 833 if (!length) length = i; 834 835 var ret = ''; 836 837 if (!hasUtf) { 838 var MAX_CHUNK = 1024; // split up into chunks, because .apply on a huge string can overflow the stack 839 var curr; 840 while (length > 0) { 841 curr = String.fromCharCode.apply(String, HEAPU8.subarray(ptr, ptr + Math.min(length, MAX_CHUNK))); 842 ret = ret ? ret + curr : curr; 843 ptr += MAX_CHUNK; 844 length -= MAX_CHUNK; 845 } 846 return ret; 847 } 848 849 var utf8 = new Runtime.UTF8Processor(); 850 for (i = 0; i < length; i++) { 851 t = HEAPU8[(((ptr)+(i))|0)]; 852 ret += utf8.processCChar(t); 853 } 854 return ret; 855} 856Module['Pointer_stringify'] = Pointer_stringify; 857 858// Given a pointer 'ptr' to a null-terminated UTF16LE-encoded string in the emscripten HEAP, returns 859// a copy of that string as a Javascript String object. 860function UTF16ToString(ptr) { 861 var i = 0; 862 863 var str = ''; 864 while (1) { 865 var codeUnit = HEAP16[(((ptr)+(i*2))>>1)]; 866 if (codeUnit == 0) 867 return str; 868 ++i; 869 // fromCharCode constructs a character from a UTF-16 code unit, so we can pass the UTF16 string right through. 870 str += String.fromCharCode(codeUnit); 871 } 872} 873Module['UTF16ToString'] = UTF16ToString; 874 875// Copies the given Javascript String object 'str' to the emscripten HEAP at address 'outPtr', 876// null-terminated and encoded in UTF16LE form. The copy will require at most (str.length*2+1)*2 bytes of space in the HEAP. 877function stringToUTF16(str, outPtr) { 878 for(var i = 0; i < str.length; ++i) { 879 // charCodeAt returns a UTF-16 encoded code unit, so it can be directly written to the HEAP. 880 var codeUnit = str.charCodeAt(i); // possibly a lead surrogate 881 HEAP16[(((outPtr)+(i*2))>>1)]=codeUnit; 882 } 883 // Null-terminate the pointer to the HEAP. 884 HEAP16[(((outPtr)+(str.length*2))>>1)]=0; 885} 886Module['stringToUTF16'] = stringToUTF16; 887 888// Given a pointer 'ptr' to a null-terminated UTF32LE-encoded string in the emscripten HEAP, returns 889// a copy of that string as a Javascript String object. 890function UTF32ToString(ptr) { 891 var i = 0; 892 893 var str = ''; 894 while (1) { 895 var utf32 = HEAP32[(((ptr)+(i*4))>>2)]; 896 if (utf32 == 0) 897 return str; 898 ++i; 899 // Gotcha: fromCharCode constructs a character from a UTF-16 encoded code (pair), not from a Unicode code point! So encode the code point to UTF-16 for constructing. 900 if (utf32 >= 0x10000) { 901 var ch = utf32 - 0x10000; 902 str += String.fromCharCode(0xD800 | (ch >> 10), 0xDC00 | (ch & 0x3FF)); 903 } else { 904 str += String.fromCharCode(utf32); 905 } 906 } 907} 908Module['UTF32ToString'] = UTF32ToString; 909 910// Copies the given Javascript String object 'str' to the emscripten HEAP at address 'outPtr', 911// null-terminated and encoded in UTF32LE form. The copy will require at most (str.length+1)*4 bytes of space in the HEAP, 912// but can use less, since str.length does not return the number of characters in the string, but the number of UTF-16 code units in the string. 913function stringToUTF32(str, outPtr) { 914 var iChar = 0; 915 for(var iCodeUnit = 0; iCodeUnit < str.length; ++iCodeUnit) { 916 // Gotcha: charCodeAt returns a 16-bit word that is a UTF-16 encoded code unit, not a Unicode code point of the character! We must decode the string to UTF-32 to the heap. 917 var codeUnit = str.charCodeAt(iCodeUnit); // possibly a lead surrogate 918 if (codeUnit >= 0xD800 && codeUnit <= 0xDFFF) { 919 var trailSurrogate = str.charCodeAt(++iCodeUnit); 920 codeUnit = 0x10000 + ((codeUnit & 0x3FF) << 10) | (trailSurrogate & 0x3FF); 921 } 922 HEAP32[(((outPtr)+(iChar*4))>>2)]=codeUnit; 923 ++iChar; 924 } 925 // Null-terminate the pointer to the HEAP. 926 HEAP32[(((outPtr)+(iChar*4))>>2)]=0; 927} 928Module['stringToUTF32'] = stringToUTF32; 929 930function demangle(func) { 931 var i = 3; 932 // params, etc. 933 var basicTypes = { 934 'v': 'void', 935 'b': 'bool', 936 'c': 'char', 937 's': 'short', 938 'i': 'int', 939 'l': 'long', 940 'f': 'float', 941 'd': 'double', 942 'w': 'wchar_t', 943 'a': 'signed char', 944 'h': 'unsigned char', 945 't': 'unsigned short', 946 'j': 'unsigned int', 947 'm': 'unsigned long', 948 'x': 'long long', 949 'y': 'unsigned long long', 950 'z': '...' 951 }; 952 var subs = []; 953 var first = true; 954 function dump(x) { 955 //return; 956 if (x) Module.print(x); 957 Module.print(func); 958 var pre = ''; 959 for (var a = 0; a < i; a++) pre += ' '; 960 Module.print (pre + '^'); 961 } 962 function parseNested() { 963 i++; 964 if (func[i] === 'K') i++; // ignore const 965 var parts = []; 966 while (func[i] !== 'E') { 967 if (func[i] === 'S') { // substitution 968 i++; 969 var next = func.indexOf('_', i); 970 var num = func.substring(i, next) || 0; 971 parts.push(subs[num] || '?'); 972 i = next+1; 973 continue; 974 } 975 if (func[i] === 'C') { // constructor 976 parts.push(parts[parts.length-1]); 977 i += 2; 978 continue; 979 } 980 var size = parseInt(func.substr(i)); 981 var pre = size.toString().length; 982 if (!size || !pre) { i--; break; } // counter i++ below us 983 var curr = func.substr(i + pre, size); 984 parts.push(curr); 985 subs.push(curr); 986 i += pre + size; 987 } 988 i++; // skip E 989 return parts; 990 } 991 function parse(rawList, limit, allowVoid) { // main parser 992 limit = limit || Infinity; 993 var ret = '', list = []; 994 function flushList() { 995 return '(' + list.join(', ') + ')'; 996 } 997 var name; 998 if (func[i] === 'N') { 999 // namespaced N-E 1000 name = parseNested().join('::'); 1001 limit--; 1002 if (limit === 0) return rawList ? [name] : name; 1003 } else { 1004 // not namespaced 1005 if (func[i] === 'K' || (first && func[i] === 'L')) i++; // ignore const and first 'L' 1006 var size = parseInt(func.substr(i)); 1007 if (size) { 1008 var pre = size.toString().length; 1009 name = func.substr(i + pre, size); 1010 i += pre + size; 1011 } 1012 } 1013 first = false; 1014 if (func[i] === 'I') { 1015 i++; 1016 var iList = parse(true); 1017 var iRet = parse(true, 1, true); 1018 ret += iRet[0] + ' ' + name + '<' + iList.join(', ') + '>'; 1019 } else { 1020 ret = name; 1021 } 1022 paramLoop: while (i < func.length && limit-- > 0) { 1023 //dump('paramLoop'); 1024 var c = func[i++]; 1025 if (c in basicTypes) { 1026 list.push(basicTypes[c]); 1027 } else { 1028 switch (c) { 1029 case 'P': list.push(parse(true, 1, true)[0] + '*'); break; // pointer 1030 case 'R': list.push(parse(true, 1, true)[0] + '&'); break; // reference 1031 case 'L': { // literal 1032 i++; // skip basic type 1033 var end = func.indexOf('E', i); 1034 var size = end - i; 1035 list.push(func.substr(i, size)); 1036 i += size + 2; // size + 'EE' 1037 break; 1038 } 1039 case 'A': { // array 1040 var size = parseInt(func.substr(i)); 1041 i += size.toString().length; 1042 if (func[i] !== '_') throw '?'; 1043 i++; // skip _ 1044 list.push(parse(true, 1, true)[0] + ' [' + size + ']'); 1045 break; 1046 } 1047 case 'E': break paramLoop; 1048 default: ret += '?' + c; break paramLoop; 1049 } 1050 } 1051 } 1052 if (!allowVoid && list.length === 1 && list[0] === 'void') list = []; // avoid (void) 1053 if (rawList) { 1054 if (ret) { 1055 list.push(ret + '?'); 1056 } 1057 return list; 1058 } else { 1059 return ret + flushList(); 1060 } 1061 } 1062 try { 1063 // Special-case the entry point, since its name differs from other name mangling. 1064 if (func == 'Object._main' || func == '_main') { 1065 return 'main()'; 1066 } 1067 if (typeof func === 'number') func = Pointer_stringify(func); 1068 if (func[0] !== '_') return func; 1069 if (func[1] !== '_') return func; // C function 1070 if (func[2] !== 'Z') return func; 1071 switch (func[3]) { 1072 case 'n': return 'operator new()'; 1073 case 'd': return 'operator delete()'; 1074 } 1075 return parse(); 1076 } catch(e) { 1077 return func; 1078 } 1079} 1080 1081function demangleAll(text) { 1082 return text.replace(/__Z[\w\d_]+/g, function(x) { var y = demangle(x); return x === y ? x : (x + ' [' + y + ']') }); 1083} 1084 1085function stackTrace() { 1086 var stack = new Error().stack; 1087 return stack ? demangleAll(stack) : '(no stack trace available)'; // Stack trace is not available at least on IE10 and Safari 6. 1088} 1089 1090// Memory management 1091 1092var PAGE_SIZE = 4096; 1093function alignMemoryPage(x) { 1094 return (x+4095)&-4096; 1095} 1096 1097var HEAP; 1098var HEAP8, HEAPU8, HEAP16, HEAPU16, HEAP32, HEAPU32, HEAPF32, HEAPF64; 1099 1100var STATIC_BASE = 0, STATICTOP = 0, staticSealed = false; // static area 1101var STACK_BASE = 0, STACKTOP = 0, STACK_MAX = 0; // stack area 1102var DYNAMIC_BASE = 0, DYNAMICTOP = 0; // dynamic area handled by sbrk 1103 1104function enlargeMemory() { 1105 abort('Cannot enlarge memory arrays. Either (1) compile with -s TOTAL_MEMORY=X with X higher than the current value ' + TOTAL_MEMORY + ', (2) compile with ALLOW_MEMORY_GROWTH which adjusts the size at runtime but prevents some optimizations, or (3) set Module.TOTAL_MEMORY before the program runs.'); 1106} 1107 1108var TOTAL_STACK = Module['TOTAL_STACK'] || 5242880; 1109var TOTAL_MEMORY = Module['TOTAL_MEMORY'] || 134217728; 1110var FAST_MEMORY = Module['FAST_MEMORY'] || 2097152; 1111 1112var totalMemory = 4096; 1113while (totalMemory < TOTAL_MEMORY || totalMemory < 2*TOTAL_STACK) { 1114 if (totalMemory < 16*1024*1024) { 1115 totalMemory *= 2; 1116 } else { 1117 totalMemory += 16*1024*1024 1118 } 1119} 1120if (totalMemory !== TOTAL_MEMORY) { 1121 Module.printErr('increasing TOTAL_MEMORY to ' + totalMemory + ' to be more reasonable'); 1122 TOTAL_MEMORY = totalMemory; 1123} 1124 1125// Initialize the runtime's memory 1126// check for full engine support (use string 'subarray' to avoid closure compiler confusion) 1127assert(typeof Int32Array !== 'undefined' && typeof Float64Array !== 'undefined' && !!(new Int32Array(1)['subarray']) && !!(new Int32Array(1)['set']), 1128 'JS engine does not provide full typed array support'); 1129 1130var buffer = new ArrayBuffer(TOTAL_MEMORY); 1131HEAP8 = new Int8Array(buffer); 1132HEAP16 = new Int16Array(buffer); 1133HEAP32 = new Int32Array(buffer); 1134HEAPU8 = new Uint8Array(buffer); 1135HEAPU16 = new Uint16Array(buffer); 1136HEAPU32 = new Uint32Array(buffer); 1137HEAPF32 = new Float32Array(buffer); 1138HEAPF64 = new Float64Array(buffer); 1139 1140// Endianness check (note: assumes compiler arch was little-endian) 1141HEAP32[0] = 255; 1142assert(HEAPU8[0] === 255 && HEAPU8[3] === 0, 'Typed arrays 2 must be run on a little-endian system'); 1143 1144Module['HEAP'] = HEAP; 1145Module['HEAP8'] = HEAP8; 1146Module['HEAP16'] = HEAP16; 1147Module['HEAP32'] = HEAP32; 1148Module['HEAPU8'] = HEAPU8; 1149Module['HEAPU16'] = HEAPU16; 1150Module['HEAPU32'] = HEAPU32; 1151Module['HEAPF32'] = HEAPF32; 1152Module['HEAPF64'] = HEAPF64; 1153 1154function callRuntimeCallbacks(callbacks) { 1155 while(callbacks.length > 0) { 1156 var callback = callbacks.shift(); 1157 if (typeof callback == 'function') { 1158 callback(); 1159 continue; 1160 } 1161 var func = callback.func; 1162 if (typeof func === 'number') { 1163 if (callback.arg === undefined) { 1164 Runtime.dynCall('v', func); 1165 } else { 1166 Runtime.dynCall('vi', func, [callback.arg]); 1167 } 1168 } else { 1169 func(callback.arg === undefined ? null : callback.arg); 1170 } 1171 } 1172} 1173 1174var __ATPRERUN__ = []; // functions called before the runtime is initialized 1175var __ATINIT__ = []; // functions called during startup 1176var __ATMAIN__ = []; // functions called when main() is to be run 1177var __ATEXIT__ = []; // functions called during shutdown 1178var __ATPOSTRUN__ = []; // functions called after the runtime has exited 1179 1180var runtimeInitialized = false; 1181 1182function preRun() { 1183 // compatibility - merge in anything from Module['preRun'] at this time 1184 if (Module['preRun']) { 1185 if (typeof Module['preRun'] == 'function') Module['preRun'] = [Module['preRun']]; 1186 while (Module['preRun'].length) { 1187 addOnPreRun(Module['preRun'].shift()); 1188 } 1189 } 1190 callRuntimeCallbacks(__ATPRERUN__); 1191} 1192 1193function ensureInitRuntime() { 1194 if (runtimeInitialized) return; 1195 runtimeInitialized = true; 1196 callRuntimeCallbacks(__ATINIT__); 1197} 1198 1199function preMain() { 1200 callRuntimeCallbacks(__ATMAIN__); 1201} 1202 1203function exitRuntime() { 1204 callRuntimeCallbacks(__ATEXIT__); 1205} 1206 1207function postRun() { 1208 // compatibility - merge in anything from Module['postRun'] at this time 1209 if (Module['postRun']) { 1210 if (typeof Module['postRun'] == 'function') Module['postRun'] = [Module['postRun']]; 1211 while (Module['postRun'].length) { 1212 addOnPostRun(Module['postRun'].shift()); 1213 } 1214 } 1215 callRuntimeCallbacks(__ATPOSTRUN__); 1216} 1217 1218function addOnPreRun(cb) { 1219 __ATPRERUN__.unshift(cb); 1220} 1221Module['addOnPreRun'] = Module.addOnPreRun = addOnPreRun; 1222 1223function addOnInit(cb) { 1224 __ATINIT__.unshift(cb); 1225} 1226Module['addOnInit'] = Module.addOnInit = addOnInit; 1227 1228function addOnPreMain(cb) { 1229 __ATMAIN__.unshift(cb); 1230} 1231Module['addOnPreMain'] = Module.addOnPreMain = addOnPreMain; 1232 1233function addOnExit(cb) { 1234 __ATEXIT__.unshift(cb); 1235} 1236Module['addOnExit'] = Module.addOnExit = addOnExit; 1237 1238function addOnPostRun(cb) { 1239 __ATPOSTRUN__.unshift(cb); 1240} 1241Module['addOnPostRun'] = Module.addOnPostRun = addOnPostRun; 1242 1243// Tools 1244 1245// This processes a JS string into a C-line array of numbers, 0-terminated. 1246// For LLVM-originating strings, see parser.js:parseLLVMString function 1247function intArrayFromString(stringy, dontAddNull, length /* optional */) { 1248 var ret = (new Runtime.UTF8Processor()).processJSString(stringy); 1249 if (length) { 1250 ret.length = length; 1251 } 1252 if (!dontAddNull) { 1253 ret.push(0); 1254 } 1255 return ret; 1256} 1257Module['intArrayFromString'] = intArrayFromString; 1258 1259function intArrayToString(array) { 1260 var ret = []; 1261 for (var i = 0; i < array.length; i++) { 1262 var chr = array[i]; 1263 if (chr > 0xFF) { 1264 chr &= 0xFF; 1265 } 1266 ret.push(String.fromCharCode(chr)); 1267 } 1268 return ret.join(''); 1269} 1270Module['intArrayToString'] = intArrayToString; 1271 1272// Write a Javascript array to somewhere in the heap 1273function writeStringToMemory(string, buffer, dontAddNull) { 1274 var array = intArrayFromString(string, dontAddNull); 1275 var i = 0; 1276 while (i < array.length) { 1277 var chr = array[i]; 1278 HEAP8[(((buffer)+(i))|0)]=chr; 1279 i = i + 1; 1280 } 1281} 1282Module['writeStringToMemory'] = writeStringToMemory; 1283 1284function writeArrayToMemory(array, buffer) { 1285 for (var i = 0; i < array.length; i++) { 1286 HEAP8[(((buffer)+(i))|0)]=array[i]; 1287 } 1288} 1289Module['writeArrayToMemory'] = writeArrayToMemory; 1290 1291function writeAsciiToMemory(str, buffer, dontAddNull) { 1292 for (var i = 0; i < str.length; i++) { 1293 HEAP8[(((buffer)+(i))|0)]=str.charCodeAt(i); 1294 } 1295 if (!dontAddNull) HEAP8[(((buffer)+(str.length))|0)]=0; 1296} 1297Module['writeAsciiToMemory'] = writeAsciiToMemory; 1298 1299function unSign(value, bits, ignore) { 1300 if (value >= 0) { 1301 return value; 1302 } 1303 return bits <= 32 ? 2*Math.abs(1 << (bits-1)) + value // Need some trickery, since if bits == 32, we are right at the limit of the bits JS uses in bitshifts 1304 : Math.pow(2, bits) + value; 1305} 1306function reSign(value, bits, ignore) { 1307 if (value <= 0) { 1308 return value; 1309 } 1310 var half = bits <= 32 ? Math.abs(1 << (bits-1)) // abs is needed if bits == 32 1311 : Math.pow(2, bits-1); 1312 if (value >= half && (bits <= 32 || value > half)) { // for huge values, we can hit the precision limit and always get true here. so don't do that 1313 // but, in general there is no perfect solution here. With 64-bit ints, we get rounding and errors 1314 // TODO: In i64 mode 1, resign the two parts separately and safely 1315 value = -2*half + value; // Cannot bitshift half, as it may be at the limit of the bits JS uses in bitshifts 1316 } 1317 return value; 1318} 1319 1320// check for imul support, and also for correctness ( https://bugs.webkit.org/show_bug.cgi?id=126345 ) 1321if (!Math['imul'] || Math['imul'](0xffffffff, 5) !== -5) Math['imul'] = function imul(a, b) { 1322 var ah = a >>> 16; 1323 var al = a & 0xffff; 1324 var bh = b >>> 16; 1325 var bl = b & 0xffff; 1326 return (al*bl + ((ah*bl + al*bh) << 16))|0; 1327}; 1328Math.imul = Math['imul']; 1329 1330 1331var Math_abs = Math.abs; 1332var Math_cos = Math.cos; 1333var Math_sin = Math.sin; 1334var Math_tan = Math.tan; 1335var Math_acos = Math.acos; 1336var Math_asin = Math.asin; 1337var Math_atan = Math.atan; 1338var Math_atan2 = Math.atan2; 1339var Math_exp = Math.exp; 1340var Math_log = Math.log; 1341var Math_sqrt = Math.sqrt; 1342var Math_ceil = Math.ceil; 1343var Math_floor = Math.floor; 1344var Math_pow = Math.pow; 1345var Math_imul = Math.imul; 1346var Math_fround = Math.fround; 1347var Math_min = Math.min; 1348 1349// A counter of dependencies for calling run(). If we need to 1350// do asynchronous work before running, increment this and 1351// decrement it. Incrementing must happen in a place like 1352// PRE_RUN_ADDITIONS (used by emcc to add file preloading). 1353// Note that you can add dependencies in preRun, even though 1354// it happens right before run - run will be postponed until 1355// the dependencies are met. 1356var runDependencies = 0; 1357var runDependencyWatcher = null; 1358var dependenciesFulfilled = null; // overridden to take different actions when all run dependencies are fulfilled 1359 1360function addRunDependency(id) { 1361 runDependencies++; 1362 if (Module['monitorRunDependencies']) { 1363 Module['monitorRunDependencies'](runDependencies); 1364 } 1365} 1366Module['addRunDependency'] = addRunDependency; 1367function removeRunDependency(id) { 1368 runDependencies--; 1369 if (Module['monitorRunDependencies']) { 1370 Module['monitorRunDependencies'](runDependencies); 1371 } 1372 if (runDependencies == 0) { 1373 if (runDependencyWatcher !== null) { 1374 clearInterval(runDependencyWatcher); 1375 runDependencyWatcher = null; 1376 } 1377 if (dependenciesFulfilled) { 1378 var callback = dependenciesFulfilled; 1379 dependenciesFulfilled = null; 1380 callback(); // can add another dependenciesFulfilled 1381 } 1382 } 1383} 1384Module['removeRunDependency'] = removeRunDependency; 1385 1386Module["preloadedImages"] = {}; // maps url to image data 1387Module["preloadedAudios"] = {}; // maps url to audio data 1388 1389 1390var memoryInitializer = null; 1391 1392// === Body === 1393 1394 1395 1396 1397 1398STATIC_BASE = 8; 1399 1400STATICTOP = STATIC_BASE + Runtime.alignMemory(547); 1401/* global initializers */ __ATINIT__.push(); 1402 1403 1404/* memory initializer */ allocate([101,114,114,111,114,58,32,37,100,10,0,0,0,0,0,0,80,102,97,110,110,107,117,99,104,101,110,40,37,100,41,32,61,32,37,100,46,10,0,0,37,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], "i8", ALLOC_NONE, Runtime.GLOBAL_BASE); 1405 1406 1407 1408 1409var tempDoublePtr = Runtime.alignMemory(allocate(12, "i8", ALLOC_STATIC), 8); 1410 1411assert(tempDoublePtr % 8 == 0); 1412 1413function copyTempFloat(ptr) { // functions, because inlining this code increases code size too much 1414 1415 HEAP8[tempDoublePtr] = HEAP8[ptr]; 1416 1417 HEAP8[tempDoublePtr+1] = HEAP8[ptr+1]; 1418 1419 HEAP8[tempDoublePtr+2] = HEAP8[ptr+2]; 1420 1421 HEAP8[tempDoublePtr+3] = HEAP8[ptr+3]; 1422 1423} 1424 1425function copyTempDouble(ptr) { 1426 1427 HEAP8[tempDoublePtr] = HEAP8[ptr]; 1428 1429 HEAP8[tempDoublePtr+1] = HEAP8[ptr+1]; 1430 1431 HEAP8[tempDoublePtr+2] = HEAP8[ptr+2]; 1432 1433 HEAP8[tempDoublePtr+3] = HEAP8[ptr+3]; 1434 1435 HEAP8[tempDoublePtr+4] = HEAP8[ptr+4]; 1436 1437 HEAP8[tempDoublePtr+5] = HEAP8[ptr+5]; 1438 1439 HEAP8[tempDoublePtr+6] = HEAP8[ptr+6]; 1440 1441 HEAP8[tempDoublePtr+7] = HEAP8[ptr+7]; 1442 1443} 1444 1445 1446 1447 1448 1449 1450 var ERRNO_CODES={EPERM:1,ENOENT:2,ESRCH:3,EINTR:4,EIO:5,ENXIO:6,E2BIG:7,ENOEXEC:8,EBADF:9,ECHILD:10,EAGAIN:11,EWOULDBLOCK:11,ENOMEM:12,EACCES:13,EFAULT:14,ENOTBLK:15,EBUSY:16,EEXIST:17,EXDEV:18,ENODEV:19,ENOTDIR:20,EISDIR:21,EINVAL:22,ENFILE:23,EMFILE:24,ENOTTY:25,ETXTBSY:26,EFBIG:27,ENOSPC:28,ESPIPE:29,EROFS:30,EMLINK:31,EPIPE:32,EDOM:33,ERANGE:34,ENOMSG:42,EIDRM:43,ECHRNG:44,EL2NSYNC:45,EL3HLT:46,EL3RST:47,ELNRNG:48,EUNATCH:49,ENOCSI:50,EL2HLT:51,EDEADLK:35,ENOLCK:37,EBADE:52,EBADR:53,EXFULL:54,ENOANO:55,EBADRQC:56,EBADSLT:57,EDEADLOCK:35,EBFONT:59,ENOSTR:60,ENODATA:61,ETIME:62,ENOSR:63,ENONET:64,ENOPKG:65,EREMOTE:66,ENOLINK:67,EADV:68,ESRMNT:69,ECOMM:70,EPROTO:71,EMULTIHOP:72,EDOTDOT:73,EBADMSG:74,ENOTUNIQ:76,EBADFD:77,EREMCHG:78,ELIBACC:79,ELIBBAD:80,ELIBSCN:81,ELIBMAX:82,ELIBEXEC:83,ENOSYS:38,ENOTEMPTY:39,ENAMETOOLONG:36,ELOOP:40,EOPNOTSUPP:95,EPFNOSUPPORT:96,ECONNRESET:104,ENOBUFS:105,EAFNOSUPPORT:97,EPROTOTYPE:91,ENOTSOCK:88,ENOPROTOOPT:92,ESHUTDOWN:108,ECONNREFUSED:111,EADDRINUSE:98,ECONNABORTED:103,ENETUNREACH:101,ENETDOWN:100,ETIMEDOUT:110,EHOSTDOWN:112,EHOSTUNREACH:113,EINPROGRESS:115,EALREADY:114,EDESTADDRREQ:89,EMSGSIZE:90,EPROTONOSUPPORT:93,ESOCKTNOSUPPORT:94,EADDRNOTAVAIL:99,ENETRESET:102,EISCONN:106,ENOTCONN:107,ETOOMANYREFS:109,EUSERS:87,EDQUOT:122,ESTALE:116,ENOTSUP:95,ENOMEDIUM:123,EILSEQ:84,EOVERFLOW:75,ECANCELED:125,ENOTRECOVERABLE:131,EOWNERDEAD:130,ESTRPIPE:86}; 1451 1452 var ERRNO_MESSAGES={0:"Success",1:"Not super-user",2:"No such file or directory",3:"No such process",4:"Interrupted system call",5:"I/O error",6:"No such device or address",7:"Arg list too long",8:"Exec format error",9:"Bad file number",10:"No children",11:"No more processes",12:"Not enough core",13:"Permission denied",14:"Bad address",15:"Block device required",16:"Mount device busy",17:"File exists",18:"Cross-device link",19:"No such device",20:"Not a directory",21:"Is a directory",22:"Invalid argument",23:"Too many open files in system",24:"Too many open files",25:"Not a typewriter",26:"Text file busy",27:"File too large",28:"No space left on device",29:"Illegal seek",30:"Read only file system",31:"Too many links",32:"Broken pipe",33:"Math arg out of domain of func",34:"Math result not representable",35:"File locking deadlock error",36:"File or path name too long",37:"No record locks available",38:"Function not implemented",39:"Directory not empty",40:"Too many symbolic links",42:"No message of desired type",43:"Identifier removed",44:"Channel number out of range",45:"Level 2 not synchronized",46:"Level 3 halted",47:"Level 3 reset",48:"Link number out of range",49:"Protocol driver not attached",50:"No CSI structure available",51:"Level 2 halted",52:"Invalid exchange",53:"Invalid request descriptor",54:"Exchange full",55:"No anode",56:"Invalid request code",57:"Invalid slot",59:"Bad font file fmt",60:"Device not a stream",61:"No data (for no delay io)",62:"Timer expired",63:"Out of streams resources",64:"Machine is not on the network",65:"Package not installed",66:"The object is remote",67:"The link has been severed",68:"Advertise error",69:"Srmount error",70:"Communication error on send",71:"Protocol error",72:"Multihop attempted",73:"Cross mount point (not really error)",74:"Trying to read unreadable message",75:"Value too large for defined data type",76:"Given log. name not unique",77:"f.d. invalid for this operation",78:"Remote address changed",79:"Can access a needed shared lib",80:"Accessing a corrupted shared lib",81:".lib section in a.out corrupted",82:"Attempting to link in too many libs",83:"Attempting to exec a shared library",84:"Illegal byte sequence",86:"Streams pipe error",87:"Too many users",88:"Socket operation on non-socket",89:"Destination address required",90:"Message too long",91:"Protocol wrong type for socket",92:"Protocol not available",93:"Unknown protocol",94:"Socket type not supported",95:"Not supported",96:"Protocol family not supported",97:"Address family not supported by protocol family",98:"Address already in use",99:"Address not available",100:"Network interface is not configured",101:"Network is unreachable",102:"Connection reset by network",103:"Connection aborted",104:"Connection reset by peer",105:"No buffer space available",106:"Socket is already connected",107:"Socket is not connected",108:"Can't send after socket shutdown",109:"Too many references",110:"Connection timed out",111:"Connection refused",112:"Host is down",113:"Host is unreachable",114:"Socket already connected",115:"Connection already in progress",116:"Stale file handle",122:"Quota exceeded",123:"No medium (in tape drive)",125:"Operation canceled",130:"Previous owner died",131:"State not recoverable"}; 1453 1454 1455 var ___errno_state=0;function ___setErrNo(value) { 1456 // For convenient setting and returning of errno. 1457 HEAP32[((___errno_state)>>2)]=value; 1458 return value; 1459 } 1460 1461 var PATH={splitPath:function (filename) { 1462 var splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/; 1463 return splitPathRe.exec(filename).slice(1); 1464 },normalizeArray:function (parts, allowAboveRoot) { 1465 // if the path tries to go above the root, `up` ends up > 0 1466 var up = 0; 1467 for (var i = parts.length - 1; i >= 0; i--) { 1468 var last = parts[i]; 1469 if (last === '.') { 1470 parts.splice(i, 1); 1471 } else if (last === '..') { 1472 parts.splice(i, 1); 1473 up++; 1474 } else if (up) { 1475 parts.splice(i, 1); 1476 up--; 1477 } 1478 } 1479 // if the path is allowed to go above the root, restore leading ..s 1480 if (allowAboveRoot) { 1481 for (; up--; up) { 1482 parts.unshift('..'); 1483 } 1484 } 1485 return parts; 1486 },normalize:function (path) { 1487 var isAbsolute = path.charAt(0) === '/', 1488 trailingSlash = path.substr(-1) === '/'; 1489 // Normalize the path 1490 path = PATH.normalizeArray(path.split('/').filter(function(p) { 1491 return !!p; 1492 }), !isAbsolute).join('/'); 1493 if (!path && !isAbsolute) { 1494 path = '.'; 1495 } 1496 if (path && trailingSlash) { 1497 path += '/'; 1498 } 1499 return (isAbsolute ? '/' : '') + path; 1500 },dirname:function (path) { 1501 var result = PATH.splitPath(path), 1502 root = result[0], 1503 dir = result[1]; 1504 if (!root && !dir) { 1505 // No dirname whatsoever 1506 return '.'; 1507 } 1508 if (dir) { 1509 // It has a dirname, strip trailing slash 1510 dir = dir.substr(0, dir.length - 1); 1511 } 1512 return root + dir; 1513 },basename:function (path) { 1514 // EMSCRIPTEN return '/'' for '/', not an empty string 1515 if (path === '/') return '/'; 1516 var lastSlash = path.lastIndexOf('/'); 1517 if (lastSlash === -1) return path; 1518 return path.substr(lastSlash+1); 1519 },extname:function (path) { 1520 return PATH.splitPath(path)[3]; 1521 },join:function () { 1522 var paths = Array.prototype.slice.call(arguments, 0); 1523 return PATH.normalize(paths.join('/')); 1524 },join2:function (l, r) { 1525 return PATH.normalize(l + '/' + r); 1526 },resolve:function () { 1527 var resolvedPath = '', 1528 resolvedAbsolute = false; 1529 for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) { 1530 var path = (i >= 0) ? arguments[i] : FS.cwd(); 1531 // Skip empty and invalid entries 1532 if (typeof path !== 'string') { 1533 throw new TypeError('Arguments to path.resolve must be strings'); 1534 } else if (!path) { 1535 continue; 1536 } 1537 resolvedPath = path + '/' + resolvedPath; 1538 resolvedAbsolute = path.charAt(0) === '/'; 1539 } 1540 // At this point the path should be resolved to a full absolute path, but 1541 // handle relative paths to be safe (might happen when process.cwd() fails) 1542 resolvedPath = PATH.normalizeArray(resolvedPath.split('/').filter(function(p) { 1543 return !!p; 1544 }), !resolvedAbsolute).join('/'); 1545 return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.'; 1546 },relative:function (from, to) { 1547 from = PATH.resolve(from).substr(1); 1548 to = PATH.resolve(to).substr(1); 1549 function trim(arr) { 1550 var start = 0; 1551 for (; start < arr.length; start++) { 1552 if (arr[start] !== '') break; 1553 } 1554 var end = arr.length - 1; 1555 for (; end >= 0; end--) { 1556 if (arr[end] !== '') break; 1557 } 1558 if (start > end) return []; 1559 return arr.slice(start, end - start + 1); 1560 } 1561 var fromParts = trim(from.split('/')); 1562 var toParts = trim(to.split('/')); 1563 var length = Math.min(fromParts.length, toParts.length); 1564 var samePartsLength = length; 1565 for (var i = 0; i < length; i++) { 1566 if (fromParts[i] !== toParts[i]) { 1567 samePartsLength = i; 1568 break; 1569 } 1570 } 1571 var outputParts = []; 1572 for (var i = samePartsLength; i < fromParts.length; i++) { 1573 outputParts.push('..'); 1574 } 1575 outputParts = outputParts.concat(toParts.slice(samePartsLength)); 1576 return outputParts.join('/'); 1577 }}; 1578 1579 var TTY={ttys:[],init:function () { 1580 // https://github.com/kripken/emscripten/pull/1555 1581 // if (ENVIRONMENT_IS_NODE) { 1582 // // currently, FS.init does not distinguish if process.stdin is a file or TTY 1583 // // device, it always assumes it's a TTY device. because of this, we're forcing 1584 // // process.stdin to UTF8 encoding to at least make stdin reading compatible 1585 // // with text files until FS.init can be refactored. 1586 // process['stdin']['setEncoding']('utf8'); 1587 // } 1588 },shutdown:function () { 1589 // https://github.com/kripken/emscripten/pull/1555 1590 // if (ENVIRONMENT_IS_NODE) { 1591 // // inolen: any idea as to why node -e 'process.stdin.read()' wouldn't exit immediately (with process.stdin being a tty)? 1592 // // isaacs: because now it's reading from the stream, you've expressed interest in it, so that read() kicks off a _read() which creates a ReadReq operation 1593 // // inolen: I thought read() in that case was a synchronous operation that just grabbed some amount of buffered data if it exists? 1594 // // isaacs: it is. but it also triggers a _read() call, which calls readStart() on the handle 1595 // // isaacs: do process.stdin.pause() and i'd think it'd probably close the pending call 1596 // process['stdin']['pause'](); 1597 // } 1598 },register:function (dev, ops) { 1599 TTY.ttys[dev] = { input: [], output: [], ops: ops }; 1600 FS.registerDevice(dev, TTY.stream_ops); 1601 },stream_ops:{open:function (stream) { 1602 var tty = TTY.ttys[stream.node.rdev]; 1603 if (!tty) { 1604 throw new FS.ErrnoError(ERRNO_CODES.ENODEV); 1605 } 1606 stream.tty = tty; 1607 stream.seekable = false; 1608 },close:function (stream) { 1609 // flush any pending line data 1610 if (stream.tty.output.length) { 1611 stream.tty.ops.put_char(stream.tty, 10); 1612 } 1613 },read:function (stream, buffer, offset, length, pos /* ignored */) { 1614 if (!stream.tty || !stream.tty.ops.get_char) { 1615 throw new FS.ErrnoError(ERRNO_CODES.ENXIO); 1616 } 1617 var bytesRead = 0; 1618 for (var i = 0; i < length; i++) { 1619 var result; 1620 try { 1621 result = stream.tty.ops.get_char(stream.tty); 1622 } catch (e) { 1623 throw new FS.ErrnoError(ERRNO_CODES.EIO); 1624 } 1625 if (result === undefined && bytesRead === 0) { 1626 throw new FS.ErrnoError(ERRNO_CODES.EAGAIN); 1627 } 1628 if (result === null || result === undefined) break; 1629 bytesRead++; 1630 buffer[offset+i] = result; 1631 } 1632 if (bytesRead) { 1633 stream.node.timestamp = Date.now(); 1634 } 1635 return bytesRead; 1636 },write:function (stream, buffer, offset, length, pos) { 1637 if (!stream.tty || !stream.tty.ops.put_char) { 1638 throw new FS.ErrnoError(ERRNO_CODES.ENXIO); 1639 } 1640 for (var i = 0; i < length; i++) { 1641 try { 1642 stream.tty.ops.put_char(stream.tty, buffer[offset+i]); 1643 } catch (e) { 1644 throw new FS.ErrnoError(ERRNO_CODES.EIO); 1645 } 1646 } 1647 if (length) { 1648 stream.node.timestamp = Date.now(); 1649 } 1650 return i; 1651 }},default_tty_ops:{get_char:function (tty) { 1652 if (!tty.input.length) { 1653 var result = null; 1654 if (ENVIRONMENT_IS_NODE) { 1655 result = process['stdin']['read'](); 1656 if (!result) { 1657 if (process['stdin']['_readableState'] && process['stdin']['_readableState']['ended']) { 1658 return null; // EOF 1659 } 1660 return undefined; // no data available 1661 } 1662 } else if (typeof window != 'undefined' && 1663 typeof window.prompt == 'function') { 1664 // Browser. 1665 result = window.prompt('Input: '); // returns null on cancel 1666 if (result !== null) { 1667 result += '\n'; 1668 } 1669 } else if (typeof readline == 'function') { 1670 // Command line. 1671 result = readline(); 1672 if (result !== null) { 1673 result += '\n'; 1674 } 1675 } 1676 if (!result) { 1677 return null; 1678 } 1679 tty.input = intArrayFromString(result, true); 1680 } 1681 return tty.input.shift(); 1682 },put_char:function (tty, val) { 1683 if (val === null || val === 10) { 1684 Module['print'](tty.output.join('')); 1685 tty.output = []; 1686 } else { 1687 tty.output.push(TTY.utf8.processCChar(val)); 1688 } 1689 }},default_tty1_ops:{put_char:function (tty, val) { 1690 if (val === null || val === 10) { 1691 Module['printErr'](tty.output.join('')); 1692 tty.output = []; 1693 } else { 1694 tty.output.push(TTY.utf8.processCChar(val)); 1695 } 1696 }}}; 1697 1698 var MEMFS={ops_table:null,CONTENT_OWNING:1,CONTENT_FLEXIBLE:2,CONTENT_FIXED:3,mount:function (mount) { 1699 return MEMFS.createNode(null, '/', 16384 | 511 /* 0777 */, 0); 1700 },createNode:function (parent, name, mode, dev) { 1701 if (FS.isBlkdev(mode) || FS.isFIFO(mode)) { 1702 // no supported 1703 throw new FS.ErrnoError(ERRNO_CODES.EPERM); 1704 } 1705 if (!MEMFS.ops_table) { 1706 MEMFS.ops_table = { 1707 dir: { 1708 node: { 1709 getattr: MEMFS.node_ops.getattr, 1710 setattr: MEMFS.node_ops.setattr, 1711 lookup: MEMFS.node_ops.lookup, 1712 mknod: MEMFS.node_ops.mknod, 1713 rename: MEMFS.node_ops.rename, 1714 unlink: MEMFS.node_ops.unlink, 1715 rmdir: MEMFS.node_ops.rmdir, 1716 readdir: MEMFS.node_ops.readdir, 1717 symlink: MEMFS.node_ops.symlink 1718 }, 1719 stream: { 1720 llseek: MEMFS.stream_ops.llseek 1721 } 1722 }, 1723 file: { 1724 node: { 1725 getattr: MEMFS.node_ops.getattr, 1726 setattr: MEMFS.node_ops.setattr 1727 }, 1728 stream: { 1729 llseek: MEMFS.stream_ops.llseek, 1730 read: MEMFS.stream_ops.read, 1731 write: MEMFS.stream_ops.write, 1732 allocate: MEMFS.stream_ops.allocate, 1733 mmap: MEMFS.stream_ops.mmap 1734 } 1735 }, 1736 link: { 1737 node: { 1738 getattr: MEMFS.node_ops.getattr, 1739 setattr: MEMFS.node_ops.setattr, 1740 readlink: MEMFS.node_ops.readlink 1741 }, 1742 stream: {} 1743 }, 1744 chrdev: { 1745 node: { 1746 getattr: MEMFS.node_ops.getattr, 1747 setattr: MEMFS.node_ops.setattr 1748 }, 1749 stream: FS.chrdev_stream_ops 1750 }, 1751 }; 1752 } 1753 var node = FS.createNode(parent, name, mode, dev); 1754 if (FS.isDir(node.mode)) { 1755 node.node_ops = MEMFS.ops_table.dir.node; 1756 node.stream_ops = MEMFS.ops_table.dir.stream; 1757 node.contents = {}; 1758 } else if (FS.isFile(node.mode)) { 1759 node.node_ops = MEMFS.ops_table.file.node; 1760 node.stream_ops = MEMFS.ops_table.file.stream; 1761 node.contents = []; 1762 node.contentMode = MEMFS.CONTENT_FLEXIBLE; 1763 } else if (FS.isLink(node.mode)) { 1764 node.node_ops = MEMFS.ops_table.link.node; 1765 node.stream_ops = MEMFS.ops_table.link.stream; 1766 } else if (FS.isChrdev(node.mode)) { 1767 node.node_ops = MEMFS.ops_table.chrdev.node; 1768 node.stream_ops = MEMFS.ops_table.chrdev.stream; 1769 } 1770 node.timestamp = Date.now(); 1771 // add the new node to the parent 1772 if (parent) { 1773 parent.contents[name] = node; 1774 } 1775 return node; 1776 },ensureFlexible:function (node) { 1777 if (node.contentMode !== MEMFS.CONTENT_FLEXIBLE) { 1778 var contents = node.contents; 1779 node.contents = Array.prototype.slice.call(contents); 1780 node.contentMode = MEMFS.CONTENT_FLEXIBLE; 1781 } 1782 },node_ops:{getattr:function (node) { 1783 var attr = {}; 1784 // device numbers reuse inode numbers. 1785 attr.dev = FS.isChrdev(node.mode) ? node.id : 1; 1786 attr.ino = node.id; 1787 attr.mode = node.mode; 1788 attr.nlink = 1; 1789 attr.uid = 0; 1790 attr.gid = 0; 1791 attr.rdev = node.rdev; 1792 if (FS.isDir(node.mode)) { 1793 attr.size = 4096; 1794 } else if (FS.isFile(node.mode)) { 1795 attr.size = node.contents.length; 1796 } else if (FS.isLink(node.mode)) { 1797 attr.size = node.link.length; 1798 } else { 1799 attr.size = 0; 1800 } 1801 attr.atime = new Date(node.timestamp); 1802 attr.mtime = new Date(node.timestamp); 1803 attr.ctime = new Date(node.timestamp); 1804 // NOTE: In our implementation, st_blocks = Math.ceil(st_size/st_blksize), 1805 // but this is not required by the standard. 1806 attr.blksize = 4096; 1807 attr.blocks = Math.ceil(attr.size / attr.blksize); 1808 return attr; 1809 },setattr:function (node, attr) { 1810 if (attr.mode !== undefined) { 1811 node.mode = attr.mode; 1812 } 1813 if (attr.timestamp !== undefined) { 1814 node.timestamp = attr.timestamp; 1815 } 1816 if (attr.size !== undefined) { 1817 MEMFS.ensureFlexible(node); 1818 var contents = node.contents; 1819 if (attr.size < contents.length) contents.length = attr.size; 1820 else while (attr.size > contents.length) contents.push(0); 1821 } 1822 },lookup:function (parent, name) { 1823 throw FS.genericErrors[ERRNO_CODES.ENOENT]; 1824 },mknod:function (parent, name, mode, dev) { 1825 return MEMFS.createNode(parent, name, mode, dev); 1826 },rename:function (old_node, new_dir, new_name) { 1827 // if we're overwriting a directory at new_name, make sure it's empty. 1828 if (FS.isDir(old_node.mode)) { 1829 var new_node; 1830 try { 1831 new_node = FS.lookupNode(new_dir, new_name); 1832 } catch (e) { 1833 } 1834 if (new_node) { 1835 for (var i in new_node.contents) { 1836 throw new FS.ErrnoError(ERRNO_CODES.ENOTEMPTY); 1837 } 1838 } 1839 } 1840 // do the internal rewiring 1841 delete old_node.parent.contents[old_node.name]; 1842 old_node.name = new_name; 1843 new_dir.contents[new_name] = old_node; 1844 old_node.parent = new_dir; 1845 },unlink:function (parent, name) { 1846 delete parent.contents[name]; 1847 },rmdir:function (parent, name) { 1848 var node = FS.lookupNode(parent, name); 1849 for (var i in node.contents) { 1850 throw new FS.ErrnoError(ERRNO_CODES.ENOTEMPTY); 1851 } 1852 delete parent.contents[name]; 1853 },readdir:function (node) { 1854 var entries = ['.', '..'] 1855 for (var key in node.contents) { 1856 if (!node.contents.hasOwnProperty(key)) { 1857 continue; 1858 } 1859 entries.push(key); 1860 } 1861 return entries; 1862 },symlink:function (parent, newname, oldpath) { 1863 var node = MEMFS.createNode(parent, newname, 511 /* 0777 */ | 40960, 0); 1864 node.link = oldpath; 1865 return node; 1866 },readlink:function (node) { 1867 if (!FS.isLink(node.mode)) { 1868 throw new FS.ErrnoError(ERRNO_CODES.EINVAL); 1869 } 1870 return node.link; 1871 }},stream_ops:{read:function (stream, buffer, offset, length, position) { 1872 var contents = stream.node.contents; 1873 if (position >= contents.length) 1874 return 0; 1875 var size = Math.min(contents.length - position, length); 1876 assert(size >= 0); 1877 if (size > 8 && contents.subarray) { // non-trivial, and typed array 1878 buffer.set(contents.subarray(position, position + size), offset); 1879 } else 1880 { 1881 for (var i = 0; i < size; i++) { 1882 buffer[offset + i] = contents[position + i]; 1883 } 1884 } 1885 return size; 1886 },write:function (stream, buffer, offset, length, position, canOwn) { 1887 var node = stream.node; 1888 node.timestamp = Date.now(); 1889 var contents = node.contents; 1890 if (length && contents.length === 0 && position === 0 && buffer.subarray) { 1891 // just replace it with the new data 1892 if (canOwn && offset === 0) { 1893 node.contents = buffer; // this could be a subarray of Emscripten HEAP, or allocated from some other source. 1894 node.contentMode = (buffer.buffer === HEAP8.buffer) ? MEMFS.CONTENT_OWNING : MEMFS.CONTENT_FIXED; 1895 } else { 1896 node.contents = new Uint8Array(buffer.subarray(offset, offset+length)); 1897 node.contentMode = MEMFS.CONTENT_FIXED; 1898 } 1899 return length; 1900 } 1901 MEMFS.ensureFlexible(node); 1902 var contents = node.contents; 1903 while (contents.length < position) contents.push(0); 1904 for (var i = 0; i < length; i++) { 1905 contents[position + i] = buffer[offset + i]; 1906 } 1907 return length; 1908 },llseek:function (stream, offset, whence) { 1909 var position = offset; 1910 if (whence === 1) { // SEEK_CUR. 1911 position += stream.position; 1912 } else if (whence === 2) { // SEEK_END. 1913 if (FS.isFile(stream.node.mode)) { 1914 position += stream.node.contents.length; 1915 } 1916 } 1917 if (position < 0) { 1918 throw new FS.ErrnoError(ERRNO_CODES.EINVAL); 1919 } 1920 stream.ungotten = []; 1921 stream.position = position; 1922 return position; 1923 },allocate:function (stream, offset, length) { 1924 MEMFS.ensureFlexible(stream.node); 1925 var contents = stream.node.contents; 1926 var limit = offset + length; 1927 while (limit > contents.length) contents.push(0); 1928 },mmap:function (stream, buffer, offset, length, position, prot, flags) { 1929 if (!FS.isFile(stream.node.mode)) { 1930 throw new FS.ErrnoError(ERRNO_CODES.ENODEV); 1931 } 1932 var ptr; 1933 var allocated; 1934 var contents = stream.node.contents; 1935 // Only make a new copy when MAP_PRIVATE is specified. 1936 if ( !(flags & 2) && 1937 (contents.buffer === buffer || contents.buffer === buffer.buffer) ) { 1938 // We can't emulate MAP_SHARED when the file is not backed by the buffer 1939 // we're mapping to (e.g. the HEAP buffer). 1940 allocated = false; 1941 ptr = contents.byteOffset; 1942 } else { 1943 // Try to avoid unnecessary slices. 1944 if (position > 0 || position + length < contents.length) { 1945 if (contents.subarray) { 1946 contents = contents.subarray(position, position + length); 1947 } else { 1948 contents = Array.prototype.slice.call(contents, position, position + length); 1949 } 1950 } 1951 allocated = true; 1952 ptr = _malloc(length); 1953 if (!ptr) { 1954 throw new FS.ErrnoError(ERRNO_CODES.ENOMEM); 1955 } 1956 buffer.set(contents, ptr); 1957 } 1958 return { ptr: ptr, allocated: allocated }; 1959 }}}; 1960 1961 var IDBFS={dbs:{},indexedDB:function () { 1962 return window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB; 1963 },DB_VERSION:21,DB_STORE_NAME:"FILE_DATA",mount:function (mount) { 1964 // reuse all of the core MEMFS functionality 1965 return MEMFS.mount.apply(null, arguments); 1966 },syncfs:function (mount, populate, callback) { 1967 IDBFS.getLocalSet(mount, function(err, local) { 1968 if (err) return callback(err); 1969 1970 IDBFS.getRemoteSet(mount, function(err, remote) { 1971 if (err) return callback(err); 1972 1973 var src = populate ? remote : local; 1974 var dst = populate ? local : remote; 1975 1976 IDBFS.reconcile(src, dst, callback); 1977 }); 1978 }); 1979 },getDB:function (name, callback) { 1980 // check the cache first 1981 var db = IDBFS.dbs[name]; 1982 if (db) { 1983 return callback(null, db); 1984 } 1985 1986 var req; 1987 try { 1988 req = IDBFS.indexedDB().open(name, IDBFS.DB_VERSION); 1989 } catch (e) { 1990 return callback(e); 1991 } 1992 req.onupgradeneeded = function(e) { 1993 var db = e.target.result; 1994 var transaction = e.target.transaction; 1995 1996 var fileStore; 1997 1998 if (db.objectStoreNames.contains(IDBFS.DB_STORE_NAME)) { 1999 fileStore = transaction.objectStore(IDBFS.DB_STORE_NAME); 2000 } else { 2001 fileStore = db.createObjectStore(IDBFS.DB_STORE_NAME); 2002 } 2003 2004 fileStore.createIndex('timestamp', 'timestamp', { unique: false }); 2005 }; 2006 req.onsuccess = function() { 2007 db = req.result; 2008 2009 // add to the cache 2010 IDBFS.dbs[name] = db; 2011 callback(null, db); 2012 }; 2013 req.onerror = function() { 2014 callback(this.error); 2015 }; 2016 },getLocalSet:function (mount, callback) { 2017 var entries = {}; 2018 2019 function isRealDir(p) { 2020 return p !== '.' && p !== '..'; 2021 }; 2022 function toAbsolute(root) { 2023 return function(p) { 2024 return PATH.join2(root, p); 2025 } 2026 }; 2027 2028 var check = FS.readdir(mount.mountpoint).filter(isRealDir).map(toAbsolute(mount.mountpoint)); 2029 2030 while (check.length) { 2031 var path = check.pop(); 2032 var stat; 2033 2034 try { 2035 stat = FS.stat(path); 2036 } catch (e) { 2037 return callback(e); 2038 } 2039 2040 if (FS.isDir(stat.mode)) { 2041 check.push.apply(check, FS.readdir(path).filter(isRealDir).map(toAbsolute(path))); 2042 } 2043 2044 entries[path] = { timestamp: stat.mtime }; 2045 } 2046 2047 return callback(null, { type: 'local', entries: entries }); 2048 },getRemoteSet:function (mount, callback) { 2049 var entries = {}; 2050 2051 IDBFS.getDB(mount.mountpoint, function(err, db) { 2052 if (err) return callback(err); 2053 2054 var transaction = db.transaction([IDBFS.DB_STORE_NAME], 'readonly'); 2055 transaction.onerror = function() { callback(this.error); }; 2056 2057 var store = transaction.objectStore(IDBFS.DB_STORE_NAME); 2058 var index = store.index('timestamp'); 2059 2060 index.openKeyCursor().onsuccess = function(event) { 2061 var cursor = event.target.result; 2062 2063 if (!cursor) { 2064 return callback(null, { type: 'remote', db: db, entries: entries }); 2065 } 2066 2067 entries[cursor.primaryKey] = { timestamp: cursor.key }; 2068 2069 cursor.continue(); 2070 }; 2071 }); 2072 },loadLocalEntry:function (path, callback) { 2073 var stat, node; 2074 2075 try { 2076 var lookup = FS.lookupPath(path); 2077 node = lookup.node; 2078 stat = FS.stat(path); 2079 } catch (e) { 2080 return callback(e); 2081 } 2082 2083 if (FS.isDir(stat.mode)) { 2084 return callback(null, { timestamp: stat.mtime, mode: stat.mode }); 2085 } else if (FS.isFile(stat.mode)) { 2086 return callback(null, { timestamp: stat.mtime, mode: stat.mode, contents: node.contents }); 2087 } else { 2088 return callback(new Error('node type not supported')); 2089 } 2090 },storeLocalEntry:function (path, entry, callback) { 2091 try { 2092 if (FS.isDir(entry.mode)) { 2093 FS.mkdir(path, entry.mode); 2094 } else if (FS.isFile(entry.mode)) { 2095 FS.writeFile(path, entry.contents, { encoding: 'binary', canOwn: true }); 2096 } else { 2097 return callback(new Error('node type not supported')); 2098 } 2099 2100 FS.utime(path, entry.timestamp, entry.timestamp); 2101 } catch (e) { 2102 return callback(e); 2103 } 2104 2105 callback(null); 2106 },removeLocalEntry:function (path, callback) { 2107 try { 2108 var lookup = FS.lookupPath(path); 2109 var stat = FS.stat(path); 2110 2111 if (FS.isDir(stat.mode)) { 2112 FS.rmdir(path); 2113 } else if (FS.isFile(stat.mode)) { 2114 FS.unlink(path); 2115 } 2116 } catch (e) { 2117 return callback(e); 2118 } 2119 2120 callback(null); 2121 },loadRemoteEntry:function (store, path, callback) { 2122 var req = store.get(path); 2123 req.onsuccess = function(event) { callback(null, event.target.result); }; 2124 req.onerror = function() { callback(this.error); }; 2125 },storeRemoteEntry:function (store, path, entry, callback) { 2126 var req = store.put(entry, path); 2127 req.onsuccess = function() { callback(null); }; 2128 req.onerror = function() { callback(this.error); }; 2129 },removeRemoteEntry:function (store, path, callback) { 2130 var req = store.delete(path); 2131 req.onsuccess = function() { callback(null); }; 2132 req.onerror = function() { callback(this.error); }; 2133 },reconcile:function (src, dst, callback) { 2134 var total = 0; 2135 2136 var create = []; 2137 Object.keys(src.entries).forEach(function (key) { 2138 var e = src.entries[key]; 2139 var e2 = dst.entries[key]; 2140 if (!e2 || e.timestamp > e2.timestamp) { 2141 create.push(key); 2142 total++; 2143 } 2144 }); 2145 2146 var remove = []; 2147 Object.keys(dst.entries).forEach(function (key) { 2148 var e = dst.entries[key]; 2149 var e2 = src.entries[key]; 2150 if (!e2) { 2151 remove.push(key); 2152 total++; 2153 } 2154 }); 2155 2156 if (!total) { 2157 return callback(null); 2158 } 2159 2160 var errored = false; 2161 var completed = 0; 2162 var db = src.type === 'remote' ? src.db : dst.db; 2163 var transaction = db.transaction([IDBFS.DB_STORE_NAME], 'readwrite'); 2164 var store = transaction.objectStore(IDBFS.DB_STORE_NAME); 2165 2166 function done(err) { 2167 if (err) { 2168 if (!done.errored) { 2169 done.errored = true; 2170 return callback(err); 2171 } 2172 return; 2173 } 2174 if (++completed >= total) { 2175 return callback(null); 2176 } 2177 }; 2178 2179 transaction.onerror = function() { done(this.error); }; 2180 2181 // sort paths in ascending order so directory entries are created 2182 // before the files inside them 2183 create.sort().forEach(function (path) { 2184 if (dst.type === 'local') { 2185 IDBFS.loadRemoteEntry(store, path, function (err, entry) { 2186 if (err) return done(err); 2187 IDBFS.storeLocalEntry(path, entry, done); 2188 }); 2189 } else { 2190 IDBFS.loadLocalEntry(path, function (err, entry) { 2191 if (err) return done(err); 2192 IDBFS.storeRemoteEntry(store, path, entry, done); 2193 }); 2194 } 2195 }); 2196 2197 // sort paths in descending order so files are deleted before their 2198 // parent directories 2199 remove.sort().reverse().forEach(function(path) { 2200 if (dst.type === 'local') { 2201 IDBFS.removeLocalEntry(path, done); 2202 } else { 2203 IDBFS.removeRemoteEntry(store, path, done); 2204 } 2205 }); 2206 }}; 2207 2208 var NODEFS={isWindows:false,staticInit:function () { 2209 NODEFS.isWindows = !!process.platform.match(/^win/); 2210 },mount:function (mount) { 2211 assert(ENVIRONMENT_IS_NODE); 2212 return NODEFS.createNode(null, '/', NODEFS.getMode(mount.opts.root), 0); 2213 },createNode:function (parent, name, mode, dev) { 2214 if (!FS.isDir(mode) && !FS.isFile(mode) && !FS.isLink(mode)) { 2215 throw new FS.ErrnoError(ERRNO_CODES.EINVAL); 2216 } 2217 var node = FS.createNode(parent, name, mode); 2218 node.node_ops = NODEFS.node_ops; 2219 node.stream_ops = NODEFS.stream_ops; 2220 return node; 2221 },getMode:function (path) { 2222 var stat; 2223 try { 2224 stat = fs.lstatSync(path); 2225 if (NODEFS.isWindows) { 2226 // On Windows, directories return permission bits 'rw-rw-rw-', even though they have 'rwxrwxrwx', so 2227 // propagate write bits to execute bits. 2228 stat.mode = stat.mode | ((stat.mode & 146) >> 1); 2229 } 2230 } catch (e) { 2231 if (!e.code) throw e; 2232 throw new FS.ErrnoError(ERRNO_CODES[e.code]); 2233 } 2234 return stat.mode; 2235 },realPath:function (node) { 2236 var parts = []; 2237 while (node.parent !== node) { 2238 parts.push(node.name); 2239 node = node.parent; 2240 } 2241 parts.push(node.mount.opts.root); 2242 parts.reverse(); 2243 return PATH.join.apply(null, parts); 2244 },flagsToPermissionStringMap:{0:"r",1:"r+",2:"r+",64:"r",65:"r+",66:"r+",129:"rx+",193:"rx+",514:"w+",577:"w",578:"w+",705:"wx",706:"wx+",1024:"a",1025:"a",1026:"a+",1089:"a",1090:"a+",1153:"ax",1154:"ax+",1217:"ax",1218:"ax+",4096:"rs",4098:"rs+"},flagsToPermissionString:function (flags) { 2245 if (flags in NODEFS.flagsToPermissionStringMap) { 2246 return NODEFS.flagsToPermissionStringMap[flags]; 2247 } else { 2248 return flags; 2249 } 2250 },node_ops:{getattr:function (node) { 2251 var path = NODEFS.realPath(node); 2252 var stat; 2253 try { 2254 stat = fs.lstatSync(path); 2255 } catch (e) { 2256 if (!e.code) throw e; 2257 throw new FS.ErrnoError(ERRNO_CODES[e.code]); 2258 } 2259 // node.js v0.10.20 doesn't report blksize and blocks on Windows. Fake them with default blksize of 4096. 2260 // See http://support.microsoft.com/kb/140365 2261 if (NODEFS.isWindows && !stat.blksize) { 2262 stat.blksize = 4096; 2263 } 2264 if (NODEFS.isWindows && !stat.blocks) { 2265 stat.blocks = (stat.size+stat.blksize-1)/stat.blksize|0; 2266 } 2267 return { 2268 dev: stat.dev, 2269 ino: stat.ino, 2270 mode: stat.mode, 2271 nlink: stat.nlink, 2272 uid: stat.uid, 2273 gid: stat.gid, 2274 rdev: stat.rdev, 2275 size: stat.size, 2276 atime: stat.atime, 2277 mtime: stat.mtime, 2278 ctime: stat.ctime, 2279 blksize: stat.blksize, 2280 blocks: stat.blocks 2281 }; 2282 },setattr:function (node, attr) { 2283 var path = NODEFS.realPath(node); 2284 try { 2285 if (attr.mode !== undefined) { 2286 fs.chmodSync(path, attr.mode); 2287 // update the common node structure mode as well 2288 node.mode = attr.mode; 2289 } 2290 if (attr.timestamp !== undefined) { 2291 var date = new Date(attr.timestamp); 2292 fs.utimesSync(path, date, date); 2293 } 2294 if (attr.size !== undefined) { 2295 fs.truncateSync(path, attr.size); 2296 } 2297 } catch (e) { 2298 if (!e.code) throw e; 2299 throw new FS.ErrnoError(ERRNO_CODES[e.code]); 2300 } 2301 },lookup:function (parent, name) { 2302 var path = PATH.join2(NODEFS.realPath(parent), name); 2303 var mode = NODEFS.getMode(path); 2304 return NODEFS.createNode(parent, name, mode); 2305 },mknod:function (parent, name, mode, dev) { 2306 var node = NODEFS.createNode(parent, name, mode, dev); 2307 // create the backing node for this in the fs root as well 2308 var path = NODEFS.realPath(node); 2309 try { 2310 if (FS.isDir(node.mode)) { 2311 fs.mkdirSync(path, node.mode); 2312 } else { 2313 fs.writeFileSync(path, '', { mode: node.mode }); 2314 } 2315 } catch (e) { 2316 if (!e.code) throw e; 2317 throw new FS.ErrnoError(ERRNO_CODES[e.code]); 2318 } 2319 return node; 2320 },rename:function (oldNode, newDir, newName) { 2321 var oldPath = NODEFS.realPath(oldNode); 2322 var newPath = PATH.join2(NODEFS.realPath(newDir), newName); 2323 try { 2324 fs.renameSync(oldPath, newPath); 2325 } catch (e) { 2326 if (!e.code) throw e; 2327 throw new FS.ErrnoError(ERRNO_CODES[e.code]); 2328 } 2329 },unlink:function (parent, name) { 2330 var path = PATH.join2(NODEFS.realPath(parent), name); 2331 try { 2332 fs.unlinkSync(path); 2333 } catch (e) { 2334 if (!e.code) throw e; 2335 throw new FS.ErrnoError(ERRNO_CODES[e.code]); 2336 } 2337 },rmdir:function (parent, name) { 2338 var path = PATH.join2(NODEFS.realPath(parent), name); 2339 try { 2340 fs.rmdirSync(path); 2341 } catch (e) { 2342 if (!e.code) throw e; 2343 throw new FS.ErrnoError(ERRNO_CODES[e.code]); 2344 } 2345 },readdir:function (node) { 2346 var path = NODEFS.realPath(node); 2347 try { 2348 return fs.readdirSync(path); 2349 } catch (e) { 2350 if (!e.code) throw e; 2351 throw new FS.ErrnoError(ERRNO_CODES[e.code]); 2352 } 2353 },symlink:function (parent, newName, oldPath) { 2354 var newPath = PATH.join2(NODEFS.realPath(parent), newName); 2355 try { 2356 fs.symlinkSync(oldPath, newPath); 2357 } catch (e) { 2358 if (!e.code) throw e; 2359 throw new FS.ErrnoError(ERRNO_CODES[e.code]); 2360 } 2361 },readlink:function (node) { 2362 var path = NODEFS.realPath(node); 2363 try { 2364 return fs.readlinkSync(path); 2365 } catch (e) { 2366 if (!e.code) throw e; 2367 throw new FS.ErrnoError(ERRNO_CODES[e.code]); 2368 } 2369 }},stream_ops:{open:function (stream) { 2370 var path = NODEFS.realPath(stream.node); 2371 try { 2372 if (FS.isFile(stream.node.mode)) { 2373 stream.nfd = fs.openSync(path, NODEFS.flagsToPermissionString(stream.flags)); 2374 } 2375 } catch (e) { 2376 if (!e.code) throw e; 2377 throw new FS.ErrnoError(ERRNO_CODES[e.code]); 2378 } 2379 },close:function (stream) { 2380 try { 2381 if (FS.isFile(stream.node.mode) && stream.nfd) { 2382 fs.closeSync(stream.nfd); 2383 } 2384 } catch (e) { 2385 if (!e.code) throw e; 2386 throw new FS.ErrnoError(ERRNO_CODES[e.code]); 2387 } 2388 },read:function (stream, buffer, offset, length, position) { 2389 // FIXME this is terrible. 2390 var nbuffer = new Buffer(length); 2391 var res; 2392 try { 2393 res = fs.readSync(stream.nfd, nbuffer, 0, length, position); 2394 } catch (e) { 2395 throw new FS.ErrnoError(ERRNO_CODES[e.code]); 2396 } 2397 if (res > 0) { 2398 for (var i = 0; i < res; i++) { 2399 buffer[offset + i] = nbuffer[i]; 2400 } 2401 } 2402 return res; 2403 },write:function (stream, buffer, offset, length, position) { 2404 // FIXME this is terrible. 2405 var nbuffer = new Buffer(buffer.subarray(offset, offset + length)); 2406 var res; 2407 try { 2408 res = fs.writeSync(stream.nfd, nbuffer, 0, length, position); 2409 } catch (e) { 2410 throw new FS.ErrnoError(ERRNO_CODES[e.code]); 2411 } 2412 return res; 2413 },llseek:function (stream, offset, whence) { 2414 var position = offset; 2415 if (whence === 1) { // SEEK_CUR. 2416 position += stream.position; 2417 } else if (whence === 2) { // SEEK_END. 2418 if (FS.isFile(stream.node.mode)) { 2419 try { 2420 var stat = fs.fstatSync(stream.nfd); 2421 position += stat.size; 2422 } catch (e) { 2423 throw new FS.ErrnoError(ERRNO_CODES[e.code]); 2424 } 2425 } 2426 } 2427 2428 if (position < 0) { 2429 throw new FS.ErrnoError(ERRNO_CODES.EINVAL); 2430 } 2431 2432 stream.position = position; 2433 return position; 2434 }}}; 2435 2436 var _stdin=allocate(1, "i32*", ALLOC_STATIC); 2437 2438 var _stdout=allocate(1, "i32*", ALLOC_STATIC); 2439 2440 var _stderr=allocate(1, "i32*", ALLOC_STATIC); 2441 2442 function _fflush(stream) { 2443 // int fflush(FILE *stream); 2444 // http://pubs.opengroup.org/onlinepubs/000095399/functions/fflush.html 2445 // we don't currently perform any user-space buffering of data 2446 }var FS={root:null,mounts:[],devices:[null],streams:[],nextInode:1,nameTable:null,currentPath:"/",initialized:false,ignorePermissions:true,ErrnoError:null,genericErrors:{},handleFSError:function (e) { 2447 if (!(e instanceof FS.ErrnoError)) throw e + ' : ' + stackTrace(); 2448 return ___setErrNo(e.errno); 2449 },lookupPath:function (path, opts) { 2450 path = PATH.resolve(FS.cwd(), path); 2451 opts = opts || {}; 2452 2453 var defaults = { 2454 follow_mount: true, 2455 recurse_count: 0 2456 }; 2457 for (var key in defaults) { 2458 if (opts[key] === undefined) { 2459 opts[key] = defaults[key]; 2460 } 2461 } 2462 2463 if (opts.recurse_count > 8) { // max recursive lookup of 8 2464 throw new FS.ErrnoError(ERRNO_CODES.ELOOP); 2465 } 2466 2467 // split the path 2468 var parts = PATH.normalizeArray(path.split('/').filter(function(p) { 2469 return !!p; 2470 }), false); 2471 2472 // start at the root 2473 var current = FS.root; 2474 var current_path = '/'; 2475 2476 for (var i = 0; i < parts.length; i++) { 2477 var islast = (i === parts.length-1); 2478 if (islast && opts.parent) { 2479 // stop resolving 2480 break; 2481 } 2482 2483 current = FS.lookupNode(current, parts[i]); 2484 current_path = PATH.join2(current_path, parts[i]); 2485 2486 // jump to the mount's root node if this is a mountpoint 2487 if (FS.isMountpoint(current)) { 2488 if (!islast || (islast && opts.follow_mount)) { 2489 current = current.mounted.root; 2490 } 2491 } 2492 2493 // by default, lookupPath will not follow a symlink if it is the final path component. 2494 // setting opts.follow = true will override this behavior. 2495 if (!islast || opts.follow) { 2496 var count = 0; 2497 while (FS.isLink(current.mode)) { 2498 var link = FS.readlink(current_path); 2499 current_path = PATH.resolve(PATH.dirname(current_path), link); 2500 2501 var lookup = FS.lookupPath(current_path, { recurse_count: opts.recurse_count }); 2502 current = lookup.node; 2503 2504 if (count++ > 40) { // limit max consecutive symlinks to 40 (SYMLOOP_MAX). 2505 throw new FS.ErrnoError(ERRNO_CODES.ELOOP); 2506 } 2507 } 2508 } 2509 } 2510 2511 return { path: current_path, node: current }; 2512 },getPath:function (node) { 2513 var path; 2514 while (true) { 2515 if (FS.isRoot(node)) { 2516 var mount = node.mount.mountpoint; 2517 if (!path) return mount; 2518 return mount[mount.length-1] !== '/' ? mount + '/' + path : mount + path; 2519 } 2520 path = path ? node.name + '/' + path : node.name; 2521 node = node.parent; 2522 } 2523 },hashName:function (parentid, name) { 2524 var hash = 0; 2525 2526 2527 for (var i = 0; i < name.length; i++) { 2528 hash = ((hash << 5) - hash + name.charCodeAt(i)) | 0; 2529 } 2530 return ((parentid + hash) >>> 0) % FS.nameTable.length; 2531 },hashAddNode:function (node) { 2532 var hash = FS.hashName(node.parent.id, node.name); 2533 node.name_next = FS.nameTable[hash]; 2534 FS.nameTable[hash] = node; 2535 },hashRemoveNode:function (node) { 2536 var hash = FS.hashName(node.parent.id, node.name); 2537 if (FS.nameTable[hash] === node) { 2538 FS.nameTable[hash] = node.name_next; 2539 } else { 2540 var current = FS.nameTable[hash]; 2541 while (current) { 2542 if (current.name_next === node) { 2543 current.name_next = node.name_next; 2544 break; 2545 } 2546 current = current.name_next; 2547 } 2548 } 2549 },lookupNode:function (parent, name) { 2550 var err = FS.mayLookup(parent); 2551 if (err) { 2552 throw new FS.ErrnoError(err); 2553 } 2554 var hash = FS.hashName(parent.id, name); 2555 for (var node = FS.nameTable[hash]; node; node = node.name_next) { 2556 var nodeName = node.name; 2557 if (node.parent.id === parent.id && nodeName === name) { 2558 return node; 2559 } 2560 } 2561 // if we failed to find it in the cache, call into the VFS 2562 return FS.lookup(parent, name); 2563 },createNode:function (parent, name, mode, rdev) { 2564 if (!FS.FSNode) { 2565 FS.FSNode = function(parent, name, mode, rdev) { 2566 if (!parent) { 2567 parent = this; // root node sets parent to itself 2568 } 2569 this.parent = parent; 2570 this.mount = parent.mount; 2571 this.mounted = null; 2572 this.id = FS.nextInode++; 2573 this.name = name; 2574 this.mode = mode; 2575 this.node_ops = {}; 2576 this.stream_ops = {}; 2577 this.rdev = rdev; 2578 }; 2579 2580 FS.FSNode.prototype = {}; 2581 2582 // compatibility 2583 var readMode = 292 | 73; 2584 var writeMode = 146; 2585 2586 // NOTE we must use Object.defineProperties instead of individual calls to 2587 // Object.defineProperty in order to make closure compiler happy 2588 Object.defineProperties(FS.FSNode.prototype, { 2589 read: { 2590 get: function() { return (this.mode & readMode) === readMode; }, 2591 set: function(val) { val ? this.mode |= readMode : this.mode &= ~readMode; } 2592 }, 2593 write: { 2594 get: function() { return (this.mode & writeMode) === writeMode; }, 2595 set: function(val) { val ? this.mode |= writeMode : this.mode &= ~writeMode; } 2596 }, 2597 isFolder: { 2598 get: function() { return FS.isDir(this.mode); }, 2599 }, 2600 isDevice: { 2601 get: function() { return FS.isChrdev(this.mode); }, 2602 }, 2603 }); 2604 } 2605 2606 var node = new FS.FSNode(parent, name, mode, rdev); 2607 2608 FS.hashAddNode(node); 2609 2610 return node; 2611 },destroyNode:function (node) { 2612 FS.hashRemoveNode(node); 2613 },isRoot:function (node) { 2614 return node === node.parent; 2615 },isMountpoint:function (node) { 2616 return !!node.mounted; 2617 },isFile:function (mode) { 2618 return (mode & 61440) === 32768; 2619 },isDir:function (mode) { 2620 return (mode & 61440) === 16384; 2621 },isLink:function (mode) { 2622 return (mode & 61440) === 40960; 2623 },isChrdev:function (mode) { 2624 return (mode & 61440) === 8192; 2625 },isBlkdev:function (mode) { 2626 return (mode & 61440) === 24576; 2627 },isFIFO:function (mode) { 2628 return (mode & 61440) === 4096; 2629 },isSocket:function (mode) { 2630 return (mode & 49152) === 49152; 2631 },flagModes:{"r":0,"rs":1052672,"r+":2,"w":577,"wx":705,"xw":705,"w+":578,"wx+":706,"xw+":706,"a":1089,"ax":1217,"xa":1217,"a+":1090,"ax+":1218,"xa+":1218},modeStringToFlags:function (str) { 2632 var flags = FS.flagModes[str]; 2633 if (typeof flags === 'undefined') { 2634 throw new Error('Unknown file open mode: ' + str); 2635 } 2636 return flags; 2637 },flagsToPermissionString:function (flag) { 2638 var accmode = flag & 2097155; 2639 var perms = ['r', 'w', 'rw'][accmode]; 2640 if ((flag & 512)) { 2641 perms += 'w'; 2642 } 2643 return perms; 2644 },nodePermissions:function (node, perms) { 2645 if (FS.ignorePermissions) { 2646 return 0; 2647 } 2648 // return 0 if any user, group or owner bits are set. 2649 if (perms.indexOf('r') !== -1 && !(node.mode & 292)) { 2650 return ERRNO_CODES.EACCES; 2651 } else if (perms.indexOf('w') !== -1 && !(node.mode & 146)) { 2652 return ERRNO_CODES.EACCES; 2653 } else if (perms.indexOf('x') !== -1 && !(node.mode & 73)) { 2654 return ERRNO_CODES.EACCES; 2655 } 2656 return 0; 2657 },mayLookup:function (dir) { 2658 return FS.nodePermissions(dir, 'x'); 2659 },mayCreate:function (dir, name) { 2660 try { 2661 var node = FS.lookupNode(dir, name); 2662 return ERRNO_CODES.EEXIST; 2663 } catch (e) { 2664 } 2665 return FS.nodePermissions(dir, 'wx'); 2666 },mayDelete:function (dir, name, isdir) { 2667 var node; 2668 try { 2669 node = FS.lookupNode(dir, name); 2670 } catch (e) { 2671 return e.errno; 2672 } 2673 var err = FS.nodePermissions(dir, 'wx'); 2674 if (err) { 2675 return err; 2676 } 2677 if (isdir) { 2678 if (!FS.isDir(node.mode)) { 2679 return ERRNO_CODES.ENOTDIR; 2680 } 2681 if (FS.isRoot(node) || FS.getPath(node) === FS.cwd()) { 2682 return ERRNO_CODES.EBUSY; 2683 } 2684 } else { 2685 if (FS.isDir(node.mode)) { 2686 return ERRNO_CODES.EISDIR; 2687 } 2688 } 2689 return 0; 2690 },mayOpen:function (node, flags) { 2691 if (!node) { 2692 return ERRNO_CODES.ENOENT; 2693 } 2694 if (FS.isLink(node.mode)) { 2695 return ERRNO_CODES.ELOOP; 2696 } else if (FS.isDir(node.mode)) { 2697 if ((flags & 2097155) !== 0 || // opening for write 2698 (flags & 512)) { 2699 return ERRNO_CODES.EISDIR; 2700 } 2701 } 2702 return FS.nodePermissions(node, FS.flagsToPermissionString(flags)); 2703 },MAX_OPEN_FDS:4096,nextfd:function (fd_start, fd_end) { 2704 fd_start = fd_start || 0; 2705 fd_end = fd_end || FS.MAX_OPEN_FDS; 2706 for (var fd = fd_start; fd <= fd_end; fd++) { 2707 if (!FS.streams[fd]) { 2708 return fd; 2709 } 2710 } 2711 throw new FS.ErrnoError(ERRNO_CODES.EMFILE); 2712 },getStream:function (fd) { 2713 return FS.streams[fd]; 2714 },createStream:function (stream, fd_start, fd_end) { 2715 if (!FS.FSStream) { 2716 FS.FSStream = function(){}; 2717 FS.FSStream.prototype = {}; 2718 // compatibility 2719 Object.defineProperties(FS.FSStream.prototype, { 2720 object: { 2721 get: function() { return this.node; }, 2722 set: function(val) { this.node = val; } 2723 }, 2724 isRead: { 2725 get: function() { return (this.flags & 2097155) !== 1; } 2726 }, 2727 isWrite: { 2728 get: function() { return (this.flags & 2097155) !== 0; } 2729 }, 2730 isAppend: { 2731 get: function() { return (this.flags & 1024); } 2732 } 2733 }); 2734 } 2735 if (0) { 2736 // reuse the object 2737 stream.__proto__ = FS.FSStream.prototype; 2738 } else { 2739 var newStream = new FS.FSStream(); 2740 for (var p in stream) { 2741 newStream[p] = stream[p]; 2742 } 2743 stream = newStream; 2744 } 2745 var fd = FS.nextfd(fd_start, fd_end); 2746 stream.fd = fd; 2747 FS.streams[fd] = stream; 2748 return stream; 2749 },closeStream:function (fd) { 2750 FS.streams[fd] = null; 2751 },getStreamFromPtr:function (ptr) { 2752 return FS.streams[ptr - 1]; 2753 },getPtrForStream:function (stream) { 2754 return stream ? stream.fd + 1 : 0; 2755 },chrdev_stream_ops:{open:function (stream) { 2756 var device = FS.getDevice(stream.node.rdev); 2757 // override node's stream ops with the device's 2758 stream.stream_ops = device.stream_ops; 2759 // forward the open call 2760 if (stream.stream_ops.open) { 2761 stream.stream_ops.open(stream); 2762 } 2763 },llseek:function () { 2764 throw new FS.ErrnoError(ERRNO_CODES.ESPIPE); 2765 }},major:function (dev) { 2766 return ((dev) >> 8); 2767 },minor:function (dev) { 2768 return ((dev) & 0xff); 2769 },makedev:function (ma, mi) { 2770 return ((ma) << 8 | (mi)); 2771 },registerDevice:function (dev, ops) { 2772 FS.devices[dev] = { stream_ops: ops }; 2773 },getDevice:function (dev) { 2774 return FS.devices[dev]; 2775 },getMounts:function (mount) { 2776 var mounts = []; 2777 var check = [mount]; 2778 2779 while (check.length) { 2780 var m = check.pop(); 2781 2782 mounts.push(m); 2783 2784 check.push.apply(check, m.mounts); 2785 } 2786 2787 return mounts; 2788 },syncfs:function (populate, callback) { 2789 if (typeof(populate) === 'function') { 2790 callback = populate; 2791 populate = false; 2792 } 2793 2794 var mounts = FS.getMounts(FS.root.mount); 2795 var completed = 0; 2796 2797 function done(err) { 2798 if (err) { 2799 if (!done.errored) { 2800 done.errored = true; 2801 return callback(err); 2802 } 2803 return; 2804 } 2805 if (++completed >= mounts.length) { 2806 callback(null); 2807 } 2808 }; 2809 2810 // sync all mounts 2811 mounts.forEach(function (mount) { 2812 if (!mount.type.syncfs) { 2813 return done(null); 2814 } 2815 mount.type.syncfs(mount, populate, done); 2816 }); 2817 },mount:function (type, opts, mountpoint) { 2818 var root = mountpoint === '/'; 2819 var pseudo = !mountpoint; 2820 var node; 2821 2822 if (root && FS.root) { 2823 throw new FS.ErrnoError(ERRNO_CODES.EBUSY); 2824 } else if (!root && !pseudo) { 2825 var lookup = FS.lookupPath(mountpoint, { follow_mount: false }); 2826 2827 mountpoint = lookup.path; // use the absolute path 2828 node = lookup.node; 2829 2830 if (FS.isMountpoint(node)) { 2831 throw new FS.ErrnoError(ERRNO_CODES.EBUSY); 2832 } 2833 2834 if (!FS.isDir(node.mode)) { 2835 throw new FS.ErrnoError(ERRNO_CODES.ENOTDIR); 2836 } 2837 } 2838 2839 var mount = { 2840 type: type, 2841 opts: opts, 2842 mountpoint: mountpoint, 2843 mounts: [] 2844 }; 2845 2846 // create a root node for the fs 2847 var mountRoot = type.mount(mount); 2848 mountRoot.mount = mount; 2849 mount.root = mountRoot; 2850 2851 if (root) { 2852 FS.root = mountRoot; 2853 } else if (node) { 2854 // set as a mountpoint 2855 node.mounted = mount; 2856 2857 // add the new mount to the current mount's children 2858 if (node.mount) { 2859 node.mount.mounts.push(mount); 2860 } 2861 } 2862 2863 return mountRoot; 2864 },unmount:function (mountpoint) { 2865 var lookup = FS.lookupPath(mountpoint, { follow_mount: false }); 2866 2867 if (!FS.isMountpoint(lookup.node)) { 2868 throw new FS.ErrnoError(ERRNO_CODES.EINVAL); 2869 } 2870 2871 // destroy the nodes for this mount, and all its child mounts 2872 var node = lookup.node; 2873 var mount = node.mounted; 2874 var mounts = FS.getMounts(mount); 2875 2876 Object.keys(FS.nameTable).forEach(function (hash) { 2877 var current = FS.nameTable[hash]; 2878 2879 while (current) { 2880 var next = current.name_next; 2881 2882 if (mounts.indexOf(current.mount) !== -1) { 2883 FS.destroyNode(current); 2884 } 2885 2886 current = next; 2887 } 2888 }); 2889 2890 // no longer a mountpoint 2891 node.mounted = null; 2892 2893 // remove this mount from the child mounts 2894 var idx = node.mount.mounts.indexOf(mount); 2895 assert(idx !== -1); 2896 node.mount.mounts.splice(idx, 1); 2897 },lookup:function (parent, name) { 2898 return parent.node_ops.lookup(parent, name); 2899 },mknod:function (path, mode, dev) { 2900 var lookup = FS.lookupPath(path, { parent: true }); 2901 var parent = lookup.node; 2902 var name = PATH.basename(path); 2903 var err = FS.mayCreate(parent, name); 2904 if (err) { 2905 throw new FS.ErrnoError(err); 2906 } 2907 if (!parent.node_ops.mknod) { 2908 throw new FS.ErrnoError(ERRNO_CODES.EPERM); 2909 } 2910 return parent.node_ops.mknod(parent, name, mode, dev); 2911 },create:function (path, mode) { 2912 mode = mode !== undefined ? mode : 438 /* 0666 */; 2913 mode &= 4095; 2914 mode |= 32768; 2915 return FS.mknod(path, mode, 0); 2916 },mkdir:function (path, mode) { 2917 mode = mode !== undefined ? mode : 511 /* 0777 */; 2918 mode &= 511 | 512; 2919 mode |= 16384; 2920 return FS.mknod(path, mode, 0); 2921 },mkdev:function (path, mode, dev) { 2922 if (typeof(dev) === 'undefined') { 2923 dev = mode; 2924 mode = 438 /* 0666 */; 2925 } 2926 mode |= 8192; 2927 return FS.mknod(path, mode, dev); 2928 },symlink:function (oldpath, newpath) { 2929 var lookup = FS.lookupPath(newpath, { parent: true }); 2930 var parent = lookup.node; 2931 var newname = PATH.basename(newpath); 2932 var err = FS.mayCreate(parent, newname); 2933 if (err) { 2934 throw new FS.ErrnoError(err); 2935 } 2936 if (!parent.node_ops.symlink) { 2937 throw new FS.ErrnoError(ERRNO_CODES.EPERM); 2938 } 2939 return parent.node_ops.symlink(parent, newname, oldpath); 2940 },rename:function (old_path, new_path) { 2941 var old_dirname = PATH.dirname(old_path); 2942 var new_dirname = PATH.dirname(new_path); 2943 var old_name = PATH.basename(old_path); 2944 var new_name = PATH.basename(new_path); 2945 // parents must exist 2946 var lookup, old_dir, new_dir; 2947 try { 2948 lookup = FS.lookupPath(old_path, { parent: true }); 2949 old_dir = lookup.node; 2950 lookup = FS.lookupPath(new_path, { parent: true }); 2951 new_dir = lookup.node; 2952 } catch (e) { 2953 throw new FS.ErrnoError(ERRNO_CODES.EBUSY); 2954 } 2955 // need to be part of the same mount 2956 if (old_dir.mount !== new_dir.mount) { 2957 throw new FS.ErrnoError(ERRNO_CODES.EXDEV); 2958 } 2959 // source must exist 2960 var old_node = FS.lookupNode(old_dir, old_name); 2961 // old path should not be an ancestor of the new path 2962 var relative = PATH.relative(old_path, new_dirname); 2963 if (relative.charAt(0) !== '.') { 2964 throw new FS.ErrnoError(ERRNO_CODES.EINVAL); 2965 } 2966 // new path should not be an ancestor of the old path 2967 relative = PATH.relative(new_path, old_dirname); 2968 if (relative.charAt(0) !== '.') { 2969 throw new FS.ErrnoError(ERRNO_CODES.ENOTEMPTY); 2970 } 2971 // see if the new path already exists 2972 var new_node; 2973 try { 2974 new_node = FS.lookupNode(new_dir, new_name); 2975 } catch (e) { 2976 // not fatal 2977 } 2978 // early out if nothing needs to change 2979 if (old_node === new_node) { 2980 return; 2981 } 2982 // we'll need to delete the old entry 2983 var isdir = FS.isDir(old_node.mode); 2984 var err = FS.mayDelete(old_dir, old_name, isdir); 2985 if (err) { 2986 throw new FS.ErrnoError(err); 2987 } 2988 // need delete permissions if we'll be overwriting. 2989 // need create permissions if new doesn't already exist. 2990 err = new_node ? 2991 FS.mayDelete(new_dir, new_name, isdir) : 2992 FS.mayCreate(new_dir, new_name); 2993 if (err) { 2994 throw new FS.ErrnoError(err); 2995 } 2996 if (!old_dir.node_ops.rename) { 2997 throw new FS.ErrnoError(ERRNO_CODES.EPERM); 2998 } 2999 if (FS.isMountpoint(old_node) || (new_node && FS.isMountpoint(new_node))) { 3000 throw new FS.ErrnoError(ERRNO_CODES.EBUSY); 3001 } 3002 // if we are going to change the parent, check write permissions 3003 if (new_dir !== old_dir) { 3004 err = FS.nodePermissions(old_dir, 'w'); 3005 if (err) { 3006 throw new FS.ErrnoError(err); 3007 } 3008 } 3009 // remove the node from the lookup hash 3010 FS.hashRemoveNode(old_node); 3011 // do the underlying fs rename 3012 try { 3013 old_dir.node_ops.rename(old_node, new_dir, new_name); 3014 } catch (e) { 3015 throw e; 3016 } finally { 3017 // add the node back to the hash (in case node_ops.rename 3018 // changed its name) 3019 FS.hashAddNode(old_node); 3020 } 3021 },rmdir:function (path) { 3022 var lookup = FS.lookupPath(path, { parent: true }); 3023 var parent = lookup.node; 3024 var name = PATH.basename(path); 3025 var node = FS.lookupNode(parent, name); 3026 var err = FS.mayDelete(parent, name, true); 3027 if (err) { 3028 throw new FS.ErrnoError(err); 3029 } 3030 if (!parent.node_ops.rmdir) { 3031 throw new FS.ErrnoError(ERRNO_CODES.EPERM); 3032 } 3033 if (FS.isMountpoint(node)) { 3034 throw new FS.ErrnoError(ERRNO_CODES.EBUSY); 3035 } 3036 parent.node_ops.rmdir(parent, name); 3037 FS.destroyNode(node); 3038 },readdir:function (path) { 3039 var lookup = FS.lookupPath(path, { follow: true }); 3040 var node = lookup.node; 3041 if (!node.node_ops.readdir) { 3042 throw new FS.ErrnoError(ERRNO_CODES.ENOTDIR); 3043 } 3044 return node.node_ops.readdir(node); 3045 },unlink:function (path) { 3046 var lookup = FS.lookupPath(path, { parent: true }); 3047 var parent = lookup.node; 3048 var name = PATH.basename(path); 3049 var node = FS.lookupNode(parent, name); 3050 var err = FS.mayDelete(parent, name, false); 3051 if (err) { 3052 // POSIX says unlink should set EPERM, not EISDIR 3053 if (err === ERRNO_CODES.EISDIR) err = ERRNO_CODES.EPERM; 3054 throw new FS.ErrnoError(err); 3055 } 3056 if (!parent.node_ops.unlink) { 3057 throw new FS.ErrnoError(ERRNO_CODES.EPERM); 3058 } 3059 if (FS.isMountpoint(node)) { 3060 throw new FS.ErrnoError(ERRNO_CODES.EBUSY); 3061 } 3062 parent.node_ops.unlink(parent, name); 3063 FS.destroyNode(node); 3064 },readlink:function (path) { 3065 var lookup = FS.lookupPath(path); 3066 var link = lookup.node; 3067 if (!link.node_ops.readlink) { 3068 throw new FS.ErrnoError(ERRNO_CODES.EINVAL); 3069 } 3070 return link.node_ops.readlink(link); 3071 },stat:function (path, dontFollow) { 3072 var lookup = FS.lookupPath(path, { follow: !dontFollow }); 3073 var node = lookup.node; 3074 if (!node.node_ops.getattr) { 3075 throw new FS.ErrnoError(ERRNO_CODES.EPERM); 3076 } 3077 return node.node_ops.getattr(node); 3078 },lstat:function (path) { 3079 return FS.stat(path, true); 3080 },chmod:function (path, mode, dontFollow) { 3081 var node; 3082 if (typeof path === 'string') { 3083 var lookup = FS.lookupPath(path, { follow: !dontFollow }); 3084 node = lookup.node; 3085 } else { 3086 node = path; 3087 } 3088 if (!node.node_ops.setattr) { 3089 throw new FS.ErrnoError(ERRNO_CODES.EPERM); 3090 } 3091 node.node_ops.setattr(node, { 3092 mode: (mode & 4095) | (node.mode & ~4095), 3093 timestamp: Date.now() 3094 }); 3095 },lchmod:function (path, mode) { 3096 FS.chmod(path, mode, true); 3097 },fchmod:function (fd, mode) { 3098 var stream = FS.getStream(fd); 3099 if (!stream) { 3100 throw new FS.ErrnoError(ERRNO_CODES.EBADF); 3101 } 3102 FS.chmod(stream.node, mode); 3103 },chown:function (path, uid, gid, dontFollow) { 3104 var node; 3105 if (typeof path === 'string') { 3106 var lookup = FS.lookupPath(path, { follow: !dontFollow }); 3107 node = lookup.node; 3108 } else { 3109 node = path; 3110 } 3111 if (!node.node_ops.setattr) { 3112 throw new FS.ErrnoError(ERRNO_CODES.EPERM); 3113 } 3114 node.node_ops.setattr(node, { 3115 timestamp: Date.now() 3116 // we ignore the uid / gid for now 3117 }); 3118 },lchown:function (path, uid, gid) { 3119 FS.chown(path, uid, gid, true); 3120 },fchown:function (fd, uid, gid) { 3121 var stream = FS.getStream(fd); 3122 if (!stream) { 3123 throw new FS.ErrnoError(ERRNO_CODES.EBADF); 3124 } 3125 FS.chown(stream.node, uid, gid); 3126 },truncate:function (path, len) { 3127 if (len < 0) { 3128 throw new FS.ErrnoError(ERRNO_CODES.EINVAL); 3129 } 3130 var node; 3131 if (typeof path === 'string') { 3132 var lookup = FS.lookupPath(path, { follow: true }); 3133 node = lookup.node; 3134 } else { 3135 node = path; 3136 } 3137 if (!node.node_ops.setattr) { 3138 throw new FS.ErrnoError(ERRNO_CODES.EPERM); 3139 } 3140 if (FS.isDir(node.mode)) { 3141 throw new FS.ErrnoError(ERRNO_CODES.EISDIR); 3142 } 3143 if (!FS.isFile(node.mode)) { 3144 throw new FS.ErrnoError(ERRNO_CODES.EINVAL); 3145 } 3146 var err = FS.nodePermissions(node, 'w'); 3147 if (err) { 3148 throw new FS.ErrnoError(err); 3149 } 3150 node.node_ops.setattr(node, { 3151 size: len, 3152 timestamp: Date.now() 3153 }); 3154 },ftruncate:function (fd, len) { 3155 var stream = FS.getStream(fd); 3156 if (!stream) { 3157 throw new FS.ErrnoError(ERRNO_CODES.EBADF); 3158 } 3159 if ((stream.flags & 2097155) === 0) { 3160 throw new FS.ErrnoError(ERRNO_CODES.EINVAL); 3161 } 3162 FS.truncate(stream.node, len); 3163 },utime:function (path, atime, mtime) { 3164 var lookup = FS.lookupPath(path, { follow: true }); 3165 var node = lookup.node; 3166 node.node_ops.setattr(node, { 3167 timestamp: Math.max(atime, mtime) 3168 }); 3169 },open:function (path, flags, mode, fd_start, fd_end) { 3170 flags = typeof flags === 'string' ? FS.modeStringToFlags(flags) : flags; 3171 mode = typeof mode === 'undefined' ? 438 /* 0666 */ : mode; 3172 if ((flags & 64)) { 3173 mode = (mode & 4095) | 32768; 3174 } else { 3175 mode = 0; 3176 } 3177 var node; 3178 if (typeof path === 'object') { 3179 node = path; 3180 } else { 3181 path = PATH.normalize(path); 3182 try { 3183 var lookup = FS.lookupPath(path, { 3184 follow: !(flags & 131072) 3185 }); 3186 node = lookup.node; 3187 } catch (e) { 3188 // ignore 3189 } 3190 } 3191 // perhaps we need to create the node 3192 if ((flags & 64)) { 3193 if (node) { 3194 // if O_CREAT and O_EXCL are set, error out if the node already exists 3195 if ((flags & 128)) { 3196 throw new FS.ErrnoError(ERRNO_CODES.EEXIST); 3197 } 3198 } else { 3199 // node doesn't exist, try to create it 3200 node = FS.mknod(path, mode, 0); 3201 } 3202 } 3203 if (!node) { 3204 throw new FS.ErrnoError(ERRNO_CODES.ENOENT); 3205 } 3206 // can't truncate a device 3207 if (FS.isChrdev(node.mode)) { 3208 flags &= ~512; 3209 } 3210 // check permissions 3211 var err = FS.mayOpen(node, flags); 3212 if (err) { 3213 throw new FS.ErrnoError(err); 3214 } 3215 // do truncation if necessary 3216 if ((flags & 512)) { 3217 FS.truncate(node, 0); 3218 } 3219 // we've already handled these, don't pass down to the underlying vfs 3220 flags &= ~(128 | 512); 3221 3222 // register the stream with the filesystem 3223 var stream = FS.createStream({ 3224 node: node, 3225 path: FS.getPath(node), // we want the absolute path to the node 3226 flags: flags, 3227 seekable: true, 3228 position: 0, 3229 stream_ops: node.stream_ops, 3230 // used by the file family libc calls (fopen, fwrite, ferror, etc.) 3231 ungotten: [], 3232 error: false 3233 }, fd_start, fd_end); 3234 // call the new stream's open function 3235 if (stream.stream_ops.open) { 3236 stream.stream_ops.open(stream); 3237 } 3238 if (Module['logReadFiles'] && !(flags & 1)) { 3239 if (!FS.readFiles) FS.readFiles = {}; 3240 if (!(path in FS.readFiles)) { 3241 FS.readFiles[path] = 1; 3242 Module['printErr']('read file: ' + path); 3243 } 3244 } 3245 return stream; 3246 },close:function (stream) { 3247 try { 3248 if (stream.stream_ops.close) { 3249 stream.stream_ops.close(stream); 3250 } 3251 } catch (e) { 3252 throw e; 3253 } finally { 3254 FS.closeStream(stream.fd); 3255 } 3256 },llseek:function (stream, offset, whence) { 3257 if (!stream.seekable || !stream.stream_ops.llseek) { 3258 throw new FS.ErrnoError(ERRNO_CODES.ESPIPE); 3259 } 3260 return stream.stream_ops.llseek(stream, offset, whence); 3261 },read:function (stream, buffer, offset, length, position) { 3262 if (length < 0 || position < 0) { 3263 throw new FS.ErrnoError(ERRNO_CODES.EINVAL); 3264 } 3265 if ((stream.flags & 2097155) === 1) { 3266 throw new FS.ErrnoError(ERRNO_CODES.EBADF); 3267 } 3268 if (FS.isDir(stream.node.mode)) { 3269 throw new FS.ErrnoError(ERRNO_CODES.EISDIR); 3270 } 3271 if (!stream.stream_ops.read) { 3272 throw new FS.ErrnoError(ERRNO_CODES.EINVAL); 3273 } 3274 var seeking = true; 3275 if (typeof position === 'undefined') { 3276 position = stream.position; 3277 seeking = false; 3278 } else if (!stream.seekable) { 3279 throw new FS.ErrnoError(ERRNO_CODES.ESPIPE); 3280 } 3281 var bytesRead = stream.stream_ops.read(stream, buffer, offset, length, position); 3282 if (!seeking) stream.position += bytesRead; 3283 return bytesRead; 3284 },write:function (stream, buffer, offset, length, position, canOwn) { 3285 if (length < 0 || position < 0) { 3286 throw new FS.ErrnoError(ERRNO_CODES.EINVAL); 3287 } 3288 if ((stream.flags & 2097155) === 0) { 3289 throw new FS.ErrnoError(ERRNO_CODES.EBADF); 3290 } 3291 if (FS.isDir(stream.node.mode)) { 3292 throw new FS.ErrnoError(ERRNO_CODES.EISDIR); 3293 } 3294 if (!stream.stream_ops.write) { 3295 throw new FS.ErrnoError(ERRNO_CODES.EINVAL); 3296 } 3297 var seeking = true; 3298 if (typeof position === 'undefined') { 3299 position = stream.position; 3300 seeking = false; 3301 } else if (!stream.seekable) { 3302 throw new FS.ErrnoError(ERRNO_CODES.ESPIPE); 3303 } 3304 if (stream.flags & 1024) { 3305 // seek to the end before writing in append mode 3306 FS.llseek(stream, 0, 2); 3307 } 3308 var bytesWritten = stream.stream_ops.write(stream, buffer, offset, length, position, canOwn); 3309 if (!seeking) stream.position += bytesWritten; 3310 return bytesWritten; 3311 },allocate:function (stream, offset, length) { 3312 if (offset < 0 || length <= 0) { 3313 throw new FS.ErrnoError(ERRNO_CODES.EINVAL); 3314 } 3315 if ((stream.flags & 2097155) === 0) { 3316 throw new FS.ErrnoError(ERRNO_CODES.EBADF); 3317 } 3318 if (!FS.isFile(stream.node.mode) && !FS.isDir(node.mode)) { 3319 throw new FS.ErrnoError(ERRNO_CODES.ENODEV); 3320 } 3321 if (!stream.stream_ops.allocate) { 3322 throw new FS.ErrnoError(ERRNO_CODES.EOPNOTSUPP); 3323 } 3324 stream.stream_ops.allocate(stream, offset, length); 3325 },mmap:function (stream, buffer, offset, length, position, prot, flags) { 3326 // TODO if PROT is PROT_WRITE, make sure we have write access 3327 if ((stream.flags & 2097155) === 1) { 3328 throw new FS.ErrnoError(ERRNO_CODES.EACCES); 3329 } 3330 if (!stream.stream_ops.mmap) { 3331 throw new FS.ErrnoError(ERRNO_CODES.ENODEV); 3332 } 3333 return stream.stream_ops.mmap(stream, buffer, offset, length, position, prot, flags); 3334 },ioctl:function (stream, cmd, arg) { 3335 if (!stream.stream_ops.ioctl) { 3336 throw new FS.ErrnoError(ERRNO_CODES.ENOTTY); 3337 } 3338 return stream.stream_ops.ioctl(stream, cmd, arg); 3339 },readFile:function (path, opts) { 3340 opts = opts || {}; 3341 opts.flags = opts.flags || 'r'; 3342 opts.encoding = opts.encoding || 'binary'; 3343 if (opts.encoding !== 'utf8' && opts.encoding !== 'binary') { 3344 throw new Error('Invalid encoding type "' + opts.encoding + '"'); 3345 } 3346 var ret; 3347 var stream = FS.open(path, opts.flags); 3348 var stat = FS.stat(path); 3349 var length = stat.size; 3350 var buf = new Uint8Array(length); 3351 FS.read(stream, buf, 0, length, 0); 3352 if (opts.encoding === 'utf8') { 3353 ret = ''; 3354 var utf8 = new Runtime.UTF8Processor(); 3355 for (var i = 0; i < length; i++) { 3356 ret += utf8.processCChar(buf[i]); 3357 } 3358 } else if (opts.encoding === 'binary') { 3359 ret = buf; 3360 } 3361 FS.close(stream); 3362 return ret; 3363 },writeFile:function (path, data, opts) { 3364 opts = opts || {}; 3365 opts.flags = opts.flags || 'w'; 3366 opts.encoding = opts.encoding || 'utf8'; 3367 if (opts.encoding !== 'utf8' && opts.encoding !== 'binary') { 3368 throw new Error('Invalid encoding type "' + opts.encoding + '"'); 3369 } 3370 var stream = FS.open(path, opts.flags, opts.mode); 3371 if (opts.encoding === 'utf8') { 3372 var utf8 = new Runtime.UTF8Processor(); 3373 var buf = new Uint8Array(utf8.processJSString(data)); 3374 FS.write(stream, buf, 0, buf.length, 0, opts.canOwn); 3375 } else if (opts.encoding === 'binary') { 3376 FS.write(stream, data, 0, data.length, 0, opts.canOwn); 3377 } 3378 FS.close(stream); 3379 },cwd:function () { 3380 return FS.currentPath; 3381 },chdir:function (path) { 3382 var lookup = FS.lookupPath(path, { follow: true }); 3383 if (!FS.isDir(lookup.node.mode)) { 3384 throw new FS.ErrnoError(ERRNO_CODES.ENOTDIR); 3385 } 3386 var err = FS.nodePermissions(lookup.node, 'x'); 3387 if (err) { 3388 throw new FS.ErrnoError(err); 3389 } 3390 FS.currentPath = lookup.path; 3391 },createDefaultDirectories:function () { 3392 FS.mkdir('/tmp'); 3393 },createDefaultDevices:function () { 3394 // create /dev 3395 FS.mkdir('/dev'); 3396 // setup /dev/null 3397 FS.registerDevice(FS.makedev(1, 3), { 3398 read: function() { return 0; }, 3399 write: function() { return 0; } 3400 }); 3401 FS.mkdev('/dev/null', FS.makedev(1, 3)); 3402 // setup /dev/tty and /dev/tty1 3403 // stderr needs to print output using Module['printErr'] 3404 // so we register a second tty just for it. 3405 TTY.register(FS.makedev(5, 0), TTY.default_tty_ops); 3406 TTY.register(FS.makedev(6, 0), TTY.default_tty1_ops); 3407 FS.mkdev('/dev/tty', FS.makedev(5, 0)); 3408 FS.mkdev('/dev/tty1', FS.makedev(6, 0)); 3409 // we're not going to emulate the actual shm device, 3410 // just create the tmp dirs that reside in it commonly 3411 FS.mkdir('/dev/shm'); 3412 FS.mkdir('/dev/shm/tmp'); 3413 },createStandardStreams:function () { 3414 // TODO deprecate the old functionality of a single 3415 // input / output callback and that utilizes FS.createDevice 3416 // and instead require a unique set of stream ops 3417 3418 // by default, we symlink the standard streams to the 3419 // default tty devices. however, if the standard streams 3420 // have been overwritten we create a unique device for 3421 // them instead. 3422 if (Module['stdin']) { 3423 FS.createDevice('/dev', 'stdin', Module['stdin']); 3424 } else { 3425 FS.symlink('/dev/tty', '/dev/stdin'); 3426 } 3427 if (Module['stdout']) { 3428 FS.createDevice('/dev', 'stdout', null, Module['stdout']); 3429 } else { 3430 FS.symlink('/dev/tty', '/dev/stdout'); 3431 } 3432 if (Module['stderr']) { 3433 FS.createDevice('/dev', 'stderr', null, Module['stderr']); 3434 } else { 3435 FS.symlink('/dev/tty1', '/dev/stderr'); 3436 } 3437 3438 // open default streams for the stdin, stdout and stderr devices 3439 var stdin = FS.open('/dev/stdin', 'r'); 3440 HEAP32[((_stdin)>>2)]=FS.getPtrForStream(stdin); 3441 assert(stdin.fd === 0, 'invalid handle for stdin (' + stdin.fd + ')'); 3442 3443 var stdout = FS.open('/dev/stdout', 'w'); 3444 HEAP32[((_stdout)>>2)]=FS.getPtrForStream(stdout); 3445 assert(stdout.fd === 1, 'invalid handle for stdout (' + stdout.fd + ')'); 3446 3447 var stderr = FS.open('/dev/stderr', 'w'); 3448 HEAP32[((_stderr)>>2)]=FS.getPtrForStream(stderr); 3449 assert(stderr.fd === 2, 'invalid handle for stderr (' + stderr.fd + ')'); 3450 },ensureErrnoError:function () { 3451 if (FS.ErrnoError) return; 3452 FS.ErrnoError = function ErrnoError(errno) { 3453 this.errno = errno; 3454 for (var key in ERRNO_CODES) { 3455 if (ERRNO_CODES[key] === errno) { 3456 this.code = key; 3457 break; 3458 } 3459 } 3460 this.message = ERRNO_MESSAGES[errno]; 3461 }; 3462 FS.ErrnoError.prototype = new Error(); 3463 FS.ErrnoError.prototype.constructor = FS.ErrnoError; 3464 // Some errors may happen quite a bit, to avoid overhead we reuse them (and suffer a lack of stack info) 3465 [ERRNO_CODES.ENOENT].forEach(function(code) { 3466 FS.genericErrors[code] = new FS.ErrnoError(code); 3467 FS.genericErrors[code].stack = '<generic error, no stack>'; 3468 }); 3469 },staticInit:function () { 3470 FS.ensureErrnoError(); 3471 3472 FS.nameTable = new Array(4096); 3473 3474 FS.mount(MEMFS, {}, '/'); 3475 3476 FS.createDefaultDirectories(); 3477 FS.createDefaultDevices(); 3478 },init:function (input, output, error) { 3479 assert(!FS.init.initialized, 'FS.init was previously called. If you want to initialize later with custom parameters, remove any earlier calls (note that one is automatically added to the generated code)'); 3480 FS.init.initialized = true; 3481 3482 FS.ensureErrnoError(); 3483 3484 // Allow Module.stdin etc. to provide defaults, if none explicitly passed to us here 3485 Module['stdin'] = input || Module['stdin']; 3486 Module['stdout'] = output || Module['stdout']; 3487 Module['stderr'] = error || Module['stderr']; 3488 3489 FS.createStandardStreams(); 3490 },quit:function () { 3491 FS.init.initialized = false; 3492 for (var i = 0; i < FS.streams.length; i++) { 3493 var stream = FS.streams[i]; 3494 if (!stream) { 3495 continue; 3496 } 3497 FS.close(stream); 3498 } 3499 },getMode:function (canRead, canWrite) { 3500 var mode = 0; 3501 if (canRead) mode |= 292 | 73; 3502 if (canWrite) mode |= 146; 3503 return mode; 3504 },joinPath:function (parts, forceRelative) { 3505 var path = PATH.join.apply(null, parts); 3506 if (forceRelative && path[0] == '/') path = path.substr(1); 3507 return path; 3508 },absolutePath:function (relative, base) { 3509 return PATH.resolve(base, relative); 3510 },standardizePath:function (path) { 3511 return PATH.normalize(path); 3512 },findObject:function (path, dontResolveLastLink) { 3513 var ret = FS.analyzePath(path, dontResolveLastLink); 3514 if (ret.exists) { 3515 return ret.object; 3516 } else { 3517 ___setErrNo(ret.error); 3518 return null; 3519 } 3520 },analyzePath:function (path, dontResolveLastLink) { 3521 // operate from within the context of the symlink's target 3522 try { 3523 var lookup = FS.lookupPath(path, { follow: !dontResolveLastLink }); 3524 path = lookup.path; 3525 } catch (e) { 3526 } 3527 var ret = { 3528 isRoot: false, exists: false, error: 0, name: null, path: null, object: null, 3529 parentExists: false, parentPath: null, parentObject: null 3530 }; 3531 try { 3532 var lookup = FS.lookupPath(path, { parent: true }); 3533 ret.parentExists = true; 3534 ret.parentPath = lookup.path; 3535 ret.parentObject = lookup.node; 3536 ret.name = PATH.basename(path); 3537 lookup = FS.lookupPath(path, { follow: !dontResolveLastLink }); 3538 ret.exists = true; 3539 ret.path = lookup.path; 3540 ret.object = lookup.node; 3541 ret.name = lookup.node.name; 3542 ret.isRoot = lookup.path === '/'; 3543 } catch (e) { 3544 ret.error = e.errno; 3545 }; 3546 return ret; 3547 },createFolder:function (parent, name, canRead, canWrite) { 3548 var path = PATH.join2(typeof parent === 'string' ? parent : FS.getPath(parent), name); 3549 var mode = FS.getMode(canRead, canWrite); 3550 return FS.mkdir(path, mode); 3551 },createPath:function (parent, path, canRead, canWrite) { 3552 parent = typeof parent === 'string' ? parent : FS.getPath(parent); 3553 var parts = path.split('/').reverse(); 3554 while (parts.length) { 3555 var part = parts.pop(); 3556 if (!part) continue; 3557 var current = PATH.join2(parent, part); 3558 try { 3559 FS.mkdir(current); 3560 } catch (e) { 3561 // ignore EEXIST 3562 } 3563 parent = current; 3564 } 3565 return current; 3566 },createFile:function (parent, name, properties, canRead, canWrite) { 3567 var path = PATH.join2(typeof parent === 'string' ? parent : FS.getPath(parent), name); 3568 var mode = FS.getMode(canRead, canWrite); 3569 return FS.create(path, mode); 3570 },createDataFile:function (parent, name, data, canRead, canWrite, canOwn) { 3571 var path = name ? PATH.join2(typeof parent === 'string' ? parent : FS.getPath(parent), name) : parent; 3572 var mode = FS.getMode(canRead, canWrite); 3573 var node = FS.create(path, mode); 3574 if (data) { 3575 if (typeof data === 'string') { 3576 var arr = new Array(data.length); 3577 for (var i = 0, len = data.length; i < len; ++i) arr[i] = data.charCodeAt(i); 3578 data = arr; 3579 } 3580 // make sure we can write to the file 3581 FS.chmod(node, mode | 146); 3582 var stream = FS.open(node, 'w'); 3583 FS.write(stream, data, 0, data.length, 0, canOwn); 3584 FS.close(stream); 3585 FS.chmod(node, mode); 3586 } 3587 return node; 3588 },createDevice:function (parent, name, input, output) { 3589 var path = PATH.join2(typeof parent === 'string' ? parent : FS.getPath(parent), name); 3590 var mode = FS.getMode(!!input, !!output); 3591 if (!FS.createDevice.major) FS.createDevice.major = 64; 3592 var dev = FS.makedev(FS.createDevice.major++, 0); 3593 // Create a fake device that a set of stream ops to emulate 3594 // the old behavior. 3595 FS.registerDevice(dev, { 3596 open: function(stream) { 3597 stream.seekable = false; 3598 }, 3599 close: function(stream) { 3600 // flush any pending line data 3601 if (output && output.buffer && output.buffer.length) { 3602 output(10); 3603 } 3604 }, 3605 read: function(stream, buffer, offset, length, pos /* ignored */) { 3606 var bytesRead = 0; 3607 for (var i = 0; i < length; i++) { 3608 var result; 3609 try { 3610 result = input(); 3611 } catch (e) { 3612 throw new FS.ErrnoError(ERRNO_CODES.EIO); 3613 } 3614 if (result === undefined && bytesRead === 0) { 3615 throw new FS.ErrnoError(ERRNO_CODES.EAGAIN); 3616 } 3617 if (result === null || result === undefined) break; 3618 bytesRead++; 3619 buffer[offset+i] = result; 3620 } 3621 if (bytesRead) { 3622 stream.node.timestamp = Date.now(); 3623 } 3624 return bytesRead; 3625 }, 3626 write: function(stream, buffer, offset, length, pos) { 3627 for (var i = 0; i < length; i++) { 3628 try { 3629 output(buffer[offset+i]); 3630 } catch (e) { 3631 throw new FS.ErrnoError(ERRNO_CODES.EIO); 3632 } 3633 } 3634 if (length) { 3635 stream.node.timestamp = Date.now(); 3636 } 3637 return i; 3638 } 3639 }); 3640 return FS.mkdev(path, mode, dev); 3641 },createLink:function (parent, name, target, canRead, canWrite) { 3642 var path = PATH.join2(typeof parent === 'string' ? parent : FS.getPath(parent), name); 3643 return FS.symlink(target, path); 3644 },forceLoadFile:function (obj) { 3645 if (obj.isDevice || obj.isFolder || obj.link || obj.contents) return true; 3646 var success = true; 3647 if (typeof XMLHttpRequest !== 'undefined') { 3648 throw new Error("Lazy loading should have been performed (contents set) in createLazyFile, but it was not. Lazy loading only works in web workers. Use --embed-file or --preload-file in emcc on the main thread."); 3649 } else if (Module['read']) { 3650 // Command-line. 3651 try { 3652 // WARNING: Can't read binary files in V8's d8 or tracemonkey's js, as 3653 // read() will try to parse UTF8. 3654 obj.contents = intArrayFromString(Module['read'](obj.url), true); 3655 } catch (e) { 3656 success = false; 3657 } 3658 } else { 3659 throw new Error('Cannot load without read() or XMLHttpRequest.'); 3660 } 3661 if (!success) ___setErrNo(ERRNO_CODES.EIO); 3662 return success; 3663 },createLazyFile:function (parent, name, url, canRead, canWrite) { 3664 // Lazy chunked Uint8Array (implements get and length from Uint8Array). Actual getting is abstracted away for eventual reuse. 3665 function LazyUint8Array() { 3666 this.lengthKnown = false; 3667 this.chunks = []; // Loaded chunks. Index is the chunk number 3668 } 3669 LazyUint8Array.prototype.get = function LazyUint8Array_get(idx) { 3670 if (idx > this.length-1 || idx < 0) { 3671 return undefined; 3672 } 3673 var chunkOffset = idx % this.chunkSize; 3674 var chunkNum = Math.floor(idx / this.chunkSize); 3675 return this.getter(chunkNum)[chunkOffset]; 3676 } 3677 LazyUint8Array.prototype.setDataGetter = function LazyUint8Array_setDataGetter(getter) { 3678 this.getter = getter; 3679 } 3680 LazyUint8Array.prototype.cacheLength = function LazyUint8Array_cacheLength() { 3681 // Find length 3682 var xhr = new XMLHttpRequest(); 3683 xhr.open('HEAD', url, false); 3684 xhr.send(null); 3685 if (!(xhr.status >= 200 && xhr.status < 300 || xhr.status === 304)) throw new Error("Couldn't load " + url + ". Status: " + xhr.status); 3686 var datalength = Number(xhr.getResponseHeader("Content-length")); 3687 var header; 3688 var hasByteServing = (header = xhr.getResponseHeader("Accept-Ranges")) && header === "bytes"; 3689 var chunkSize = 1024*1024; // Chunk size in bytes 3690 3691 if (!hasByteServing) chunkSize = datalength; 3692 3693 // Function to get a range from the remote URL. 3694 var doXHR = (function(from, to) { 3695 if (from > to) throw new Error("invalid range (" + from + ", " + to + ") or no bytes requested!"); 3696 if (to > datalength-1) throw new Error("only " + datalength + " bytes available! programmer error!"); 3697 3698 // TODO: Use mozResponseArrayBuffer, responseStream, etc. if available. 3699 var xhr = new XMLHttpRequest(); 3700 xhr.open('GET', url, false); 3701 if (datalength !== chunkSize) xhr.setRequestHeader("Range", "bytes=" + from + "-" + to); 3702 3703 // Some hints to the browser that we want binary data. 3704 if (typeof Uint8Array != 'undefined') xhr.responseType = 'arraybuffer'; 3705 if (xhr.overrideMimeType) { 3706 xhr.overrideMimeType('text/plain; charset=x-user-defined'); 3707 } 3708 3709 xhr.send(null); 3710 if (!(xhr.status >= 200 && xhr.status < 300 || xhr.status === 304)) throw new Error("Couldn't load " + url + ". Status: " + xhr.status); 3711 if (xhr.response !== undefined) { 3712 return new Uint8Array(xhr.response || []); 3713 } else { 3714 return intArrayFromString(xhr.responseText || '', true); 3715 } 3716 }); 3717 var lazyArray = this; 3718 lazyArray.setDataGetter(function(chunkNum) { 3719 var start = chunkNum * chunkSize; 3720 var end = (chunkNum+1) * chunkSize - 1; // including this byte 3721 end = Math.min(end, datalength-1); // if datalength-1 is selected, this is the last block 3722 if (typeof(lazyArray.chunks[chunkNum]) === "undefined") { 3723 lazyArray.chunks[chunkNum] = doXHR(start, end); 3724 } 3725 if (typeof(lazyArray.chunks[chunkNum]) === "undefined") throw new Error("doXHR failed!"); 3726 return lazyArray.chunks[chunkNum]; 3727 }); 3728 3729 this._length = datalength; 3730 this._chunkSize = chunkSize; 3731 this.lengthKnown = true; 3732 } 3733 if (typeof XMLHttpRequest !== 'undefined') { 3734 if (!ENVIRONMENT_IS_WORKER) throw 'Cannot do synchronous binary XHRs outside webworkers in modern browsers. Use --embed-file or --preload-file in emcc'; 3735 var lazyArray = new LazyUint8Array(); 3736 Object.defineProperty(lazyArray, "length", { 3737 get: function() { 3738 if(!this.lengthKnown) { 3739 this.cacheLength(); 3740 } 3741 return this._length; 3742 } 3743 }); 3744 Object.defineProperty(lazyArray, "chunkSize", { 3745 get: function() { 3746 if(!this.lengthKnown) { 3747 this.cacheLength(); 3748 } 3749 return this._chunkSize; 3750 } 3751 }); 3752 3753 var properties = { isDevice: false, contents: lazyArray }; 3754 } else { 3755 var properties = { isDevice: false, url: url }; 3756 } 3757 3758 var node = FS.createFile(parent, name, properties, canRead, canWrite); 3759 // This is a total hack, but I want to get this lazy file code out of the 3760 // core of MEMFS. If we want to keep this lazy file concept I feel it should 3761 // be its own thin LAZYFS proxying calls to MEMFS. 3762 if (properties.contents) { 3763 node.contents = properties.contents; 3764 } else if (properties.url) { 3765 node.contents = null; 3766 node.url = properties.url; 3767 } 3768 // override each stream op with one that tries to force load the lazy file first 3769 var stream_ops = {}; 3770 var keys = Object.keys(node.stream_ops); 3771 keys.forEach(function(key) { 3772 var fn = node.stream_ops[key]; 3773 stream_ops[key] = function forceLoadLazyFile() { 3774 if (!FS.forceLoadFile(node)) { 3775 throw new FS.ErrnoError(ERRNO_CODES.EIO); 3776 } 3777 return fn.apply(null, arguments); 3778 }; 3779 }); 3780 // use a custom read function 3781 stream_ops.read = function stream_ops_read(stream, buffer, offset, length, position) { 3782 if (!FS.forceLoadFile(node)) { 3783 throw new FS.ErrnoError(ERRNO_CODES.EIO); 3784 } 3785 var contents = stream.node.contents; 3786 if (position >= contents.length) 3787 return 0; 3788 var size = Math.min(contents.length - position, length); 3789 assert(size >= 0); 3790 if (contents.slice) { // normal array 3791 for (var i = 0; i < size; i++) { 3792 buffer[offset + i] = contents[position + i]; 3793 } 3794 } else { 3795 for (var i = 0; i < size; i++) { // LazyUint8Array from sync binary XHR 3796 buffer[offset + i] = contents.get(position + i); 3797 } 3798 } 3799 return size; 3800 }; 3801 node.stream_ops = stream_ops; 3802 return node; 3803 },createPreloadedFile:function (parent, name, url, canRead, canWrite, onload, onerror, dontCreateFile, canOwn) { 3804 Browser.init(); 3805 // TODO we should allow people to just pass in a complete filename instead 3806 // of parent and name being that we just join them anyways 3807 var fullname = name ? PATH.resolve(PATH.join2(parent, name)) : parent; 3808 function processData(byteArray) { 3809 function finish(byteArray) { 3810 if (!dontCreateFile) { 3811 FS.createDataFile(parent, name, byteArray, canRead, canWrite, canOwn); 3812 } 3813 if (onload) onload(); 3814 removeRunDependency('cp ' + fullname); 3815 } 3816 var handled = false; 3817 Module['preloadPlugins'].forEach(function(plugin) { 3818 if (handled) return; 3819 if (plugin['canHandle'](fullname)) { 3820 plugin['handle'](byteArray, fullname, finish, function() { 3821 if (onerror) onerror(); 3822 removeRunDependency('cp ' + fullname); 3823 }); 3824 handled = true; 3825 } 3826 }); 3827 if (!handled) finish(byteArray); 3828 } 3829 addRunDependency('cp ' + fullname); 3830 if (typeof url == 'string') { 3831 Browser.asyncLoad(url, function(byteArray) { 3832 processData(byteArray); 3833 }, onerror); 3834 } else { 3835 processData(url); 3836 } 3837 },indexedDB:function () { 3838 return window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB; 3839 },DB_NAME:function () { 3840 return 'EM_FS_' + window.location.pathname; 3841 },DB_VERSION:20,DB_STORE_NAME:"FILE_DATA",saveFilesToDB:function (paths, onload, onerror) { 3842 onload = onload || function(){}; 3843 onerror = onerror || function(){}; 3844 var indexedDB = FS.indexedDB(); 3845 try { 3846 var openRequest = indexedDB.open(FS.DB_NAME(), FS.DB_VERSION); 3847 } catch (e) { 3848 return onerror(e); 3849 } 3850 openRequest.onupgradeneeded = function openRequest_onupgradeneeded() { 3851 console.log('creating db'); 3852 var db = openRequest.result; 3853 db.createObjectStore(FS.DB_STORE_NAME); 3854 }; 3855 openRequest.onsuccess = function openRequest_onsuccess() { 3856 var db = openRequest.result; 3857 var transaction = db.transaction([FS.DB_STORE_NAME], 'readwrite'); 3858 var files = transaction.objectStore(FS.DB_STORE_NAME); 3859 var ok = 0, fail = 0, total = paths.length; 3860 function finish() { 3861 if (fail == 0) onload(); else onerror(); 3862 } 3863 paths.forEach(function(path) { 3864 var putRequest = files.put(FS.analyzePath(path).object.contents, path); 3865 putRequest.onsuccess = function putRequest_onsuccess() { ok++; if (ok + fail == total) finish() }; 3866 putRequest.onerror = function putRequest_onerror() { fail++; if (ok + fail == total) finish() }; 3867 }); 3868 transaction.onerror = onerror; 3869 }; 3870 openRequest.onerror = onerror; 3871 },loadFilesFromDB:function (paths, onload, onerror) { 3872 onload = onload || function(){}; 3873 onerror = onerror || function(){}; 3874 var indexedDB = FS.indexedDB(); 3875 try { 3876 var openRequest = indexedDB.open(FS.DB_NAME(), FS.DB_VERSION); 3877 } catch (e) { 3878 return onerror(e); 3879 } 3880 openRequest.onupgradeneeded = onerror; // no database to load from 3881 openRequest.onsuccess = function openRequest_onsuccess() { 3882 var db = openRequest.result; 3883 try { 3884 var transaction = db.transaction([FS.DB_STORE_NAME], 'readonly'); 3885 } catch(e) { 3886 onerror(e); 3887 return; 3888 } 3889 var files = transaction.objectStore(FS.DB_STORE_NAME); 3890 var ok = 0, fail = 0, total = paths.length; 3891 function finish() { 3892 if (fail == 0) onload(); else onerror(); 3893 } 3894 paths.forEach(function(path) { 3895 var getRequest = files.get(path); 3896 getRequest.onsuccess = function getRequest_onsuccess() { 3897 if (FS.analyzePath(path).exists) { 3898 FS.unlink(path); 3899 } 3900 FS.createDataFile(PATH.dirname(path), PATH.basename(path), getRequest.result, true, true, true); 3901 ok++; 3902 if (ok + fail == total) finish(); 3903 }; 3904 getRequest.onerror = function getRequest_onerror() { fail++; if (ok + fail == total) finish() }; 3905 }); 3906 transaction.onerror = onerror; 3907 }; 3908 openRequest.onerror = onerror; 3909 }}; 3910 3911 3912 3913 3914 function _mkport() { throw 'TODO' }var SOCKFS={mount:function (mount) { 3915 return FS.createNode(null, '/', 16384 | 511 /* 0777 */, 0); 3916 },createSocket:function (family, type, protocol) { 3917 var streaming = type == 1; 3918 if (protocol) { 3919 assert(streaming == (protocol == 6)); // if SOCK_STREAM, must be tcp 3920 } 3921 3922 // create our internal socket structure 3923 var sock = { 3924 family: family, 3925 type: type, 3926 protocol: protocol, 3927 server: null, 3928 peers: {}, 3929 pending: [], 3930 recv_queue: [], 3931 sock_ops: SOCKFS.websocket_sock_ops 3932 }; 3933 3934 // create the filesystem node to store the socket structure 3935 var name = SOCKFS.nextname(); 3936 var node = FS.createNode(SOCKFS.root, name, 49152, 0); 3937 node.sock = sock; 3938 3939 // and the wrapping stream that enables library functions such 3940 // as read and write to indirectly interact with the socket 3941 var stream = FS.createStream({ 3942 path: name, 3943 node: node, 3944 flags: FS.modeStringToFlags('r+'), 3945 seekable: false, 3946 stream_ops: SOCKFS.stream_ops 3947 }); 3948 3949 // map the new stream to the socket structure (sockets have a 1:1 3950 // relationship with a stream) 3951 sock.stream = stream; 3952 3953 return sock; 3954 },getSocket:function (fd) { 3955 var stream = FS.getStream(fd); 3956 if (!stream || !FS.isSocket(stream.node.mode)) { 3957 return null; 3958 } 3959 return stream.node.sock; 3960 },stream_ops:{poll:function (stream) { 3961 var sock = stream.node.sock; 3962 return sock.sock_ops.poll(sock); 3963 },ioctl:function (stream, request, varargs) { 3964 var sock = stream.node.sock; 3965 return sock.sock_ops.ioctl(sock, request, varargs); 3966 },read:function (stream, buffer, offset, length, position /* ignored */) { 3967 var sock = stream.node.sock; 3968 var msg = sock.sock_ops.recvmsg(sock, length); 3969 if (!msg) { 3970 // socket is closed 3971 return 0; 3972 } 3973 buffer.set(msg.buffer, offset); 3974 return msg.buffer.length; 3975 },write:function (stream, buffer, offset, length, position /* ignored */) { 3976 var sock = stream.node.sock; 3977 return sock.sock_ops.sendmsg(sock, buffer, offset, length); 3978 },close:function (stream) { 3979 var sock = stream.node.sock; 3980 sock.sock_ops.close(sock); 3981 }},nextname:function () { 3982 if (!SOCKFS.nextname.current) { 3983 SOCKFS.nextname.current = 0; 3984 } 3985 return 'socket[' + (SOCKFS.nextname.current++) + ']'; 3986 },websocket_sock_ops:{createPeer:function (sock, addr, port) { 3987 var ws; 3988 3989 if (typeof addr === 'object') { 3990 ws = addr; 3991 addr = null; 3992 port = null; 3993 } 3994 3995 if (ws) { 3996 // for sockets that've already connected (e.g. we're the server) 3997 // we can inspect the _socket property for the address 3998 if (ws._socket) { 3999 addr = ws._socket.remoteAddress; 4000 port = ws._socket.remotePort; 4001 } 4002 // if we're just now initializing a connection to the remote, 4003 // inspect the url property 4004 else { 4005 var result = /ws[s]?:\/\/([^:]+):(\d+)/.exec(ws.url); 4006 if (!result) { 4007 throw new Error('WebSocket URL must be in the format ws(s)://address:port'); 4008 } 4009 addr = result[1]; 4010 port = parseInt(result[2], 10); 4011 } 4012 } else { 4013 // create the actual websocket object and connect 4014 try { 4015 // runtimeConfig gets set to true if WebSocket runtime configuration is available. 4016 var runtimeConfig = (Module['websocket'] && ('object' === typeof Module['websocket'])); 4017 4018 // The default value is 'ws://' the replace is needed because the compiler replaces "//" comments with '#' 4019 // comments without checking context, so we'd end up with ws:#, the replace swaps the "#" for "//" again. 4020 var url = 'ws:#'.replace('#', '//'); 4021 4022 if (runtimeConfig) { 4023 if ('string' === typeof Module['websocket']['url']) { 4024 url = Module['websocket']['url']; // Fetch runtime WebSocket URL config. 4025 } 4026 } 4027 4028 if (url === 'ws://' || url === 'wss://') { // Is the supplied URL config just a prefix, if so complete it. 4029 url = url + addr + ':' + port; 4030 } 4031 4032 // Make the WebSocket subprotocol (Sec-WebSocket-Protocol) default to binary if no configuration is set. 4033 var subProtocols = 'binary'; // The default value is 'binary' 4034 4035 if (runtimeConfig) { 4036 if ('string' === typeof Module['websocket']['subprotocol']) { 4037 subProtocols = Module['websocket']['subprotocol']; // Fetch runtime WebSocket subprotocol config. 4038 } 4039 } 4040 4041 // The regex trims the string (removes spaces at the beginning and end, then splits the string by 4042 // <any space>,<any space> into an Array. Whitespace removal is important for Websockify and ws. 4043 subProtocols = subProtocols.replace(/^ +| +$/g,"").split(/ *, */); 4044 4045 // The node ws library API for specifying optional subprotocol is slightly different than the browser's. 4046 var opts = ENVIRONMENT_IS_NODE ? {'protocol': subProtocols.toString()} : subProtocols; 4047 4048 // If node we use the ws library. 4049 var WebSocket = ENVIRONMENT_IS_NODE ? require('ws') : window['WebSocket']; 4050 ws = new WebSocket(url, opts); 4051 ws.binaryType = 'arraybuffer'; 4052 } catch (e) { 4053 throw new FS.ErrnoError(ERRNO_CODES.EHOSTUNREACH); 4054 } 4055 } 4056 4057 4058 var peer = { 4059 addr: addr, 4060 port: port, 4061 socket: ws, 4062 dgram_send_queue: [] 4063 }; 4064 4065 SOCKFS.websocket_sock_ops.addPeer(sock, peer); 4066 SOCKFS.websocket_sock_ops.handlePeerEvents(sock, peer); 4067 4068 // if this is a bound dgram socket, send the port number first to allow 4069 // us to override the ephemeral port reported to us by remotePort on the 4070 // remote end. 4071 if (sock.type === 2 && typeof sock.sport !== 'undefined') { 4072 peer.dgram_send_queue.push(new Uint8Array([ 4073 255, 255, 255, 255, 4074 'p'.charCodeAt(0), 'o'.charCodeAt(0), 'r'.charCodeAt(0), 't'.charCodeAt(0), 4075 ((sock.sport & 0xff00) >> 8) , (sock.sport & 0xff) 4076 ])); 4077 } 4078 4079 return peer; 4080 },getPeer:function (sock, addr, port) { 4081 return sock.peers[addr + ':' + port]; 4082 },addPeer:function (sock, peer) { 4083 sock.peers[peer.addr + ':' + peer.port] = peer; 4084 },removePeer:function (sock, peer) { 4085 delete sock.peers[peer.addr + ':' + peer.port]; 4086 },handlePeerEvents:function (sock, peer) { 4087 var first = true; 4088 4089 var handleOpen = function () { 4090 try { 4091 var queued = peer.dgram_send_queue.shift(); 4092 while (queued) { 4093 peer.socket.send(queued); 4094 queued = peer.dgram_send_queue.shift(); 4095 } 4096 } catch (e) { 4097 // not much we can do here in the way of proper error handling as we've already 4098 // lied and said this data was sent. shut it down. 4099 peer.socket.close(); 4100 } 4101 }; 4102 4103 function handleMessage(data) { 4104 assert(typeof data !== 'string' && data.byteLength !== undefined); // must receive an ArrayBuffer 4105 data = new Uint8Array(data); // make a typed array view on the array buffer 4106 4107 4108 // if this is the port message, override the peer's port with it 4109 var wasfirst = first; 4110 first = false; 4111 if (wasfirst && 4112 data.length === 10 && 4113 data[0] === 255 && data[1] === 255 && data[2] === 255 && data[3] === 255 && 4114 data[4] === 'p'.charCodeAt(0) && data[5] === 'o'.charCodeAt(0) && data[6] === 'r'.charCodeAt(0) && data[7] === 't'.charCodeAt(0)) { 4115 // update the peer's port and it's key in the peer map 4116 var newport = ((data[8] << 8) | data[9]); 4117 SOCKFS.websocket_sock_ops.removePeer(sock, peer); 4118 peer.port = newport; 4119 SOCKFS.websocket_sock_ops.addPeer(sock, peer); 4120 return; 4121 } 4122 4123 sock.recv_queue.push({ addr: peer.addr, port: peer.port, data: data }); 4124 }; 4125 4126 if (ENVIRONMENT_IS_NODE) { 4127 peer.socket.on('open', handleOpen); 4128 peer.socket.on('message', function(data, flags) { 4129 if (!flags.binary) { 4130 return; 4131 } 4132 handleMessage((new Uint8Array(data)).buffer); // copy from node Buffer -> ArrayBuffer 4133 }); 4134 peer.socket.on('error', function() { 4135 // don't throw 4136 }); 4137 } else { 4138 peer.socket.onopen = handleOpen; 4139 peer.socket.onmessage = function peer_socket_onmessage(event) { 4140 handleMessage(event.data); 4141 }; 4142 } 4143 },poll:function (sock) { 4144 if (sock.type === 1 && sock.server) { 4145 // listen sockets should only say they're available for reading 4146 // if there are pending clients. 4147 return sock.pending.length ? (64 | 1) : 0; 4148 } 4149 4150 var mask = 0; 4151 var dest = sock.type === 1 ? // we only care about the socket state for connection-based sockets 4152 SOCKFS.websocket_sock_ops.getPeer(sock, sock.daddr, sock.dport) : 4153 null; 4154 4155 if (sock.recv_queue.length || 4156 !dest || // connection-less sockets are always ready to read 4157 (dest && dest.socket.readyState === dest.socket.CLOSING) || 4158 (dest && dest.socket.readyState === dest.socket.CLOSED)) { // let recv return 0 once closed 4159 mask |= (64 | 1); 4160 } 4161 4162 if (!dest || // connection-less sockets are always ready to write 4163 (dest && dest.socket.readyState === dest.socket.OPEN)) { 4164 mask |= 4; 4165 } 4166 4167 if ((dest && dest.socket.readyState === dest.socket.CLOSING) || 4168 (dest && dest.socket.readyState === dest.socket.CLOSED)) { 4169 mask |= 16; 4170 } 4171 4172 return mask; 4173 },ioctl:function (sock, request, arg) { 4174 switch (request) { 4175 case 21531: 4176 var bytes = 0; 4177 if (sock.recv_queue.length) { 4178 bytes = sock.recv_queue[0].data.length; 4179 } 4180 HEAP32[((arg)>>2)]=bytes; 4181 return 0; 4182 default: 4183 return ERRNO_CODES.EINVAL; 4184 } 4185 },close:function (sock) { 4186 // if we've spawned a listen server, close it 4187 if (sock.server) { 4188 try { 4189 sock.server.close(); 4190 } catch (e) { 4191 } 4192 sock.server = null; 4193 } 4194 // close any peer connections 4195 var peers = Object.keys(sock.peers); 4196 for (var i = 0; i < peers.length; i++) { 4197 var peer = sock.peers[peers[i]]; 4198 try { 4199 peer.socket.close(); 4200 } catch (e) { 4201 } 4202 SOCKFS.websocket_sock_ops.removePeer(sock, peer); 4203 } 4204 return 0; 4205 },bind:function (sock, addr, port) { 4206 if (typeof sock.saddr !== 'undefined' || typeof sock.sport !== 'undefined') { 4207 throw new FS.ErrnoError(ERRNO_CODES.EINVAL); // already bound 4208 } 4209 sock.saddr = addr; 4210 sock.sport = port || _mkport(); 4211 // in order to emulate dgram sockets, we need to launch a listen server when 4212 // binding on a connection-less socket 4213 // note: this is only required on the server side 4214 if (sock.type === 2) { 4215 // close the existing server if it exists 4216 if (sock.server) { 4217 sock.server.close(); 4218 sock.server = null; 4219 } 4220 // swallow error operation not supported error that occurs when binding in the 4221 // browser where this isn't supported 4222 try { 4223 sock.sock_ops.listen(sock, 0); 4224 } catch (e) { 4225 if (!(e instanceof FS.ErrnoError)) throw e; 4226 if (e.errno !== ERRNO_CODES.EOPNOTSUPP) throw e; 4227 } 4228 } 4229 },connect:function (sock, addr, port) { 4230 if (sock.server) { 4231 throw new FS.ErrnoError(ERRNO_CODS.EOPNOTSUPP); 4232 } 4233 4234 // TODO autobind 4235 // if (!sock.addr && sock.type == 2) { 4236 // } 4237 4238 // early out if we're already connected / in the middle of connecting 4239 if (typeof sock.daddr !== 'undefined' && typeof sock.dport !== 'undefined') { 4240 var dest = SOCKFS.websocket_sock_ops.getPeer(sock, sock.daddr, sock.dport); 4241 if (dest) { 4242 if (dest.socket.readyState === dest.socket.CONNECTING) { 4243 throw new FS.ErrnoError(ERRNO_CODES.EALREADY); 4244 } else { 4245 throw new FS.ErrnoError(ERRNO_CODES.EISCONN); 4246 } 4247 } 4248 } 4249 4250 // add the socket to our peer list and set our 4251 // destination address / port to match 4252 var peer = SOCKFS.websocket_sock_ops.createPeer(sock, addr, port); 4253 sock.daddr = peer.addr; 4254 sock.dport = peer.port; 4255 4256 // always "fail" in non-blocking mode 4257 throw new FS.ErrnoError(ERRNO_CODES.EINPROGRESS); 4258 },listen:function (sock, backlog) { 4259 if (!ENVIRONMENT_IS_NODE) { 4260 throw new FS.ErrnoError(ERRNO_CODES.EOPNOTSUPP); 4261 } 4262 if (sock.server) { 4263 throw new FS.ErrnoError(ERRNO_CODES.EINVAL); // already listening 4264 } 4265 var WebSocketServer = require('ws').Server; 4266 var host = sock.saddr; 4267 sock.server = new WebSocketServer({ 4268 host: host, 4269 port: sock.sport 4270 // TODO support backlog 4271 }); 4272 4273 sock.server.on('connection', function(ws) { 4274 if (sock.type === 1) { 4275 var newsock = SOCKFS.createSocket(sock.family, sock.type, sock.protocol); 4276 4277 // create a peer on the new socket 4278 var peer = SOCKFS.websocket_sock_ops.createPeer(newsock, ws); 4279 newsock.daddr = peer.addr; 4280 newsock.dport = peer.port; 4281 4282 // push to queue for accept to pick up 4283 sock.pending.push(newsock); 4284 } else { 4285 // create a peer on the listen socket so calling sendto 4286 // with the listen socket and an address will resolve 4287 // to the correct client 4288 SOCKFS.websocket_sock_ops.createPeer(sock, ws); 4289 } 4290 }); 4291 sock.server.on('closed', function() { 4292 sock.server = null; 4293 }); 4294 sock.server.on('error', function() { 4295 // don't throw 4296 }); 4297 },accept:function (listensock) { 4298 if (!listensock.server) { 4299 throw new FS.ErrnoError(ERRNO_CODES.EINVAL); 4300 } 4301 var newsock = listensock.pending.shift(); 4302 newsock.stream.flags = listensock.stream.flags; 4303 return newsock; 4304 },getname:function (sock, peer) { 4305 var addr, port; 4306 if (peer) { 4307 if (sock.daddr === undefined || sock.dport === undefined) { 4308 throw new FS.ErrnoError(ERRNO_CODES.ENOTCONN); 4309 } 4310 addr = sock.daddr; 4311 port = sock.dport; 4312 } else { 4313 // TODO saddr and sport will be set for bind()'d UDP sockets, but what 4314 // should we be returning for TCP sockets that've been connect()'d? 4315 addr = sock.saddr || 0; 4316 port = sock.sport || 0; 4317 } 4318 return { addr: addr, port: port }; 4319 },sendmsg:function (sock, buffer, offset, length, addr, port) { 4320 if (sock.type === 2) { 4321 // connection-less sockets will honor the message address, 4322 // and otherwise fall back to the bound destination address 4323 if (addr === undefined || port === undefined) { 4324 addr = sock.daddr; 4325 port = sock.dport; 4326 } 4327 // if there was no address to fall back to, error out 4328 if (addr === undefined || port === undefined) { 4329 throw new FS.ErrnoError(ERRNO_CODES.EDESTADDRREQ); 4330 } 4331 } else { 4332 // connection-based sockets will only use the bound 4333 addr = sock.daddr; 4334 port = sock.dport; 4335 } 4336 4337 // find the peer for the destination address 4338 var dest = SOCKFS.websocket_sock_ops.getPeer(sock, addr, port); 4339 4340 // early out if not connected with a connection-based socket 4341 if (sock.type === 1) { 4342 if (!dest || dest.socket.readyState === dest.socket.CLOSING || dest.socket.readyState === dest.socket.CLOSED) { 4343 throw new FS.ErrnoError(ERRNO_CODES.ENOTCONN); 4344 } else if (dest.socket.readyState === dest.socket.CONNECTING) { 4345 throw new FS.ErrnoError(ERRNO_CODES.EAGAIN); 4346 } 4347 } 4348 4349 // create a copy of the incoming data to send, as the WebSocket API 4350 // doesn't work entirely with an ArrayBufferView, it'll just send 4351 // the entire underlying buffer 4352 var data; 4353 if (buffer instanceof Array || buffer instanceof ArrayBuffer) { 4354 data = buffer.slice(offset, offset + length); 4355 } else { // ArrayBufferView 4356 data = buffer.buffer.slice(buffer.byteOffset + offset, buffer.byteOffset + offset + length); 4357 } 4358 4359 // if we're emulating a connection-less dgram socket and don't have 4360 // a cached connection, queue the buffer to send upon connect and 4361 // lie, saying the data was sent now. 4362 if (sock.type === 2) { 4363 if (!dest || dest.socket.readyState !== dest.socket.OPEN) { 4364 // if we're not connected, open a new connection 4365 if (!dest || dest.socket.readyState === dest.socket.CLOSING || dest.socket.readyState === dest.socket.CLOSED) { 4366 dest = SOCKFS.websocket_sock_ops.createPeer(sock, addr, port); 4367 } 4368 dest.dgram_send_queue.push(data); 4369 return length; 4370 } 4371 } 4372 4373 try { 4374 // send the actual data 4375 dest.socket.send(data); 4376 return length; 4377 } catch (e) { 4378 throw new FS.ErrnoError(ERRNO_CODES.EINVAL); 4379 } 4380 },recvmsg:function (sock, length) { 4381 // http://pubs.opengroup.org/onlinepubs/7908799/xns/recvmsg.html 4382 if (sock.type === 1 && sock.server) { 4383 // tcp servers should not be recv()'ing on the listen socket 4384 throw new FS.ErrnoError(ERRNO_CODES.ENOTCONN); 4385 } 4386 4387 var queued = sock.recv_queue.shift(); 4388 if (!queued) { 4389 if (sock.type === 1) { 4390 var dest = SOCKFS.websocket_sock_ops.getPeer(sock, sock.daddr, sock.dport); 4391 4392 if (!dest) { 4393 // if we have a destination address but are not connected, error out 4394 throw new FS.ErrnoError(ERRNO_CODES.ENOTCONN); 4395 } 4396 else if (dest.socket.readyState === dest.socket.CLOSING || dest.socket.readyState === dest.socket.CLOSED) { 4397 // return null if the socket has closed 4398 return null; 4399 } 4400 else { 4401 // else, our socket is in a valid state but truly has nothing available 4402 throw new FS.ErrnoError(ERRNO_CODES.EAGAIN); 4403 } 4404 } else { 4405 throw new FS.ErrnoError(ERRNO_CODES.EAGAIN); 4406 } 4407 } 4408 4409 // queued.data will be an ArrayBuffer if it's unadulterated, but if it's 4410 // requeued TCP data it'll be an ArrayBufferView 4411 var queuedLength = queued.data.byteLength || queued.data.length; 4412 var queuedOffset = queued.data.byteOffset || 0; 4413 var queuedBuffer = queued.data.buffer || queued.data; 4414 var bytesRead = Math.min(length, queuedLength); 4415 var res = { 4416 buffer: new Uint8Array(queuedBuffer, queuedOffset, bytesRead), 4417 addr: queued.addr, 4418 port: queued.port 4419 }; 4420 4421 4422 // push back any unread data for TCP connections 4423 if (sock.type === 1 && bytesRead < queuedLength) { 4424 var bytesRemaining = queuedLength - bytesRead; 4425 queued.data = new Uint8Array(queuedBuffer, queuedOffset + bytesRead, bytesRemaining); 4426 sock.recv_queue.unshift(queued); 4427 } 4428 4429 return res; 4430 }}};function _send(fd, buf, len, flags) { 4431 var sock = SOCKFS.getSocket(fd); 4432 if (!sock) { 4433 ___setErrNo(ERRNO_CODES.EBADF); 4434 return -1; 4435 } 4436 // TODO honor flags 4437 return _write(fd, buf, len); 4438 } 4439 4440 function _pwrite(fildes, buf, nbyte, offset) { 4441 // ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset); 4442 // http://pubs.opengroup.org/onlinepubs/000095399/functions/write.html 4443 var stream = FS.getStream(fildes); 4444 if (!stream) { 4445 ___setErrNo(ERRNO_CODES.EBADF); 4446 return -1; 4447 } 4448 try { 4449 var slab = HEAP8; 4450 return FS.write(stream, slab, buf, nbyte, offset); 4451 } catch (e) { 4452 FS.handleFSError(e); 4453 return -1; 4454 } 4455 }function _write(fildes, buf, nbyte) { 4456 // ssize_t write(int fildes, const void *buf, size_t nbyte); 4457 // http://pubs.opengroup.org/onlinepubs/000095399/functions/write.html 4458 var stream = FS.getStream(fildes); 4459 if (!stream) { 4460 ___setErrNo(ERRNO_CODES.EBADF); 4461 return -1; 4462 } 4463 4464 4465 try { 4466 var slab = HEAP8; 4467 return FS.write(stream, slab, buf, nbyte); 4468 } catch (e) { 4469 FS.handleFSError(e); 4470 return -1; 4471 } 4472 } 4473 4474 function _fileno(stream) { 4475 // int fileno(FILE *stream); 4476 // http://pubs.opengroup.org/onlinepubs/000095399/functions/fileno.html 4477 stream = FS.getStreamFromPtr(stream); 4478 if (!stream) return -1; 4479 return stream.fd; 4480 }function _fwrite(ptr, size, nitems, stream) { 4481 // size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream); 4482 // http://pubs.opengroup.org/onlinepubs/000095399/functions/fwrite.html 4483 var bytesToWrite = nitems * size; 4484 if (bytesToWrite == 0) return 0; 4485 var fd = _fileno(stream); 4486 var bytesWritten = _write(fd, ptr, bytesToWrite); 4487 if (bytesWritten == -1) { 4488 var streamObj = FS.getStreamFromPtr(stream); 4489 if (streamObj) streamObj.error = true; 4490 return 0; 4491 } else { 4492 return Math.floor(bytesWritten / size); 4493 } 4494 } 4495 4496 4497 4498 Module["_strlen"] = _strlen; 4499 4500 function __reallyNegative(x) { 4501 return x < 0 || (x === 0 && (1/x) === -Infinity); 4502 }function __formatString(format, varargs) { 4503 var textIndex = format; 4504 var argIndex = 0; 4505 function getNextArg(type) { 4506 // NOTE: Explicitly ignoring type safety. Otherwise this fails: 4507 // int x = 4; printf("%c\n", (char)x); 4508 var ret; 4509 if (type === 'double') { 4510 ret = HEAPF64[(((varargs)+(argIndex))>>3)]; 4511 } else if (type == 'i64') { 4512 ret = [HEAP32[(((varargs)+(argIndex))>>2)], 4513 HEAP32[(((varargs)+(argIndex+4))>>2)]]; 4514 4515 } else { 4516 type = 'i32'; // varargs are always i32, i64, or double 4517 ret = HEAP32[(((varargs)+(argIndex))>>2)]; 4518 } 4519 argIndex += Runtime.getNativeFieldSize(type); 4520 return ret; 4521 } 4522 4523 var ret = []; 4524 var curr, next, currArg; 4525 while(1) { 4526 var startTextIndex = textIndex; 4527 curr = HEAP8[(textIndex)]; 4528 if (curr === 0) break; 4529 next = HEAP8[((textIndex+1)|0)]; 4530 if (curr == 37) { 4531 // Handle flags. 4532 var flagAlwaysSigned = false; 4533 var flagLeftAlign = false; 4534 var flagAlternative = false; 4535 var flagZeroPad = false; 4536 var flagPadSign = false; 4537 flagsLoop: while (1) { 4538 switch (next) { 4539 case 43: 4540 flagAlwaysSigned = true; 4541 break; 4542 case 45: 4543 flagLeftAlign = true; 4544 break; 4545 case 35: 4546 flagAlternative = true; 4547 break; 4548 case 48: 4549 if (flagZeroPad) { 4550 break flagsLoop; 4551 } else { 4552 flagZeroPad = true; 4553 break; 4554 } 4555 case 32: 4556 flagPadSign = true; 4557 break; 4558 default: 4559 break flagsLoop; 4560 } 4561 textIndex++; 4562 next = HEAP8[((textIndex+1)|0)]; 4563 } 4564 4565 // Handle width. 4566 var width = 0; 4567 if (next == 42) { 4568 width = getNextArg('i32'); 4569 textIndex++; 4570 next = HEAP8[((textIndex+1)|0)]; 4571 } else { 4572 while (next >= 48 && next <= 57) { 4573 width = width * 10 + (next - 48); 4574 textIndex++; 4575 next = HEAP8[((textIndex+1)|0)]; 4576 } 4577 } 4578 4579 // Handle precision. 4580 var precisionSet = false, precision = -1; 4581 if (next == 46) { 4582 precision = 0; 4583 precisionSet = true; 4584 textIndex++; 4585 next = HEAP8[((textIndex+1)|0)]; 4586 if (next == 42) { 4587 precision = getNextArg('i32'); 4588 textIndex++; 4589 } else { 4590 while(1) { 4591 var precisionChr = HEAP8[((textIndex+1)|0)]; 4592 if (precisionChr < 48 || 4593 precisionChr > 57) break; 4594 precision = precision * 10 + (precisionChr - 48); 4595 textIndex++; 4596 } 4597 } 4598 next = HEAP8[((textIndex+1)|0)]; 4599 } 4600 if (precision < 0) { 4601 precision = 6; // Standard default. 4602 precisionSet = false; 4603 } 4604 4605 // Handle integer sizes. WARNING: These assume a 32-bit architecture! 4606 var argSize; 4607 switch (String.fromCharCode(next)) { 4608 case 'h': 4609 var nextNext = HEAP8[((textIndex+2)|0)]; 4610 if (nextNext == 104) { 4611 textIndex++; 4612 argSize = 1; // char (actually i32 in varargs) 4613 } else { 4614 argSize = 2; // short (actually i32 in varargs) 4615 } 4616 break; 4617 case 'l': 4618 var nextNext = HEAP8[((textIndex+2)|0)]; 4619 if (nextNext == 108) { 4620 textIndex++; 4621 argSize = 8; // long long 4622 } else { 4623 argSize = 4; // long 4624 } 4625 break; 4626 case 'L': // long long 4627 case 'q': // int64_t 4628 case 'j': // intmax_t 4629 argSize = 8; 4630 break; 4631 case 'z': // size_t 4632 case 't': // ptrdiff_t 4633 case 'I': // signed ptrdiff_t or unsigned size_t 4634 argSize = 4; 4635 break; 4636 default: 4637 argSize = null; 4638 } 4639 if (argSize) textIndex++; 4640 next = HEAP8[((textIndex+1)|0)]; 4641 4642 // Handle type specifier. 4643 switch (String.fromCharCode(next)) { 4644 case 'd': case 'i': case 'u': case 'o': case 'x': case 'X': case 'p': { 4645 // Integer. 4646 var signed = next == 100 || next == 105; 4647 argSize = argSize || 4; 4648 var currArg = getNextArg('i' + (argSize * 8)); 4649 var argText; 4650 // Flatten i64-1 [low, high] into a (slightly rounded) double 4651 if (argSize == 8) { 4652 currArg = Runtime.makeBigInt(currArg[0], currArg[1], next == 117); 4653 } 4654 // Truncate to requested size. 4655 if (argSize <= 4) { 4656 var limit = Math.pow(256, argSize) - 1; 4657 currArg = (signed ? reSign : unSign)(currArg & limit, argSize * 8); 4658 } 4659 // Format the number. 4660 var currAbsArg = Math.abs(currArg); 4661 var prefix = ''; 4662 if (next == 100 || next == 105) { 4663 argText = reSign(currArg, 8 * argSize, 1).toString(10); 4664 } else if (next == 117) { 4665 argText = unSign(currArg, 8 * argSize, 1).toString(10); 4666 currArg = Math.abs(currArg); 4667 } else if (next == 111) { 4668 argText = (flagAlternative ? '0' : '') + currAbsArg.toString(8); 4669 } else if (next == 120 || next == 88) { 4670 prefix = (flagAlternative && currArg != 0) ? '0x' : ''; 4671 if (currArg < 0) { 4672 // Represent negative numbers in hex as 2's complement. 4673 currArg = -currArg; 4674 argText = (currAbsArg - 1).toString(16); 4675 var buffer = []; 4676 for (var i = 0; i < argText.length; i++) { 4677 buffer.push((0xF - parseInt(argText[i], 16)).toString(16)); 4678 } 4679 argText = buffer.join(''); 4680 while (argText.length < argSize * 2) argText = 'f' + argText; 4681 } else { 4682 argText = currAbsArg.toString(16); 4683 } 4684 if (next == 88) { 4685 prefix = prefix.toUpperCase(); 4686 argText = argText.toUpperCase(); 4687 } 4688 } else if (next == 112) { 4689 if (currAbsArg === 0) { 4690 argText = '(nil)'; 4691 } else { 4692 prefix = '0x'; 4693 argText = currAbsArg.toString(16); 4694 } 4695 } 4696 if (precisionSet) { 4697 while (argText.length < precision) { 4698 argText = '0' + argText; 4699 } 4700 } 4701 4702 // Add sign if needed 4703 if (currArg >= 0) { 4704 if (flagAlwaysSigned) { 4705 prefix = '+' + prefix; 4706 } else if (flagPadSign) { 4707 prefix = ' ' + prefix; 4708 } 4709 } 4710 4711 // Move sign to prefix so we zero-pad after the sign 4712 if (argText.charAt(0) == '-') { 4713 prefix = '-' + prefix; 4714 argText = argText.substr(1); 4715 } 4716 4717 // Add padding. 4718 while (prefix.length + argText.length < width) { 4719 if (flagLeftAlign) { 4720 argText += ' '; 4721 } else { 4722 if (flagZeroPad) { 4723 argText = '0' + argText; 4724 } else { 4725 prefix = ' ' + prefix; 4726 } 4727 } 4728 } 4729 4730 // Insert the result into the buffer. 4731 argText = prefix + argText; 4732 argText.split('').forEach(function(chr) { 4733 ret.push(chr.charCodeAt(0)); 4734 }); 4735 break; 4736 } 4737 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G': { 4738 // Float. 4739 var currArg = getNextArg('double'); 4740 var argText; 4741 if (isNaN(currArg)) { 4742 argText = 'nan'; 4743 flagZeroPad = false; 4744 } else if (!isFinite(currArg)) { 4745 argText = (currArg < 0 ? '-' : '') + 'inf'; 4746 flagZeroPad = false; 4747 } else { 4748 var isGeneral = false; 4749 var effectivePrecision = Math.min(precision, 20); 4750 4751 // Convert g/G to f/F or e/E, as per: 4752 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/printf.html 4753 if (next == 103 || next == 71) { 4754 isGeneral = true; 4755 precision = precision || 1; 4756 var exponent = parseInt(currArg.toExponential(effectivePrecision).split('e')[1], 10); 4757 if (precision > exponent && exponent >= -4) { 4758 next = ((next == 103) ? 'f' : 'F').charCodeAt(0); 4759 precision -= exponent + 1; 4760 } else { 4761 next = ((next == 103) ? 'e' : 'E').charCodeAt(0); 4762 precision--; 4763 } 4764 effectivePrecision = Math.min(precision, 20); 4765 } 4766 4767 if (next == 101 || next == 69) { 4768 argText = currArg.toExponential(effectivePrecision); 4769 // Make sure the exponent has at least 2 digits. 4770 if (/[eE][-+]\d$/.test(argText)) { 4771 argText = argText.slice(0, -1) + '0' + argText.slice(-1); 4772 } 4773 } else if (next == 102 || next == 70) { 4774 argText = currArg.toFixed(effectivePrecision); 4775 if (currArg === 0 && __reallyNegative(currArg)) { 4776 argText = '-' + argText; 4777 } 4778 } 4779 4780 var parts = argText.split('e'); 4781 if (isGeneral && !flagAlternative) { 4782 // Discard trailing zeros and periods. 4783 while (parts[0].length > 1 && parts[0].indexOf('.') != -1 && 4784 (parts[0].slice(-1) == '0' || parts[0].slice(-1) == '.')) { 4785 parts[0] = parts[0].slice(0, -1); 4786 } 4787 } else { 4788 // Make sure we have a period in alternative mode. 4789 if (flagAlternative && argText.indexOf('.') == -1) parts[0] += '.'; 4790 // Zero pad until required precision. 4791 while (precision > effectivePrecision++) parts[0] += '0'; 4792 } 4793 argText = parts[0] + (parts.length > 1 ? 'e' + parts[1] : ''); 4794 4795 // Capitalize 'E' if needed. 4796 if (next == 69) argText = argText.toUpperCase(); 4797 4798 // Add sign. 4799 if (currArg >= 0) { 4800 if (flagAlwaysSigned) { 4801 argText = '+' + argText; 4802 } else if (flagPadSign) { 4803 argText = ' ' + argText; 4804 } 4805 } 4806 } 4807 4808 // Add padding. 4809 while (argText.length < width) { 4810 if (flagLeftAlign) { 4811 argText += ' '; 4812 } else { 4813 if (flagZeroPad && (argText[0] == '-' || argText[0] == '+')) { 4814 argText = argText[0] + '0' + argText.slice(1); 4815 } else { 4816 argText = (flagZeroPad ? '0' : ' ') + argText; 4817 } 4818 } 4819 } 4820 4821 // Adjust case. 4822 if (next < 97) argText = argText.toUpperCase(); 4823 4824 // Insert the result into the buffer. 4825 argText.split('').forEach(function(chr) { 4826 ret.push(chr.charCodeAt(0)); 4827 }); 4828 break; 4829 } 4830 case 's': { 4831 // String. 4832 var arg = getNextArg('i8*'); 4833 var argLength = arg ? _strlen(arg) : '(null)'.length; 4834 if (precisionSet) argLength = Math.min(argLength, precision); 4835 if (!flagLeftAlign) { 4836 while (argLength < width--) { 4837 ret.push(32); 4838 } 4839 } 4840 if (arg) { 4841 for (var i = 0; i < argLength; i++) { 4842 ret.push(HEAPU8[((arg++)|0)]); 4843 } 4844 } else { 4845 ret = ret.concat(intArrayFromString('(null)'.substr(0, argLength), true)); 4846 } 4847 if (flagLeftAlign) { 4848 while (argLength < width--) { 4849 ret.push(32); 4850 } 4851 } 4852 break; 4853 } 4854 case 'c': { 4855 // Character. 4856 if (flagLeftAlign) ret.push(getNextArg('i8')); 4857 while (--width > 0) { 4858 ret.push(32); 4859 } 4860 if (!flagLeftAlign) ret.push(getNextArg('i8')); 4861 break; 4862 } 4863 case 'n': { 4864 // Write the length written so far to the next parameter. 4865 var ptr = getNextArg('i32*'); 4866 HEAP32[((ptr)>>2)]=ret.length; 4867 break; 4868 } 4869 case '%': { 4870 // Literal percent sign. 4871 ret.push(curr); 4872 break; 4873 } 4874 default: { 4875 // Unknown specifiers remain untouched. 4876 for (var i = startTextIndex; i < textIndex + 2; i++) { 4877 ret.push(HEAP8[(i)]); 4878 } 4879 } 4880 } 4881 textIndex += 2; 4882 // TODO: Support a/A (hex float) and m (last error) specifiers. 4883 // TODO: Support %1${specifier} for arg selection. 4884 } else { 4885 ret.push(curr); 4886 textIndex += 1; 4887 } 4888 } 4889 return ret; 4890 }function _fprintf(stream, format, varargs) { 4891 // int fprintf(FILE *restrict stream, const char *restrict format, ...); 4892 // http://pubs.opengroup.org/onlinepubs/000095399/functions/printf.html 4893 var result = __formatString(format, varargs); 4894 var stack = Runtime.stackSave(); 4895 var ret = _fwrite(allocate(result, 'i8', ALLOC_STACK), 1, result.length, stream); 4896 Runtime.stackRestore(stack); 4897 return ret; 4898 }function _printf(format, varargs) { 4899 // int printf(const char *restrict format, ...); 4900 // http://pubs.opengroup.org/onlinepubs/000095399/functions/printf.html 4901 var stdout = HEAP32[((_stdout)>>2)]; 4902 return _fprintf(stdout, format, varargs); 4903 } 4904 4905 4906 function _fputc(c, stream) { 4907 // int fputc(int c, FILE *stream); 4908 // http://pubs.opengroup.org/onlinepubs/000095399/functions/fputc.html 4909 var chr = unSign(c & 0xFF); 4910 HEAP8[((_fputc.ret)|0)]=chr; 4911 var fd = _fileno(stream); 4912 var ret = _write(fd, _fputc.ret, 1); 4913 if (ret == -1) { 4914 var streamObj = FS.getStreamFromPtr(stream); 4915 if (streamObj) streamObj.error = true; 4916 return -1; 4917 } else { 4918 return chr; 4919 } 4920 }function _putchar(c) { 4921 // int putchar(int c); 4922 // http://pubs.opengroup.org/onlinepubs/000095399/functions/putchar.html 4923 return _fputc(c, HEAP32[((_stdout)>>2)]); 4924 } 4925 4926 function _sbrk(bytes) { 4927 // Implement a Linux-like 'memory area' for our 'process'. 4928 // Changes the size of the memory area by |bytes|; returns the 4929 // address of the previous top ('break') of the memory area 4930 // We control the "dynamic" memory - DYNAMIC_BASE to DYNAMICTOP 4931 var self = _sbrk; 4932 if (!self.called) { 4933 DYNAMICTOP = alignMemoryPage(DYNAMICTOP); // make sure we start out aligned 4934 self.called = true; 4935 assert(Runtime.dynamicAlloc); 4936 self.alloc = Runtime.dynamicAlloc; 4937 Runtime.dynamicAlloc = function() { abort('cannot dynamically allocate, sbrk now has control') }; 4938 } 4939 var ret = DYNAMICTOP; 4940 if (bytes != 0) self.alloc(bytes); 4941 return ret; // Previous break location. 4942 } 4943 4944 function _sysconf(name) { 4945 // long sysconf(int name); 4946 // http://pubs.opengroup.org/onlinepubs/009695399/functions/sysconf.html 4947 switch(name) { 4948 case 30: return PAGE_SIZE; 4949 case 132: 4950 case 133: 4951 case 12: 4952 case 137: 4953 case 138: 4954 case 15: 4955 case 235: 4956 case 16: 4957 case 17: 4958 case 18: 4959 case 19: 4960 case 20: 4961 case 149: 4962 case 13: 4963 case 10: 4964 case 236: 4965 case 153: 4966 case 9: 4967 case 21: 4968 case 22: 4969 case 159: 4970 case 154: 4971 case 14: 4972 case 77: 4973 case 78: 4974 case 139: 4975 case 80: 4976 case 81: 4977 case 79: 4978 case 82: 4979 case 68: 4980 case 67: 4981 case 164: 4982 case 11: 4983 case 29: 4984 case 47: 4985 case 48: 4986 case 95: 4987 case 52: 4988 case 51: 4989 case 46: 4990 return 200809; 4991 case 27: 4992 case 246: 4993 case 127: 4994 case 128: 4995 case 23: 4996 case 24: 4997 case 160: 4998 case 161: 4999 case 181: 5000 case 182: 5001 case 242: 5002 case 183: 5003 case 184: 5004 case 243: 5005 case 244: 5006 case 245: 5007 case 165: 5008 case 178: 5009 case 179: 5010 case 49: 5011 case 50: 5012 case 168: 5013 case 169: 5014 case 175: 5015 case 170: 5016 case 171: 5017 case 172: 5018 case 97: 5019 case 76: 5020 case 32: 5021 case 173: 5022 case 35: 5023 return -1; 5024 case 176: 5025 case 177: 5026 case 7: 5027 case 155: 5028 case 8: 5029 case 157: 5030 case 125: 5031 case 126: 5032 case 92: 5033 case 93: 5034 case 129: 5035 case 130: 5036 case 131: 5037 case 94: 5038 case 91: 5039 return 1; 5040 case 74: 5041 case 60: 5042 case 69: 5043 case 70: 5044 case 4: 5045 return 1024; 5046 case 31: 5047 case 42: 5048 case 72: 5049 return 32; 5050 case 87: 5051 case 26: 5052 case 33: 5053 return 2147483647; 5054 case 34: 5055 case 1: 5056 return 47839; 5057 case 38: 5058 case 36: 5059 return 99; 5060 case 43: 5061 case 37: 5062 return 2048; 5063 case 0: return 2097152; 5064 case 3: return 65536; 5065 case 28: return 32768; 5066 case 44: return 32767; 5067 case 75: return 16384; 5068 case 39: return 1000; 5069 case 89: return 700; 5070 case 71: return 256; 5071 case 40: return 255; 5072 case 2: return 100; 5073 case 180: return 64; 5074 case 25: return 20; 5075 case 5: return 16; 5076 case 6: return 6; 5077 case 73: return 4; 5078 case 84: return 1; 5079 } 5080 ___setErrNo(ERRNO_CODES.EINVAL); 5081 return -1; 5082 } 5083 5084 5085 Module["_memset"] = _memset; 5086 5087 function ___errno_location() { 5088 return ___errno_state; 5089 } 5090 5091 function _abort() { 5092 Module['abort'](); 5093 } 5094 5095 var Browser={mainLoop:{scheduler:null,method:"",shouldPause:false,paused:false,queue:[],pause:function () { 5096 Browser.mainLoop.shouldPause = true; 5097 },resume:function () { 5098 if (Browser.mainLoop.paused) { 5099 Browser.mainLoop.paused = false; 5100 Browser.mainLoop.scheduler(); 5101 } 5102 Browser.mainLoop.shouldPause = false; 5103 },updateStatus:function () { 5104 if (Module['setStatus']) { 5105 var message = Module['statusMessage'] || 'Please wait...'; 5106 var remaining = Browser.mainLoop.remainingBlockers; 5107 var expected = Browser.mainLoop.expectedBlockers; 5108 if (remaining) { 5109 if (remaining < expected) { 5110 Module['setStatus'](message + ' (' + (expected - remaining) + '/' + expected + ')'); 5111 } else { 5112 Module['setStatus'](message); 5113 } 5114 } else { 5115 Module['setStatus'](''); 5116 } 5117 } 5118 }},isFullScreen:false,pointerLock:false,moduleContextCreatedCallbacks:[],workers:[],init:function () { 5119 if (!Module["preloadPlugins"]) Module["preloadPlugins"] = []; // needs to exist even in workers 5120 5121 if (Browser.initted || ENVIRONMENT_IS_WORKER) return; 5122 Browser.initted = true; 5123 5124 try { 5125 new Blob(); 5126 Browser.hasBlobConstructor = true; 5127 } catch(e) { 5128 Browser.hasBlobConstructor = false; 5129 console.log("warning: no blob constructor, cannot create blobs with mimetypes"); 5130 } 5131 Browser.BlobBuilder = typeof MozBlobBuilder != "undefined" ? MozBlobBuilder : (typeof WebKitBlobBuilder != "undefined" ? WebKitBlobBuilder : (!Browser.hasBlobConstructor ? console.log("warning: no BlobBuilder") : null)); 5132 Browser.URLObject = typeof window != "undefined" ? (window.URL ? window.URL : window.webkitURL) : undefined; 5133 if (!Module.noImageDecoding && typeof Browser.URLObject === 'undefined') { 5134 console.log("warning: Browser does not support creating object URLs. Built-in browser image decoding will not be available."); 5135 Module.noImageDecoding = true; 5136 } 5137 5138 // Support for plugins that can process preloaded files. You can add more of these to 5139 // your app by creating and appending to Module.preloadPlugins. 5140 // 5141 // Each plugin is asked if it can handle a file based on the file's name. If it can, 5142 // it is given the file's raw data. When it is done, it calls a callback with the file's 5143 // (possibly modified) data. For example, a plugin might decompress a file, or it 5144 // might create some side data structure for use later (like an Image element, etc.). 5145 5146 var imagePlugin = {}; 5147 imagePlugin['canHandle'] = function imagePlugin_canHandle(name) { 5148 return !Module.noImageDecoding && /\.(jpg|jpeg|png|bmp)$/i.test(name); 5149 }; 5150 imagePlugin['handle'] = function imagePlugin_handle(byteArray, name, onload, onerror) { 5151 var b = null; 5152 if (Browser.hasBlobConstructor) { 5153 try { 5154 b = new Blob([byteArray], { type: Browser.getMimetype(name) }); 5155 if (b.size !== byteArray.length) { // Safari bug #118630 5156 // Safari's Blob can only take an ArrayBuffer 5157 b = new Blob([(new Uint8Array(byteArray)).buffer], { type: Browser.getMimetype(name) }); 5158 } 5159 } catch(e) { 5160 Runtime.warnOnce('Blob constructor present but fails: ' + e + '; falling back to blob builder'); 5161 } 5162 } 5163 if (!b) { 5164 var bb = new Browser.BlobBuilder(); 5165 bb.append((new Uint8Array(byteArray)).buffer); // we need to pass a buffer, and must copy the array to get the right data range 5166 b = bb.getBlob(); 5167 } 5168 var url = Browser.URLObject.createObjectURL(b); 5169 var img = new Image(); 5170 img.onload = function img_onload() { 5171 assert(img.complete, 'Image ' + name + ' could not be decoded'); 5172 var canvas = document.createElement('canvas'); 5173 canvas.width = img.width; 5174 canvas.height = img.height; 5175 var ctx = canvas.getContext('2d'); 5176 ctx.drawImage(img, 0, 0); 5177 Module["preloadedImages"][name] = canvas; 5178 Browser.URLObject.revokeObjectURL(url); 5179 if (onload) onload(byteArray); 5180 }; 5181 img.onerror = function img_onerror(event) { 5182 console.log('Image ' + url + ' could not be decoded'); 5183 if (onerror) onerror(); 5184 }; 5185 img.src = url; 5186 }; 5187 Module['preloadPlugins'].push(imagePlugin); 5188 5189 var audioPlugin = {}; 5190 audioPlugin['canHandle'] = function audioPlugin_canHandle(name) { 5191 return !Module.noAudioDecoding && name.substr(-4) in { '.ogg': 1, '.wav': 1, '.mp3': 1 }; 5192 }; 5193 audioPlugin['handle'] = function audioPlugin_handle(byteArray, name, onload, onerror) { 5194 var done = false; 5195 function finish(audio) { 5196 if (done) return; 5197 done = true; 5198 Module["preloadedAudios"][name] = audio; 5199 if (onload) onload(byteArray); 5200 } 5201 function fail() { 5202 if (done) return; 5203 done = true; 5204 Module["preloadedAudios"][name] = new Audio(); // empty shim 5205 if (onerror) onerror(); 5206 } 5207 if (Browser.hasBlobConstructor) { 5208 try { 5209 var b = new Blob([byteArray], { type: Browser.getMimetype(name) }); 5210 } catch(e) { 5211 return fail(); 5212 } 5213 var url = Browser.URLObject.createObjectURL(b); // XXX we never revoke this! 5214 var audio = new Audio(); 5215 audio.addEventListener('canplaythrough', function() { finish(audio) }, false); // use addEventListener due to chromium bug 124926 5216 audio.onerror = function audio_onerror(event) { 5217 if (done) return; 5218 console.log('warning: browser could not fully decode audio ' + name + ', trying slower base64 approach'); 5219 function encode64(data) { 5220 var BASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; 5221 var PAD = '='; 5222 var ret = ''; 5223 var leftchar = 0; 5224 var leftbits = 0; 5225 for (var i = 0; i < data.length; i++) { 5226 leftchar = (leftchar << 8) | data[i]; 5227 leftbits += 8; 5228 while (leftbits >= 6) { 5229 var curr = (leftchar >> (leftbits-6)) & 0x3f; 5230 leftbits -= 6; 5231 ret += BASE[curr]; 5232 } 5233 } 5234 if (leftbits == 2) { 5235 ret += BASE[(leftchar&3) << 4]; 5236 ret += PAD + PAD; 5237 } else if (leftbits == 4) { 5238 ret += BASE[(leftchar&0xf) << 2]; 5239 ret += PAD; 5240 } 5241 return ret; 5242 } 5243 audio.src = 'data:audio/x-' + name.substr(-3) + ';base64,' + encode64(byteArray); 5244 finish(audio); // we don't wait for confirmation this worked - but it's worth trying 5245 }; 5246 audio.src = url; 5247 // workaround for chrome bug 124926 - we do not always get oncanplaythrough or onerror 5248 Browser.safeSetTimeout(function() { 5249 finish(audio); // try to use it even though it is not necessarily ready to play 5250 }, 10000); 5251 } else { 5252 return fail(); 5253 } 5254 }; 5255 Module['preloadPlugins'].push(audioPlugin); 5256 5257 // Canvas event setup 5258 5259 var canvas = Module['canvas']; 5260 5261 // forced aspect ratio can be enabled by defining 'forcedAspectRatio' on Module 5262 // Module['forcedAspectRatio'] = 4 / 3; 5263 5264 canvas.requestPointerLock = canvas['requestPointerLock'] || 5265 canvas['mozRequestPointerLock'] || 5266 canvas['webkitRequestPointerLock'] || 5267 canvas['msRequestPointerLock'] || 5268 function(){}; 5269 canvas.exitPointerLock = document['exitPointerLock'] || 5270 document['mozExitPointerLock'] || 5271 document['webkitExitPointerLock'] || 5272 document['msExitPointerLock'] || 5273 function(){}; // no-op if function does not exist 5274 canvas.exitPointerLock = canvas.exitPointerLock.bind(document); 5275 5276 function pointerLockChange() { 5277 Browser.pointerLock = document['pointerLockElement'] === canvas || 5278 document['mozPointerLockElement'] === canvas || 5279 document['webkitPointerLockElement'] === canvas || 5280 document['msPointerLockElement'] === canvas; 5281 } 5282 5283 document.addEventListener('pointerlockchange', pointerLockChange, false); 5284 document.addEventListener('mozpointerlockchange', pointerLockChange, false); 5285 document.addEventListener('webkitpointerlockchange', pointerLockChange, false); 5286 document.addEventListener('mspointerlockchange', pointerLockChange, false); 5287 5288 if (Module['elementPointerLock']) { 5289 canvas.addEventListener("click", function(ev) { 5290 if (!Browser.pointerLock && canvas.requestPointerLock) { 5291 canvas.requestPointerLock(); 5292 ev.preventDefault(); 5293 } 5294 }, false); 5295 } 5296 },createContext:function (canvas, useWebGL, setInModule, webGLContextAttributes) { 5297 var ctx; 5298 var errorInfo = '?'; 5299 function onContextCreationError(event) { 5300 errorInfo = event.statusMessage || errorInfo; 5301 } 5302 try { 5303 if (useWebGL) { 5304 var contextAttributes = { 5305 antialias: false, 5306 alpha: false 5307 }; 5308 5309 if (webGLContextAttributes) { 5310 for (var attribute in webGLContextAttributes) { 5311 contextAttributes[attribute] = webGLContextAttributes[attribute]; 5312 } 5313 } 5314 5315 5316 canvas.addEventListener('webglcontextcreationerror', onContextCreationError, false); 5317 try { 5318 ['experimental-webgl', 'webgl'].some(function(webglId) { 5319 return ctx = canvas.getContext(webglId, contextAttributes); 5320 }); 5321 } finally { 5322 canvas.removeEventListener('webglcontextcreationerror', onContextCreationError, false); 5323 } 5324 } else { 5325 ctx = canvas.getContext('2d'); 5326 } 5327 if (!ctx) throw ':('; 5328 } catch (e) { 5329 Module.print('Could not create canvas: ' + [errorInfo, e]); 5330 return null; 5331 } 5332 if (useWebGL) { 5333 // Set the background of the WebGL canvas to black 5334 canvas.style.backgroundColor = "black"; 5335 5336 // Warn on context loss 5337 canvas.addEventListener('webglcontextlost', function(event) { 5338 alert('WebGL context lost. You will need to reload the page.'); 5339 }, false); 5340 } 5341 if (setInModule) { 5342 GLctx = Module.ctx = ctx; 5343 Module.useWebGL = useWebGL; 5344 Browser.moduleContextCreatedCallbacks.forEach(function(callback) { callback() }); 5345 Browser.init(); 5346 } 5347 return ctx; 5348 },destroyContext:function (canvas, useWebGL, setInModule) {},fullScreenHandlersInstalled:false,lockPointer:undefined,resizeCanvas:undefined,requestFullScreen:function (lockPointer, resizeCanvas) { 5349 Browser.lockPointer = lockPointer; 5350 Browser.resizeCanvas = resizeCanvas; 5351 if (typeof Browser.lockPointer === 'undefined') Browser.lockPointer = true; 5352 if (typeof Browser.resizeCanvas === 'undefined') Browser.resizeCanvas = false; 5353 5354 var canvas = Module['canvas']; 5355 function fullScreenChange() { 5356 Browser.isFullScreen = false; 5357 var canvasContainer = canvas.parentNode; 5358 if ((document['webkitFullScreenElement'] || document['webkitFullscreenElement'] || 5359 document['mozFullScreenElement'] || document['mozFullscreenElement'] || 5360 document['fullScreenElement'] || document['fullscreenElement'] || 5361 document['msFullScreenElement'] || document['msFullscreenElement'] || 5362 document['webkitCurrentFullScreenElement']) === canvasContainer) { 5363 canvas.cancelFullScreen = document['cancelFullScreen'] || 5364 document['mozCancelFullScreen'] || 5365 document['webkitCancelFullScreen'] || 5366 document['msExitFullscreen'] || 5367 document['exitFullscreen'] || 5368 function() {}; 5369 canvas.cancelFullScreen = canvas.cancelFullScreen.bind(document); 5370 if (Browser.lockPointer) canvas.requestPointerLock(); 5371 Browser.isFullScreen = true; 5372 if (Browser.resizeCanvas) Browser.setFullScreenCanvasSize(); 5373 } else { 5374 5375 // remove the full screen specific parent of the canvas again to restore the HTML structure from before going full screen 5376 canvasContainer.parentNode.insertBefore(canvas, canvasContainer); 5377 canvasContainer.parentNode.removeChild(canvasContainer); 5378 5379 if (Browser.resizeCanvas) Browser.setWindowedCanvasSize(); 5380 } 5381 if (Module['onFullScreen']) Module['onFullScreen'](Browser.isFullScreen); 5382 Browser.updateCanvasDimensions(canvas); 5383 } 5384 5385 if (!Browser.fullScreenHandlersInstalled) { 5386 Browser.fullScreenHandlersInstalled = true; 5387 document.addEventListener('fullscreenchange', fullScreenChange, false); 5388 document.addEventListener('mozfullscreenchange', fullScreenChange, false); 5389 document.addEventListener('webkitfullscreenchange', fullScreenChange, false); 5390 document.addEventListener('MSFullscreenChange', fullScreenChange, false); 5391 } 5392 5393 // create a new parent to ensure the canvas has no siblings. this allows browsers to optimize full screen performance when its parent is the full screen root 5394 var canvasContainer = document.createElement("div"); 5395 canvas.parentNode.insertBefore(canvasContainer, canvas); 5396 canvasContainer.appendChild(canvas); 5397 5398 // use parent of canvas as full screen root to allow aspect ratio correction (Firefox stretches the root to screen size) 5399 canvasContainer.requestFullScreen = canvasContainer['requestFullScreen'] || 5400 canvasContainer['mozRequestFullScreen'] || 5401 canvasContainer['msRequestFullscreen'] || 5402 (canvasContainer['webkitRequestFullScreen'] ? function() { canvasContainer['webkitRequestFullScreen'](Element['ALLOW_KEYBOARD_INPUT']) } : null); 5403 canvasContainer.requestFullScreen(); 5404 },requestAnimationFrame:function requestAnimationFrame(func) { 5405 if (typeof window === 'undefined') { // Provide fallback to setTimeout if window is undefined (e.g. in Node.js) 5406 setTimeout(func, 1000/60); 5407 } else { 5408 if (!window.requestAnimationFrame) { 5409 window.requestAnimationFrame = window['requestAnimationFrame'] || 5410 window['mozRequestAnimationFrame'] || 5411 window['webkitRequestAnimationFrame'] || 5412 window['msRequestAnimationFrame'] || 5413 window['oRequestAnimationFrame'] || 5414 window['setTimeout']; 5415 } 5416 window.requestAnimationFrame(func); 5417 } 5418 },safeCallback:function (func) { 5419 return function() { 5420 if (!ABORT) return func.apply(null, arguments); 5421 }; 5422 },safeRequestAnimationFrame:function (func) { 5423 return Browser.requestAnimationFrame(function() { 5424 if (!ABORT) func(); 5425 }); 5426 },safeSetTimeout:function (func, timeout) { 5427 return setTimeout(function() { 5428 if (!ABORT) func(); 5429 }, timeout); 5430 },safeSetInterval:function (func, timeout) { 5431 return setInterval(function() { 5432 if (!ABORT) func(); 5433 }, timeout); 5434 },getMimetype:function (name) { 5435 return { 5436 'jpg': 'image/jpeg', 5437 'jpeg': 'image/jpeg', 5438 'png': 'image/png', 5439 'bmp': 'image/bmp', 5440 'ogg': 'audio/ogg', 5441 'wav': 'audio/wav', 5442 'mp3': 'audio/mpeg' 5443 }[name.substr(name.lastIndexOf('.')+1)]; 5444 },getUserMedia:function (func) { 5445 if(!window.getUserMedia) { 5446 window.getUserMedia = navigator['getUserMedia'] || 5447 navigator['mozGetUserMedia']; 5448 } 5449 window.getUserMedia(func); 5450 },getMovementX:function (event) { 5451 return event['movementX'] || 5452 event['mozMovementX'] || 5453 event['webkitMovementX'] || 5454 0; 5455 },getMovementY:function (event) { 5456 return event['movementY'] || 5457 event['mozMovementY'] || 5458 event['webkitMovementY'] || 5459 0; 5460 },getMouseWheelDelta:function (event) { 5461 return Math.max(-1, Math.min(1, event.type === 'DOMMouseScroll' ? event.detail : -event.wheelDelta)); 5462 },mouseX:0,mouseY:0,mouseMovementX:0,mouseMovementY:0,calculateMouseEvent:function (event) { // event should be mousemove, mousedown or mouseup 5463 if (Browser.pointerLock) { 5464 // When the pointer is locked, calculate the coordinates 5465 // based on the movement of the mouse. 5466 // Workaround for Firefox bug 764498 5467 if (event.type != 'mousemove' && 5468 ('mozMovementX' in event)) { 5469 Browser.mouseMovementX = Browser.mouseMovementY = 0; 5470 } else { 5471 Browser.mouseMovementX = Browser.getMovementX(event); 5472 Browser.mouseMovementY = Browser.getMovementY(event); 5473 } 5474 5475 // check if SDL is available 5476 if (typeof SDL != "undefined") { 5477 Browser.mouseX = SDL.mouseX + Browser.mouseMovementX; 5478 Browser.mouseY = SDL.mouseY + Browser.mouseMovementY; 5479 } else { 5480 // just add the mouse delta to the current absolut mouse position 5481 // FIXME: ideally this should be clamped against the canvas size and zero 5482 Browser.mouseX += Browser.mouseMovementX; 5483 Browser.mouseY += Browser.mouseMovementY; 5484 } 5485 } else { 5486 // Otherwise, calculate the movement based on the changes 5487 // in the coordinates. 5488 var rect = Module["canvas"].getBoundingClientRect(); 5489 var x, y; 5490 5491 // Neither .scrollX or .pageXOffset are defined in a spec, but 5492 // we prefer .scrollX because it is currently in a spec draft. 5493 // (see: http://www.w3.org/TR/2013/WD-cssom-view-20131217/) 5494 var scrollX = ((typeof window.scrollX !== 'undefined') ? window.scrollX : window.pageXOffset); 5495 var scrollY = ((typeof window.scrollY !== 'undefined') ? window.scrollY : window.pageYOffset); 5496 if (event.type == 'touchstart' || 5497 event.type == 'touchend' || 5498 event.type == 'touchmove') { 5499 var t = event.touches.item(0); 5500 if (t) { 5501 x = t.pageX - (scrollX + rect.left); 5502 y = t.pageY - (scrollY + rect.top); 5503 } else { 5504 return; 5505 } 5506 } else { 5507 x = event.pageX - (scrollX + rect.left); 5508 y = event.pageY - (scrollY + rect.top); 5509 } 5510 5511 // the canvas might be CSS-scaled compared to its backbuffer; 5512 // SDL-using content will want mouse coordinates in terms 5513 // of backbuffer units. 5514 var cw = Module["canvas"].width; 5515 var ch = Module["canvas"].height; 5516 x = x * (cw / rect.width); 5517 y = y * (ch / rect.height); 5518 5519 Browser.mouseMovementX = x - Browser.mouseX; 5520 Browser.mouseMovementY = y - Browser.mouseY; 5521 Browser.mouseX = x; 5522 Browser.mouseY = y; 5523 } 5524 },xhrLoad:function (url, onload, onerror) { 5525 var xhr = new XMLHttpRequest(); 5526 xhr.open('GET', url, true); 5527 xhr.responseType = 'arraybuffer'; 5528 xhr.onload = function xhr_onload() { 5529 if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) { // file URLs can return 0 5530 onload(xhr.response); 5531 } else { 5532 onerror(); 5533 } 5534 }; 5535 xhr.onerror = onerror; 5536 xhr.send(null); 5537 },asyncLoad:function (url, onload, onerror, noRunDep) { 5538 Browser.xhrLoad(url, function(arrayBuffer) { 5539 assert(arrayBuffer, 'Loading data file "' + url + '" failed (no arrayBuffer).'); 5540 onload(new Uint8Array(arrayBuffer)); 5541 if (!noRunDep) removeRunDependency('al ' + url); 5542 }, function(event) { 5543 if (onerror) { 5544 onerror(); 5545 } else { 5546 throw 'Loading data file "' + url + '" failed.'; 5547 } 5548 }); 5549 if (!noRunDep) addRunDependency('al ' + url); 5550 },resizeListeners:[],updateResizeListeners:function () { 5551 var canvas = Module['canvas']; 5552 Browser.resizeListeners.forEach(function(listener) { 5553 listener(canvas.width, canvas.height); 5554 }); 5555 },setCanvasSize:function (width, height, noUpdates) { 5556 var canvas = Module['canvas']; 5557 Browser.updateCanvasDimensions(canvas, width, height); 5558 if (!noUpdates) Browser.updateResizeListeners(); 5559 },windowedWidth:0,windowedHeight:0,setFullScreenCanvasSize:function () { 5560 // check if SDL is available 5561 if (typeof SDL != "undefined") { 5562 var flags = HEAPU32[((SDL.screen+Runtime.QUANTUM_SIZE*0)>>2)]; 5563 flags = flags | 0x00800000; // set SDL_FULLSCREEN flag 5564 HEAP32[((SDL.screen+Runtime.QUANTUM_SIZE*0)>>2)]=flags 5565 } 5566 Browser.updateResizeListeners(); 5567 },setWindowedCanvasSize:function () { 5568 // check if SDL is available 5569 if (typeof SDL != "undefined") { 5570 var flags = HEAPU32[((SDL.screen+Runtime.QUANTUM_SIZE*0)>>2)]; 5571 flags = flags & ~0x00800000; // clear SDL_FULLSCREEN flag 5572 HEAP32[((SDL.screen+Runtime.QUANTUM_SIZE*0)>>2)]=flags 5573 } 5574 Browser.updateResizeListeners(); 5575 },updateCanvasDimensions:function (canvas, wNative, hNative) { 5576 if (wNative && hNative) { 5577 canvas.widthNative = wNative; 5578 canvas.heightNative = hNative; 5579 } else { 5580 wNative = canvas.widthNative; 5581 hNative = canvas.heightNative; 5582 } 5583 var w = wNative; 5584 var h = hNative; 5585 if (Module['forcedAspectRatio'] && Module['forcedAspectRatio'] > 0) { 5586 if (w/h < Module['forcedAspectRatio']) { 5587 w = Math.round(h * Module['forcedAspectRatio']); 5588 } else { 5589 h = Math.round(w / Module['forcedAspectRatio']); 5590 } 5591 } 5592 if (((document['webkitFullScreenElement'] || document['webkitFullscreenElement'] || 5593 document['mozFullScreenElement'] || document['mozFullscreenElement'] || 5594 document['fullScreenElement'] || document['fullscreenElement'] || 5595 document['msFullScreenElement'] || document['msFullscreenElement'] || 5596 document['webkitCurrentFullScreenElement']) === canvas.parentNode) && (typeof screen != 'undefined')) { 5597 var factor = Math.min(screen.width / w, screen.height / h); 5598 w = Math.round(w * factor); 5599 h = Math.round(h * factor); 5600 } 5601 if (Browser.resizeCanvas) { 5602 if (canvas.width != w) canvas.width = w; 5603 if (canvas.height != h) canvas.height = h; 5604 if (typeof canvas.style != 'undefined') { 5605 canvas.style.removeProperty( "width"); 5606 canvas.style.removeProperty("height"); 5607 } 5608 } else { 5609 if (canvas.width != wNative) canvas.width = wNative; 5610 if (canvas.height != hNative) canvas.height = hNative; 5611 if (typeof canvas.style != 'undefined') { 5612 if (w != wNative || h != hNative) { 5613 canvas.style.setProperty( "width", w + "px", "important"); 5614 canvas.style.setProperty("height", h + "px", "important"); 5615 } else { 5616 canvas.style.removeProperty( "width"); 5617 canvas.style.removeProperty("height"); 5618 } 5619 } 5620 } 5621 }}; 5622 5623 function _time(ptr) { 5624 var ret = Math.floor(Date.now()/1000); 5625 if (ptr) { 5626 HEAP32[((ptr)>>2)]=ret; 5627 } 5628 return ret; 5629 } 5630 5631 5632 5633 function _emscripten_memcpy_big(dest, src, num) { 5634 HEAPU8.set(HEAPU8.subarray(src, src+num), dest); 5635 return dest; 5636 } 5637 Module["_memcpy"] = _memcpy; 5638FS.staticInit();__ATINIT__.unshift({ func: function() { if (!Module["noFSInit"] && !FS.init.initialized) FS.init() } });__ATMAIN__.push({ func: function() { FS.ignorePermissions = false } });__ATEXIT__.push({ func: function() { FS.quit() } });Module["FS_createFolder"] = FS.createFolder;Module["FS_createPath"] = FS.createPath;Module["FS_createDataFile"] = FS.createDataFile;Module["FS_createPreloadedFile"] = FS.createPreloadedFile;Module["FS_createLazyFile"] = FS.createLazyFile;Module["FS_createLink"] = FS.createLink;Module["FS_createDevice"] = FS.createDevice; 5639___errno_state = Runtime.staticAlloc(4); HEAP32[((___errno_state)>>2)]=0; 5640__ATINIT__.unshift({ func: function() { TTY.init() } });__ATEXIT__.push({ func: function() { TTY.shutdown() } });TTY.utf8 = new Runtime.UTF8Processor(); 5641if (ENVIRONMENT_IS_NODE) { var fs = require("fs"); NODEFS.staticInit(); } 5642__ATINIT__.push({ func: function() { SOCKFS.root = FS.mount(SOCKFS, {}, null); } }); 5643_fputc.ret = allocate([0], "i8", ALLOC_STATIC); 5644Module["requestFullScreen"] = function Module_requestFullScreen(lockPointer, resizeCanvas) { Browser.requestFullScreen(lockPointer, resizeCanvas) }; 5645 Module["requestAnimationFrame"] = function Module_requestAnimationFrame(func) { Browser.requestAnimationFrame(func) }; 5646 Module["setCanvasSize"] = function Module_setCanvasSize(width, height, noUpdates) { Browser.setCanvasSize(width, height, noUpdates) }; 5647 Module["pauseMainLoop"] = function Module_pauseMainLoop() { Browser.mainLoop.pause() }; 5648 Module["resumeMainLoop"] = function Module_resumeMainLoop() { Browser.mainLoop.resume() }; 5649 Module["getUserMedia"] = function Module_getUserMedia() { Browser.getUserMedia() } 5650STACK_BASE = STACKTOP = Runtime.alignMemory(STATICTOP); 5651 5652staticSealed = true; // seal the static portion of memory 5653 5654STACK_MAX = STACK_BASE + 5242880; 5655 5656DYNAMIC_BASE = DYNAMICTOP = Runtime.alignMemory(STACK_MAX); 5657 5658assert(DYNAMIC_BASE < TOTAL_MEMORY, "TOTAL_MEMORY not big enough for stack"); 5659 5660 5661var Math_min = Math.min; 5662function asmPrintInt(x, y) { 5663 Module.print('int ' + x + ',' + y);// + ' ' + new Error().stack); 5664} 5665function asmPrintFloat(x, y) { 5666 Module.print('float ' + x + ',' + y);// + ' ' + new Error().stack); 5667} 5668// EMSCRIPTEN_START_ASM 5669var asm = Wasm.instantiateModuleFromAsm((function Module(global, env, buffer) { 5670 'use asm'; 5671 var HEAP8 = new global.Int8Array(buffer); 5672 var HEAP16 = new global.Int16Array(buffer); 5673 var HEAP32 = new global.Int32Array(buffer); 5674 var HEAPU8 = new global.Uint8Array(buffer); 5675 var HEAPU16 = new global.Uint16Array(buffer); 5676 var HEAPU32 = new global.Uint32Array(buffer); 5677 var HEAPF32 = new global.Float32Array(buffer); 5678 var HEAPF64 = new global.Float64Array(buffer); 5679 5680 var STACKTOP=env.STACKTOP|0; 5681 var STACK_MAX=env.STACK_MAX|0; 5682 var tempDoublePtr=env.tempDoublePtr|0; 5683 var ABORT=env.ABORT|0; 5684 5685 var __THREW__ = 0; 5686 var threwValue = 0; 5687 var setjmpId = 0; 5688 var undef = 0; 5689 var nan = +env.NaN, inf = +env.Infinity; 5690 var tempInt = 0, tempBigInt = 0, tempBigIntP = 0, tempBigIntS = 0, tempBigIntR = 0.0, tempBigIntI = 0, tempBigIntD = 0, tempValue = 0, tempDouble = 0.0; 5691 5692 var tempRet0 = 0; 5693 var tempRet1 = 0; 5694 var tempRet2 = 0; 5695 var tempRet3 = 0; 5696 var tempRet4 = 0; 5697 var tempRet5 = 0; 5698 var tempRet6 = 0; 5699 var tempRet7 = 0; 5700 var tempRet8 = 0; 5701 var tempRet9 = 0; 5702 var Math_floor=global.Math.floor; 5703 var Math_abs=global.Math.abs; 5704 var Math_sqrt=global.Math.sqrt; 5705 var Math_pow=global.Math.pow; 5706 var Math_cos=global.Math.cos; 5707 var Math_sin=global.Math.sin; 5708 var Math_tan=global.Math.tan; 5709 var Math_acos=global.Math.acos; 5710 var Math_asin=global.Math.asin; 5711 var Math_atan=global.Math.atan; 5712 var Math_atan2=global.Math.atan2; 5713 var Math_exp=global.Math.exp; 5714 var Math_log=global.Math.log; 5715 var Math_ceil=global.Math.ceil; 5716 var Math_imul=global.Math.imul; 5717 var abort=env.abort; 5718 var assert=env.assert; 5719 var asmPrintInt=env.asmPrintInt; 5720 var asmPrintFloat=env.asmPrintFloat; 5721 var Math_min=env.min; 5722 var _fflush=env._fflush; 5723 var _emscripten_memcpy_big=env._emscripten_memcpy_big; 5724 var _putchar=env._putchar; 5725 var _fputc=env._fputc; 5726 var _send=env._send; 5727 var _pwrite=env._pwrite; 5728 var _abort=env._abort; 5729 var __reallyNegative=env.__reallyNegative; 5730 var _fwrite=env._fwrite; 5731 var _sbrk=env._sbrk; 5732 var _mkport=env._mkport; 5733 var _fprintf=env._fprintf; 5734 var ___setErrNo=env.___setErrNo; 5735 var __formatString=env.__formatString; 5736 var _fileno=env._fileno; 5737 var _printf=env._printf; 5738 var _time=env._time; 5739 var _sysconf=env._sysconf; 5740 var _write=env._write; 5741 var ___errno_location=env.___errno_location; 5742 var tempFloat = 0.0; 5743 5744// EMSCRIPTEN_START_FUNCS 5745function _malloc(i12) { 5746 i12 = i12 | 0; 5747 var i1 = 0, i2 = 0, i3 = 0, i4 = 0, i5 = 0, i6 = 0, i7 = 0, i8 = 0, i9 = 0, i10 = 0, i11 = 0, i13 = 0, i14 = 0, i15 = 0, i16 = 0, i17 = 0, i18 = 0, i19 = 0, i20 = 0, i21 = 0, i22 = 0, i23 = 0, i24 = 0, i25 = 0, i26 = 0, i27 = 0, i28 = 0, i29 = 0, i30 = 0, i31 = 0, i32 = 0; 5748 i1 = STACKTOP; 5749 do { 5750 if (i12 >>> 0 < 245) { 5751 if (i12 >>> 0 < 11) { 5752 i12 = 16; 5753 } else { 5754 i12 = i12 + 11 & -8; 5755 } 5756 i20 = i12 >>> 3; 5757 i18 = HEAP32[14] | 0; 5758 i21 = i18 >>> i20; 5759 if ((i21 & 3 | 0) != 0) { 5760 i6 = (i21 & 1 ^ 1) + i20 | 0; 5761 i5 = i6 << 1; 5762 i3 = 96 + (i5 << 2) | 0; 5763 i5 = 96 + (i5 + 2 << 2) | 0; 5764 i7 = HEAP32[i5 >> 2] | 0; 5765 i2 = i7 + 8 | 0; 5766 i4 = HEAP32[i2 >> 2] | 0; 5767 do { 5768 if ((i3 | 0) != (i4 | 0)) { 5769 if (i4 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 5770 _abort(); 5771 } 5772 i8 = i4 + 12 | 0; 5773 if ((HEAP32[i8 >> 2] | 0) == (i7 | 0)) { 5774 HEAP32[i8 >> 2] = i3; 5775 HEAP32[i5 >> 2] = i4; 5776 break; 5777 } else { 5778 _abort(); 5779 } 5780 } else { 5781 HEAP32[14] = i18 & ~(1 << i6); 5782 } 5783 } while (0); 5784 i32 = i6 << 3; 5785 HEAP32[i7 + 4 >> 2] = i32 | 3; 5786 i32 = i7 + (i32 | 4) | 0; 5787 HEAP32[i32 >> 2] = HEAP32[i32 >> 2] | 1; 5788 i32 = i2; 5789 STACKTOP = i1; 5790 return i32 | 0; 5791 } 5792 if (i12 >>> 0 > (HEAP32[64 >> 2] | 0) >>> 0) { 5793 if ((i21 | 0) != 0) { 5794 i7 = 2 << i20; 5795 i7 = i21 << i20 & (i7 | 0 - i7); 5796 i7 = (i7 & 0 - i7) + -1 | 0; 5797 i2 = i7 >>> 12 & 16; 5798 i7 = i7 >>> i2; 5799 i6 = i7 >>> 5 & 8; 5800 i7 = i7 >>> i6; 5801 i5 = i7 >>> 2 & 4; 5802 i7 = i7 >>> i5; 5803 i4 = i7 >>> 1 & 2; 5804 i7 = i7 >>> i4; 5805 i3 = i7 >>> 1 & 1; 5806 i3 = (i6 | i2 | i5 | i4 | i3) + (i7 >>> i3) | 0; 5807 i7 = i3 << 1; 5808 i4 = 96 + (i7 << 2) | 0; 5809 i7 = 96 + (i7 + 2 << 2) | 0; 5810 i5 = HEAP32[i7 >> 2] | 0; 5811 i2 = i5 + 8 | 0; 5812 i6 = HEAP32[i2 >> 2] | 0; 5813 do { 5814 if ((i4 | 0) != (i6 | 0)) { 5815 if (i6 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 5816 _abort(); 5817 } 5818 i8 = i6 + 12 | 0; 5819 if ((HEAP32[i8 >> 2] | 0) == (i5 | 0)) { 5820 HEAP32[i8 >> 2] = i4; 5821 HEAP32[i7 >> 2] = i6; 5822 break; 5823 } else { 5824 _abort(); 5825 } 5826 } else { 5827 HEAP32[14] = i18 & ~(1 << i3); 5828 } 5829 } while (0); 5830 i6 = i3 << 3; 5831 i4 = i6 - i12 | 0; 5832 HEAP32[i5 + 4 >> 2] = i12 | 3; 5833 i3 = i5 + i12 | 0; 5834 HEAP32[i5 + (i12 | 4) >> 2] = i4 | 1; 5835 HEAP32[i5 + i6 >> 2] = i4; 5836 i6 = HEAP32[64 >> 2] | 0; 5837 if ((i6 | 0) != 0) { 5838 i5 = HEAP32[76 >> 2] | 0; 5839 i8 = i6 >>> 3; 5840 i9 = i8 << 1; 5841 i6 = 96 + (i9 << 2) | 0; 5842 i7 = HEAP32[14] | 0; 5843 i8 = 1 << i8; 5844 if ((i7 & i8 | 0) != 0) { 5845 i7 = 96 + (i9 + 2 << 2) | 0; 5846 i8 = HEAP32[i7 >> 2] | 0; 5847 if (i8 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 5848 _abort(); 5849 } else { 5850 i28 = i7; 5851 i27 = i8; 5852 } 5853 } else { 5854 HEAP32[14] = i7 | i8; 5855 i28 = 96 + (i9 + 2 << 2) | 0; 5856 i27 = i6; 5857 } 5858 HEAP32[i28 >> 2] = i5; 5859 HEAP32[i27 + 12 >> 2] = i5; 5860 HEAP32[i5 + 8 >> 2] = i27; 5861 HEAP32[i5 + 12 >> 2] = i6; 5862 } 5863 HEAP32[64 >> 2] = i4; 5864 HEAP32[76 >> 2] = i3; 5865 i32 = i2; 5866 STACKTOP = i1; 5867 return i32 | 0; 5868 } 5869 i18 = HEAP32[60 >> 2] | 0; 5870 if ((i18 | 0) != 0) { 5871 i2 = (i18 & 0 - i18) + -1 | 0; 5872 i31 = i2 >>> 12 & 16; 5873 i2 = i2 >>> i31; 5874 i30 = i2 >>> 5 & 8; 5875 i2 = i2 >>> i30; 5876 i32 = i2 >>> 2 & 4; 5877 i2 = i2 >>> i32; 5878 i6 = i2 >>> 1 & 2; 5879 i2 = i2 >>> i6; 5880 i3 = i2 >>> 1 & 1; 5881 i3 = HEAP32[360 + ((i30 | i31 | i32 | i6 | i3) + (i2 >>> i3) << 2) >> 2] | 0; 5882 i2 = (HEAP32[i3 + 4 >> 2] & -8) - i12 | 0; 5883 i6 = i3; 5884 while (1) { 5885 i5 = HEAP32[i6 + 16 >> 2] | 0; 5886 if ((i5 | 0) == 0) { 5887 i5 = HEAP32[i6 + 20 >> 2] | 0; 5888 if ((i5 | 0) == 0) { 5889 break; 5890 } 5891 } 5892 i6 = (HEAP32[i5 + 4 >> 2] & -8) - i12 | 0; 5893 i4 = i6 >>> 0 < i2 >>> 0; 5894 i2 = i4 ? i6 : i2; 5895 i6 = i5; 5896 i3 = i4 ? i5 : i3; 5897 } 5898 i6 = HEAP32[72 >> 2] | 0; 5899 if (i3 >>> 0 < i6 >>> 0) { 5900 _abort(); 5901 } 5902 i4 = i3 + i12 | 0; 5903 if (!(i3 >>> 0 < i4 >>> 0)) { 5904 _abort(); 5905 } 5906 i5 = HEAP32[i3 + 24 >> 2] | 0; 5907 i7 = HEAP32[i3 + 12 >> 2] | 0; 5908 do { 5909 if ((i7 | 0) == (i3 | 0)) { 5910 i8 = i3 + 20 | 0; 5911 i7 = HEAP32[i8 >> 2] | 0; 5912 if ((i7 | 0) == 0) { 5913 i8 = i3 + 16 | 0; 5914 i7 = HEAP32[i8 >> 2] | 0; 5915 if ((i7 | 0) == 0) { 5916 i26 = 0; 5917 break; 5918 } 5919 } 5920 while (1) { 5921 i10 = i7 + 20 | 0; 5922 i9 = HEAP32[i10 >> 2] | 0; 5923 if ((i9 | 0) != 0) { 5924 i7 = i9; 5925 i8 = i10; 5926 continue; 5927 } 5928 i10 = i7 + 16 | 0; 5929 i9 = HEAP32[i10 >> 2] | 0; 5930 if ((i9 | 0) == 0) { 5931 break; 5932 } else { 5933 i7 = i9; 5934 i8 = i10; 5935 } 5936 } 5937 if (i8 >>> 0 < i6 >>> 0) { 5938 _abort(); 5939 } else { 5940 HEAP32[i8 >> 2] = 0; 5941 i26 = i7; 5942 break; 5943 } 5944 } else { 5945 i8 = HEAP32[i3 + 8 >> 2] | 0; 5946 if (i8 >>> 0 < i6 >>> 0) { 5947 _abort(); 5948 } 5949 i6 = i8 + 12 | 0; 5950 if ((HEAP32[i6 >> 2] | 0) != (i3 | 0)) { 5951 _abort(); 5952 } 5953 i9 = i7 + 8 | 0; 5954 if ((HEAP32[i9 >> 2] | 0) == (i3 | 0)) { 5955 HEAP32[i6 >> 2] = i7; 5956 HEAP32[i9 >> 2] = i8; 5957 i26 = i7; 5958 break; 5959 } else { 5960 _abort(); 5961 } 5962 } 5963 } while (0); 5964 do { 5965 if ((i5 | 0) != 0) { 5966 i7 = HEAP32[i3 + 28 >> 2] | 0; 5967 i6 = 360 + (i7 << 2) | 0; 5968 if ((i3 | 0) == (HEAP32[i6 >> 2] | 0)) { 5969 HEAP32[i6 >> 2] = i26; 5970 if ((i26 | 0) == 0) { 5971 HEAP32[60 >> 2] = HEAP32[60 >> 2] & ~(1 << i7); 5972 break; 5973 } 5974 } else { 5975 if (i5 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 5976 _abort(); 5977 } 5978 i6 = i5 + 16 | 0; 5979 if ((HEAP32[i6 >> 2] | 0) == (i3 | 0)) { 5980 HEAP32[i6 >> 2] = i26; 5981 } else { 5982 HEAP32[i5 + 20 >> 2] = i26; 5983 } 5984 if ((i26 | 0) == 0) { 5985 break; 5986 } 5987 } 5988 if (i26 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 5989 _abort(); 5990 } 5991 HEAP32[i26 + 24 >> 2] = i5; 5992 i5 = HEAP32[i3 + 16 >> 2] | 0; 5993 do { 5994 if ((i5 | 0) != 0) { 5995 if (i5 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 5996 _abort(); 5997 } else { 5998 HEAP32[i26 + 16 >> 2] = i5; 5999 HEAP32[i5 + 24 >> 2] = i26; 6000 break; 6001 } 6002 } 6003 } while (0); 6004 i5 = HEAP32[i3 + 20 >> 2] | 0; 6005 if ((i5 | 0) != 0) { 6006 if (i5 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 6007 _abort(); 6008 } else { 6009 HEAP32[i26 + 20 >> 2] = i5; 6010 HEAP32[i5 + 24 >> 2] = i26; 6011 break; 6012 } 6013 } 6014 } 6015 } while (0); 6016 if (i2 >>> 0 < 16) { 6017 i32 = i2 + i12 | 0; 6018 HEAP32[i3 + 4 >> 2] = i32 | 3; 6019 i32 = i3 + (i32 + 4) | 0; 6020 HEAP32[i32 >> 2] = HEAP32[i32 >> 2] | 1; 6021 } else { 6022 HEAP32[i3 + 4 >> 2] = i12 | 3; 6023 HEAP32[i3 + (i12 | 4) >> 2] = i2 | 1; 6024 HEAP32[i3 + (i2 + i12) >> 2] = i2; 6025 i6 = HEAP32[64 >> 2] | 0; 6026 if ((i6 | 0) != 0) { 6027 i5 = HEAP32[76 >> 2] | 0; 6028 i8 = i6 >>> 3; 6029 i9 = i8 << 1; 6030 i6 = 96 + (i9 << 2) | 0; 6031 i7 = HEAP32[14] | 0; 6032 i8 = 1 << i8; 6033 if ((i7 & i8 | 0) != 0) { 6034 i7 = 96 + (i9 + 2 << 2) | 0; 6035 i8 = HEAP32[i7 >> 2] | 0; 6036 if (i8 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 6037 _abort(); 6038 } else { 6039 i25 = i7; 6040 i24 = i8; 6041 } 6042 } else { 6043 HEAP32[14] = i7 | i8; 6044 i25 = 96 + (i9 + 2 << 2) | 0; 6045 i24 = i6; 6046 } 6047 HEAP32[i25 >> 2] = i5; 6048 HEAP32[i24 + 12 >> 2] = i5; 6049 HEAP32[i5 + 8 >> 2] = i24; 6050 HEAP32[i5 + 12 >> 2] = i6; 6051 } 6052 HEAP32[64 >> 2] = i2; 6053 HEAP32[76 >> 2] = i4; 6054 } 6055 i32 = i3 + 8 | 0; 6056 STACKTOP = i1; 6057 return i32 | 0; 6058 } 6059 } 6060 } else { 6061 if (!(i12 >>> 0 > 4294967231)) { 6062 i24 = i12 + 11 | 0; 6063 i12 = i24 & -8; 6064 i26 = HEAP32[60 >> 2] | 0; 6065 if ((i26 | 0) != 0) { 6066 i25 = 0 - i12 | 0; 6067 i24 = i24 >>> 8; 6068 if ((i24 | 0) != 0) { 6069 if (i12 >>> 0 > 16777215) { 6070 i27 = 31; 6071 } else { 6072 i31 = (i24 + 1048320 | 0) >>> 16 & 8; 6073 i32 = i24 << i31; 6074 i30 = (i32 + 520192 | 0) >>> 16 & 4; 6075 i32 = i32 << i30; 6076 i27 = (i32 + 245760 | 0) >>> 16 & 2; 6077 i27 = 14 - (i30 | i31 | i27) + (i32 << i27 >>> 15) | 0; 6078 i27 = i12 >>> (i27 + 7 | 0) & 1 | i27 << 1; 6079 } 6080 } else { 6081 i27 = 0; 6082 } 6083 i30 = HEAP32[360 + (i27 << 2) >> 2] | 0; 6084 L126 : do { 6085 if ((i30 | 0) == 0) { 6086 i29 = 0; 6087 i24 = 0; 6088 } else { 6089 if ((i27 | 0) == 31) { 6090 i24 = 0; 6091 } else { 6092 i24 = 25 - (i27 >>> 1) | 0; 6093 } 6094 i29 = 0; 6095 i28 = i12 << i24; 6096 i24 = 0; 6097 while (1) { 6098 i32 = HEAP32[i30 + 4 >> 2] & -8; 6099 i31 = i32 - i12 | 0; 6100 if (i31 >>> 0 < i25 >>> 0) { 6101 if ((i32 | 0) == (i12 | 0)) { 6102 i25 = i31; 6103 i29 = i30; 6104 i24 = i30; 6105 break L126; 6106 } else { 6107 i25 = i31; 6108 i24 = i30; 6109 } 6110 } 6111 i31 = HEAP32[i30 + 20 >> 2] | 0; 6112 i30 = HEAP32[i30 + (i28 >>> 31 << 2) + 16 >> 2] | 0; 6113 i29 = (i31 | 0) == 0 | (i31 | 0) == (i30 | 0) ? i29 : i31; 6114 if ((i30 | 0) == 0) { 6115 break; 6116 } else { 6117 i28 = i28 << 1; 6118 } 6119 } 6120 } 6121 } while (0); 6122 if ((i29 | 0) == 0 & (i24 | 0) == 0) { 6123 i32 = 2 << i27; 6124 i26 = i26 & (i32 | 0 - i32); 6125 if ((i26 | 0) == 0) { 6126 break; 6127 } 6128 i32 = (i26 & 0 - i26) + -1 | 0; 6129 i28 = i32 >>> 12 & 16; 6130 i32 = i32 >>> i28; 6131 i27 = i32 >>> 5 & 8; 6132 i32 = i32 >>> i27; 6133 i30 = i32 >>> 2 & 4; 6134 i32 = i32 >>> i30; 6135 i31 = i32 >>> 1 & 2; 6136 i32 = i32 >>> i31; 6137 i29 = i32 >>> 1 & 1; 6138 i29 = HEAP32[360 + ((i27 | i28 | i30 | i31 | i29) + (i32 >>> i29) << 2) >> 2] | 0; 6139 } 6140 if ((i29 | 0) != 0) { 6141 while (1) { 6142 i27 = (HEAP32[i29 + 4 >> 2] & -8) - i12 | 0; 6143 i26 = i27 >>> 0 < i25 >>> 0; 6144 i25 = i26 ? i27 : i25; 6145 i24 = i26 ? i29 : i24; 6146 i26 = HEAP32[i29 + 16 >> 2] | 0; 6147 if ((i26 | 0) != 0) { 6148 i29 = i26; 6149 continue; 6150 } 6151 i29 = HEAP32[i29 + 20 >> 2] | 0; 6152 if ((i29 | 0) == 0) { 6153 break; 6154 } 6155 } 6156 } 6157 if ((i24 | 0) != 0 ? i25 >>> 0 < ((HEAP32[64 >> 2] | 0) - i12 | 0) >>> 0 : 0) { 6158 i4 = HEAP32[72 >> 2] | 0; 6159 if (i24 >>> 0 < i4 >>> 0) { 6160 _abort(); 6161 } 6162 i2 = i24 + i12 | 0; 6163 if (!(i24 >>> 0 < i2 >>> 0)) { 6164 _abort(); 6165 } 6166 i3 = HEAP32[i24 + 24 >> 2] | 0; 6167 i6 = HEAP32[i24 + 12 >> 2] | 0; 6168 do { 6169 if ((i6 | 0) == (i24 | 0)) { 6170 i6 = i24 + 20 | 0; 6171 i5 = HEAP32[i6 >> 2] | 0; 6172 if ((i5 | 0) == 0) { 6173 i6 = i24 + 16 | 0; 6174 i5 = HEAP32[i6 >> 2] | 0; 6175 if ((i5 | 0) == 0) { 6176 i22 = 0; 6177 break; 6178 } 6179 } 6180 while (1) { 6181 i8 = i5 + 20 | 0; 6182 i7 = HEAP32[i8 >> 2] | 0; 6183 if ((i7 | 0) != 0) { 6184 i5 = i7; 6185 i6 = i8; 6186 continue; 6187 } 6188 i7 = i5 + 16 | 0; 6189 i8 = HEAP32[i7 >> 2] | 0; 6190 if ((i8 | 0) == 0) { 6191 break; 6192 } else { 6193 i5 = i8; 6194 i6 = i7; 6195 } 6196 } 6197 if (i6 >>> 0 < i4 >>> 0) { 6198 _abort(); 6199 } else { 6200 HEAP32[i6 >> 2] = 0; 6201 i22 = i5; 6202 break; 6203 } 6204 } else { 6205 i5 = HEAP32[i24 + 8 >> 2] | 0; 6206 if (i5 >>> 0 < i4 >>> 0) { 6207 _abort(); 6208 } 6209 i7 = i5 + 12 | 0; 6210 if ((HEAP32[i7 >> 2] | 0) != (i24 | 0)) { 6211 _abort(); 6212 } 6213 i4 = i6 + 8 | 0; 6214 if ((HEAP32[i4 >> 2] | 0) == (i24 | 0)) { 6215 HEAP32[i7 >> 2] = i6; 6216 HEAP32[i4 >> 2] = i5; 6217 i22 = i6; 6218 break; 6219 } else { 6220 _abort(); 6221 } 6222 } 6223 } while (0); 6224 do { 6225 if ((i3 | 0) != 0) { 6226 i4 = HEAP32[i24 + 28 >> 2] | 0; 6227 i5 = 360 + (i4 << 2) | 0; 6228 if ((i24 | 0) == (HEAP32[i5 >> 2] | 0)) { 6229 HEAP32[i5 >> 2] = i22; 6230 if ((i22 | 0) == 0) { 6231 HEAP32[60 >> 2] = HEAP32[60 >> 2] & ~(1 << i4); 6232 break; 6233 } 6234 } else { 6235 if (i3 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 6236 _abort(); 6237 } 6238 i4 = i3 + 16 | 0; 6239 if ((HEAP32[i4 >> 2] | 0) == (i24 | 0)) { 6240 HEAP32[i4 >> 2] = i22; 6241 } else { 6242 HEAP32[i3 + 20 >> 2] = i22; 6243 } 6244 if ((i22 | 0) == 0) { 6245 break; 6246 } 6247 } 6248 if (i22 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 6249 _abort(); 6250 } 6251 HEAP32[i22 + 24 >> 2] = i3; 6252 i3 = HEAP32[i24 + 16 >> 2] | 0; 6253 do { 6254 if ((i3 | 0) != 0) { 6255 if (i3 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 6256 _abort(); 6257 } else { 6258 HEAP32[i22 + 16 >> 2] = i3; 6259 HEAP32[i3 + 24 >> 2] = i22; 6260 break; 6261 } 6262 } 6263 } while (0); 6264 i3 = HEAP32[i24 + 20 >> 2] | 0; 6265 if ((i3 | 0) != 0) { 6266 if (i3 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 6267 _abort(); 6268 } else { 6269 HEAP32[i22 + 20 >> 2] = i3; 6270 HEAP32[i3 + 24 >> 2] = i22; 6271 break; 6272 } 6273 } 6274 } 6275 } while (0); 6276 L204 : do { 6277 if (!(i25 >>> 0 < 16)) { 6278 HEAP32[i24 + 4 >> 2] = i12 | 3; 6279 HEAP32[i24 + (i12 | 4) >> 2] = i25 | 1; 6280 HEAP32[i24 + (i25 + i12) >> 2] = i25; 6281 i4 = i25 >>> 3; 6282 if (i25 >>> 0 < 256) { 6283 i6 = i4 << 1; 6284 i3 = 96 + (i6 << 2) | 0; 6285 i5 = HEAP32[14] | 0; 6286 i4 = 1 << i4; 6287 if ((i5 & i4 | 0) != 0) { 6288 i5 = 96 + (i6 + 2 << 2) | 0; 6289 i4 = HEAP32[i5 >> 2] | 0; 6290 if (i4 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 6291 _abort(); 6292 } else { 6293 i21 = i5; 6294 i20 = i4; 6295 } 6296 } else { 6297 HEAP32[14] = i5 | i4; 6298 i21 = 96 + (i6 + 2 << 2) | 0; 6299 i20 = i3; 6300 } 6301 HEAP32[i21 >> 2] = i2; 6302 HEAP32[i20 + 12 >> 2] = i2; 6303 HEAP32[i24 + (i12 + 8) >> 2] = i20; 6304 HEAP32[i24 + (i12 + 12) >> 2] = i3; 6305 break; 6306 } 6307 i3 = i25 >>> 8; 6308 if ((i3 | 0) != 0) { 6309 if (i25 >>> 0 > 16777215) { 6310 i3 = 31; 6311 } else { 6312 i31 = (i3 + 1048320 | 0) >>> 16 & 8; 6313 i32 = i3 << i31; 6314 i30 = (i32 + 520192 | 0) >>> 16 & 4; 6315 i32 = i32 << i30; 6316 i3 = (i32 + 245760 | 0) >>> 16 & 2; 6317 i3 = 14 - (i30 | i31 | i3) + (i32 << i3 >>> 15) | 0; 6318 i3 = i25 >>> (i3 + 7 | 0) & 1 | i3 << 1; 6319 } 6320 } else { 6321 i3 = 0; 6322 } 6323 i6 = 360 + (i3 << 2) | 0; 6324 HEAP32[i24 + (i12 + 28) >> 2] = i3; 6325 HEAP32[i24 + (i12 + 20) >> 2] = 0; 6326 HEAP32[i24 + (i12 + 16) >> 2] = 0; 6327 i4 = HEAP32[60 >> 2] | 0; 6328 i5 = 1 << i3; 6329 if ((i4 & i5 | 0) == 0) { 6330 HEAP32[60 >> 2] = i4 | i5; 6331 HEAP32[i6 >> 2] = i2; 6332 HEAP32[i24 + (i12 + 24) >> 2] = i6; 6333 HEAP32[i24 + (i12 + 12) >> 2] = i2; 6334 HEAP32[i24 + (i12 + 8) >> 2] = i2; 6335 break; 6336 } 6337 i4 = HEAP32[i6 >> 2] | 0; 6338 if ((i3 | 0) == 31) { 6339 i3 = 0; 6340 } else { 6341 i3 = 25 - (i3 >>> 1) | 0; 6342 } 6343 L225 : do { 6344 if ((HEAP32[i4 + 4 >> 2] & -8 | 0) != (i25 | 0)) { 6345 i3 = i25 << i3; 6346 while (1) { 6347 i6 = i4 + (i3 >>> 31 << 2) + 16 | 0; 6348 i5 = HEAP32[i6 >> 2] | 0; 6349 if ((i5 | 0) == 0) { 6350 break; 6351 } 6352 if ((HEAP32[i5 + 4 >> 2] & -8 | 0) == (i25 | 0)) { 6353 i18 = i5; 6354 break L225; 6355 } else { 6356 i3 = i3 << 1; 6357 i4 = i5; 6358 } 6359 } 6360 if (i6 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 6361 _abort(); 6362 } else { 6363 HEAP32[i6 >> 2] = i2; 6364 HEAP32[i24 + (i12 + 24) >> 2] = i4; 6365 HEAP32[i24 + (i12 + 12) >> 2] = i2; 6366 HEAP32[i24 + (i12 + 8) >> 2] = i2; 6367 break L204; 6368 } 6369 } else { 6370 i18 = i4; 6371 } 6372 } while (0); 6373 i4 = i18 + 8 | 0; 6374 i3 = HEAP32[i4 >> 2] | 0; 6375 i5 = HEAP32[72 >> 2] | 0; 6376 if (i18 >>> 0 < i5 >>> 0) { 6377 _abort(); 6378 } 6379 if (i3 >>> 0 < i5 >>> 0) { 6380 _abort(); 6381 } else { 6382 HEAP32[i3 + 12 >> 2] = i2; 6383 HEAP32[i4 >> 2] = i2; 6384 HEAP32[i24 + (i12 + 8) >> 2] = i3; 6385 HEAP32[i24 + (i12 + 12) >> 2] = i18; 6386 HEAP32[i24 + (i12 + 24) >> 2] = 0; 6387 break; 6388 } 6389 } else { 6390 i32 = i25 + i12 | 0; 6391 HEAP32[i24 + 4 >> 2] = i32 | 3; 6392 i32 = i24 + (i32 + 4) | 0; 6393 HEAP32[i32 >> 2] = HEAP32[i32 >> 2] | 1; 6394 } 6395 } while (0); 6396 i32 = i24 + 8 | 0; 6397 STACKTOP = i1; 6398 return i32 | 0; 6399 } 6400 } 6401 } else { 6402 i12 = -1; 6403 } 6404 } 6405 } while (0); 6406 i18 = HEAP32[64 >> 2] | 0; 6407 if (!(i12 >>> 0 > i18 >>> 0)) { 6408 i3 = i18 - i12 | 0; 6409 i2 = HEAP32[76 >> 2] | 0; 6410 if (i3 >>> 0 > 15) { 6411 HEAP32[76 >> 2] = i2 + i12; 6412 HEAP32[64 >> 2] = i3; 6413 HEAP32[i2 + (i12 + 4) >> 2] = i3 | 1; 6414 HEAP32[i2 + i18 >> 2] = i3; 6415 HEAP32[i2 + 4 >> 2] = i12 | 3; 6416 } else { 6417 HEAP32[64 >> 2] = 0; 6418 HEAP32[76 >> 2] = 0; 6419 HEAP32[i2 + 4 >> 2] = i18 | 3; 6420 i32 = i2 + (i18 + 4) | 0; 6421 HEAP32[i32 >> 2] = HEAP32[i32 >> 2] | 1; 6422 } 6423 i32 = i2 + 8 | 0; 6424 STACKTOP = i1; 6425 return i32 | 0; 6426 } 6427 i18 = HEAP32[68 >> 2] | 0; 6428 if (i12 >>> 0 < i18 >>> 0) { 6429 i31 = i18 - i12 | 0; 6430 HEAP32[68 >> 2] = i31; 6431 i32 = HEAP32[80 >> 2] | 0; 6432 HEAP32[80 >> 2] = i32 + i12; 6433 HEAP32[i32 + (i12 + 4) >> 2] = i31 | 1; 6434 HEAP32[i32 + 4 >> 2] = i12 | 3; 6435 i32 = i32 + 8 | 0; 6436 STACKTOP = i1; 6437 return i32 | 0; 6438 } 6439 do { 6440 if ((HEAP32[132] | 0) == 0) { 6441 i18 = _sysconf(30) | 0; 6442 if ((i18 + -1 & i18 | 0) == 0) { 6443 HEAP32[536 >> 2] = i18; 6444 HEAP32[532 >> 2] = i18; 6445 HEAP32[540 >> 2] = -1; 6446 HEAP32[544 >> 2] = -1; 6447 HEAP32[548 >> 2] = 0; 6448 HEAP32[500 >> 2] = 0; 6449 HEAP32[132] = (_time(0) | 0) & -16 ^ 1431655768; 6450 break; 6451 } else { 6452 _abort(); 6453 } 6454 } 6455 } while (0); 6456 i20 = i12 + 48 | 0; 6457 i25 = HEAP32[536 >> 2] | 0; 6458 i21 = i12 + 47 | 0; 6459 i22 = i25 + i21 | 0; 6460 i25 = 0 - i25 | 0; 6461 i18 = i22 & i25; 6462 if (!(i18 >>> 0 > i12 >>> 0)) { 6463 i32 = 0; 6464 STACKTOP = i1; 6465 return i32 | 0; 6466 } 6467 i24 = HEAP32[496 >> 2] | 0; 6468 if ((i24 | 0) != 0 ? (i31 = HEAP32[488 >> 2] | 0, i32 = i31 + i18 | 0, i32 >>> 0 <= i31 >>> 0 | i32 >>> 0 > i24 >>> 0) : 0) { 6469 i32 = 0; 6470 STACKTOP = i1; 6471 return i32 | 0; 6472 } 6473 L269 : do { 6474 if ((HEAP32[500 >> 2] & 4 | 0) == 0) { 6475 i26 = HEAP32[80 >> 2] | 0; 6476 L271 : do { 6477 if ((i26 | 0) != 0) { 6478 i24 = 504 | 0; 6479 while (1) { 6480 i27 = HEAP32[i24 >> 2] | 0; 6481 if (!(i27 >>> 0 > i26 >>> 0) ? (i23 = i24 + 4 | 0, (i27 + (HEAP32[i23 >> 2] | 0) | 0) >>> 0 > i26 >>> 0) : 0) { 6482 break; 6483 } 6484 i24 = HEAP32[i24 + 8 >> 2] | 0; 6485 if ((i24 | 0) == 0) { 6486 i13 = 182; 6487 break L271; 6488 } 6489 } 6490 if ((i24 | 0) != 0) { 6491 i25 = i22 - (HEAP32[68 >> 2] | 0) & i25; 6492 if (i25 >>> 0 < 2147483647) { 6493 i13 = _sbrk(i25 | 0) | 0; 6494 i26 = (i13 | 0) == ((HEAP32[i24 >> 2] | 0) + (HEAP32[i23 >> 2] | 0) | 0); 6495 i22 = i13; 6496 i24 = i25; 6497 i23 = i26 ? i13 : -1; 6498 i25 = i26 ? i25 : 0; 6499 i13 = 191; 6500 } else { 6501 i25 = 0; 6502 } 6503 } else { 6504 i13 = 182; 6505 } 6506 } else { 6507 i13 = 182; 6508 } 6509 } while (0); 6510 do { 6511 if ((i13 | 0) == 182) { 6512 i23 = _sbrk(0) | 0; 6513 if ((i23 | 0) != (-1 | 0)) { 6514 i24 = i23; 6515 i22 = HEAP32[532 >> 2] | 0; 6516 i25 = i22 + -1 | 0; 6517 if ((i25 & i24 | 0) == 0) { 6518 i25 = i18; 6519 } else { 6520 i25 = i18 - i24 + (i25 + i24 & 0 - i22) | 0; 6521 } 6522 i24 = HEAP32[488 >> 2] | 0; 6523 i26 = i24 + i25 | 0; 6524 if (i25 >>> 0 > i12 >>> 0 & i25 >>> 0 < 2147483647) { 6525 i22 = HEAP32[496 >> 2] | 0; 6526 if ((i22 | 0) != 0 ? i26 >>> 0 <= i24 >>> 0 | i26 >>> 0 > i22 >>> 0 : 0) { 6527 i25 = 0; 6528 break; 6529 } 6530 i22 = _sbrk(i25 | 0) | 0; 6531 i13 = (i22 | 0) == (i23 | 0); 6532 i24 = i25; 6533 i23 = i13 ? i23 : -1; 6534 i25 = i13 ? i25 : 0; 6535 i13 = 191; 6536 } else { 6537 i25 = 0; 6538 } 6539 } else { 6540 i25 = 0; 6541 } 6542 } 6543 } while (0); 6544 L291 : do { 6545 if ((i13 | 0) == 191) { 6546 i13 = 0 - i24 | 0; 6547 if ((i23 | 0) != (-1 | 0)) { 6548 i17 = i23; 6549 i14 = i25; 6550 i13 = 202; 6551 break L269; 6552 } 6553 do { 6554 if ((i22 | 0) != (-1 | 0) & i24 >>> 0 < 2147483647 & i24 >>> 0 < i20 >>> 0 ? (i19 = HEAP32[536 >> 2] | 0, i19 = i21 - i24 + i19 & 0 - i19, i19 >>> 0 < 2147483647) : 0) { 6555 if ((_sbrk(i19 | 0) | 0) == (-1 | 0)) { 6556 _sbrk(i13 | 0) | 0; 6557 break L291; 6558 } else { 6559 i24 = i19 + i24 | 0; 6560 break; 6561 } 6562 } 6563 } while (0); 6564 if ((i22 | 0) != (-1 | 0)) { 6565 i17 = i22; 6566 i14 = i24; 6567 i13 = 202; 6568 break L269; 6569 } 6570 } 6571 } while (0); 6572 HEAP32[500 >> 2] = HEAP32[500 >> 2] | 4; 6573 i13 = 199; 6574 } else { 6575 i25 = 0; 6576 i13 = 199; 6577 } 6578 } while (0); 6579 if ((((i13 | 0) == 199 ? i18 >>> 0 < 2147483647 : 0) ? (i17 = _sbrk(i18 | 0) | 0, i16 = _sbrk(0) | 0, (i16 | 0) != (-1 | 0) & (i17 | 0) != (-1 | 0) & i17 >>> 0 < i16 >>> 0) : 0) ? (i15 = i16 - i17 | 0, i14 = i15 >>> 0 > (i12 + 40 | 0) >>> 0, i14) : 0) { 6580 i14 = i14 ? i15 : i25; 6581 i13 = 202; 6582 } 6583 if ((i13 | 0) == 202) { 6584 i15 = (HEAP32[488 >> 2] | 0) + i14 | 0; 6585 HEAP32[488 >> 2] = i15; 6586 if (i15 >>> 0 > (HEAP32[492 >> 2] | 0) >>> 0) { 6587 HEAP32[492 >> 2] = i15; 6588 } 6589 i15 = HEAP32[80 >> 2] | 0; 6590 L311 : do { 6591 if ((i15 | 0) != 0) { 6592 i21 = 504 | 0; 6593 while (1) { 6594 i16 = HEAP32[i21 >> 2] | 0; 6595 i19 = i21 + 4 | 0; 6596 i20 = HEAP32[i19 >> 2] | 0; 6597 if ((i17 | 0) == (i16 + i20 | 0)) { 6598 i13 = 214; 6599 break; 6600 } 6601 i18 = HEAP32[i21 + 8 >> 2] | 0; 6602 if ((i18 | 0) == 0) { 6603 break; 6604 } else { 6605 i21 = i18; 6606 } 6607 } 6608 if (((i13 | 0) == 214 ? (HEAP32[i21 + 12 >> 2] & 8 | 0) == 0 : 0) ? i15 >>> 0 >= i16 >>> 0 & i15 >>> 0 < i17 >>> 0 : 0) { 6609 HEAP32[i19 >> 2] = i20 + i14; 6610 i2 = (HEAP32[68 >> 2] | 0) + i14 | 0; 6611 i3 = i15 + 8 | 0; 6612 if ((i3 & 7 | 0) == 0) { 6613 i3 = 0; 6614 } else { 6615 i3 = 0 - i3 & 7; 6616 } 6617 i32 = i2 - i3 | 0; 6618 HEAP32[80 >> 2] = i15 + i3; 6619 HEAP32[68 >> 2] = i32; 6620 HEAP32[i15 + (i3 + 4) >> 2] = i32 | 1; 6621 HEAP32[i15 + (i2 + 4) >> 2] = 40; 6622 HEAP32[84 >> 2] = HEAP32[544 >> 2]; 6623 break; 6624 } 6625 if (i17 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 6626 HEAP32[72 >> 2] = i17; 6627 } 6628 i19 = i17 + i14 | 0; 6629 i16 = 504 | 0; 6630 while (1) { 6631 if ((HEAP32[i16 >> 2] | 0) == (i19 | 0)) { 6632 i13 = 224; 6633 break; 6634 } 6635 i18 = HEAP32[i16 + 8 >> 2] | 0; 6636 if ((i18 | 0) == 0) { 6637 break; 6638 } else { 6639 i16 = i18; 6640 } 6641 } 6642 if ((i13 | 0) == 224 ? (HEAP32[i16 + 12 >> 2] & 8 | 0) == 0 : 0) { 6643 HEAP32[i16 >> 2] = i17; 6644 i6 = i16 + 4 | 0; 6645 HEAP32[i6 >> 2] = (HEAP32[i6 >> 2] | 0) + i14; 6646 i6 = i17 + 8 | 0; 6647 if ((i6 & 7 | 0) == 0) { 6648 i6 = 0; 6649 } else { 6650 i6 = 0 - i6 & 7; 6651 } 6652 i7 = i17 + (i14 + 8) | 0; 6653 if ((i7 & 7 | 0) == 0) { 6654 i13 = 0; 6655 } else { 6656 i13 = 0 - i7 & 7; 6657 } 6658 i15 = i17 + (i13 + i14) | 0; 6659 i8 = i6 + i12 | 0; 6660 i7 = i17 + i8 | 0; 6661 i10 = i15 - (i17 + i6) - i12 | 0; 6662 HEAP32[i17 + (i6 + 4) >> 2] = i12 | 3; 6663 L348 : do { 6664 if ((i15 | 0) != (HEAP32[80 >> 2] | 0)) { 6665 if ((i15 | 0) == (HEAP32[76 >> 2] | 0)) { 6666 i32 = (HEAP32[64 >> 2] | 0) + i10 | 0; 6667 HEAP32[64 >> 2] = i32; 6668 HEAP32[76 >> 2] = i7; 6669 HEAP32[i17 + (i8 + 4) >> 2] = i32 | 1; 6670 HEAP32[i17 + (i32 + i8) >> 2] = i32; 6671 break; 6672 } 6673 i12 = i14 + 4 | 0; 6674 i18 = HEAP32[i17 + (i12 + i13) >> 2] | 0; 6675 if ((i18 & 3 | 0) == 1) { 6676 i11 = i18 & -8; 6677 i16 = i18 >>> 3; 6678 do { 6679 if (!(i18 >>> 0 < 256)) { 6680 i9 = HEAP32[i17 + ((i13 | 24) + i14) >> 2] | 0; 6681 i19 = HEAP32[i17 + (i14 + 12 + i13) >> 2] | 0; 6682 do { 6683 if ((i19 | 0) == (i15 | 0)) { 6684 i19 = i13 | 16; 6685 i18 = i17 + (i12 + i19) | 0; 6686 i16 = HEAP32[i18 >> 2] | 0; 6687 if ((i16 | 0) == 0) { 6688 i18 = i17 + (i19 + i14) | 0; 6689 i16 = HEAP32[i18 >> 2] | 0; 6690 if ((i16 | 0) == 0) { 6691 i5 = 0; 6692 break; 6693 } 6694 } 6695 while (1) { 6696 i20 = i16 + 20 | 0; 6697 i19 = HEAP32[i20 >> 2] | 0; 6698 if ((i19 | 0) != 0) { 6699 i16 = i19; 6700 i18 = i20; 6701 continue; 6702 } 6703 i19 = i16 + 16 | 0; 6704 i20 = HEAP32[i19 >> 2] | 0; 6705 if ((i20 | 0) == 0) { 6706 break; 6707 } else { 6708 i16 = i20; 6709 i18 = i19; 6710 } 6711 } 6712 if (i18 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 6713 _abort(); 6714 } else { 6715 HEAP32[i18 >> 2] = 0; 6716 i5 = i16; 6717 break; 6718 } 6719 } else { 6720 i18 = HEAP32[i17 + ((i13 | 8) + i14) >> 2] | 0; 6721 if (i18 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 6722 _abort(); 6723 } 6724 i16 = i18 + 12 | 0; 6725 if ((HEAP32[i16 >> 2] | 0) != (i15 | 0)) { 6726 _abort(); 6727 } 6728 i20 = i19 + 8 | 0; 6729 if ((HEAP32[i20 >> 2] | 0) == (i15 | 0)) { 6730 HEAP32[i16 >> 2] = i19; 6731 HEAP32[i20 >> 2] = i18; 6732 i5 = i19; 6733 break; 6734 } else { 6735 _abort(); 6736 } 6737 } 6738 } while (0); 6739 if ((i9 | 0) != 0) { 6740 i16 = HEAP32[i17 + (i14 + 28 + i13) >> 2] | 0; 6741 i18 = 360 + (i16 << 2) | 0; 6742 if ((i15 | 0) == (HEAP32[i18 >> 2] | 0)) { 6743 HEAP32[i18 >> 2] = i5; 6744 if ((i5 | 0) == 0) { 6745 HEAP32[60 >> 2] = HEAP32[60 >> 2] & ~(1 << i16); 6746 break; 6747 } 6748 } else { 6749 if (i9 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 6750 _abort(); 6751 } 6752 i16 = i9 + 16 | 0; 6753 if ((HEAP32[i16 >> 2] | 0) == (i15 | 0)) { 6754 HEAP32[i16 >> 2] = i5; 6755 } else { 6756 HEAP32[i9 + 20 >> 2] = i5; 6757 } 6758 if ((i5 | 0) == 0) { 6759 break; 6760 } 6761 } 6762 if (i5 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 6763 _abort(); 6764 } 6765 HEAP32[i5 + 24 >> 2] = i9; 6766 i15 = i13 | 16; 6767 i9 = HEAP32[i17 + (i15 + i14) >> 2] | 0; 6768 do { 6769 if ((i9 | 0) != 0) { 6770 if (i9 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 6771 _abort(); 6772 } else { 6773 HEAP32[i5 + 16 >> 2] = i9; 6774 HEAP32[i9 + 24 >> 2] = i5; 6775 break; 6776 } 6777 } 6778 } while (0); 6779 i9 = HEAP32[i17 + (i12 + i15) >> 2] | 0; 6780 if ((i9 | 0) != 0) { 6781 if (i9 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 6782 _abort(); 6783 } else { 6784 HEAP32[i5 + 20 >> 2] = i9; 6785 HEAP32[i9 + 24 >> 2] = i5; 6786 break; 6787 } 6788 } 6789 } 6790 } else { 6791 i5 = HEAP32[i17 + ((i13 | 8) + i14) >> 2] | 0; 6792 i12 = HEAP32[i17 + (i14 + 12 + i13) >> 2] | 0; 6793 i18 = 96 + (i16 << 1 << 2) | 0; 6794 if ((i5 | 0) != (i18 | 0)) { 6795 if (i5 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 6796 _abort(); 6797 } 6798 if ((HEAP32[i5 + 12 >> 2] | 0) != (i15 | 0)) { 6799 _abort(); 6800 } 6801 } 6802 if ((i12 | 0) == (i5 | 0)) { 6803 HEAP32[14] = HEAP32[14] & ~(1 << i16); 6804 break; 6805 } 6806 if ((i12 | 0) != (i18 | 0)) { 6807 if (i12 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 6808 _abort(); 6809 } 6810 i16 = i12 + 8 | 0; 6811 if ((HEAP32[i16 >> 2] | 0) == (i15 | 0)) { 6812 i9 = i16; 6813 } else { 6814 _abort(); 6815 } 6816 } else { 6817 i9 = i12 + 8 | 0; 6818 } 6819 HEAP32[i5 + 12 >> 2] = i12; 6820 HEAP32[i9 >> 2] = i5; 6821 } 6822 } while (0); 6823 i15 = i17 + ((i11 | i13) + i14) | 0; 6824 i10 = i11 + i10 | 0; 6825 } 6826 i5 = i15 + 4 | 0; 6827 HEAP32[i5 >> 2] = HEAP32[i5 >> 2] & -2; 6828 HEAP32[i17 + (i8 + 4) >> 2] = i10 | 1; 6829 HEAP32[i17 + (i10 + i8) >> 2] = i10; 6830 i5 = i10 >>> 3; 6831 if (i10 >>> 0 < 256) { 6832 i10 = i5 << 1; 6833 i2 = 96 + (i10 << 2) | 0; 6834 i9 = HEAP32[14] | 0; 6835 i5 = 1 << i5; 6836 if ((i9 & i5 | 0) != 0) { 6837 i9 = 96 + (i10 + 2 << 2) | 0; 6838 i5 = HEAP32[i9 >> 2] | 0; 6839 if (i5 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 6840 _abort(); 6841 } else { 6842 i3 = i9; 6843 i4 = i5; 6844 } 6845 } else { 6846 HEAP32[14] = i9 | i5; 6847 i3 = 96 + (i10 + 2 << 2) | 0; 6848 i4 = i2; 6849 } 6850 HEAP32[i3 >> 2] = i7; 6851 HEAP32[i4 + 12 >> 2] = i7; 6852 HEAP32[i17 + (i8 + 8) >> 2] = i4; 6853 HEAP32[i17 + (i8 + 12) >> 2] = i2; 6854 break; 6855 } 6856 i3 = i10 >>> 8; 6857 if ((i3 | 0) != 0) { 6858 if (i10 >>> 0 > 16777215) { 6859 i3 = 31; 6860 } else { 6861 i31 = (i3 + 1048320 | 0) >>> 16 & 8; 6862 i32 = i3 << i31; 6863 i30 = (i32 + 520192 | 0) >>> 16 & 4; 6864 i32 = i32 << i30; 6865 i3 = (i32 + 245760 | 0) >>> 16 & 2; 6866 i3 = 14 - (i30 | i31 | i3) + (i32 << i3 >>> 15) | 0; 6867 i3 = i10 >>> (i3 + 7 | 0) & 1 | i3 << 1; 6868 } 6869 } else { 6870 i3 = 0; 6871 } 6872 i4 = 360 + (i3 << 2) | 0; 6873 HEAP32[i17 + (i8 + 28) >> 2] = i3; 6874 HEAP32[i17 + (i8 + 20) >> 2] = 0; 6875 HEAP32[i17 + (i8 + 16) >> 2] = 0; 6876 i9 = HEAP32[60 >> 2] | 0; 6877 i5 = 1 << i3; 6878 if ((i9 & i5 | 0) == 0) { 6879 HEAP32[60 >> 2] = i9 | i5; 6880 HEAP32[i4 >> 2] = i7; 6881 HEAP32[i17 + (i8 + 24) >> 2] = i4; 6882 HEAP32[i17 + (i8 + 12) >> 2] = i7; 6883 HEAP32[i17 + (i8 + 8) >> 2] = i7; 6884 break; 6885 } 6886 i4 = HEAP32[i4 >> 2] | 0; 6887 if ((i3 | 0) == 31) { 6888 i3 = 0; 6889 } else { 6890 i3 = 25 - (i3 >>> 1) | 0; 6891 } 6892 L444 : do { 6893 if ((HEAP32[i4 + 4 >> 2] & -8 | 0) != (i10 | 0)) { 6894 i3 = i10 << i3; 6895 while (1) { 6896 i5 = i4 + (i3 >>> 31 << 2) + 16 | 0; 6897 i9 = HEAP32[i5 >> 2] | 0; 6898 if ((i9 | 0) == 0) { 6899 break; 6900 } 6901 if ((HEAP32[i9 + 4 >> 2] & -8 | 0) == (i10 | 0)) { 6902 i2 = i9; 6903 break L444; 6904 } else { 6905 i3 = i3 << 1; 6906 i4 = i9; 6907 } 6908 } 6909 if (i5 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 6910 _abort(); 6911 } else { 6912 HEAP32[i5 >> 2] = i7; 6913 HEAP32[i17 + (i8 + 24) >> 2] = i4; 6914 HEAP32[i17 + (i8 + 12) >> 2] = i7; 6915 HEAP32[i17 + (i8 + 8) >> 2] = i7; 6916 break L348; 6917 } 6918 } else { 6919 i2 = i4; 6920 } 6921 } while (0); 6922 i4 = i2 + 8 | 0; 6923 i3 = HEAP32[i4 >> 2] | 0; 6924 i5 = HEAP32[72 >> 2] | 0; 6925 if (i2 >>> 0 < i5 >>> 0) { 6926 _abort(); 6927 } 6928 if (i3 >>> 0 < i5 >>> 0) { 6929 _abort(); 6930 } else { 6931 HEAP32[i3 + 12 >> 2] = i7; 6932 HEAP32[i4 >> 2] = i7; 6933 HEAP32[i17 + (i8 + 8) >> 2] = i3; 6934 HEAP32[i17 + (i8 + 12) >> 2] = i2; 6935 HEAP32[i17 + (i8 + 24) >> 2] = 0; 6936 break; 6937 } 6938 } else { 6939 i32 = (HEAP32[68 >> 2] | 0) + i10 | 0; 6940 HEAP32[68 >> 2] = i32; 6941 HEAP32[80 >> 2] = i7; 6942 HEAP32[i17 + (i8 + 4) >> 2] = i32 | 1; 6943 } 6944 } while (0); 6945 i32 = i17 + (i6 | 8) | 0; 6946 STACKTOP = i1; 6947 return i32 | 0; 6948 } 6949 i3 = 504 | 0; 6950 while (1) { 6951 i2 = HEAP32[i3 >> 2] | 0; 6952 if (!(i2 >>> 0 > i15 >>> 0) ? (i11 = HEAP32[i3 + 4 >> 2] | 0, i10 = i2 + i11 | 0, i10 >>> 0 > i15 >>> 0) : 0) { 6953 break; 6954 } 6955 i3 = HEAP32[i3 + 8 >> 2] | 0; 6956 } 6957 i3 = i2 + (i11 + -39) | 0; 6958 if ((i3 & 7 | 0) == 0) { 6959 i3 = 0; 6960 } else { 6961 i3 = 0 - i3 & 7; 6962 } 6963 i2 = i2 + (i11 + -47 + i3) | 0; 6964 i2 = i2 >>> 0 < (i15 + 16 | 0) >>> 0 ? i15 : i2; 6965 i3 = i2 + 8 | 0; 6966 i4 = i17 + 8 | 0; 6967 if ((i4 & 7 | 0) == 0) { 6968 i4 = 0; 6969 } else { 6970 i4 = 0 - i4 & 7; 6971 } 6972 i32 = i14 + -40 - i4 | 0; 6973 HEAP32[80 >> 2] = i17 + i4; 6974 HEAP32[68 >> 2] = i32; 6975 HEAP32[i17 + (i4 + 4) >> 2] = i32 | 1; 6976 HEAP32[i17 + (i14 + -36) >> 2] = 40; 6977 HEAP32[84 >> 2] = HEAP32[544 >> 2]; 6978 HEAP32[i2 + 4 >> 2] = 27; 6979 HEAP32[i3 + 0 >> 2] = HEAP32[504 >> 2]; 6980 HEAP32[i3 + 4 >> 2] = HEAP32[508 >> 2]; 6981 HEAP32[i3 + 8 >> 2] = HEAP32[512 >> 2]; 6982 HEAP32[i3 + 12 >> 2] = HEAP32[516 >> 2]; 6983 HEAP32[504 >> 2] = i17; 6984 HEAP32[508 >> 2] = i14; 6985 HEAP32[516 >> 2] = 0; 6986 HEAP32[512 >> 2] = i3; 6987 i4 = i2 + 28 | 0; 6988 HEAP32[i4 >> 2] = 7; 6989 if ((i2 + 32 | 0) >>> 0 < i10 >>> 0) { 6990 while (1) { 6991 i3 = i4 + 4 | 0; 6992 HEAP32[i3 >> 2] = 7; 6993 if ((i4 + 8 | 0) >>> 0 < i10 >>> 0) { 6994 i4 = i3; 6995 } else { 6996 break; 6997 } 6998 } 6999 } 7000 if ((i2 | 0) != (i15 | 0)) { 7001 i2 = i2 - i15 | 0; 7002 i3 = i15 + (i2 + 4) | 0; 7003 HEAP32[i3 >> 2] = HEAP32[i3 >> 2] & -2; 7004 HEAP32[i15 + 4 >> 2] = i2 | 1; 7005 HEAP32[i15 + i2 >> 2] = i2; 7006 i3 = i2 >>> 3; 7007 if (i2 >>> 0 < 256) { 7008 i4 = i3 << 1; 7009 i2 = 96 + (i4 << 2) | 0; 7010 i5 = HEAP32[14] | 0; 7011 i3 = 1 << i3; 7012 if ((i5 & i3 | 0) != 0) { 7013 i4 = 96 + (i4 + 2 << 2) | 0; 7014 i3 = HEAP32[i4 >> 2] | 0; 7015 if (i3 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 7016 _abort(); 7017 } else { 7018 i7 = i4; 7019 i8 = i3; 7020 } 7021 } else { 7022 HEAP32[14] = i5 | i3; 7023 i7 = 96 + (i4 + 2 << 2) | 0; 7024 i8 = i2; 7025 } 7026 HEAP32[i7 >> 2] = i15; 7027 HEAP32[i8 + 12 >> 2] = i15; 7028 HEAP32[i15 + 8 >> 2] = i8; 7029 HEAP32[i15 + 12 >> 2] = i2; 7030 break; 7031 } 7032 i3 = i2 >>> 8; 7033 if ((i3 | 0) != 0) { 7034 if (i2 >>> 0 > 16777215) { 7035 i3 = 31; 7036 } else { 7037 i31 = (i3 + 1048320 | 0) >>> 16 & 8; 7038 i32 = i3 << i31; 7039 i30 = (i32 + 520192 | 0) >>> 16 & 4; 7040 i32 = i32 << i30; 7041 i3 = (i32 + 245760 | 0) >>> 16 & 2; 7042 i3 = 14 - (i30 | i31 | i3) + (i32 << i3 >>> 15) | 0; 7043 i3 = i2 >>> (i3 + 7 | 0) & 1 | i3 << 1; 7044 } 7045 } else { 7046 i3 = 0; 7047 } 7048 i7 = 360 + (i3 << 2) | 0; 7049 HEAP32[i15 + 28 >> 2] = i3; 7050 HEAP32[i15 + 20 >> 2] = 0; 7051 HEAP32[i15 + 16 >> 2] = 0; 7052 i4 = HEAP32[60 >> 2] | 0; 7053 i5 = 1 << i3; 7054 if ((i4 & i5 | 0) == 0) { 7055 HEAP32[60 >> 2] = i4 | i5; 7056 HEAP32[i7 >> 2] = i15; 7057 HEAP32[i15 + 24 >> 2] = i7; 7058 HEAP32[i15 + 12 >> 2] = i15; 7059 HEAP32[i15 + 8 >> 2] = i15; 7060 break; 7061 } 7062 i4 = HEAP32[i7 >> 2] | 0; 7063 if ((i3 | 0) == 31) { 7064 i3 = 0; 7065 } else { 7066 i3 = 25 - (i3 >>> 1) | 0; 7067 } 7068 L499 : do { 7069 if ((HEAP32[i4 + 4 >> 2] & -8 | 0) != (i2 | 0)) { 7070 i3 = i2 << i3; 7071 while (1) { 7072 i7 = i4 + (i3 >>> 31 << 2) + 16 | 0; 7073 i5 = HEAP32[i7 >> 2] | 0; 7074 if ((i5 | 0) == 0) { 7075 break; 7076 } 7077 if ((HEAP32[i5 + 4 >> 2] & -8 | 0) == (i2 | 0)) { 7078 i6 = i5; 7079 break L499; 7080 } else { 7081 i3 = i3 << 1; 7082 i4 = i5; 7083 } 7084 } 7085 if (i7 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 7086 _abort(); 7087 } else { 7088 HEAP32[i7 >> 2] = i15; 7089 HEAP32[i15 + 24 >> 2] = i4; 7090 HEAP32[i15 + 12 >> 2] = i15; 7091 HEAP32[i15 + 8 >> 2] = i15; 7092 break L311; 7093 } 7094 } else { 7095 i6 = i4; 7096 } 7097 } while (0); 7098 i4 = i6 + 8 | 0; 7099 i3 = HEAP32[i4 >> 2] | 0; 7100 i2 = HEAP32[72 >> 2] | 0; 7101 if (i6 >>> 0 < i2 >>> 0) { 7102 _abort(); 7103 } 7104 if (i3 >>> 0 < i2 >>> 0) { 7105 _abort(); 7106 } else { 7107 HEAP32[i3 + 12 >> 2] = i15; 7108 HEAP32[i4 >> 2] = i15; 7109 HEAP32[i15 + 8 >> 2] = i3; 7110 HEAP32[i15 + 12 >> 2] = i6; 7111 HEAP32[i15 + 24 >> 2] = 0; 7112 break; 7113 } 7114 } 7115 } else { 7116 i32 = HEAP32[72 >> 2] | 0; 7117 if ((i32 | 0) == 0 | i17 >>> 0 < i32 >>> 0) { 7118 HEAP32[72 >> 2] = i17; 7119 } 7120 HEAP32[504 >> 2] = i17; 7121 HEAP32[508 >> 2] = i14; 7122 HEAP32[516 >> 2] = 0; 7123 HEAP32[92 >> 2] = HEAP32[132]; 7124 HEAP32[88 >> 2] = -1; 7125 i2 = 0; 7126 do { 7127 i32 = i2 << 1; 7128 i31 = 96 + (i32 << 2) | 0; 7129 HEAP32[96 + (i32 + 3 << 2) >> 2] = i31; 7130 HEAP32[96 + (i32 + 2 << 2) >> 2] = i31; 7131 i2 = i2 + 1 | 0; 7132 } while ((i2 | 0) != 32); 7133 i2 = i17 + 8 | 0; 7134 if ((i2 & 7 | 0) == 0) { 7135 i2 = 0; 7136 } else { 7137 i2 = 0 - i2 & 7; 7138 } 7139 i32 = i14 + -40 - i2 | 0; 7140 HEAP32[80 >> 2] = i17 + i2; 7141 HEAP32[68 >> 2] = i32; 7142 HEAP32[i17 + (i2 + 4) >> 2] = i32 | 1; 7143 HEAP32[i17 + (i14 + -36) >> 2] = 40; 7144 HEAP32[84 >> 2] = HEAP32[544 >> 2]; 7145 } 7146 } while (0); 7147 i2 = HEAP32[68 >> 2] | 0; 7148 if (i2 >>> 0 > i12 >>> 0) { 7149 i31 = i2 - i12 | 0; 7150 HEAP32[68 >> 2] = i31; 7151 i32 = HEAP32[80 >> 2] | 0; 7152 HEAP32[80 >> 2] = i32 + i12; 7153 HEAP32[i32 + (i12 + 4) >> 2] = i31 | 1; 7154 HEAP32[i32 + 4 >> 2] = i12 | 3; 7155 i32 = i32 + 8 | 0; 7156 STACKTOP = i1; 7157 return i32 | 0; 7158 } 7159 } 7160 HEAP32[(___errno_location() | 0) >> 2] = 12; 7161 i32 = 0; 7162 STACKTOP = i1; 7163 return i32 | 0; 7164} 7165function _free(i7) { 7166 i7 = i7 | 0; 7167 var i1 = 0, i2 = 0, i3 = 0, i4 = 0, i5 = 0, i6 = 0, i8 = 0, i9 = 0, i10 = 0, i11 = 0, i12 = 0, i13 = 0, i14 = 0, i15 = 0, i16 = 0, i17 = 0, i18 = 0, i19 = 0, i20 = 0, i21 = 0; 7168 i1 = STACKTOP; 7169 if ((i7 | 0) == 0) { 7170 STACKTOP = i1; 7171 return; 7172 } 7173 i15 = i7 + -8 | 0; 7174 i16 = HEAP32[72 >> 2] | 0; 7175 if (i15 >>> 0 < i16 >>> 0) { 7176 _abort(); 7177 } 7178 i13 = HEAP32[i7 + -4 >> 2] | 0; 7179 i12 = i13 & 3; 7180 if ((i12 | 0) == 1) { 7181 _abort(); 7182 } 7183 i8 = i13 & -8; 7184 i6 = i7 + (i8 + -8) | 0; 7185 do { 7186 if ((i13 & 1 | 0) == 0) { 7187 i19 = HEAP32[i15 >> 2] | 0; 7188 if ((i12 | 0) == 0) { 7189 STACKTOP = i1; 7190 return; 7191 } 7192 i15 = -8 - i19 | 0; 7193 i13 = i7 + i15 | 0; 7194 i12 = i19 + i8 | 0; 7195 if (i13 >>> 0 < i16 >>> 0) { 7196 _abort(); 7197 } 7198 if ((i13 | 0) == (HEAP32[76 >> 2] | 0)) { 7199 i2 = i7 + (i8 + -4) | 0; 7200 if ((HEAP32[i2 >> 2] & 3 | 0) != 3) { 7201 i2 = i13; 7202 i11 = i12; 7203 break; 7204 } 7205 HEAP32[64 >> 2] = i12; 7206 HEAP32[i2 >> 2] = HEAP32[i2 >> 2] & -2; 7207 HEAP32[i7 + (i15 + 4) >> 2] = i12 | 1; 7208 HEAP32[i6 >> 2] = i12; 7209 STACKTOP = i1; 7210 return; 7211 } 7212 i18 = i19 >>> 3; 7213 if (i19 >>> 0 < 256) { 7214 i2 = HEAP32[i7 + (i15 + 8) >> 2] | 0; 7215 i11 = HEAP32[i7 + (i15 + 12) >> 2] | 0; 7216 i14 = 96 + (i18 << 1 << 2) | 0; 7217 if ((i2 | 0) != (i14 | 0)) { 7218 if (i2 >>> 0 < i16 >>> 0) { 7219 _abort(); 7220 } 7221 if ((HEAP32[i2 + 12 >> 2] | 0) != (i13 | 0)) { 7222 _abort(); 7223 } 7224 } 7225 if ((i11 | 0) == (i2 | 0)) { 7226 HEAP32[14] = HEAP32[14] & ~(1 << i18); 7227 i2 = i13; 7228 i11 = i12; 7229 break; 7230 } 7231 if ((i11 | 0) != (i14 | 0)) { 7232 if (i11 >>> 0 < i16 >>> 0) { 7233 _abort(); 7234 } 7235 i14 = i11 + 8 | 0; 7236 if ((HEAP32[i14 >> 2] | 0) == (i13 | 0)) { 7237 i17 = i14; 7238 } else { 7239 _abort(); 7240 } 7241 } else { 7242 i17 = i11 + 8 | 0; 7243 } 7244 HEAP32[i2 + 12 >> 2] = i11; 7245 HEAP32[i17 >> 2] = i2; 7246 i2 = i13; 7247 i11 = i12; 7248 break; 7249 } 7250 i17 = HEAP32[i7 + (i15 + 24) >> 2] | 0; 7251 i18 = HEAP32[i7 + (i15 + 12) >> 2] | 0; 7252 do { 7253 if ((i18 | 0) == (i13 | 0)) { 7254 i19 = i7 + (i15 + 20) | 0; 7255 i18 = HEAP32[i19 >> 2] | 0; 7256 if ((i18 | 0) == 0) { 7257 i19 = i7 + (i15 + 16) | 0; 7258 i18 = HEAP32[i19 >> 2] | 0; 7259 if ((i18 | 0) == 0) { 7260 i14 = 0; 7261 break; 7262 } 7263 } 7264 while (1) { 7265 i21 = i18 + 20 | 0; 7266 i20 = HEAP32[i21 >> 2] | 0; 7267 if ((i20 | 0) != 0) { 7268 i18 = i20; 7269 i19 = i21; 7270 continue; 7271 } 7272 i20 = i18 + 16 | 0; 7273 i21 = HEAP32[i20 >> 2] | 0; 7274 if ((i21 | 0) == 0) { 7275 break; 7276 } else { 7277 i18 = i21; 7278 i19 = i20; 7279 } 7280 } 7281 if (i19 >>> 0 < i16 >>> 0) { 7282 _abort(); 7283 } else { 7284 HEAP32[i19 >> 2] = 0; 7285 i14 = i18; 7286 break; 7287 } 7288 } else { 7289 i19 = HEAP32[i7 + (i15 + 8) >> 2] | 0; 7290 if (i19 >>> 0 < i16 >>> 0) { 7291 _abort(); 7292 } 7293 i16 = i19 + 12 | 0; 7294 if ((HEAP32[i16 >> 2] | 0) != (i13 | 0)) { 7295 _abort(); 7296 } 7297 i20 = i18 + 8 | 0; 7298 if ((HEAP32[i20 >> 2] | 0) == (i13 | 0)) { 7299 HEAP32[i16 >> 2] = i18; 7300 HEAP32[i20 >> 2] = i19; 7301 i14 = i18; 7302 break; 7303 } else { 7304 _abort(); 7305 } 7306 } 7307 } while (0); 7308 if ((i17 | 0) != 0) { 7309 i18 = HEAP32[i7 + (i15 + 28) >> 2] | 0; 7310 i16 = 360 + (i18 << 2) | 0; 7311 if ((i13 | 0) == (HEAP32[i16 >> 2] | 0)) { 7312 HEAP32[i16 >> 2] = i14; 7313 if ((i14 | 0) == 0) { 7314 HEAP32[60 >> 2] = HEAP32[60 >> 2] & ~(1 << i18); 7315 i2 = i13; 7316 i11 = i12; 7317 break; 7318 } 7319 } else { 7320 if (i17 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 7321 _abort(); 7322 } 7323 i16 = i17 + 16 | 0; 7324 if ((HEAP32[i16 >> 2] | 0) == (i13 | 0)) { 7325 HEAP32[i16 >> 2] = i14; 7326 } else { 7327 HEAP32[i17 + 20 >> 2] = i14; 7328 } 7329 if ((i14 | 0) == 0) { 7330 i2 = i13; 7331 i11 = i12; 7332 break; 7333 } 7334 } 7335 if (i14 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 7336 _abort(); 7337 } 7338 HEAP32[i14 + 24 >> 2] = i17; 7339 i16 = HEAP32[i7 + (i15 + 16) >> 2] | 0; 7340 do { 7341 if ((i16 | 0) != 0) { 7342 if (i16 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 7343 _abort(); 7344 } else { 7345 HEAP32[i14 + 16 >> 2] = i16; 7346 HEAP32[i16 + 24 >> 2] = i14; 7347 break; 7348 } 7349 } 7350 } while (0); 7351 i15 = HEAP32[i7 + (i15 + 20) >> 2] | 0; 7352 if ((i15 | 0) != 0) { 7353 if (i15 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 7354 _abort(); 7355 } else { 7356 HEAP32[i14 + 20 >> 2] = i15; 7357 HEAP32[i15 + 24 >> 2] = i14; 7358 i2 = i13; 7359 i11 = i12; 7360 break; 7361 } 7362 } else { 7363 i2 = i13; 7364 i11 = i12; 7365 } 7366 } else { 7367 i2 = i13; 7368 i11 = i12; 7369 } 7370 } else { 7371 i2 = i15; 7372 i11 = i8; 7373 } 7374 } while (0); 7375 if (!(i2 >>> 0 < i6 >>> 0)) { 7376 _abort(); 7377 } 7378 i12 = i7 + (i8 + -4) | 0; 7379 i13 = HEAP32[i12 >> 2] | 0; 7380 if ((i13 & 1 | 0) == 0) { 7381 _abort(); 7382 } 7383 if ((i13 & 2 | 0) == 0) { 7384 if ((i6 | 0) == (HEAP32[80 >> 2] | 0)) { 7385 i21 = (HEAP32[68 >> 2] | 0) + i11 | 0; 7386 HEAP32[68 >> 2] = i21; 7387 HEAP32[80 >> 2] = i2; 7388 HEAP32[i2 + 4 >> 2] = i21 | 1; 7389 if ((i2 | 0) != (HEAP32[76 >> 2] | 0)) { 7390 STACKTOP = i1; 7391 return; 7392 } 7393 HEAP32[76 >> 2] = 0; 7394 HEAP32[64 >> 2] = 0; 7395 STACKTOP = i1; 7396 return; 7397 } 7398 if ((i6 | 0) == (HEAP32[76 >> 2] | 0)) { 7399 i21 = (HEAP32[64 >> 2] | 0) + i11 | 0; 7400 HEAP32[64 >> 2] = i21; 7401 HEAP32[76 >> 2] = i2; 7402 HEAP32[i2 + 4 >> 2] = i21 | 1; 7403 HEAP32[i2 + i21 >> 2] = i21; 7404 STACKTOP = i1; 7405 return; 7406 } 7407 i11 = (i13 & -8) + i11 | 0; 7408 i12 = i13 >>> 3; 7409 do { 7410 if (!(i13 >>> 0 < 256)) { 7411 i10 = HEAP32[i7 + (i8 + 16) >> 2] | 0; 7412 i15 = HEAP32[i7 + (i8 | 4) >> 2] | 0; 7413 do { 7414 if ((i15 | 0) == (i6 | 0)) { 7415 i13 = i7 + (i8 + 12) | 0; 7416 i12 = HEAP32[i13 >> 2] | 0; 7417 if ((i12 | 0) == 0) { 7418 i13 = i7 + (i8 + 8) | 0; 7419 i12 = HEAP32[i13 >> 2] | 0; 7420 if ((i12 | 0) == 0) { 7421 i9 = 0; 7422 break; 7423 } 7424 } 7425 while (1) { 7426 i14 = i12 + 20 | 0; 7427 i15 = HEAP32[i14 >> 2] | 0; 7428 if ((i15 | 0) != 0) { 7429 i12 = i15; 7430 i13 = i14; 7431 continue; 7432 } 7433 i14 = i12 + 16 | 0; 7434 i15 = HEAP32[i14 >> 2] | 0; 7435 if ((i15 | 0) == 0) { 7436 break; 7437 } else { 7438 i12 = i15; 7439 i13 = i14; 7440 } 7441 } 7442 if (i13 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 7443 _abort(); 7444 } else { 7445 HEAP32[i13 >> 2] = 0; 7446 i9 = i12; 7447 break; 7448 } 7449 } else { 7450 i13 = HEAP32[i7 + i8 >> 2] | 0; 7451 if (i13 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 7452 _abort(); 7453 } 7454 i14 = i13 + 12 | 0; 7455 if ((HEAP32[i14 >> 2] | 0) != (i6 | 0)) { 7456 _abort(); 7457 } 7458 i12 = i15 + 8 | 0; 7459 if ((HEAP32[i12 >> 2] | 0) == (i6 | 0)) { 7460 HEAP32[i14 >> 2] = i15; 7461 HEAP32[i12 >> 2] = i13; 7462 i9 = i15; 7463 break; 7464 } else { 7465 _abort(); 7466 } 7467 } 7468 } while (0); 7469 if ((i10 | 0) != 0) { 7470 i12 = HEAP32[i7 + (i8 + 20) >> 2] | 0; 7471 i13 = 360 + (i12 << 2) | 0; 7472 if ((i6 | 0) == (HEAP32[i13 >> 2] | 0)) { 7473 HEAP32[i13 >> 2] = i9; 7474 if ((i9 | 0) == 0) { 7475 HEAP32[60 >> 2] = HEAP32[60 >> 2] & ~(1 << i12); 7476 break; 7477 } 7478 } else { 7479 if (i10 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 7480 _abort(); 7481 } 7482 i12 = i10 + 16 | 0; 7483 if ((HEAP32[i12 >> 2] | 0) == (i6 | 0)) { 7484 HEAP32[i12 >> 2] = i9; 7485 } else { 7486 HEAP32[i10 + 20 >> 2] = i9; 7487 } 7488 if ((i9 | 0) == 0) { 7489 break; 7490 } 7491 } 7492 if (i9 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 7493 _abort(); 7494 } 7495 HEAP32[i9 + 24 >> 2] = i10; 7496 i6 = HEAP32[i7 + (i8 + 8) >> 2] | 0; 7497 do { 7498 if ((i6 | 0) != 0) { 7499 if (i6 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 7500 _abort(); 7501 } else { 7502 HEAP32[i9 + 16 >> 2] = i6; 7503 HEAP32[i6 + 24 >> 2] = i9; 7504 break; 7505 } 7506 } 7507 } while (0); 7508 i6 = HEAP32[i7 + (i8 + 12) >> 2] | 0; 7509 if ((i6 | 0) != 0) { 7510 if (i6 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 7511 _abort(); 7512 } else { 7513 HEAP32[i9 + 20 >> 2] = i6; 7514 HEAP32[i6 + 24 >> 2] = i9; 7515 break; 7516 } 7517 } 7518 } 7519 } else { 7520 i9 = HEAP32[i7 + i8 >> 2] | 0; 7521 i7 = HEAP32[i7 + (i8 | 4) >> 2] | 0; 7522 i8 = 96 + (i12 << 1 << 2) | 0; 7523 if ((i9 | 0) != (i8 | 0)) { 7524 if (i9 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 7525 _abort(); 7526 } 7527 if ((HEAP32[i9 + 12 >> 2] | 0) != (i6 | 0)) { 7528 _abort(); 7529 } 7530 } 7531 if ((i7 | 0) == (i9 | 0)) { 7532 HEAP32[14] = HEAP32[14] & ~(1 << i12); 7533 break; 7534 } 7535 if ((i7 | 0) != (i8 | 0)) { 7536 if (i7 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 7537 _abort(); 7538 } 7539 i8 = i7 + 8 | 0; 7540 if ((HEAP32[i8 >> 2] | 0) == (i6 | 0)) { 7541 i10 = i8; 7542 } else { 7543 _abort(); 7544 } 7545 } else { 7546 i10 = i7 + 8 | 0; 7547 } 7548 HEAP32[i9 + 12 >> 2] = i7; 7549 HEAP32[i10 >> 2] = i9; 7550 } 7551 } while (0); 7552 HEAP32[i2 + 4 >> 2] = i11 | 1; 7553 HEAP32[i2 + i11 >> 2] = i11; 7554 if ((i2 | 0) == (HEAP32[76 >> 2] | 0)) { 7555 HEAP32[64 >> 2] = i11; 7556 STACKTOP = i1; 7557 return; 7558 } 7559 } else { 7560 HEAP32[i12 >> 2] = i13 & -2; 7561 HEAP32[i2 + 4 >> 2] = i11 | 1; 7562 HEAP32[i2 + i11 >> 2] = i11; 7563 } 7564 i6 = i11 >>> 3; 7565 if (i11 >>> 0 < 256) { 7566 i7 = i6 << 1; 7567 i3 = 96 + (i7 << 2) | 0; 7568 i8 = HEAP32[14] | 0; 7569 i6 = 1 << i6; 7570 if ((i8 & i6 | 0) != 0) { 7571 i6 = 96 + (i7 + 2 << 2) | 0; 7572 i7 = HEAP32[i6 >> 2] | 0; 7573 if (i7 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 7574 _abort(); 7575 } else { 7576 i4 = i6; 7577 i5 = i7; 7578 } 7579 } else { 7580 HEAP32[14] = i8 | i6; 7581 i4 = 96 + (i7 + 2 << 2) | 0; 7582 i5 = i3; 7583 } 7584 HEAP32[i4 >> 2] = i2; 7585 HEAP32[i5 + 12 >> 2] = i2; 7586 HEAP32[i2 + 8 >> 2] = i5; 7587 HEAP32[i2 + 12 >> 2] = i3; 7588 STACKTOP = i1; 7589 return; 7590 } 7591 i4 = i11 >>> 8; 7592 if ((i4 | 0) != 0) { 7593 if (i11 >>> 0 > 16777215) { 7594 i4 = 31; 7595 } else { 7596 i20 = (i4 + 1048320 | 0) >>> 16 & 8; 7597 i21 = i4 << i20; 7598 i19 = (i21 + 520192 | 0) >>> 16 & 4; 7599 i21 = i21 << i19; 7600 i4 = (i21 + 245760 | 0) >>> 16 & 2; 7601 i4 = 14 - (i19 | i20 | i4) + (i21 << i4 >>> 15) | 0; 7602 i4 = i11 >>> (i4 + 7 | 0) & 1 | i4 << 1; 7603 } 7604 } else { 7605 i4 = 0; 7606 } 7607 i5 = 360 + (i4 << 2) | 0; 7608 HEAP32[i2 + 28 >> 2] = i4; 7609 HEAP32[i2 + 20 >> 2] = 0; 7610 HEAP32[i2 + 16 >> 2] = 0; 7611 i7 = HEAP32[60 >> 2] | 0; 7612 i6 = 1 << i4; 7613 L199 : do { 7614 if ((i7 & i6 | 0) != 0) { 7615 i5 = HEAP32[i5 >> 2] | 0; 7616 if ((i4 | 0) == 31) { 7617 i4 = 0; 7618 } else { 7619 i4 = 25 - (i4 >>> 1) | 0; 7620 } 7621 L205 : do { 7622 if ((HEAP32[i5 + 4 >> 2] & -8 | 0) != (i11 | 0)) { 7623 i4 = i11 << i4; 7624 i7 = i5; 7625 while (1) { 7626 i6 = i7 + (i4 >>> 31 << 2) + 16 | 0; 7627 i5 = HEAP32[i6 >> 2] | 0; 7628 if ((i5 | 0) == 0) { 7629 break; 7630 } 7631 if ((HEAP32[i5 + 4 >> 2] & -8 | 0) == (i11 | 0)) { 7632 i3 = i5; 7633 break L205; 7634 } else { 7635 i4 = i4 << 1; 7636 i7 = i5; 7637 } 7638 } 7639 if (i6 >>> 0 < (HEAP32[72 >> 2] | 0) >>> 0) { 7640 _abort(); 7641 } else { 7642 HEAP32[i6 >> 2] = i2; 7643 HEAP32[i2 + 24 >> 2] = i7; 7644 HEAP32[i2 + 12 >> 2] = i2; 7645 HEAP32[i2 + 8 >> 2] = i2; 7646 break L199; 7647 } 7648 } else { 7649 i3 = i5; 7650 } 7651 } while (0); 7652 i5 = i3 + 8 | 0; 7653 i4 = HEAP32[i5 >> 2] | 0; 7654 i6 = HEAP32[72 >> 2] | 0; 7655 if (i3 >>> 0 < i6 >>> 0) { 7656 _abort(); 7657 } 7658 if (i4 >>> 0 < i6 >>> 0) { 7659 _abort(); 7660 } else { 7661 HEAP32[i4 + 12 >> 2] = i2; 7662 HEAP32[i5 >> 2] = i2; 7663 HEAP32[i2 + 8 >> 2] = i4; 7664 HEAP32[i2 + 12 >> 2] = i3; 7665 HEAP32[i2 + 24 >> 2] = 0; 7666 break; 7667 } 7668 } else { 7669 HEAP32[60 >> 2] = i7 | i6; 7670 HEAP32[i5 >> 2] = i2; 7671 HEAP32[i2 + 24 >> 2] = i5; 7672 HEAP32[i2 + 12 >> 2] = i2; 7673 HEAP32[i2 + 8 >> 2] = i2; 7674 } 7675 } while (0); 7676 i21 = (HEAP32[88 >> 2] | 0) + -1 | 0; 7677 HEAP32[88 >> 2] = i21; 7678 if ((i21 | 0) == 0) { 7679 i2 = 512 | 0; 7680 } else { 7681 STACKTOP = i1; 7682 return; 7683 } 7684 while (1) { 7685 i2 = HEAP32[i2 >> 2] | 0; 7686 if ((i2 | 0) == 0) { 7687 break; 7688 } else { 7689 i2 = i2 + 8 | 0; 7690 } 7691 } 7692 HEAP32[88 >> 2] = -1; 7693 STACKTOP = i1; 7694 return; 7695} 7696function __Z15fannkuch_workerPv(i9) { 7697 i9 = i9 | 0; 7698 var i1 = 0, i2 = 0, i3 = 0, i4 = 0, i5 = 0, i6 = 0, i7 = 0, i8 = 0, i10 = 0, i11 = 0, i12 = 0, i13 = 0, i14 = 0, i15 = 0, i16 = 0, i17 = 0, i18 = 0, i19 = 0, i20 = 0; 7699 i3 = STACKTOP; 7700 i7 = HEAP32[i9 + 4 >> 2] | 0; 7701 i6 = i7 << 2; 7702 i5 = _malloc(i6) | 0; 7703 i2 = _malloc(i6) | 0; 7704 i6 = _malloc(i6) | 0; 7705 i10 = (i7 | 0) > 0; 7706 if (i10) { 7707 i8 = 0; 7708 do { 7709 HEAP32[i5 + (i8 << 2) >> 2] = i8; 7710 i8 = i8 + 1 | 0; 7711 } while ((i8 | 0) != (i7 | 0)); 7712 i8 = i7 + -1 | 0; 7713 i17 = HEAP32[i9 >> 2] | 0; 7714 HEAP32[i5 + (i17 << 2) >> 2] = i8; 7715 i9 = i5 + (i8 << 2) | 0; 7716 HEAP32[i9 >> 2] = i17; 7717 if (i10) { 7718 i10 = i7 << 2; 7719 i11 = 0; 7720 i12 = i7; 7721 L7 : while (1) { 7722 if ((i12 | 0) > 1) { 7723 while (1) { 7724 i13 = i12 + -1 | 0; 7725 HEAP32[i6 + (i13 << 2) >> 2] = i12; 7726 if ((i13 | 0) > 1) { 7727 i12 = i13; 7728 } else { 7729 i12 = 1; 7730 break; 7731 } 7732 } 7733 } 7734 i13 = HEAP32[i5 >> 2] | 0; 7735 if ((i13 | 0) != 0 ? (HEAP32[i9 >> 2] | 0) != (i8 | 0) : 0) { 7736 _memcpy(i2 | 0, i5 | 0, i10 | 0) | 0; 7737 i15 = 0; 7738 i14 = HEAP32[i2 >> 2] | 0; 7739 while (1) { 7740 i17 = i14 + -1 | 0; 7741 if ((i17 | 0) > 1) { 7742 i16 = 1; 7743 do { 7744 i20 = i2 + (i16 << 2) | 0; 7745 i19 = HEAP32[i20 >> 2] | 0; 7746 i18 = i2 + (i17 << 2) | 0; 7747 HEAP32[i20 >> 2] = HEAP32[i18 >> 2]; 7748 HEAP32[i18 >> 2] = i19; 7749 i16 = i16 + 1 | 0; 7750 i17 = i17 + -1 | 0; 7751 } while ((i16 | 0) < (i17 | 0)); 7752 } 7753 i15 = i15 + 1 | 0; 7754 i20 = i2 + (i14 << 2) | 0; 7755 i16 = HEAP32[i20 >> 2] | 0; 7756 HEAP32[i20 >> 2] = i14; 7757 if ((i16 | 0) == 0) { 7758 break; 7759 } else { 7760 i14 = i16; 7761 } 7762 } 7763 i11 = (i11 | 0) < (i15 | 0) ? i15 : i11; 7764 } 7765 if ((i12 | 0) >= (i8 | 0)) { 7766 i8 = 34; 7767 break; 7768 } 7769 while (1) { 7770 if ((i12 | 0) > 0) { 7771 i14 = 0; 7772 while (1) { 7773 i15 = i14 + 1 | 0; 7774 HEAP32[i5 + (i14 << 2) >> 2] = HEAP32[i5 + (i15 << 2) >> 2]; 7775 if ((i15 | 0) == (i12 | 0)) { 7776 i14 = i12; 7777 break; 7778 } else { 7779 i14 = i15; 7780 } 7781 } 7782 } else { 7783 i14 = 0; 7784 } 7785 HEAP32[i5 + (i14 << 2) >> 2] = i13; 7786 i14 = i6 + (i12 << 2) | 0; 7787 i20 = (HEAP32[i14 >> 2] | 0) + -1 | 0; 7788 HEAP32[i14 >> 2] = i20; 7789 i14 = i12 + 1 | 0; 7790 if ((i20 | 0) > 0) { 7791 continue L7; 7792 } 7793 if ((i14 | 0) >= (i8 | 0)) { 7794 i8 = 34; 7795 break L7; 7796 } 7797 i13 = HEAP32[i5 >> 2] | 0; 7798 i12 = i14; 7799 } 7800 } 7801 if ((i8 | 0) == 34) { 7802 _free(i5); 7803 _free(i2); 7804 _free(i6); 7805 STACKTOP = i3; 7806 return i11 | 0; 7807 } 7808 } else { 7809 i1 = i9; 7810 i4 = i8; 7811 } 7812 } else { 7813 i4 = i7 + -1 | 0; 7814 i20 = HEAP32[i9 >> 2] | 0; 7815 HEAP32[i5 + (i20 << 2) >> 2] = i4; 7816 i1 = i5 + (i4 << 2) | 0; 7817 HEAP32[i1 >> 2] = i20; 7818 } 7819 i11 = 0; 7820 L36 : while (1) { 7821 if ((i7 | 0) > 1) { 7822 while (1) { 7823 i8 = i7 + -1 | 0; 7824 HEAP32[i6 + (i8 << 2) >> 2] = i7; 7825 if ((i8 | 0) > 1) { 7826 i7 = i8; 7827 } else { 7828 i7 = 1; 7829 break; 7830 } 7831 } 7832 } 7833 i8 = HEAP32[i5 >> 2] | 0; 7834 if ((i8 | 0) != 0 ? (HEAP32[i1 >> 2] | 0) != (i4 | 0) : 0) { 7835 i10 = 0; 7836 i9 = HEAP32[i2 >> 2] | 0; 7837 while (1) { 7838 i13 = i9 + -1 | 0; 7839 if ((i13 | 0) > 1) { 7840 i12 = 1; 7841 do { 7842 i18 = i2 + (i12 << 2) | 0; 7843 i19 = HEAP32[i18 >> 2] | 0; 7844 i20 = i2 + (i13 << 2) | 0; 7845 HEAP32[i18 >> 2] = HEAP32[i20 >> 2]; 7846 HEAP32[i20 >> 2] = i19; 7847 i12 = i12 + 1 | 0; 7848 i13 = i13 + -1 | 0; 7849 } while ((i12 | 0) < (i13 | 0)); 7850 } 7851 i10 = i10 + 1 | 0; 7852 i20 = i2 + (i9 << 2) | 0; 7853 i12 = HEAP32[i20 >> 2] | 0; 7854 HEAP32[i20 >> 2] = i9; 7855 if ((i12 | 0) == 0) { 7856 break; 7857 } else { 7858 i9 = i12; 7859 } 7860 } 7861 i11 = (i11 | 0) < (i10 | 0) ? i10 : i11; 7862 } 7863 if ((i7 | 0) >= (i4 | 0)) { 7864 i8 = 34; 7865 break; 7866 } 7867 while (1) { 7868 if ((i7 | 0) > 0) { 7869 i9 = 0; 7870 while (1) { 7871 i10 = i9 + 1 | 0; 7872 HEAP32[i5 + (i9 << 2) >> 2] = HEAP32[i5 + (i10 << 2) >> 2]; 7873 if ((i10 | 0) == (i7 | 0)) { 7874 i9 = i7; 7875 break; 7876 } else { 7877 i9 = i10; 7878 } 7879 } 7880 } else { 7881 i9 = 0; 7882 } 7883 HEAP32[i5 + (i9 << 2) >> 2] = i8; 7884 i9 = i6 + (i7 << 2) | 0; 7885 i20 = (HEAP32[i9 >> 2] | 0) + -1 | 0; 7886 HEAP32[i9 >> 2] = i20; 7887 i9 = i7 + 1 | 0; 7888 if ((i20 | 0) > 0) { 7889 continue L36; 7890 } 7891 if ((i9 | 0) >= (i4 | 0)) { 7892 i8 = 34; 7893 break L36; 7894 } 7895 i8 = HEAP32[i5 >> 2] | 0; 7896 i7 = i9; 7897 } 7898 } 7899 if ((i8 | 0) == 34) { 7900 _free(i5); 7901 _free(i2); 7902 _free(i6); 7903 STACKTOP = i3; 7904 return i11 | 0; 7905 } 7906 return 0; 7907} 7908function _main(i3, i5) { 7909 i3 = i3 | 0; 7910 i5 = i5 | 0; 7911 var i1 = 0, i2 = 0, i4 = 0, i6 = 0, i7 = 0, i8 = 0, i9 = 0, i10 = 0, i11 = 0; 7912 i2 = STACKTOP; 7913 STACKTOP = STACKTOP + 16 | 0; 7914 i1 = i2; 7915 L1 : do { 7916 if ((i3 | 0) > 1) { 7917 i3 = HEAP8[HEAP32[i5 + 4 >> 2] | 0] | 0; 7918 switch (i3 | 0) { 7919 case 50: 7920 { 7921 i3 = 10; 7922 break L1; 7923 } 7924 case 51: 7925 { 7926 i4 = 4; 7927 break L1; 7928 } 7929 case 52: 7930 { 7931 i3 = 11; 7932 break L1; 7933 } 7934 case 53: 7935 { 7936 i3 = 12; 7937 break L1; 7938 } 7939 case 49: 7940 { 7941 i3 = 9; 7942 break L1; 7943 } 7944 case 48: 7945 { 7946 i11 = 0; 7947 STACKTOP = i2; 7948 return i11 | 0; 7949 } 7950 default: 7951 { 7952 HEAP32[i1 >> 2] = i3 + -48; 7953 _printf(8, i1 | 0) | 0; 7954 i11 = -1; 7955 STACKTOP = i2; 7956 return i11 | 0; 7957 } 7958 } 7959 } else { 7960 i4 = 4; 7961 } 7962 } while (0); 7963 if ((i4 | 0) == 4) { 7964 i3 = 11; 7965 } 7966 i5 = i3 + -1 | 0; 7967 i6 = 0; 7968 i7 = 0; 7969 while (1) { 7970 i4 = _malloc(12) | 0; 7971 HEAP32[i4 >> 2] = i7; 7972 HEAP32[i4 + 4 >> 2] = i3; 7973 HEAP32[i4 + 8 >> 2] = i6; 7974 i7 = i7 + 1 | 0; 7975 if ((i7 | 0) == (i5 | 0)) { 7976 break; 7977 } else { 7978 i6 = i4; 7979 } 7980 } 7981 i5 = i3 << 2; 7982 i6 = _malloc(i5) | 0; 7983 i5 = _malloc(i5) | 0; 7984 i7 = 0; 7985 do { 7986 HEAP32[i6 + (i7 << 2) >> 2] = i7; 7987 i7 = i7 + 1 | 0; 7988 } while ((i7 | 0) != (i3 | 0)); 7989 i8 = i3; 7990 i7 = 30; 7991 L19 : do { 7992 i9 = 0; 7993 do { 7994 HEAP32[i1 >> 2] = (HEAP32[i6 + (i9 << 2) >> 2] | 0) + 1; 7995 _printf(48, i1 | 0) | 0; 7996 i9 = i9 + 1 | 0; 7997 } while ((i9 | 0) != (i3 | 0)); 7998 _putchar(10) | 0; 7999 i7 = i7 + -1 | 0; 8000 if ((i8 | 0) <= 1) { 8001 if ((i8 | 0) == (i3 | 0)) { 8002 break; 8003 } 8004 } else { 8005 while (1) { 8006 i9 = i8 + -1 | 0; 8007 HEAP32[i5 + (i9 << 2) >> 2] = i8; 8008 if ((i9 | 0) > 1) { 8009 i8 = i9; 8010 } else { 8011 i8 = 1; 8012 break; 8013 } 8014 } 8015 } 8016 while (1) { 8017 i9 = HEAP32[i6 >> 2] | 0; 8018 if ((i8 | 0) > 0) { 8019 i11 = 0; 8020 while (1) { 8021 i10 = i11 + 1 | 0; 8022 HEAP32[i6 + (i11 << 2) >> 2] = HEAP32[i6 + (i10 << 2) >> 2]; 8023 if ((i10 | 0) == (i8 | 0)) { 8024 i10 = i8; 8025 break; 8026 } else { 8027 i11 = i10; 8028 } 8029 } 8030 } else { 8031 i10 = 0; 8032 } 8033 HEAP32[i6 + (i10 << 2) >> 2] = i9; 8034 i9 = i5 + (i8 << 2) | 0; 8035 i11 = (HEAP32[i9 >> 2] | 0) + -1 | 0; 8036 HEAP32[i9 >> 2] = i11; 8037 i9 = i8 + 1 | 0; 8038 if ((i11 | 0) > 0) { 8039 break; 8040 } 8041 if ((i9 | 0) == (i3 | 0)) { 8042 break L19; 8043 } else { 8044 i8 = i9; 8045 } 8046 } 8047 } while ((i7 | 0) != 0); 8048 _free(i6); 8049 _free(i5); 8050 if ((i4 | 0) == 0) { 8051 i5 = 0; 8052 } else { 8053 i5 = 0; 8054 while (1) { 8055 i6 = __Z15fannkuch_workerPv(i4) | 0; 8056 i5 = (i5 | 0) < (i6 | 0) ? i6 : i5; 8057 i6 = HEAP32[i4 + 8 >> 2] | 0; 8058 _free(i4); 8059 if ((i6 | 0) == 0) { 8060 break; 8061 } else { 8062 i4 = i6; 8063 } 8064 } 8065 } 8066 HEAP32[i1 >> 2] = i3; 8067 HEAP32[i1 + 4 >> 2] = i5; 8068 _printf(24, i1 | 0) | 0; 8069 i11 = 0; 8070 STACKTOP = i2; 8071 return i11 | 0; 8072} 8073function _memcpy(i3, i2, i1) { 8074 i3 = i3 | 0; 8075 i2 = i2 | 0; 8076 i1 = i1 | 0; 8077 var i4 = 0; 8078 if ((i1 | 0) >= 4096) return _emscripten_memcpy_big(i3 | 0, i2 | 0, i1 | 0) | 0; 8079 i4 = i3 | 0; 8080 if ((i3 & 3) == (i2 & 3)) { 8081 while (i3 & 3) { 8082 if ((i1 | 0) == 0) return i4 | 0; 8083 HEAP8[i3] = HEAP8[i2] | 0; 8084 i3 = i3 + 1 | 0; 8085 i2 = i2 + 1 | 0; 8086 i1 = i1 - 1 | 0; 8087 } 8088 while ((i1 | 0) >= 4) { 8089 HEAP32[i3 >> 2] = HEAP32[i2 >> 2]; 8090 i3 = i3 + 4 | 0; 8091 i2 = i2 + 4 | 0; 8092 i1 = i1 - 4 | 0; 8093 } 8094 } 8095 while ((i1 | 0) > 0) { 8096 HEAP8[i3] = HEAP8[i2] | 0; 8097 i3 = i3 + 1 | 0; 8098 i2 = i2 + 1 | 0; 8099 i1 = i1 - 1 | 0; 8100 } 8101 return i4 | 0; 8102} 8103function _memset(i1, i4, i3) { 8104 i1 = i1 | 0; 8105 i4 = i4 | 0; 8106 i3 = i3 | 0; 8107 var i2 = 0, i5 = 0, i6 = 0, i7 = 0; 8108 i2 = i1 + i3 | 0; 8109 if ((i3 | 0) >= 20) { 8110 i4 = i4 & 255; 8111 i7 = i1 & 3; 8112 i6 = i4 | i4 << 8 | i4 << 16 | i4 << 24; 8113 i5 = i2 & ~3; 8114 if (i7) { 8115 i7 = i1 + 4 - i7 | 0; 8116 while ((i1 | 0) < (i7 | 0)) { 8117 HEAP8[i1] = i4; 8118 i1 = i1 + 1 | 0; 8119 } 8120 } 8121 while ((i1 | 0) < (i5 | 0)) { 8122 HEAP32[i1 >> 2] = i6; 8123 i1 = i1 + 4 | 0; 8124 } 8125 } 8126 while ((i1 | 0) < (i2 | 0)) { 8127 HEAP8[i1] = i4; 8128 i1 = i1 + 1 | 0; 8129 } 8130 return i1 - i3 | 0; 8131} 8132function copyTempDouble(i1) { 8133 i1 = i1 | 0; 8134 HEAP8[tempDoublePtr] = HEAP8[i1]; 8135 HEAP8[tempDoublePtr + 1 | 0] = HEAP8[i1 + 1 | 0]; 8136 HEAP8[tempDoublePtr + 2 | 0] = HEAP8[i1 + 2 | 0]; 8137 HEAP8[tempDoublePtr + 3 | 0] = HEAP8[i1 + 3 | 0]; 8138 HEAP8[tempDoublePtr + 4 | 0] = HEAP8[i1 + 4 | 0]; 8139 HEAP8[tempDoublePtr + 5 | 0] = HEAP8[i1 + 5 | 0]; 8140 HEAP8[tempDoublePtr + 6 | 0] = HEAP8[i1 + 6 | 0]; 8141 HEAP8[tempDoublePtr + 7 | 0] = HEAP8[i1 + 7 | 0]; 8142} 8143function copyTempFloat(i1) { 8144 i1 = i1 | 0; 8145 HEAP8[tempDoublePtr] = HEAP8[i1]; 8146 HEAP8[tempDoublePtr + 1 | 0] = HEAP8[i1 + 1 | 0]; 8147 HEAP8[tempDoublePtr + 2 | 0] = HEAP8[i1 + 2 | 0]; 8148 HEAP8[tempDoublePtr + 3 | 0] = HEAP8[i1 + 3 | 0]; 8149} 8150function runPostSets() {} 8151function _strlen(i1) { 8152 i1 = i1 | 0; 8153 var i2 = 0; 8154 i2 = i1; 8155 while (HEAP8[i2] | 0) { 8156 i2 = i2 + 1 | 0; 8157 } 8158 return i2 - i1 | 0; 8159} 8160function stackAlloc(i1) { 8161 i1 = i1 | 0; 8162 var i2 = 0; 8163 i2 = STACKTOP; 8164 STACKTOP = STACKTOP + i1 | 0; 8165 STACKTOP = STACKTOP + 7 & -8; 8166 return i2 | 0; 8167} 8168function setThrew(i1, i2) { 8169 i1 = i1 | 0; 8170 i2 = i2 | 0; 8171 if ((__THREW__ | 0) == 0) { 8172 __THREW__ = i1; 8173 threwValue = i2; 8174 } 8175} 8176function stackRestore(i1) { 8177 i1 = i1 | 0; 8178 STACKTOP = i1; 8179} 8180function setTempRet9(i1) { 8181 i1 = i1 | 0; 8182 tempRet9 = i1; 8183} 8184function setTempRet8(i1) { 8185 i1 = i1 | 0; 8186 tempRet8 = i1; 8187} 8188function setTempRet7(i1) { 8189 i1 = i1 | 0; 8190 tempRet7 = i1; 8191} 8192function setTempRet6(i1) { 8193 i1 = i1 | 0; 8194 tempRet6 = i1; 8195} 8196function setTempRet5(i1) { 8197 i1 = i1 | 0; 8198 tempRet5 = i1; 8199} 8200function setTempRet4(i1) { 8201 i1 = i1 | 0; 8202 tempRet4 = i1; 8203} 8204function setTempRet3(i1) { 8205 i1 = i1 | 0; 8206 tempRet3 = i1; 8207} 8208function setTempRet2(i1) { 8209 i1 = i1 | 0; 8210 tempRet2 = i1; 8211} 8212function setTempRet1(i1) { 8213 i1 = i1 | 0; 8214 tempRet1 = i1; 8215} 8216function setTempRet0(i1) { 8217 i1 = i1 | 0; 8218 tempRet0 = i1; 8219} 8220function stackSave() { 8221 return STACKTOP | 0; 8222} 8223 8224// EMSCRIPTEN_END_FUNCS 8225 8226 8227 return { _strlen: _strlen, _free: _free, _main: _main, _memset: _memset, _malloc: _malloc, _memcpy: _memcpy, runPostSets: runPostSets, stackAlloc: stackAlloc, stackSave: stackSave, stackRestore: stackRestore, setThrew: setThrew, setTempRet0: setTempRet0, setTempRet1: setTempRet1, setTempRet2: setTempRet2, setTempRet3: setTempRet3, setTempRet4: setTempRet4, setTempRet5: setTempRet5, setTempRet6: setTempRet6, setTempRet7: setTempRet7, setTempRet8: setTempRet8, setTempRet9: setTempRet9 }; 8228}).toString(), 8229// EMSCRIPTEN_END_ASM 8230{ "Math": Math, "Int8Array": Int8Array, "Int16Array": Int16Array, "Int32Array": Int32Array, "Uint8Array": Uint8Array, "Uint16Array": Uint16Array, "Uint32Array": Uint32Array, "Float32Array": Float32Array, "Float64Array": Float64Array, "abort": abort, "assert": assert, "asmPrintInt": asmPrintInt, "asmPrintFloat": asmPrintFloat, "min": Math_min, "_fflush": _fflush, "_emscripten_memcpy_big": _emscripten_memcpy_big, "_putchar": _putchar, "_fputc": _fputc, "_send": _send, "_pwrite": _pwrite, "_abort": _abort, "__reallyNegative": __reallyNegative, "_fwrite": _fwrite, "_sbrk": _sbrk, "_mkport": _mkport, "_fprintf": _fprintf, "___setErrNo": ___setErrNo, "__formatString": __formatString, "_fileno": _fileno, "_printf": _printf, "_time": _time, "_sysconf": _sysconf, "_write": _write, "___errno_location": ___errno_location, "STACKTOP": STACKTOP, "STACK_MAX": STACK_MAX, "tempDoublePtr": tempDoublePtr, "ABORT": ABORT, "NaN": NaN, "Infinity": Infinity }, buffer); 8231var _strlen = Module["_strlen"] = asm["_strlen"]; 8232var _free = Module["_free"] = asm["_free"]; 8233var _main = Module["_main"] = asm["_main"]; 8234var _memset = Module["_memset"] = asm["_memset"]; 8235var _malloc = Module["_malloc"] = asm["_malloc"]; 8236var _memcpy = Module["_memcpy"] = asm["_memcpy"]; 8237var runPostSets = Module["runPostSets"] = asm["runPostSets"]; 8238 8239Runtime.stackAlloc = function(size) { return asm['stackAlloc'](size) }; 8240Runtime.stackSave = function() { return asm['stackSave']() }; 8241Runtime.stackRestore = function(top) { asm['stackRestore'](top) }; 8242 8243 8244// Warning: printing of i64 values may be slightly rounded! No deep i64 math used, so precise i64 code not included 8245var i64Math = null; 8246 8247// === Auto-generated postamble setup entry stuff === 8248 8249if (memoryInitializer) { 8250 if (ENVIRONMENT_IS_NODE || ENVIRONMENT_IS_SHELL) { 8251 var data = Module['readBinary'](memoryInitializer); 8252 HEAPU8.set(data, STATIC_BASE); 8253 } else { 8254 addRunDependency('memory initializer'); 8255 Browser.asyncLoad(memoryInitializer, function(data) { 8256 HEAPU8.set(data, STATIC_BASE); 8257 removeRunDependency('memory initializer'); 8258 }, function(data) { 8259 throw 'could not load memory initializer ' + memoryInitializer; 8260 }); 8261 } 8262} 8263 8264function ExitStatus(status) { 8265 this.name = "ExitStatus"; 8266 this.message = "Program terminated with exit(" + status + ")"; 8267 this.status = status; 8268}; 8269ExitStatus.prototype = new Error(); 8270ExitStatus.prototype.constructor = ExitStatus; 8271 8272var initialStackTop; 8273var preloadStartTime = null; 8274var calledMain = false; 8275 8276dependenciesFulfilled = function runCaller() { 8277 // If run has never been called, and we should call run (INVOKE_RUN is true, and Module.noInitialRun is not false) 8278 if (!Module['calledRun'] && shouldRunNow) run([].concat(Module["arguments"])); 8279 if (!Module['calledRun']) dependenciesFulfilled = runCaller; // try this again later, after new deps are fulfilled 8280} 8281 8282Module['callMain'] = Module.callMain = function callMain(args) { 8283 assert(runDependencies == 0, 'cannot call main when async dependencies remain! (listen on __ATMAIN__)'); 8284 assert(__ATPRERUN__.length == 0, 'cannot call main when preRun functions remain to be called'); 8285 8286 args = args || []; 8287 8288 ensureInitRuntime(); 8289 8290 var argc = args.length+1; 8291 function pad() { 8292 for (var i = 0; i < 4-1; i++) { 8293 argv.push(0); 8294 } 8295 } 8296 var argv = [allocate(intArrayFromString("/bin/this.program"), 'i8', ALLOC_NORMAL) ]; 8297 pad(); 8298 for (var i = 0; i < argc-1; i = i + 1) { 8299 argv.push(allocate(intArrayFromString(args[i]), 'i8', ALLOC_NORMAL)); 8300 pad(); 8301 } 8302 argv.push(0); 8303 argv = allocate(argv, 'i32', ALLOC_NORMAL); 8304 8305 initialStackTop = STACKTOP; 8306 8307 try { 8308 8309 var ret = Module['_main'](argc, argv, 0); 8310 8311 8312 // if we're not running an evented main loop, it's time to exit 8313 if (!Module['noExitRuntime']) { 8314 exit(ret); 8315 } 8316 } 8317 catch(e) { 8318 if (e instanceof ExitStatus) { 8319 // exit() throws this once it's done to make sure execution 8320 // has been stopped completely 8321 return; 8322 } else if (e == 'SimulateInfiniteLoop') { 8323 // running an evented main loop, don't immediately exit 8324 Module['noExitRuntime'] = true; 8325 return; 8326 } else { 8327 if (e && typeof e === 'object' && e.stack) Module.printErr('exception thrown: ' + [e, e.stack]); 8328 throw e; 8329 } 8330 } finally { 8331 calledMain = true; 8332 } 8333} 8334 8335 8336 8337 8338function run(args) { 8339 args = args || Module['arguments']; 8340 8341 if (preloadStartTime === null) preloadStartTime = Date.now(); 8342 8343 if (runDependencies > 0) { 8344 Module.printErr('run() called, but dependencies remain, so not running'); 8345 return; 8346 } 8347 8348 preRun(); 8349 8350 if (runDependencies > 0) return; // a preRun added a dependency, run will be called later 8351 if (Module['calledRun']) return; // run may have just been called through dependencies being fulfilled just in this very frame 8352 8353 function doRun() { 8354 if (Module['calledRun']) return; // run may have just been called while the async setStatus time below was happening 8355 Module['calledRun'] = true; 8356 8357 ensureInitRuntime(); 8358 8359 preMain(); 8360 8361 if (ENVIRONMENT_IS_WEB && preloadStartTime !== null) { 8362 Module.printErr('pre-main prep time: ' + (Date.now() - preloadStartTime) + ' ms'); 8363 } 8364 8365 if (Module['_main'] && shouldRunNow) { 8366 Module['callMain'](args); 8367 } 8368 8369 postRun(); 8370 } 8371 8372 if (Module['setStatus']) { 8373 Module['setStatus']('Running...'); 8374 setTimeout(function() { 8375 setTimeout(function() { 8376 Module['setStatus'](''); 8377 }, 1); 8378 if (!ABORT) doRun(); 8379 }, 1); 8380 } else { 8381 doRun(); 8382 } 8383} 8384Module['run'] = Module.run = run; 8385 8386function exit(status) { 8387 ABORT = true; 8388 EXITSTATUS = status; 8389 STACKTOP = initialStackTop; 8390 8391 // exit the runtime 8392 exitRuntime(); 8393 8394 // TODO We should handle this differently based on environment. 8395 // In the browser, the best we can do is throw an exception 8396 // to halt execution, but in node we could process.exit and 8397 // I'd imagine SM shell would have something equivalent. 8398 // This would let us set a proper exit status (which 8399 // would be great for checking test exit statuses). 8400 // https://github.com/kripken/emscripten/issues/1371 8401 8402 // throw an exception to halt the current execution 8403 throw new ExitStatus(status); 8404} 8405Module['exit'] = Module.exit = exit; 8406 8407function abort(text) { 8408 if (text) { 8409 Module.print(text); 8410 Module.printErr(text); 8411 } 8412 8413 ABORT = true; 8414 EXITSTATUS = 1; 8415 8416 var extra = '\nIf this abort() is unexpected, build with -s ASSERTIONS=1 which can give more information.'; 8417 8418 throw 'abort() at ' + stackTrace() + extra; 8419} 8420Module['abort'] = Module.abort = abort; 8421 8422// {{PRE_RUN_ADDITIONS}} 8423 8424if (Module['preInit']) { 8425 if (typeof Module['preInit'] == 'function') Module['preInit'] = [Module['preInit']]; 8426 while (Module['preInit'].length > 0) { 8427 Module['preInit'].pop()(); 8428 } 8429} 8430 8431// shouldRunNow refers to calling main(), not run(). 8432var shouldRunNow = true; 8433if (Module['noInitialRun']) { 8434 shouldRunNow = false; 8435} 8436 8437 8438run([].concat(Module["arguments"])); 8439