From 8949026c63dac2fc4ff9bb0fdfeab4790d77fca5 Mon Sep 17 00:00:00 2001 From: Jos van den Oever Date: Thu, 17 May 2018 10:47:18 +0200 Subject: [PATCH] Make primitive types more precise and add test --- demo/src/Bindings.cpp | 8 ++++---- demo/src/Bindings.h | 2 +- src/cpp.cpp | 10 ++++++---- src/parseJson.cpp | 8 ++++---- tests/rust_functions/src/implementation.rs | 6 ++++++ tests/rust_functions/src/interface.rs | 10 ++++++++++ tests/test_functions.cpp | 12 ++++++++++++ tests/test_functions.json | 14 ++++++++++++++ tests/test_functions_rust.cpp | 5 +++++ tests/test_functions_rust.h | 1 + tests/test_list_types_rust.cpp | 8 ++++---- tests/test_object_types_rust.cpp | 8 ++++---- tests/test_object_types_rust.h | 4 ++-- 13 files changed, 73 insertions(+), 23 deletions(-) diff --git a/demo/src/Bindings.cpp b/demo/src/Bindings.cpp index 3ddb32c..b42866e 100644 --- a/demo/src/Bindings.cpp +++ b/demo/src/Bindings.cpp @@ -114,7 +114,7 @@ extern "C" { Fibonacci::Private* fibonacci_new(Fibonacci*, void (*)(Fibonacci*), void (*)(Fibonacci*)); void fibonacci_free(Fibonacci::Private*); quint32 fibonacci_input_get(const Fibonacci::Private*); - void fibonacci_input_set(Fibonacci::Private*, uint); + void fibonacci_input_set(Fibonacci::Private*, quint32); quint64 fibonacci_result_get(const Fibonacci::Private*); }; @@ -484,8 +484,8 @@ extern "C" { float processes_data_cpu_usage(const Processes::Private*, quintptr); quint64 processes_data_memory(const Processes::Private*, quintptr); void processes_data_name(const Processes::Private*, quintptr, QString*, qstring_set); - uint processes_data_pid(const Processes::Private*, quintptr); - uint processes_data_uid(const Processes::Private*, quintptr); + quint32 processes_data_pid(const Processes::Private*, quintptr); + quint32 processes_data_uid(const Processes::Private*, quintptr); void processes_sort(Processes::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder); int processes_row_count(const Processes::Private*, quintptr, bool); @@ -1182,7 +1182,7 @@ quint32 Fibonacci::input() const { return fibonacci_input_get(m_d); } -void Fibonacci::setInput(uint v) { +void Fibonacci::setInput(quint32 v) { fibonacci_input_set(m_d, v); } quint64 Fibonacci::result() const diff --git a/demo/src/Bindings.h b/demo/src/Bindings.h index 3d809b3..cd60549 100644 --- a/demo/src/Bindings.h +++ b/demo/src/Bindings.h @@ -68,7 +68,7 @@ public: explicit Fibonacci(QObject *parent = nullptr); ~Fibonacci(); quint32 input() const; - void setInput(uint v); + void setInput(quint32 v); quint64 result() const; signals: void inputChanged(); diff --git a/src/cpp.cpp b/src/cpp.cpp index b4349d2..b704bc5 100644 --- a/src/cpp.cpp +++ b/src/cpp.cpp @@ -579,10 +579,12 @@ public: for (auto f: o.functions) { h << " Q_INVOKABLE " << f.type.name << " " << f.name << "("; for (auto a = f.args.begin(); a < f.args.end(); a++) { - h << QString("%1 %2%3").arg(a->type.cppSetType, a->name, a + 1 < f.args.end() ? ", " : ""); + if (a != f.args.begin()) { + h << ", "; + } + h << QString("%1 %2").arg(a->type.cppSetType, a->name); } - h << QString(")%1;") - .arg(f.mut ? "" : " const"); + h << QString(")%1;").arg(f.mut ? "" : " const"); h << endl; } if (baseType(o) == "QAbstractItemModel") { @@ -739,7 +741,7 @@ void writeFunctionCDecl(QTextStream& cpp, const Function& f, const QString& lcna } else if (a->type.name == "QByteArray") { cpp << ", const char*, int"; } else { - cpp << ", " << a->type.rustType; + cpp << ", " << a->type.name; } } // If the return type is QString or QByteArray, append a pointer to the diff --git a/src/parseJson.cpp b/src/parseJson.cpp index f5e0254..edcad34 100644 --- a/src/parseJson.cpp +++ b/src/parseJson.cpp @@ -68,8 +68,8 @@ QList& bindingTypeProperties() { f.append({ .type = BindingType::UInt16, .name = "quint16", - .cppSetType = "uint", - .cSetType = "uint", + .cppSetType = "quint16", + .cSetType = "quint16", .rustType = "u16", .rustTypeInit = "0" }); @@ -84,8 +84,8 @@ QList& bindingTypeProperties() { f.append({ .type = BindingType::UInt32, .name = "quint32", - .cppSetType = "uint", - .cSetType = "uint", + .cppSetType = "quint32", + .cSetType = "quint32", .rustType = "u32", .rustTypeInit = "0" }); diff --git a/tests/rust_functions/src/implementation.rs b/tests/rust_functions/src/implementation.rs index 043cc48..4f7fa62 100644 --- a/tests/rust_functions/src/implementation.rs +++ b/tests/rust_functions/src/implementation.rs @@ -30,6 +30,12 @@ impl PersonTrait for Person { self.user_name = format!("{}{}", self.user_name, self.user_name); } + fn append(&mut self, suffix: String, amount: u32) { + for _ in 0..amount { + self.user_name += &suffix; + } + } + fn greet(&self, name: String) -> String { format!("Hello {}, my name is {}, how is it going?", name, self.user_name) } diff --git a/tests/rust_functions/src/interface.rs b/tests/rust_functions/src/interface.rs index 364105f..1758eb6 100644 --- a/tests/rust_functions/src/interface.rs +++ b/tests/rust_functions/src/interface.rs @@ -50,6 +50,7 @@ pub trait PersonTrait { fn emit(&self) -> &PersonEmitter; fn user_name(&self) -> &str; fn set_user_name(&mut self, value: String); + fn append(&mut self, suffix: String, amount: u32) -> (); fn double_name(&mut self) -> (); fn greet(&self, name: String) -> String; fn quote(&self, prefix: String, suffix: String) -> String; @@ -95,6 +96,15 @@ pub extern "C" fn person_user_name_set(ptr: *mut Person, v: *const c_ushort, len o.set_user_name(s); } +#[no_mangle] +pub extern "C" fn person_append(ptr: *mut Person, suffix_str: *const c_ushort, suffix_len: c_int, amount: u32) -> () { + let mut suffix = String::new(); + set_string_from_utf16(&mut suffix, suffix_str, suffix_len); + let o = unsafe { &mut *ptr }; + let r = o.append(suffix, amount); + r +} + #[no_mangle] pub extern "C" fn person_double_name(ptr: *mut Person) -> () { let o = unsafe { &mut *ptr }; diff --git a/tests/test_functions.cpp b/tests/test_functions.cpp index df6922c..b484ce1 100644 --- a/tests/test_functions.cpp +++ b/tests/test_functions.cpp @@ -30,6 +30,7 @@ private slots: void testStringFunction(); void testSimpleFunction(); void testVoidFunction(); + void testAppendFunction(); void testQuoteFunction(); void testQuoteBytesFunction(); }; @@ -61,6 +62,17 @@ void TestRustObject::testVoidFunction() QCOMPARE(person.userName(), QString("KonqiKonqi")); } +void TestRustObject::testAppendFunction() +{ + // GIVEN + Person person; + person.setUserName("Konqi"); + + // THEN + person.append("!", 3); + QCOMPARE(person.userName(), QString("Konqi!!!")); +} + void TestRustObject::testQuoteFunction() { // GIVEN diff --git a/tests/test_functions.json b/tests/test_functions.json index 0c6d42e..6046bf7 100644 --- a/tests/test_functions.json +++ b/tests/test_functions.json @@ -30,6 +30,20 @@ "mut": true, "arguments": [] }, + "append": { + "return": "void", + "mut": true, + "arguments": [ + { + "name": "suffix", + "type": "QString" + }, + { + "name": "amount", + "type": "quint32" + } + ] + }, "vowelsInName": { "return": "quint8", "arguments": [] diff --git a/tests/test_functions_rust.cpp b/tests/test_functions_rust.cpp index 0fd3fe5..4e53979 100644 --- a/tests/test_functions_rust.cpp +++ b/tests/test_functions_rust.cpp @@ -17,6 +17,7 @@ extern "C" { void person_free(Person::Private*); void person_user_name_get(const Person::Private*, QString*, qstring_set); void person_user_name_set(Person::Private*, const ushort *str, int len); + void person_append(Person::Private*, const ushort*, int, quint32); void person_double_name(Person::Private*); void person_greet(const Person::Private*, const ushort*, int, QString*, qstring_set); void person_quote(const Person::Private*, const ushort*, int, const ushort*, int, QString*, qstring_set); @@ -53,6 +54,10 @@ QString Person::userName() const void Person::setUserName(const QString& v) { person_user_name_set(m_d, reinterpret_cast(v.data()), v.size()); } +void Person::append(const QString& suffix, quint32 amount) +{ + return person_append(m_d, suffix.utf16(), suffix.size(), amount); +} void Person::doubleName() { return person_double_name(m_d); diff --git a/tests/test_functions_rust.h b/tests/test_functions_rust.h index f5a82e6..fdf2945 100644 --- a/tests/test_functions_rust.h +++ b/tests/test_functions_rust.h @@ -22,6 +22,7 @@ public: ~Person(); QString userName() const; void setUserName(const QString& v); + Q_INVOKABLE void append(const QString& suffix, quint32 amount); Q_INVOKABLE void doubleName(); Q_INVOKABLE QString greet(const QString& name) const; Q_INVOKABLE QString quote(const QString& prefix, const QString& suffix) const; diff --git a/tests/test_list_types_rust.cpp b/tests/test_list_types_rust.cpp index 2290bdc..acb5579 100644 --- a/tests/test_list_types_rust.cpp +++ b/tests/test_list_types_rust.cpp @@ -80,10 +80,10 @@ extern "C" { bool list_set_data_optional_string_none(List::Private*, int); void list_data_string(const List::Private*, int, QString*, qstring_set); bool list_set_data_string(List::Private*, int, const ushort* s, int len); - uint list_data_u16(const List::Private*, int); - bool list_set_data_u16(List::Private*, int, uint); - uint list_data_u32(const List::Private*, int); - bool list_set_data_u32(List::Private*, int, uint); + quint16 list_data_u16(const List::Private*, int); + bool list_set_data_u16(List::Private*, int, quint16); + quint32 list_data_u32(const List::Private*, int); + bool list_set_data_u32(List::Private*, int, quint32); quint64 list_data_u64(const List::Private*, int); bool list_set_data_u64(List::Private*, int, quint64); quint8 list_data_u8(const List::Private*, int); diff --git a/tests/test_object_types_rust.cpp b/tests/test_object_types_rust.cpp index afee15d..6b9523c 100644 --- a/tests/test_object_types_rust.cpp +++ b/tests/test_object_types_rust.cpp @@ -146,9 +146,9 @@ extern "C" { void object_string_get(const Object::Private*, QString*, qstring_set); void object_string_set(Object::Private*, const ushort *str, int len); quint16 object_u16_get(const Object::Private*); - void object_u16_set(Object::Private*, uint); + void object_u16_set(Object::Private*, quint16); quint32 object_u32_get(const Object::Private*); - void object_u32_set(Object::Private*, uint); + void object_u32_set(Object::Private*, quint32); quint64 object_u64_get(const Object::Private*); void object_u64_set(Object::Private*, quint64); quint8 object_u8_get(const Object::Private*); @@ -320,14 +320,14 @@ quint16 Object::u16() const { return object_u16_get(m_d); } -void Object::setU16(uint v) { +void Object::setU16(quint16 v) { object_u16_set(m_d, v); } quint32 Object::u32() const { return object_u32_get(m_d); } -void Object::setU32(uint v) { +void Object::setU32(quint32 v) { object_u32_set(m_d, v); } quint64 Object::u64() const diff --git a/tests/test_object_types_rust.h b/tests/test_object_types_rust.h index 0dba40b..69b1863 100644 --- a/tests/test_object_types_rust.h +++ b/tests/test_object_types_rust.h @@ -63,9 +63,9 @@ public: QString string() const; void setString(const QString& v); quint16 u16() const; - void setU16(uint v); + void setU16(quint16 v); quint32 u32() const; - void setU32(uint v); + void setU32(quint32 v); quint64 u64() const; void setU64(quint64 v); quint8 u8() const;