/**
* @license
* Copyright 2010 The Emscripten Authors
* SPDX-License-Identifier: MIT
*
* Helpers and tools for use at compile time by JavaScript library files.
*
* Tests live in test/other/test_parseTools.js.
*/
global.FOUR_GB = 4 * 1024 * 1024 * 1024;
const FLOAT_TYPES = new Set(['float', 'double']);
// Does simple 'macro' substitution, using Django-like syntax,
// {{{ code }}} will be replaced with |eval(code)|.
// NOTE: Be careful with that ret check. If ret is |0|, |ret ? ret.toString() : ''| would result in ''!
function processMacros(text) {
// The `?` here in makes the regex non-greedy so it matches with the closest
// set of closing braces.
// `[\s\S]` works like `.` but include newline.
return text.replace(/{{{([\s\S]+?)}}}/g, (_, str) => {
try {
const ret = eval(str);
return ret !== null ? ret.toString() : '';
} catch (ex) {
ex.stack = 'In the following macro:\n\n' + str + '\n\n' + ex.stack;
throw ex;
}
});
}
// Simple #if/else/endif preprocessing for a file. Checks if the
// ident checked is true in our global.
// Also handles #include x.js (similar to C #include )
function preprocess(filename) {
let text = read(filename);
if (EXPORT_ES6 && USE_ES6_IMPORT_META) {
// `eval`, Terser and Closure don't support module syntax; to allow it,
// we need to temporarily replace `import.meta` and `await import` usages
// with placeholders during preprocess phase, and back after all the other ops.
// See also: `phase_final_emitting` in emcc.py.
text = text
.replace(/\bimport\.meta\b/g, 'EMSCRIPTEN$IMPORT$META')
.replace(/\bawait import\b/g, 'EMSCRIPTEN$AWAIT$IMPORT');
}
// Remove windows line endings, if any
text = text.replace(/\r\n/g, '\n');
const IGNORE = 0;
const SHOW = 1;
// This state is entered after we have shown one of the block of an if/elif/else sequence.
// Once we enter this state we dont show any blocks or evaluate any
// conditions until the sequence ends.
const IGNORE_ALL = 2;
const showStack = [];
const showCurrentLine = () => showStack.every((x) => x == SHOW);
const oldFilename = currentFile;
currentFile = filename;
const fileExt = filename.split('.').pop().toLowerCase();
const isHtml = (fileExt === 'html' || fileExt === 'htm') ? true : false;
let inStyle = false;
const lines = text.split('\n');
let ret = '';
let emptyLine = false;
try {
for (let [i, line] of lines.entries()) {
if (isHtml) {
if (line.includes('