~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to src/preamble.js

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-09-20 22:44:35 UTC
  • mfrom: (4.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130920224435-apuwj4fsl3fqv1a6
Tags: 1.5.6~20130920~6010666-1
* New snapshot release
* Update the list of supported architectures to the same as libv8
  (Closes: #723129)
* emlibtool has been removed from upstream.
* Fix warning syntax-error-in-dep5-copyright
* Refresh of the patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
156
156
  SAFE_HEAP_ACCESS(dest, HEAP_HISTORY[dest] || null, true, false);
157
157
}
158
158
 
 
159
function SAFE_HEAP_FILL_HISTORY(from, to, type) {
 
160
#if SAFE_HEAP_LOG
 
161
  Module.print('SAFE_HEAP fill: ' + [from, to, type]);
 
162
#endif
 
163
  for (var i = from; i < to; i++) {
 
164
    HEAP_HISTORY[i] = type;
 
165
  }
 
166
}
 
167
 
159
168
//==========================================
160
169
#endif
161
170
 
232
241
#endif
233
242
 
234
243
var ABORT = false; // whether we are quitting the application. no code should run after this. set in exit() and abort()
 
244
var EXITSTATUS = 0;
235
245
 
236
246
var undef = 0;
237
247
// tempInt is used for 32-bit signed values or smaller. tempBigInt is used
242
252
var tempRet0, tempRet1, tempRet2, tempRet3, tempRet4, tempRet5, tempRet6, tempRet7, tempRet8, tempRet9;
243
253
#endif
244
254
 
245
 
function abort(text) {
246
 
  Module.print(text + ':\n' + (new Error).stack);
247
 
  ABORT = true;
248
 
  throw "Assertion: " + text;
249
 
}
250
 
 
251
255
function assert(condition, text) {
252
256
  if (!condition) {
253
257
    abort('Assertion failed: ' + text);
267
271
//
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)
281
285
// Returns the C function with a specified identifier (for C++, you need to do manual name mangling)
282
286
function getCFunc(ident) {
283
287
  try {
284
 
    var func = globalScope['Module']['_' + ident]; // closure exported function
 
288
    var func = Module['_' + ident]; // closure exported function
285
289
    if (!func) func = eval('_' + ident); // explicit lookup
286
290
  } catch(e) {
287
291
  }
520
524
Module['allocate'] = allocate;
521
525
 
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;
525
530
  var t;
526
531
  var i = 0;
527
532
  while (1) {
 
533
#if ASSERTIONS
 
534
    assert(ptr + i < TOTAL_MEMORY);
 
535
#endif
528
536
    t = {{{ makeGetValue('ptr', 'i', 'i8', 0, 1) }}};
529
537
    if (t >= 128) hasUtf = true;
530
538
    else if (t == 0 && !length) break;
561
569
}
562
570
Module['Pointer_stringify'] = Pointer_stringify;
563
571
 
 
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) {
 
575
  var i = 0;
 
576
 
 
577
  var str = '';
 
578
  while (1) {
 
579
    var codeUnit = {{{ makeGetValue('ptr', 'i*2', 'i16') }}};
 
580
    if (codeUnit == 0)
 
581
      return str;
 
582
    ++i;
 
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);
 
585
  }
 
586
}
 
587
Module['UTF16ToString'] = UTF16ToString;
 
588
 
 
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') }}}
 
596
  }
 
597
  // Null-terminate the pointer to the HEAP.
 
598
  {{{ makeSetValue('outPtr', 'str.length*2', 0, 'i16') }}}
 
599
}
 
600
Module['stringToUTF16'] = stringToUTF16;
 
601
 
 
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) {
 
605
  var i = 0;
 
606
 
 
607
  var str = '';
 
608
  while (1) {
 
609
    var utf32 = {{{ makeGetValue('ptr', 'i*4', 'i32') }}};
 
610
    if (utf32 == 0)
 
611
      return str;
 
612
    ++i;
 
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));
 
617
    } else {
 
618
      str += String.fromCharCode(utf32);
 
619
    }
 
620
  }
 
621
}
 
622
Module['UTF32ToString'] = UTF32ToString;
 
623
 
 
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) {
 
628
  var iChar = 0;
 
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);
 
635
    }
 
636
    {{{ makeSetValue('outPtr', 'iChar*4', 'codeUnit', 'i32') }}}
 
637
    ++iChar;
 
638
  }
 
639
  // Null-terminate the pointer to the HEAP.
 
640
  {{{ makeSetValue('outPtr', 'iChar*4', 0, 'i32') }}}
 
641
}
 
642
Module['stringToUTF32'] = stringToUTF32;
 
643
 
564
644
// Memory management
565
645
 
