aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-01-18 14:07:57 -0800
committerAlon Zakai <alonzakai@gmail.com>2013-01-18 14:07:57 -0800
commit1191cf32e013d45a18753e09c383f2627428715b (patch)
tree70b155517aeb1c1f31a28fc4912a7d6ff7f4ab38
parentc9625dea12ca07fee75a3934f741c9a54d1d2113 (diff)
wrap library functions before putting them in asm function tables
-rwxr-xr-xemscripten.py4
-rw-r--r--src/modules.js20
2 files changed, 22 insertions, 2 deletions
diff --git a/emscripten.py b/emscripten.py
index dd3b32af..e15add2b 100755
--- a/emscripten.py
+++ b/emscripten.py
@@ -300,6 +300,8 @@ def emscript(infile, settings, outfile, libraries=[]):
simple = os.environ.get('EMCC_SIMPLE_ASM')
class Counter:
i = 0
+ pre_tables = last_forwarded_json['Functions']['tables']['pre']
+ del last_forwarded_json['Functions']['tables']['pre']
def make_table(sig, raw):
i = Counter.i
Counter.i += 1
@@ -438,7 +440,7 @@ var asm = (function(global, env, buffer) {
Runtime.stackAlloc = function(size) { return asm.stackAlloc(size) };
Runtime.stackSave = function() { return asm.stackSave() };
Runtime.stackRestore = function(top) { asm.stackRestore(top) };
-''' % ('\n'.join(function_tables_impls) + '\n' + function_tables_defs.replace('\n', '\n '), exports, sending, receiving)
+''' % (pre_tables + '\n'.join(function_tables_impls) + '\n' + function_tables_defs.replace('\n', '\n '), exports, sending, receiving)
# Set function table masks
def function_table_maskize(js):
diff --git a/src/modules.js b/src/modules.js
index a84800a8..d517ac70 100644
--- a/src/modules.js
+++ b/src/modules.js
@@ -264,7 +264,7 @@ var Functions = {
function emptyTable(sig) {
return zeros(total);
}
- var tables = {};
+ var tables = { pre: '' };
if (ASM_JS) {
['v', 'vi', 'ii', 'iii'].forEach(function(sig) { // add some default signatures that are used in the library
tables[sig] = emptyTable(sig); // TODO: make them compact
@@ -277,7 +277,9 @@ var Functions = {
tables[sig][this.indexedFunctions[ident]] = ident;
}
var generated = false;
+ var wrapped = {};
for (var t in tables) {
+ if (t == 'pre') continue;
generated = true;
var table = tables[t];
for (var i = 0; i < table.length; i++) {
@@ -294,6 +296,22 @@ var Functions = {
table[i] = (libName.indexOf('.') < 0 ? '_' : '') + libName;
}
}
+ var curr = table[i];
+ if (curr && Functions.unimplementedFunctions[table[i]]) {
+ // This is a library function, we can't just put it in the function table, need a wrapper
+ if (!wrapped[curr]) {
+ var args = '', arg_coercions = '', call = curr + '(', ret = t[0] == 'v' ? '' : ('return ' + (t[0] == 'f' ? '+0' : '0'));
+ for (var i = 1; i < t.length; i++) {
+ args += (i > 1 ? ',' : '') + 'a' + i;
+ arg_coercions += 'a' + i + '=' + (i > 1 ? ';' : '') + asmCoercion('a' + i, t[i] == 'f' ? 'float' : 'i32');
+ call += (i > 1 ? ',' : '') + asmCoercion('a' + i, t[i] == 'f' ? 'float' : 'i32');
+ }
+ call += ')';
+ tables.pre += 'function ' + curr + '__wrapper(' + args + ') { ' + arg_coercions + ' ; ' + call + ' ; ' + ret + ' }\n';
+ wrapped[curr] = 1;
+ }
+ table[i] = curr + '__wrapper';
+ }
}
var indices = table.toString().replace('"', '');
if (BUILD_AS_SHARED_LIB) {