diff options
author | Stephan Bergmann <stephan.bergmann@allotropia.de> | 2024-06-12 13:20:46 +0200 |
---|---|---|
committer | Stephan Bergmann <stephan.bergmann@allotropia.de> | 2024-06-12 15:44:12 +0200 |
commit | 887ebfd6e56069d281cd0c1ce893b794a3d854d3 (patch) | |
tree | 9b45c779093d0de25f4098c68b2c07c88c886aea /static | |
parent | 13874ba730ed94a38271e3b053e11a28f7d8268f (diff) |
Embind: Centrally initialize via Module.initUno() in a new uno.js
...so that the unoObject function can be moved there too (so that other code
outside embindtest.js can reuse it)
Change-Id: Id3edb7cede56321db29ba435f221cb7702a3513c
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/168700
Reviewed-by: Stephan Bergmann <stephan.bergmann@allotropia.de>
Tested-by: Jenkins
Diffstat (limited to 'static')
-rw-r--r-- | static/README.wasm.md | 8 | ||||
-rw-r--r-- | static/emscripten/uno.js | 82 |
2 files changed, 86 insertions, 4 deletions
diff --git a/static/README.wasm.md b/static/README.wasm.md index c07c3a9a389c..7d69716e7173 100644 --- a/static/README.wasm.md +++ b/static/README.wasm.md @@ -210,8 +210,8 @@ improvement! ;) Some usage examples through javascript of the current implementation: ```js // inserts a string at the start of the Writer document. -const uno = init_unoembind_uno(Module); -const css = uno.com.sun.star; +Module.initUno(); +const css = Module.uno.com.sun.star; const xModel = Module.getCurrentModelFromViewSh(); const xTextDocument = css.text.XTextDocument.query(xModel); const xText = xTextDocument.getText(); @@ -221,8 +221,8 @@ xTextCursor.setString("string here!"); ```js // changes each paragraph of the Writer document to a random color. -const uno = init_unoembind_uno(Module); -const css = uno.com.sun.star; +Module.initUno(); +const css = Module.uno.com.sun.star; const xModel = Module.getCurrentModelFromViewSh(); const xTextDocument = css.text.XTextDocument.query(xModel); const xText = xTextDocument.getText(); diff --git a/static/emscripten/uno.js b/static/emscripten/uno.js new file mode 100644 index 000000000000..4f3b2e69849d --- /dev/null +++ b/static/emscripten/uno.js @@ -0,0 +1,82 @@ +/* -*- Mode: JS; 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.initUno = function() { + if (Module.uno === undefined) { + Module.uno = init_unoembind_uno(Module); + } +}; + +Module.unoObject = function(interfaces, obj) { + Module.initUno(); + interfaces = ['com.sun.star.lang.XTypeProvider'].concat(interfaces); + obj.impl_refcount = 0; + obj.impl_types = new Module.uno_Sequence_type(interfaces.length, Module.uno_Sequence.FromSize); + for (let i = 0; i !== interfaces.length; ++i) { + obj.impl_types.set(i, Module.uno_Type.Interface(interfaces[i])); + } + obj.impl_implementationId = new Module.uno_Sequence_byte([]); + obj.queryInterface = function(type) { + for (const i in obj._types) { + if (i === type.toString()) { + return new Module.uno_Any( + type, + Module['uno_Type_' + i.replace(/\./g, '$')].reference( + obj._impl[obj._types[i]])); + } + } + return new Module.uno_Any(Module.uno_Type.Void(), undefined); + }; + obj.acquire = function() { ++obj.impl_refcount; }; + obj.release = function() { + if (--obj.impl_refcount === 0) { + for (const i in obj._impl) { + i.delete(); + } + obj.impl_types.delete(); + obj.impl_implementationId.delete(); + } + }; + obj.getTypes = function() { return obj.impl_types; }; + obj.getImplementationId = function() { return obj.impl_implementationId; }; + obj._impl = {}; + interfaces.forEach((i) => { + obj._impl[i] = Module['uno_Type_' + i.replace(/\./g, '$')].implement(obj); + }); + obj._types = {}; + const walk = function(td, impl) { + const name = td.getName(); + if (!Object.hasOwn(obj._types, name)) { + if (td.getTypeClass() != Module.uno.com.sun.star.uno.TypeClass.INTERFACE) { + throw new Error('not a UNO interface type: ' + name); + } + obj._types[name] = impl; + const bases = Module.uno.com.sun.star.reflection.XInterfaceTypeDescription2.query(td) + .getBaseTypes(); + for (let i = 0; i !== bases.size(); ++i) { + walk(bases.get(i), impl) + } + bases.delete(); + } + }; + const tdmAny = Module.getUnoComponentContext().getValueByName( + '/singletons/com.sun.star.reflection.theTypeDescriptionManager'); + const tdm = Module.uno.com.sun.star.container.XHierarchicalNameAccess.query(tdmAny.get()); + interfaces.forEach((i) => { + const td = tdm.getByHierarchicalName(i); + walk(Module.uno.com.sun.star.reflection.XTypeDescription.query(td.get()), i); + td.delete(); + }) + tdmAny.delete(); + obj._types['com.sun.star.uno.XInterface'] = 'com.sun.star.lang.XTypeProvider'; + obj.acquire(); + return obj; +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |