The ValueRef helper class now keeps a pointer to the JS_Runtime instead
of the JS_Context and uses that to free the JSValue instead. This is
needed if the ValueRef is kept alive until the end of the execution of
the script because in that case the JS_Context will be destroyed before
the final garbage collection is run. This can happen if the ValueRef is
being stored in the opaque data for an object such as is the case for
the WrapperData struct.
This was picked up in the following ubsan build:
https://ci.libreoffice.org/job/lo_ubsan/3913/consoleFull#-1184025599d893063f-7f3d-4b7e-b56f-4e0f225817cd
It is also picked up by running CppunitTest_jsuno_testuno under
valgrind.
Change-Id: Ic929fb8ab9015ddcbb9fd69a4a7985e9d2b18b0a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/205353
Reviewed-by: Neil Roberts <nroberts@libreoffice.org>
Tested-by: Jenkins
Adds the following features in order to make the QuickJS engine slightly
more compatible with the Rhino engine:
Makes “Packages” be an alias for “uno.idl”
Adds an implementation of importClass. This tries to get the name of the
thing that is being imported and then sets a global variable with that
name to point to that thing. The name is retrieved by adding a
getImportClassName method to most of the objects returned by
moduleGetOwnProperty.
Inserts a com.sun.star.uno.UnoRuntime object with a hack on
moduleGetOwnProperty. This object has a method called “queryInterface”
to allow doing UnoRuntime.queryInterface(interface,obj). The
implementation just calls
obj.queryInterface(uno.type.interface(interface))
This is enough to run the HelloWorld example from the Rhino JavaScript
provider.
Change-Id: I569fe4dad4cd8cdc6029a60f8e5421a5de1588c2
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/204912
Tested-by: Jenkins
Reviewed-by: Neil Roberts <nroberts@libreoffice.org>
The main bulk of moduleGetOwnProperty is now split out into a helper
function that just directly returns the property value. The caller then
handles the caching. This makes it easier to add an early return with
different values in a later patch.
Change-Id: Ifaf346df7d1d576cc0a466eb3bd4a3dc2cfab732
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/204911
Tested-by: Jenkins
Reviewed-by: Neil Roberts <nroberts@libreoffice.org>
Instead of directly adding the toString method to an object every time
an instance of the appropriate class is created, the method is now added
to a prototype object and this is set on the class.
Change-Id: I7c69cb5294ba52a84a88aba354684263d4b3736c
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/204910
Tested-by: Jenkins
Reviewed-by: Neil Roberts <nroberts@libreoffice.org>
get_property completely overrides the property resolution mechanism and
stops QuickJS from looking at the prototypes. If we replace it with
get_own_property, as well as fixing that it will make has_property work
correctly too. Sadly this also means we have to override the property
setter, which means we can’t just call JS_SetProperty on the object when
we want to cache the value. Instead, the wrapper and module classes now
have a struct as their opaque data and this contains an additional JS
Object to use as a hash table for the cache.
Change-Id: Ia1580ba2017d6376c6f8565ceff6bd46afa7ffc4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/204909
Tested-by: Jenkins
Reviewed-by: Neil Roberts <nroberts@libreoffice.org>
Now that endsWith has an overload that takes a single character, we
might as well use it here. This also uses the “rest” parameter to avoid
the if-statement.
Change-Id: I7b82e65a943715bb4a65574482944c846a696396
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/204908
Tested-by: Jenkins
Reviewed-by: Neil Roberts <nroberts@libreoffice.org>
More of the tests will later want to use the assertError function so
let’s move it to a shared helper file.
Change-Id: Ib19e51e659214f8d7f08cedd98bf54f4cf06d4ee
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/204907
Tested-by: Jenkins
Reviewed-by: Neil Roberts <nroberts@libreoffice.org>
Adds an optional parameter to jsuno::execute to specify a list of global
variables to define in the global scope of the running script. The
values are references to XInterfaces.
Change-Id: I03b7de9fb904f4a172324f0e54c11ff9b0d76134
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/205120
Tested-by: Jenkins
Reviewed-by: Neil Roberts <nroberts@libreoffice.org>
Previously the functions that have required arguments were checking that
the right number of arguments were passed with an assert. On debug
builds this would cause LibreOffice to just crash when an argument was
missing. You might think that the length parameter to JS_NewCFunction
would make QuickJS itself check that enough arguments were passed, but
instead that just makes sure that the array passed to the C function has
enough space for that many arguments.
This patch replaces those checks with an exception on failure.
Change-Id: Ief0f48bca684dd2b4379711ab7650edd548ceef4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/204302
Reviewed-by: Neil Roberts <nroberts@libreoffice.org>
Tested-by: Jenkins
Somewhat loosely based on <https://github.com/allotropia/zetajs>.
Main part that is still missing from this WIP commit is implementing UNO objects
in JS. (And there are a number of TODOs throughout, mostly regarding error
handling.)
===
Comments from Neil Roberts:
This patch was cherry-picked from
1cd8ad398b38763cb3578f1305a28e7774163714 in Collabora’s repo. The
following changes were made in order to get it to pass the CI:
* The unit test has been modified to continue using embindtest instead
of testuno.
* The name of the external library is quickjs instead of qjs because a
different patch is used to add the external QuickJS package.
* The asserts on argc that were captured in the lambda function have been
moved outside of the lambda function in order to avoid having an unused
capture on release builds. The argc parameter has then been marked as
[[maybe_unused]].
* The constructors for JSClassDef have been changed to consistently use
{ } brace initialisers because otherwise it fails to build on macOS.
===
Change-Id: I8810e43fe5f3e4b1a53a654070fe87e95f51231a
Reviewed-on: https://gerrit.collaboraoffice.com/c/core/+/190
Tested-by: Jenkins CPCI <releng@collaboraoffice.com>
Reviewed-by: Michael Stahl <michael.stahl@collabora.com>
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/203963
Tested-by: Jenkins
Reviewed-by: Neil Roberts <nroberts@libreoffice.org>