Closed tsavo-at-pieces closed 1 month ago
@mraleph @dcharkes perhaps this is something that one of you might know about?
Regardless, just something I noticed and it's not broken at the moment although did trip me up pretty hard. I was writing some pretty funny C code to emulate and debug this with ffigen 😅
Thanks! -T
@tsavo-at-pieces nice to meet you!
If mode_t
is an UnsignedLong
or UnsignedShort
, we should generate an AbiSpecificInteger
.
/// The C `mode_t` type.
@AbiSpecificIntegerMapping({
Abi.macosArm64: Uint64(),
Abi.macosX64: Uint16(),
})
final class Mode extends AbiSpecificInteger {
const Mode();
}
See the documentation on type-map
in the readme on how to make FFIgen generate Mode
instead of UnsignedLong
or UnsignedShort
.
type-map:
'native-types':
'mode_t':
'lib': 'your_lib_where_you_define_Mode'
'c-type': 'Mode'
'dart-type': 'int'
Nice to meet you as well @dcharkes wonderful work being done over in these parts!
I did make it that far which was how I was able to determine the nuance for sem_open 🙌
That said I was just using ffigen in development and ended up removing it from the final dependency once I knew the types thanks to the elegant @Native decorators ✨
I guess my question was more around how I could achieve a similar effect only using the decorators when I do know the type discrepancies between architectures.
Lmk thoughts and thanks again for the quick ping back here!
I guess my question was more around how I could achieve a similar effect only using the decorators when I do know the type discrepancies between architectures.
I'm not sure I understand the question. I was thinking to use AbiSpecificInteger
for both sem_t
and mode_t
.
/// mode_t
@AbiSpecificIntegerMapping({
Abi.macosArm64: Uint64(),
Abi.macosX64: Uint16(),
})
final class Mode extends AbiSpecificInteger {
const Mode();
}
/// sem_t
@AbiSpecificIntegerMapping({
// ...
})
final class Sem extends AbiSpecificInteger {
const Sem();
}
@Native<Pointer<Sem> Function(Pointer<Char>, Int, VarArgs<(Mode, UnsignedInt)>)>()
external Pointer<Sem> sem_open(Pointer<Char> name, int oflag, int mode, int value);
Is there a reason you cannot use AbiSpecificInteger
for mode_t
?
That said I was just using ffigen in development and ended up removing it from the final dependency once I knew the types thanks to the elegant https://github.com/Native decorators ✨
FFIgen can generate @Native
annotations:
# ffigen.yaml
ffi-native:
assetId: 'myasset' # Optional.
Apologies @dcharkes - I meant specifically without ffigen at all i.e. I'm am hand-writing the @Native decorators similar to native_synchronization package
I was unsure how to specify the Abi specific type alongside the hand written decorators and you gave me exactly what I was missing - I completely overlooked extending AbiSpecificInteger and was trying to use a typedef 🫠
I have updated the code and this all makes a lot more sense now 😅
I have updated the package and we are good to go 🙌 feel free to check it out on pub.dev | runtime_native_semaphores
Cheers!
Hey all!
I have been working on a package (runtime_native_semaphores) and ran into an interesting scenario:
Not sure if this is a bug or not but while I was implementing sem_open bindings on Unix (specifically MacOS arm64 and x86_64 architectures) I discovered that mode_t had variable types.
Notably, on arm64
mode_t
needs to be passed/bound as anUnsignedLong
and on x86_64 architectures it needs to be passed/bound as aUnsignedShort
This was my workaround:
where the above code came from this file
This was the only exception that needed to be made as my other bindings i.e.
sem_unlink
,sem_post
,sem_wait
,sem_trywait
andsem_close
were all successfully implemented leveraging the new @Native decorator like so:Is there a world where one might be able to leverage something like
@Native<T>(abi: Abi.macosArm64)
or@Native<T>(on: Abi.macosArm64)
to help determine which of two architecture specific bindings should be activated at runtime/compilation time?It could perhaps look something like this:
or perhaps (although likely harder to do in dart as one might implement in other languages):
Maybe I'm missing something obvious on my side - any thoughts/help would be greatly appreciated!
Nevertheless, great work! It's lovely to use so far 🙌