aboutsummaryrefslogtreecommitdiff
path: root/src/jsifier.js
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-07-03 14:12:18 -0700
committerAlon Zakai <alonzakai@gmail.com>2013-07-03 15:31:05 -0700
commit1804c48e607cbdd499799c840828085ccedf630e (patch)
tree3f141e647ca57b58e7aac7415adc5b7524702af4 /src/jsifier.js
parentc8065da220560eb91d473ce2859b72c41ebb4304 (diff)
run postSets in a global initializer, before all other global initializers
Diffstat (limited to 'src/jsifier.js')
-rw-r--r--src/jsifier.js19
1 files changed, 17 insertions, 2 deletions
diff --git a/src/jsifier.js b/src/jsifier.js
index c0f42ceb..5cdacc7a 100644
--- a/src/jsifier.js
+++ b/src/jsifier.js
@@ -1639,11 +1639,11 @@ function JSify(data, functionsOnly, givenFunctions) {
print('/* no memory initializer */'); // test purposes
}
- // Run postsets right before main, and after the memory initializer has been set up
+ // Define postsets. These will be run in ATINIT, right before global initializers (which might need the postsets). We cannot
+ // run them now because the memory initializer might not have been applied yet.
print('function runPostSets() {\n');
print(itemsDict.GlobalVariablePostSet.map(function(item) { return item.JS }).join('\n'));
print('}\n');
- print('if (!awaitingMemoryInitializer) runPostSets();\n'); // if we load the memory initializer, this is done later
if (USE_TYPED_ARRAYS == 2) {
print('var tempDoublePtr = Runtime.alignMemory(allocate(12, "i8", ALLOC_STATIC), 8);\n');
@@ -1816,6 +1816,21 @@ function JSify(data, functionsOnly, givenFunctions) {
substrate.addItems(data.functionStubs, 'FunctionStub');
assert(data.functions.length == 0);
} else {
+ if (phase == 'pre') {
+ // ensure there is a global ctors, for runPostSets
+ if ('_llvm_global_ctors' in data.globalVariables) {
+ data.globalVariables._llvm_global_ctors.ctors.unshift('runPostSets'); // run postsets right before global initializers
+ hasCtors = true;
+ } else {
+ substrate.addItems([{
+ intertype: 'GlobalVariableStub',
+ ident: '_llvm_global_ctors',
+ type: '[1 x { i32, void ()* }]',
+ ctors: ["runPostSets"],
+ }], 'GlobalVariable');
+ }
+ }
+
substrate.addItems(sortGlobals(data.globalVariables), 'GlobalVariable');
substrate.addItems(data.aliass, 'Alias');
substrate.addItems(data.functions, 'FunctionSplitter');