static/source/embindmaker/embindmaker.cxx       |   62 +++++++++++++++++++++---
 udkapi/UnoApi_udkapi.mk                         |    1 
 udkapi/org/libreoffice/embindtest/Constants.idl |   27 ++++++++++
 unotest/source/embindtest/embindtest.js         |   20 +++++++
 4 files changed, 104 insertions(+), 6 deletions(-)

New commits:
commit 6064939d40119877eb65d5f0ce80f65d9fac53da
Author:     Stephan Bergmann <stephan.bergm...@allotropia.de>
AuthorDate: Wed Feb 28 14:57:35 2024 +0100
Commit:     Stephan Bergmann <stephan.bergm...@allotropia.de>
CommitDate: Thu Feb 29 07:53:54 2024 +0100

    Embind support for constant groups
    
    The constants are only reflected directly as JS values in the generated *.js
    file.  Reflecting them via emscripten::constant in the generated *.cxx did 
not
    work well:  Most importantly, emscripten::constant (and its underlying
    _embind_register_constant) coerce all values to double, so UNO (unsigned) 
hyper
    would not survive as JS BigInt, which would then cause e.g.
    
    >     
console.assert(test.isHyper(uno.org.libreoffice.embindtest.Constants.Hyper));
    
    passing such a (JS number, not BigInt) value into the
    org.libreoffice.embindtest.XTest::isHyper method (which expects a UNO 
hyper) to
    fail.  (Also, constants of UNO boolean type would be represented as numbers 
0/1
    rather than as false/true.)
    
    Change-Id: I056db0ccce0bf40eb53728fd439cc74964eb6951
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/164097
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <stephan.bergm...@allotropia.de>

diff --git a/static/source/embindmaker/embindmaker.cxx 
b/static/source/embindmaker/embindmaker.cxx
index 37175d100cdf..89fdf834ea1d 100644
--- a/static/source/embindmaker/embindmaker.cxx
+++ b/static/source/embindmaker/embindmaker.cxx
@@ -25,6 +25,7 @@
 #include <utility>
 #include <vector>
 
+#include <codemaker/commoncpp.hxx>
 #include <codemaker/global.hxx>
 #include <codemaker/typemanager.hxx>
 #include <osl/file.hxx>
@@ -169,17 +170,66 @@ void scan(rtl::Reference<unoidl::MapCursor> const& 
cursor, std::u16string_view p
                 break;
             }
             case unoidl::Entity::SORT_ENUM_TYPE:
-                module->mappings.emplace_back(id, "uno_Type_" + jsName(name));
+                module->mappings.emplace_back(id, "instance.uno_Type_" + 
jsName(name));
                 enums.emplace_back(name);
                 break;
             case unoidl::Entity::SORT_PLAIN_STRUCT_TYPE:
-                module->mappings.emplace_back(id, "uno_Type_" + jsName(name));
+                module->mappings.emplace_back(id, "instance.uno_Type_" + 
jsName(name));
                 structs.emplace_back(name);
                 break;
             case unoidl::Entity::SORT_INTERFACE_TYPE:
-                module->mappings.emplace_back(id, "uno_Type_" + jsName(name));
+                module->mappings.emplace_back(id, "instance.uno_Type_" + 
jsName(name));
                 interfaces.emplace_back(name);
                 break;
