Skip to content

Commit ba249f5

Browse files
committed
emit proper redirect thunks in side modules
1 parent a8b0615 commit ba249f5

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

emscripten.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ def fix_dot_zero(m):
165165
metadata['declares'] = filter(lambda i64_func: i64_func not in ['getHigh32', 'setHigh32', '__muldi3', '__divdi3', '__remdi3', '__udivdi3', '__uremdi3'], metadata['declares']) # FIXME: do these one by one as normal js lib funcs
166166

167167
# Integrate info from backend
168+
if settings['SIDE_MODULE']:
169+
settings['DEFAULT_LIBRARY_FUNCS_TO_INCLUDE'] = [] # we don't need any JS library contents in side modules
168170
settings['DEFAULT_LIBRARY_FUNCS_TO_INCLUDE'] = list(
169171
set(settings['DEFAULT_LIBRARY_FUNCS_TO_INCLUDE'] + map(shared.JS.to_nice_ident, metadata['declares'])).difference(
170172
map(lambda x: x[1:], metadata['implementedFunctions'])
@@ -306,7 +308,7 @@ def move_preasm(m):
306308
contents = m.groups(0)[0]
307309
outfile.write(contents + '\n')
308310
return ''
309-
if not settings['BOOTSTRAPPING_STRUCT_INFO']:
311+
if not settings['BOOTSTRAPPING_STRUCT_INFO'] and not settings['SIDE_MODULE']:
310312
funcs_js[1] = re.sub(r'/\* PRE_ASM \*/(.*)\n', lambda m: move_preasm(m), funcs_js[1])
311313

312314
class Counter:

src/jsifier.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function JSify(data, functionsOnly) {
6262
var libFuncsToInclude;
6363
if (INCLUDE_FULL_LIBRARY) {
6464
assert(!(BUILD_AS_SHARED_LIB || SIDE_MODULE), 'Cannot have both INCLUDE_FULL_LIBRARY and BUILD_AS_SHARED_LIB/SIDE_MODULE set.')
65-
libFuncsToInclude = MAIN_MODULE ? DEFAULT_LIBRARY_FUNCS_TO_INCLUDE.slice(0) : [];
65+
libFuncsToInclude = (MAIN_MODULE || SIDE_MODULE) ? DEFAULT_LIBRARY_FUNCS_TO_INCLUDE.slice(0) : [];
6666
for (var key in LibraryManager.library) {
6767
if (!key.match(/__(deps|postset|inline|asm|sig)$/)) {
6868
libFuncsToInclude.push(key);
@@ -125,7 +125,7 @@ function JSify(data, functionsOnly) {
125125

126126
var noExport = false;
127127

128-
if (!LibraryManager.library.hasOwnProperty(ident) && !LibraryManager.library.hasOwnProperty(ident + '__inline')) {
128+
if ((!LibraryManager.library.hasOwnProperty(ident) && !LibraryManager.library.hasOwnProperty(ident + '__inline')) || SIDE_MODULE) {
129129
if (notDep) {
130130
if (VERBOSE || ident.substr(0, 11) !== 'emscripten_') { // avoid warning on emscripten_* functions which are for internal usage anyhow
131131
if (ERROR_ON_UNDEFINED_SYMBOLS) error('unresolved symbol: ' + ident);
@@ -136,7 +136,12 @@ function JSify(data, functionsOnly) {
136136
// emit a stub that will fail at runtime
137137
LibraryManager.library[shortident] = new Function("Module['printErr']('missing function: " + shortident + "'); abort(-1);");
138138
} else {
139-
LibraryManager.library[shortident] = new Function("return Module['_" + shortident + "'].apply(null, arguments);");
139+
LibraryManager.library[shortident] = new Function("return " + (MAIN_MODULE ? '' : 'parent') + "Module['_" + shortident + "'].apply(null, arguments);");
140+
if (SIDE_MODULE) {
141+
// no dependencies, just emit the thunk
142+
Functions.libraryFunctions[finalName] = 1;
143+
return processLibraryFunction(LibraryManager.library[shortident], ident, finalName);
144+
}
140145
noExport = true;
141146
}
142147
}
@@ -206,7 +211,6 @@ function JSify(data, functionsOnly) {
206211
EXPORTED_FUNCTIONS[finalName] = 1;
207212
Functions.libraryFunctions[finalName] = 2;
208213
}
209-
if (SIDE_MODULE) return ';'; // we import into the side module js library stuff from the outside parent
210214
if ((EXPORT_ALL || (finalName in EXPORTED_FUNCTIONS)) && !noExport) {
211215
contentText += '\nModule["' + finalName + '"] = ' + finalName + ';';
212216
}

tests/test_core.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3776,7 +3776,7 @@ def test_dylink_class(self):
37763776
''', side=r'''
37773777
#include "header.h"
37783778
Class::Class(const char *name) { printf("new %s\n", name); }
3779-
''', expected=['new main\n'], need_reverse=False)
3779+
''', expected=['new main\n'])
37803780

37813781
def test_random(self):
37823782
src = r'''#include <stdlib.h>

0 commit comments

Comments
 (0)