diff --git a/desktop/source/app/appinit.cxx b/desktop/source/app/appinit.cxx index 6f8bffa1146a..4490529a77ba 100644 --- a/desktop/source/app/appinit.cxx +++ b/desktop/source/app/appinit.cxx @@ -84,9 +84,36 @@ EM_JS(void, runUnoScriptUrl, (char16_t const * url), { }).then(blob => blob.text()).then(text => eval(text)); }); +EM_JS(void, setupMainChannel, (), { + const orig = self.onmessage; + self.onmessage = function(e) { + if (e.data.cmd === "LOWA-channel") { + self.onmessage = orig; + Module.uno_mainPort = e.ports[0]; + } else if (orig) { + orig(e); + } + }; +}); + +extern "C" void resolveUnoMain() { + EM_ASM( + let sofficeMain; + for (const i in PThread.pthreads) { + const worker = PThread.pthreads[i]; + if (worker.workerID === 1) { + sofficeMain = worker; + break; + } + } + const channel = new MessageChannel(); + sofficeMain.postMessage({cmd:"LOWA-channel"}, [channel.port2]); + Module.uno_main$resolve(channel.port1); + ); +} + void initUno() { init_unoembind_uno(); - EM_ASM(Module.uno_init$resolve();); std::vector urls; emscripten_sync_run_in_main_runtime_thread(EM_FUNC_SIG_VI, getUnoScriptUrls, &urls); for (auto const & url: urls) { @@ -95,6 +122,9 @@ void initUno() { } runUnoScriptUrl(url.c_str()); } + setupMainChannel(); + EM_ASM(Module.uno_init$resolve();); + emscripten_async_run_in_main_runtime_thread(EM_FUNC_SIG_V, resolveUnoMain); } } diff --git a/static/emscripten/uno.js b/static/emscripten/uno.js index 7d051a24a84b..84b301f3e3b3 100644 --- a/static/emscripten/uno.js +++ b/static/emscripten/uno.js @@ -19,6 +19,11 @@ Module.uno_init = new Promise(function (resolve, reject) { Module.uno_init$reject = reject; }); +Module.uno_main = new Promise(function (resolve, reject) { + Module.uno_main$resolve = resolve; + Module.uno_main$reject = reject; +}); + Module.catchUnoException = function(exception) { // Rethrow non-C++ exceptions (non-UNO C++ exceptions are mapped to css.uno.RuntimeException in // Module.getUnoExceptionFromCxaException):