+            case unoidl::Entity::SORT_CONSTANT_GROUP:
+            {
+                auto const& members
+                    = 
static_cast<unoidl::ConstantGroupEntity*>(ent.get())->getMembers();
+                if (!members.empty())
+                {
+                    auto sub = std::make_shared<Module>();
+                    for (auto const& member : members)
+                    {
+                        OUString value;
+                        switch (member.value.type)
+                        {
+                            case unoidl::ConstantValue::TYPE_BOOLEAN:
+                                value = member.value.booleanValue ? 
u"true"_ustr : u"false"_ustr;
+                                break;
+                            case unoidl::ConstantValue::TYPE_BYTE:
+                                value = 
OUString::number(member.value.byteValue);
+                                break;
+                            case unoidl::ConstantValue::TYPE_SHORT:
+                                value = 
OUString::number(member.value.shortValue);
+                                break;
+                            case unoidl::ConstantValue::TYPE_UNSIGNED_SHORT:
+                                value = 
OUString::number(member.value.unsignedShortValue);
+                                break;
+                            case unoidl::ConstantValue::TYPE_LONG:
+                                value = 
OUString::number(member.value.longValue);
+                                break;
+                            case unoidl::ConstantValue::TYPE_UNSIGNED_LONG:
+                                value = 
OUString::number(member.value.unsignedLongValue);
+                                break;
+                            case unoidl::ConstantValue::TYPE_HYPER:
+                                value = 
OUString::number(member.value.hyperValue) + "n";
+                                break;
+                            case unoidl::ConstantValue::TYPE_UNSIGNED_HYPER:
+                                value = 
OUString::number(member.value.unsignedHyperValue) + "n";
+                                break;
+                            case unoidl::ConstantValue::TYPE_FLOAT:
+                                value = 
OUString::number(member.value.floatValue);
+                                break;
+                            case unoidl::ConstantValue::TYPE_DOUBLE:
+                                value = 
OUString::number(member.value.doubleValue);
+                                break;
+                        }
+                        sub->mappings.emplace_back(member.name, value);
+                    }
+                    module->modules[id] = sub;
+                }
+                break;
+            }
             case unoidl::Entity::SORT_SINGLE_INTERFACE_BASED_SERVICE:
             {
                 auto const& ctors
@@ -191,7 +241,7 @@ void scan(rtl::Reference<unoidl::MapCursor> const& cursor, 
std::u16string_view p
                     for (auto const& ctor : ctors)
                     {
                         
sub->mappings.emplace_back(getServiceConstructorName(ctor),
-                                                   jsServiceConstructor(name, 
ctor));
+                                                   "instance." + 
jsServiceConstructor(name, ctor));
                     }
                     module->modules[id] = sub;
                     services.emplace_back(name);
@@ -199,7 +249,7 @@ void scan(rtl::Reference<unoidl::MapCursor> const& cursor, 
std::u16string_view p
             }
             break;
             case unoidl::Entity::SORT_INTERFACE_BASED_SINGLETON:
-                module->mappings.emplace_back(id, jsSingleton(name));
+                module->mappings.emplace_back(id, "instance." + 
jsSingleton(name));
                 singletons.emplace_back(name);
                 break;
             default:
@@ -682,7 +732,7 @@ void writeJsMap(std::ostream& out, Module const& module, 
std::string const& pref
         {
             out << ",
";
         }
-        out << prefix << "'" << id << "': instance." << to;
+        out << prefix << "'" << id << "': " << to;
         comma = true;
     }
     for (auto const & [ id, sub ] : module.modules)
diff --git a/udkapi/UnoApi_udkapi.mk b/udkapi/UnoApi_udkapi.mk
index 5f22026027ee..15164a798006 100644
--- a/udkapi/UnoApi_udkapi.mk
+++ b/udkapi/UnoApi_udkapi.mk
@@ -522,6 +522,7 @@ $(eval $(call 
gb_UnoApi_add_idlfiles,udkapi,com/sun/star/util,\
 
 ifeq ($(OS)-$(ENABLE_DBGUTIL),EMSCRIPTEN-TRUE)
 $(eval $(call gb_UnoApi_add_idlfiles,udkapi,org/libreoffice/embindtest, \
+    Constants \
     Struct \
     XTest \
 ))
diff --git a/udkapi/org/libreoffice/embindtest/Constants.idl 
b/udkapi/org/libreoffice/embindtest/Constants.idl
new file mode 100644
index 000000000000..06ecd22fb56d
--- /dev/null
+++ b/udkapi/org/libreoffice/embindtest/Constants.idl
@@ -0,0 +1,27 @@
+/* -*- 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/.
+ */
+
+module org { module libreoffice { module embindtest {
+
+constants Constants {
+    const boolean Boolean = True;
+    const byte Byte = -12;
+    const short Short = -1234;
+    const unsigned short UnsignedShort = 54321;
+    const long Long = -123456;
+    const unsigned long UnsignedLong = 3456789012;
+    const hyper Hyper = -123456789;
+    const unsigned hyper UnsignedHyper = 9876543210;
+    const float Float = -10.25;
+    const double Double = 100.5;
+};
+
+}; }; };
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s 
cinkeys+=0=break: */
diff --git a/unotest/source/embindtest/embindtest.js 
b/unotest/source/embindtest/embindtest.js
index 146be3fa545a..4eb9e60832e6 100644
--- a/unotest/source/embindtest/embindtest.js
+++ b/unotest/source/embindtest/embindtest.js
@@ -247,6 +247,26 @@ Module.addOnPostRun(function() {
         console.assert(test.isSequenceStruct(v));
         v.delete();
     }
+    console.assert(uno.org.libreoffice.embindtest.Constants.Boolean === true);
+    
console.assert(test.isBoolean(uno.org.libreoffice.embindtest.Constants.Boolean));
+    console.assert(uno.org.libreoffice.embindtest.Constants.Byte === -12);
+    console.assert(test.isByte(uno.org.libreoffice.embindtest.Constants.Byte));
+    console.assert(uno.org.libreoffice.embindtest.Constants.Short === -1234);
+    
console.assert(test.isShort(uno.org.libreoffice.embindtest.Constants.Short));
+    console.assert(uno.org.libreoffice.embindtest.Constants.UnsignedShort === 
54321);
+    
console.assert(test.isUnsignedShort(uno.org.libreoffice.embindtest.Constants.UnsignedShort));
+    console.assert(uno.org.libreoffice.embindtest.Constants.Long === -123456);
+    console.assert(test.isLong(uno.org.libreoffice.embindtest.Constants.Long));
+    console.assert(uno.org.libreoffice.embindtest.Constants.UnsignedLong === 
3456789012);
+    
console.assert(test.isUnsignedLong(uno.org.libreoffice.embindtest.Constants.UnsignedLong));
+    console.assert(uno.org.libreoffice.embindtest.Constants.Hyper === 
-123456789n);
+    
console.assert(test.isHyper(uno.org.libreoffice.embindtest.Constants.Hyper));
+    console.assert(uno.org.libreoffice.embindtest.Constants.UnsignedHyper === 
9876543210n);
+    
console.assert(test.isUnsignedHyper(uno.org.libreoffice.embindtest.Constants.UnsignedHyper));
+    console.assert(uno.org.libreoffice.embindtest.Constants.Float === -10.25);
+    
console.assert(test.isFloat(uno.org.libreoffice.embindtest.Constants.Float));
+    console.assert(uno.org.libreoffice.embindtest.Constants.Double === 100.5);
+    
console.assert(test.isDouble(uno.org.libreoffice.embindtest.Constants.Double));
 });
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s 
cinkeys+=0=break: */

Reply via email to