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

[Feature] Config to expose unused typedefs #320

Closed
Sunbreak opened this issue Jun 8, 2021 · 4 comments · Fixed by #1266
Closed

[Feature] Config to expose unused typedefs #320

Sunbreak opened this issue Jun 8, 2021 · 4 comments · Fixed by #1266

Comments

@Sunbreak
Copy link
Contributor

Sunbreak commented Jun 8, 2021

dart-archive/ffigen#224 generates only the required typedefs, which results in hacks like this:

https://github.com/Sunbreak/dynamic_load.trial/blob/56953bb6e0b5d4badffb13bf3446cd794c914d26/example/ntdll/winternl.dart#L20

// FIXME: https://github.com/dart-lang/native/issues/320
abstract class SYSTEM_INFORMATION_CLASS {
}

class SYSTEM_PROCESS_INFORMATION extends ffi.Struct {
}

SYSTEM_INFORMATION_CLASS and SYSTEM_PROCESS_INFORMATION is a usecase for #359

@mannprerak2
Copy link
Contributor

Sorry, I don't understand. But aren't these enums and struct?

Can you highlight some C code and it's corresponding Dart code that you would like ffigen to generate?

@Sunbreak
Copy link
Contributor Author

Sunbreak commented Jun 9, 2021

Generated Dart code are private, so I have to define them myself as workaround. Below are generated Dart code:

abstract class _SYSTEM_INFORMATION_CLASS {
}
class _SYSTEM_PROCESS_INFORMATION extends ffi.Struct {
}

@mannprerak2
Copy link
Contributor

Typedefs to enums are never generated (even if it's a referred typedef)

I guess we can add a config for targetting these unused typedefs. And also generate referrred typedefs for enum since they can be non-private and be useful.

I'll try to make a PR soon, but for now you can simply rename the enum and struct to remove the underscore.

@liamappelbe liamappelbe transferred this issue from dart-archive/ffigen Nov 15, 2023
@rainyl
Copy link
Contributor

rainyl commented Jul 6, 2024

This feature is also useful when we want to split wrapper type definitions and functions or split the generated bindings to different files when the project becomes complicate and large.

e.g.,

// types.h
typedef void *SomeClassA;
typedef void *SomeClassB;
typedef void (*Callback)(void *);
...and many other similar typedefs.

then if we just generate this file, all typedefs will not be generated because they are not used.

I know that you may want to say: just include it in the headers that use it!

e.g.,

// moduleA.h
#include "types.h"

// return value is a status code
int create_A(Callback callback);
int A_getB(SomeClassA self, Callback callback);
// moduleA.cpp
#include "moduleA.h"

int create_A(Callback callback){
  callback(new SomeClassA());
  return 0;
}

int A_getB(SomeClassA self, Callback callback){
  SomeClassB *b = self->getB();  // here, since SomeClassB doesn't appear in moduleA.h, it will not be generated.
  callback(b);
  return 0;
}

Yes it works, but still, only those used in moduleA.h (SomeClassA) will be included, it will be annoying to use A_getB since SomeClassB is not generated.

The above is just a simple usage, if we want to split the bindings to different modules, it will be better to also generate unused typedefs such as SomeClassB, so we can just import the generated moduleA.generated.dart to use SomeClassB instead of import another types.generated.dart to access SomeClassB.

Edit 1:

This is particularly useful when we want to split different modules, e.g., if a moduleB.cpp uses some common typedefs from types.h, without symbol file imported, moduleB.generated.dart will re-define the common typedefs, which will cause conflicts, HOWEVER, if we want to import symbols generated from types.h, it's impossible for now because typedefs in types.h are NOT used!

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

Successfully merging a pull request may close this issue.

4 participants