static/source/unoembindhelpers/PrimaryBindings.cxx | 12 ++++++++-- unotest/source/embindtest/embindtest.js | 24 ++++++++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-)
New commits: commit ac175a43c95457129c418ae0efb9cbe5f32f9ee5 Author: Stephan Bergmann <stephan.bergm...@allotropia.de> AuthorDate: Thu Jul 25 18:27:46 2024 +0200 Commit: Stephan Bergmann <stephan.bergm...@allotropia.de> CommitDate: Thu Jul 25 22:39:23 2024 +0200 Add a toDelete parameter to Module.throwUnoException ...as there can be situations (cf. the newly added test code in embindtest.js) where objects that need to be deleted are passed as payload of the exception value to be thrown. So there now is a (mandatory, as Embind doesn't allow to implicitly pass undefined) third parameter now that must be an array of objects on which to call .delete(). (75fe059974dcb80c3f78110c73ab799afc6f4ca3 "Embind: throwUnoException from JS" had deliberately made throwUnoException take two arguments, because if it "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()". There now would be such a place, but it would probably still be tedious most of the time to explicitly construct a new uno_Any to pass into throwUnoException both as the exception and in the toDelete array. So keep that design decision to have throwUnoException take individual type and value arguments.) Change-Id: Idec029d9e500457b02d20d899b9a2328cd7a5d7e Reviewed-on: https://gerrit.libreoffice.org/c/core/+/171024 Reviewed-by: Stephan Bergmann <stephan.bergm...@allotropia.de> Tested-by: Jenkins diff --git a/static/source/unoembindhelpers/PrimaryBindings.cxx b/static/source/unoembindhelpers/PrimaryBindings.cxx index f1e57f281220..ab2adecad091 100644 --- a/static/source/unoembindhelpers/PrimaryBindings.cxx +++ b/static/source/unoembindhelpers/PrimaryBindings.cxx @@ -34,6 +34,7 @@ #include <uno/data.h> #include <cassert> +#include <cstddef> #include <cstdint> #include <stdexcept> #include <string> @@ -407,8 +408,15 @@ 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("throwUnoException", +[](css::uno::Type const& type, emscripten::val const& value, + emscripten::val const& toDelete) { + auto const any = constructAny(type, value); + auto const len = toDelete["length"].as<std::size_t>(); + for (std::size_t i = 0; i != len; ++i) + { + toDelete[i].call<void>("delete"); + } + cppu::throwException(any); }); function("sameUnoObject", +[](css::uno::Reference<css::uno::XInterface> const& ref1, diff --git a/unotest/source/embindtest/embindtest.js b/unotest/source/embindtest/embindtest.js index a81937deed87..ba25b050ffb7 100644 --- a/unotest/source/embindtest/embindtest.js +++ b/unotest/source/embindtest/embindtest.js @@ -645,6 +645,28 @@ Module.uno_init.then(function() { console.assert(exc.Message.startsWith('test')); any.delete(); } + try { + const wrapped = new Module.uno_Any( + Module.uno_Type.Exception('com.sun.star.uno.RuntimeException'), + {Message: 'test', Context: test}); + Module.throwUnoException( + Module.uno_Type.Exception('com.sun.star.lang.WrappedTargetException'), + {Message: 'wrapped', Context: test, TargetException: wrapped}, [wrapped]); + console.assert(false); + } catch (e) { + const any = Module.catchUnoException(e); + console.assert(any.getType() == 'com.sun.star.lang.WrappedTargetException'); + const exc = any.get(); + console.assert(exc.Message.startsWith('wrapped')); + console.assert(Module.sameUnoObject(exc.Context, test)); + const wrappedAny = exc.TargetException; + console.assert(wrappedAny.getType() == 'com.sun.star.uno.RuntimeException'); + const wrappedExc = wrappedAny.get(); + console.assert(wrappedExc.Message.startsWith('test')); + console.assert(Module.sameUnoObject(wrappedExc.Context, test)); + any.delete(); + wrappedAny.delete(); + } const obj = Module.unoObject( ['com.sun.star.task.XJob', 'com.sun.star.task.XJobExecutor', 'org.libreoffice.embindtest.XAttributes'], @@ -653,7 +675,7 @@ Module.uno_init.then(function() { if (args.size() !== 1 || args.get(0).Name !== 'name') { Module.throwUnoException( Module.uno_Type.Exception('com.sun.star.lang.IllegalArgumentException'), - {Message: 'bad args', Context: null, ArgumentPosition: 0}); + {Message: 'bad args', Context: null, ArgumentPosition: 0}, []); } console.log('Hello ' + args.get(0).Value.get()); return new Module.uno_Any(Module.uno_Type.Void(), undefined);