242
252
var tempRet0, tempRet1, tempRet2, tempRet3, tempRet4, tempRet5, tempRet6, tempRet7, tempRet8, tempRet9;
245
function abort(text) {
246
Module.print(text + ':\n' + (new Error).stack);
248
throw "Assertion: " + text;
251
255
function assert(condition, text) {
252
256
if (!condition) {
253
257
abort('Assertion failed: ' + text);
268
272
// @param ident The name of the C function (note that C++ functions will be name-mangled - use extern "C")
269
273
// @param returnType The return type of the function, one of the JS types 'number', 'string' or 'array' (use 'number' for any C pointer, and
270
// 'array' for JavaScript arrays and typed arrays).
274
// 'array' for JavaScript arrays and typed arrays; note that arrays are 8-bit).
271
275
// @param argTypes An array of the types of arguments for the function (if there are no arguments, this can be ommitted). Types are as in returnType,
272
276
// except that 'array' is not possible (there is no way for us to know the length of the array)
273
277
// @param args An array of the arguments to the function, as native JS values (as in returnType)
520
524
Module['allocate'] = allocate;
522
526
function Pointer_stringify(ptr, /* optional */ length) {
527
// TODO: use TextDecoder
523
528
// Find the length, and check for UTF while doing so
524
529
var hasUtf = false;
534
assert(ptr + i < TOTAL_MEMORY);
528
536
t = {{{ makeGetValue('ptr', 'i', 'i8', 0, 1) }}};
529
537
if (t >= 128) hasUtf = true;
530
538
else if (t == 0 && !length) break;
562
570
Module['Pointer_stringify'] = Pointer_stringify;
572
// Given a pointer 'ptr' to a null-terminated UTF16LE-encoded string in the emscripten HEAP, returns
573
// a copy of that string as a Javascript String object.
574
function UTF16ToString(ptr) {
579
var codeUnit = {{{ makeGetValue('ptr', 'i*2', 'i16') }}};
583
// fromCharCode constructs a character from a UTF-16 code unit, so we can pass the UTF16 string right through.
584
str += String.fromCharCode(codeUnit);
587
Module['UTF16ToString'] = UTF16ToString;
589
// Copies the given Javascript String object 'str' to the emscripten HEAP at address 'outPtr',
590
// null-terminated and encoded in UTF16LE form. The copy will require at most (str.length*2+1)*2 bytes of space in the HEAP.
591
function stringToUTF16(str, outPtr) {
592
for(var i = 0; i < str.length; ++i) {
593
// charCodeAt returns a UTF-16 encoded code unit, so it can be directly written to the HEAP.
594
var codeUnit = str.charCodeAt(i); // possibly a lead surrogate
595
{{{ makeSetValue('outPtr', 'i*2', 'codeUnit', 'i16') }}}
597
// Null-terminate the pointer to the HEAP.
598
{{{ makeSetValue('outPtr', 'str.length*2', 0, 'i16') }}}
600
Module['stringToUTF16'] = stringToUTF16;
602
// Given a pointer 'ptr' to a null-terminated UTF32LE-encoded string in the emscripten HEAP, returns
603
// a copy of that string as a Javascript String object.
604
function UTF32ToString(ptr) {
609
var utf32 = {{{ makeGetValue('ptr', 'i*4', 'i32') }}};
613
// 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.
614
if (utf32 >= 0x10000) {
615
var ch = utf32 - 0x10000;
616
str += String.fromCharCode(0xD800 | (ch >> 10), 0xDC00 | (ch & 0x3FF));
618
str += String.fromCharCode(utf32);
622
Module['UTF32ToString'] = UTF32ToString;
624
// Copies the given Javascript String object 'str' to the emscripten HEAP at address 'outPtr',
625
// null-terminated and encoded in UTF32LE form. The copy will require at most (str.length+1)*4 bytes of space in the HEAP,
626
// but can use less, since str.length does not return the number of characters in the string, but the number of UTF-16 code units in the string.
627
function stringToUTF32(str, outPtr) {
629
for(var iCodeUnit = 0; iCodeUnit < str.length; ++iCodeUnit) {
630
// 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.
631
var codeUnit = str.charCodeAt(iCodeUnit); // possibly a lead surrogate
632
if (codeUnit >= 0xD800 && codeUnit <= 0xDFFF) {
633
var trailSurrogate = str.charCodeAt(++iCodeUnit);
634
codeUnit = 0x10000 + ((codeUnit & 0x3FF) << 10) | (trailSurrogate & 0x3FF);
636
{{{ makeSetValue('outPtr', 'iChar*4', 'codeUnit', 'i32') }}}
639
// Null-terminate the pointer to the HEAP.
640
{{{ makeSetValue('outPtr', 'iChar*4', 0, 'i32') }}}
642
Module['stringToUTF32'] = stringToUTF32;
564
644
// Memory management
566
646
var PAGE_SIZE = 4096;
587
667
function enlargeMemory() {
588
668
#if ALLOW_MEMORY_GROWTH == 0
590
abort('Cannot enlarge memory arrays. Either (1) compile with -s TOTAL_MEMORY=X with X higher than the current value, (2) compile with ALLOW_MEMORY_GROWTH which adjusts the size at runtime but prevents some optimizations, or (3) set Module.TOTAL_MEMORY before the program runs.');
670
abort('Cannot enlarge memory arrays. Either (1) compile with -s TOTAL_MEMORY=X with X higher than the current value ' + TOTAL_MEMORY + ', (2) compile with ALLOW_MEMORY_GROWTH which adjusts the size at runtime but prevents some optimizations, or (3) set Module.TOTAL_MEMORY before the program runs.');
592
abort('Cannot enlarge memory arrays in asm.js. Either (1) compile with -s TOTAL_MEMORY=X with X higher than the current value, or (2) set Module.TOTAL_MEMORY before the program runs.');
672
abort('Cannot enlarge memory arrays in asm.js. Either (1) compile with -s TOTAL_MEMORY=X with X higher than the current value ' + TOTAL_MEMORY + ', or (2) set Module.TOTAL_MEMORY before the program runs.');
595
675
// TOTAL_MEMORY is the current size of the actual array, and DYNAMICTOP is the new top.
711
var __ATINIT__ = []; // functions called during startup
712
var __ATMAIN__ = []; // functions called when main() is to be run
713
var __ATEXIT__ = []; // functions called during shutdown
791
var __ATPRERUN__ = []; // functions called before the runtime is initialized
792
var __ATINIT__ = []; // functions called during startup
793
var __ATMAIN__ = []; // functions called when main() is to be run
794
var __ATEXIT__ = []; // functions called during shutdown
795
var __ATPOSTRUN__ = []; // functions called after the runtime has exited
715
797
var runtimeInitialized = false;
800
// compatibility - merge in anything from Module['preRun'] at this time
801
if (Module['preRun']) {
802
if (typeof Module['preRun'] == 'function') Module['preRun'] = [Module['preRun']];
803
while (Module['preRun'].length) {
804
addOnPreRun(Module['preRun'].shift());
807
callRuntimeCallbacks(__ATPRERUN__);
717
810
function ensureInitRuntime() {
718
811
if (runtimeInitialized) return;
719
812
runtimeInitialized = true;
720
813
callRuntimeCallbacks(__ATINIT__);
722
816
function preMain() {
723
817
callRuntimeCallbacks(__ATMAIN__);
725
820
function exitRuntime() {
726
821
callRuntimeCallbacks(__ATEXIT__);
825
// compatibility - merge in anything from Module['postRun'] at this time
826
if (Module['postRun']) {
827
if (typeof Module['postRun'] == 'function') Module['postRun'] = [Module['postRun']];
828
while (Module['postRun'].length) {
829
addOnPostRun(Module['postRun'].shift());
832
callRuntimeCallbacks(__ATPOSTRUN__);
835
function addOnPreRun(cb) {
836
__ATPRERUN__.unshift(cb);
838
Module['addOnPreRun'] = Module.addOnPreRun = addOnPreRun;
840
function addOnInit(cb) {
841
__ATINIT__.unshift(cb);
843
Module['addOnInit'] = Module.addOnInit = addOnInit;
845
function addOnPreMain(cb) {
846
__ATMAIN__.unshift(cb);
848
Module['addOnPreMain'] = Module.addOnPreMain = addOnPreMain;
850
function addOnExit(cb) {
851
__ATEXIT__.unshift(cb);
853
Module['addOnExit'] = Module.addOnExit = addOnExit;
855
function addOnPostRun(cb) {
856
__ATPOSTRUN__.unshift(cb);
858
Module['addOnPostRun'] = Module.addOnPostRun = addOnPostRun;
731
862
// This processes a JS string into a C-line array of numbers, 0-terminated.