MCPcopy Create free account
hub / github.com/emscripten-core/emscripten / test_preload_module

Method test_preload_module

test/test_other.py:14447–14500  ·  view source on GitHub ↗
(self, args)

Source from the content-addressed store, hash-verified

14445 'pthread': (['-g', '-pthread', '-Wno-experimental', '-sPROXY_TO_PTHREAD', '-sEXIT_RUNTIME'],),
14446 })
14447 def test_preload_module(self, args):
14448 if '-pthread' in args:
14449 self.require_pthreads()
14450 # TODO(sbc): This test is copyied from test_browser.py. Perhaps find a better way to
14451 # share code between them.
14452 create_file('library.c', r'''
14453 #include <stdio.h>
14454 int library_func() {
14455 return 42;
14456 }
14457 ''&#x27;)
14458 self.run_process([EMCC, 'library.c', '-sSIDE_MODULE', '-o', 'tmp.so'] + args)
14459 create_file('main.c', r''&#x27;
14460 #include <assert.h>
14461 #include <dlfcn.h>
14462 #include <stdio.h>
14463 #include <sys/stat.h>
14464 #include <emscripten.h>
14465 #include <emscripten/threading.h>
14466
14467 int main() {
14468 // Check the file exists in the VFS
14469 struct stat statbuf;
14470 assert(stat("/library.so", &statbuf) == 0);
14471
14472 // Check that it was preloaded.
14473 // The preloading actually only happens on the main thread where the filesystem
14474 // lives. On worker threads the module object is shared via sharedModules.
14475 if (emscripten_is_main_runtime_thread()) {
14476 int found = EM_ASM_INT(
14477 return preloadedWasm['/library.so'] !== undefined;
14478 );
14479 assert(found);
14480 } else {
14481 int found = EM_ASM_INT(
14482 err('sharedModules:', sharedModules);
14483 return sharedModules['/library.so'] !== undefined;
14484 );
14485 assert(found);
14486 }
14487 void *lib_handle = dlopen("/library.so", RTLD_NOW);
14488 assert(lib_handle);
14489 typedef int (*voidfunc)();
14490 voidfunc x = (voidfunc)dlsym(lib_handle, "library_func");
14491 assert(x);
14492 assert(x() == 42);
14493 printf("done\n");
14494 return 0;
14495 }
14496 ''&#x27;)
14497 expected = 'done\n'
14498 if '-pthread' in args:
14499 expected = "sharedModules: { '/library.so': Module [WebAssembly.Module] {} }\ndone\n"
14500 self.do_runf('main.c', expected, cflags=['-sMAIN_MODULE=2', '--preload-file', 'tmp.so@library.so', '--use-preload-plugins'] + args)
14501
14502 @requires_pthreads
14503 def test_standalone_whole_archive(self):

Callers

nothing calls this directly

Calls 4

create_fileFunction · 0.90
require_pthreadsMethod · 0.80
run_processMethod · 0.80
do_runfMethod · 0.80

Tested by

no test coverage detected