diff options
author | Stephan Bergmann <stephan.bergmann@allotropia.de> | 2024-02-07 14:47:54 +0100 |
---|---|---|
committer | Stephan Bergmann <stephan.bergmann@allotropia.de> | 2024-02-08 09:30:22 +0100 |
commit | 72056edca078dd010368de15d03321a60c6d5cdd (patch) | |
tree | cf95055fa1cd91da12d868feaff8afce94733265 /include | |
parent | 578d9be50413a4bdc809f8b5f58cc177458f8325 (diff) |
Embind: Allow UNO sequences to be constructed from JS arrays
...though
> let seq = new Module.uno_Sequence_string(["foo", "bar", "baz"]);
> // ...
> delete seq;
is still ugly.
And Embind only allows for overload resolution by number of parameters, not by
their type, so using the original sequence constructor had to be changed to
> let seq = new Module.uno_Sequence_string(3, Module.uno_Sequence.FromSize);
> seq.set(0, "foo");
> seq.set(1, "bar");
> seq.set(2, "baz");
> // ...
> delete seq;
Change-Id: If26ff4a485ba16b65cf24b6fe729d379c733c473
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/163097
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <stephan.bergmann@allotropia.de>
Diffstat (limited to 'include')
-rw-r--r-- | include/static/unoembindhelpers/PrimaryBindings.hxx | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/include/static/unoembindhelpers/PrimaryBindings.hxx b/include/static/unoembindhelpers/PrimaryBindings.hxx index 61efaf01153b..4df301277cc9 100644 --- a/include/static/unoembindhelpers/PrimaryBindings.hxx +++ b/include/static/unoembindhelpers/PrimaryBindings.hxx @@ -11,6 +11,8 @@ #include <sal/config.h> +#include <cstdint> +#include <limits> #include <stdexcept> #include <emscripten/bind.h> @@ -36,6 +38,11 @@ enum class uno_Reference FromAny }; +enum class uno_Sequence +{ + FromSize +}; + template <typename T> struct UnoInOutParam { UnoInOutParam() {} @@ -72,7 +79,21 @@ void checkSequenceAccess(css::uno::Sequence<T> const& sequence, sal_Int32 index) template <typename T> void registerSequence(char const* name) { emscripten::class_<css::uno::Sequence<T>>(name) - .constructor(+[](sal_Int32 size) { + .constructor(+[](emscripten::val const& members) { + auto const len = members["length"].as<std::uint32_t>(); + if (len > std::numeric_limits<sal_Int32>::max()) + { + throw std::length_error("JavaScript array length too large for C++ UNO sequence"); + } + css::uno::Sequence<T> seq(len); + auto const p = seq.getArray(); + for (std::uint32_t i = 0; i != len; ++i) + { + p[i] = members[i].as<T>(); + } + return seq; + }) + .constructor(+[](sal_Int32 size, [[maybe_unused]] uno_Sequence) { checkSequenceSize(size); return css::uno::Sequence<T>(size); }) |