-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add QJSValue, QJSEngine, and related types to cxx-qt-lib-extras with QJSValue serde support #1147
base: main
Are you sure you want to change the base?
Conversation
- Implement QJSValue with serialization and deserialization support - Add QJSEngine with methods for creating and manipulating JavaScript values - Implement QJSValueIterator for iterating over object properties - Create QJSValueList for managing lists of QJSValues - Add serializer and deserializer for converting between Rust types and QJSValues - Update Cargo.toml to include serde as an optional dependency - Extend build script to include new QML-related headers and source files
I actually think this should be in cxx-qt-lib proper with |
Looks interesting! I would be interested in usecases, as i've hardly used any of the QJS* APIs over multiple years of QML dev 😅 as it usually points that your backend is having knowledge of your frontend which is not generally a good idea. -extras is a good place for this to start, but as you say some of the API is also in QQmlEngine so may need some thought if that could work in -extras or not. |
That was just the easiest way to get JSON in and out of Javascript for a multiprocess extension system with user defined types. It also helped with build times since I didn’t need to run cxx-qt bridge generation and cc for a bunch of QML types that were basically just dumb records. The vast majority of my backend doesn’t know anything about QML or JS but there’s a lot of glue code that handles request/responses for the backend “services” that implement logic I think this also opens the door to auto-(de)serializing in invokables. For example (on my phone so this rough) #[qinvokable]
#[deserialize(bar)]
#[serialize_return]
fn foo(&self, bar: RustArgType) -> RustReturnType; |
Ah interesting so it's being used like an extended |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @akiselev ,
this PR looks super interesting. Like Andrew mentioned, I also haven't used much of QJSValue, but especially the serialize/deserialize aspect looks intriguing.
As is policy with -lib-extras, I'm happy to approve this PR, as long as "it compiles", which at the moment includes passing CI.
Please fix the formatting and add the appropriate licensing.
I left a comment, which you can also feel free to ignore, that was just a suggestion for a small trick you may not be familiar with yet :)
namespace cxxqtlib2 | ||
{ | ||
::std::unique_ptr<QJSEngine> | ||
qjsengineNew(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be possible to replace this with our make_unique template from cxx-qt-lib.
Take a look at the qpainter_init_default()
function in qpainter.rs for how to use this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CI can be quieted by including REUSE metadata, see the other files with SPDX for reference. Then run it over with cargo fmt
and then clang-format -i <your cpp/h file>
.
|
||
[build-dependencies] | ||
cxx-qt-build.workspace = true | ||
|
||
[features] | ||
default = [] | ||
default = ["serde"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: I'm unsure if serde should be a default here
@@ -0,0 +1,26 @@ | |||
// #include "cxx-qt-lib-extras/qapplication.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leftover include?
@@ -0,0 +1,25 @@ | |||
// #include "cxx-qt-lib-extras/qapplication.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
@@ -0,0 +1,124 @@ | |||
// #include "cxx-qt-lib-extras/qapplication.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
I made serde a default feature of
cxx-qt-lib-extras
to make sure tests run in CI, can update before mergeQJSEngine
is the entry point for manipulating the Javascript engine including evaluating arbitrary Javascript, creating new objects for the serializer, etc.QJSValue
allows accessing and manipulating Javascript valuesQJSValueList
is used for call Javascript functions from Rust but I haven't gotten around to the full implementation yetQJSValueIterator
is used by the (de)serializer support to iterate over objects and arrays.