diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-04-03 18:57:57 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-04-03 18:57:57 -0700 |
commit | 927355084b711b8aaf54668ab791a4693cf45aaf (patch) | |
tree | e3cda277d19c6d29d17197ca918c429fcf0f1ef7 | |
parent | ecb57d53441c7c9670173ae4d2d094ab7b606665 (diff) |
refactor/simplify constant generation
-rw-r--r-- | src/jsifier.js | 40 |
1 files changed, 17 insertions, 23 deletions
diff --git a/src/jsifier.js b/src/jsifier.js index c9476647..97a25188 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -231,12 +231,11 @@ function JSify(data, functionsOnly, givenFunctions) { if (value.intertype in PARSABLE_LLVM_FUNCTIONS) { return [finalizeLLVMFunctionCall(value)]; } else if (Runtime.isNumberType(type) || pointingLevels(type) >= 1) { - return makeGlobalUse(indexizeFunctions(parseNumerical(value.value), type)); + return [makeGlobalUse(indexizeFunctions(parseNumerical(value.value), type))]; } else if (value.intertype === 'emptystruct') { return makeEmptyStruct(type); } else if (value.intertype === 'string') { - return JSON.stringify(parseLLVMString(value.text)) + - ' /* ' + value.text.substr(0, 20).replace(/[*<>]/g, '_') + ' */'; // make string safe for inclusion in comment + return parseLLVMString(value.text); } else { return alignStruct(handleSegments(value.contents), type); } @@ -244,9 +243,7 @@ function JSify(data, functionsOnly, givenFunctions) { function parseConst(value, type, ident) { var constant = makeConst(value, type); - if (typeof constant === 'object') { - constant = flatten(constant).map(function(x) { return parseNumerical(x) }) - } + constant = flatten(constant).map(function(x) { return parseNumerical(x) }) return constant; } @@ -254,6 +251,7 @@ function JSify(data, functionsOnly, givenFunctions) { substrate.addActor('GlobalVariable', { processItem: function(item) { function needsPostSet(value) { + if (typeof value !== 'string') return false; return value[0] in UNDERSCORE_OPENPARENS || value.substr(0, 14) === 'CHECK_OVERFLOW' || value.substr(0, 6) === 'GLOBAL'; } @@ -289,25 +287,21 @@ function JSify(data, functionsOnly, givenFunctions) { } else { constant = makeEmptyStruct(item.type); } - constant = JSON.stringify(constant); } else { constant = parseConst(item.value, item.type, item.ident); } - if (typeof constant === 'string' && constant[0] != '[') { - constant = [constant]; // A single item. We may need a postset for it. - } - if (typeof constant === 'object') { - // This is a flattened object. We need to find its idents, so they can be assigned to later - constant.forEach(function(value, i) { - if (needsPostSet(value)) { // ident, or expression containing an ident - ret.push({ - intertype: 'GlobalVariablePostSet', - JS: makeSetValue(makeGlobalUse(item.ident), i, value, 'i32', false, true) + ';' // ignore=true, since e.g. rtti and statics cause lots of safe_heap errors - }); - constant[i] = '0'; - } - }); - } + assert(typeof constant === 'object');//, [typeof constant, JSON.stringify(constant), item.external]); + + // This is a flattened object. We need to find its idents, so they can be assigned to later + constant.forEach(function(value, i) { + if (needsPostSet(value)) { // ident, or expression containing an ident + ret.push({ + intertype: 'GlobalVariablePostSet', + JS: makeSetValue(makeGlobalUse(item.ident), i, value, 'i32', false, true) + ';' // ignore=true, since e.g. rtti and statics cause lots of safe_heap errors + }); + constant[i] = '0'; + } + }); if (item.external) { // External variables in shared libraries should not be declared as @@ -322,7 +316,7 @@ function JSify(data, functionsOnly, givenFunctions) { constant = makePointer(constant, null, allocator, item.type, index); var js; - js = (index !== null ? '' : item.ident + '=') + constant + ';'; // \n Module.print("' + item.ident + ' :" + ' + makeGlobalUse(item.ident) + ');'; + js = (index !== null ? '' : item.ident + '=') + constant + ';'; // Special case: class vtables. We make sure they are null-terminated, to allow easy runtime operations if (item.ident.substr(0, 5) == '__ZTV') { |