566
646
var PAGE_SIZE = 4096;
587
667
function enlargeMemory() {
588
668
#if ALLOW_MEMORY_GROWTH == 0
589
669
#if ASM_JS == 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.');
591
671
#else
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.');
593
673
#endif
594
674
#else
595
675
  // TOTAL_MEMORY is the current size of the actual array, and DYNAMICTOP is the new top.
708
788
  }
709
789
}
710
790
 
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
714
796
 
715
797
var runtimeInitialized = false;
716
798
 
 
799
function preRun() {
 
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());
 
805
    }
 
806
  }
 
807
  callRuntimeCallbacks(__ATPRERUN__);
 
808
}
 
809
 
717
810
function ensureInitRuntime() {
718
811
  if (runtimeInitialized) return;
719
812
  runtimeInitialized = true;
720
813
  callRuntimeCallbacks(__ATINIT__);
721
814
}
 
815
 
722
816
function preMain() {
723
817
  callRuntimeCallbacks(__ATMAIN__);
724
818
}
 
819
 
725
820
function exitRuntime() {
726
821
  callRuntimeCallbacks(__ATEXIT__);
727
822
}
728
823
 
 
824
function postRun() {
 
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());
 
830
    }
 
831
  }
 
832
  callRuntimeCallbacks(__ATPOSTRUN__);
 
833
}
 
834
 
 
835
function addOnPreRun(cb) {
 
836
  __ATPRERUN__.unshift(cb);
 
837
}
 
838
Module['addOnPreRun'] = Module.addOnPreRun = addOnPreRun;
 
839
 
 
840
function addOnInit(cb) {
 
841
  __ATINIT__.unshift(cb);
 
842
}
 
843
Module['addOnInit'] = Module.addOnInit = addOnInit;
 
844
 
 
845
function addOnPreMain(cb) {
 
846
  __ATMAIN__.unshift(cb);
 
847
}
 
848
Module['addOnPreMain'] = Module.addOnPreMain = addOnPreMain;
 
849
 
 
850
function addOnExit(cb) {
 
851
  __ATEXIT__.unshift(cb);
 
852
}
 
853
Module['addOnExit'] = Module.addOnExit = addOnExit;
 
854
 
 
855
function addOnPostRun(cb) {
 
856
  __ATPOSTRUN__.unshift(cb);
 
857
}
 
858
Module['addOnPostRun'] = Module.addOnPostRun = addOnPostRun;
 
859
 
729
860
// Tools
730
861
 
731
862
// This processes a JS string into a C-line array of numbers, 0-terminated.
793
924
  return (a*b)|0; // fast but imprecise
794
925
};
795
926
#endif
 
927
Math.imul = Math['imul'];
 
928
 
 
929
#if TO_FLOAT32
 
930
if (!Math['toFloat32']) Math['toFloat32'] = function(x) {
 
931
  return x;
 
932
};
 
933
Math.toFloat32 = Math['toFloat32'];
 
934
#endif
796
935
 
797
936
// A counter of dependencies for calling run(). If we need to
798
937
// do asynchronous work before running, increment this and
861
1000
Module["preloadedImages"] = {}; // maps url to image data
862
1001
Module["preloadedAudios"] = {}; // maps url to audio data
863
1002
 
864
 
function addPreRun(func) {
865
 
  if (!Module['preRun']) Module['preRun'] = [];
866
 
  else if (typeof Module['preRun'] == 'function') Module['preRun'] = [Module['preRun']];
867
 
  Module['preRun'].push(func);
868
 
}
869
 
 
870
1003
#if PGO
871
1004
var PGOMonitor = {
872
1005
  called: {},
881
1014
};
882
1015
Module['PGOMonitor'] = PGOMonitor;
883
1016
__ATEXIT__.push({ func: function() { PGOMonitor.dump() } });
884
 
addPreRun(function() { addRunDependency('pgo') });
 
1017
addOnPreRun(function() { addRunDependency('pgo') });
885
1018
#endif
886
1019
 
887
 
var awaitingMemoryInitializer = false;
888
 
 
889
1020
function loadMemoryInitializer(filename) {
890
1021
  function applyData(data) {
891
1022
#if USE_TYPED_ARRAYS == 2
893
1024
#else
894
1025
    allocate(data, 'i8', ALLOC_NONE, STATIC_BASE);
895
1026
#endif
896
 
    runPostSets();
897
1027
  }
898
1028
 
899
1029
  // always do this asynchronously, to keep shell and web as similar as possible
900
 
  addPreRun(function() {
 
1030
  addOnPreRun(function() {
901
1031
    if (ENVIRONMENT_IS_NODE || ENVIRONMENT_IS_SHELL) {
902
1032
      applyData(Module['readBinary'](filename));
903
1033
    } else {
908
1038
      });
909
1039
    }
910
1040
  });
911
 
 
912
 
  awaitingMemoryInitializer = false;
913
1041
}
914
1042
 
915
1043
// === Body ===