diff options
author | Stephan Bergmann <stephan.bergmann@allotropia.de> | 2024-04-18 15:16:01 +0200 |
---|---|---|
committer | Stephan Bergmann <stephan.bergmann@allotropia.de> | 2024-04-18 22:16:33 +0200 |
commit | 75fe059974dcb80c3f78110c73ab799afc6f4ca3 (patch) | |
tree | d1b9141da932ed9dc8b076bdf2c733de715f2d52 /static | |
parent | 4322ec8f8f89549410d108551d21f5058b1c8261 (diff) |
Embind: throwUnoException from JS
(If throwUnoException directly took a css::uno::Any argument, JS client code
would need to create one with `new Module.uno_Any(...)` and call .delete() on
it, but there is no place where it could call .delete(), so make
throwUnoException take two arguments instead and assemble the css::uno::Any on
the C++ side.)
Change-Id: Iae4ae6af804354d5cf752115e272b79d61427440
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/166253
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <stephan.bergmann@allotropia.de>
Diffstat (limited to 'static')
-rw-r--r-- | static/source/unoembindhelpers/PrimaryBindings.cxx | 122 |
1 files changed, 64 insertions, 58 deletions
diff --git a/static/source/unoembindhelpers/PrimaryBindings.cxx b/static/source/unoembindhelpers/PrimaryBindings.cxx index fee0c3e214d1..7abf88669d02 100644 --- a/static/source/unoembindhelpers/PrimaryBindings.cxx +++ b/static/source/unoembindhelpers/PrimaryBindings.cxx @@ -16,6 +16,7 @@ #include <com/sun/star/uno/RuntimeException.hpp> #include <com/sun/star/uno/Type.hxx> #include <comphelper/processfactory.hxx> +#include <cppuhelper/exc_hlp.hxx> #include <o3tl/any.hxx> #include <o3tl/temporary.hxx> #include <o3tl/unreachable.hxx> @@ -191,6 +192,65 @@ std::type_info const* getTypeId(css::uno::Type const& type) } return i->second; } + +Any constructAny(const css::uno::Type& rUnoType, const val& rObject) +{ + switch (rUnoType.getTypeClass()) + { + case TypeClass_VOID: + return {}; + case TypeClass_BOOLEAN: + return Any{ rObject.as<bool>() }; + case TypeClass_BYTE: + return Any{ rObject.as<sal_Int8>() }; + case TypeClass_SHORT: + return Any{ rObject.as<sal_Int16>() }; + case TypeClass_UNSIGNED_SHORT: + return Any{ rObject.as<sal_uInt16>() }; + case TypeClass_LONG: + return Any{ rObject.as<sal_Int32>() }; + case TypeClass_UNSIGNED_LONG: + return Any{ rObject.as<sal_uInt32>() }; + case TypeClass_HYPER: + return Any{ rObject.as<sal_Int64>() }; + case TypeClass_UNSIGNED_HYPER: + return Any{ rObject.as<sal_uInt64>() }; + case TypeClass_FLOAT: + return Any{ rObject.as<float>() }; + case TypeClass_DOUBLE: + return Any{ rObject.as<double>() }; + case TypeClass_CHAR: + return Any{ rObject.as<char16_t>() }; + case TypeClass_STRING: + return Any{ OUString(rObject.as<std::u16string>()) }; + case TypeClass_TYPE: + return css::uno::Any(rObject.as<css::uno::Type>()); + case TypeClass_SEQUENCE: + case TypeClass_STRUCT: + case TypeClass_EXCEPTION: + case TypeClass_INTERFACE: + { + emscripten::internal::EM_DESTRUCTORS destructors = nullptr; + emscripten::internal::EM_GENERIC_WIRE_TYPE result + = _emval_as(rObject.as_handle(), getTypeId(rUnoType), &destructors); + emscripten::internal::DestructorsRunner dr(destructors); + return css::uno::Any(emscripten::internal::fromGenericWireType<void const*>(result), + rUnoType); + } + case TypeClass_ENUM: + { + emscripten::internal::EM_DESTRUCTORS destructors = nullptr; + emscripten::internal::EM_GENERIC_WIRE_TYPE result + = _emval_as(rObject.as_handle(), getTypeId(rUnoType), &destructors); + emscripten::internal::DestructorsRunner dr(destructors); + return css::uno::Any( + &o3tl::temporary(emscripten::internal::fromGenericWireType<sal_Int32>(result)), + rUnoType); + } + default: + throw std::invalid_argument("bad type class"); + } +} } namespace unoembindhelpers::detail @@ -248,64 +308,7 @@ EMSCRIPTEN_BINDINGS(PrimaryBindings) // Any class_<Any>("uno_Any") - .constructor(+[](const css::uno::Type& rUnoType, const val& rObject) -> Any { - switch (rUnoType.getTypeClass()) - { - case TypeClass_VOID: - return {}; - case TypeClass_BOOLEAN: - return Any{ rObject.as<bool>() }; - case TypeClass_BYTE: - return Any{ rObject.as<sal_Int8>() }; - case TypeClass_SHORT: - return Any{ rObject.as<sal_Int16>() }; - case TypeClass_UNSIGNED_SHORT: - return Any{ rObject.as<sal_uInt16>() }; - case TypeClass_LONG: - return Any{ rObject.as<sal_Int32>() }; - case TypeClass_UNSIGNED_LONG: - return Any{ rObject.as<sal_uInt32>() }; - case TypeClass_HYPER: - return Any{ rObject.as<sal_Int64>() }; - case TypeClass_UNSIGNED_HYPER: - return Any{ rObject.as<sal_uInt64>() }; - case TypeClass_FLOAT: - return Any{ rObject.as<float>() }; - case TypeClass_DOUBLE: - return Any{ rObject.as<double>() }; - case TypeClass_CHAR: - return Any{ rObject.as<char16_t>() }; - case TypeClass_STRING: - return Any{ OUString(rObject.as<std::u16string>()) }; - case TypeClass_TYPE: - return css::uno::Any(rObject.as<css::uno::Type>()); - case TypeClass_SEQUENCE: - case TypeClass_STRUCT: - case TypeClass_EXCEPTION: - case TypeClass_INTERFACE: - { - emscripten::internal::EM_DESTRUCTORS destructors = nullptr; - emscripten::internal::EM_GENERIC_WIRE_TYPE result - = _emval_as(rObject.as_handle(), getTypeId(rUnoType), &destructors); - emscripten::internal::DestructorsRunner dr(destructors); - return css::uno::Any( - emscripten::internal::fromGenericWireType<void const*>(result), rUnoType); - } - case TypeClass_ENUM: - { - emscripten::internal::EM_DESTRUCTORS destructors = nullptr; - emscripten::internal::EM_GENERIC_WIRE_TYPE result - = _emval_as(rObject.as_handle(), getTypeId(rUnoType), &destructors); - emscripten::internal::DestructorsRunner dr(destructors); - return css::uno::Any( - &o3tl::temporary( - emscripten::internal::fromGenericWireType<sal_Int32>(result)), - rUnoType); - } - default: - throw std::invalid_argument("bad type class"); - } - }) + .constructor(&constructAny) .function("get", +[](css::uno::Any const& self) { switch (self.getValueType().getTypeClass()) { @@ -395,6 +398,9 @@ EMSCRIPTEN_BINDINGS(PrimaryBindings) function("getCurrentModelFromViewSh", &getCurrentModelFromViewSh); function("getUnoComponentContext", &comphelper::getProcessComponentContext); + function("throwUnoException", +[](css::uno::Type const& type, emscripten::val const& value) { + cppu::throwException(constructAny(type, value)); + }); function("rtl_uString_release", +[](std::uintptr_t ptr) { rtl_uString_release(reinterpret_cast<rtl_uString*>(ptr)); }); |