desktop/Library_sofficeapp.mk             |    1 
 desktop/inc/initjsunoscripting.hxx        |   20 +++++
 desktop/source/app/appinit.cxx            |  100 --------------------------
 desktop/source/app/initjsunoscripting.cxx |  113 ++++++++++++++++++++++++++++++
 desktop/source/lib/init.cxx               |    5 +
 solenv/clang-format/excludelist           |    1 
 6 files changed, 142 insertions(+), 98 deletions(-)

New commits:
commit e02f10d20b461a2033fa80b6a2bed6684f42a85f
Author:     Stephan Bergmann <stephan.bergm...@collabora.com>
AuthorDate: Fri Jul 18 14:29:49 2025 +0200
Commit:     Michael Stahl <michael.st...@collabora.com>
CommitDate: Tue Jul 29 16:19:08 2025 +0200

    Emscripten: Call initJsUnoScripting also from LOKit
    
    ...so that functionality is available there, too.  (And by breaking the 
code out
    into its own file, give the function a more self-explanatory name than the
    original "initUno".)
    
    Change-Id: Ic65fbd98447574d881c4e7827a2cfeb67d3ff1fa
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/188040
    Reviewed-by: Stephan Bergmann <stephan.bergm...@collabora.com>
    Tested-by: Jenkins
    (cherry picked from commit 0b10ff07c172d9de167ccec1442596054969e9da)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/188046
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Michael Stahl <michael.st...@collabora.com>

