Skip to content

Commit 4a59767

Browse files
committed
Add GdextBuild::{before_api, since_api}; fix itest depending on compiled rather than runtime version
1 parent 8402bd4 commit 4a59767

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

godot-codegen/src/central_generator.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,32 @@ fn make_build_config(header: &Header) -> TokenStream {
516516
};
517517
(version.major as u8, version.minor as u8, version.patch as u8)
518518
}
519+
520+
/// For a string "4.x", returns `true` if the current Godot version is strictly less than 4.x.
521+
///
522+
/// Runtime equivalent of `#[cfg(before_api = "4.x")]`.
523+
///
524+
/// # Panics
525+
/// On bad input.
526+
pub fn before_api(major_minor: &str) -> bool {
527+
let mut parts = major_minor.split('.');
528+
let queried_major = parts.next().unwrap().parse::<u8>().expect("invalid major version");
529+
let queried_minor = parts.next().unwrap().parse::<u8>().expect("invalid minor version");
530+
assert_eq!(queried_major, 4, "major version must be 4");
531+
532+
let (_, minor, _) = Self::godot_runtime_version_triple();
533+
minor < queried_minor
534+
}
535+
536+
/// For a string "4.x", returns `true` if the current Godot version is equal or greater to 4.x.
537+
///
538+
/// Runtime equivalent of `#[cfg(since_api = "4.x")]`.
539+
///
540+
/// # Panics
541+
/// On bad input.
542+
pub fn since_api(major_minor: &str) -> bool {
543+
!Self::before_api(major_minor)
544+
}
519545
}
520546
}
521547
}

godot-core/src/init/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use godot_ffi as sys;
88

99
use std::cell;
1010

11+
pub use sys::GdextBuild;
12+
1113
#[doc(hidden)]
1214
// TODO consider body safe despite unsafe function, and explicitly mark unsafe {} locations
1315
pub unsafe fn __gdext_load_library<E: ExtensionLibrary>(

itest/rust/src/builtin_tests/containers/dictionary_test.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::collections::{HashMap, HashSet};
88

99
use godot::builtin::meta::{FromGodot, ToGodot};
1010
use godot::builtin::{dict, varray, Dictionary, Variant};
11+
use godot::sys::GdextBuild;
1112

1213
use crate::framework::{expect_panic, itest};
1314

@@ -354,10 +355,11 @@ fn dictionary_equal() {
354355
assert_ne!(dict! {"foo": "bar"}, dict! {"bar": "foo"});
355356

356357
// Changed in https://github.com/godotengine/godot/pull/74588.
357-
#[cfg(before_api = "4.2")]
358-
assert_eq!(dict! {1: f32::NAN}, dict! {1: f32::NAN});
359-
#[cfg(since_api = "4.2")]
360-
assert_ne!(dict! {1: f32::NAN}, dict! {1: f32::NAN});
358+
if GdextBuild::before_api("4.2") {
359+
assert_eq!(dict! {1: f32::NAN}, dict! {1: f32::NAN});
360+
} else {
361+
assert_ne!(dict! {1: f32::NAN}, dict! {1: f32::NAN});
362+
}
361363
}
362364

363365
#[itest]

0 commit comments

Comments
 (0)