dart-lang / native

Dart packages related to FFI and native assets bundling.
BSD 3-Clause "New" or "Revised" License
83 stars 27 forks source link

C uintptr_t is being mapped to ffi.UnsignedLong instead of ffi.UIntPtr #494

Closed rogurotus closed 1 year ago

rogurotus commented 1 year ago

Hello. I discovered unexpected behavior

temp.h

#include <stdint.h>

uintptr_t temp_u();
intptr_t temp_i();

using ffigen on linux I get this.

...
  int temp_u() {
    return _temp_u();
  }

  late final _temp_uPtr =
      _lookup<ffi.NativeFunction<uintptr_t Function()>>('temp_u');
  late final _temp_u = _temp_uPtr.asFunction<int Function()>();

  int temp_i() {
    return _temp_i();
  }

  late final _temp_iPtr =
      _lookup<ffi.NativeFunction<ffi.IntPtr Function()>>('temp_i');
  late final _temp_i = _temp_iPtr.asFunction<int Function()>();

  typedef uintptr_t = ffi.UnsignedLong;
...

for some reason intptr_t converts correctly to ffi.IntPtr but uintptr_t is converted to typedef uintptr_t = ffi.UnsignedLong; Why is uintptr_t not converted to ffi.UIntPtr?

ffi.UnsignedLong is 64 for linux but 32 for windows, which causes errors when using generated dart code by linux on windows.

dcharkes commented 1 year ago

Which version of ffigen are you using?

It sounds very similar to

rogurotus commented 1 year ago

ffigen: ^7.2.3

rogurotus commented 1 year ago

size_t works correctly

dcharkes commented 1 year ago

This reproduces for me

  int temp_u() {
    return _temp_u();
  }

  late final _temp_uPtr =
      _lookup<ffi.NativeFunction<uintptr_t Function()>>('temp_u');
  late final _temp_u = _temp_uPtr.asFunction<int Function()>();

  int temp_i() {
    return _temp_i();
  }

  late final _temp_iPtr =
      _lookup<ffi.NativeFunction<ffi.IntPtr Function()>>('temp_i');
  late final _temp_i = _temp_iPtr.asFunction<int Function()>();

typedef uintptr_t = ffi.UnsignedLong;
dcharkes commented 1 year ago

It's missing here:

https://github.com/dart-lang/ffigen/blob/443fd70b69072c6894858485ce31544a44f6f87b/lib/src/header_parser/type_extractor/cxtypekindmap.dart#L25-L40

Adding it there works for me.

Would you like to contribute a PR for all types defined in https://github.com/dart-lang/sdk/blob/ff2d661d5ba07766fc27196e98df6c6ae80444b9/sdk/lib/ffi/c_type.dart @rogurotus ?

rogurotus commented 1 year ago

I will trust it to you

dcharkes commented 1 year ago

@mannprerak2 What is the difference between cxTypeKindToImportedTypes, suportedTypedefToSuportedNativeType, and supportedTypedefToImportedType in that file? Should they all be unified?

mannprerak2 commented 1 year ago

cxTypeKindToImportedTypes

This is for mapping the primitive types in C.

suportedTypedefToSuportedNativeType & supportedTypedefToImportedType

These are for mapping some supported types, such as those defined in #include<stdint.h>. These are behind a flag(use-supported-typedefs) in config and can be turned off. We can merge these 2 together by using ImportedType for UInt8, UInt16, etc as well.

dcharkes commented 1 year ago

Do you want to make the PR? Otherwise I can take a look tomorrow.

mannprerak2 commented 1 year ago

I can do it on the weekend if that's fine.

dcharkes commented 1 year ago

Thanks @mannprerak2 !

dcharkes commented 1 year ago

@rogurotus I have published v7.2.4 which includes the fix.

I can do it on the weekend if that's fine.

@mannprerak2 I want my weekends when your weekends start! 😄 Thanks! 🎸

mannprerak2 commented 1 year ago

@mannprerak2 I want my weekends when your weekends start! 😄 Thanks!

@dcharkes Haha, It's the same, just had some spare time in between meetings. 😃