Skip to content

Commit afeebd2

Browse files
authored
Fix embind to work (more) on MINIMAL_RUNTIME. (emscripten-core#10417)
* Fix embind to work (more) on MINIMAL_RUNTIME. Remove old super-ancient pre-asm.js binding code from embind. * Add some more embind deps to free to resolve indirect dependency.
1 parent 4b365a6 commit afeebd2

5 files changed

Lines changed: 30 additions & 34 deletions

File tree

src/deps_info.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,11 @@
9494
"_embind_register_std_string": ["malloc", "free"],
9595
"_embind_register_std_wstring": ["malloc", "free"],
9696
"__syscall192": ["emscripten_builtin_memalign"],
97-
"pthread_create": ["malloc", "free", "emscripten_main_thread_process_queued_calls"]
97+
"pthread_create": ["malloc", "free", "emscripten_main_thread_process_queued_calls"],
98+
"$getTypeName": ["free"],
99+
"_embind_register_std_string": ["free"],
100+
"_embind_register_std_wstring": ["free"],
101+
"_embind_register_function": ["free"],
102+
"_embind_register_class": ["free"],
103+
"_embind_register_enum_value": ["free"]
98104
}

src/embind/embind.js

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ var LibraryEmbind = {
393393
return ret;
394394
},
395395

396-
$getTypeName__deps: ['free', '$readLatin1String'],
396+
$getTypeName__deps: ['$readLatin1String'],
397397
$getTypeName: function(type) {
398398
var ptr = ___getTypeName(type);
399399
var rv = readLatin1String(ptr);
@@ -1108,34 +1108,12 @@ var LibraryEmbind = {
11081108
#endif
11091109
}
11101110

1111-
var fp;
1112-
if (Module['FUNCTION_TABLE_' + signature] !== undefined) {
1113-
fp = Module['FUNCTION_TABLE_' + signature][rawFunction];
1114-
} else if (typeof FUNCTION_TABLE !== "undefined") {
1115-
fp = FUNCTION_TABLE[rawFunction];
1116-
} else {
1117-
// asm.js does not give direct access to the function tables,
1118-
// and thus we must go through the dynCall interface which allows
1119-
// calling into a signature's function table by pointer value.
1120-
//
1121-
// https://github.com/dherman/asm.js/issues/83
1122-
//
1123-
// This has three main penalties:
1124-
// - dynCall is another function call in the path from JavaScript to C++.
1125-
// - JITs may not predict through the function table indirection at runtime.
1126-
var dc = Module['dynCall_' + signature];
1127-
if (dc === undefined) {
1128-
// We will always enter this branch if the signature
1129-
// contains 'f' and PRECISE_F32 is not enabled.
1130-
//
1131-
// Try again, replacing 'f' with 'd'.
1132-
dc = Module['dynCall_' + signature.replace(/f/g, 'd')];
1133-
if (dc === undefined) {
1134-
throwBindingError("No dynCall invoker for signature: " + signature);
1135-
}
1136-
}
1137-
fp = makeDynCaller(dc);
1138-
}
1111+
#if MINIMAL_RUNTIME
1112+
var dc = asm['dynCall_' + signature];
1113+
#else
1114+
var dc = Module['dynCall_' + signature];
1115+
#endif
1116+
var fp = makeDynCaller(dc);
11391117

11401118
if (typeof fp !== "function") {
11411119
throwBindingError("unknown function pointer with signature " + signature + ": " + rawFunction);

src/parseTools.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,3 +1674,7 @@ function makeAsmImportsAccessInPthread(variable) {
16741674
return variable;
16751675
}
16761676
}
1677+
1678+
function hasExportedFunction(func) {
1679+
return Object.keys(EXPORTED_FUNCTIONS).indexOf(func) != -1;
1680+
}

src/postamble_minimal.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
{{{ exportRuntime() }}}
55

6+
#if hasExportedFunction('_main') // Only if user is exporting a C main(), we will generate a run() function that can be used to launch main.
67
function run() {
78
#if MEMORYPROFILER
89
emscriptenMemoryProfiler.onPreloadComplete();
@@ -36,6 +37,7 @@ function run() {
3637
checkStackCookie();
3738
#endif
3839
}
40+
#endif
3941

4042
function initRuntime(asm) {
4143
#if ASSERTIONS
@@ -145,10 +147,10 @@ WebAssembly.instantiate(Module['wasm'], imports).then(function(output) {
145147
wasmModule = output.module || Module['wasm'];
146148
#endif
147149

148-
#if !(LibraryManager.has('library_exports.js') && (WASM || WASM_BACKEND))
149-
// If not using the emscripten_get_exported_function() API, keep the 'asm' exports
150-
// variable in local scope to this instantiate function. (otherwise access it without
151-
// to export it to outer scope)
150+
#if !(LibraryManager.has('library_exports.js') && (WASM || WASM_BACKEND)) && !EMBIND
151+
// If not using the emscripten_get_exported_function() API or embind, keep the 'asm'
152+
// exports variable in local scope to this instantiate function to save code size.
153+
// (otherwise access it without to export it to outer scope)
152154
var
153155
#endif
154156

src/shell_minimal.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,19 @@ function err(text) {
129129
// compilation is ready. In that callback, call the function run() to start
130130
// the program.
131131
function ready() {
132+
#if INVOKE_RUN && hasExportedFunction('_main')
132133
#if USE_PTHREADS
133134
if (!ENVIRONMENT_IS_PTHREAD) {
134135
#endif
135136
run();
136137
#if USE_PTHREADS
137138
}
138139
#endif
140+
#else
141+
#if ASSERTIONS
142+
console.log('ready() called, and INVOKE_RUN=0. The runtime is now ready for you to call run() to invoke application _main(). You can also override ready() in a --pre-js file to get this signal as a callback')
143+
#endif
144+
#endif
139145
}
140146

141147
// --pre-jses are emitted after the Module integration code, so that they can

0 commit comments

Comments
 (0)