PageRenderTime 76ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 4ms

/test/mjsunit/asm/embenchen/lua_binarytrees.js

http://v8.googlecode.com/
JavaScript | 14373 lines | 13162 code | 592 blank | 619 comment | 3858 complexity | 4bf4fb04b77a8edbd76184a5ed3432a9 MD5 | raw file
Possible License(s): BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. // Copyright 2014 the V8 project authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. var EXPECTED_OUTPUT =
  5. 'stretch tree of depth 10\t check: -1\n' +
  6. '1448\t trees of depth 4\t check: -1448\n' +
  7. '362\t trees of depth 6\t check: -362\n' +
  8. '90\t trees of depth 8\t check: -90\n' +
  9. 'long lived tree of depth 9\t check: -1\n';
  10. var Module = {
  11. arguments: [1],
  12. print: function(x) {Module.printBuffer += x + '\n';},
  13. preRun: [function() {Module.printBuffer = ''}],
  14. postRun: [function() {
  15. assertEquals(EXPECTED_OUTPUT, Module.printBuffer);
  16. }],
  17. };
  18. var Module;
  19. if (typeof Module === 'undefined') Module = eval('(function() { try { return Module || {} } catch(e) { return {} } })()');
  20. if (!Module.expectedDataFileDownloads) {
  21. Module.expectedDataFileDownloads = 0;
  22. Module.finishedDataFileDownloads = 0;
  23. }
  24. Module.expectedDataFileDownloads++;
  25. (function() {
  26. function runWithFS() {
  27. function assert(check, msg) {
  28. if (!check) throw msg + new Error().stack;
  29. }
  30. Module['FS_createDataFile']('/', 'binarytrees.lua', [45, 45, 32, 84, 104, 101, 32, 67, 111, 109, 112, 117, 116, 101, 114, 32, 76, 97, 110, 103, 117, 97, 103, 101, 32, 66, 101, 110, 99, 104, 109, 97, 114, 107, 115, 32, 71, 97, 109, 101, 10, 45, 45, 32, 104, 116, 116, 112, 58, 47, 47, 98, 101, 110, 99, 104, 109, 97, 114, 107, 115, 103, 97, 109, 101, 46, 97, 108, 105, 111, 116, 104, 46, 100, 101, 98, 105, 97, 110, 46, 111, 114, 103, 47, 10, 45, 45, 32, 99, 111, 110, 116, 114, 105, 98, 117, 116, 101, 100, 32, 98, 121, 32, 77, 105, 107, 101, 32, 80, 97, 108, 108, 10, 10, 108, 111, 99, 97, 108, 32, 102, 117, 110, 99, 116, 105, 111, 110, 32, 66, 111, 116, 116, 111, 109, 85, 112, 84, 114, 101, 101, 40, 105, 116, 101, 109, 44, 32, 100, 101, 112, 116, 104, 41, 10, 32, 32, 105, 102, 32, 100, 101, 112, 116, 104, 32, 62, 32, 48, 32, 116, 104, 101, 110, 10, 32, 32, 32, 32, 108, 111, 99, 97, 108, 32, 105, 32, 61, 32, 105, 116, 101, 109, 32, 43, 32, 105, 116, 101, 109, 10, 32, 32, 32, 32, 100, 101, 112, 116, 104, 32, 61, 32, 100, 101, 112, 116, 104, 32, 45, 32, 49, 10, 32, 32, 32, 32, 108, 111, 99, 97, 108, 32, 108, 101, 102, 116, 44, 32, 114, 105, 103, 104, 116, 32, 61, 32, 66, 111, 116, 116, 111, 109, 85, 112, 84, 114, 101, 101, 40, 105, 45, 49, 44, 32, 100, 101, 112, 116, 104, 41, 44, 32, 66, 111, 116, 116, 111, 109, 85, 112, 84, 114, 101, 101, 40, 105, 44, 32, 100, 101, 112, 116, 104, 41, 10, 32, 32, 32, 32, 114, 101, 116, 117, 114, 110, 32, 123, 32, 105, 116, 101, 109, 44, 32, 108, 101, 102, 116, 44, 32, 114, 105, 103, 104, 116, 32, 125, 10, 32, 32, 101, 108, 115, 101, 10, 32, 32, 32, 32, 114, 101, 116, 117, 114, 110, 32, 123, 32, 105, 116, 101, 109, 32, 125, 10, 32, 32, 101, 110, 100, 10, 101, 110, 100, 10, 10, 108, 111, 99, 97, 108, 32, 102, 117, 110, 99, 116, 105, 111, 110, 32, 73, 116, 101, 109, 67, 104, 101, 99, 107, 40, 116, 114, 101, 101, 41, 10, 32, 32, 105, 102, 32, 116, 114, 101, 101, 91, 50, 93, 32, 116, 104, 101, 110, 10, 32, 32, 32, 32, 114, 101, 116, 117, 114, 110, 32, 116, 114, 101, 101, 91, 49, 93, 32, 43, 32, 73, 116, 101, 109, 67, 104, 101, 99, 107, 40, 116, 114, 101, 101, 91, 50, 93, 41, 32, 45, 32, 73, 116, 101, 109, 67, 104, 101, 99, 107, 40, 116, 114, 101, 101, 91, 51, 93, 41, 10, 32, 32, 101, 108, 115, 101, 10, 32, 32, 32, 32, 114, 101, 116, 117, 114, 110, 32, 116, 114, 101, 101, 91, 49, 93, 10, 32, 32, 101, 110, 100, 10, 101, 110, 100, 10, 10, 108, 111, 99, 97, 108, 32, 78, 32, 61, 32, 116, 111, 110, 117, 109, 98, 101, 114, 40, 97, 114, 103, 32, 97, 110, 100, 32, 97, 114, 103, 91, 49, 93, 41, 32, 111, 114, 32, 52, 10, 10, 105, 102, 32, 78, 32, 61, 61, 32, 48, 32, 116, 104, 101, 110, 10, 32, 32, 78, 32, 61, 32, 48, 10, 101, 108, 115, 101, 105, 102, 32, 78, 32, 61, 61, 32, 49, 32, 116, 104, 101, 110, 10, 32, 32, 78, 32, 61, 32, 57, 46, 53, 10, 101, 108, 115, 101, 105, 102, 32, 78, 32, 61, 61, 32, 50, 32, 116, 104, 101, 110, 10, 32, 32, 78, 32, 61, 32, 49, 49, 46, 57, 57, 10, 101, 108, 115, 101, 105, 102, 32, 78, 32, 61, 61, 32, 51, 32, 116, 104, 101, 110, 10, 32, 32, 78, 32, 61, 32, 49, 50, 46, 56, 53, 10, 101, 108, 115, 101, 105, 102, 32, 78, 32, 61, 61, 32, 52, 32, 116, 104, 101, 110, 10, 32, 32, 78, 32, 61, 32, 49, 52, 46, 55, 50, 10, 101, 108, 115, 101, 105, 102, 32, 78, 32, 61, 61, 32, 53, 32, 116, 104, 101, 110, 10, 32, 32, 78, 32, 61, 32, 49, 53, 46, 56, 50, 10, 101, 110, 100, 10, 10, 108, 111, 99, 97, 108, 32, 109, 105, 110, 100, 101, 112, 116, 104, 32, 61, 32, 52, 10, 108, 111, 99, 97, 108, 32, 109, 97, 120, 100, 101, 112, 116, 104, 32, 61, 32, 109, 105, 110, 100, 101, 112, 116, 104, 32, 43, 32, 50, 10, 105, 102, 32, 109, 97, 120, 100, 101, 112, 116, 104, 32, 60, 32, 78, 32, 116, 104, 101, 110, 32, 109, 97, 120, 100, 101, 112, 116, 104, 32, 61, 32, 78, 32, 101, 110, 100, 10, 10, 100, 111, 10, 32, 32, 108, 111, 99, 97, 108, 32, 115, 116, 114, 101, 116, 99, 104, 100, 101, 112, 116, 104, 32, 61, 32, 109, 97, 120, 100, 101, 112, 116, 104, 32, 43, 32, 49, 10, 32, 32, 108, 111, 99, 97, 108, 32, 115, 116, 114, 101, 116, 99, 104, 116, 114, 101, 101, 32, 61, 32, 66, 111, 116, 116, 111, 109, 85, 112, 84, 114, 101, 101, 40, 48, 44, 32, 115, 116, 114, 101, 116, 99, 104, 100, 101, 112, 116, 104, 41, 10, 32, 32, 105, 111, 46, 119, 114, 105, 116, 101, 40, 115, 116, 114, 105, 110, 103, 46, 102, 111, 114, 109, 97, 116, 40, 34, 115, 116, 114, 101, 116, 99, 104, 32, 116, 114, 101, 101, 32, 111, 102, 32, 100, 101, 112, 116, 104, 32, 37, 100, 92, 116, 32, 99, 104, 101, 99, 107, 58, 32, 37, 100, 92, 110, 34, 44, 10, 32, 32, 32, 32, 115, 116, 114, 101, 116, 99, 104, 100, 101, 112, 116, 104, 44, 32, 73, 116, 101, 109, 67, 104, 101, 99, 107, 40, 115, 116, 114, 101, 116, 99, 104, 116, 114, 101, 101, 41, 41, 41, 10, 101, 110, 100, 10, 10, 108, 111, 99, 97, 108, 32, 108, 111, 110, 103, 108, 105, 118, 101, 100, 116, 114, 101, 101, 32, 61, 32, 66, 111, 116, 116, 111, 109, 85, 112, 84, 114, 101, 101, 40, 48, 44, 32, 109, 97, 120, 100, 101, 112, 116, 104, 41, 10, 10, 102, 111, 114, 32, 100, 101, 112, 116, 104, 61, 109, 105, 110, 100, 101, 112, 116, 104, 44, 109, 97, 120, 100, 101, 112, 116, 104, 44, 50, 32, 100, 111, 10, 32, 32, 108, 111, 99, 97, 108, 32, 105, 116, 101, 114, 97, 116, 105, 111, 110, 115, 32, 61, 32, 50, 32, 94, 32, 40, 109, 97, 120, 100, 101, 112, 116, 104, 32, 45, 32, 100, 101, 112, 116, 104, 32, 43, 32, 109, 105, 110, 100, 101, 112, 116, 104, 41, 10, 32, 32, 108, 111, 99, 97, 108, 32, 99, 104, 101, 99, 107, 32, 61, 32, 48, 10, 32, 32, 102, 111, 114, 32, 105, 61, 49, 44, 105, 116, 101, 114, 97, 116, 105, 111, 110, 115, 32, 100, 111, 10, 32, 32, 32, 32, 99, 104, 101, 99, 107, 32, 61, 32, 99, 104, 101, 99, 107, 32, 43, 32, 73, 116, 101, 109, 67, 104, 101, 99, 107, 40, 66, 111, 116, 116, 111, 109, 85, 112, 84, 114, 101, 101, 40, 49, 44, 32, 100, 101, 112, 116, 104, 41, 41, 32, 43, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 73, 116, 101, 109, 67, 104, 101, 99, 107, 40, 66, 111, 116, 116, 111, 109, 85, 112, 84, 114, 101, 101, 40, 45, 49, 44, 32, 100, 101, 112, 116, 104, 41, 41, 10, 32, 32, 101, 110, 100, 10, 32, 32, 105, 111, 46, 119, 114, 105, 116, 101, 40, 115, 116, 114, 105, 110, 103, 46, 102, 111, 114, 109, 97, 116, 40, 34, 37, 100, 92, 116, 32, 116, 114, 101, 101, 115, 32, 111, 102, 32, 100, 101, 112, 116, 104, 32, 37, 100, 92, 116, 32, 99, 104, 101, 99, 107, 58, 32, 37, 100, 92, 110, 34, 44, 10, 32, 32, 32, 32, 105, 116, 101, 114, 97, 116, 105, 111, 110, 115, 42, 50, 44, 32, 100, 101, 112, 116, 104, 44, 32, 99, 104, 101, 99, 107, 41, 41, 10, 101, 110, 100, 10, 10, 105, 111, 46, 119, 114, 105, 116, 101, 40, 115, 116, 114, 105, 110, 103, 46, 102, 111, 114, 109, 97, 116, 40, 34, 108, 111, 110, 103, 32, 108, 105, 118, 101, 100, 32, 116, 114, 101, 101, 32, 111, 102, 32, 100, 101, 112, 116, 104, 32, 37, 100, 92, 116, 32, 99, 104, 101, 99, 107, 58, 32, 37, 100, 92, 110, 34, 44, 10, 32, 32, 109, 97, 120, 100, 101, 112, 116, 104, 44, 32, 73, 116, 101, 109, 67, 104, 101, 99, 107, 40, 108, 111, 110, 103, 108, 105, 118, 101, 100, 116, 114, 101, 101, 41, 41, 41, 10], true, true);
  31. }
  32. if (Module['calledRun']) {
  33. runWithFS();
  34. } else {
  35. if (!Module['preRun']) Module['preRun'] = [];
  36. Module["preRun"].push(runWithFS); // FS is not initialized yet, wait for it
  37. }
  38. })();
  39. // The Module object: Our interface to the outside world. We import
  40. // and export values on it, and do the work to get that through
  41. // closure compiler if necessary. There are various ways Module can be used:
  42. // 1. Not defined. We create it here
  43. // 2. A function parameter, function(Module) { ..generated code.. }
  44. // 3. pre-run appended it, var Module = {}; ..generated code..
  45. // 4. External script tag defines var Module.
  46. // We need to do an eval in order to handle the closure compiler
  47. // case, where this code here is minified but Module was defined
  48. // elsewhere (e.g. case 4 above). We also need to check if Module
  49. // already exists (e.g. case 3 above).
  50. // Note that if you want to run closure, and also to use Module
  51. // after the generated code, you will need to define var Module = {};
  52. // before the code. Then that object will be used in the code, and you
  53. // can continue to use Module afterwards as well.
  54. var Module;
  55. if (!Module) Module = (typeof Module !== 'undefined' ? Module : null) || {};
  56. // Sometimes an existing Module object exists with properties
  57. // meant to overwrite the default module functionality. Here
  58. // we collect those properties and reapply _after_ we configure
  59. // the current environment's defaults to avoid having to be so
  60. // defensive during initialization.
  61. var moduleOverrides = {};
  62. for (var key in Module) {
  63. if (Module.hasOwnProperty(key)) {
  64. moduleOverrides[key] = Module[key];
  65. }
  66. }
  67. // The environment setup code below is customized to use Module.
  68. // *** Environment setup code ***
  69. var ENVIRONMENT_IS_NODE = typeof process === 'object' && typeof require === 'function';
  70. var ENVIRONMENT_IS_WEB = typeof window === 'object';
  71. var ENVIRONMENT_IS_WORKER = typeof importScripts === 'function';
  72. var ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;
  73. if (ENVIRONMENT_IS_NODE) {
  74. // Expose functionality in the same simple way that the shells work
  75. // Note that we pollute the global namespace here, otherwise we break in node
  76. if (!Module['print']) Module['print'] = function print(x) {
  77. process['stdout'].write(x + '\n');
  78. };
  79. if (!Module['printErr']) Module['printErr'] = function printErr(x) {
  80. process['stderr'].write(x + '\n');
  81. };
  82. var nodeFS = require('fs');
  83. var nodePath = require('path');
  84. Module['read'] = function read(filename, binary) {
  85. filename = nodePath['normalize'](filename);
  86. var ret = nodeFS['readFileSync'](filename);
  87. // The path is absolute if the normalized version is the same as the resolved.
  88. if (!ret && filename != nodePath['resolve'](filename)) {
  89. filename = path.join(__dirname, '..', 'src', filename);
  90. ret = nodeFS['readFileSync'](filename);
  91. }
  92. if (ret && !binary) ret = ret.toString();
  93. return ret;
  94. };
  95. Module['readBinary'] = function readBinary(filename) { return Module['read'](filename, true) };
  96. Module['load'] = function load(f) {
  97. globalEval(read(f));
  98. };
  99. Module['arguments'] = process['argv'].slice(2);
  100. module['exports'] = Module;
  101. }
  102. else if (ENVIRONMENT_IS_SHELL) {
  103. if (!Module['print']) Module['print'] = print;
  104. if (typeof printErr != 'undefined') Module['printErr'] = printErr; // not present in v8 or older sm
  105. if (typeof read != 'undefined') {
  106. Module['read'] = read;
  107. } else {
  108. Module['read'] = function read() { throw 'no read() available (jsc?)' };
  109. }
  110. Module['readBinary'] = function readBinary(f) {
  111. return read(f, 'binary');
  112. };
  113. if (typeof scriptArgs != 'undefined') {
  114. Module['arguments'] = scriptArgs;
  115. } else if (typeof arguments != 'undefined') {
  116. Module['arguments'] = arguments;
  117. }
  118. this['Module'] = Module;
  119. eval("if (typeof gc === 'function' && gc.toString().indexOf('[native code]') > 0) var gc = undefined"); // wipe out the SpiderMonkey shell 'gc' function, which can confuse closure (uses it as a minified name, and it is then initted to a non-falsey value unexpectedly)
  120. }
  121. else if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) {
  122. Module['read'] = function read(url) {
  123. var xhr = new XMLHttpRequest();
  124. xhr.open('GET', url, false);
  125. xhr.send(null);
  126. return xhr.responseText;
  127. };
  128. if (typeof arguments != 'undefined') {
  129. Module['arguments'] = arguments;
  130. }
  131. if (typeof console !== 'undefined') {
  132. if (!Module['print']) Module['print'] = function print(x) {
  133. console.log(x);
  134. };
  135. if (!Module['printErr']) Module['printErr'] = function printErr(x) {
  136. console.log(x);
  137. };
  138. } else {
  139. // Probably a worker, and without console.log. We can do very little here...
  140. var TRY_USE_DUMP = false;
  141. if (!Module['print']) Module['print'] = (TRY_USE_DUMP && (typeof(dump) !== "undefined") ? (function(x) {
  142. dump(x);
  143. }) : (function(x) {
  144. // self.postMessage(x); // enable this if you want stdout to be sent as messages
  145. }));
  146. }
  147. if (ENVIRONMENT_IS_WEB) {
  148. window['Module'] = Module;
  149. } else {
  150. Module['load'] = importScripts;
  151. }
  152. }
  153. else {
  154. // Unreachable because SHELL is dependant on the others
  155. throw 'Unknown runtime environment. Where are we?';
  156. }
  157. function globalEval(x) {
  158. eval.call(null, x);
  159. }
  160. if (!Module['load'] == 'undefined' && Module['read']) {
  161. Module['load'] = function load(f) {
  162. globalEval(Module['read'](f));
  163. };
  164. }
  165. if (!Module['print']) {
  166. Module['print'] = function(){};
  167. }
  168. if (!Module['printErr']) {
  169. Module['printErr'] = Module['print'];
  170. }
  171. if (!Module['arguments']) {
  172. Module['arguments'] = [];
  173. }
  174. // *** Environment setup code ***
  175. // Closure helpers
  176. Module.print = Module['print'];
  177. Module.printErr = Module['printErr'];
  178. // Callbacks
  179. Module['preRun'] = [];
  180. Module['postRun'] = [];
  181. // Merge back in the overrides
  182. for (var key in moduleOverrides) {
  183. if (moduleOverrides.hasOwnProperty(key)) {
  184. Module[key] = moduleOverrides[key];
  185. }
  186. }
  187. // === Auto-generated preamble library stuff ===
  188. //========================================
  189. // Runtime code shared with compiler
  190. //========================================
  191. var Runtime = {
  192. stackSave: function () {
  193. return STACKTOP;
  194. },
  195. stackRestore: function (stackTop) {
  196. STACKTOP = stackTop;
  197. },
  198. forceAlign: function (target, quantum) {
  199. quantum = quantum || 4;
  200. if (quantum == 1) return target;
  201. if (isNumber(target) && isNumber(quantum)) {
  202. return Math.ceil(target/quantum)*quantum;
  203. } else if (isNumber(quantum) && isPowerOfTwo(quantum)) {
  204. return '(((' +target + ')+' + (quantum-1) + ')&' + -quantum + ')';
  205. }
  206. return 'Math.ceil((' + target + ')/' + quantum + ')*' + quantum;
  207. },
  208. isNumberType: function (type) {
  209. return type in Runtime.INT_TYPES || type in Runtime.FLOAT_TYPES;
  210. },
  211. isPointerType: function isPointerType(type) {
  212. return type[type.length-1] == '*';
  213. },
  214. isStructType: function isStructType(type) {
  215. if (isPointerType(type)) return false;
  216. if (isArrayType(type)) return true;
  217. if (/<?\{ ?[^}]* ?\}>?/.test(type)) return true; // { i32, i8 } etc. - anonymous struct types
  218. // See comment in isStructPointerType()
  219. return type[0] == '%';
  220. },
  221. INT_TYPES: {"i1":0,"i8":0,"i16":0,"i32":0,"i64":0},
  222. FLOAT_TYPES: {"float":0,"double":0},
  223. or64: function (x, y) {
  224. var l = (x | 0) | (y | 0);
  225. var h = (Math.round(x / 4294967296) | Math.round(y / 4294967296)) * 4294967296;
  226. return l + h;
  227. },
  228. and64: function (x, y) {
  229. var l = (x | 0) & (y | 0);
  230. var h = (Math.round(x / 4294967296) & Math.round(y / 4294967296)) * 4294967296;
  231. return l + h;
  232. },
  233. xor64: function (x, y) {
  234. var l = (x | 0) ^ (y | 0);
  235. var h = (Math.round(x / 4294967296) ^ Math.round(y / 4294967296)) * 4294967296;
  236. return l + h;
  237. },
  238. getNativeTypeSize: function (type) {
  239. switch (type) {
  240. case 'i1': case 'i8': return 1;
  241. case 'i16': return 2;
  242. case 'i32': return 4;
  243. case 'i64': return 8;
  244. case 'float': return 4;
  245. case 'double': return 8;
  246. default: {
  247. if (type[type.length-1] === '*') {
  248. return Runtime.QUANTUM_SIZE; // A pointer
  249. } else if (type[0] === 'i') {
  250. var bits = parseInt(type.substr(1));
  251. assert(bits % 8 === 0);
  252. return bits/8;
  253. } else {
  254. return 0;
  255. }
  256. }
  257. }
  258. },
  259. getNativeFieldSize: function (type) {
  260. return Math.max(Runtime.getNativeTypeSize(type), Runtime.QUANTUM_SIZE);
  261. },
  262. dedup: function dedup(items, ident) {
  263. var seen = {};
  264. if (ident) {
  265. return items.filter(function(item) {
  266. if (seen[item[ident]]) return false;
  267. seen[item[ident]] = true;
  268. return true;
  269. });
  270. } else {
  271. return items.filter(function(item) {
  272. if (seen[item]) return false;
  273. seen[item] = true;
  274. return true;
  275. });
  276. }
  277. },
  278. set: function set() {
  279. var args = typeof arguments[0] === 'object' ? arguments[0] : arguments;
  280. var ret = {};
  281. for (var i = 0; i < args.length; i++) {
  282. ret[args[i]] = 0;
  283. }
  284. return ret;
  285. },
  286. STACK_ALIGN: 8,
  287. getAlignSize: function (type, size, vararg) {
  288. // we align i64s and doubles on 64-bit boundaries, unlike x86
  289. if (!vararg && (type == 'i64' || type == 'double')) return 8;
  290. if (!type) return Math.min(size, 8); // align structures internally to 64 bits
  291. return Math.min(size || (type ? Runtime.getNativeFieldSize(type) : 0), Runtime.QUANTUM_SIZE);
  292. },
  293. calculateStructAlignment: function calculateStructAlignment(type) {
  294. type.flatSize = 0;
  295. type.alignSize = 0;
  296. var diffs = [];
  297. var prev = -1;
  298. var index = 0;
  299. type.flatIndexes = type.fields.map(function(field) {
  300. index++;
  301. var size, alignSize;
  302. if (Runtime.isNumberType(field) || Runtime.isPointerType(field)) {
  303. size = Runtime.getNativeTypeSize(field); // pack char; char; in structs, also char[X]s.
  304. alignSize = Runtime.getAlignSize(field, size);
  305. } else if (Runtime.isStructType(field)) {
  306. if (field[1] === '0') {
  307. // this is [0 x something]. When inside another structure like here, it must be at the end,
  308. // and it adds no size
  309. // XXX this happens in java-nbody for example... assert(index === type.fields.length, 'zero-length in the middle!');
  310. size = 0;
  311. if (Types.types[field]) {
  312. alignSize = Runtime.getAlignSize(null, Types.types[field].alignSize);
  313. } else {
  314. alignSize = type.alignSize || QUANTUM_SIZE;
  315. }
  316. } else {
  317. size = Types.types[field].flatSize;
  318. alignSize = Runtime.getAlignSize(null, Types.types[field].alignSize);
  319. }
  320. } else if (field[0] == 'b') {
  321. // bN, large number field, like a [N x i8]
  322. size = field.substr(1)|0;
  323. alignSize = 1;
  324. } else if (field[0] === '<') {
  325. // vector type
  326. size = alignSize = Types.types[field].flatSize; // fully aligned
  327. } else if (field[0] === 'i') {
  328. // illegal integer field, that could not be legalized because it is an internal structure field
  329. // it is ok to have such fields, if we just use them as markers of field size and nothing more complex
  330. size = alignSize = parseInt(field.substr(1))/8;
  331. assert(size % 1 === 0, 'cannot handle non-byte-size field ' + field);
  332. } else {
  333. assert(false, 'invalid type for calculateStructAlignment');
  334. }
  335. if (type.packed) alignSize = 1;
  336. type.alignSize = Math.max(type.alignSize, alignSize);
  337. var curr = Runtime.alignMemory(type.flatSize, alignSize); // if necessary, place this on aligned memory
  338. type.flatSize = curr + size;
  339. if (prev >= 0) {
  340. diffs.push(curr-prev);
  341. }
  342. prev = curr;
  343. return curr;
  344. });
  345. if (type.name_ && type.name_[0] === '[') {
  346. // arrays have 2 elements, so we get the proper difference. then we scale here. that way we avoid
  347. // allocating a potentially huge array for [999999 x i8] etc.
  348. type.flatSize = parseInt(type.name_.substr(1))*type.flatSize/2;
  349. }
  350. type.flatSize = Runtime.alignMemory(type.flatSize, type.alignSize);
  351. if (diffs.length == 0) {
  352. type.flatFactor = type.flatSize;
  353. } else if (Runtime.dedup(diffs).length == 1) {
  354. type.flatFactor = diffs[0];
  355. }
  356. type.needsFlattening = (type.flatFactor != 1);
  357. return type.flatIndexes;
  358. },
  359. generateStructInfo: function (struct, typeName, offset) {
  360. var type, alignment;
  361. if (typeName) {
  362. offset = offset || 0;
  363. type = (typeof Types === 'undefined' ? Runtime.typeInfo : Types.types)[typeName];
  364. if (!type) return null;
  365. if (type.fields.length != struct.length) {
  366. printErr('Number of named fields must match the type for ' + typeName + ': possibly duplicate struct names. Cannot return structInfo');
  367. return null;
  368. }
  369. alignment = type.flatIndexes;
  370. } else {
  371. var type = { fields: struct.map(function(item) { return item[0] }) };
  372. alignment = Runtime.calculateStructAlignment(type);
  373. }
  374. var ret = {
  375. __size__: type.flatSize
  376. };
  377. if (typeName) {
  378. struct.forEach(function(item, i) {
  379. if (typeof item === 'string') {
  380. ret[item] = alignment[i] + offset;
  381. } else {
  382. // embedded struct
  383. var key;
  384. for (var k in item) key = k;
  385. ret[key] = Runtime.generateStructInfo(item[key], type.fields[i], alignment[i]);
  386. }
  387. });
  388. } else {
  389. struct.forEach(function(item, i) {
  390. ret[item[1]] = alignment[i];
  391. });
  392. }
  393. return ret;
  394. },
  395. dynCall: function (sig, ptr, args) {
  396. if (args && args.length) {
  397. if (!args.splice) args = Array.prototype.slice.call(args);
  398. args.splice(0, 0, ptr);
  399. return Module['dynCall_' + sig].apply(null, args);
  400. } else {
  401. return Module['dynCall_' + sig].call(null, ptr);
  402. }
  403. },
  404. functionPointers: [],
  405. addFunction: function (func) {
  406. for (var i = 0; i < Runtime.functionPointers.length; i++) {
  407. if (!Runtime.functionPointers[i]) {
  408. Runtime.functionPointers[i] = func;
  409. return 2*(1 + i);
  410. }
  411. }
  412. throw 'Finished up all reserved function pointers. Use a higher value for RESERVED_FUNCTION_POINTERS.';
  413. },
  414. removeFunction: function (index) {
  415. Runtime.functionPointers[(index-2)/2] = null;
  416. },
  417. getAsmConst: function (code, numArgs) {
  418. // code is a constant string on the heap, so we can cache these
  419. if (!Runtime.asmConstCache) Runtime.asmConstCache = {};
  420. var func = Runtime.asmConstCache[code];
  421. if (func) return func;
  422. var args = [];
  423. for (var i = 0; i < numArgs; i++) {
  424. args.push(String.fromCharCode(36) + i); // $0, $1 etc
  425. }
  426. var source = Pointer_stringify(code);
  427. if (source[0] === '"') {
  428. // tolerate EM_ASM("..code..") even though EM_ASM(..code..) is correct
  429. if (source.indexOf('"', 1) === source.length-1) {
  430. source = source.substr(1, source.length-2);
  431. } else {
  432. // something invalid happened, e.g. EM_ASM("..code($0)..", input)
  433. abort('invalid EM_ASM input |' + source + '|. Please use EM_ASM(..code..) (no quotes) or EM_ASM({ ..code($0).. }, input) (to input values)');
  434. }
  435. }
  436. try {
  437. var evalled = eval('(function(' + args.join(',') + '){ ' + source + ' })'); // new Function does not allow upvars in node
  438. } catch(e) {
  439. Module.printErr('error in executing inline EM_ASM code: ' + e + ' on: \n\n' + source + '\n\nwith args |' + args + '| (make sure to use the right one out of EM_ASM, EM_ASM_ARGS, etc.)');
  440. throw e;
  441. }
  442. return Runtime.asmConstCache[code] = evalled;
  443. },
  444. warnOnce: function (text) {
  445. if (!Runtime.warnOnce.shown) Runtime.warnOnce.shown = {};
  446. if (!Runtime.warnOnce.shown[text]) {
  447. Runtime.warnOnce.shown[text] = 1;
  448. Module.printErr(text);
  449. }
  450. },
  451. funcWrappers: {},
  452. getFuncWrapper: function (func, sig) {
  453. assert(sig);
  454. if (!Runtime.funcWrappers[func]) {
  455. Runtime.funcWrappers[func] = function dynCall_wrapper() {
  456. return Runtime.dynCall(sig, func, arguments);
  457. };
  458. }
  459. return Runtime.funcWrappers[func];
  460. },
  461. UTF8Processor: function () {
  462. var buffer = [];
  463. var needed = 0;
  464. this.processCChar = function (code) {
  465. code = code & 0xFF;
  466. if (buffer.length == 0) {
  467. if ((code & 0x80) == 0x00) { // 0xxxxxxx
  468. return String.fromCharCode(code);
  469. }
  470. buffer.push(code);
  471. if ((code & 0xE0) == 0xC0) { // 110xxxxx
  472. needed = 1;
  473. } else if ((code & 0xF0) == 0xE0) { // 1110xxxx
  474. needed = 2;
  475. } else { // 11110xxx
  476. needed = 3;
  477. }
  478. return '';
  479. }
  480. if (needed) {
  481. buffer.push(code);
  482. needed--;
  483. if (needed > 0) return '';
  484. }
  485. var c1 = buffer[0];
  486. var c2 = buffer[1];
  487. var c3 = buffer[2];
  488. var c4 = buffer[3];
  489. var ret;
  490. if (buffer.length == 2) {
  491. ret = String.fromCharCode(((c1 & 0x1F) << 6) | (c2 & 0x3F));
  492. } else if (buffer.length == 3) {
  493. ret = String.fromCharCode(((c1 & 0x0F) << 12) | ((c2 & 0x3F) << 6) | (c3 & 0x3F));
  494. } else {
  495. // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
  496. var codePoint = ((c1 & 0x07) << 18) | ((c2 & 0x3F) << 12) |
  497. ((c3 & 0x3F) << 6) | (c4 & 0x3F);
  498. ret = String.fromCharCode(
  499. Math.floor((codePoint - 0x10000) / 0x400) + 0xD800,
  500. (codePoint - 0x10000) % 0x400 + 0xDC00);
  501. }
  502. buffer.length = 0;
  503. return ret;
  504. }
  505. this.processJSString = function processJSString(string) {
  506. /* TODO: use TextEncoder when present,
  507. var encoder = new TextEncoder();
  508. encoder['encoding'] = "utf-8";
  509. var utf8Array = encoder['encode'](aMsg.data);
  510. */
  511. string = unescape(encodeURIComponent(string));
  512. var ret = [];
  513. for (var i = 0; i < string.length; i++) {
  514. ret.push(string.charCodeAt(i));
  515. }
  516. return ret;
  517. }
  518. },
  519. getCompilerSetting: function (name) {
  520. throw 'You must build with -s RETAIN_COMPILER_SETTINGS=1 for Runtime.getCompilerSetting or emscripten_get_compiler_setting to work';
  521. },
  522. stackAlloc: function (size) { var ret = STACKTOP;STACKTOP = (STACKTOP + size)|0;STACKTOP = (((STACKTOP)+7)&-8); return ret; },
  523. staticAlloc: function (size) { var ret = STATICTOP;STATICTOP = (STATICTOP + size)|0;STATICTOP = (((STATICTOP)+7)&-8); return ret; },
  524. dynamicAlloc: function (size) { var ret = DYNAMICTOP;DYNAMICTOP = (DYNAMICTOP + size)|0;DYNAMICTOP = (((DYNAMICTOP)+7)&-8); if (DYNAMICTOP >= TOTAL_MEMORY) enlargeMemory();; return ret; },
  525. alignMemory: function (size,quantum) { var ret = size = Math.ceil((size)/(quantum ? quantum : 8))*(quantum ? quantum : 8); return ret; },
  526. makeBigInt: function (low,high,unsigned) { var ret = (unsigned ? ((+((low>>>0)))+((+((high>>>0)))*(+4294967296))) : ((+((low>>>0)))+((+((high|0)))*(+4294967296)))); return ret; },
  527. GLOBAL_BASE: 8,
  528. QUANTUM_SIZE: 4,
  529. __dummy__: 0
  530. }
  531. Module['Runtime'] = Runtime;
  532. //========================================
  533. // Runtime essentials
  534. //========================================
  535. var __THREW__ = 0; // Used in checking for thrown exceptions.
  536. var ABORT = false; // whether we are quitting the application. no code should run after this. set in exit() and abort()
  537. var EXITSTATUS = 0;
  538. var undef = 0;
  539. // tempInt is used for 32-bit signed values or smaller. tempBigInt is used
  540. // for 32-bit unsigned values or more than 32 bits. TODO: audit all uses of tempInt
  541. var tempValue, tempInt, tempBigInt, tempInt2, tempBigInt2, tempPair, tempBigIntI, tempBigIntR, tempBigIntS, tempBigIntP, tempBigIntD, tempDouble, tempFloat;
  542. var tempI64, tempI64b;
  543. var tempRet0, tempRet1, tempRet2, tempRet3, tempRet4, tempRet5, tempRet6, tempRet7, tempRet8, tempRet9;
  544. function assert(condition, text) {
  545. if (!condition) {
  546. abort('Assertion failed: ' + text);
  547. }
  548. }
  549. var globalScope = this;
  550. // C calling interface. A convenient way to call C functions (in C files, or
  551. // defined with extern "C").
  552. //
  553. // Note: LLVM optimizations can inline and remove functions, after which you will not be
  554. // able to call them. Closure can also do so. To avoid that, add your function to
  555. // the exports using something like
  556. //
  557. // -s EXPORTED_FUNCTIONS='["_main", "_myfunc"]'
  558. //
  559. // @param ident The name of the C function (note that C++ functions will be name-mangled - use extern "C")
  560. // @param returnType The return type of the function, one of the JS types 'number', 'string' or 'array' (use 'number' for any C pointer, and
  561. // 'array' for JavaScript arrays and typed arrays; note that arrays are 8-bit).
  562. // @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,
  563. // except that 'array' is not possible (there is no way for us to know the length of the array)
  564. // @param args An array of the arguments to the function, as native JS values (as in returnType)
  565. // Note that string arguments will be stored on the stack (the JS string will become a C string on the stack).
  566. // @return The return value, as a native JS value (as in returnType)
  567. function ccall(ident, returnType, argTypes, args) {
  568. return ccallFunc(getCFunc(ident), returnType, argTypes, args);
  569. }
  570. Module["ccall"] = ccall;
  571. // Returns the C function with a specified identifier (for C++, you need to do manual name mangling)
  572. function getCFunc(ident) {
  573. try {
  574. var func = Module['_' + ident]; // closure exported function
  575. if (!func) func = eval('_' + ident); // explicit lookup
  576. } catch(e) {
  577. }
  578. assert(func, 'Cannot call unknown function ' + ident + ' (perhaps LLVM optimizations or closure removed it?)');
  579. return func;
  580. }
  581. // Internal function that does a C call using a function, not an identifier
  582. function ccallFunc(func, returnType, argTypes, args) {
  583. var stack = 0;
  584. function toC(value, type) {
  585. if (type == 'string') {
  586. if (value === null || value === undefined || value === 0) return 0; // null string
  587. value = intArrayFromString(value);
  588. type = 'array';
  589. }
  590. if (type == 'array') {
  591. if (!stack) stack = Runtime.stackSave();
  592. var ret = Runtime.stackAlloc(value.length);
  593. writeArrayToMemory(value, ret);
  594. return ret;
  595. }
  596. return value;
  597. }
  598. function fromC(value, type) {
  599. if (type == 'string') {
  600. return Pointer_stringify(value);
  601. }
  602. assert(type != 'array');
  603. return value;
  604. }
  605. var i = 0;
  606. var cArgs = args ? args.map(function(arg) {
  607. return toC(arg, argTypes[i++]);
  608. }) : [];
  609. var ret = fromC(func.apply(null, cArgs), returnType);
  610. if (stack) Runtime.stackRestore(stack);
  611. return ret;
  612. }
  613. // Returns a native JS wrapper for a C function. This is similar to ccall, but
  614. // returns a function you can call repeatedly in a normal way. For example:
  615. //
  616. // var my_function = cwrap('my_c_function', 'number', ['number', 'number']);
  617. // alert(my_function(5, 22));
  618. // alert(my_function(99, 12));
  619. //
  620. function cwrap(ident, returnType, argTypes) {
  621. var func = getCFunc(ident);
  622. return function() {
  623. return ccallFunc(func, returnType, argTypes, Array.prototype.slice.call(arguments));
  624. }
  625. }
  626. Module["cwrap"] = cwrap;
  627. // Sets a value in memory in a dynamic way at run-time. Uses the
  628. // type data. This is the same as makeSetValue, except that
  629. // makeSetValue is done at compile-time and generates the needed
  630. // code then, whereas this function picks the right code at
  631. // run-time.
  632. // Note that setValue and getValue only do *aligned* writes and reads!
  633. // Note that ccall uses JS types as for defining types, while setValue and
  634. // getValue need LLVM types ('i8', 'i32') - this is a lower-level operation
  635. function setValue(ptr, value, type, noSafe) {
  636. type = type || 'i8';
  637. if (type.charAt(type.length-1) === '*') type = 'i32'; // pointers are 32-bit
  638. switch(type) {
  639. case 'i1': HEAP8[(ptr)]=value; break;
  640. case 'i8': HEAP8[(ptr)]=value; break;
  641. case 'i16': HEAP16[((ptr)>>1)]=value; break;
  642. case 'i32': HEAP32[((ptr)>>2)]=value; break;
  643. case 'i64': (tempI64 = [value>>>0,(tempDouble=value,(+(Math_abs(tempDouble))) >= (+1) ? (tempDouble > (+0) ? ((Math_min((+(Math_floor((tempDouble)/(+4294967296)))), (+4294967295)))|0)>>>0 : (~~((+(Math_ceil((tempDouble - +(((~~(tempDouble)))>>>0))/(+4294967296))))))>>>0) : 0)],HEAP32[((ptr)>>2)]=tempI64[0],HEAP32[(((ptr)+(4))>>2)]=tempI64[1]); break;
  644. case 'float': HEAPF32[((ptr)>>2)]=value; break;
  645. case 'double': HEAPF64[((ptr)>>3)]=value; break;
  646. default: abort('invalid type for setValue: ' + type);
  647. }
  648. }
  649. Module['setValue'] = setValue;
  650. // Parallel to setValue.
  651. function getValue(ptr, type, noSafe) {
  652. type = type || 'i8';
  653. if (type.charAt(type.length-1) === '*') type = 'i32'; // pointers are 32-bit
  654. switch(type) {
  655. case 'i1': return HEAP8[(ptr)];
  656. case 'i8': return HEAP8[(ptr)];
  657. case 'i16': return HEAP16[((ptr)>>1)];
  658. case 'i32': return HEAP32[((ptr)>>2)];
  659. case 'i64': return HEAP32[((ptr)>>2)];
  660. case 'float': return HEAPF32[((ptr)>>2)];
  661. case 'double': return HEAPF64[((ptr)>>3)];
  662. default: abort('invalid type for setValue: ' + type);
  663. }
  664. return null;
  665. }
  666. Module['getValue'] = getValue;
  667. var ALLOC_NORMAL = 0; // Tries to use _malloc()
  668. var ALLOC_STACK = 1; // Lives for the duration of the current function call
  669. var ALLOC_STATIC = 2; // Cannot be freed
  670. var ALLOC_DYNAMIC = 3; // Cannot be freed except through sbrk
  671. var ALLOC_NONE = 4; // Do not allocate
  672. Module['ALLOC_NORMAL'] = ALLOC_NORMAL;
  673. Module['ALLOC_STACK'] = ALLOC_STACK;
  674. Module['ALLOC_STATIC'] = ALLOC_STATIC;
  675. Module['ALLOC_DYNAMIC'] = ALLOC_DYNAMIC;
  676. Module['ALLOC_NONE'] = ALLOC_NONE;
  677. // allocate(): This is for internal use. You can use it yourself as well, but the interface
  678. // is a little tricky (see docs right below). The reason is that it is optimized
  679. // for multiple syntaxes to save space in generated code. So you should
  680. // normally not use allocate(), and instead allocate memory using _malloc(),
  681. // initialize it with setValue(), and so forth.
  682. // @slab: An array of data, or a number. If a number, then the size of the block to allocate,
  683. // in *bytes* (note that this is sometimes confusing: the next parameter does not
  684. // affect this!)
  685. // @types: Either an array of types, one for each byte (or 0 if no type at that position),
  686. // or a single type which is used for the entire block. This only matters if there
  687. // is initial data - if @slab is a number, then this does not matter at all and is
  688. // ignored.
  689. // @allocator: How to allocate memory, see ALLOC_*
  690. function allocate(slab, types, allocator, ptr) {
  691. var zeroinit, size;
  692. if (typeof slab === 'number') {
  693. zeroinit = true;
  694. size = slab;
  695. } else {
  696. zeroinit = false;
  697. size = slab.length;
  698. }
  699. var singleType = typeof types === 'string' ? types : null;
  700. var ret;
  701. if (allocator == ALLOC_NONE) {
  702. ret = ptr;
  703. } else {
  704. ret = [_malloc, Runtime.stackAlloc, Runtime.staticAlloc, Runtime.dynamicAlloc][allocator === undefined ? ALLOC_STATIC : allocator](Math.max(size, singleType ? 1 : types.length));
  705. }
  706. if (zeroinit) {
  707. var ptr = ret, stop;
  708. assert((ret & 3) == 0);
  709. stop = ret + (size & ~3);
  710. for (; ptr < stop; ptr += 4) {
  711. HEAP32[((ptr)>>2)]=0;
  712. }
  713. stop = ret + size;
  714. while (ptr < stop) {
  715. HEAP8[((ptr++)|0)]=0;
  716. }
  717. return ret;
  718. }
  719. if (singleType === 'i8') {
  720. if (slab.subarray || slab.slice) {
  721. HEAPU8.set(slab, ret);
  722. } else {
  723. HEAPU8.set(new Uint8Array(slab), ret);
  724. }
  725. return ret;
  726. }
  727. var i = 0, type, typeSize, previousType;
  728. while (i < size) {
  729. var curr = slab[i];
  730. if (typeof curr === 'function') {
  731. curr = Runtime.getFunctionIndex(curr);
  732. }
  733. type = singleType || types[i];
  734. if (type === 0) {
  735. i++;
  736. continue;
  737. }
  738. if (type == 'i64') type = 'i32'; // special case: we have one i32 here, and one i32 later
  739. setValue(ret+i, curr, type);
  740. // no need to look up size unless type changes, so cache it
  741. if (previousType !== type) {
  742. typeSize = Runtime.getNativeTypeSize(type);
  743. previousType = type;
  744. }
  745. i += typeSize;
  746. }
  747. return ret;
  748. }
  749. Module['allocate'] = allocate;
  750. function Pointer_stringify(ptr, /* optional */ length) {
  751. // TODO: use TextDecoder
  752. // Find the length, and check for UTF while doing so
  753. var hasUtf = false;
  754. var t;
  755. var i = 0;
  756. while (1) {
  757. t = HEAPU8[(((ptr)+(i))|0)];
  758. if (t >= 128) hasUtf = true;
  759. else if (t == 0 && !length) break;
  760. i++;
  761. if (length && i == length) break;
  762. }
  763. if (!length) length = i;
  764. var ret = '';
  765. if (!hasUtf) {
  766. var MAX_CHUNK = 1024; // split up into chunks, because .apply on a huge string can overflow the stack
  767. var curr;
  768. while (length > 0) {
  769. curr = String.fromCharCode.apply(String, HEAPU8.subarray(ptr, ptr + Math.min(length, MAX_CHUNK)));
  770. ret = ret ? ret + curr : curr;
  771. ptr += MAX_CHUNK;
  772. length -= MAX_CHUNK;
  773. }
  774. return ret;
  775. }
  776. var utf8 = new Runtime.UTF8Processor();
  777. for (i = 0; i < length; i++) {
  778. t = HEAPU8[(((ptr)+(i))|0)];
  779. ret += utf8.processCChar(t);
  780. }
  781. return ret;
  782. }
  783. Module['Pointer_stringify'] = Pointer_stringify;
  784. // Given a pointer 'ptr' to a null-terminated UTF16LE-encoded string in the emscripten HEAP, returns
  785. // a copy of that string as a Javascript String object.
  786. function UTF16ToString(ptr) {
  787. var i = 0;
  788. var str = '';
  789. while (1) {
  790. var codeUnit = HEAP16[(((ptr)+(i*2))>>1)];
  791. if (codeUnit == 0)
  792. return str;
  793. ++i;
  794. // fromCharCode constructs a character from a UTF-16 code unit, so we can pass the UTF16 string right through.
  795. str += String.fromCharCode(codeUnit);
  796. }
  797. }
  798. Module['UTF16ToString'] = UTF16ToString;
  799. // Copies the given Javascript String object 'str' to the emscripten HEAP at address 'outPtr',
  800. // null-terminated and encoded in UTF16LE form. The copy will require at most (str.length*2+1)*2 bytes of space in the HEAP.
  801. function stringToUTF16(str, outPtr) {
  802. for(var i = 0; i < str.length; ++i) {
  803. // charCodeAt returns a UTF-16 encoded code unit, so it can be directly written to the HEAP.
  804. var codeUnit = str.charCodeAt(i); // possibly a lead surrogate
  805. HEAP16[(((outPtr)+(i*2))>>1)]=codeUnit;
  806. }
  807. // Null-terminate the pointer to the HEAP.
  808. HEAP16[(((outPtr)+(str.length*2))>>1)]=0;
  809. }
  810. Module['stringToUTF16'] = stringToUTF16;
  811. // Given a pointer 'ptr' to a null-terminated UTF32LE-encoded string in the emscripten HEAP, returns
  812. // a copy of that string as a Javascript String object.
  813. function UTF32ToString(ptr) {
  814. var i = 0;
  815. var str = '';
  816. while (1) {
  817. var utf32 = HEAP32[(((ptr)+(i*4))>>2)];
  818. if (utf32 == 0)
  819. return str;
  820. ++i;
  821. // 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.
  822. if (utf32 >= 0x10000) {
  823. var ch = utf32 - 0x10000;
  824. str += String.fromCharCode(0xD800 | (ch >> 10), 0xDC00 | (ch & 0x3FF));
  825. } else {
  826. str += String.fromCharCode(utf32);
  827. }
  828. }
  829. }
  830. Module['UTF32ToString'] = UTF32ToString;
  831. // Copies the given Javascript String object 'str' to the emscripten HEAP at address 'outPtr',
  832. // null-terminated and encoded in UTF32LE form. The copy will require at most (str.length+1)*4 bytes of space in the HEAP,
  833. // 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.
  834. function stringToUTF32(str, outPtr) {
  835. var iChar = 0;
  836. for(var iCodeUnit = 0; iCodeUnit < str.length; ++iCodeUnit) {
  837. // 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.
  838. var codeUnit = str.charCodeAt(iCodeUnit); // possibly a lead surrogate
  839. if (codeUnit >= 0xD800 && codeUnit <= 0xDFFF) {
  840. var trailSurrogate = str.charCodeAt(++iCodeUnit);
  841. codeUnit = 0x10000 + ((codeUnit & 0x3FF) << 10) | (trailSurrogate & 0x3FF);
  842. }
  843. HEAP32[(((outPtr)+(iChar*4))>>2)]=codeUnit;
  844. ++iChar;
  845. }
  846. // Null-terminate the pointer to the HEAP.
  847. HEAP32[(((outPtr)+(iChar*4))>>2)]=0;
  848. }
  849. Module['stringToUTF32'] = stringToUTF32;
  850. function demangle(func) {
  851. var i = 3;
  852. // params, etc.
  853. var basicTypes = {
  854. 'v': 'void',
  855. 'b': 'bool',
  856. 'c': 'char',
  857. 's': 'short',
  858. 'i': 'int',
  859. 'l': 'long',
  860. 'f': 'float',
  861. 'd': 'double',
  862. 'w': 'wchar_t',
  863. 'a': 'signed char',
  864. 'h': 'unsigned char',
  865. 't': 'unsigned short',
  866. 'j': 'unsigned int',
  867. 'm': 'unsigned long',
  868. 'x': 'long long',
  869. 'y': 'unsigned long long',
  870. 'z': '...'
  871. };
  872. var subs = [];
  873. var first = true;
  874. function dump(x) {
  875. //return;
  876. if (x) Module.print(x);
  877. Module.print(func);
  878. var pre = '';
  879. for (var a = 0; a < i; a++) pre += ' ';
  880. Module.print (pre + '^');
  881. }
  882. function parseNested() {
  883. i++;
  884. if (func[i] === 'K') i++; // ignore const
  885. var parts = [];
  886. while (func[i] !== 'E') {
  887. if (func[i] === 'S') { // substitution
  888. i++;
  889. var next = func.indexOf('_', i);
  890. var num = func.substring(i, next) || 0;
  891. parts.push(subs[num] || '?');
  892. i = next+1;
  893. continue;
  894. }
  895. if (func[i] === 'C') { // constructor
  896. parts.push(parts[parts.length-1]);
  897. i += 2;
  898. continue;
  899. }
  900. var size = parseInt(func.substr(i));
  901. var pre = size.toString().length;
  902. if (!size || !pre) { i--; break; } // counter i++ below us
  903. var curr = func.substr(i + pre, size);
  904. parts.push(curr);
  905. subs.push(curr);
  906. i += pre + size;
  907. }
  908. i++; // skip E
  909. return parts;
  910. }
  911. function parse(rawList, limit, allowVoid) { // main parser
  912. limit = limit || Infinity;
  913. var ret = '', list = [];
  914. function flushList() {
  915. return '(' + list.join(', ') + ')';
  916. }
  917. var name;
  918. if (func[i] === 'N') {
  919. // namespaced N-E
  920. name = parseNested().join('::');
  921. limit--;
  922. if (limit === 0) return rawList ? [name] : name;
  923. } else {
  924. // not namespaced
  925. if (func[i] === 'K' || (first && func[i] === 'L')) i++; // ignore const and first 'L'
  926. var size = parseInt(func.substr(i));
  927. if (size) {
  928. var pre = size.toString().length;
  929. name = func.substr(i + pre, size);
  930. i += pre + size;
  931. }
  932. }
  933. first = false;
  934. if (func[i] === 'I') {
  935. i++;
  936. var iList = parse(true);
  937. var iRet = parse(true, 1, true);
  938. ret += iRet[0] + ' ' + name + '<' + iList.join(', ') + '>';
  939. } else {
  940. ret = name;
  941. }
  942. paramLoop: while (i < func.length && limit-- > 0) {
  943. //dump('paramLoop');
  944. var c = func[i++];
  945. if (c in basicTypes) {
  946. list.push(basicTypes[c]);
  947. } else {
  948. switch (c) {
  949. case 'P': list.push(parse(true, 1, true)[0] + '*'); break; // pointer
  950. case 'R': list.push(parse(true, 1, true)[0] + '&'); break; // reference
  951. case 'L': { // literal
  952. i++; // skip basic type
  953. var end = func.indexOf('E', i);
  954. var size = end - i;
  955. list.push(func.substr(i, size));
  956. i += size + 2; // size + 'EE'
  957. break;
  958. }
  959. case 'A': { // array
  960. var size = parseInt(func.substr(i));
  961. i += size.toString().length;
  962. if (func[i] !== '_') throw '?';
  963. i++; // skip _
  964. list.push(parse(true, 1, true)[0] + ' [' + size + ']');
  965. break;
  966. }
  967. case 'E': break paramLoop;
  968. default: ret += '?' + c; break paramLoop;
  969. }
  970. }
  971. }
  972. if (!allowVoid && list.length === 1 && list[0] === 'void') list = []; // avoid (void)
  973. if (rawList) {
  974. if (ret) {
  975. list.push(ret + '?');
  976. }
  977. return list;
  978. } else {
  979. return ret + flushList();
  980. }
  981. }
  982. try {
  983. // Special-case the entry point, since its name differs from other name mangling.
  984. if (func == 'Object._main' || func == '_main') {
  985. return 'main()';
  986. }
  987. if (typeof func === 'number') func = Pointer_stringify(func);
  988. if (func[0] !== '_') return func;
  989. if (func[1] !== '_') return func; // C function
  990. if (func[2] !== 'Z') return func;
  991. switch (func[3]) {
  992. case 'n': return 'operator new()';
  993. case 'd': return 'operator delete()';
  994. }
  995. return parse();
  996. } catch(e) {
  997. return func;
  998. }
  999. }
  1000. function demangleAll(text) {
  1001. return text.replace(/__Z[\w\d_]+/g, function(x) { var y = demangle(x); return x === y ? x : (x + ' [' + y + ']') });
  1002. }
  1003. function stackTrace() {
  1004. var stack = new Error().stack;
  1005. return stack ? demangleAll(stack) : '(no stack trace available)'; // Stack trace is not available at least on IE10 and Safari 6.
  1006. }
  1007. // Memory management
  1008. var PAGE_SIZE = 4096;
  1009. function alignMemoryPage(x) {
  1010. return (x+4095)&-4096;
  1011. }
  1012. var HEAP;
  1013. var HEAP8, HEAPU8, HEAP16, HEAPU16, HEAP32, HEAPU32, HEAPF32, HEAPF64;
  1014. var STATIC_BASE = 0, STATICTOP = 0, staticSealed = false; // static area
  1015. var STACK_BASE = 0, STACKTOP = 0, STACK_MAX = 0; // stack area
  1016. var DYNAMIC_BASE = 0, DYNAMICTOP = 0; // dynamic area handled by sbrk
  1017. function enlargeMemory() {
  1018. 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.');
  1019. }
  1020. var TOTAL_STACK = Module['TOTAL_STACK'] || 5242880;
  1021. var TOTAL_MEMORY = Module['TOTAL_MEMORY'] || 134217728;
  1022. var FAST_MEMORY = Module['FAST_MEMORY'] || 2097152;
  1023. var totalMemory = 4096;
  1024. while (totalMemory < TOTAL_MEMORY || totalMemory < 2*TOTAL_STACK) {
  1025. if (totalMemory < 16*1024*1024) {
  1026. totalMemory *= 2;
  1027. } else {
  1028. totalMemory += 16*1024*1024
  1029. }
  1030. }
  1031. if (totalMemory !== TOTAL_MEMORY) {
  1032. Module.printErr('increasing TOTAL_MEMORY to ' + totalMemory + ' to be more reasonable');
  1033. TOTAL_MEMORY = totalMemory;
  1034. }
  1035. // Initialize the runtime's memory
  1036. // check for full engine support (use string 'subarray' to avoid closure compiler confusion)
  1037. assert(typeof Int32Array !== 'undefined' && typeof Float64Array !== 'undefined' && !!(new Int32Array(1)['subarray']) && !!(new Int32Array(1)['set']),
  1038. 'JS engine does not provide full typed array support');
  1039. var buffer = new ArrayBuffer(TOTAL_MEMORY);
  1040. HEAP8 = new Int8Array(buffer);
  1041. HEAP16 = new Int16Array(buffer);
  1042. HEAP32 = new Int32Array(buffer);
  1043. HEAPU8 = new Uint8Array(buffer);
  1044. HEAPU16 = new Uint16Array(buffer);
  1045. HEAPU32 = new Uint32Array(buffer);
  1046. HEAPF32 = new Float32Array(buffer);
  1047. HEAPF64 = new Float64Array(buffer);
  1048. // Endianness check (note: assumes compiler arch was little-endian)
  1049. HEAP32[0] = 255;
  1050. assert(HEAPU8[0] === 255 && HEAPU8[3] === 0, 'Typed arrays 2 must be run on a little-endian system');
  1051. Module['HEAP'] = HEAP;
  1052. Module['HEAP8'] = HEAP8;
  1053. Module['HEAP16'] = HEAP16;
  1054. Module['HEAP32'] = HEAP32;
  1055. Module['HEAPU8'] = HEAPU8;
  1056. Module['HEAPU16'] = HEAPU16;
  1057. Module['HEAPU32'] = HEAPU32;
  1058. Module['HEAPF32'] = HEAPF32;
  1059. Module['HEAPF64'] = HEAPF64;
  1060. function callRuntimeCallbacks(callbacks) {
  1061. while(callbacks.length > 0) {
  1062. var callback = callbacks.shift();
  1063. if (typeof callback == 'function') {
  1064. callback();
  1065. continue;
  1066. }
  1067. var func = callback.func;
  1068. if (typeof func === 'number') {
  1069. if (callback.arg === undefined) {
  1070. Runtime.dynCall('v', func);
  1071. } else {
  1072. Runtime.dynCall('vi', func, [callback.arg]);
  1073. }
  1074. } else {
  1075. func(callback.arg === undefined ? null : callback.arg);
  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. var runtimeInitialized = false;
  1085. function preRun() {
  1086. // compatibility - merge in anything from Module['preRun'] at this time
  1087. if (Module['preRun']) {
  1088. if (typeof Module['preRun'] == 'function') Module['preRun'] = [Module['preRun']];
  1089. while (Module['preRun'].length) {
  1090. addOnPreRun(Module['preRun'].shift());
  1091. }
  1092. }
  1093. callRuntimeCallbacks(__ATPRERUN__);
  1094. }
  1095. function ensureInitRuntime() {
  1096. if (runtimeInitialized) return;
  1097. runtimeInitialized = true;
  1098. callRuntimeCallbacks(__ATINIT__);
  1099. }
  1100. function preMain() {
  1101. callRuntimeCallbacks(__ATMAIN__);
  1102. }
  1103. function exitRuntime() {
  1104. callRuntimeCallbacks(__ATEXIT__);
  1105. }
  1106. function postRun() {
  1107. // compatibility - merge in anything from Module['postRun'] at this time
  1108. if (Module['postRun']) {
  1109. if (typeof Module['postRun'] == 'function') Module['postRun'] = [Module['postRun']];
  1110. while (Module['postRun'].length) {
  1111. addOnPostRun(Module['postRun'].shift());
  1112. }
  1113. }
  1114. callRuntimeCallbacks(__ATPOSTRUN__);
  1115. }
  1116. function addOnPreRun(cb) {
  1117. __ATPRERUN__.unshift(cb);
  1118. }
  1119. Module['addOnPreRun'] = Module.addOnPreRun = addOnPreRun;
  1120. function addOnInit(cb) {
  1121. __ATINIT__.unshift(cb);
  1122. }
  1123. Module['addOnInit'] = Module.addOnInit = addOnInit;
  1124. function addOnPreMain(cb) {
  1125. __ATMAIN__.unshift(cb);
  1126. }
  1127. Module['addOnPreMain'] = Module.addOnPreMain = addOnPreMain;
  1128. function addOnExit(cb) {
  1129. __ATEXIT__.unshift(cb);
  1130. }
  1131. Module['addOnExit'] = Module.addOnExit = addOnExit;
  1132. function addOnPostRun(cb) {
  1133. __ATPOSTRUN__.unshift(cb);
  1134. }
  1135. Module['addOnPostRun'] = Module.addOnPostRun = addOnPostRun;
  1136. // Tools
  1137. // This processes a JS string into a C-line array of numbers, 0-terminated.
  1138. // For LLVM-originating strings, see parser.js:parseLLVMString function
  1139. function intArrayFromString(stringy, dontAddNull, length /* optional */) {
  1140. var

Large files files are truncated, but you can click here to view the full file