Skip to content

Expose equals and compare over FFI#269

Merged
nekevss merged 11 commits intoboa-dev:mainfrom
Magnus-Fjeldstad:equal-and-compare
May 7, 2025
Merged

Expose equals and compare over FFI#269
nekevss merged 11 commits intoboa-dev:mainfrom
Magnus-Fjeldstad:equal-and-compare

Conversation

@Magnus-Fjeldstad
Copy link
Copy Markdown
Contributor

Solves #266

Is there a way to add tests for these new method? We belive we found a solution for the issue, but we havent been able to test it. We would like some feedback.

Worked with @HenrikTennebekk

@sffc
Copy link
Copy Markdown
Contributor

sffc commented Apr 30, 2025

Thanks for regenerating the FFI bindings

Probably good to add C++ tests in https://github.com/boa-dev/temporal/blob/main/temporal_capi/cpp_tests/simple.cpp

CC @Manishearth

Comment thread temporal_capi/src/plain_year_month.rs Outdated
)
}

pub fn compare_iso_year_month(year1: i32, month1: u8, year2: i32, month2: u8) -> i32 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion: write this as a match on (year1, month1).cmp((year2, month2)) instead

And link to the spec entry

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

also, thought: I don't think it would be a bad idea to just have a method return std::cmp::Ordering here, that converts to the integer anyway, and makes it easier to add comparator support later

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

should i do this for all the new compare funcitons?

Comment thread temporal_capi/src/plain_month_day.rs Outdated
Comment on lines +61 to +72
pub fn compare_iso_month_day(month1: u8, day1: u8, month2: u8, day2: u8) -> i32 {
if month1 > month2 {
1
} else if month1 < month2 {
-1
} else if day1 > day2 {
1
} else if day1 < day2 {
-1
} else {
0
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Issue: PlainMonthDay does not have a compare function

Comment thread temporal_capi/src/plain_date.rs Outdated
)
}

pub fn compare_iso_date(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please use the library's compare function. Don't write your own. only write a thing wrapper.

Copy link
Copy Markdown
Member

@nekevss nekevss left a comment

Choose a reason for hiding this comment

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

Thanks for working on this! I'm adding a couple early review comments as I believe you're still looking at adding some tests.

I think using the native rust methods wherever possible is the best approach as it assures that the compare methods work the same between the Rust implementation and the FFI.

EDIT: Just noticed, Manish and Shane's comments. Feel free to disregard my review coments as I believe we have the same comments.

Comment thread temporal_capi/src/plain_date_time.rs Outdated
self.0 == other.0
}

pub fn compare(one: &Self, two: &Self) -> i32 {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

suggestion: I believe there should be a simpler way to implement these.

The built-ins that have an IsoDate internally should have a method called compare_iso that does this work for you.

The exception to this I believe is the time oriented types, which should implement PartialOrd and Ord. In these cases, you can use Ord's method cmp and cast Ordering into an i8. Although, ZonedDateTime implements a compare_instant method instead of an Ord implementation.

Comment thread temporal_capi/src/plain_date_time.rs Outdated
.iter()
.find(|&&ord| ord != std::cmp::Ordering::Equal)
.map_or(0, |ord| match ord {
std::cmp::Ordering::Greater => 1,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thought: Ordering is repr(i8), so you should be able to cast it. Somthing like: i32::from(ord as i8)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also diplomat supports returning Ordering over FFI, since it supports custom comparators.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ooooooooh, good to know!

Comment thread temporal_capi/src/plain_date_time.rs Outdated
microsecond2: u16,
nanosecond2: u16,
) -> i32 {
let comparisons = [
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

issue: no, this should just use (year1, month1, ...).cmp((year2, month2, ...))

@Magnus-Fjeldstad
Copy link
Copy Markdown
Contributor Author

Tried to follow your feedback, please give me more feedback if i havent implemented it correctly.

@nekevss @Manishearth

Comment thread temporal_capi/src/plain_month_day.rs Outdated

pub fn compare_iso_year_month(year1: i32, month1: u8, year2: i32, month2: u8) -> i32 {
match (year1, month1).cmp(&(year2, month2)) {
std::cmp::Ordering::Less => -1,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: core::cmp::Ordering

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

but also, you can just as i32 here

but also, I think all of these APIs should just return core::cmp::Ordering directly.

Copy link
Copy Markdown
Contributor Author

@Magnus-Fjeldstad Magnus-Fjeldstad May 5, 2025

Choose a reason for hiding this comment

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

Hey, thanks for the respone, should i do this for all the files.
I.e:
plain_date_time.rs
plain_date.rs
plain_month_day.rs
plain_time.rs
plain_year_month.rs

im sorry if this might be a silly question, but im not so used to the github lingo just yet

Here is my proposed new code for compare in plain_month_day.rs

pub fn compare_iso_year_month(
            year1: i32,
            month1: u8,
            year2: i32,
            month2: u8,
        ) -> core::cmp::Ordering {
            (year1, month1).cmp(&(year2, month2))
        }

        pub fn compare(one: &Self, two: &Self) -> core::cmp::Ordering {
            Self::compare_iso_year_month(
                one.iso_year(),
                one.iso_month(),
                two.iso_year(),
                two.iso_month(),
            )
        }
        ```

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes. Is there a reason you want separate compare vs compare_iso_year_month methods? It doesn't seem like the spec needs them, just compare() is fine

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, not really i just initally had issues using just compare(), but i think i can make it work with just one.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For reference why compare_iso vs. compare.

#175

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't mind the name, it's specifically that we have two functions. We should just have one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Made them into one function now, also used core insted of std

@nekevss
Copy link
Copy Markdown
Member

nekevss commented May 5, 2025

This just needs a rebase / merge to resolve the conflicts, and then it should be good to go.

@nekevss nekevss added C-enhancement New feature or request C-FFI Changes related to FFI labels May 6, 2025
@nekevss nekevss merged commit 01cb167 into boa-dev:main May 7, 2025
8 checks passed
nekevss pushed a commit that referenced this pull request May 10, 2025
Solves #266 

Is there a way to add tests for these new method? We belive we found a
solution for the issue, but we havent been able to test it. We would
like some feedback.

Worked with @HenrikTennebekk

---------

Co-authored-by: Henrik Tennebekk <henrik.ten@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-enhancement New feature or request C-FFI Changes related to FFI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants