wasm_box2d.js (119293B)
1 const isSimulator = [ 2 "arm-simulator", "arm64-simulator", "mips64-simulator" 3 ].some(config => getBuildConfiguration(config)); 4 5 // This test often times out on debug simulators due to the extreme slowdown. 6 if (getBuildConfiguration("debug") && isSimulator) 7 quit(); 8 9 // All the glue code is wrapped in a function so it can be executed uncached and 10 // cached or compiled separately. 11 function runBox2d(cacheEntryOrModule) { 12 13 // The Module object: Our interface to the outside world. We import 14 // and export values on it, and do the work to get that through 15 // closure compiler if necessary. There are various ways Module can be used: 16 // 1. Not defined. We create it here 17 // 2. A function parameter, function(Module) { ..generated code.. } 18 // 3. pre-run appended it, var Module = {}; ..generated code.. 19 // 4. External script tag defines var Module. 20 // We need to do an eval in order to handle the closure compiler 21 // case, where this code here is minified but Module was defined 22 // elsewhere (e.g. case 4 above). We also need to check if Module 23 // already exists (e.g. case 3 above). 24 // Note that if you want to run closure, and also to use Module 25 // after the generated code, you will need to define var Module = {}; 26 // before the code. Then that object will be used in the code, and you 27 // can continue to use Module afterwards as well. 28 var Module; 29 if (!Module) Module = (typeof Module !== 'undefined' ? Module : null) || {}; 30 31 // Sometimes an existing Module object exists with properties 32 // meant to overwrite the default module functionality. Here 33 // we collect those properties and reapply _after_ we configure 34 // the current environment's defaults to avoid having to be so 35 // defensive during initialization. 36 var moduleOverrides = {}; 37 for (var key in Module) { 38 if (Module.hasOwnProperty(key)) { 39 moduleOverrides[key] = Module[key]; 40 } 41 } 42 43 if (!Module['print']) Module['print'] = print; 44 if (typeof printErr != 'undefined') Module['printErr'] = print; 45 46 if (typeof read != 'undefined') { 47 Module['read'] = read; 48 } else { 49 Module['read'] = function read() { throw 'no read() available' }; 50 } 51 52 if (typeof scriptArgs != 'undefined') { 53 Module['arguments'] = scriptArgs; 54 } else if (typeof arguments != 'undefined') { 55 Module['arguments'] = arguments; 56 } 57 58 59 function globalEval(x) { 60 eval.call(null, x); 61 } 62 if (!Module['load'] && Module['read']) { 63 Module['load'] = function load(f) { 64 globalEval(Module['read'](f)); 65 }; 66 } 67 if (!Module['print']) { 68 Module['print'] = function(){}; 69 } 70 if (!Module['printErr']) { 71 Module['printErr'] = Module['print']; 72 } 73 if (!Module['arguments']) { 74 Module['arguments'] = []; 75 } 76 if (!Module['thisProgram']) { 77 Module['thisProgram'] = './this.program'; 78 } 79 80 // *** Environment setup code *** 81 82 // Closure helpers 83 Module.print = Module['print']; 84 Module.printErr = Module['printErr']; 85 86 // Callbacks 87 Module['preRun'] = []; 88 Module['postRun'] = []; 89 90 // Merge back in the overrides 91 for (var key in moduleOverrides) { 92 if (moduleOverrides.hasOwnProperty(key)) { 93 Module[key] = moduleOverrides[key]; 94 } 95 } 96 // Free the object hierarchy contained in the overrides, this lets the GC 97 // reclaim data used e.g. in memoryInitializerRequest, which is a large typed array. 98 moduleOverrides = undefined; 99 100 101 102 // {{PREAMBLE_ADDITIONS}} 103 104 // === Preamble library stuff === 105 106 // Documentation for the public APIs defined in this file must be updated in: 107 // site/source/docs/api_reference/preamble.js.rst 108 // A prebuilt local version of the documentation is available at: 109 // site/build/text/docs/api_reference/preamble.js.txt 110 // You can also build docs locally as HTML or other formats in site/ 111 // An online HTML version (which may be of a different version of Emscripten) 112 // is up at http://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html 113 114 //======================================== 115 // Runtime code shared with compiler 116 //======================================== 117 118 var Runtime = { 119 setTempRet0: function (value) { 120 tempRet0 = value; 121 }, 122 getTempRet0: function () { 123 return tempRet0; 124 }, 125 stackSave: function () { 126 return STACKTOP; 127 }, 128 stackRestore: function (stackTop) { 129 STACKTOP = stackTop; 130 }, 131 getNativeTypeSize: function (type) { 132 switch (type) { 133 case 'i1': case 'i8': return 1; 134 case 'i16': return 2; 135 case 'i32': return 4; 136 case 'i64': return 8; 137 case 'float': return 4; 138 case 'double': return 8; 139 default: { 140 if (type[type.length-1] === '*') { 141 return Runtime.QUANTUM_SIZE; // A pointer 142 } else if (type[0] === 'i') { 143 var bits = parseInt(type.substr(1)); 144 assert(bits % 8 === 0); 145 return bits/8; 146 } else { 147 return 0; 148 } 149 } 150 } 151 }, 152 getNativeFieldSize: function (type) { 153 return Math.max(Runtime.getNativeTypeSize(type), Runtime.QUANTUM_SIZE); 154 }, 155 STACK_ALIGN: 16, 156 prepVararg: function (ptr, type) { 157 if (type === 'double' || type === 'i64') { 158 // move so the load is aligned 159 if (ptr & 7) { 160 assert((ptr & 7) === 4); 161 ptr += 4; 162 } 163 } else { 164 assert((ptr & 3) === 0); 165 } 166 return ptr; 167 }, 168 getAlignSize: function (type, size, vararg) { 169 // we align i64s and doubles on 64-bit boundaries, unlike x86 170 if (!vararg && (type == 'i64' || type == 'double')) return 8; 171 if (!type) return Math.min(size, 8); // align structures internally to 64 bits 172 return Math.min(size || (type ? Runtime.getNativeFieldSize(type) : 0), Runtime.QUANTUM_SIZE); 173 }, 174 dynCall: function (sig, ptr, args) { 175 if (args && args.length) { 176 return Module['dynCall_' + sig].apply(null, [ptr].concat(args)); 177 } else { 178 return Module['dynCall_' + sig].call(null, ptr); 179 } 180 }, 181 functionPointers: [], 182 addFunction: function (func) { 183 for (var i = 0; i < Runtime.functionPointers.length; i++) { 184 if (!Runtime.functionPointers[i]) { 185 Runtime.functionPointers[i] = func; 186 return 2*(1 + i); 187 } 188 } 189 throw 'Finished up all reserved function pointers. Use a higher value for RESERVED_FUNCTION_POINTERS.'; 190 }, 191 removeFunction: function (index) { 192 Runtime.functionPointers[(index-2)/2] = null; 193 }, 194 warnOnce: function (text) { 195 if (!Runtime.warnOnce.shown) Runtime.warnOnce.shown = {}; 196 if (!Runtime.warnOnce.shown[text]) { 197 Runtime.warnOnce.shown[text] = 1; 198 Module.printErr(text); 199 } 200 }, 201 funcWrappers: {}, 202 getFuncWrapper: function (func, sig) { 203 assert(sig); 204 if (!Runtime.funcWrappers[sig]) { 205 Runtime.funcWrappers[sig] = {}; 206 } 207 var sigCache = Runtime.funcWrappers[sig]; 208 if (!sigCache[func]) { 209 // optimize away arguments usage in common cases 210 if (sig.length === 1) { 211 sigCache[func] = function dynCall_wrapper() { 212 return Runtime.dynCall(sig, func); 213 }; 214 } else if (sig.length === 2) { 215 sigCache[func] = function dynCall_wrapper(arg) { 216 return Runtime.dynCall(sig, func, [arg]); 217 }; 218 } else { 219 // general case 220 sigCache[func] = function dynCall_wrapper() { 221 return Runtime.dynCall(sig, func, Array.prototype.slice.call(arguments)); 222 }; 223 } 224 } 225 return sigCache[func]; 226 }, 227 getCompilerSetting: function (name) { 228 throw 'You must build with -s RETAIN_COMPILER_SETTINGS=1 for Runtime.getCompilerSetting or emscripten_get_compiler_setting to work'; 229 }, 230 stackAlloc: function (size) { var ret = STACKTOP;STACKTOP = (STACKTOP + size)|0;STACKTOP = (((STACKTOP)+15)&-16); return ret; }, 231 staticAlloc: function (size) { var ret = STATICTOP;STATICTOP = (STATICTOP + size)|0;STATICTOP = (((STATICTOP)+15)&-16); return ret; }, 232 dynamicAlloc: function (size) { var ret = HEAP32[DYNAMICTOP_PTR>>2];var end = (((ret + size + 15)|0) & -16);HEAP32[DYNAMICTOP_PTR>>2] = end;if (end >= TOTAL_MEMORY) {var success = enlargeMemory();if (!success) {HEAP32[DYNAMICTOP_PTR>>2] = ret;return 0;}}return ret;}, 233 alignMemory: function (size,quantum) { var ret = size = Math.ceil((size)/(quantum ? quantum : 16))*(quantum ? quantum : 16); return ret; }, 234 makeBigInt: function (low,high,unsigned) { var ret = (unsigned ? ((+((low>>>0)))+((+((high>>>0)))*4294967296.0)) : ((+((low>>>0)))+((+((high|0)))*4294967296.0))); return ret; }, 235 GLOBAL_BASE: 1024, 236 QUANTUM_SIZE: 4, 237 __dummy__: 0 238 } 239 240 241 242 Module["Runtime"] = Runtime; 243 244 245 246 //======================================== 247 // Runtime essentials 248 //======================================== 249 250 var ABORT = 0; // whether we are quitting the application. no code should run after this. set in exit() and abort() 251 var EXITSTATUS = 0; 252 253 function assert(condition, text) { 254 if (!condition) { 255 abort('Assertion failed: ' + text); 256 } 257 } 258 259 var globalScope = this; 260 261 // Returns the C function with a specified identifier (for C++, you need to do manual name mangling) 262 function getCFunc(ident) { 263 var func = Module['_' + ident]; // closure exported function 264 if (!func) { 265 try { func = eval('_' + ident); } catch(e) {} 266 } 267 assert(func, 'Cannot call unknown function ' + ident + ' (perhaps LLVM optimizations or closure removed it?)'); 268 return func; 269 } 270 271 var cwrap, ccall; 272 (function(){ 273 var JSfuncs = { 274 // Helpers for cwrap -- it can't refer to Runtime directly because it might 275 // be renamed by closure, instead it calls JSfuncs['stackSave'].body to find 276 // out what the minified function name is. 277 'stackSave': function() { 278 Runtime.stackSave() 279 }, 280 'stackRestore': function() { 281 Runtime.stackRestore() 282 }, 283 // type conversion from js to c 284 'arrayToC' : function(arr) { 285 var ret = Runtime.stackAlloc(arr.length); 286 writeArrayToMemory(arr, ret); 287 return ret; 288 }, 289 'stringToC' : function(str) { 290 var ret = 0; 291 if (str !== null && str !== undefined && str !== 0) { // null string 292 // at most 4 bytes per UTF-8 code point, +1 for the trailing '\0' 293 var len = (str.length << 2) + 1; 294 ret = Runtime.stackAlloc(len); 295 stringToUTF8(str, ret, len); 296 } 297 return ret; 298 } 299 }; 300 // For fast lookup of conversion functions 301 var toC = {'string' : JSfuncs['stringToC'], 'array' : JSfuncs['arrayToC']}; 302 303 // C calling interface. 304 ccall = function ccallFunc(ident, returnType, argTypes, args, opts) { 305 var func = getCFunc(ident); 306 var cArgs = []; 307 var stack = 0; 308 if (args) { 309 for (var i = 0; i < args.length; i++) { 310 var converter = toC[argTypes[i]]; 311 if (converter) { 312 if (stack === 0) stack = Runtime.stackSave(); 313 cArgs[i] = converter(args[i]); 314 } else { 315 cArgs[i] = args[i]; 316 } 317 } 318 } 319 var ret = func.apply(null, cArgs); 320 if (returnType === 'string') ret = Pointer_stringify(ret); 321 if (stack !== 0) { 322 if (opts && opts.async) { 323 EmterpreterAsync.asyncFinalizers.push(function() { 324 Runtime.stackRestore(stack); 325 }); 326 return; 327 } 328 Runtime.stackRestore(stack); 329 } 330 return ret; 331 } 332 333 var sourceRegex = /^function\s*[a-zA-Z$_0-9]*\s*\(([^)]*)\)\s*{\s*([^*]*?)[\s;]*(?:return\s*(.*?)[;\s]*)?}$/; 334 function parseJSFunc(jsfunc) { 335 // Match the body and the return value of a javascript function source 336 var parsed = jsfunc.toString().match(sourceRegex).slice(1); 337 return {arguments : parsed[0], body : parsed[1], returnValue: parsed[2]} 338 } 339 340 // sources of useful functions. we create this lazily as it can trigger a source decompression on this entire file 341 var JSsource = null; 342 function ensureJSsource() { 343 if (!JSsource) { 344 JSsource = {}; 345 for (var fun in JSfuncs) { 346 if (JSfuncs.hasOwnProperty(fun)) { 347 // Elements of toCsource are arrays of three items: 348 // the code, and the return value 349 JSsource[fun] = parseJSFunc(JSfuncs[fun]); 350 } 351 } 352 } 353 } 354 355 cwrap = function cwrap(ident, returnType, argTypes) { 356 argTypes = argTypes || []; 357 var cfunc = getCFunc(ident); 358 // When the function takes numbers and returns a number, we can just return 359 // the original function 360 var numericArgs = argTypes.every(function(type){ return type === 'number'}); 361 var numericRet = (returnType !== 'string'); 362 if ( numericRet && numericArgs) { 363 return cfunc; 364 } 365 // Creation of the arguments list (["$1","$2",...,"$nargs"]) 366 var argNames = argTypes.map(function(x,i){return '$'+i}); 367 var funcstr = "(function(" + argNames.join(',') + ") {"; 368 var nargs = argTypes.length; 369 if (!numericArgs) { 370 // Generate the code needed to convert the arguments from javascript 371 // values to pointers 372 ensureJSsource(); 373 funcstr += 'var stack = ' + JSsource['stackSave'].body + ';'; 374 for (var i = 0; i < nargs; i++) { 375 var arg = argNames[i], type = argTypes[i]; 376 if (type === 'number') continue; 377 var convertCode = JSsource[type + 'ToC']; // [code, return] 378 funcstr += 'var ' + convertCode.arguments + ' = ' + arg + ';'; 379 funcstr += convertCode.body + ';'; 380 funcstr += arg + '=(' + convertCode.returnValue + ');'; 381 } 382 } 383 384 // When the code is compressed, the name of cfunc is not literally 'cfunc' anymore 385 var cfuncname = parseJSFunc(function(){return cfunc}).returnValue; 386 // Call the function 387 funcstr += 'var ret = ' + cfuncname + '(' + argNames.join(',') + ');'; 388 if (!numericRet) { // Return type can only by 'string' or 'number' 389 // Convert the result to a string 390 var strgfy = parseJSFunc(function(){return Pointer_stringify}).returnValue; 391 funcstr += 'ret = ' + strgfy + '(ret);'; 392 } 393 if (!numericArgs) { 394 // If we had a stack, restore it 395 ensureJSsource(); 396 funcstr += JSsource['stackRestore'].body.replace('()', '(stack)') + ';'; 397 } 398 funcstr += 'return ret})'; 399 return eval(funcstr); 400 }; 401 })(); 402 Module["ccall"] = ccall; 403 Module["cwrap"] = cwrap; 404 405 function setValue(ptr, value, type, noSafe) { 406 type = type || 'i8'; 407 if (type.charAt(type.length-1) === '*') type = 'i32'; // pointers are 32-bit 408 switch(type) { 409 case 'i1': HEAP8[((ptr)>>0)]=value; break; 410 case 'i8': HEAP8[((ptr)>>0)]=value; break; 411 case 'i16': HEAP16[((ptr)>>1)]=value; break; 412 case 'i32': HEAP32[((ptr)>>2)]=value; break; 413 case 'i64': (tempI64 = [value>>>0,(tempDouble=value,(+(Math_abs(tempDouble))) >= 1.0 ? (tempDouble > 0.0 ? ((Math_min((+(Math_floor((tempDouble)/4294967296.0))), 4294967295.0))|0)>>>0 : (~~((+(Math_ceil((tempDouble - +(((~~(tempDouble)))>>>0))/4294967296.0)))))>>>0) : 0)],HEAP32[((ptr)>>2)]=tempI64[0],HEAP32[(((ptr)+(4))>>2)]=tempI64[1]); break; 414 case 'float': HEAPF32[((ptr)>>2)]=value; break; 415 case 'double': HEAPF64[((ptr)>>3)]=value; break; 416 default: abort('invalid type for setValue: ' + type); 417 } 418 } 419 Module["setValue"] = setValue; 420 421 422 function getValue(ptr, type, noSafe) { 423 type = type || 'i8'; 424 if (type.charAt(type.length-1) === '*') type = 'i32'; // pointers are 32-bit 425 switch(type) { 426 case 'i1': return HEAP8[((ptr)>>0)]; 427 case 'i8': return HEAP8[((ptr)>>0)]; 428 case 'i16': return HEAP16[((ptr)>>1)]; 429 case 'i32': return HEAP32[((ptr)>>2)]; 430 case 'i64': return HEAP32[((ptr)>>2)]; 431 case 'float': return HEAPF32[((ptr)>>2)]; 432 case 'double': return HEAPF64[((ptr)>>3)]; 433 default: abort('invalid type for setValue: ' + type); 434 } 435 return null; 436 } 437 Module["getValue"] = getValue; 438 439 var ALLOC_NORMAL = 0; // Tries to use _malloc() 440 var ALLOC_STACK = 1; // Lives for the duration of the current function call 441 var ALLOC_STATIC = 2; // Cannot be freed 442 var ALLOC_DYNAMIC = 3; // Cannot be freed except through sbrk 443 var ALLOC_NONE = 4; // Do not allocate 444 Module["ALLOC_NORMAL"] = ALLOC_NORMAL; 445 Module["ALLOC_STACK"] = ALLOC_STACK; 446 Module["ALLOC_STATIC"] = ALLOC_STATIC; 447 Module["ALLOC_DYNAMIC"] = ALLOC_DYNAMIC; 448 Module["ALLOC_NONE"] = ALLOC_NONE; 449 450 // allocate(): This is for internal use. You can use it yourself as well, but the interface 451 // is a little tricky (see docs right below). The reason is that it is optimized 452 // for multiple syntaxes to save space in generated code. So you should 453 // normally not use allocate(), and instead allocate memory using _malloc(), 454 // initialize it with setValue(), and so forth. 455 // @slab: An array of data, or a number. If a number, then the size of the block to allocate, 456 // in *bytes* (note that this is sometimes confusing: the next parameter does not 457 // affect this!) 458 // @types: Either an array of types, one for each byte (or 0 if no type at that position), 459 // or a single type which is used for the entire block. This only matters if there 460 // is initial data - if @slab is a number, then this does not matter at all and is 461 // ignored. 462 // @allocator: How to allocate memory, see ALLOC_* 463 function allocate(slab, types, allocator, ptr) { 464 var zeroinit, size; 465 if (typeof slab === 'number') { 466 zeroinit = true; 467 size = slab; 468 } else { 469 zeroinit = false; 470 size = slab.length; 471 } 472 473 var singleType = typeof types === 'string' ? types : null; 474 475 var ret; 476 if (allocator == ALLOC_NONE) { 477 ret = ptr; 478 } else { 479 ret = [typeof _malloc === 'function' ? _malloc : Runtime.staticAlloc, Runtime.stackAlloc, Runtime.staticAlloc, Runtime.dynamicAlloc][allocator === undefined ? ALLOC_STATIC : allocator](Math.max(size, singleType ? 1 : types.length)); 480 } 481 482 if (zeroinit) { 483 var ptr = ret, stop; 484 assert((ret & 3) == 0); 485 stop = ret + (size & ~3); 486 for (; ptr < stop; ptr += 4) { 487 HEAP32[((ptr)>>2)]=0; 488 } 489 stop = ret + size; 490 while (ptr < stop) { 491 HEAP8[((ptr++)>>0)]=0; 492 } 493 return ret; 494 } 495 496 if (singleType === 'i8') { 497 if (slab.subarray || slab.slice) { 498 HEAPU8.set(slab, ret); 499 } else { 500 HEAPU8.set(new Uint8Array(slab), ret); 501 } 502 return ret; 503 } 504 505 var i = 0, type, typeSize, previousType; 506 while (i < size) { 507 var curr = slab[i]; 508 509 if (typeof curr === 'function') { 510 curr = Runtime.getFunctionIndex(curr); 511 } 512 513 type = singleType || types[i]; 514 if (type === 0) { 515 i++; 516 continue; 517 } 518 519 if (type == 'i64') type = 'i32'; // special case: we have one i32 here, and one i32 later 520 521 setValue(ret+i, curr, type); 522 523 // no need to look up size unless type changes, so cache it 524 if (previousType !== type) { 525 typeSize = Runtime.getNativeTypeSize(type); 526 previousType = type; 527 } 528 i += typeSize; 529 } 530 531 return ret; 532 } 533 Module["allocate"] = allocate; 534 535 // Allocate memory during any stage of startup - static memory early on, dynamic memory later, malloc when ready 536 function getMemory(size) { 537 if (!staticSealed) return Runtime.staticAlloc(size); 538 if (!runtimeInitialized) return Runtime.dynamicAlloc(size); 539 return _malloc(size); 540 } 541 Module["getMemory"] = getMemory; 542 543 function Pointer_stringify(ptr, /* optional */ length) { 544 if (length === 0 || !ptr) return ''; 545 // TODO: use TextDecoder 546 // Find the length, and check for UTF while doing so 547 var hasUtf = 0; 548 var t; 549 var i = 0; 550 while (1) { 551 t = HEAPU8[(((ptr)+(i))>>0)]; 552 hasUtf |= t; 553 if (t == 0 && !length) break; 554 i++; 555 if (length && i == length) break; 556 } 557 if (!length) length = i; 558 559 var ret = ''; 560 561 if (hasUtf < 128) { 562 var MAX_CHUNK = 1024; // split up into chunks, because .apply on a huge string can overflow the stack 563 var curr; 564 while (length > 0) { 565 curr = String.fromCharCode.apply(String, HEAPU8.subarray(ptr, ptr + Math.min(length, MAX_CHUNK))); 566 ret = ret ? ret + curr : curr; 567 ptr += MAX_CHUNK; 568 length -= MAX_CHUNK; 569 } 570 return ret; 571 } 572 return Module['UTF8ToString'](ptr); 573 } 574 Module["Pointer_stringify"] = Pointer_stringify; 575 576 // Given a pointer 'ptr' to a null-terminated ASCII-encoded string in the emscripten HEAP, returns 577 // a copy of that string as a Javascript String object. 578 579 function AsciiToString(ptr) { 580 var str = ''; 581 while (1) { 582 var ch = HEAP8[((ptr++)>>0)]; 583 if (!ch) return str; 584 str += String.fromCharCode(ch); 585 } 586 } 587 Module["AsciiToString"] = AsciiToString; 588 589 // Copies the given Javascript String object 'str' to the emscripten HEAP at address 'outPtr', 590 // null-terminated and encoded in ASCII form. The copy will require at most str.length+1 bytes of space in the HEAP. 591 592 function stringToAscii(str, outPtr) { 593 return writeAsciiToMemory(str, outPtr, false); 594 } 595 Module["stringToAscii"] = stringToAscii; 596 597 // Given a pointer 'ptr' to a null-terminated UTF8-encoded string in the given array that contains uint8 values, returns 598 // a copy of that string as a Javascript String object. 599 600 var UTF8Decoder = typeof TextDecoder !== 'undefined' ? new TextDecoder('utf8') : undefined; 601 function UTF8ArrayToString(u8Array, idx) { 602 var endPtr = idx; 603 // TextDecoder needs to know the byte length in advance, it doesn't stop on null terminator by itself. 604 // Also, use the length info to avoid running tiny strings through TextDecoder, since .subarray() allocates garbage. 605 while (u8Array[endPtr]) ++endPtr; 606 607 if (endPtr - idx > 16 && u8Array.subarray && UTF8Decoder) { 608 return UTF8Decoder.decode(u8Array.subarray(idx, endPtr)); 609 } else { 610 var u0, u1, u2, u3, u4, u5; 611 612 var str = ''; 613 while (1) { 614 // For UTF8 byte structure, see http://en.wikipedia.org/wiki/UTF-8#Description and https://www.ietf.org/rfc/rfc2279.txt and https://tools.ietf.org/html/rfc3629 615 u0 = u8Array[idx++]; 616 if (!u0) return str; 617 if (!(u0 & 0x80)) { str += String.fromCharCode(u0); continue; } 618 u1 = u8Array[idx++] & 63; 619 if ((u0 & 0xE0) == 0xC0) { str += String.fromCharCode(((u0 & 31) << 6) | u1); continue; } 620 u2 = u8Array[idx++] & 63; 621 if ((u0 & 0xF0) == 0xE0) { 622 u0 = ((u0 & 15) << 12) | (u1 << 6) | u2; 623 } else { 624 u3 = u8Array[idx++] & 63; 625 if ((u0 & 0xF8) == 0xF0) { 626 u0 = ((u0 & 7) << 18) | (u1 << 12) | (u2 << 6) | u3; 627 } else { 628 u4 = u8Array[idx++] & 63; 629 if ((u0 & 0xFC) == 0xF8) { 630 u0 = ((u0 & 3) << 24) | (u1 << 18) | (u2 << 12) | (u3 << 6) | u4; 631 } else { 632 u5 = u8Array[idx++] & 63; 633 u0 = ((u0 & 1) << 30) | (u1 << 24) | (u2 << 18) | (u3 << 12) | (u4 << 6) | u5; 634 } 635 } 636 } 637 if (u0 < 0x10000) { 638 str += String.fromCharCode(u0); 639 } else { 640 var ch = u0 - 0x10000; 641 str += String.fromCharCode(0xD800 | (ch >> 10), 0xDC00 | (ch & 0x3FF)); 642 } 643 } 644 } 645 } 646 Module["UTF8ArrayToString"] = UTF8ArrayToString; 647 648 // Given a pointer 'ptr' to a null-terminated UTF8-encoded string in the emscripten HEAP, returns 649 // a copy of that string as a Javascript String object. 650 651 function UTF8ToString(ptr) { 652 return UTF8ArrayToString(HEAPU8,ptr); 653 } 654 Module["UTF8ToString"] = UTF8ToString; 655 656 // Copies the given Javascript String object 'str' to the given byte array at address 'outIdx', 657 // encoded in UTF8 form and null-terminated. The copy will require at most str.length*4+1 bytes of space in the HEAP. 658 // Use the function lengthBytesUTF8 to compute the exact number of bytes (excluding null terminator) that this function will write. 659 // Parameters: 660 // str: the Javascript string to copy. 661 // outU8Array: the array to copy to. Each index in this array is assumed to be one 8-byte element. 662 // outIdx: The starting offset in the array to begin the copying. 663 // maxBytesToWrite: The maximum number of bytes this function can write to the array. This count should include the null 664 // terminator, i.e. if maxBytesToWrite=1, only the null terminator will be written and nothing else. 665 // maxBytesToWrite=0 does not write any bytes to the output, not even the null terminator. 666 // Returns the number of bytes written, EXCLUDING the null terminator. 667 668 function stringToUTF8Array(str, outU8Array, outIdx, maxBytesToWrite) { 669 if (!(maxBytesToWrite > 0)) // Parameter maxBytesToWrite is not optional. Negative values, 0, null, undefined and false each don't write out any bytes. 670 return 0; 671 672 var startIdx = outIdx; 673 var endIdx = outIdx + maxBytesToWrite - 1; // -1 for string null terminator. 674 for (var i = 0; i < str.length; ++i) { 675 // Gotcha: charCodeAt returns a 16-bit word that is a UTF-16 encoded code unit, not a Unicode code point of the character! So decode UTF16->UTF32->UTF8. 676 // See http://unicode.org/faq/utf_bom.html#utf16-3 677 // For UTF8 byte structure, see http://en.wikipedia.org/wiki/UTF-8#Description and https://www.ietf.org/rfc/rfc2279.txt and https://tools.ietf.org/html/rfc3629 678 var u = str.charCodeAt(i); // possibly a lead surrogate 679 if (u >= 0xD800 && u <= 0xDFFF) u = 0x10000 + ((u & 0x3FF) << 10) | (str.charCodeAt(++i) & 0x3FF); 680 if (u <= 0x7F) { 681 if (outIdx >= endIdx) break; 682 outU8Array[outIdx++] = u; 683 } else if (u <= 0x7FF) { 684 if (outIdx + 1 >= endIdx) break; 685 outU8Array[outIdx++] = 0xC0 | (u >> 6); 686 outU8Array[outIdx++] = 0x80 | (u & 63); 687 } else if (u <= 0xFFFF) { 688 if (outIdx + 2 >= endIdx) break; 689 outU8Array[outIdx++] = 0xE0 | (u >> 12); 690 outU8Array[outIdx++] = 0x80 | ((u >> 6) & 63); 691 outU8Array[outIdx++] = 0x80 | (u & 63); 692 } else if (u <= 0x1FFFFF) { 693 if (outIdx + 3 >= endIdx) break; 694 outU8Array[outIdx++] = 0xF0 | (u >> 18); 695 outU8Array[outIdx++] = 0x80 | ((u >> 12) & 63); 696 outU8Array[outIdx++] = 0x80 | ((u >> 6) & 63); 697 outU8Array[outIdx++] = 0x80 | (u & 63); 698 } else if (u <= 0x3FFFFFF) { 699 if (outIdx + 4 >= endIdx) break; 700 outU8Array[outIdx++] = 0xF8 | (u >> 24); 701 outU8Array[outIdx++] = 0x80 | ((u >> 18) & 63); 702 outU8Array[outIdx++] = 0x80 | ((u >> 12) & 63); 703 outU8Array[outIdx++] = 0x80 | ((u >> 6) & 63); 704 outU8Array[outIdx++] = 0x80 | (u & 63); 705 } else { 706 if (outIdx + 5 >= endIdx) break; 707 outU8Array[outIdx++] = 0xFC | (u >> 30); 708 outU8Array[outIdx++] = 0x80 | ((u >> 24) & 63); 709 outU8Array[outIdx++] = 0x80 | ((u >> 18) & 63); 710 outU8Array[outIdx++] = 0x80 | ((u >> 12) & 63); 711 outU8Array[outIdx++] = 0x80 | ((u >> 6) & 63); 712 outU8Array[outIdx++] = 0x80 | (u & 63); 713 } 714 } 715 // Null-terminate the pointer to the buffer. 716 outU8Array[outIdx] = 0; 717 return outIdx - startIdx; 718 } 719 Module["stringToUTF8Array"] = stringToUTF8Array; 720 721 // Copies the given Javascript String object 'str' to the emscripten HEAP at address 'outPtr', 722 // null-terminated and encoded in UTF8 form. The copy will require at most str.length*4+1 bytes of space in the HEAP. 723 // Use the function lengthBytesUTF8 to compute the exact number of bytes (excluding null terminator) that this function will write. 724 // Returns the number of bytes written, EXCLUDING the null terminator. 725 726 function stringToUTF8(str, outPtr, maxBytesToWrite) { 727 return stringToUTF8Array(str, HEAPU8,outPtr, maxBytesToWrite); 728 } 729 Module["stringToUTF8"] = stringToUTF8; 730 731 // Returns the number of bytes the given Javascript string takes if encoded as a UTF8 byte array, EXCLUDING the null terminator byte. 732 733 function lengthBytesUTF8(str) { 734 var len = 0; 735 for (var i = 0; i < str.length; ++i) { 736 // Gotcha: charCodeAt returns a 16-bit word that is a UTF-16 encoded code unit, not a Unicode code point of the character! So decode UTF16->UTF32->UTF8. 737 // See http://unicode.org/faq/utf_bom.html#utf16-3 738 var u = str.charCodeAt(i); // possibly a lead surrogate 739 if (u >= 0xD800 && u <= 0xDFFF) u = 0x10000 + ((u & 0x3FF) << 10) | (str.charCodeAt(++i) & 0x3FF); 740 if (u <= 0x7F) { 741 ++len; 742 } else if (u <= 0x7FF) { 743 len += 2; 744 } else if (u <= 0xFFFF) { 745 len += 3; 746 } else if (u <= 0x1FFFFF) { 747 len += 4; 748 } else if (u <= 0x3FFFFFF) { 749 len += 5; 750 } else { 751 len += 6; 752 } 753 } 754 return len; 755 } 756 Module["lengthBytesUTF8"] = lengthBytesUTF8; 757 758 // Given a pointer 'ptr' to a null-terminated UTF16LE-encoded string in the emscripten HEAP, returns 759 // a copy of that string as a Javascript String object. 760 761 var UTF16Decoder = typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-16le') : undefined; 762 function UTF16ToString(ptr) { 763 var endPtr = ptr; 764 // TextDecoder needs to know the byte length in advance, it doesn't stop on null terminator by itself. 765 // Also, use the length info to avoid running tiny strings through TextDecoder, since .subarray() allocates garbage. 766 var idx = endPtr >> 1; 767 while (HEAP16[idx]) ++idx; 768 endPtr = idx << 1; 769 770 if (endPtr - ptr > 32 && UTF16Decoder) { 771 return UTF16Decoder.decode(HEAPU8.subarray(ptr, endPtr)); 772 } else { 773 var i = 0; 774 775 var str = ''; 776 while (1) { 777 var codeUnit = HEAP16[(((ptr)+(i*2))>>1)]; 778 if (codeUnit == 0) return str; 779 ++i; 780 // fromCharCode constructs a character from a UTF-16 code unit, so we can pass the UTF16 string right through. 781 str += String.fromCharCode(codeUnit); 782 } 783 } 784 } 785 786 787 // Copies the given Javascript String object 'str' to the emscripten HEAP at address 'outPtr', 788 // null-terminated and encoded in UTF16 form. The copy will require at most str.length*4+2 bytes of space in the HEAP. 789 // Use the function lengthBytesUTF16() to compute the exact number of bytes (excluding null terminator) that this function will write. 790 // Parameters: 791 // str: the Javascript string to copy. 792 // outPtr: Byte address in Emscripten HEAP where to write the string to. 793 // maxBytesToWrite: The maximum number of bytes this function can write to the array. This count should include the null 794 // terminator, i.e. if maxBytesToWrite=2, only the null terminator will be written and nothing else. 795 // maxBytesToWrite<2 does not write any bytes to the output, not even the null terminator. 796 // Returns the number of bytes written, EXCLUDING the null terminator. 797 798 function stringToUTF16(str, outPtr, maxBytesToWrite) { 799 // Backwards compatibility: if max bytes is not specified, assume unsafe unbounded write is allowed. 800 if (maxBytesToWrite === undefined) { 801 maxBytesToWrite = 0x7FFFFFFF; 802 } 803 if (maxBytesToWrite < 2) return 0; 804 maxBytesToWrite -= 2; // Null terminator. 805 var startPtr = outPtr; 806 var numCharsToWrite = (maxBytesToWrite < str.length*2) ? (maxBytesToWrite / 2) : str.length; 807 for (var i = 0; i < numCharsToWrite; ++i) { 808 // charCodeAt returns a UTF-16 encoded code unit, so it can be directly written to the HEAP. 809 var codeUnit = str.charCodeAt(i); // possibly a lead surrogate 810 HEAP16[((outPtr)>>1)]=codeUnit; 811 outPtr += 2; 812 } 813 // Null-terminate the pointer to the HEAP. 814 HEAP16[((outPtr)>>1)]=0; 815 return outPtr - startPtr; 816 } 817 818 819 // Returns the number of bytes the given Javascript string takes if encoded as a UTF16 byte array, EXCLUDING the null terminator byte. 820 821 function lengthBytesUTF16(str) { 822 return str.length*2; 823 } 824 825 826 function UTF32ToString(ptr) { 827 var i = 0; 828 829 var str = ''; 830 while (1) { 831 var utf32 = HEAP32[(((ptr)+(i*4))>>2)]; 832 if (utf32 == 0) 833 return str; 834 ++i; 835 // 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. 836 // See http://unicode.org/faq/utf_bom.html#utf16-3 837 if (utf32 >= 0x10000) { 838 var ch = utf32 - 0x10000; 839 str += String.fromCharCode(0xD800 | (ch >> 10), 0xDC00 | (ch & 0x3FF)); 840 } else { 841 str += String.fromCharCode(utf32); 842 } 843 } 844 } 845 846 847 // Copies the given Javascript String object 'str' to the emscripten HEAP at address 'outPtr', 848 // null-terminated and encoded in UTF32 form. The copy will require at most str.length*4+4 bytes of space in the HEAP. 849 // Use the function lengthBytesUTF32() to compute the exact number of bytes (excluding null terminator) that this function will write. 850 // Parameters: 851 // str: the Javascript string to copy. 852 // outPtr: Byte address in Emscripten HEAP where to write the string to. 853 // maxBytesToWrite: The maximum number of bytes this function can write to the array. This count should include the null 854 // terminator, i.e. if maxBytesToWrite=4, only the null terminator will be written and nothing else. 855 // maxBytesToWrite<4 does not write any bytes to the output, not even the null terminator. 856 // Returns the number of bytes written, EXCLUDING the null terminator. 857 858 function stringToUTF32(str, outPtr, maxBytesToWrite) { 859 // Backwards compatibility: if max bytes is not specified, assume unsafe unbounded write is allowed. 860 if (maxBytesToWrite === undefined) { 861 maxBytesToWrite = 0x7FFFFFFF; 862 } 863 if (maxBytesToWrite < 4) return 0; 864 var startPtr = outPtr; 865 var endPtr = startPtr + maxBytesToWrite - 4; 866 for (var i = 0; i < str.length; ++i) { 867 // 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. 868 // See http://unicode.org/faq/utf_bom.html#utf16-3 869 var codeUnit = str.charCodeAt(i); // possibly a lead surrogate 870 if (codeUnit >= 0xD800 && codeUnit <= 0xDFFF) { 871 var trailSurrogate = str.charCodeAt(++i); 872 codeUnit = 0x10000 + ((codeUnit & 0x3FF) << 10) | (trailSurrogate & 0x3FF); 873 } 874 HEAP32[((outPtr)>>2)]=codeUnit; 875 outPtr += 4; 876 if (outPtr + 4 > endPtr) break; 877 } 878 // Null-terminate the pointer to the HEAP. 879 HEAP32[((outPtr)>>2)]=0; 880 return outPtr - startPtr; 881 } 882 883 884 // Returns the number of bytes the given Javascript string takes if encoded as a UTF16 byte array, EXCLUDING the null terminator byte. 885 886 function lengthBytesUTF32(str) { 887 var len = 0; 888 for (var i = 0; i < str.length; ++i) { 889 // 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. 890 // See http://unicode.org/faq/utf_bom.html#utf16-3 891 var codeUnit = str.charCodeAt(i); 892 if (codeUnit >= 0xD800 && codeUnit <= 0xDFFF) ++i; // possibly a lead surrogate, so skip over the tail surrogate. 893 len += 4; 894 } 895 896 return len; 897 } 898 899 900 function demangle(func) { 901 var __cxa_demangle_func = Module['___cxa_demangle'] || Module['__cxa_demangle']; 902 if (__cxa_demangle_func) { 903 try { 904 var s = 905 func.substr(1); 906 var len = lengthBytesUTF8(s)+1; 907 var buf = _malloc(len); 908 stringToUTF8(s, buf, len); 909 var status = _malloc(4); 910 var ret = __cxa_demangle_func(buf, 0, 0, status); 911 if (getValue(status, 'i32') === 0 && ret) { 912 return Pointer_stringify(ret); 913 } 914 // otherwise, libcxxabi failed 915 } catch(e) { 916 // ignore problems here 917 } finally { 918 if (buf) _free(buf); 919 if (status) _free(status); 920 if (ret) _free(ret); 921 } 922 // failure when using libcxxabi, don't demangle 923 return func; 924 } 925 Runtime.warnOnce('warning: build with -s DEMANGLE_SUPPORT=1 to link in libcxxabi demangling'); 926 return func; 927 } 928 929 function demangleAll(text) { 930 var regex = 931 /__Z[\w\d_]+/g; 932 return text.replace(regex, 933 function(x) { 934 var y = demangle(x); 935 return x === y ? x : (x + ' [' + y + ']'); 936 }); 937 } 938 939 function jsStackTrace() { 940 var err = new Error(); 941 if (!err.stack) { 942 // IE10+ special cases: It does have callstack info, but it is only populated if an Error object is thrown, 943 // so try that as a special-case. 944 try { 945 throw new Error(0); 946 } catch(e) { 947 err = e; 948 } 949 if (!err.stack) { 950 return '(no stack trace available)'; 951 } 952 } 953 return err.stack.toString(); 954 } 955 956 function stackTrace() { 957 var js = jsStackTrace(); 958 if (Module['extraStackTrace']) js += '\n' + Module['extraStackTrace'](); 959 return demangleAll(js); 960 } 961 Module["stackTrace"] = stackTrace; 962 963 // Memory management 964 965 var PAGE_SIZE = 16384; 966 var WASM_PAGE_SIZE = 65536; 967 var ASMJS_PAGE_SIZE = 16777216; 968 var MIN_TOTAL_MEMORY = 16777216; 969 970 function alignUp(x, multiple) { 971 if (x % multiple > 0) { 972 x += multiple - (x % multiple); 973 } 974 return x; 975 } 976 977 var HEAP; 978 var buffer; 979 var HEAP8, HEAPU8, HEAP16, HEAPU16, HEAP32, HEAPU32, HEAPF32, HEAPF64; 980 981 function updateGlobalBuffer(buf) { 982 Module['buffer'] = buffer = buf; 983 } 984 985 function updateGlobalBufferViews() { 986 Module['HEAP8'] = HEAP8 = new Int8Array(buffer); 987 Module['HEAP16'] = HEAP16 = new Int16Array(buffer); 988 Module['HEAP32'] = HEAP32 = new Int32Array(buffer); 989 Module['HEAPU8'] = HEAPU8 = new Uint8Array(buffer); 990 Module['HEAPU16'] = HEAPU16 = new Uint16Array(buffer); 991 Module['HEAPU32'] = HEAPU32 = new Uint32Array(buffer); 992 Module['HEAPF32'] = HEAPF32 = new Float32Array(buffer); 993 Module['HEAPF64'] = HEAPF64 = new Float64Array(buffer); 994 } 995 996 var STATIC_BASE, STATICTOP, staticSealed; // static area 997 var STACK_BASE, STACKTOP, STACK_MAX; // stack area 998 var DYNAMIC_BASE, DYNAMICTOP_PTR; // dynamic area handled by sbrk 999 1000 STATIC_BASE = STATICTOP = STACK_BASE = STACKTOP = STACK_MAX = DYNAMIC_BASE = DYNAMICTOP_PTR = 0; 1001 staticSealed = false; 1002 1003 1004 1005 function abortOnCannotGrowMemory() { 1006 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 -s ALLOW_MEMORY_GROWTH=1 which adjusts the size at runtime but prevents some optimizations, (3) set Module.TOTAL_MEMORY to a higher value before the program runs, or if you want malloc to return NULL (0) instead of this abort, compile with -s ABORTING_MALLOC=0 '); 1007 } 1008 1009 1010 function enlargeMemory() { 1011 abortOnCannotGrowMemory(); 1012 } 1013 1014 1015 var TOTAL_STACK = Module['TOTAL_STACK'] || 5242880; 1016 var TOTAL_MEMORY = Module['TOTAL_MEMORY'] || 16777216; 1017 if (TOTAL_MEMORY < TOTAL_STACK) Module.printErr('TOTAL_MEMORY should be larger than TOTAL_STACK, was ' + TOTAL_MEMORY + '! (TOTAL_STACK=' + TOTAL_STACK + ')'); 1018 1019 // Initialize the runtime's memory 1020 1021 1022 1023 // Use a provided buffer, if there is one, or else allocate a new one 1024 if (Module['buffer']) { 1025 buffer = Module['buffer']; 1026 } else { 1027 // Use a WebAssembly memory where available 1028 if (typeof WebAssembly === 'object' && typeof WebAssembly.Memory === 'function') { 1029 Module['wasmMemory'] = new WebAssembly.Memory({ initial: TOTAL_MEMORY / WASM_PAGE_SIZE, maximum: TOTAL_MEMORY / WASM_PAGE_SIZE }); 1030 buffer = Module['wasmMemory'].buffer; 1031 } else 1032 { 1033 buffer = new ArrayBuffer(TOTAL_MEMORY); 1034 } 1035 } 1036 updateGlobalBufferViews(); 1037 1038 1039 function getTotalMemory() { 1040 return TOTAL_MEMORY; 1041 } 1042 1043 // Endianness check (note: assumes compiler arch was little-endian) 1044 HEAP32[0] = 0x63736d65; /* 'emsc' */ 1045 HEAP16[1] = 0x6373; 1046 if (HEAPU8[2] !== 0x73 || HEAPU8[3] !== 0x63) throw 'Runtime error: expected the system to be little-endian!'; 1047 1048 Module['HEAP'] = HEAP; 1049 Module['buffer'] = buffer; 1050 Module['HEAP8'] = HEAP8; 1051 Module['HEAP16'] = HEAP16; 1052 Module['HEAP32'] = HEAP32; 1053 Module['HEAPU8'] = HEAPU8; 1054 Module['HEAPU16'] = HEAPU16; 1055 Module['HEAPU32'] = HEAPU32; 1056 Module['HEAPF32'] = HEAPF32; 1057 Module['HEAPF64'] = HEAPF64; 1058 1059 function callRuntimeCallbacks(callbacks) { 1060 while(callbacks.length > 0) { 1061 var callback = callbacks.shift(); 1062 if (typeof callback == 'function') { 1063 callback(); 1064 continue; 1065 } 1066 var func = callback.func; 1067 if (typeof func === 'number') { 1068 if (callback.arg === undefined) { 1069 Module['dynCall_v'](func); 1070 } else { 1071 Module['dynCall_vi'](func, callback.arg); 1072 } 1073 } else { 1074 func(callback.arg === undefined ? null : callback.arg); 1075 } 1076 } 1077 } 1078 1079 var __ATPRERUN__ = []; // functions called before the runtime is initialized 1080 var __ATINIT__ = []; // functions called during startup 1081 var __ATMAIN__ = []; // functions called when main() is to be run 1082 var __ATEXIT__ = []; // functions called during shutdown 1083 var __ATPOSTRUN__ = []; // functions called after the runtime has exited 1084 1085 var runtimeInitialized = false; 1086 var runtimeExited = false; 1087 1088 1089 function preRun() { 1090 // compatibility - merge in anything from Module['preRun'] at this time 1091 if (Module['preRun']) { 1092 if (typeof Module['preRun'] == 'function') Module['preRun'] = [Module['preRun']]; 1093 while (Module['preRun'].length) { 1094 addOnPreRun(Module['preRun'].shift()); 1095 } 1096 } 1097 callRuntimeCallbacks(__ATPRERUN__); 1098 } 1099 1100 function ensureInitRuntime() { 1101 if (runtimeInitialized) return; 1102 runtimeInitialized = true; 1103 callRuntimeCallbacks(__ATINIT__); 1104 } 1105 1106 function preMain() { 1107 callRuntimeCallbacks(__ATMAIN__); 1108 } 1109 1110 function exitRuntime() { 1111 callRuntimeCallbacks(__ATEXIT__); 1112 runtimeExited = true; 1113 } 1114 1115 function postRun() { 1116 // compatibility - merge in anything from Module['postRun'] at this time 1117 if (Module['postRun']) { 1118 if (typeof Module['postRun'] == 'function') Module['postRun'] = [Module['postRun']]; 1119 while (Module['postRun'].length) { 1120 addOnPostRun(Module['postRun'].shift()); 1121 } 1122 } 1123 callRuntimeCallbacks(__ATPOSTRUN__); 1124 } 1125 1126 function addOnPreRun(cb) { 1127 __ATPRERUN__.unshift(cb); 1128 } 1129 Module["addOnPreRun"] = addOnPreRun; 1130 1131 function addOnInit(cb) { 1132 __ATINIT__.unshift(cb); 1133 } 1134 Module["addOnInit"] = addOnInit; 1135 1136 function addOnPreMain(cb) { 1137 __ATMAIN__.unshift(cb); 1138 } 1139 Module["addOnPreMain"] = addOnPreMain; 1140 1141 function addOnExit(cb) { 1142 __ATEXIT__.unshift(cb); 1143 } 1144 Module["addOnExit"] = addOnExit; 1145 1146 function addOnPostRun(cb) { 1147 __ATPOSTRUN__.unshift(cb); 1148 } 1149 Module["addOnPostRun"] = addOnPostRun; 1150 1151 // Tools 1152 1153 1154 function intArrayFromString(stringy, dontAddNull, length /* optional */) { 1155 var len = length > 0 ? length : lengthBytesUTF8(stringy)+1; 1156 var u8array = new Array(len); 1157 var numBytesWritten = stringToUTF8Array(stringy, u8array, 0, u8array.length); 1158 if (dontAddNull) u8array.length = numBytesWritten; 1159 return u8array; 1160 } 1161 Module["intArrayFromString"] = intArrayFromString; 1162 1163 function intArrayToString(array) { 1164 var ret = []; 1165 for (var i = 0; i < array.length; i++) { 1166 var chr = array[i]; 1167 if (chr > 0xFF) { 1168 chr &= 0xFF; 1169 } 1170 ret.push(String.fromCharCode(chr)); 1171 } 1172 return ret.join(''); 1173 } 1174 Module["intArrayToString"] = intArrayToString; 1175 1176 // Deprecated: This function should not be called because it is unsafe and does not provide 1177 // a maximum length limit of how many bytes it is allowed to write. Prefer calling the 1178 // function stringToUTF8Array() instead, which takes in a maximum length that can be used 1179 // to be secure from out of bounds writes. 1180 function writeStringToMemory(string, buffer, dontAddNull) { 1181 Runtime.warnOnce('writeStringToMemory is deprecated and should not be called! Use stringToUTF8() instead!'); 1182 1183 var lastChar, end; 1184 if (dontAddNull) { 1185 // stringToUTF8Array always appends null. If we don't want to do that, remember the 1186 // character that existed at the location where the null will be placed, and restore 1187 // that after the write (below). 1188 end = buffer + lengthBytesUTF8(string); 1189 lastChar = HEAP8[end]; 1190 } 1191 stringToUTF8(string, buffer, Infinity); 1192 if (dontAddNull) HEAP8[end] = lastChar; // Restore the value under the null character. 1193 } 1194 Module["writeStringToMemory"] = writeStringToMemory; 1195 1196 function writeArrayToMemory(array, buffer) { 1197 HEAP8.set(array, buffer); 1198 } 1199 Module["writeArrayToMemory"] = writeArrayToMemory; 1200 1201 function writeAsciiToMemory(str, buffer, dontAddNull) { 1202 for (var i = 0; i < str.length; ++i) { 1203 HEAP8[((buffer++)>>0)]=str.charCodeAt(i); 1204 } 1205 // Null-terminate the pointer to the HEAP. 1206 if (!dontAddNull) HEAP8[((buffer)>>0)]=0; 1207 } 1208 Module["writeAsciiToMemory"] = writeAsciiToMemory; 1209 1210 function unSign(value, bits, ignore) { 1211 if (value >= 0) { 1212 return value; 1213 } 1214 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 1215 : Math.pow(2, bits) + value; 1216 } 1217 function reSign(value, bits, ignore) { 1218 if (value <= 0) { 1219 return value; 1220 } 1221 var half = bits <= 32 ? Math.abs(1 << (bits-1)) // abs is needed if bits == 32 1222 : Math.pow(2, bits-1); 1223 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 1224 // but, in general there is no perfect solution here. With 64-bit ints, we get rounding and errors 1225 // TODO: In i64 mode 1, resign the two parts separately and safely 1226 value = -2*half + value; // Cannot bitshift half, as it may be at the limit of the bits JS uses in bitshifts 1227 } 1228 return value; 1229 } 1230 1231 1232 // check for imul support, and also for correctness ( https://bugs.webkit.org/show_bug.cgi?id=126345 ) 1233 if (!Math['imul'] || Math['imul'](0xffffffff, 5) !== -5) Math['imul'] = function imul(a, b) { 1234 var ah = a >>> 16; 1235 var al = a & 0xffff; 1236 var bh = b >>> 16; 1237 var bl = b & 0xffff; 1238 return (al*bl + ((ah*bl + al*bh) << 16))|0; 1239 }; 1240 Math.imul = Math['imul']; 1241 1242 if (!Math['fround']) { 1243 var froundBuffer = new Float32Array(1); 1244 Math['fround'] = function(x) { froundBuffer[0] = x; return froundBuffer[0] }; 1245 } 1246 Math.fround = Math['fround']; 1247 1248 if (!Math['clz32']) Math['clz32'] = function(x) { 1249 x = x >>> 0; 1250 for (var i = 0; i < 32; i++) { 1251 if (x & (1 << (31 - i))) return i; 1252 } 1253 return 32; 1254 }; 1255 Math.clz32 = Math['clz32'] 1256 1257 if (!Math['trunc']) Math['trunc'] = function(x) { 1258 return x < 0 ? Math.ceil(x) : Math.floor(x); 1259 }; 1260 Math.trunc = Math['trunc']; 1261 1262 var Math_abs = Math.abs; 1263 var Math_cos = Math.cos; 1264 var Math_sin = Math.sin; 1265 var Math_tan = Math.tan; 1266 var Math_acos = Math.acos; 1267 var Math_asin = Math.asin; 1268 var Math_atan = Math.atan; 1269 var Math_atan2 = Math.atan2; 1270 var Math_exp = Math.exp; 1271 var Math_log = Math.log; 1272 var Math_sqrt = Math.sqrt; 1273 var Math_ceil = Math.ceil; 1274 var Math_floor = Math.floor; 1275 var Math_pow = Math.pow; 1276 var Math_imul = Math.imul; 1277 var Math_fround = Math.fround; 1278 var Math_round = Math.round; 1279 var Math_min = Math.min; 1280 var Math_clz32 = Math.clz32; 1281 var Math_trunc = Math.trunc; 1282 1283 // A counter of dependencies for calling run(). If we need to 1284 // do asynchronous work before running, increment this and 1285 // decrement it. Incrementing must happen in a place like 1286 // PRE_RUN_ADDITIONS (used by emcc to add file preloading). 1287 // Note that you can add dependencies in preRun, even though 1288 // it happens right before run - run will be postponed until 1289 // the dependencies are met. 1290 var runDependencies = 0; 1291 var runDependencyWatcher = null; 1292 var dependenciesFulfilled = null; // overridden to take different actions when all run dependencies are fulfilled 1293 1294 function getUniqueRunDependency(id) { 1295 return id; 1296 } 1297 1298 function addRunDependency(id) { 1299 runDependencies++; 1300 if (Module['monitorRunDependencies']) { 1301 Module['monitorRunDependencies'](runDependencies); 1302 } 1303 } 1304 Module["addRunDependency"] = addRunDependency; 1305 1306 function removeRunDependency(id) { 1307 runDependencies--; 1308 if (Module['monitorRunDependencies']) { 1309 Module['monitorRunDependencies'](runDependencies); 1310 } 1311 if (runDependencies == 0) { 1312 if (runDependencyWatcher !== null) { 1313 clearInterval(runDependencyWatcher); 1314 runDependencyWatcher = null; 1315 } 1316 if (dependenciesFulfilled) { 1317 var callback = dependenciesFulfilled; 1318 dependenciesFulfilled = null; 1319 callback(); // can add another dependenciesFulfilled 1320 } 1321 } 1322 } 1323 Module["removeRunDependency"] = removeRunDependency; 1324 1325 Module["preloadedImages"] = {}; // maps url to image data 1326 Module["preloadedAudios"] = {}; // maps url to audio data 1327 1328 1329 1330 var memoryInitializer = null; 1331 1332 1333 1334 1335 1336 function integrateWasmJS(Module) { 1337 // wasm.js has several methods for creating the compiled code module here: 1338 // * 'native-wasm' : use native WebAssembly support in the browser 1339 // * 'interpret-s-expr': load s-expression code from a .wast and interpret 1340 // * 'interpret-binary': load binary wasm and interpret 1341 // * 'interpret-asm2wasm': load asm.js code, translate to wasm, and interpret 1342 // * 'asmjs': no wasm, just load the asm.js code and use that (good for testing) 1343 // The method can be set at compile time (BINARYEN_METHOD), or runtime by setting Module['wasmJSMethod']. 1344 // The method can be a comma-separated list, in which case, we will try the 1345 // options one by one. Some of them can fail gracefully, and then we can try 1346 // the next. 1347 1348 // inputs 1349 1350 var method = Module['wasmJSMethod'] || 'native-wasm'; 1351 Module['wasmJSMethod'] = method; 1352 1353 var wasmBinaryFile = Module['wasmBinaryFile'] || scriptdir + 'wasm_box2d.wasm'; 1354 1355 // utilities 1356 1357 var wasmPageSize = 64*1024; 1358 1359 var asm2wasmImports = { // special asm2wasm imports 1360 "f64-rem": function(x, y) { 1361 return x % y; 1362 }, 1363 "f64-to-int": function(x) { 1364 return x | 0; 1365 }, 1366 "i32s-div": function(x, y) { 1367 return ((x | 0) / (y | 0)) | 0; 1368 }, 1369 "i32u-div": function(x, y) { 1370 return ((x >>> 0) / (y >>> 0)) >>> 0; 1371 }, 1372 "i32s-rem": function(x, y) { 1373 return ((x | 0) % (y | 0)) | 0; 1374 }, 1375 "i32u-rem": function(x, y) { 1376 return ((x >>> 0) % (y >>> 0)) >>> 0; 1377 }, 1378 "debugger": function() { 1379 debugger; 1380 }, 1381 }; 1382 1383 var info = { 1384 'global': null, 1385 'env': null, 1386 'asm2wasm': asm2wasmImports, 1387 'parent': Module // Module inside wasm-js.cpp refers to wasm-js.cpp; this allows access to the outside program. 1388 }; 1389 1390 var exports = null; 1391 1392 function lookupImport(mod, base) { 1393 var lookup = info; 1394 if (mod.indexOf('.') < 0) { 1395 lookup = (lookup || {})[mod]; 1396 } else { 1397 var parts = mod.split('.'); 1398 lookup = (lookup || {})[parts[0]]; 1399 lookup = (lookup || {})[parts[1]]; 1400 } 1401 if (base) { 1402 lookup = (lookup || {})[base]; 1403 } 1404 if (lookup === undefined) { 1405 abort('bad lookupImport to (' + mod + ').' + base); 1406 } 1407 return lookup; 1408 } 1409 1410 function mergeMemory(newBuffer) { 1411 // The wasm instance creates its memory. But static init code might have written to 1412 // buffer already, including the mem init file, and we must copy it over in a proper merge. 1413 // TODO: avoid this copy, by avoiding such static init writes 1414 // TODO: in shorter term, just copy up to the last static init write 1415 var oldBuffer = Module['buffer']; 1416 if (newBuffer.byteLength < oldBuffer.byteLength) { 1417 Module['printErr']('the new buffer in mergeMemory is smaller than the previous one. in native wasm, we should grow memory here'); 1418 } 1419 var oldView = new Int8Array(oldBuffer); 1420 var newView = new Int8Array(newBuffer); 1421 1422 // If we have a mem init file, do not trample it 1423 if (!memoryInitializer) { 1424 oldView.set(newView.subarray(Module['STATIC_BASE'], Module['STATIC_BASE'] + Module['STATIC_BUMP']), Module['STATIC_BASE']); 1425 } 1426 1427 newView.set(oldView); 1428 updateGlobalBuffer(newBuffer); 1429 updateGlobalBufferViews(); 1430 } 1431 1432 var WasmTypes = { 1433 none: 0, 1434 i32: 1, 1435 i64: 2, 1436 f32: 3, 1437 f64: 4 1438 }; 1439 1440 function fixImports(imports) { 1441 if (!0) return imports; 1442 var ret = {}; 1443 for (var i in imports) { 1444 var fixed = i; 1445 if (fixed[0] == '_') fixed = fixed.substr(1); 1446 ret[fixed] = imports[i]; 1447 } 1448 return ret; 1449 } 1450 1451 // do-method functions 1452 1453 function doJustAsm(global, env, providedBuffer) { 1454 // if no Module.asm, or it's the method handler helper (see below), then apply 1455 // the asmjs 1456 if (typeof Module['asm'] !== 'function' || Module['asm'] === methodHandler) { 1457 if (!Module['asmPreload']) { 1458 // you can load the .asm.js file before this, to avoid this sync xhr and eval 1459 eval(Module['read'](asmjsCodeFile)); // set Module.asm 1460 } else { 1461 Module['asm'] = Module['asmPreload']; 1462 } 1463 } 1464 if (typeof Module['asm'] !== 'function') { 1465 Module['printErr']('asm evalling did not set the module properly'); 1466 return false; 1467 } 1468 return Module['asm'](global, env, providedBuffer); 1469 } 1470 1471 function doNativeWasm(global, env, providedBuffer) { 1472 if (typeof WebAssembly !== 'object') { 1473 Module['printErr']('no native wasm support detected'); 1474 return false; 1475 } 1476 // prepare memory import 1477 if (!(Module['wasmMemory'] instanceof WebAssembly.Memory)) { 1478 Module['printErr']('no native wasm Memory in use'); 1479 return false; 1480 } 1481 env['memory'] = Module['wasmMemory']; 1482 // Load the wasm module and create an instance of using native support in the JS engine. 1483 info['global'] = { 1484 'NaN': NaN, 1485 'Infinity': Infinity 1486 }; 1487 info['global.Math'] = global.Math; 1488 info['env'] = env; 1489 // handle a generated wasm instance, receiving its exports and 1490 // performing other necessary setup 1491 function receiveInstance(instance) { 1492 exports = instance.exports; 1493 if (exports.memory) mergeMemory(exports.memory); 1494 Module['asm'] = exports; 1495 Module["usingWasm"] = true; 1496 } 1497 Module['printErr']('asynchronously preparing wasm'); 1498 addRunDependency('wasm-instantiate'); // we can't run yet 1499 1500 (cacheEntryOrModule instanceof WebAssembly.Module 1501 ? WebAssembly.instantiate(cacheEntryOrModule, info) 1502 .then(instance => ({instance, module:cacheEntryOrModule})) 1503 : wasmStreamingEnabled() 1504 ? WebAssembly.instantiateStreaming(cacheEntryOrModule, info) 1505 : WebAssembly.instantiate(cacheEntryOrModule.getBuffer(), info)) 1506 .then(function(output) { 1507 if (!cacheEntryOrModule.module) 1508 cacheEntryOrModule.module = output.module; 1509 1510 // receiveInstance() will swap in the exports (to Module.asm) so they can be called 1511 receiveInstance(output.instance); 1512 removeRunDependency('wasm-instantiate'); 1513 }) 1514 .catch(function(reason) { 1515 Module['printErr']('failed to asynchronously prepare wasm:\n ' + reason); 1516 }); 1517 return {}; // no exports yet; we'll fill them in later 1518 } 1519 1520 // We may have a preloaded value in Module.asm, save it 1521 Module['asmPreload'] = Module['asm']; 1522 1523 // Memory growth integration code 1524 Module['reallocBuffer'] = function(size) { 1525 var PAGE_MULTIPLE = Module["usingWasm"] ? WASM_PAGE_SIZE : ASMJS_PAGE_SIZE; // In wasm, heap size must be a multiple of 64KB. In asm.js, they need to be multiples of 16MB. 1526 size = alignUp(size, PAGE_MULTIPLE); // round up to wasm page size 1527 var old = Module['buffer']; 1528 var oldSize = old.byteLength; 1529 if (Module["usingWasm"]) { 1530 try { 1531 var result = Module['wasmMemory'].grow((size - oldSize) / wasmPageSize); // .grow() takes a delta compared to the previous size 1532 if (result !== (-1 | 0)) { 1533 // success in native wasm memory growth, get the buffer from the memory 1534 return Module['buffer'] = Module['wasmMemory'].buffer; 1535 } else { 1536 return null; 1537 } 1538 } catch(e) { 1539 return null; 1540 } 1541 } else { 1542 exports['__growWasmMemory']((size - oldSize) / wasmPageSize); // tiny wasm method that just does memory.grow 1543 // in interpreter, we replace Module.buffer if we allocate 1544 return Module['buffer'] !== old ? Module['buffer'] : null; // if it was reallocated, it changed 1545 } 1546 }; 1547 1548 // Provide an "asm.js function" for the application, called to "link" the asm.js module. We instantiate 1549 // the wasm module at that time, and it receives imports and provides exports and so forth, the app 1550 // doesn't need to care that it is wasm or olyfilled wasm or asm.js. 1551 1552 Module['asm'] = function(global, env, providedBuffer) { 1553 global = fixImports(global); 1554 env = fixImports(env); 1555 1556 // import table 1557 if (!env['table']) { 1558 var TABLE_SIZE = Module['wasmTableSize']; 1559 if (TABLE_SIZE === undefined) TABLE_SIZE = 1024; // works in binaryen interpreter at least 1560 var MAX_TABLE_SIZE = Module['wasmMaxTableSize']; 1561 if (typeof WebAssembly === 'object' && typeof WebAssembly.Table === 'function') { 1562 if (MAX_TABLE_SIZE !== undefined) { 1563 env['table'] = new WebAssembly.Table({ initial: TABLE_SIZE, maximum: MAX_TABLE_SIZE, element: 'anyfunc' }); 1564 } else { 1565 env['table'] = new WebAssembly.Table({ initial: TABLE_SIZE, element: 'anyfunc' }); 1566 } 1567 } else { 1568 env['table'] = new Array(TABLE_SIZE); // works in binaryen interpreter at least 1569 } 1570 Module['wasmTable'] = env['table']; 1571 } 1572 1573 if (!env['memoryBase']) { 1574 env['memoryBase'] = Module['STATIC_BASE']; // tell the memory segments where to place themselves 1575 } 1576 if (!env['tableBase']) { 1577 env['tableBase'] = 0; // table starts at 0 by default, in dynamic linking this will change 1578 } 1579 1580 return doNativeWasm(global, env, providedBuffer); 1581 }; 1582 1583 var methodHandler = Module['asm']; // note our method handler, as we may modify Module['asm'] later 1584 } 1585 1586 integrateWasmJS(Module); 1587 1588 // === Body === 1589 1590 var ASM_CONSTS = []; 1591 1592 1593 1594 1595 STATIC_BASE = 1024; 1596 1597 STATICTOP = STATIC_BASE + 11904; 1598 /* global initializers */ __ATINIT__.push(); 1599 1600 1601 memoryInitializer = Module["wasmJSMethod"].indexOf("asmjs") >= 0 || Module["wasmJSMethod"].indexOf("interpret-asm2wasm") >= 0 ? "wasm_box2d.js.mem" : null; 1602 1603 1604 1605 1606 var STATIC_BUMP = 11904; 1607 Module["STATIC_BASE"] = STATIC_BASE; 1608 Module["STATIC_BUMP"] = STATIC_BUMP; 1609 1610 /* no memory initializer */ 1611 var tempDoublePtr = STATICTOP; STATICTOP += 16; 1612 1613 function copyTempFloat(ptr) { // functions, because inlining this code increases code size too much 1614 1615 HEAP8[tempDoublePtr] = HEAP8[ptr]; 1616 1617 HEAP8[tempDoublePtr+1] = HEAP8[ptr+1]; 1618 1619 HEAP8[tempDoublePtr+2] = HEAP8[ptr+2]; 1620 1621 HEAP8[tempDoublePtr+3] = HEAP8[ptr+3]; 1622 1623 } 1624 1625 function copyTempDouble(ptr) { 1626 1627 HEAP8[tempDoublePtr] = HEAP8[ptr]; 1628 1629 HEAP8[tempDoublePtr+1] = HEAP8[ptr+1]; 1630 1631 HEAP8[tempDoublePtr+2] = HEAP8[ptr+2]; 1632 1633 HEAP8[tempDoublePtr+3] = HEAP8[ptr+3]; 1634 1635 HEAP8[tempDoublePtr+4] = HEAP8[ptr+4]; 1636 1637 HEAP8[tempDoublePtr+5] = HEAP8[ptr+5]; 1638 1639 HEAP8[tempDoublePtr+6] = HEAP8[ptr+6]; 1640 1641 HEAP8[tempDoublePtr+7] = HEAP8[ptr+7]; 1642 1643 } 1644 1645 // {{PRE_LIBRARY}} 1646 1647 1648 function ___cxa_pure_virtual() { 1649 ABORT = true; 1650 throw 'Pure virtual function called!'; 1651 } 1652 1653 function ___assert_fail(condition, filename, line, func) { 1654 ABORT = true; 1655 throw 'Assertion failed: ' + Pointer_stringify(condition) + ', at: ' + [filename ? Pointer_stringify(filename) : 'unknown filename', line, func ? Pointer_stringify(func) : 'unknown function'] + ' at ' + stackTrace(); 1656 } 1657 1658 1659 1660 var Browser={mainLoop:{scheduler:null,method:"",currentlyRunningMainloop:0,func:null,arg:0,timingMode:0,timingValue:0,currentFrameNumber:0,queue:[],pause:function () { 1661 Browser.mainLoop.scheduler = null; 1662 Browser.mainLoop.currentlyRunningMainloop++; // Incrementing this signals the previous main loop that it's now become old, and it must return. 1663 },resume:function () { 1664 Browser.mainLoop.currentlyRunningMainloop++; 1665 var timingMode = Browser.mainLoop.timingMode; 1666 var timingValue = Browser.mainLoop.timingValue; 1667 var func = Browser.mainLoop.func; 1668 Browser.mainLoop.func = null; 1669 _emscripten_set_main_loop(func, 0, false, Browser.mainLoop.arg, true /* do not set timing and call scheduler, we will do it on the next lines */); 1670 _emscripten_set_main_loop_timing(timingMode, timingValue); 1671 Browser.mainLoop.scheduler(); 1672 },updateStatus:function () { 1673 if (Module['setStatus']) { 1674 var message = Module['statusMessage'] || 'Please wait...'; 1675 var remaining = Browser.mainLoop.remainingBlockers; 1676 var expected = Browser.mainLoop.expectedBlockers; 1677 if (remaining) { 1678 if (remaining < expected) { 1679 Module['setStatus'](message + ' (' + (expected - remaining) + '/' + expected + ')'); 1680 } else { 1681 Module['setStatus'](message); 1682 } 1683 } else { 1684 Module['setStatus'](''); 1685 } 1686 } 1687 },runIter:function (func) { 1688 if (ABORT) return; 1689 if (Module['preMainLoop']) { 1690 var preRet = Module['preMainLoop'](); 1691 if (preRet === false) { 1692 return; // |return false| skips a frame 1693 } 1694 } 1695 try { 1696 func(); 1697 } catch (e) { 1698 if (e instanceof ExitStatus) { 1699 return; 1700 } else { 1701 if (e && typeof e === 'object' && e.stack) Module.printErr('exception thrown: ' + [e, e.stack]); 1702 throw e; 1703 } 1704 } 1705 if (Module['postMainLoop']) Module['postMainLoop'](); 1706 }},isFullscreen:false,pointerLock:false,moduleContextCreatedCallbacks:[],workers:[],init:function () { 1707 if (!Module["preloadPlugins"]) Module["preloadPlugins"] = []; // needs to exist even in workers 1708 1709 if (Browser.initted) return; 1710 Browser.initted = true; 1711 1712 try { 1713 new Blob(); 1714 Browser.hasBlobConstructor = true; 1715 } catch(e) { 1716 Browser.hasBlobConstructor = false; 1717 console.log("warning: no blob constructor, cannot create blobs with mimetypes"); 1718 } 1719 Browser.BlobBuilder = typeof MozBlobBuilder != "undefined" ? MozBlobBuilder : (typeof WebKitBlobBuilder != "undefined" ? WebKitBlobBuilder : (!Browser.hasBlobConstructor ? console.log("warning: no BlobBuilder") : null)); 1720 Browser.URLObject = typeof window != "undefined" ? (window.URL ? window.URL : window.webkitURL) : undefined; 1721 if (!Module.noImageDecoding && typeof Browser.URLObject === 'undefined') { 1722 console.log("warning: Browser does not support creating object URLs. Built-in browser image decoding will not be available."); 1723 Module.noImageDecoding = true; 1724 } 1725 1726 // Support for plugins that can process preloaded files. You can add more of these to 1727 // your app by creating and appending to Module.preloadPlugins. 1728 // 1729 // Each plugin is asked if it can handle a file based on the file's name. If it can, 1730 // it is given the file's raw data. When it is done, it calls a callback with the file's 1731 // (possibly modified) data. For example, a plugin might decompress a file, or it 1732 // might create some side data structure for use later (like an Image element, etc.). 1733 1734 var imagePlugin = {}; 1735 imagePlugin['canHandle'] = function imagePlugin_canHandle(name) { 1736 return !Module.noImageDecoding && /\.(jpg|jpeg|png|bmp)$/i.test(name); 1737 }; 1738 imagePlugin['handle'] = function imagePlugin_handle(byteArray, name, onload, onerror) { 1739 var b = null; 1740 if (Browser.hasBlobConstructor) { 1741 try { 1742 b = new Blob([byteArray], { type: Browser.getMimetype(name) }); 1743 if (b.size !== byteArray.length) { // Safari bug #118630 1744 // Safari's Blob can only take an ArrayBuffer 1745 b = new Blob([(new Uint8Array(byteArray)).buffer], { type: Browser.getMimetype(name) }); 1746 } 1747 } catch(e) { 1748 Runtime.warnOnce('Blob constructor present but fails: ' + e + '; falling back to blob builder'); 1749 } 1750 } 1751 if (!b) { 1752 var bb = new Browser.BlobBuilder(); 1753 bb.append((new Uint8Array(byteArray)).buffer); // we need to pass a buffer, and must copy the array to get the right data range 1754 b = bb.getBlob(); 1755 } 1756 var url = Browser.URLObject.createObjectURL(b); 1757 var img = new Image(); 1758 img.onload = function img_onload() { 1759 assert(img.complete, 'Image ' + name + ' could not be decoded'); 1760 var canvas = document.createElement('canvas'); 1761 canvas.width = img.width; 1762 canvas.height = img.height; 1763 var ctx = canvas.getContext('2d'); 1764 ctx.drawImage(img, 0, 0); 1765 Module["preloadedImages"][name] = canvas; 1766 Browser.URLObject.revokeObjectURL(url); 1767 if (onload) onload(byteArray); 1768 }; 1769 img.onerror = function img_onerror(event) { 1770 console.log('Image ' + url + ' could not be decoded'); 1771 if (onerror) onerror(); 1772 }; 1773 img.src = url; 1774 }; 1775 Module['preloadPlugins'].push(imagePlugin); 1776 1777 var audioPlugin = {}; 1778 audioPlugin['canHandle'] = function audioPlugin_canHandle(name) { 1779 return !Module.noAudioDecoding && name.substr(-4) in { '.ogg': 1, '.wav': 1, '.mp3': 1 }; 1780 }; 1781 audioPlugin['handle'] = function audioPlugin_handle(byteArray, name, onload, onerror) { 1782 var done = false; 1783 function finish(audio) { 1784 if (done) return; 1785 done = true; 1786 Module["preloadedAudios"][name] = audio; 1787 if (onload) onload(byteArray); 1788 } 1789 function fail() { 1790 if (done) return; 1791 done = true; 1792 Module["preloadedAudios"][name] = new Audio(); // empty shim 1793 if (onerror) onerror(); 1794 } 1795 if (Browser.hasBlobConstructor) { 1796 try { 1797 var b = new Blob([byteArray], { type: Browser.getMimetype(name) }); 1798 } catch(e) { 1799 return fail(); 1800 } 1801 var url = Browser.URLObject.createObjectURL(b); // XXX we never revoke this! 1802 var audio = new Audio(); 1803 audio.addEventListener('canplaythrough', function() { finish(audio) }, false); // use addEventListener due to chromium bug 124926 1804 audio.onerror = function audio_onerror(event) { 1805 if (done) return; 1806 console.log('warning: browser could not fully decode audio ' + name + ', trying slower base64 approach'); 1807 function encode64(data) { 1808 var BASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; 1809 var PAD = '='; 1810 var ret = ''; 1811 var leftchar = 0; 1812 var leftbits = 0; 1813 for (var i = 0; i < data.length; i++) { 1814 leftchar = (leftchar << 8) | data[i]; 1815 leftbits += 8; 1816 while (leftbits >= 6) { 1817 var curr = (leftchar >> (leftbits-6)) & 0x3f; 1818 leftbits -= 6; 1819 ret += BASE[curr]; 1820 } 1821 } 1822 if (leftbits == 2) { 1823 ret += BASE[(leftchar&3) << 4]; 1824 ret += PAD + PAD; 1825 } else if (leftbits == 4) { 1826 ret += BASE[(leftchar&0xf) << 2]; 1827 ret += PAD; 1828 } 1829 return ret; 1830 } 1831 audio.src = 'data:audio/x-' + name.substr(-3) + ';base64,' + encode64(byteArray); 1832 finish(audio); // we don't wait for confirmation this worked - but it's worth trying 1833 }; 1834 audio.src = url; 1835 // workaround for chrome bug 124926 - we do not always get oncanplaythrough or onerror 1836 Browser.safeSetTimeout(function() { 1837 finish(audio); // try to use it even though it is not necessarily ready to play 1838 }, 10000); 1839 } else { 1840 return fail(); 1841 } 1842 }; 1843 Module['preloadPlugins'].push(audioPlugin); 1844 1845 // Canvas event setup 1846 1847 function pointerLockChange() { 1848 Browser.pointerLock = document['pointerLockElement'] === Module['canvas'] || 1849 document['mozPointerLockElement'] === Module['canvas'] || 1850 document['webkitPointerLockElement'] === Module['canvas'] || 1851 document['msPointerLockElement'] === Module['canvas']; 1852 } 1853 var canvas = Module['canvas']; 1854 if (canvas) { 1855 // forced aspect ratio can be enabled by defining 'forcedAspectRatio' on Module 1856 // Module['forcedAspectRatio'] = 4 / 3; 1857 1858 canvas.requestPointerLock = canvas['requestPointerLock'] || 1859 canvas['mozRequestPointerLock'] || 1860 canvas['webkitRequestPointerLock'] || 1861 canvas['msRequestPointerLock'] || 1862 function(){}; 1863 canvas.exitPointerLock = document['exitPointerLock'] || 1864 document['mozExitPointerLock'] || 1865 document['webkitExitPointerLock'] || 1866 document['msExitPointerLock'] || 1867 function(){}; // no-op if function does not exist 1868 canvas.exitPointerLock = canvas.exitPointerLock.bind(document); 1869 1870 document.addEventListener('pointerlockchange', pointerLockChange, false); 1871 document.addEventListener('mozpointerlockchange', pointerLockChange, false); 1872 document.addEventListener('webkitpointerlockchange', pointerLockChange, false); 1873 document.addEventListener('mspointerlockchange', pointerLockChange, false); 1874 1875 if (Module['elementPointerLock']) { 1876 canvas.addEventListener("click", function(ev) { 1877 if (!Browser.pointerLock && Module['canvas'].requestPointerLock) { 1878 Module['canvas'].requestPointerLock(); 1879 ev.preventDefault(); 1880 } 1881 }, false); 1882 } 1883 } 1884 },createContext:function (canvas, useWebGL, setInModule, webGLContextAttributes) { 1885 if (useWebGL && Module.ctx && canvas == Module.canvas) return Module.ctx; // no need to recreate GL context if it's already been created for this canvas. 1886 1887 var ctx; 1888 var contextHandle; 1889 if (useWebGL) { 1890 // For GLES2/desktop GL compatibility, adjust a few defaults to be different to WebGL defaults, so that they align better with the desktop defaults. 1891 var contextAttributes = { 1892 antialias: false, 1893 alpha: false 1894 }; 1895 1896 if (webGLContextAttributes) { 1897 for (var attribute in webGLContextAttributes) { 1898 contextAttributes[attribute] = webGLContextAttributes[attribute]; 1899 } 1900 } 1901 1902 contextHandle = GL.createContext(canvas, contextAttributes); 1903 if (contextHandle) { 1904 ctx = GL.getContext(contextHandle).GLctx; 1905 } 1906 } else { 1907 ctx = canvas.getContext('2d'); 1908 } 1909 1910 if (!ctx) return null; 1911 1912 if (setInModule) { 1913 if (!useWebGL) assert(typeof GLctx === 'undefined', 'cannot set in module if GLctx is used, but we are a non-GL context that would replace it'); 1914 1915 Module.ctx = ctx; 1916 if (useWebGL) GL.makeContextCurrent(contextHandle); 1917 Module.useWebGL = useWebGL; 1918 Browser.moduleContextCreatedCallbacks.forEach(function(callback) { callback() }); 1919 Browser.init(); 1920 } 1921 return ctx; 1922 },destroyContext:function (canvas, useWebGL, setInModule) {},fullscreenHandlersInstalled:false,lockPointer:undefined,resizeCanvas:undefined,requestFullscreen:function (lockPointer, resizeCanvas, vrDevice) { 1923 Browser.lockPointer = lockPointer; 1924 Browser.resizeCanvas = resizeCanvas; 1925 Browser.vrDevice = vrDevice; 1926 if (typeof Browser.lockPointer === 'undefined') Browser.lockPointer = true; 1927 if (typeof Browser.resizeCanvas === 'undefined') Browser.resizeCanvas = false; 1928 if (typeof Browser.vrDevice === 'undefined') Browser.vrDevice = null; 1929 1930 var canvas = Module['canvas']; 1931 function fullscreenChange() { 1932 Browser.isFullscreen = false; 1933 var canvasContainer = canvas.parentNode; 1934 if ((document['fullscreenElement'] || document['mozFullScreenElement'] || 1935 document['msFullscreenElement'] || document['webkitFullscreenElement'] || 1936 document['webkitCurrentFullScreenElement']) === canvasContainer) { 1937 canvas.exitFullscreen = document['exitFullscreen'] || 1938 document['cancelFullScreen'] || 1939 document['mozCancelFullScreen'] || 1940 document['msExitFullscreen'] || 1941 document['webkitCancelFullScreen'] || 1942 function() {}; 1943 canvas.exitFullscreen = canvas.exitFullscreen.bind(document); 1944 if (Browser.lockPointer) canvas.requestPointerLock(); 1945 Browser.isFullscreen = true; 1946 if (Browser.resizeCanvas) Browser.setFullscreenCanvasSize(); 1947 } else { 1948 1949 // remove the full screen specific parent of the canvas again to restore the HTML structure from before going full screen 1950 canvasContainer.parentNode.insertBefore(canvas, canvasContainer); 1951 canvasContainer.parentNode.removeChild(canvasContainer); 1952 1953 if (Browser.resizeCanvas) Browser.setWindowedCanvasSize(); 1954 } 1955 if (Module['onFullScreen']) Module['onFullScreen'](Browser.isFullscreen); 1956 if (Module['onFullscreen']) Module['onFullscreen'](Browser.isFullscreen); 1957 Browser.updateCanvasDimensions(canvas); 1958 } 1959 1960 if (!Browser.fullscreenHandlersInstalled) { 1961 Browser.fullscreenHandlersInstalled = true; 1962 document.addEventListener('fullscreenchange', fullscreenChange, false); 1963 document.addEventListener('mozfullscreenchange', fullscreenChange, false); 1964 document.addEventListener('webkitfullscreenchange', fullscreenChange, false); 1965 document.addEventListener('MSFullscreenChange', fullscreenChange, false); 1966 } 1967 1968 // 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 1969 var canvasContainer = document.createElement("div"); 1970 canvas.parentNode.insertBefore(canvasContainer, canvas); 1971 canvasContainer.appendChild(canvas); 1972 1973 // use parent of canvas as full screen root to allow aspect ratio correction (Firefox stretches the root to screen size) 1974 canvasContainer.requestFullscreen = canvasContainer['requestFullscreen'] || 1975 canvasContainer['mozRequestFullScreen'] || 1976 canvasContainer['msRequestFullscreen'] || 1977 (canvasContainer['webkitRequestFullscreen'] ? function() { canvasContainer['webkitRequestFullscreen'](Element['ALLOW_KEYBOARD_INPUT']) } : null) || 1978 (canvasContainer['webkitRequestFullScreen'] ? function() { canvasContainer['webkitRequestFullScreen'](Element['ALLOW_KEYBOARD_INPUT']) } : null); 1979 1980 if (vrDevice) { 1981 canvasContainer.requestFullscreen({ vrDisplay: vrDevice }); 1982 } else { 1983 canvasContainer.requestFullscreen(); 1984 } 1985 },requestFullScreen:function (lockPointer, resizeCanvas, vrDevice) { 1986 Module.printErr('Browser.requestFullScreen() is deprecated. Please call Browser.requestFullscreen instead.'); 1987 Browser.requestFullScreen = function(lockPointer, resizeCanvas, vrDevice) { 1988 return Browser.requestFullscreen(lockPointer, resizeCanvas, vrDevice); 1989 } 1990 return Browser.requestFullscreen(lockPointer, resizeCanvas, vrDevice); 1991 },nextRAF:0,fakeRequestAnimationFrame:function (func) { 1992 // try to keep 60fps between calls to here 1993 var now = Date.now(); 1994 if (Browser.nextRAF === 0) { 1995 Browser.nextRAF = now + 1000/60; 1996 } else { 1997 while (now + 2 >= Browser.nextRAF) { // fudge a little, to avoid timer jitter causing us to do lots of delay:0 1998 Browser.nextRAF += 1000/60; 1999 } 2000 } 2001 var delay = Math.max(Browser.nextRAF - now, 0); 2002 setTimeout(func, delay); 2003 },requestAnimationFrame:function requestAnimationFrame(func) { 2004 if (typeof window === 'undefined') { // Provide fallback to setTimeout if window is undefined (e.g. in Node.js) 2005 Browser.fakeRequestAnimationFrame(func); 2006 } else { 2007 if (!window.requestAnimationFrame) { 2008 window.requestAnimationFrame = window['requestAnimationFrame'] || 2009 window['mozRequestAnimationFrame'] || 2010 window['webkitRequestAnimationFrame'] || 2011 window['msRequestAnimationFrame'] || 2012 window['oRequestAnimationFrame'] || 2013 Browser.fakeRequestAnimationFrame; 2014 } 2015 window.requestAnimationFrame(func); 2016 } 2017 },safeCallback:function (func) { 2018 return function() { 2019 if (!ABORT) return func.apply(null, arguments); 2020 }; 2021 },allowAsyncCallbacks:true,queuedAsyncCallbacks:[],pauseAsyncCallbacks:function () { 2022 Browser.allowAsyncCallbacks = false; 2023 },resumeAsyncCallbacks:function () { // marks future callbacks as ok to execute, and synchronously runs any remaining ones right now 2024 Browser.allowAsyncCallbacks = true; 2025 if (Browser.queuedAsyncCallbacks.length > 0) { 2026 var callbacks = Browser.queuedAsyncCallbacks; 2027 Browser.queuedAsyncCallbacks = []; 2028 callbacks.forEach(function(func) { 2029 func(); 2030 }); 2031 } 2032 },safeRequestAnimationFrame:function (func) { 2033 return Browser.requestAnimationFrame(function() { 2034 if (ABORT) return; 2035 if (Browser.allowAsyncCallbacks) { 2036 func(); 2037 } else { 2038 Browser.queuedAsyncCallbacks.push(func); 2039 } 2040 }); 2041 },safeSetTimeout:function (func, timeout) { 2042 Module['noExitRuntime'] = true; 2043 return setTimeout(function() { 2044 if (ABORT) return; 2045 if (Browser.allowAsyncCallbacks) { 2046 func(); 2047 } else { 2048 Browser.queuedAsyncCallbacks.push(func); 2049 } 2050 }, timeout); 2051 },safeSetInterval:function (func, timeout) { 2052 Module['noExitRuntime'] = true; 2053 return setInterval(function() { 2054 if (ABORT) return; 2055 if (Browser.allowAsyncCallbacks) { 2056 func(); 2057 } // drop it on the floor otherwise, next interval will kick in 2058 }, timeout); 2059 },getMimetype:function (name) { 2060 return { 2061 'jpg': 'image/jpeg', 2062 'jpeg': 'image/jpeg', 2063 'png': 'image/png', 2064 'bmp': 'image/bmp', 2065 'ogg': 'audio/ogg', 2066 'wav': 'audio/wav', 2067 'mp3': 'audio/mpeg' 2068 }[name.substr(name.lastIndexOf('.')+1)]; 2069 },getUserMedia:function (func) { 2070 if(!window.getUserMedia) { 2071 window.getUserMedia = navigator.mediaDevices.getUserMedia; 2072 } 2073 window.getUserMedia(func); 2074 },getMovementX:function (event) { 2075 return event['movementX'] || 2076 event['mozMovementX'] || 2077 event['webkitMovementX'] || 2078 0; 2079 },getMovementY:function (event) { 2080 return event['movementY'] || 2081 event['mozMovementY'] || 2082 event['webkitMovementY'] || 2083 0; 2084 },getMouseWheelDelta:function (event) { 2085 var delta = 0; 2086 switch (event.type) { 2087 case 'DOMMouseScroll': 2088 delta = event.detail; 2089 break; 2090 case 'mousewheel': 2091 delta = event.wheelDelta; 2092 break; 2093 case 'wheel': 2094 delta = event['deltaY']; 2095 break; 2096 default: 2097 throw 'unrecognized mouse wheel event: ' + event.type; 2098 } 2099 return delta; 2100 },mouseX:0,mouseY:0,mouseMovementX:0,mouseMovementY:0,touches:{},lastTouches:{},calculateMouseEvent:function (event) { // event should be mousemove, mousedown or mouseup 2101 if (Browser.pointerLock) { 2102 // When the pointer is locked, calculate the coordinates 2103 // based on the movement of the mouse. 2104 // Workaround for Firefox bug 764498 2105 if (event.type != 'mousemove' && 2106 ('mozMovementX' in event)) { 2107 Browser.mouseMovementX = Browser.mouseMovementY = 0; 2108 } else { 2109 Browser.mouseMovementX = Browser.getMovementX(event); 2110 Browser.mouseMovementY = Browser.getMovementY(event); 2111 } 2112 2113 // check if SDL is available 2114 if (typeof SDL != "undefined") { 2115 Browser.mouseX = SDL.mouseX + Browser.mouseMovementX; 2116 Browser.mouseY = SDL.mouseY + Browser.mouseMovementY; 2117 } else { 2118 // just add the mouse delta to the current absolut mouse position 2119 // FIXME: ideally this should be clamped against the canvas size and zero 2120 Browser.mouseX += Browser.mouseMovementX; 2121 Browser.mouseY += Browser.mouseMovementY; 2122 } 2123 } else { 2124 // Otherwise, calculate the movement based on the changes 2125 // in the coordinates. 2126 var rect = Module["canvas"].getBoundingClientRect(); 2127 var cw = Module["canvas"].width; 2128 var ch = Module["canvas"].height; 2129 2130 // Neither .scrollX or .pageXOffset are defined in a spec, but 2131 // we prefer .scrollX because it is currently in a spec draft. 2132 // (see: http://www.w3.org/TR/2013/WD-cssom-view-20131217/) 2133 var scrollX = ((typeof window.scrollX !== 'undefined') ? window.scrollX : window.pageXOffset); 2134 var scrollY = ((typeof window.scrollY !== 'undefined') ? window.scrollY : window.pageYOffset); 2135 2136 if (event.type === 'touchstart' || event.type === 'touchend' || event.type === 'touchmove') { 2137 var touch = event.touch; 2138 if (touch === undefined) { 2139 return; // the "touch" property is only defined in SDL 2140 2141 } 2142 var adjustedX = touch.pageX - (scrollX + rect.left); 2143 var adjustedY = touch.pageY - (scrollY + rect.top); 2144 2145 adjustedX = adjustedX * (cw / rect.width); 2146 adjustedY = adjustedY * (ch / rect.height); 2147 2148 var coords = { x: adjustedX, y: adjustedY }; 2149 2150 if (event.type === 'touchstart') { 2151 Browser.lastTouches[touch.identifier] = coords; 2152 Browser.touches[touch.identifier] = coords; 2153 } else if (event.type === 'touchend' || event.type === 'touchmove') { 2154 var last = Browser.touches[touch.identifier]; 2155 if (!last) last = coords; 2156 Browser.lastTouches[touch.identifier] = last; 2157 Browser.touches[touch.identifier] = coords; 2158 } 2159 return; 2160 } 2161 2162 var x = event.pageX - (scrollX + rect.left); 2163 var y = event.pageY - (scrollY + rect.top); 2164 2165 // the canvas might be CSS-scaled compared to its backbuffer; 2166 // SDL-using content will want mouse coordinates in terms 2167 // of backbuffer units. 2168 x = x * (cw / rect.width); 2169 y = y * (ch / rect.height); 2170 2171 Browser.mouseMovementX = x - Browser.mouseX; 2172 Browser.mouseMovementY = y - Browser.mouseY; 2173 Browser.mouseX = x; 2174 Browser.mouseY = y; 2175 } 2176 },asyncLoad:function (url, onload, onerror, noRunDep) { 2177 var dep = !noRunDep ? getUniqueRunDependency('al ' + url) : ''; 2178 Module['readAsync'](url, function(arrayBuffer) { 2179 assert(arrayBuffer, 'Loading data file "' + url + '" failed (no arrayBuffer).'); 2180 onload(new Uint8Array(arrayBuffer)); 2181 if (dep) removeRunDependency(dep); 2182 }, function(event) { 2183 if (onerror) { 2184 onerror(); 2185 } else { 2186 throw 'Loading data file "' + url + '" failed.'; 2187 } 2188 }); 2189 if (dep) addRunDependency(dep); 2190 },resizeListeners:[],updateResizeListeners:function () { 2191 var canvas = Module['canvas']; 2192 Browser.resizeListeners.forEach(function(listener) { 2193 listener(canvas.width, canvas.height); 2194 }); 2195 },setCanvasSize:function (width, height, noUpdates) { 2196 var canvas = Module['canvas']; 2197 Browser.updateCanvasDimensions(canvas, width, height); 2198 if (!noUpdates) Browser.updateResizeListeners(); 2199 },windowedWidth:0,windowedHeight:0,setFullscreenCanvasSize:function () { 2200 // check if SDL is available 2201 if (typeof SDL != "undefined") { 2202 var flags = HEAPU32[((SDL.screen+Runtime.QUANTUM_SIZE*0)>>2)]; 2203 flags = flags | 0x00800000; // set SDL_FULLSCREEN flag 2204 HEAP32[((SDL.screen+Runtime.QUANTUM_SIZE*0)>>2)]=flags 2205 } 2206 Browser.updateResizeListeners(); 2207 },setWindowedCanvasSize:function () { 2208 // check if SDL is available 2209 if (typeof SDL != "undefined") { 2210 var flags = HEAPU32[((SDL.screen+Runtime.QUANTUM_SIZE*0)>>2)]; 2211 flags = flags & ~0x00800000; // clear SDL_FULLSCREEN flag 2212 HEAP32[((SDL.screen+Runtime.QUANTUM_SIZE*0)>>2)]=flags 2213 } 2214 Browser.updateResizeListeners(); 2215 },updateCanvasDimensions:function (canvas, wNative, hNative) { 2216 if (wNative && hNative) { 2217 canvas.widthNative = wNative; 2218 canvas.heightNative = hNative; 2219 } else { 2220 wNative = canvas.widthNative; 2221 hNative = canvas.heightNative; 2222 } 2223 var w = wNative; 2224 var h = hNative; 2225 if (Module['forcedAspectRatio'] && Module['forcedAspectRatio'] > 0) { 2226 if (w/h < Module['forcedAspectRatio']) { 2227 w = Math.round(h * Module['forcedAspectRatio']); 2228 } else { 2229 h = Math.round(w / Module['forcedAspectRatio']); 2230 } 2231 } 2232 if (((document['fullscreenElement'] || document['mozFullScreenElement'] || 2233 document['msFullscreenElement'] || document['webkitFullscreenElement'] || 2234 document['webkitCurrentFullScreenElement']) === canvas.parentNode) && (typeof screen != 'undefined')) { 2235 var factor = Math.min(screen.width / w, screen.height / h); 2236 w = Math.round(w * factor); 2237 h = Math.round(h * factor); 2238 } 2239 if (Browser.resizeCanvas) { 2240 if (canvas.width != w) canvas.width = w; 2241 if (canvas.height != h) canvas.height = h; 2242 if (typeof canvas.style != 'undefined') { 2243 canvas.style.removeProperty( "width"); 2244 canvas.style.removeProperty("height"); 2245 } 2246 } else { 2247 if (canvas.width != wNative) canvas.width = wNative; 2248 if (canvas.height != hNative) canvas.height = hNative; 2249 if (typeof canvas.style != 'undefined') { 2250 if (w != wNative || h != hNative) { 2251 canvas.style.setProperty( "width", w + "px", "important"); 2252 canvas.style.setProperty("height", h + "px", "important"); 2253 } else { 2254 canvas.style.removeProperty( "width"); 2255 canvas.style.removeProperty("height"); 2256 } 2257 } 2258 } 2259 },wgetRequests:{},nextWgetRequestHandle:0,getNextWgetRequestHandle:function () { 2260 var handle = Browser.nextWgetRequestHandle; 2261 Browser.nextWgetRequestHandle++; 2262 return handle; 2263 }};function _emscripten_set_main_loop_timing(mode, value) { 2264 Browser.mainLoop.timingMode = mode; 2265 Browser.mainLoop.timingValue = value; 2266 2267 if (!Browser.mainLoop.func) { 2268 return 1; // Return non-zero on failure, can't set timing mode when there is no main loop. 2269 } 2270 2271 if (mode == 0 /*EM_TIMING_SETTIMEOUT*/) { 2272 Browser.mainLoop.scheduler = function Browser_mainLoop_scheduler_setTimeout() { 2273 var timeUntilNextTick = Math.max(0, Browser.mainLoop.tickStartTime + value - _emscripten_get_now())|0; 2274 setTimeout(Browser.mainLoop.runner, timeUntilNextTick); // doing this each time means that on exception, we stop 2275 }; 2276 Browser.mainLoop.method = 'timeout'; 2277 } else if (mode == 1 /*EM_TIMING_RAF*/) { 2278 Browser.mainLoop.scheduler = function Browser_mainLoop_scheduler_rAF() { 2279 Browser.requestAnimationFrame(Browser.mainLoop.runner); 2280 }; 2281 Browser.mainLoop.method = 'rAF'; 2282 } else if (mode == 2 /*EM_TIMING_SETIMMEDIATE*/) { 2283 if (!window['setImmediate']) { 2284 // Emulate setImmediate. (note: not a complete polyfill, we don't emulate clearImmediate() to keep code size to minimum, since not needed) 2285 var setImmediates = []; 2286 var emscriptenMainLoopMessageId = 'setimmediate'; 2287 function Browser_setImmediate_messageHandler(event) { 2288 if (event.source === window && event.data === emscriptenMainLoopMessageId) { 2289 event.stopPropagation(); 2290 setImmediates.shift()(); 2291 } 2292 } 2293 window.addEventListener("message", Browser_setImmediate_messageHandler, true); 2294 window['setImmediate'] = function Browser_emulated_setImmediate(func) { 2295 setImmediates.push(func); 2296 } 2297 } 2298 Browser.mainLoop.scheduler = function Browser_mainLoop_scheduler_setImmediate() { 2299 window['setImmediate'](Browser.mainLoop.runner); 2300 }; 2301 Browser.mainLoop.method = 'immediate'; 2302 } 2303 return 0; 2304 } 2305 2306 function _emscripten_get_now() { abort() }function _emscripten_set_main_loop(func, fps, simulateInfiniteLoop, arg, noSetTiming) { 2307 Module['noExitRuntime'] = true; 2308 2309 assert(!Browser.mainLoop.func, 'emscripten_set_main_loop: there can only be one main loop function at once: call emscripten_cancel_main_loop to cancel the previous one before setting a new one with different parameters.'); 2310 2311 Browser.mainLoop.func = func; 2312 Browser.mainLoop.arg = arg; 2313 2314 var browserIterationFunc; 2315 if (typeof arg !== 'undefined') { 2316 browserIterationFunc = function() { 2317 Module['dynCall_vi'](func, arg); 2318 }; 2319 } else { 2320 browserIterationFunc = function() { 2321 Module['dynCall_v'](func); 2322 }; 2323 } 2324 2325 var thisMainLoopId = Browser.mainLoop.currentlyRunningMainloop; 2326 2327 Browser.mainLoop.runner = function Browser_mainLoop_runner() { 2328 if (ABORT) return; 2329 if (Browser.mainLoop.queue.length > 0) { 2330 var start = Date.now(); 2331 var blocker = Browser.mainLoop.queue.shift(); 2332 blocker.func(blocker.arg); 2333 if (Browser.mainLoop.remainingBlockers) { 2334 var remaining = Browser.mainLoop.remainingBlockers; 2335 var next = remaining%1 == 0 ? remaining-1 : Math.floor(remaining); 2336 if (blocker.counted) { 2337 Browser.mainLoop.remainingBlockers = next; 2338 } else { 2339 // not counted, but move the progress along a tiny bit 2340 next = next + 0.5; // do not steal all the next one's progress 2341 Browser.mainLoop.remainingBlockers = (8*remaining + next)/9; 2342 } 2343 } 2344 console.log('main loop blocker "' + blocker.name + '" took ' + (Date.now() - start) + ' ms'); //, left: ' + Browser.mainLoop.remainingBlockers); 2345 Browser.mainLoop.updateStatus(); 2346 2347 // catches pause/resume main loop from blocker execution 2348 if (thisMainLoopId < Browser.mainLoop.currentlyRunningMainloop) return; 2349 2350 setTimeout(Browser.mainLoop.runner, 0); 2351 return; 2352 } 2353 2354 // catch pauses from non-main loop sources 2355 if (thisMainLoopId < Browser.mainLoop.currentlyRunningMainloop) return; 2356 2357 // Implement very basic swap interval control 2358 Browser.mainLoop.currentFrameNumber = Browser.mainLoop.currentFrameNumber + 1 | 0; 2359 if (Browser.mainLoop.timingMode == 1/*EM_TIMING_RAF*/ && Browser.mainLoop.timingValue > 1 && Browser.mainLoop.currentFrameNumber % Browser.mainLoop.timingValue != 0) { 2360 // Not the scheduled time to render this frame - skip. 2361 Browser.mainLoop.scheduler(); 2362 return; 2363 } else if (Browser.mainLoop.timingMode == 0/*EM_TIMING_SETTIMEOUT*/) { 2364 Browser.mainLoop.tickStartTime = _emscripten_get_now(); 2365 } 2366 2367 // Signal GL rendering layer that processing of a new frame is about to start. This helps it optimize 2368 // VBO double-buffering and reduce GPU stalls. 2369 2370 2371 if (Browser.mainLoop.method === 'timeout' && Module.ctx) { 2372 Module.printErr('Looks like you are rendering without using requestAnimationFrame for the main loop. You should use 0 for the frame rate in emscripten_set_main_loop in order to use requestAnimationFrame, as that can greatly improve your frame rates!'); 2373 Browser.mainLoop.method = ''; // just warn once per call to set main loop 2374 } 2375 2376 Browser.mainLoop.runIter(browserIterationFunc); 2377 2378 2379 // catch pauses from the main loop itself 2380 if (thisMainLoopId < Browser.mainLoop.currentlyRunningMainloop) return; 2381 2382 // Queue new audio data. This is important to be right after the main loop invocation, so that we will immediately be able 2383 // to queue the newest produced audio samples. 2384 // TODO: Consider adding pre- and post- rAF callbacks so that GL.newRenderingFrameStarted() and SDL.audio.queueNewAudioData() 2385 // do not need to be hardcoded into this function, but can be more generic. 2386 if (typeof SDL === 'object' && SDL.audio && SDL.audio.queueNewAudioData) SDL.audio.queueNewAudioData(); 2387 2388 Browser.mainLoop.scheduler(); 2389 } 2390 2391 if (!noSetTiming) { 2392 if (fps && fps > 0) _emscripten_set_main_loop_timing(0/*EM_TIMING_SETTIMEOUT*/, 1000.0 / fps); 2393 else _emscripten_set_main_loop_timing(1/*EM_TIMING_RAF*/, 1); // Do rAF by rendering each frame (no decimating) 2394 2395 Browser.mainLoop.scheduler(); 2396 } 2397 2398 if (simulateInfiniteLoop) { 2399 throw 'SimulateInfiniteLoop'; 2400 } 2401 } 2402 2403 2404 Module["_memset"] = _memset; 2405 2406 function _pthread_cleanup_push(routine, arg) { 2407 __ATEXIT__.push(function() { Module['dynCall_vi'](routine, arg) }) 2408 _pthread_cleanup_push.level = __ATEXIT__.length; 2409 } 2410 2411 function _pthread_cleanup_pop() { 2412 assert(_pthread_cleanup_push.level == __ATEXIT__.length, 'cannot pop if something else added meanwhile!'); 2413 __ATEXIT__.pop(); 2414 _pthread_cleanup_push.level = __ATEXIT__.length; 2415 } 2416 2417 function _abort() { 2418 Module['abort'](); 2419 } 2420 2421 2422 function __ZSt18uncaught_exceptionv() { // std::uncaught_exception() 2423 return !!__ZSt18uncaught_exceptionv.uncaught_exception; 2424 } 2425 2426 var EXCEPTIONS={last:0,caught:[],infos:{},deAdjust:function (adjusted) { 2427 if (!adjusted || EXCEPTIONS.infos[adjusted]) return adjusted; 2428 for (var ptr in EXCEPTIONS.infos) { 2429 var info = EXCEPTIONS.infos[ptr]; 2430 if (info.adjusted === adjusted) { 2431 return ptr; 2432 } 2433 } 2434 return adjusted; 2435 },addRef:function (ptr) { 2436 if (!ptr) return; 2437 var info = EXCEPTIONS.infos[ptr]; 2438 info.refcount++; 2439 },decRef:function (ptr) { 2440 if (!ptr) return; 2441 var info = EXCEPTIONS.infos[ptr]; 2442 assert(info.refcount > 0); 2443 info.refcount--; 2444 // A rethrown exception can reach refcount 0; it must not be discarded 2445 // Its next handler will clear the rethrown flag and addRef it, prior to 2446 // final decRef and destruction here 2447 if (info.refcount === 0 && !info.rethrown) { 2448 if (info.destructor) { 2449 Module['dynCall_vi'](info.destructor, ptr); 2450 } 2451 delete EXCEPTIONS.infos[ptr]; 2452 ___cxa_free_exception(ptr); 2453 } 2454 },clearRef:function (ptr) { 2455 if (!ptr) return; 2456 var info = EXCEPTIONS.infos[ptr]; 2457 info.refcount = 0; 2458 }};function ___cxa_begin_catch(ptr) { 2459 var info = EXCEPTIONS.infos[ptr]; 2460 if (info && !info.caught) { 2461 info.caught = true; 2462 __ZSt18uncaught_exceptionv.uncaught_exception--; 2463 } 2464 if (info) info.rethrown = false; 2465 EXCEPTIONS.caught.push(ptr); 2466 EXCEPTIONS.addRef(EXCEPTIONS.deAdjust(ptr)); 2467 return ptr; 2468 } 2469 2470 function _pthread_once(ptr, func) { 2471 if (!_pthread_once.seen) _pthread_once.seen = {}; 2472 if (ptr in _pthread_once.seen) return; 2473 Module['dynCall_v'](func); 2474 _pthread_once.seen[ptr] = 1; 2475 } 2476 2477 function ___lock() {} 2478 2479 function ___unlock() {} 2480 2481 2482 var SYSCALLS={varargs:0,get:function (varargs) { 2483 SYSCALLS.varargs += 4; 2484 var ret = HEAP32[(((SYSCALLS.varargs)-(4))>>2)]; 2485 return ret; 2486 },getStr:function () { 2487 var ret = Pointer_stringify(SYSCALLS.get()); 2488 return ret; 2489 },get64:function () { 2490 var low = SYSCALLS.get(), high = SYSCALLS.get(); 2491 if (low >= 0) assert(high === 0); 2492 else assert(high === -1); 2493 return low; 2494 },getZero:function () { 2495 assert(SYSCALLS.get() === 0); 2496 }};function ___syscall6(which, varargs) {SYSCALLS.varargs = varargs; 2497 try { 2498 // close 2499 var stream = SYSCALLS.getStreamFromFD(); 2500 FS.close(stream); 2501 return 0; 2502 } catch (e) { 2503 if (typeof FS === 'undefined' || !(e instanceof FS.ErrnoError)) abort(e); 2504 return -e.errno; 2505 } 2506 } 2507 2508 2509 var PTHREAD_SPECIFIC={};function _pthread_getspecific(key) { 2510 return PTHREAD_SPECIFIC[key] || 0; 2511 } 2512 2513 2514 function ___setErrNo(value) { 2515 if (Module['___errno_location']) HEAP32[((Module['___errno_location']())>>2)]=value; 2516 return value; 2517 } 2518 Module["_sbrk"] = _sbrk; 2519 2520 function _clock() { 2521 if (_clock.start === undefined) _clock.start = Date.now(); 2522 return ((Date.now() - _clock.start) * (1000000 / 1000))|0; 2523 } 2524 2525 2526 var PTHREAD_SPECIFIC_NEXT_KEY=1; 2527 2528 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};function _pthread_key_create(key, destructor) { 2529 if (key == 0) { 2530 return ERRNO_CODES.EINVAL; 2531 } 2532 HEAP32[((key)>>2)]=PTHREAD_SPECIFIC_NEXT_KEY; 2533 // values start at 0 2534 PTHREAD_SPECIFIC[PTHREAD_SPECIFIC_NEXT_KEY] = 0; 2535 PTHREAD_SPECIFIC_NEXT_KEY++; 2536 return 0; 2537 } 2538 2539 2540 2541 function ___resumeException(ptr) { 2542 if (!EXCEPTIONS.last) { EXCEPTIONS.last = ptr; } 2543 throw ptr + " - Exception catching is disabled, this exception cannot be caught. Compile with -s DISABLE_EXCEPTION_CATCHING=0 or DISABLE_EXCEPTION_CATCHING=2 to catch."; 2544 }function ___cxa_find_matching_catch() { 2545 var thrown = EXCEPTIONS.last; 2546 if (!thrown) { 2547 // just pass through the null ptr 2548 return ((Runtime.setTempRet0(0),0)|0); 2549 } 2550 var info = EXCEPTIONS.infos[thrown]; 2551 var throwntype = info.type; 2552 if (!throwntype) { 2553 // just pass through the thrown ptr 2554 return ((Runtime.setTempRet0(0),thrown)|0); 2555 } 2556 var typeArray = Array.prototype.slice.call(arguments); 2557 2558 var pointer = Module['___cxa_is_pointer_type'](throwntype); 2559 // can_catch receives a **, add indirection 2560 if (!___cxa_find_matching_catch.buffer) ___cxa_find_matching_catch.buffer = _malloc(4); 2561 HEAP32[((___cxa_find_matching_catch.buffer)>>2)]=thrown; 2562 thrown = ___cxa_find_matching_catch.buffer; 2563 // The different catch blocks are denoted by different types. 2564 // Due to inheritance, those types may not precisely match the 2565 // type of the thrown object. Find one which matches, and 2566 // return the type of the catch block which should be called. 2567 for (var i = 0; i < typeArray.length; i++) { 2568 if (typeArray[i] && Module['___cxa_can_catch'](typeArray[i], throwntype, thrown)) { 2569 thrown = HEAP32[((thrown)>>2)]; // undo indirection 2570 info.adjusted = thrown; 2571 return ((Runtime.setTempRet0(typeArray[i]),thrown)|0); 2572 } 2573 } 2574 // Shouldn't happen unless we have bogus data in typeArray 2575 // or encounter a type for which emscripten doesn't have suitable 2576 // typeinfo defined. Best-efforts match just in case. 2577 thrown = HEAP32[((thrown)>>2)]; // undo indirection 2578 return ((Runtime.setTempRet0(throwntype),thrown)|0); 2579 }function ___gxx_personality_v0() { 2580 } 2581 2582 2583 function _emscripten_memcpy_big(dest, src, num) { 2584 HEAPU8.set(HEAPU8.subarray(src, src+num), dest); 2585 return dest; 2586 } 2587 Module["_memcpy"] = _memcpy; 2588 2589 function ___syscall140(which, varargs) {SYSCALLS.varargs = varargs; 2590 try { 2591 // llseek 2592 var stream = SYSCALLS.getStreamFromFD(), offset_high = SYSCALLS.get(), offset_low = SYSCALLS.get(), result = SYSCALLS.get(), whence = SYSCALLS.get(); 2593 var offset = offset_low; 2594 assert(offset_high === 0); 2595 FS.llseek(stream, offset, whence); 2596 HEAP32[((result)>>2)]=stream.position; 2597 if (stream.getdents && offset === 0 && whence === 0) stream.getdents = null; // reset readdir state 2598 return 0; 2599 } catch (e) { 2600 if (typeof FS === 'undefined' || !(e instanceof FS.ErrnoError)) abort(e); 2601 return -e.errno; 2602 } 2603 } 2604 2605 function _pthread_setspecific(key, value) { 2606 if (!(key in PTHREAD_SPECIFIC)) { 2607 return ERRNO_CODES.EINVAL; 2608 } 2609 PTHREAD_SPECIFIC[key] = value; 2610 return 0; 2611 } 2612 2613 2614 Module["_pthread_self"] = _pthread_self; 2615 2616 function _emscripten_cancel_main_loop() { 2617 Browser.mainLoop.pause(); 2618 Browser.mainLoop.func = null; 2619 } 2620 2621 function _emscripten_run_script(ptr) { 2622 eval(Pointer_stringify(ptr)); 2623 } 2624 2625 function ___syscall146(which, varargs) {SYSCALLS.varargs = varargs; 2626 try { 2627 // writev 2628 // hack to support printf in NO_FILESYSTEM 2629 var stream = SYSCALLS.get(), iov = SYSCALLS.get(), iovcnt = SYSCALLS.get(); 2630 var ret = 0; 2631 if (!___syscall146.buffer) { 2632 ___syscall146.buffers = [null, [], []]; // 1 => stdout, 2 => stderr 2633 ___syscall146.printChar = function(stream, curr) { 2634 var buffer = ___syscall146.buffers[stream]; 2635 assert(buffer); 2636 if (curr === 0 || curr === 10) { 2637 (stream === 1 ? Module['print'] : Module['printErr'])(UTF8ArrayToString(buffer, 0)); 2638 buffer.length = 0; 2639 } else { 2640 buffer.push(curr); 2641 } 2642 }; 2643 } 2644 for (var i = 0; i < iovcnt; i++) { 2645 var ptr = HEAP32[(((iov)+(i*8))>>2)]; 2646 var len = HEAP32[(((iov)+(i*8 + 4))>>2)]; 2647 for (var j = 0; j < len; j++) { 2648 ___syscall146.printChar(stream, HEAPU8[ptr+j]); 2649 } 2650 ret += len; 2651 } 2652 return ret; 2653 } catch (e) { 2654 if (typeof FS === 'undefined' || !(e instanceof FS.ErrnoError)) abort(e); 2655 return -e.errno; 2656 } 2657 } 2658 2659 function ___syscall54(which, varargs) {SYSCALLS.varargs = varargs; 2660 try { 2661 // ioctl 2662 return 0; 2663 } catch (e) { 2664 if (typeof FS === 'undefined' || !(e instanceof FS.ErrnoError)) abort(e); 2665 return -e.errno; 2666 } 2667 } 2668 Module["requestFullScreen"] = function Module_requestFullScreen(lockPointer, resizeCanvas, vrDevice) { Module.printErr("Module.requestFullScreen is deprecated. Please call Module.requestFullscreen instead."); Module["requestFullScreen"] = Module["requestFullscreen"]; Browser.requestFullScreen(lockPointer, resizeCanvas, vrDevice) }; 2669 Module["requestFullscreen"] = function Module_requestFullscreen(lockPointer, resizeCanvas, vrDevice) { Browser.requestFullscreen(lockPointer, resizeCanvas, vrDevice) }; 2670 Module["requestAnimationFrame"] = function Module_requestAnimationFrame(func) { Browser.requestAnimationFrame(func) }; 2671 Module["setCanvasSize"] = function Module_setCanvasSize(width, height, noUpdates) { Browser.setCanvasSize(width, height, noUpdates) }; 2672 Module["pauseMainLoop"] = function Module_pauseMainLoop() { Browser.mainLoop.pause() }; 2673 Module["resumeMainLoop"] = function Module_resumeMainLoop() { Browser.mainLoop.resume() }; 2674 Module["getUserMedia"] = function Module_getUserMedia() { Browser.getUserMedia() } 2675 Module["createContext"] = function Module_createContext(canvas, useWebGL, setInModule, webGLContextAttributes) { return Browser.createContext(canvas, useWebGL, setInModule, webGLContextAttributes) }; 2676 if (typeof dateNow !== 'undefined') { 2677 _emscripten_get_now = dateNow; 2678 } else if (typeof self === 'object' && self['performance'] && typeof self['performance']['now'] === 'function') { 2679 _emscripten_get_now = function() { return self['performance']['now'](); }; 2680 } else if (typeof performance === 'object' && typeof performance['now'] === 'function') { 2681 _emscripten_get_now = function() { return performance['now'](); }; 2682 } else { 2683 _emscripten_get_now = Date.now; 2684 }; 2685 /* flush anything remaining in the buffer during shutdown */ __ATEXIT__.push(function() { var fflush = Module["_fflush"]; if (fflush) fflush(0); var printChar = ___syscall146.printChar; if (!printChar) return; var buffers = ___syscall146.buffers; if (buffers[1].length) printChar(1, 10); if (buffers[2].length) printChar(2, 10); });; 2686 DYNAMICTOP_PTR = allocate(1, "i32", ALLOC_STATIC); 2687 2688 STACK_BASE = STACKTOP = Runtime.alignMemory(STATICTOP); 2689 2690 STACK_MAX = STACK_BASE + TOTAL_STACK; 2691 2692 DYNAMIC_BASE = Runtime.alignMemory(STACK_MAX); 2693 2694 HEAP32[DYNAMICTOP_PTR>>2] = DYNAMIC_BASE; 2695 2696 staticSealed = true; // seal the static portion of memory 2697 2698 2699 2700 Module['wasmTableSize'] = 120; 2701 2702 Module['wasmMaxTableSize'] = 120; 2703 2704 function invoke_iiii(index,a1,a2,a3) { 2705 try { 2706 return Module["dynCall_iiii"](index,a1,a2,a3); 2707 } catch(e) { 2708 if (typeof e !== 'number' && e !== 'longjmp') throw e; 2709 Module["setThrew"](1, 0); 2710 } 2711 } 2712 2713 function invoke_viiiii(index,a1,a2,a3,a4,a5) { 2714 try { 2715 Module["dynCall_viiiii"](index,a1,a2,a3,a4,a5); 2716 } catch(e) { 2717 if (typeof e !== 'number' && e !== 'longjmp') throw e; 2718 Module["setThrew"](1, 0); 2719 } 2720 } 2721 2722 function invoke_vi(index,a1) { 2723 try { 2724 Module["dynCall_vi"](index,a1); 2725 } catch(e) { 2726 if (typeof e !== 'number' && e !== 'longjmp') throw e; 2727 Module["setThrew"](1, 0); 2728 } 2729 } 2730 2731 function invoke_vii(index,a1,a2) { 2732 try { 2733 Module["dynCall_vii"](index,a1,a2); 2734 } catch(e) { 2735 if (typeof e !== 'number' && e !== 'longjmp') throw e; 2736 Module["setThrew"](1, 0); 2737 } 2738 } 2739 2740 function invoke_ii(index,a1) { 2741 try { 2742 return Module["dynCall_ii"](index,a1); 2743 } catch(e) { 2744 if (typeof e !== 'number' && e !== 'longjmp') throw e; 2745 Module["setThrew"](1, 0); 2746 } 2747 } 2748 2749 function invoke_viii(index,a1,a2,a3) { 2750 try { 2751 Module["dynCall_viii"](index,a1,a2,a3); 2752 } catch(e) { 2753 if (typeof e !== 'number' && e !== 'longjmp') throw e; 2754 Module["setThrew"](1, 0); 2755 } 2756 } 2757 2758 function invoke_v(index) { 2759 try { 2760 Module["dynCall_v"](index); 2761 } catch(e) { 2762 if (typeof e !== 'number' && e !== 'longjmp') throw e; 2763 Module["setThrew"](1, 0); 2764 } 2765 } 2766 2767 function invoke_viif(index,a1,a2,a3) { 2768 try { 2769 Module["dynCall_viif"](index,a1,a2,a3); 2770 } catch(e) { 2771 if (typeof e !== 'number' && e !== 'longjmp') throw e; 2772 Module["setThrew"](1, 0); 2773 } 2774 } 2775 2776 function invoke_viiiiii(index,a1,a2,a3,a4,a5,a6) { 2777 try { 2778 Module["dynCall_viiiiii"](index,a1,a2,a3,a4,a5,a6); 2779 } catch(e) { 2780 if (typeof e !== 'number' && e !== 'longjmp') throw e; 2781 Module["setThrew"](1, 0); 2782 } 2783 } 2784 2785 function invoke_iii(index,a1,a2) { 2786 try { 2787 return Module["dynCall_iii"](index,a1,a2); 2788 } catch(e) { 2789 if (typeof e !== 'number' && e !== 'longjmp') throw e; 2790 Module["setThrew"](1, 0); 2791 } 2792 } 2793 2794 function invoke_iiiiii(index,a1,a2,a3,a4,a5) { 2795 try { 2796 return Module["dynCall_iiiiii"](index,a1,a2,a3,a4,a5); 2797 } catch(e) { 2798 if (typeof e !== 'number' && e !== 'longjmp') throw e; 2799 Module["setThrew"](1, 0); 2800 } 2801 } 2802 2803 function invoke_viiii(index,a1,a2,a3,a4) { 2804 try { 2805 Module["dynCall_viiii"](index,a1,a2,a3,a4); 2806 } catch(e) { 2807 if (typeof e !== 'number' && e !== 'longjmp') throw e; 2808 Module["setThrew"](1, 0); 2809 } 2810 } 2811 2812 Module.asmGlobalArg = { "Math": Math, "Int8Array": Int8Array, "Int16Array": Int16Array, "Int32Array": Int32Array, "Uint8Array": Uint8Array, "Uint16Array": Uint16Array, "Uint32Array": Uint32Array, "Float32Array": Float32Array, "Float64Array": Float64Array, "NaN": NaN, "Infinity": Infinity }; 2813 2814 Module.asmLibraryArg = { "abort": abort, "assert": assert, "enlargeMemory": enlargeMemory, "getTotalMemory": getTotalMemory, "abortOnCannotGrowMemory": abortOnCannotGrowMemory, "invoke_iiii": invoke_iiii, "invoke_viiiii": invoke_viiiii, "invoke_vi": invoke_vi, "invoke_vii": invoke_vii, "invoke_ii": invoke_ii, "invoke_viii": invoke_viii, "invoke_v": invoke_v, "invoke_viif": invoke_viif, "invoke_viiiiii": invoke_viiiiii, "invoke_iii": invoke_iii, "invoke_iiiiii": invoke_iiiiii, "invoke_viiii": invoke_viiii, "_pthread_cleanup_pop": _pthread_cleanup_pop, "_emscripten_run_script": _emscripten_run_script, "_pthread_key_create": _pthread_key_create, "___syscall6": ___syscall6, "___setErrNo": ___setErrNo, "___gxx_personality_v0": ___gxx_personality_v0, "___assert_fail": ___assert_fail, "__ZSt18uncaught_exceptionv": __ZSt18uncaught_exceptionv, "_emscripten_set_main_loop_timing": _emscripten_set_main_loop_timing, "___cxa_begin_catch": ___cxa_begin_catch, "_emscripten_memcpy_big": _emscripten_memcpy_big, "___resumeException": ___resumeException, "___cxa_find_matching_catch": ___cxa_find_matching_catch, "_pthread_getspecific": _pthread_getspecific, "_clock": _clock, "_pthread_once": _pthread_once, "___syscall54": ___syscall54, "___unlock": ___unlock, "_emscripten_set_main_loop": _emscripten_set_main_loop, "_emscripten_get_now": _emscripten_get_now, "_pthread_setspecific": _pthread_setspecific, "___lock": ___lock, "_abort": _abort, "_pthread_cleanup_push": _pthread_cleanup_push, "_emscripten_cancel_main_loop": _emscripten_cancel_main_loop, "___syscall140": ___syscall140, "___cxa_pure_virtual": ___cxa_pure_virtual, "___syscall146": ___syscall146, "DYNAMICTOP_PTR": DYNAMICTOP_PTR, "tempDoublePtr": tempDoublePtr, "ABORT": ABORT, "STACKTOP": STACKTOP, "STACK_MAX": STACK_MAX }; 2815 // EMSCRIPTEN_START_ASM 2816 var asm =Module["asm"]// EMSCRIPTEN_END_ASM 2817 (Module.asmGlobalArg, Module.asmLibraryArg, buffer); 2818 2819 Module["asm"] = asm; 2820 var _malloc = Module["_malloc"] = function() { return Module["asm"]["_malloc"].apply(null, arguments) }; 2821 var ___cxa_can_catch = Module["___cxa_can_catch"] = function() { return Module["asm"]["___cxa_can_catch"].apply(null, arguments) }; 2822 var getTempRet0 = Module["getTempRet0"] = function() { return Module["asm"]["getTempRet0"].apply(null, arguments) }; 2823 var _free = Module["_free"] = function() { return Module["asm"]["_free"].apply(null, arguments) }; 2824 var _main = Module["_main"] = function() { return Module["asm"]["_main"].apply(null, arguments) }; 2825 var setTempRet0 = Module["setTempRet0"] = function() { return Module["asm"]["setTempRet0"].apply(null, arguments) }; 2826 var ___cxa_is_pointer_type = Module["___cxa_is_pointer_type"] = function() { return Module["asm"]["___cxa_is_pointer_type"].apply(null, arguments) }; 2827 var establishStackSpace = Module["establishStackSpace"] = function() { return Module["asm"]["establishStackSpace"].apply(null, arguments) }; 2828 var _pthread_self = Module["_pthread_self"] = function() { return Module["asm"]["_pthread_self"].apply(null, arguments) }; 2829 var stackSave = Module["stackSave"] = function() { return Module["asm"]["stackSave"].apply(null, arguments) }; 2830 var _memset = Module["_memset"] = function() { return Module["asm"]["_memset"].apply(null, arguments) }; 2831 var _sbrk = Module["_sbrk"] = function() { return Module["asm"]["_sbrk"].apply(null, arguments) }; 2832 var stackRestore = Module["stackRestore"] = function() { return Module["asm"]["stackRestore"].apply(null, arguments) }; 2833 var _memcpy = Module["_memcpy"] = function() { return Module["asm"]["_memcpy"].apply(null, arguments) }; 2834 var stackAlloc = Module["stackAlloc"] = function() { return Module["asm"]["stackAlloc"].apply(null, arguments) }; 2835 var setThrew = Module["setThrew"] = function() { return Module["asm"]["setThrew"].apply(null, arguments) }; 2836 var _fflush = Module["_fflush"] = function() { return Module["asm"]["_fflush"].apply(null, arguments) }; 2837 var ___errno_location = Module["___errno_location"] = function() { return Module["asm"]["___errno_location"].apply(null, arguments) }; 2838 var runPostSets = Module["runPostSets"] = function() { return Module["asm"]["runPostSets"].apply(null, arguments) }; 2839 var dynCall_iiii = Module["dynCall_iiii"] = function() { return Module["asm"]["dynCall_iiii"].apply(null, arguments) }; 2840 var dynCall_viiiii = Module["dynCall_viiiii"] = function() { return Module["asm"]["dynCall_viiiii"].apply(null, arguments) }; 2841 var dynCall_vi = Module["dynCall_vi"] = function() { return Module["asm"]["dynCall_vi"].apply(null, arguments) }; 2842 var dynCall_vii = Module["dynCall_vii"] = function() { return Module["asm"]["dynCall_vii"].apply(null, arguments) }; 2843 var dynCall_ii = Module["dynCall_ii"] = function() { return Module["asm"]["dynCall_ii"].apply(null, arguments) }; 2844 var dynCall_viii = Module["dynCall_viii"] = function() { return Module["asm"]["dynCall_viii"].apply(null, arguments) }; 2845 var dynCall_v = Module["dynCall_v"] = function() { return Module["asm"]["dynCall_v"].apply(null, arguments) }; 2846 var dynCall_viif = Module["dynCall_viif"] = function() { return Module["asm"]["dynCall_viif"].apply(null, arguments) }; 2847 var dynCall_viiiiii = Module["dynCall_viiiiii"] = function() { return Module["asm"]["dynCall_viiiiii"].apply(null, arguments) }; 2848 var dynCall_iii = Module["dynCall_iii"] = function() { return Module["asm"]["dynCall_iii"].apply(null, arguments) }; 2849 var dynCall_iiiiii = Module["dynCall_iiiiii"] = function() { return Module["asm"]["dynCall_iiiiii"].apply(null, arguments) }; 2850 var dynCall_viiii = Module["dynCall_viiii"] = function() { return Module["asm"]["dynCall_viiii"].apply(null, arguments) }; 2851 ; 2852 2853 Runtime.stackAlloc = Module['stackAlloc']; 2854 Runtime.stackSave = Module['stackSave']; 2855 Runtime.stackRestore = Module['stackRestore']; 2856 Runtime.establishStackSpace = Module['establishStackSpace']; 2857 2858 Runtime.setTempRet0 = Module['setTempRet0']; 2859 Runtime.getTempRet0 = Module['getTempRet0']; 2860 2861 2862 2863 // === Auto-generated postamble setup entry stuff === 2864 2865 Module['asm'] = asm; 2866 2867 2868 2869 if (memoryInitializer) { 2870 if (typeof Module['locateFile'] === 'function') { 2871 memoryInitializer = Module['locateFile'](memoryInitializer); 2872 } else if (Module['memoryInitializerPrefixURL']) { 2873 memoryInitializer = Module['memoryInitializerPrefixURL'] + memoryInitializer; 2874 } 2875 var data = Module['readBinary'](memoryInitializer); 2876 HEAPU8.set(data, Runtime.GLOBAL_BASE); 2877 } 2878 2879 function ExitStatus(status) { 2880 this.name = "ExitStatus"; 2881 this.message = "Program terminated with exit(" + status + ")"; 2882 this.status = status; 2883 }; 2884 ExitStatus.prototype = new Error(); 2885 ExitStatus.prototype.constructor = ExitStatus; 2886 2887 var initialStackTop; 2888 var preloadStartTime = null; 2889 var calledMain = false; 2890 2891 dependenciesFulfilled = function runCaller() { 2892 // If run has never been called, and we should call run (INVOKE_RUN is true, and Module.noInitialRun is not false) 2893 if (!Module['calledRun']) run(); 2894 if (!Module['calledRun']) dependenciesFulfilled = runCaller; // try this again later, after new deps are fulfilled 2895 } 2896 2897 Module['callMain'] = Module.callMain = function callMain(args) { 2898 2899 args = args || []; 2900 2901 ensureInitRuntime(); 2902 2903 var argc = args.length+1; 2904 function pad() { 2905 for (var i = 0; i < 4-1; i++) { 2906 argv.push(0); 2907 } 2908 } 2909 var argv = [allocate(intArrayFromString(Module['thisProgram']), 'i8', ALLOC_NORMAL) ]; 2910 pad(); 2911 for (var i = 0; i < argc-1; i = i + 1) { 2912 argv.push(allocate(intArrayFromString(args[i]), 'i8', ALLOC_NORMAL)); 2913 pad(); 2914 } 2915 argv.push(0); 2916 argv = allocate(argv, 'i32', ALLOC_NORMAL); 2917 2918 2919 try { 2920 2921 var ret = Module['_main'](argc, argv, 0); 2922 2923 2924 // if we're not running an evented main loop, it's time to exit 2925 exit(ret, /* implicit = */ true); 2926 } 2927 catch(e) { 2928 if (e instanceof ExitStatus) { 2929 // exit() throws this once it's done to make sure execution 2930 // has been stopped completely 2931 return; 2932 } else if (e == 'SimulateInfiniteLoop') { 2933 // running an evented main loop, don't immediately exit 2934 Module['noExitRuntime'] = true; 2935 return; 2936 } else { 2937 if (e && typeof e === 'object' && e.stack) Module.printErr('exception thrown: ' + [e, e.stack]); 2938 throw e; 2939 } 2940 } finally { 2941 calledMain = true; 2942 } 2943 } 2944 2945 2946 2947 2948 function run(args) { 2949 args = args || Module['arguments']; 2950 2951 if (preloadStartTime === null) preloadStartTime = Date.now(); 2952 2953 if (runDependencies > 0) { 2954 return; 2955 } 2956 2957 2958 preRun(); 2959 2960 if (runDependencies > 0) return; // a preRun added a dependency, run will be called later 2961 if (Module['calledRun']) return; // run may have just been called through dependencies being fulfilled just in this very frame 2962 2963 function doRun() { 2964 if (Module['calledRun']) return; // run may have just been called while the async setStatus time below was happening 2965 Module['calledRun'] = true; 2966 2967 if (ABORT) return; 2968 2969 ensureInitRuntime(); 2970 2971 preMain(); 2972 2973 2974 if (Module['onRuntimeInitialized']) Module['onRuntimeInitialized'](); 2975 2976 if (Module['_main'] && shouldRunNow) Module['callMain'](args); 2977 2978 postRun(); 2979 } 2980 2981 if (Module['setStatus']) { 2982 Module['setStatus']('Running...'); 2983 setTimeout(function() { 2984 setTimeout(function() { 2985 Module['setStatus'](''); 2986 }, 1); 2987 doRun(); 2988 }, 1); 2989 } else { 2990 doRun(); 2991 } 2992 } 2993 Module['run'] = Module.run = run; 2994 2995 function exit(status, implicit) { 2996 throw new ExitStatus(status); 2997 } 2998 Module['exit'] = Module.exit = exit; 2999 3000 var abortDecorators = []; 3001 3002 function abort(what) { 3003 if (what !== undefined) { 3004 Module.print(what); 3005 Module.printErr(what); 3006 what = JSON.stringify(what) 3007 } else { 3008 what = ''; 3009 } 3010 3011 ABORT = true; 3012 EXITSTATUS = 1; 3013 3014 var extra = '\nIf this abort() is unexpected, build with -s ASSERTIONS=1 which can give more information.'; 3015 3016 var output = 'abort(' + what + ') at ' + stackTrace() + extra; 3017 if (abortDecorators) { 3018 abortDecorators.forEach(function(decorator) { 3019 output = decorator(output, what); 3020 }); 3021 } 3022 throw output; 3023 } 3024 Module['abort'] = Module.abort = abort; 3025 3026 // {{PRE_RUN_ADDITIONS}} 3027 3028 if (Module['preInit']) { 3029 if (typeof Module['preInit'] == 'function') Module['preInit'] = [Module['preInit']]; 3030 while (Module['preInit'].length > 0) { 3031 Module['preInit'].pop()(); 3032 } 3033 } 3034 3035 // shouldRunNow refers to calling main(), not run(). 3036 var shouldRunNow = true; 3037 if (Module['noInitialRun']) { 3038 shouldRunNow = false; 3039 } 3040 3041 3042 run(); 3043 drainJobQueue(); 3044 3045 }; // End of function wrapping the whole top-level Emscripten glue code 3046 3047 const bytecode = os.file.readFile(scriptdir + 'wasm_box2d.wasm', 'binary'); 3048 3049 if (typeof setBufferStreamParams == 'function') { 3050 setBufferStreamParams(/* delayMillis = */ 1, /* chunkSize = */ 1000); 3051 } 3052 const cacheEntry = streamCacheEntry(bytecode); 3053 3054 runBox2d(cacheEntry); 3055 3056 while (!wasmHasTier2CompilationCompleted(cacheEntry.module)) 3057 sleep(1); 3058 3059 if (wasmCachingEnabled()) { 3060 assertEq(cacheEntry.cached, true) 3061 } 3062 3063 runBox2d(cacheEntry); 3064 3065 if (wasmCachingEnabled()) { 3066 runBox2d(wasmCompileInSeparateProcess(bytecode)); 3067 }