Skip to content

Commit

Permalink
Add library version (webview#862)
Browse files Browse the repository at this point in the history
The version has been set to 0.10.0.

128 characters are available for the full SemVer 2.0.0 version number in
webview_version_info_t:

  * 32 characters for the MAJOR.MINOR.PATCH version number allows each
    element to have the maximum value of a 32-bit unsigned integer.
  * The remaining 96 characters are split evenly between pre-release
    and build metadata giving them 48 characters each.
  • Loading branch information
SteffenL authored Nov 30, 2022
1 parent c6824d4 commit 5872fea
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
75 changes: 75 additions & 0 deletions webview.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 29,66 @@
#define WEBVIEW_API extern
#endif

#ifndef WEBVIEW_VERSION_MAJOR
// The current library major version.
#define WEBVIEW_VERSION_MAJOR 0
#endif

#ifndef WEBVIEW_VERSION_MINOR
// The current library minor version.
#define WEBVIEW_VERSION_MINOR 10
#endif

#ifndef WEBVIEW_VERSION_PATCH
// The current library patch version.
#define WEBVIEW_VERSION_PATCH 0
#endif

#ifndef WEBVIEW_VERSION_PRE_RELEASE
// SemVer 2.0.0 pre-release labels prefixed with "-".
#define WEBVIEW_VERSION_PRE_RELEASE ""
#endif

#ifndef WEBVIEW_VERSION_BUILD_METADATA
// SemVer 2.0.0 build metadata prefixed with " ".
#define WEBVIEW_VERSION_BUILD_METADATA ""
#endif

// Utility macro for stringifying a macro argument.
#define WEBVIEW_STRINGIFY(x) #x

// Utility macro for stringifying the result of a macro argument expansion.
#define WEBVIEW_EXPAND_AND_STRINGIFY(x) WEBVIEW_STRINGIFY(x)

// SemVer 2.0.0 version number in MAJOR.MINOR.PATCH format.
#define WEBVIEW_VERSION_NUMBER \
WEBVIEW_EXPAND_AND_STRINGIFY(WEBVIEW_VERSION_MAJOR) \
"." WEBVIEW_EXPAND_AND_STRINGIFY( \
WEBVIEW_VERSION_MINOR) "." WEBVIEW_EXPAND_AND_STRINGIFY(WEBVIEW_VERSION_PATCH)

// Holds the elements of a MAJOR.MINOR.PATCH version number.
typedef struct {
// Major version.
unsigned int major;
// Minor version.
unsigned int minor;
// Patch version.
unsigned int patch;
} webview_version_t;

// Holds the library's version information.
typedef struct {
// The elements of the version number.
webview_version_t version;
// SemVer 2.0.0 version number in MAJOR.MINOR.PATCH format.
const char version_number[32];
// SemVer 2.0.0 pre-release labels prefixed with "-" if specified, otherwise
// an empty string.
const char pre_release[48];
// SemVer 2.0.0 build metadata prefixed with " ", otherwise an empty string.
const char build_metadata[48];
} webview_version_info_t;

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -119,6 179,10 @@ WEBVIEW_API void webview_unbind(webview_t w, const char *name);
WEBVIEW_API void webview_return(webview_t w, const char *seq, int status,
const char *result);

// Get the library's version information.
// @since 0.10
WEBVIEW_API const webview_version_info_t *webview_version();

#ifdef __cplusplus
}

Expand Down Expand Up @@ -168,6 232,13 @@ using dispatch_fn_t = std::function<void()>;

namespace detail {

// The library's version information.
constexpr const webview_version_info_t library_version_info{
{WEBVIEW_VERSION_MAJOR, WEBVIEW_VERSION_MINOR, WEBVIEW_VERSION_PATCH},
WEBVIEW_VERSION_NUMBER,
WEBVIEW_VERSION_PRE_RELEASE,
WEBVIEW_VERSION_BUILD_METADATA};

inline int json_parse_c(const char *s, size_t sz, const char *key, size_t keysz,
const char **value, size_t *valuesz) {
enum {
Expand Down Expand Up @@ -1690,6 1761,10 @@ WEBVIEW_API void webview_return(webview_t w, const char *seq, int status,
static_cast<webview::webview *>(w)->resolve(seq, status, result);
}

WEBVIEW_API const webview_version_info_t *webview_version() {
return &webview::detail::library_version_info;
}

#endif /* WEBVIEW_HEADER */
#endif /* __cplusplus */
#endif /* WEBVIEW_H */
23 changes: 23 additions & 0 deletions webview_test.cc
Original file line number Diff line number Diff line change
@@ -1,6 1,12 @@
//bin/echo; [ $(uname) = "Darwin" ] && FLAGS="-framework Webkit" || FLAGS="$(pkg-config --cflags --libs gtk -3.0 webkit2gtk-4.0)" ; c "$0" $FLAGS -std=c 11 -Wall -Wextra -pedantic -g -o webview_test && ./webview_test ; exit
// build ignore

#define WEBVIEW_VERSION_MAJOR 1
#define WEBVIEW_VERSION_MINOR 2
#define WEBVIEW_VERSION_PATCH 3
#define WEBVIEW_VERSION_PRE_RELEASE "-test"
#define WEBVIEW_VERSION_BUILD_METADATA " gaabbccd"

#include "webview.h"

#include <cassert>
Expand Down Expand Up @@ -42,6 48,22 @@ static void test_c_api() {
webview_destroy(w);
}

// =================================================================
// TEST: webview_version().
// =================================================================
static void test_c_api_version() {
auto vi = webview_version();
assert(vi);
assert(vi->version.major == 1);
assert(vi->version.minor == 2);
assert(vi->version.patch == 3);
assert(std::string(vi->version_number) == "1.2.3");
assert(std::string(vi->pre_release) == "-test");
assert(std::string(vi->build_metadata) == " gaabbccd");
// The function should return the same pointer when called again.
assert(webview_version() == vi);
}

// =================================================================
// TEST: ensure that JS code can call native code and vice versa.
// =================================================================
Expand Down Expand Up @@ -178,6 200,7 @@ int main(int argc, char *argv[]) {
std::unordered_map<std::string, std::function<void()>> all_tests = {
{"terminate", test_terminate},
{"c_api", test_c_api},
{"c_api_version", test_c_api_version},
{"bidir_comms", test_bidir_comms},
{"json", test_json}};
#if _WIN32
Expand Down

0 comments on commit 5872fea

Please sign in to comment.