Skip to content
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 support for array-length for parameters and return values #387

Merged
merged 6 commits into from
Jun 22, 2017

Conversation

sdroege
Copy link
Member

@sdroege sdroege commented Jun 19, 2017

#376

Only pointer arrays are currently supported, the translation trait is even called FromGlibPtrContainer. This should be changed really to also be able to handle all kinds of integer (incl. byte) arrays.

@sdroege
Copy link
Member Author

sdroege commented Jun 19, 2017

With #389 and this, Gtk.RecentInfo can be fully autogenerated btw.

self.parameters.push(Parameter::Out {
parameter: parameter.into(),
parameter: parameter_ffi_call_out::Parameter::new(parameter, array_length),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space

@@ -36,12 +36,12 @@ impl ToReturnValue for analysis::return_value::Info {
pub fn out_parameter_as_return_parts(analysis: &analysis::functions::Info)
-> (&'static str, &'static str) {
use analysis::out_parameters::Mode::*;
let is_tuple = analysis.outs.len() > 1;
let num_outs = analysis.outs.iter().filter(|p| p.array_length.is_none()).count();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fn len() now unused

@EPashkin
Copy link
Member

Bad change in glib:key_file - missed tuple

-    pub fn get_string_list(&self, group_name: &str, key: &str) -> Result<(Vec<String>, usize), Error> {
+    pub fn get_string_list(&self, group_name: &str, key: &str) -> Result<Vec<String>, usize, Error> {

@EPashkin
Copy link
Member

EPashkin commented Jun 19, 2017

..and still return usize

}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra line

@EPashkin
Copy link
Member

Too many long lines but code too.
Maybe code will be simpler is analysis parameter has is_array_len flag.

@EPashkin
Copy link
Member

or even SkipReason: None/InArrayLen/OutArrayLen

@EPashkin
Copy link
Member

*Too many long lines but it not big problem as code works.

@sdroege
Copy link
Member Author

sdroege commented Jun 19, 2017

Thanks, I'll clean this up tomorrow. For the code style, what do you think about my suggestion to just run things through rustfmt soon and then require pull requests to use the correct, automatically enforced format?

@EPashkin
Copy link
Member

also maybe better pass array_length: Option<(String, String)> directly to builder.

@EPashkin
Copy link
Member

About rustfmt, currently it change nothing in gir or panic with 'failed to emit error: operation not supported by the terminal'
Maybe it because some cargo bug: it build only if error stream relocated to file.
When run with 2>_ it also fails on build.rs when opening file gir\\src/git.rs (windows related problem).
Result code sometimes looks strange:

pub fn generate(
    w: &mut Write,
    env: &Env,
    prop: &ChildProperty,
    in_trait: bool,
    only_declaration: bool,
    indent: usize,
) -> Result<()> {
    try!(generate_func(
        w,
        env,
        prop,
        in_trait,
        only_declaration,
        indent,
        true,
    ));
    try!(generate_func(
        w,
        env,
        prop,
        in_trait,
        only_declaration,
        indent,
        false,
    ));

    Ok(())
}

So I don't against single apply in separate PR, but IMHO it don't ready to enforce.

@sdroege
Copy link
Member Author

sdroege commented Jun 20, 2017

The comments should all be covered now. Remaining problem is g_key_file_set_string_list(), which has the wrong type in the sys crate already. The list should be *const *const c_char but is not detected as such and also looks wrong in the .gir file. Once that is fixed, the ToGlibPtr instance for [&str] from glib/src/translate.rs has to be enabled again.
This seems to be a bug in gobject-introspection, where it fails to parse const gchar * const list[] correctly because of the square brackets probably.

Unrelated, g_strv_contains() is also wrong but not in the sys crate (it has const gchar * const * as type there, no square brackets). It should be &[&str] for the first parameter.

@EPashkin
Copy link
Member

Gio generation failed, other lib changes seems fine

D:\eap\rust\0\gir>cargo run --release -- -s -c D:\eap\rust\0\gio\Gir.toml
    Finished release [optimized] target(s) in 0.0 secs
     Running `D:\eap\rust\0\./target\release\gir.exe -s -c D:\eap\rust\0\gio\Gir.toml`
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src\libcore\option.rs:329
stack backtrace:
   0: std::sys_common::backtrace::_print
   1: std::panicking::default_hook::{{closure}}
   2: std::panicking::default_hook
   3: std::panicking::rust_panic_with_hook
   4: std::panicking::begin_panic
   5: std::panicking::begin_panic_fmt
   6: rust_begin_unwind
   7: core::panicking::panic_fmt
   8: core::panicking::panic
   9: gir::codegen::return_value::out_parameters_as_return
  10: gir::codegen::function::declaration
  11: gir::codegen::function::generate
  12: gir::codegen::object::generate
  13: gir::file_saver::save_to_file
  14: gir::codegen::objects::generate
  15: gir::codegen::normal_generate
  16: gir::do_main
  17: gir::main
  18: _rust_maybe_catch_panic
  19: std::rt::lang_start
  20: _tmainCRTStartup
  21: mainCRTStartup
  22: free_fail_stack_return
error: process didn't exit successfully: `D:\eap\rust\0\./target\release\gir.exe -s -c D:\eap\rust\0\gio\Gir.toml` (exit code: 101)

let ret_array_length = if let Some(pos) = analysis.ret.parameter.as_ref().and_then(|p| p.array_length) {
// The first parameter is &self in case of methods
let pos_offset = if analysis.kind == library::FunctionKind::Method { 1 } else { 0 };
analysis.parameters.iter().nth((pos + pos_offset) as usize)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clippy warning: called .iter().nth() on a Vec. Calling .get() is both faster and more readable

use analysis::out_parameters::Mode;
// The actual return value was inserted at position 0
let pos_offset = if analysis.outs.mode == Mode::Combined || analysis.outs.mode == Mode::Throws(true) { 1 } else { 0 };
analysis.parameters.iter().nth((pos + pos_offset) as usize)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

In the outs array they are the original one, in the params array not. It
might make sense to use analysis::parameter::Info instead of
library::Parameter for the outs array too.
@sdroege
Copy link
Member Author

sdroege commented Jun 21, 2017

https://bugzilla.gnome.org/show_bug.cgi?id=784020 (glib)
https://bugzilla.gnome.org/show_bug.cgi?id=784021 (pango)
https://bugzilla.gnome.org/show_bug.cgi?id=784022 (gtk)

For string parameters + array length, we currently still generate wrong code it seems. For some reason it is interpreted as a &[&str] then instead of a &str. E.g. in Pango after changing the .gir file for set_markup like this

-            <type name="utf8" c:type="const char*"/>
+            <array length="1" zero-terminated="0" c:type="char*">
+              <type name="utf8" c:type="char"/>
+            </array>

@EPashkin Any idea why, or should I research/debug?

@sdroege
Copy link
Member Author

sdroege commented Jun 21, 2017

https://bugzilla.gnome.org/show_bug.cgi?id=784035 (bug in gobject-introspection that causes it to drop const)

@EPashkin
Copy link
Member

<type name="utf8" c:type="char"/> gir think that type "utf8" is string and ignore "char" without * in normal mode.
In sys mode it maybe do different

@EPashkin
Copy link
Member

One symbol has other type name. IMHO pango_layout_set_markup accepts just text, and len need used from it.

@sdroege
Copy link
Member Author

sdroege commented Jun 21, 2017

gir think that type "utf8" is string and ignore "char" without * in normal mode.

Ah good point. I guess we need to parse the c:type of this, or the surrounding to know for sure. Otherwise this is ambiguous.

In sys mode it maybe do different

In sys mode it seems to count the asterisks (i.e. look at the c:type instead)

One symbol has other type name. IMHO pango_layout_set_markup accepts just text, and len need used from it.

What do you mean? It also has to be changed the same way as set_text, yes.

@EPashkin
Copy link
Member

I mean that pango_layout_set_markup accepts not array but utf8 string and array-length attribute may be cant applied to it. Same as "set_text".

@sdroege
Copy link
Member Author

sdroege commented Jun 21, 2017

They all accept a string, yes. Which is an array of utf8 characters, and the length argument is the number of characters in there. What you basically mean is what I wrote here https://bugzilla.gnome.org/show_bug.cgi?id=784035#c1 ?

In GLib and elsewhere, string parameters with an additional length parameter for the string length already use the (array length) annotation, and conceptually it's correct.

@EPashkin
Copy link
Member

I meant that string uft8 != array [u8] (at minimum it "implies" that contains valid utf8 characters)
so I don't think that annotations in https://bugzilla.gnome.org/show_bug.cgi?id=784021 can be applied to strings.

@sdroege
Copy link
Member Author

sdroege commented Jun 21, 2017

All those functions are working with UTF8 strings, not [u8]. The markup ones just in addition require the strings to be valid Pango markup (or things will fail), just like GTK labels if you configure them to show Pango markup

@EPashkin
Copy link
Member

IMHO all array-related problems is solved, can I merge this, or you want add something?

@sdroege
Copy link
Member Author

sdroege commented Jun 21, 2017

It's doing the wrong things for strings, I'd like to fix that as part of this. Otherwise the array length annotations on strings (as exist in various libraries already) cause wrong code to be generated.

But you could merge it now already if you want, and I'll work on that in another pull request then.

@EPashkin
Copy link
Member

Ok, I wait then

@sdroege
Copy link
Member Author

sdroege commented Jun 22, 2017

Good to merge. I discussed the string+length stuff with gobject-introspection people on IRC, and there's no way to correctly annotate that currently unless your "string" is actually an array of bytes (and not a UTF8 string), in which case it would be (array length=len) (element-type guint8).

@EPashkin
Copy link
Member

Thanks, @sdroege.
After merge I will wait for regen PRs (glib and gtk most needed as it don't builds).

@EPashkin EPashkin merged commit 2071a93 into gtk-rs:master Jun 22, 2017
@sdroege
Copy link
Member Author

sdroege commented Jun 22, 2017

I do them, you do them, ...?

@EPashkin
Copy link
Member

EPashkin commented Jun 22, 2017

I wait

😉

@sdroege
Copy link
Member Author

sdroege commented Jun 22, 2017

Alright, I'll try to get to that tonight then :)

@sdroege
Copy link
Member Author

sdroege commented Jun 24, 2017

All submitted

@EPashkin
Copy link
Member

@sdroege can you add link to all these PRs to this or better to #376 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants