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

(better) support for typed arrays #287

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

mildsunrise
Copy link
Contributor

See the commit messages for full change list, but basically

  • Use typed arrays for output (breaking change)
  • Allow typed arrays as input for GArray / GByteArray
  • Validate fixed array size & element size, and fix some typos

We still need a memcpy, but transferring large data buffers should be much faster now.

@mildsunrise
Copy link
Contributor Author

the last commit also allows accessing array fields on structs

@romgrk
Copy link
Owner

romgrk commented Apr 28, 2021

Yes, I've read the thing quickly but I have a work thing to finish by tomorrow, will be more available to review the PRs after that.

Two comments meanwhile:

  • Did you have a test case for array fields? If so it would be nice to add it to the test suite
  • I see some FIXME/g_assert_not_reached/g_critical. I like crashing rather than having UB, but would it be possible to use the ERROR(fmt, ...) macro for all those cases?

Copy link
Owner

@romgrk romgrk left a comment

Choose a reason for hiding this comment

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

This adds length-field handling for the field getters, but do you know how that handling works for field setters? Should we be setting the length when we set an array field?

src/gi.cc Outdated Show resolved Hide resolved
@mildsunrise
Copy link
Contributor Author

mildsunrise commented Apr 30, 2021

wait, the struct setters work with arrays? I thought they weren't primitives and you could only get the array, not set it

edit: according to the code, plain C arrays (the only kind of arrays where length makes sense) can't be set via a struct setter since it would require memory management. so we're fine. in the future we could maybe omit memcpy entirely and this would let users modify the array elements through the typed array

@mildsunrise
Copy link
Contributor Author

Yes, I've read the thing quickly but I have a work thing to finish by tomorrow, will be more available to review the PRs after that.

oh don't worry, the comment was just to keep track of the things in the PR. these PRs can perfectly wait :)

I'll fix the typo and the other two things you mentioned (test case and ERROR) when I find some time

@mildsunrise
Copy link
Contributor Author

in the meanwhile I've done some improvements to the GArray -> V8 conversion,
and also implemented conversion from GByteArray GValues <-> V8

I don't think there's an easy way to implement conversions for arbitrary GArrays for now, because we don't have info about the element GType

for input (v8 -> gi) conversion:

 - if fixed-size is set, validate that this size matches
   the size of the array that has been passed to us

 - fix bugs in typed array conversion into C array

 - implement typed array conversion into GArray as well

 - validate that the element size of the user's typedarray
   matches the element-size of the array we're emitting

 - for now, use g_assert_not_reached() because Nan exceptions
   are not propagated. in the future we could use MaybeLocal

for output (gi -> v8) conversion:

 - validate that element-size (if present) matches the type tag
we still memcpy(), but that's much more efficient than
element-by-element conversion to V8 for large buffers of data
- use the (now deprecated) GetContents() to get better
  compatibility across Node.js versions
- convert to Buffer instead of a plain Uint8Array
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.

None yet

2 participants