Skip to content

Support for Incomplete-Array in structs. #506

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

Open
mannprerak2 opened this issue Aug 6, 2020 · 6 comments
Open

Support for Incomplete-Array in structs. #506

mannprerak2 opened this issue Aug 6, 2020 · 6 comments
Labels
package:ffigen waiting-on-dart-ffi Issues which are blocked by lacking suppport in dart:ffi

Comments

@mannprerak2
Copy link
Contributor

C supports Flexible-Arrays as the last member of a struct having more than 1 member.
Structs of the following type -

struct T{
  int a;
  int b[]; // flexible array member, can only be at the end.
}

are not supported currently.

@dcharkes dcharkes added the waiting-on-dart-ffi Issues which are blocked by lacking suppport in dart:ffi label Aug 11, 2020
@Sunbreak
Copy link
Contributor

Could we support https://github.com/twain/twain-dsm/blob/master/TWAIN_DSM/src/twain.h#L218-L221

    typedef unsigned char TW_STR32[34],     FAR *pTW_STR32;
    typedef unsigned char TW_STR64[66],     FAR *pTW_STR64;
    typedef unsigned char TW_STR128[130],   FAR *pTW_STR128;
    typedef unsigned char TW_STR255[256],   FAR *pTW_STR255;

Which are used at https://github.com/twain/twain-dsm/blob/master/TWAIN_DSM/src/twain.h#L439

typedef struct {
   TW_UINT16  MajorNum;
   TW_UINT16  MinorNum;
   TW_UINT16  Language;
   TW_UINT16  Country;
   TW_STR32   Info;
} TW_VERSION, FAR * pTW_VERSION;

@mannprerak2
Copy link
Contributor Author

Hi @Sunbreak, add array-workaround: true in your config to generate this.

@Sunbreak
Copy link
Contributor

Hi @Sunbreak, add array-workaround: true in your config to generate this.

It result a quit long file with 16680 lines, T_T

@Sunbreak
Copy link
Contributor

I managed to hack like below:

// struct TW_VERSION {
//    TW_UINT16  MajorNum;
//    TW_UINT16  MinorNum;
//    TW_UINT16  Language;
//    TW_UINT16  Country;
//    TW_STR32   Info;
// };
class TWVersion extends _TWStruct<TW_VERSION> {
  static const _size = 2 + 2 + 2 + 2 + 34;

  TWVersion._fromAddress(int ptr): super._fromAddress(ptr);

  @override
  void dispose() {
    throw 'Undisposable nested structure';
  }

  @override
  int get size => _size;

  int get MajorNum => _getUint16(0);

  set MajorNum(int i) => _setUint16(0, i);

  int get MinorNum => _getUint16(2);

  set MinorNum(int i) => _setUint16(2, i);

  int get Language => _getUint16(4);

  set Language(int i) => _setUint16(4, i);

  int get Country => _getUint16(6);

  set Country(int i) => _setUint16(6, i);

  static const _InfoOffset = 2 + 2 + 2 + 2;

  String get Info => _getString(_InfoOffset, 34);

  set Info(String s) => _setString(_InfoOffset, s, 34);
}

It seems to work, even nested in another Struct _TWStruct<TW_IDENTITY>. But I'm not sure whether where is any problems

@mannprerak2
Copy link
Contributor Author

mannprerak2 commented Feb 24, 2021

It result a quit long file with 16680 lines, T_T

True, that's why we have chosen to keep it disabled by default. Hopefully, we should get inline array support soon.

I managed to hack...

That's a nice hack, but it cannot be used for all use cases (for example if these were pointers, the size would be 4/8 depending on the architecture).
Edit: Also you can't pass this by value.

@Sunbreak
Copy link
Contributor

Sunbreak commented Feb 7, 2022

Any plan?

@liamappelbe liamappelbe transferred this issue from dart-archive/ffigen Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package:ffigen waiting-on-dart-ffi Issues which are blocked by lacking suppport in dart:ffi
Projects
None yet
Development

No branches or pull requests

4 participants