diff --git a/desktop/Library_sofficeapp.mk b/desktop/Library_sofficeapp.mk
index 6c94efbfe93b..4e24628d95a7 100644
--- a/desktop/Library_sofficeapp.mk
+++ b/desktop/Library_sofficeapp.mk
@@ -122,6 +122,7 @@ $(eval $(call gb_Library_add_exception_objects,sofficeapp,\
     desktop/source/app/cmdlinehelp \
     desktop/source/app/desktopcontext \
     desktop/source/app/dispatchwatcher \
+    desktop/source/app/initjsunoscripting \
     desktop/source/app/langselect \
     desktop/source/app/lockfile2 \
     desktop/source/app/officeipcthread \
diff --git a/desktop/inc/initjsunoscripting.hxx 
b/desktop/inc/initjsunoscripting.hxx
new file mode 100644
index 000000000000..ccfe9fd1d5fa
--- /dev/null
+++ b/desktop/inc/initjsunoscripting.hxx
@@ -0,0 +1,20 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; 
fill-column: 100 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#pragma once
+
+#include <sal/config.h>
+
+#if defined EMSCRIPTEN
+
+void initJsUnoScripting();
+
+#endif
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s 
cinkeys+=0=break: */
diff --git a/desktop/source/app/appinit.cxx b/desktop/source/app/appinit.cxx
index 5d575934063c..f4fd9c621c87 100644
--- a/desktop/source/app/appinit.cxx
+++ b/desktop/source/app/appinit.cxx
@@ -19,13 +19,11 @@
 
 
 #include <algorithm>
-#include <cassert>
-#include <cstdint>
-#include <string>
 #include <vector>
 
 #include <app.hxx>
 #include <dp_shared.hxx>
+#include <initjsunoscripting.hxx>
 #include "cmdlineargs.hxx"
 #include <strings.hrc>
 #include <com/sun/star/lang/XInitialization.hpp>
@@ -36,7 +34,6 @@
 #include <officecfg/Setup.hxx>
 #include <osl/file.hxx>
 #include <rtl/bootstrap.hxx>
-#include <rtl/uri.hxx>
 #include <sal/log.hxx>
 #include <comphelper/diagnose_ex.hxx>
 
@@ -49,103 +46,10 @@
 #include <iostream>
 #include <map>
 
-#if defined EMSCRIPTEN
-#include <emscripten.h>
-#include <emscripten/threading.h>
-#include <emscripten/val.h>
-#include <bindings_uno.hxx>
-#include <config_emscripten.h>
-#endif
-
 using namespace ::com::sun::star::uno;
 using namespace ::com::sun::star::lang;
 using namespace ::com::sun::star::ucb;
 
-#if defined EMSCRIPTEN
-
-namespace {
-
-extern "C" void getUnoScriptUrls(std::vector<std::u16string> * urls) {
-    assert(urls != nullptr);
-    OUString const 
base(emscripten::val::global("document")["baseURI"].as<std::u16string>());
-    auto const val = emscripten::val::module_property("uno_scripts");
-    if (!val.isUndefined()) {
-        auto const len = val["length"].as<std::uint32_t>();
-        for (std::uint32_t i = 0; i != len; ++i) {
-            urls->push_back(
-                std::u16string(
-                    rtl::Uri::convertRelToAbs(base, 
OUString(val[i].as<std::u16string>()))));
-        }
-    }
-}
-
-#if HAVE_EMSCRIPTEN_PROXY_TO_PTHREAD
-EM_JS(void, runUnoScriptUrls, (emscripten::EM_VAL handle), {
-    importScripts.apply(self, Emval.toValue(handle));
-});
-#else
-EM_JS(void, runUnoScriptUrls, (emscripten::EM_VAL handle), {
-    const urls = Emval.toValue(handle);
-    function step() {
-        if (urls.length !== 0) {
-            const url = urls.shift();
-            fetch(url).then(res => {
-                if (!res.ok) {
-                    throw Error(
-                        "Loading <" + res.url + "> failed with " + res.status 
+ " "
-                        + res.statusText);
-                }
-                return res.blob();
-            }).then(blob => blob.text()).then(text => { eval(text); step(); });
-        }
-    };
-    step();
-});
-#endif
-
-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];
-            Module.uno_init$resolve();
-        } else if (orig) {
-            orig(e);
-        }
-    };
-});
-
-extern "C" void resolveUnoMain(pthread_t id) {
-#if HAVE_EMSCRIPTEN_PROXY_TO_PTHREAD
-    EM_ASM({
-        const sofficeMain = PThread.pthreads[$0];
-        const channel = new MessageChannel();
-        sofficeMain.postMessage({cmd:"LOWA-channel"}, [channel.port2]);
-        Module.uno_main$resolve(channel.port1);
-    }, id);
-#else
-    EM_ASM({
-        const channel = new MessageChannel();
-        postMessage({cmd:"LOWA-channel"}, [channel.port2]);
-        Module.uno_main$resolve(channel.port1);
-    }, id);
-#endif
-}
-
-void initUno() {
-    init_unoembind_uno();
-    std::vector<std::u16string> urls;
-    emscripten_sync_run_in_main_runtime_thread(EM_FUNC_SIG_VI, 
getUnoScriptUrls, &urls);
-    runUnoScriptUrls(emscripten::val::array(urls).as_handle());
-    setupMainChannel();
-    emscripten_async_run_in_main_runtime_thread(EM_FUNC_SIG_VI, 
resolveUnoMain, pthread_self());
-}
-
-}
-
-#endif
-
 namespace desktop
 {
 
@@ -179,7 +83,7 @@ void Desktop::InitApplicationServiceManager()
 #endif
     comphelper::setProcessServiceFactory(sm);
 #if defined EMSCRIPTEN
-    initUno();
+    initJsUnoScripting();
 #endif
 }
 
