Skip to content

Commit

Permalink
update (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
asukaminato0721 authored Oct 16, 2024
1 parent 8ce7545 commit d9fcb63
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 38 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

[dependencies]
chrono = "0.4.38"
fsrs = { git = "https://github.com/open-spaced-repetition/rs-fsrs", rev = "1bdd482" }
fsrs = { git = "https://github.com/open-spaced-repetition/rs-fsrs", rev = "5e6d336" }
jni = "0.21.1"
[lib]
crate-type = ["cdylib"]
28 changes: 18 additions & 10 deletions com/example/fsrs/FSRS.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,32 @@
import java.time.Instant;

public class FSRS {
private static native long FsrsNew(FSRS callback);
private static native long FsrsDefault(FSRS callback);

private static native long CardNew(FSRS callback);

private static native long schedule(FSRS callback, long fsrs, long card, long second);
private static native long selectCard(FSRS callback, long card, long rating);
private static native long getCardScheduledDays(long card);
private static native long repeat(FSRS callback, long fsrs, long card, long second);

private static native long RecordLogGet(FSRS callback, long card, long rating);

private static native long getCardScheduledDays(FSRS callback, long card);

private static native long SchedulingInfoCard(FSRS callback, long SchedulingInfo);

static {
System.loadLibrary("rs_fsrs_java");
}

public static void main(String[] args) {
FSRS gf = new FSRS();
long f = FsrsNew(gf);
long c = CardNew(gf);
long scheduledCard = schedule(gf, f, c, Instant.now().getEpochSecond());
long updated_card = selectCard(gf, scheduledCard, 4);
System.out.println(getCardScheduledDays(updated_card));
}
long fsrs = FsrsDefault(gf);
long card = CardNew(gf);
long scheduledCard = repeat(gf, fsrs, card, Instant.now().getEpochSecond());
for (long rating : new long[]{1,2,3,4}) {
long scheduling_info = RecordLogGet(gf, scheduledCard, rating);
System.out.println(getCardScheduledDays(gf,
SchedulingInfoCard(gf, scheduling_info)));

}
}
}
79 changes: 54 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,30 @@ use jni::JNIEnv;
// They carry extra lifetime information to prevent them escaping from the
// current local frame (which is the scope within which local (temporary)
// references to Java objects remain valid)
use jni::objects::{GlobalRef, JClass, JObject, JString};
use jni::objects::{GlobalRef, JClass, JObject};

use jni::sys::{jlong, jstring};
use jni::sys::jlong;

fn to_raw<T>(value: T) -> jlong {
Box::into_raw(Box::new(value)) as jlong
}

struct FSRS {
inner: fsrs::FSRS,
callback: GlobalRef,
}

#[no_mangle]
pub unsafe extern "system" fn Java_com_example_fsrs_FSRS_FsrsNew(
pub unsafe extern "system" fn Java_com_example_fsrs_FSRS_FsrsDefault(
env: JNIEnv,
_class: JClass,
callback: JObject,
) -> jlong {
let global_ref = env.new_global_ref(callback).unwrap();
Box::into_raw(Box::new(FSRS {
to_raw(FSRS {
inner: fsrs::FSRS::default(),
callback: global_ref,
})) as jlong
})
}

struct Card {
Expand All @@ -40,19 +44,19 @@ pub unsafe extern "system" fn Java_com_example_fsrs_FSRS_CardNew(
callback: JObject,
) -> jlong {
let global_ref = env.new_global_ref(callback).unwrap();
Box::into_raw(Box::new(Card {
to_raw(Card {
inner: fsrs::Card::new(),
callback: global_ref,
})) as jlong
})
}

struct ScheduledCards {
inner: fsrs::ScheduledCards,
struct RecordLog {
inner: fsrs::RecordLog,
callback: GlobalRef,
}

#[no_mangle]
pub unsafe extern "system" fn Java_com_example_fsrs_FSRS_schedule(
pub unsafe extern "system" fn Java_com_example_fsrs_FSRS_repeat(
env: JNIEnv,
_class: JClass,
callback: JObject,
Expand All @@ -64,42 +68,67 @@ pub unsafe extern "system" fn Java_com_example_fsrs_FSRS_schedule(
let card = &*(card as *const Card);
let global_ref = env.new_global_ref(callback).unwrap();

Box::into_raw(Box::new(ScheduledCards {
inner: f.inner.schedule(
to_raw(RecordLog {
inner: f.inner.repeat(
card.inner.clone(),
chrono::DateTime::from_timestamp(n as i64, 0).expect("time error"),
),
callback: global_ref,
})) as jlong
})
}

struct SchedulingInfo {
inner: fsrs::SchedulingInfo,
callback: GlobalRef,
}

#[no_mangle]
pub unsafe extern "system" fn Java_com_example_fsrs_FSRS_selectCard(
pub unsafe extern "system" fn Java_com_example_fsrs_FSRS_RecordLogGet(
env: JNIEnv,
_class: JClass,
callback: JObject,
scheduled_cards: jlong,
scheduling_info: jlong,
rating: jlong,
) -> jlong {
let f = &*(scheduled_cards as *const ScheduledCards);
let f = &*(scheduling_info as *const RecordLog);
let global_ref = env.new_global_ref(callback).unwrap();

Box::into_raw(Box::new(Card {
inner: f.inner.select_card(match rating {
1 => fsrs::Rating::Again,
2 => fsrs::Rating::Hard,
3 => fsrs::Rating::Good,
4 => fsrs::Rating::Easy,
_ => unreachable!(),
}),
to_raw(SchedulingInfo {
inner: f
.inner
.get(&match rating {
1 => fsrs::Rating::Again,
2 => fsrs::Rating::Hard,
3 => fsrs::Rating::Good,
4 => fsrs::Rating::Easy,
_ => unreachable!(),
})
.unwrap()
.to_owned(),
callback: global_ref,
})) as jlong
})
}

#[no_mangle]
pub unsafe extern "system" fn Java_com_example_fsrs_FSRS_SchedulingInfoCard(
env: JNIEnv,
_class: JClass,
callback: JObject,
scheduling_info: jlong,
) -> jlong {
let f = &*(scheduling_info as *const SchedulingInfo);
let global_ref = env.new_global_ref(callback).unwrap();
to_raw(Card {
inner: f.inner.card.clone(),
callback: global_ref,
})
}

#[no_mangle]
pub unsafe extern "system" fn Java_com_example_fsrs_FSRS_getCardScheduledDays(
env: JNIEnv,
_class: JClass,
callback: JObject,
card: jlong,
) -> jlong {
let c = &*(card as *const Card);
Expand Down

0 comments on commit d9fcb63

Please sign in to comment.