simple django-style substitution/macros

This commit is contained in:
Alon Zakai 2010-12-30 21:42:06 -08:00
Родитель 47f29aed8a
Коммит ff4341e7d5
3 изменённых файлов: 19 добавлений и 20 удалений

Просмотреть файл

@ -110,9 +110,10 @@ function JSify(data, functionsOnly, givenTypes, givenFunctions, givenGlobalVaria
value = indexizeFunctions(value);
var offset = calcFastOffset(ptr, pos, noNeedFirst);
if (SAFE_HEAP) {
return 'SAFE_HEAP_STORE(' + offset + ', ' + value + ', "' + safeQuote(type) + '")';
if (type !== 'null') type = '"' + safeQuote(type) + '"';
return 'SAFE_HEAP_STORE(' + offset + ', ' + value + ', ' + type + ');';
} else {
return makeGetSlabs(ptr, type, true).map(function(slab) { return slab + '[' + offset + '] = ' + value }).join('; ');
return makeGetSlabs(ptr, type, true).map(function(slab) { return slab + '[' + offset + '] = ' + value }).join('; ') + ';';
}
}
@ -964,6 +965,15 @@ function JSify(data, functionsOnly, givenTypes, givenFunctions, givenGlobalVaria
// Final combiner
function finalCombiner(items) {
// Does simple 'macro' substitution, using Django-like syntax,
// {{{ code }}} will be replaced with |eval(code)|.
function processMacros(text) {
return text.replace(/{{{[^}]+}}}/g, function(str) {
str = str.substr(3, str.length-6);
return eval(str).toString();
});
}
var ret = [];
if (!functionsOnly) {
ret = ret.concat(items.filter(function(item) { return item.intertype == 'type' }));
@ -982,8 +992,10 @@ function JSify(data, functionsOnly, givenTypes, givenFunctions, givenGlobalVaria
var body = preprocess(read('preamble.js').replace('{{RUNTIME}}', getRuntime()) + ret + read('postamble.js'), CONSTANTS);
var globalVars = items.filter(function(item) { return item.intertype == 'GlobalVariable' }).map(function(item) { return item.JS }).join('\n');
var globalVarsPostSets = items.filter(function(item) { return item.intertype == 'GlobalVariablePostSet' }).map(function(item) { return item.JS }).join('\n');
return read('shell.js').replace('{{BODY}}', indentify(body, 2))
.replace('{{GLOBAL_VARS}}', indentify(globalVars+'\n\n\n'+globalVarsPostSets, 4));
return processMacros(
read('shell.js').replace('{{BODY}}', indentify(body, 2))
.replace('{{GLOBAL_VARS}}', indentify(globalVars+'\n\n\n'+globalVarsPostSets, 4))
);
}
// Data

Просмотреть файл

@ -2,7 +2,7 @@
// specific to Emscripten (and hence not in utility.js).
// Simple #if/else/endif preprocessing for a file. Checks if the
// ident checked is true in our global. Also replaces some constants
// ident checked is true in our global. Also replaces some constants.
function preprocess(text, constants) {
for (constant in constants) {
text = text.replace(eval('/' + constant + '/g'), constants[constant]);

Просмотреть файл

@ -138,21 +138,8 @@ function Pointer_make(slab, pos, allocator) {
if (typeof curr === 'function') {
curr = Runtime.getFunctionIndex(curr);
}
#if SAFE_HEAP
SAFE_HEAP_STORE(ret + i, curr, null);
#else
#if USE_TYPED_ARRAYS
// TODO: Check - also in non-typedarray case - for functions, and if so add |.__index__|
if (typeof curr === 'number' || typeof curr === 'boolean') {
IHEAP[ret + i] = curr; // TODO: optimize. Can easily detect floats, but 1.0 might look like an int...
FHEAP[ret + i] = curr;
} else {
HEAP[ret + i] = curr;
}
#else
HEAP[ret + i] = curr;
#endif
#endif
{{{ makeSetValue(0, 'ret+i', 'curr', null, 'null') }}}
}
return ret;