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

Make HAL headers C-compatible for FFIs #1122

Closed
wants to merge 1 commit into from

Conversation

Lytigas
Copy link

@Lytigas Lytigas commented May 30, 2018

Allows for easy FFI's from other languages.
Overrides #535
Solves #476

@frcjenkins
Copy link

Can one of the admins verify this patch?

4 similar comments
@frcjenkins
Copy link

Can one of the admins verify this patch?

@frcjenkins
Copy link

Can one of the admins verify this patch?

@frcjenkins
Copy link

Can one of the admins verify this patch?

@frcjenkins
Copy link

Can one of the admins verify this patch?

* @link HAL_ENUM_END @endlink but uses @c int32_t as the type.
*/
#define HAL_ENUM_I32_START(name) HAL_ENUM_START(name, int32_t)
#define HAL_ENUM_I32_END(name) HAL_ENUM_END(name, int32_t)
Copy link
Member

Choose a reason for hiding this comment

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

EOL at EOF

@ThadHouse
Copy link
Member

I think we're going to just make the enum parameters int32_t rather then the macro. That will probably be cleaner for most languages.

@Lytigas Lytigas force-pushed the c-compatible-hal-headers branch from 112bafa to 43b2228 Compare May 30, 2018 05:24
@Lytigas
Copy link
Author

Lytigas commented May 30, 2018

@ThadHouse I would be happy to implement it, but can you elaborate on exactly what you mean? Are you referring to the macro itself or function signatures?

@ThadHouse
Copy link
Member

The function signatures. Really not a fan of the macro, and its not 100% standard complaint either. Enums are not safe to pass as function parameters, and their size is implementation defined by the standards. That was the main reason for the sized enums in the first place.

@ThadHouse
Copy link
Member

rust-lang/rust#36927

Theres also some suggestions on the Rust side here of how to deal with C enums, and they seem to suggest using fixed sizes as well.

@Lytigas
Copy link
Author

Lytigas commented May 30, 2018

If all HAL function signatures take int32_ts over enums, usability greatly decreases as there will effectively be no type annotations at all. How should we maintain usability? Personally I wouldn't like having to resort to doc comments. Many argument names are already the same as their types, but not all of them.

@ThadHouse
Copy link
Member

The HAL is explicitly not considered a user facing API. We often make breaking changes to it for the sake of either formatting, parameter passing, or changes in functionality.

In addition, both the Java, the Python and the C# bindings all just take a raw 32 bit integer for the enum parameters, and then expect the higher level wrappers to hide this from users. Unsized enums are just not safe across ABI boundaries, and C has no proper way of declaring guaranteed size enums.

Part of the reason for this is those other languages don't have easy FFI generators, so we actually end up manually writing them, and manually checking changes.

Fixed sizes help immensely in FFI scenarios. Its the reason we use explicit types everywhere in the HAL, rather then just raw c types.

@auscompgeek
Copy link
Member

Hm. What if we just typedef <enum-name> int32_t; when the header is being used in C? Then the HAL function prototypes would all still read to humans as taking enums, and we can keep the sized enums in C++. We'd also be able to eliminate the odd-looking end macros too.

@@ -9,7 +9,10 @@

#include <stdint.h>

#include <cstddef>
#include <stddef.h>
Copy link
Member

Choose a reason for hiding this comment

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

This should probably remain as cstddef in C++.

@Lytigas
Copy link
Author

Lytigas commented May 31, 2018

I've decided to pursue another way of getting rust-bindgen to work properly, requiring much fewer changes here.

@Lytigas Lytigas closed this May 31, 2018
PeterJohnson pushed a commit that referenced this pull request Jul 8, 2018
Also fix the return type of HAL_IsNewControlData() and HAL_MatchType's type.

Since UsageReporting is intended to be namespaced, it is hidden when this is being used in C.

Fixes: #476
Closes: #535
Ref: #1122
jwhite66 pushed a commit to jwhite66/allwpilib that referenced this pull request Jul 9, 2018
Also fix the return type of HAL_IsNewControlData() and HAL_MatchType's type.

Since UsageReporting is intended to be namespaced, it is hidden when this is being used in C.

Fixes: wpilibsuite#476
Closes: wpilibsuite#535
Ref: wpilibsuite#1122
@Lytigas Lytigas deleted the c-compatible-hal-headers branch November 16, 2018 04:48
pjbuterbaugh pushed a commit to pjbuterbaugh/allwpilib that referenced this pull request Jun 15, 2023
…ilibsuite#1122)

On XboxControllers, moving the sticks upwards causes a negative
change on the y-axis value, and vice versa. This means that driving
a motor directly off the y-axis will result in inverted controls
and cause difficulty (or damage, especially IRL). This resolves the
issue by negating the y-axis in calls to the Joystick.
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.

5 participants