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

feat(c-api) Add the WASMER_VERSION* constants, and the wasmer_version* functions #1916

Merged
merged 14 commits into from
Dec 14, 2020

Conversation

Hywan
Copy link
Contributor

@Hywan Hywan commented Dec 10, 2020

Fix #1534.

Description

This PR adds the wasmer_version. It compute a string based on the CARGO_PKG_VERSION environment variable value at compile-time. I know an enum could be better, but it's a start.

This PR adds 4 C constants:

#define WASMER_VERSION "1.0.0-beta1"
#define WASMER_VERSION_MAJOR 1
#define WASMER_VERSION_MINOR 0
#define WASMER_VERSION_PATCH 0
#define WASMER_VERSION_PRE "beta1"

Thay way, the user can query the version of Wasmer more easily.

In addition, it adds 4 functions (when the header files aren't accessible):

const char* wasmer_version(void);
uint8_t wasmer_version_major(void);
uint8_t wasmer_version_minor(void);
uint8_t wasmer_version_patch(void);
const char* wasmer_version_pre(void);

Review

  • Add a short description of the the change to the CHANGELOG.md file

@Hywan Hywan added 🎉 enhancement New feature! 📦 lib-c-api About wasmer-c-api 🧪 tests I love tests 📚 documentation Do you like to read? labels Dec 10, 2020
@Hywan Hywan self-assigned this Dec 10, 2020
@Hywan Hywan added the 1.0 Wasmer at 1.0 label Dec 10, 2020
@Hywan
Copy link
Contributor Author

Hywan commented Dec 10, 2020

bors try

bors bot added a commit that referenced this pull request Dec 10, 2020
@bors
Copy link
Contributor

bors bot commented Dec 10, 2020

/// # }
/// ```
#[no_mangle]
pub extern "C" fn wasmer_version() -> *mut c_char {
Copy link
Member

Choose a reason for hiding this comment

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

Should it be *const c_char ?

Copy link
Contributor

Choose a reason for hiding this comment

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

same as @syrusakbary, why a *mut?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So that the user doesn't need to cast it when freeing it. That's stupid, but I think it's a better ergonomic.

tl;dr: It avoids writing this:

const char* version = wasmer_version();
free((char*) version); // this cast

Copy link
Contributor

@MarkMcCaskey MarkMcCaskey left a comment

Choose a reason for hiding this comment

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

We'll need to fix the free bug before we can ship this but otherwise looks good

/// printf("%s", version);
///
/// // Free it, as we own it.
/// free(version);
Copy link
Contributor

Choose a reason for hiding this comment

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

From the docs:

The pointer which this function returns must be returned to Rust and reconstituted using CString::from_raw to be properly deallocated. Specifically, one should not use the standard C free() function to deallocate this string.

I don't think we can ever mix C and Rust ownership: CString is an owned type on the Rust side, we can't pass it back to C without providing our own destructor. Probably simpler to have the user pass us a buffer or to return a pointer to a static string which does not need to be freed

lib/c-api/wasmer.h Outdated Show resolved Hide resolved
Copy link
Member

@syrusakbary syrusakbary left a comment

Choose a reason for hiding this comment

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

Note: I did the comments as an approval by mistake.

Actually, I think there might be a better solution than a function that returns a string.

Since we are generating the headers at build time, we can check what the version is for the crate, and add that as a constant string variable in the generated header file.

Thoughts @Hywan @MarkMcCaskey @jubianchi ?

@syrusakbary
Copy link
Member

Let's use:

static const char * WASMER_VERSION = "1.0.0";

@MarkMcCaskey
Copy link
Contributor

Note: I did the comments as an approval by mistake.

Actually, I think there might be a better solution than a function that returns a string.

Since we are generating the headers at build time, we can check what the version is for the crate, and add that as a constant string variable in the generated header file.

Thoughts @Hywan @MarkMcCaskey @jubianchi ?

Yeah, that makes sense, though we should probably use a C macro:

#define WASMER_VERSION "version-number"

because static variables take up space and can collide with each other when linking and stuff

@MarkMcCaskey
Copy link
Contributor

Actually, this has one major draw back... perhaps we want to do both, but it's nice to have a standard API for querying which version of Wasmer you're running against. This will be important with 1.0.0 when we have more claims to stability and users may ship code that dynamically links against the Wasmer C API and may want to query what version it's running against

@syrusakbary
Copy link
Member

Actually, this has one major draw back... perhaps we want to do both, but it's nice to have a standard API for querying which version of Wasmer you're running against. This will be important with 1.0.0 when we have more claims to stability and users may ship code that dynamically links against the Wasmer C API and may want to query what version it's running against

Yup, that's the same I was thinking. Let's do it via macro and static string then?

@MarkMcCaskey
Copy link
Contributor

MarkMcCaskey commented Dec 11, 2020

Actually, this has one major draw back... perhaps we want to do both, but it's nice to have a standard API for querying which version of Wasmer you're running against. This will be important with 1.0.0 when we have more claims to stability and users may ship code that dynamically links against the Wasmer C API and may want to query what version it's running against

Yup, that's the same I was thinking. Let's do it via macro and static string then?

Yeah, a static string works but the static has to be defined on the Rust side, not the C side, or it has the same problem as the macro

@Hywan
Copy link
Contributor Author

Hywan commented Dec 11, 2020

Actually, I think there might be a better solution than a function that returns a string.

Of course there is! This PR was a kickstart for the discussion :-).

@Hywan
Copy link
Contributor Author

Hywan commented Dec 11, 2020

I wonder whether it's required to add a C function for that. I've added the following C constants, and I believe the user can query everything that is common with those:

#define WASMER_VERSION "1.0.0-beta1"
#define WASMER_VERSION_MAJOR 1
#define WASMER_VERSION_MINOR 0
#define WASMER_VERSION_PATCH 0
#define WASMER_VERSION_PRE "beta1"

Note that _MAJOR, _MINOR and _PATCH are integers, not strings.

Thoughts?

@Hywan Hywan changed the title feat(c-api) Add the wasmer_version function feat(c-api) Add the WASMER_VERSION* C constants Dec 11, 2020
@Hywan
Copy link
Contributor Author

Hywan commented Dec 11, 2020

bors try

bors bot added a commit that referenced this pull request Dec 11, 2020
@bors
Copy link
Contributor

bors bot commented Dec 11, 2020

@syrusakbary
Copy link
Member

Yeah, a static string works but the static has to be defined on the Rust side, not the C side, or it has the same problem as the macro

This was not addressed @Hywan

Copy link
Contributor

@jubianchi jubianchi left a comment

Choose a reason for hiding this comment

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

Seems good but:

  • What about having wasmer_version_major(), wasmer_version_minor(), wasmer_version_patch()?
  • We also have to expose the pre-release part if we want a "full" API;

@Hywan Hywan changed the title feat(c-api) Add the WASMER_VERSION* C constants feat(c-api) Add the WASMER_VERSION* constants, and the wasmer_version* functions Dec 14, 2020
@Hywan
Copy link
Contributor Author

Hywan commented Dec 14, 2020

bors r+

@bors
Copy link
Contributor

bors bot commented Dec 14, 2020

@bors bors bot merged commit 35e0021 into wasmerio:master Dec 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.0 Wasmer at 1.0 📚 documentation Do you like to read? 🎉 enhancement New feature! 📦 lib-c-api About wasmer-c-api 🧪 tests I love tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add wasmer_version to the C-API
4 participants