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

fix(value): don't assume that zero-terminated array is of type gchar** #313

Merged
merged 2 commits into from
Jan 9, 2022

Conversation

peat-psuwit
Copy link
Contributor

The array's element type can be arbitrary, so we need to check the value
with the correct type. The long switch and a template function is used
to do this job.

We can't just memcmp() with item_size, because one of the type is float,
and we can't assume a 0.0 float is the same as 0 integer.

A test, based on how I discovered this, is added.

src/value.cc Outdated
Comment on lines 349 to 357
for (int i = 0; (i < length || length == -1); i++) {
void** pointer = (void**)((ulong)data + i * item_size);
memcpy(&value, pointer, item_size);

if (length == -1 && isZero(value, item_type_info))
break;

Copy link
Owner

Choose a reason for hiding this comment

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

This seems a bit hard to read. Can we clearly separate zero-terminated and non-zero terminated cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does the current version look better? My other alternative is:

    if (length >= 0) {
        for (int i = 0; i < length; i++) {
            void** pointer = (void**)((ulong)data + i * item_size);
            memcpy(&value, pointer, item_size);

            Nan::Set(array, i, GIArgumentToV8(item_type_info, &value));
        }
    } else {
        for (int i = 0; ; i++) {
            void** pointer = (void**)((ulong)data + i * item_size);
            memcpy(&value, pointer, item_size);

            if (isZero(value, item_type_info))
                break;

            Nan::Set(array, i, GIArgumentToV8(item_type_info, &value));
        }
    }

But this feels silly.

Copy link
Owner

Choose a reason for hiding this comment

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

No, current version still feels off :|
The alternative is nice because it avoid unnecessary comparisons, but I don't think it matters enough. So anyway put it back however you want, if you could just assign bool isZeroTerminated = length == -1; so things are clear for whoever reads it.

src/value.cc Outdated Show resolved Hide resolved
src/value.cc Outdated Show resolved Hide resolved
@peat-psuwit peat-psuwit force-pushed the exec_sync branch 2 times, most recently from 9328011 to 6bfb188 Compare October 3, 2021 17:00
@peat-psuwit
Copy link
Contributor Author

I don't understand the test failure on macos.

     Failed: OUT-array (zero-terminated, of guint8)
TypeError: Not enough arguments; expected 2, have 1
    at /Users/runner/work/node-gtk/node-gtk/tests/conversion__array.js:56:23
...

The annotation of the function is:

 * g_spawn_command_line_sync:
 * @command_line: (type filename): a command line
 * @standard_output: (out) (array zero-terminated=1) (element-type guint8) (optional): return location for child output
 * @standard_error: (out) (array zero-terminated=1) (element-type guint8) (optional): return location for child errors
 * @wait_status: (out) (optional): return location for child wait status, as returned by waitpid()
 * @error: return location for errors

So the JS version should take only 1 argument, the command_line. Besides, the test works on my machine running Ubuntu 20.04. Maybe it comes from that glib on the macos one is 2.70. I don't have a Mac that can debug this.

@romgrk
Copy link
Owner

romgrk commented Oct 4, 2021

Can't see the tests, can you rebase on master to fetch the updated workflow yaml file?

For the issue, maybe you could detect the OS or glib version, and provide the missing argument depending on that?

The array's element type can be arbitrary, so we need to check the value
with the correct type. The long switch and a template function is used
to do this job.

We can't just memcmp() with item_size, because one of the type is float,
and we can't assume a 0.0 float is the same as 0 integer.

A test, based on how I discovered this, is added.
@peat-psuwit
Copy link
Contributor Author

Can't see the tests, can you rebase on master to fetch the updated workflow yaml file?

Done. Please see the result.

For the issue, maybe you could detect the OS or glib version, and provide the missing argument depending on that?

The problem is, I don't know what I'm supposed to pass in as the second argument. Without a Mac, I can't take a look at the Gir file or use a debugger to find out what the second argument is.

@romgrk
Copy link
Owner

romgrk commented Oct 5, 2021

You can add the following line somewhere in the CI script to see the metadata for that function:

node -p "require('./lib/inspect').parseNamespace('GLib').spawn_command_line_sync"

@peat-psuwit peat-psuwit marked this pull request as draft October 10, 2021 16:27
@peat-psuwit
Copy link
Contributor Author

    ArgInfo {
      infoType: 'arg',
      name: 'exit_status',
      parent: [Circular *1],
      typeName: 'gint32',
      type: [TypeInfo],
      direction: 'IN',
      transfer: 'NOTHING'
    }

Huh?

@peat-psuwit
Copy link
Contributor Author

So, I got myself a macOS VM, and discovered a problem with Homebrew's gobject-introspection bottle. Put a workaround to rebuild that from source, and now the CI is green.

@peat-psuwit peat-psuwit marked this pull request as ready for review October 10, 2021 20:33
@romgrk
Copy link
Owner

romgrk commented Jan 9, 2022

Thanks :)

@binyamin: I'm very inconsistent in my presence on github due to lack of time :( As you have write access to the repo, I bless you with the power to merge PRs if you feel like they're complete. I can also give you access to NPM so you can publish in case I'm not around.
@peat-psuwit if you want write access the above also applies to you as you've been doing good contributions to the project.

@romgrk romgrk merged commit 3051382 into romgrk:master Jan 9, 2022
@ten0s ten0s mentioned this pull request Jul 8, 2022
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