Skip to content

Commit

Permalink
Move common runtime includes to runtime_shared.js. NFC (emscripten-co…
Browse files Browse the repository at this point in the history
…re#23269)

This reduces duplication between preamble.js and preamble_minimal.js.

Even though this is completely NFC there were some code size changes due
to the ordering of the code.
  • Loading branch information
sbc100 authored Jan 2, 2025
1 parent b08c9f1 commit a7dc5cd
Show file tree
Hide file tree
Showing 38 changed files with 79 additions and 114 deletions.
48 changes: 7 additions & 41 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,6 @@ if (typeof WebAssembly != 'object') {
}
#endif

#if SAFE_HEAP
#include "runtime_safe_heap.js"
#endif

#if SHARED_MEMORY && ALLOW_MEMORY_GROWTH
#include "growableHeap.js"
#endif

#if USE_ASAN
#include "runtime_asan.js"
#endif

#if SUPPORT_BASE64_EMBEDDING || FORCE_FILESYSTEM
#include "base64Utils.js"
#endif
Expand Down Expand Up @@ -150,12 +138,16 @@ var HEAP,
var HEAP_DATA_VIEW;
#endif

#include "runtime_shared.js"
var runtimeInitialized = false;

#if PTHREADS
#include "runtime_pthread.js"
#if EXIT_RUNTIME
var runtimeExited = false;
#endif

#include "URIUtils.js"

#include "runtime_shared.js"

#if ASSERTIONS
assert(!Module['STACK_SIZE'], 'STACK_SIZE can no longer be set at runtime. Use -sSTACK_SIZE at link time')
#endif
Expand All @@ -174,8 +166,6 @@ assert(!Module['wasmMemory'], 'Use of `wasmMemory` detected. Use -sIMPORTED_MEM
assert(!Module['INITIAL_MEMORY'], 'Detected runtime INITIAL_MEMORY setting. Use -sIMPORTED_MEMORY to define wasmMemory dynamically');
#endif // !IMPORTED_MEMORY && ASSERTIONS

#include "runtime_stack_check.js"

var __ATPRERUN__ = []; // functions called before the runtime is initialized
var __ATINIT__ = []; // functions called during startup
#if HAS_MAIN
Expand All @@ -188,12 +178,6 @@ var __ATPOSTRUN__ = []; // functions called after the main() is called
var __RELOC_FUNCS__ = [];
#endif

var runtimeInitialized = false;

#if EXIT_RUNTIME
var runtimeExited = false;
#endif

function preRun() {
#if ASSERTIONS && PTHREADS
assert(!ENVIRONMENT_IS_PTHREAD); // PThreads reuse the runtime from the main thread.
Expand Down Expand Up @@ -483,8 +467,6 @@ function abort(what) {
throw e;
}

#include "memoryprofiler.js"

#if ASSERTIONS && !('$FS' in addedLibraryItems)
// show errors on likely calls to FS when it was not included
var FS = {
Expand All @@ -506,8 +488,6 @@ Module['FS_createDataFile'] = FS.createDataFile;
Module['FS_createPreloadedFile'] = FS.createPreloadedFile;
#endif

#include "URIUtils.js"

#if ASSERTIONS
function createExportWrapper(name, nargs) {
return (...args) => {
Expand All @@ -524,8 +504,6 @@ function createExportWrapper(name, nargs) {
}
#endif

#include "runtime_exceptions.js"

#if ABORT_ON_WASM_EXCEPTIONS
// `abortWrapperDepth` counts the recursion level of the wrapper function so
// that we only handle exceptions at the top level letting the exception
Expand Down Expand Up @@ -675,16 +653,6 @@ async function getWasmBinary(binaryFile) {
return getBinarySync(binaryFile);
}

#if LOAD_SOURCE_MAP
var wasmSourceMap;
#include "source_map_support.js"
#endif

#if USE_OFFSET_CONVERTER
var wasmOffsetConverter;
#include "wasm_offset_converter.js"
#endif

#if SPLIT_MODULE
{{{ makeModuleReceiveWithVar('loadSplitModule', undefined, 'instantiateSync', true) }}}
var splitModuleProxyHandler = {
Expand Down Expand Up @@ -1127,8 +1095,6 @@ var tempDouble;
var tempI64;
#endif

#include "runtime_debug.js"

#if RETAIN_COMPILER_SETTINGS
var compilerSettings = {{{ JSON.stringify(makeRetainedCompilerSettings()) }}} ;

Expand Down
44 changes: 6 additions & 38 deletions src/preamble_minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@
* SPDX-License-Identifier: MIT
*/

#if SAFE_HEAP
#include "runtime_safe_heap.js"
#endif

#if SHARED_MEMORY && ALLOW_MEMORY_GROWTH
#include "growableHeap.js"
#endif

#if USE_ASAN
#include "runtime_asan.js"
#endif

#if ASSERTIONS
/** @type {function(*, string=)} */
function assert(condition, text) {
Expand Down Expand Up @@ -58,39 +46,19 @@ var HEAP8, HEAP16, HEAP32, HEAPU8, HEAPU16, HEAPU32, HEAPF32, HEAPF64,
#endif
wasmMemory;

#include "runtime_shared.js"

#if PTHREADS
#include "runtime_pthread.js"
#endif

#if IMPORTED_MEMORY
#include "runtime_init_memory.js"
#endif // IMPORTED_MEMORY

#include "runtime_stack_check.js"

#if LOAD_SOURCE_MAP
var wasmSourceMap;
#include "source_map_support.js"
#endif

#if USE_OFFSET_CONVERTER
var wasmOffsetConverter;
#include "wasm_offset_converter.js"
#if ASSERTIONS || SAFE_HEAP || USE_ASAN
var runtimeInitialized = false;
#endif

#if EXIT_RUNTIME
var __ATEXIT__ = []; // functions called during shutdown
var runtimeExited = false;
#endif

#if ASSERTIONS || SAFE_HEAP || USE_ASAN
var runtimeInitialized = false;
#endif
#include "runtime_shared.js"

#include "memoryprofiler.js"
#include "runtime_exceptions.js"
#include "runtime_debug.js"
#if IMPORTED_MEMORY
#include "runtime_init_memory.js"
#endif // IMPORTED_MEMORY

// === Body ===
31 changes: 31 additions & 0 deletions src/runtime_shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,37 @@
* SPDX-License-Identifier: MIT
*/

#include "runtime_stack_check.js"
#include "runtime_exceptions.js"
#include "runtime_debug.js"
#include "memoryprofiler.js"

#if SAFE_HEAP
#include "runtime_safe_heap.js"
#endif

#if SHARED_MEMORY && ALLOW_MEMORY_GROWTH
#include "growableHeap.js"
#endif

#if USE_ASAN
#include "runtime_asan.js"
#endif

#if PTHREADS
#include "runtime_pthread.js"
#endif

#if LOAD_SOURCE_MAP
var wasmSourceMap;
#include "source_map_support.js"
#endif

#if USE_OFFSET_CONVERTER
var wasmOffsetConverter;
#include "wasm_offset_converter.js"
#endif

{{{
// Helper function to export a heap symbol on the module object,
// if requested.
Expand Down
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except_wasm.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8329
8328
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except_wasm.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20266
20267
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8329
8328
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20266
20267
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_lto.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8363
8360
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_mangle.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9375
9374
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_wasmfs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3600
3599
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_js_fs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7656
7655
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_wasmfs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2837
2836
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_O0.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7891
7905
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_O0.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
21016
21012
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_O2.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2357
2356
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_O3.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2296
2297
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_Os.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2296
2297
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_Oz.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2279
2278
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_dylink.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6028
6026
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_dylink.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
13317
13316
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1681
1678
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_wasmfs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2296
2297
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1873
1874
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_mem_O3.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2327
2326
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_mem_O3_grow.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2476
2474
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2177
2175
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_mem_O3_standalone.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2140
2141
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_O0.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6435
6443
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_O2.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1381
1380
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_O3.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1348
1347
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_Os.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1348
1347
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_Oz.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1348
1347
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_esm.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1541
1542
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_pthreads.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4190
4186
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_wasmfs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1348
1347
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
52853
52855
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_no_asserts.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
28644
28646
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_strict.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
51636
51638

0 comments on commit a7dc5cd

Please sign in to comment.