diff --git a/desktop/source/app/initjsunoscripting.cxx 
b/desktop/source/app/initjsunoscripting.cxx
new file mode 100644
index 000000000000..a7de93f5f4f4
--- /dev/null
+++ b/desktop/source/app/initjsunoscripting.cxx
@@ -0,0 +1,113 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; 
fill-column: 100 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <sal/config.h>
+
+#if defined EMSCRIPTEN
+
+#include <cassert>
+#include <cstdint>
+#include <string>
+#include <vector>
+
+#include <pthread.h>
+
+#include <emscripten.h>
+#include <emscripten/threading.h>
+#include <emscripten/val.h>
+
+#include <bindings_uno.hxx>
+#include <config_emscripten.h>
+#include <rtl/uri.hxx>
+#include <rtl/ustring.hxx>
+
+namespace {
+
+extern "C" void getUnoScriptUrls(std::vector<std::u16string> * urls) {
+    assert(urls != nullptr);
+    OUString const 
base(emscripten::val::global("document")["baseURI"].as<std::u16string>());
+    auto const val = emscripten::val::module_property("uno_scripts");
+    if (!val.isUndefined()) {
+        auto const len = val["length"].as<std::uint32_t>();
+        for (std::uint32_t i = 0; i != len; ++i) {
+            urls->push_back(
+                std::u16string(
+                    rtl::Uri::convertRelToAbs(base, 
OUString(val[i].as<std::u16string>()))));
+        }
+    }
+}
+
+#if HAVE_EMSCRIPTEN_PROXY_TO_PTHREAD
+EM_JS(void, runUnoScriptUrls, (emscripten::EM_VAL handle), {
+    importScripts.apply(self, Emval.toValue(handle));
+});
+#else
+EM_JS(void, runUnoScriptUrls, (emscripten::EM_VAL handle), {
+    const urls = Emval.toValue(handle);
+    function step() {
+        if (urls.length !== 0) {
+            const url = urls.shift();
+            fetch(url).then(res => {
+                if (!res.ok) {
+                    throw Error(
+                        "Loading <" + res.url + "> failed with " + res.status 
+ " "
+                        + res.statusText);
+                }
+                return res.blob();
+            }).then(blob => blob.text()).then(text => { eval(text); step(); });
+        }
+    };
+    step();
+});
+#endif
+
+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];
+            Module.uno_init$resolve();
+        } else if (orig) {
+            orig(e);
+        }
+    };
+});
+
+extern "C" void resolveUnoMain(pthread_t id) {
+#if HAVE_EMSCRIPTEN_PROXY_TO_PTHREAD
+    EM_ASM({
+        const sofficeMain = PThread.pthreads[$0];
+        const channel = new MessageChannel();
+        sofficeMain.postMessage({cmd:"LOWA-channel"}, [channel.port2]);
+        Module.uno_main$resolve(channel.port1);
+    }, id);
+#else
+    EM_ASM({
+        const channel = new MessageChannel();
+        postMessage({cmd:"LOWA-channel"}, [channel.port2]);
+        Module.uno_main$resolve(channel.port1);
+    }, id);
+#endif
+}
+
+}
+
+void initJsUnoScripting() {
+    init_unoembind_uno();
+    std::vector<std::u16string> urls;
+    emscripten_sync_run_in_main_runtime_thread(EM_FUNC_SIG_VI, 
getUnoScriptUrls, &urls);
+    runUnoScriptUrls(emscripten::val::array(urls).as_handle());
+    setupMainChannel();
+    emscripten_async_run_in_main_runtime_thread(EM_FUNC_SIG_VI, 
resolveUnoMain, pthread_self());
+}
+
+#endif
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s 
cinkeys+=0=break: */
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index 08cc32f74980..26ff38ee063c 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -213,6 +213,7 @@
 #include <svx/pageitem.hxx>
 
 #include <app.hxx>
+#include <initjsunoscripting.hxx>
 
 #include "../app/cmdlineargs.hxx"
 // We also need to hackily be able to start the main libreoffice thread:
@@ -7788,6 +7789,10 @@ static bool initialize_uno(const OUString& 
aAppProgramURL)
     xSFactory.set(xFactory, uno::UNO_QUERY_THROW);
     comphelper::setProcessServiceFactory(xSFactory);
 
+#if defined EMSCRIPTEN
+    initJsUnoScripting();
+#endif
+
     SAL_INFO("lok", "Uno initialized  - " <<  xContext.is());
 
     // set UserInstallation to user profile dir in test/user-template
diff --git a/solenv/clang-format/excludelist b/solenv/clang-format/excludelist
index e0fbec3743e6..ced5fa777b31 100644
--- a/solenv/clang-format/excludelist
+++ b/solenv/clang-format/excludelist
@@ -3082,6 +3082,7 @@ desktop/source/app/crashreport.cxx
 desktop/source/app/desktopcontext.hxx
 desktop/source/app/dispatchwatcher.cxx
 desktop/source/app/dispatchwatcher.hxx
+desktop/source/app/initjsunoscripting.cxx
 desktop/source/app/langselect.cxx
 desktop/source/app/lockfile2.cxx
 desktop/source/app/officeipcthread.cxx

Reply via email to