aboutsummaryrefslogtreecommitdiff
path: root/src/jsifier.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/jsifier.js')
-rw-r--r--src/jsifier.js177
1 files changed, 79 insertions, 98 deletions
diff --git a/src/jsifier.js b/src/jsifier.js
index c55072b4..c9476647 100644
--- a/src/jsifier.js
+++ b/src/jsifier.js
@@ -266,105 +266,85 @@ function JSify(data, functionsOnly, givenFunctions) {
item.ctors.map(function(ctor) { return ' { func: function() { ' + ctor + '() } }' }).join(',\n') +
'\n]);\n';
return ret;
+ }
+
+ var constant = null;
+ var allocator = (BUILD_AS_SHARED_LIB && !item.external) ? 'ALLOC_NORMAL' : 'ALLOC_STATIC';
+ var index = null;
+ if (item.external && BUILD_AS_SHARED_LIB) {
+ // External variables in shared libraries should not be declared as
+ // they would shadow similarly-named globals in the parent.
+ item.JS = '';
} else {
- var constant = null;
- var allocator = (BUILD_AS_SHARED_LIB && !item.external) ? 'ALLOC_NORMAL' : 'ALLOC_STATIC';
- var index = null;
- if (item.external && BUILD_AS_SHARED_LIB) {
- // External variables in shared libraries should not be declared as
- // they would shadow similarly-named globals in the parent.
- item.JS = '';
+ item.JS = makeGlobalDef(item.ident);
+ }
+
+ if (!NAMED_GLOBALS && isIndexableGlobal(item.ident)) {
+ index = makeGlobalUse(item.ident); // index !== null indicates we are indexing this
+ allocator = 'ALLOC_NONE';
+ }
+ if (item.external) {
+ if (Runtime.isNumberType(item.type) || isPointerType(item.type)) {
+ constant = zeros(Runtime.getNativeFieldSize(item.type));
} else {
- item.JS = makeGlobalDef(item.ident);
+ constant = makeEmptyStruct(item.type);
}
-
- if (item.external && !ASM_JS) { // ASM_JS considers externs to be globals
- // Import external global variables from the library if available.
- var shortident = item.ident.slice(1);
- if (LibraryManager.library[shortident] &&
- LibraryManager.library[shortident].length &&
- !BUILD_AS_SHARED_LIB) {
- if (addedLibraryItems[shortident]) return ret;
- var val = LibraryManager.library[shortident];
- var padding;
- if (Runtime.isNumberType(item.type) || isPointerType(item.type)) {
- padding = [item.type].concat(zeros(Runtime.getNativeFieldSize(item.type)-1));
- } else {
- padding = makeEmptyStruct(item.type);
- }
- var padded = val.concat(padding.slice(val.length));
- var js = item.ident + '=' + makePointer(padded, null, allocator, item.type, index) + ';'
- if (LibraryManager.library[shortident + '__postset']) {
- js += '\n' + LibraryManager.library[shortident + '__postset'];
- }
+ 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: js
- });
- }
- return ret;
- } else {
- if (!NAMED_GLOBALS && isIndexableGlobal(item.ident)) {
- index = makeGlobalUse(item.ident); // index !== null indicates we are indexing this
- allocator = 'ALLOC_NONE';
- }
- if (item.external) {
- assert(ASM_JS);
- if (Runtime.isNumberType(item.type) || isPointerType(item.type)) {
- constant = zeros(Runtime.getNativeFieldSize(item.type));
- } 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';
- }
+ 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';
}
- // NOTE: This is the only place that could potentially create static
- // allocations in a shared library.
- constant = makePointer(constant, null, allocator, item.type, index);
- var js;
+ });
+ }
- js = (index !== null ? '' : item.ident + '=') + constant + ';'; // \n Module.print("' + item.ident + ' :" + ' + makeGlobalUse(item.ident) + ');';
+ if (item.external) {
+ // External variables in shared libraries should not be declared as
+ // they would shadow similarly-named globals in the parent, so do nothing here.
+ if (BUILD_AS_SHARED_LIB) return ret;
+ // Library items need us to emit something, but everything else requires nothing.
+ if (!LibraryManager.library[item.ident.slice(1)]) return ret;
+ }
- // Special case: class vtables. We make sure they are null-terminated, to allow easy runtime operations
- if (item.ident.substr(0, 5) == '__ZTV') {
- if (index !== null) {
- index = getFastValue(index, '+', Runtime.alignMemory(calcAllocatedSize(Variables.globals[item.ident].type)));
- }
- js += '\n' + makePointer([0], null, allocator, ['void*'], index) + ';';
- }
- if (!ASM_JS && (EXPORT_ALL || (item.ident in EXPORTED_GLOBALS))) {
- js += '\nModule["' + item.ident + '"] = ' + item.ident + ';';
- }
- if (BUILD_AS_SHARED_LIB == 2 && !item.private_) {
- // TODO: make the assert conditional on ASSERTIONS
- js += 'if (globalScope) { assert(!globalScope["' + item.ident + '"]); globalScope["' + item.ident + '"] = ' + item.ident + ' }';
- }
- if (item.external && !NAMED_GLOBALS) {
- assert(ASM_JS);
- js = 'var ' + item.ident + ' = ' + js; // force an explicit naming, even if unnamed globals, for asm forwarding
- }
- return ret.concat({
- intertype: 'GlobalVariable',
- JS: js,
- });
+ // NOTE: This is the only place that could potentially create static
+ // allocations in a shared library.
+ constant = makePointer(constant, null, allocator, item.type, index);
+ var js;
+
+ js = (index !== null ? '' : item.ident + '=') + constant + ';'; // \n Module.print("' + item.ident + ' :" + ' + makeGlobalUse(item.ident) + ');';
+
+ // Special case: class vtables. We make sure they are null-terminated, to allow easy runtime operations
+ if (item.ident.substr(0, 5) == '__ZTV') {
+ if (index !== null) {
+ index = getFastValue(index, '+', Runtime.alignMemory(calcAllocatedSize(Variables.globals[item.ident].type)));
}
+ js += '\n' + makePointer([0], null, allocator, ['void*'], index) + ';';
+ }
+ if (!ASM_JS && (EXPORT_ALL || (item.ident in EXPORTED_GLOBALS))) {
+ js += '\nModule["' + item.ident + '"] = ' + item.ident + ';';
}
+ if (BUILD_AS_SHARED_LIB == 2 && !item.private_) {
+ // TODO: make the assert conditional on ASSERTIONS
+ js += 'if (globalScope) { assert(!globalScope["' + item.ident + '"]); globalScope["' + item.ident + '"] = ' + item.ident + ' }';
+ }
+ if (item.external && !NAMED_GLOBALS) {
+ js = 'var ' + item.ident + ' = ' + js; // force an explicit naming, even if unnamed globals, for asm forwarding
+ }
+ return ret.concat({
+ intertype: 'GlobalVariable',
+ JS: js,
+ });
}
});
@@ -441,7 +421,7 @@ function JSify(data, functionsOnly, givenFunctions) {
}
// In asm, we need to know about library functions. If there is a target, though, then no
// need to consider this a library function - we will call directly to it anyhow
- if (ASM_JS && !redirectedIdent && (typeof target == 'function' || /Math\..+/.exec(snippet))) {
+ if (ASM_JS && !redirectedIdent && (typeof target == 'function' || /Math\.\w+/.exec(snippet))) {
Functions.libraryFunctions[ident] = 1;
}
} else if (typeof snippet === 'object') {
@@ -1322,9 +1302,10 @@ function JSify(data, functionsOnly, givenFunctions) {
ident = Variables.resolveAliasToIdent(ident);
var shortident = ident.slice(1);
- var callIdent = LibraryManager.getRootIdent(shortident);
+ var simpleIdent = shortident;
+ var callIdent = LibraryManager.getRootIdent(simpleIdent);
if (callIdent) {
- shortident = callIdent; // ident may not be in library, if all there is is ident__inline, but in this case it is
+ simpleIdent = callIdent; // ident may not be in library, if all there is is ident__inline, but in this case it is
if (callIdent.indexOf('.') < 0) {
callIdent = '_' + callIdent; // Not Math.*, so add the normal prefix
}
@@ -1339,7 +1320,7 @@ function JSify(data, functionsOnly, givenFunctions) {
var varargsTypes = [];
var varargsByVals = {};
var ignoreFunctionIndexizing = [];
- var useJSArgs = (shortident + '__jsargs') in LibraryManager.library;
+ var useJSArgs = (simpleIdent + '__jsargs') in LibraryManager.library;
var hasVarArgs = isVarArgsFunctionType(type);
var normalArgs = (hasVarArgs && !useJSArgs) ? countNormalArgs(type) : -1;
var byPointer = getVarData(funcData, ident);
@@ -1367,7 +1348,7 @@ function JSify(data, functionsOnly, givenFunctions) {
args = args.map(function(arg, i) { return indexizeFunctions(arg, argsTypes[i]) });
if (ASM_JS) {
- if (shortident in Functions.libraryFunctions) {
+ if (shortident in Functions.libraryFunctions || simpleIdent in Functions.libraryFunctions) {
args = args.map(function(arg, i) { return asmCoercion(arg, argsTypes[i]) });
} else {
args = args.map(function(arg, i) { return asmEnsureFloat(arg, argsTypes[i]) });
@@ -1411,8 +1392,8 @@ function JSify(data, functionsOnly, givenFunctions) {
var argsText = args.join(', ');
// Inline if either we inline whenever we can (and we can), or if there is no noninlined version
- var inline = LibraryManager.library[shortident + '__inline'];
- var nonInlined = shortident in LibraryManager.library;
+ var inline = LibraryManager.library[simpleIdent + '__inline'];
+ var nonInlined = simpleIdent in LibraryManager.library;
if (inline && (INLINE_LIBRARY_FUNCS || !nonInlined)) {
return inline.apply(null, args); // Warning: inlining does not prevent recalculation of the arguments. They should be simple identifiers
}
@@ -1420,7 +1401,7 @@ function JSify(data, functionsOnly, givenFunctions) {
if (ASM_JS) {
// remove unneeded arguments, which the asm sig can show us. this lets us alias memset with llvm.memset, we just
// drop the final 2 args so things validate properly in asm
- var libsig = LibraryManager.library[shortident + '__sig'];
+ var libsig = LibraryManager.library[simpleIdent + '__sig'];
if (libsig) {
assert(!hasVarArgs);
while (libsig.length - 1 < args.length) {
@@ -1454,9 +1435,9 @@ function JSify(data, functionsOnly, givenFunctions) {
}
var ret = callIdent + '(' + args.join(', ') + ')';
- if (ASM_JS) { // TODO: do only when needed (library functions and Math.*?) XXX && shortident in Functions.libraryFunctions) {
+ if (ASM_JS) { // TODO: do only when needed (library functions and Math.*?) XXX && simpleIdent in Functions.libraryFunctions) {
ret = asmCoercion(ret, returnType);
- if (shortident == 'abort' && funcData.returnType != 'void') {
+ if (simpleIdent == 'abort' && funcData.returnType != 'void') {
ret += '; return 0'; // special case: abort() can happen without return, breaking the return type of asm functions. ensure a return
}
}