aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2011-12-05 17:57:54 -0800
committerAlon Zakai <alonzakai@gmail.com>2011-12-05 17:57:54 -0800
commit0591fabb67954257cfa3a024b78f46a392fd4a31 (patch)
treef987a75683a7fd3ec726ab3182ff84872d14bbd8
parentb2c5374402ae4e952c652772e9907a8f1c0efec9 (diff)
support for external variables in runtime linking
-rw-r--r--src/intertyper.js2
-rw-r--r--src/jsifier.js9
-rw-r--r--src/settings.js5
-rw-r--r--tests/runner.py7
4 files changed, 12 insertions, 11 deletions
diff --git a/src/intertyper.js b/src/intertyper.js
index 4d85b8a5..19eb658e 100644
--- a/src/intertyper.js
+++ b/src/intertyper.js
@@ -488,6 +488,7 @@ function intertyper(data, sidePass, baseLineNums) {
} else {
// variable
var ident = item.tokens[0].text;
+ var private_ = findTokenText(item, 'private') >= 0;
cleanOutTokens(LLVM.GLOBAL_MODIFIERS, item.tokens, [2, 3]);
var external = false;
if (item.tokens[2].text === 'external') {
@@ -500,6 +501,7 @@ function intertyper(data, sidePass, baseLineNums) {
ident: toNiceIdent(ident),
type: item.tokens[2].text,
external: external,
+ private_: private_,
lineNum: item.lineNum
};
Types.needAnalysis[ret.type] = 0;
diff --git a/src/jsifier.js b/src/jsifier.js
index f4e0ef50..a2ca03eb 100644
--- a/src/jsifier.js
+++ b/src/jsifier.js
@@ -321,10 +321,10 @@ function JSify(data, functionsOnly, givenFunctions) {
if (item.ident in EXPORTED_GLOBALS) {
js += '\nModule["' + item.ident + '"] = ' + item.ident + ';';
}
- // TODO: Support exporting BUILD_AS_SHARED_LIB == 2 globals. The problem now is that they override the main file's globals.
- //if (BUILD_AS_SHARED_LIB == 2) {
- // js += 'if (globalScope) globalScope["' + item.ident + '"] = ' + item.ident + ';'; // XXX: assert not overriding
- //}
+ 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 + ' }';
+ }
return ret.concat({
intertype: 'GlobalVariable',
JS: js,
@@ -620,6 +620,7 @@ function JSify(data, functionsOnly, givenFunctions) {
}
if (BUILD_AS_SHARED_LIB == 2) {
+ // TODO: make the assert conditional on ASSERTIONS
func.JS += 'if (globalScope) { assert(!globalScope["' + func.ident + '"]); globalScope["' + func.ident + '"] = ' + func.ident + ' }';
}
diff --git a/src/settings.js b/src/settings.js
index 5aa647b2..0964d4bb 100644
--- a/src/settings.js
+++ b/src/settings.js
@@ -179,10 +179,7 @@ var BUILD_AS_SHARED_LIB = 0; // Whether to build the code as a shared library, w
// 1 means this is a normal shared lib.
// 2 means this is a shared lib that will be linked at runtime,
// which means it will insert its functions into
- // the global namespace. See STATIC_LIBS_TO_LOAD. Note
- // that only functions are exported, not globals, since
- // in a naive implementation they can easily override main's
- // symbols (for example, the global strings, _str1 etc.).
+ // the global namespace. See STATIC_LIBS_TO_LOAD.
var RUNTIME_LINKED_LIBS = []; // If this is a main file (BUILD_AS_SHARED_LIB == 0), then
// we will link these at runtime. They must have been built with
// BUILD_AS_SHARED_LIB == 2.
diff --git a/tests/runner.py b/tests/runner.py
index 90c6267c..3b5b03f2 100644
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -2170,6 +2170,7 @@ if 'benchmark' not in str(sys.argv):
def test_runtimelink(self):
if Building.LLVM_OPTS: return self.skip('LLVM opts will optimize printf into puts in the parent, and the child will still look for puts')
+ self.banned_js_engines = [NODE_JS] # node's global scope behaves differently than everything else, needs investigation FIXME
header = r'''
struct point
@@ -2193,7 +2194,7 @@ if 'benchmark' not in str(sys.argv):
printf("supp see: %d\n", mainInt);
}
- //int suppInt = 76; // TODO: Support this. Right now, _str1 will override the main _str1
+ int suppInt = 76;
'''
supp_name = os.path.join(self.get_dir(), 'supp.c')
open(supp_name, 'w').write(supp)
@@ -2214,7 +2215,7 @@ if 'benchmark' not in str(sys.argv):
int main( int argc, const char *argv[] ) {
struct point p = { 54, 2 };
suppFunc(p);
- printf("ok.\n");
+ printf("main see: %d\nok.\n", suppInt);
return 0;
}
'''
@@ -2226,7 +2227,7 @@ if 'benchmark' not in str(sys.argv):
Settings.BUILD_AS_SHARED_LIB = 0
Settings.RUNTIME_LINKED_LIBS = ['liblib.so'];
- self.do_run(main, 'supp: 54,2\nmain: 56\nsupp see: 543\nok.')
+ self.do_run(main, 'supp: 54,2\nmain: 56\nsupp see: 543\nmain see: 76\nok.')
def test_dlfcn_basic(self):
lib_src = '''