aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/jsifier.js13
-rw-r--r--src/library.js4
-rw-r--r--src/modules.js8
-rw-r--r--src/parseTools.js6
-rw-r--r--src/shell_sharedlib.js2
5 files changed, 21 insertions, 12 deletions
diff --git a/src/jsifier.js b/src/jsifier.js
index ac45d829..d4f3be66 100644
--- a/src/jsifier.js
+++ b/src/jsifier.js
@@ -200,7 +200,10 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
processItem: function(item) {
var ret = [item];
var shortident = item.ident.substr(1);
- if (shortident in Library) {
+ if (BUILD_AS_SHARED_LIB) {
+ // Shared libraries reuse the runtime of their parents.
+ item.JS = '';
+ } else if (shortident in Library) {
function addFromLibrary(ident) {
if (ident in addedLibraryItems) return '';
// Don't replace implemented functions with library ones (which can happen when we add dependencies).
@@ -261,13 +264,7 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
} else {
ident = '_' + ident;
}
- var depsString = deps ? '\n' + deps.map(addFromLibrary).join('\n') : '';
- if (BUILD_AS_SHARED_LIB) {
- var defString = 'if (typeof ' + ident + ' === "undefined") {\n this.' + ident + '=' + snippet + ';\n}';
- } else {
- var defString = 'var ' + ident + '=' + snippet + ';';
- }
- return depsString + defString;
+ return (deps ? '\n' + deps.map(addFromLibrary).join('\n') : '') + 'var ' + ident + '=' + snippet + ';';
}
item.JS = addFromLibrary(shortident);
} else {
diff --git a/src/library.js b/src/library.js
index fb114e1e..d2e13273 100644
--- a/src/library.js
+++ b/src/library.js
@@ -1209,6 +1209,7 @@ var Library = {
// void* dlopen(const char* filename, int flag);
dlopen__deps: ['$DLFCN_DATA'],
dlopen: function(filename, flag) {
+ // TODO: Add support for LD_LIBRARY_PATH.
filename = Pointer_stringify(filename);
filename += '.js';
@@ -1227,7 +1228,7 @@ var Library = {
}
try {
- var lib_module = eval(lib_data)();
+ var lib_module = eval(lib_data)(FUNCTION_TABLE.length);
} catch (e) {
DLFCN_DATA.isError = true;
return 0;
@@ -1287,6 +1288,7 @@ var Library = {
} else {
var result = lib_module[symbol];
if (typeof result == 'function') {
+ // TODO: Cache functions rather than appending on every lookup.
FUNCTION_TABLE.push(result);
FUNCTION_TABLE.push(0);
result = FUNCTION_TABLE.length - 2;
diff --git a/src/modules.js b/src/modules.js
index fd18fc72..91758609 100644
--- a/src/modules.js
+++ b/src/modules.js
@@ -140,7 +140,13 @@ var Functions = {
// Generate code for function indexing
generateIndexing: function() {
- return 'var FUNCTION_TABLE = [' + this.indexedFunctions.toString().replace('"', '') + '];';
+ var indices = this.indexedFunctions.toString().replace('"', '');
+ if (BUILD_AS_SHARED_LIB) {
+ // Shared libraries reuse the parent's function table.
+ return 'FUNCTION_TABLE = FUNCTION_TABLE.concat([' + indices + ']);';
+ } else {
+ return 'var FUNCTION_TABLE = [' + indices + '];';
+ }
}
};
diff --git a/src/parseTools.js b/src/parseTools.js
index 98ebd32c..e307a710 100644
--- a/src/parseTools.js
+++ b/src/parseTools.js
@@ -694,7 +694,11 @@ function makeGetValue(ptr, pos, type, noNeedFirst, unsigned) {
function indexizeFunctions(value) { // TODO: Also check for externals
if (value in Functions.currFunctions) {
- return Functions.getIndex(value);
+ if (BUILD_AS_SHARED_LIB) {
+ return '(FUNCTION_TABLE_OFFSET + ' + Functions.getIndex(value) + ')';
+ } else {
+ return Functions.getIndex(value);
+ }
}
if (value && value[0] && value[0] == '_') {
var rootIdent = LibraryManager.getRootIdent(value.slice(1));
diff --git a/src/shell_sharedlib.js b/src/shell_sharedlib.js
index d7af23fc..cbdac74b 100644
--- a/src/shell_sharedlib.js
+++ b/src/shell_sharedlib.js
@@ -1,7 +1,7 @@
"use strict";
// Capture the output of this into a variable, if you want
-(function() {
+(function(FUNCTION_TABLE_OFFSET) {
var Module = {};
var args = [];
Module.arguments = [];