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: add FFI for Loro #420

Merged
merged 20 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ members = [
"crates/dev-utils",
"crates/delta",
"crates/kv-store",
"crates/loro-ffi",
]
resolver = "2"

[workspace.dependencies]
enum_dispatch = "0.3.11"
enum-as-inner = "0.5.1"
enum-as-inner = "0.6.0"
fxhash = "0.2.1"
tracing = { version = "0.1" }
serde_columnar = { version = "0.3.10" }
Expand All @@ -30,3 +31,4 @@ bytes = "1"
once_cell = "1.18.0"
xxhash-rust = { version = "0.8.12", features = ["xxh32"] }
ensure-cov = "0.1.0"
either = "1.13.0"
24 changes: 21 additions & 3 deletions crates/examples/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,27 @@ impl ActorTrait for DrawActor {
};

let map = self.doc.get_map(id);
let pos_map = map.get("pos").unwrap().unwrap_right().into_map().unwrap();
let x = pos_map.get("x").unwrap().unwrap_left().into_i64().unwrap();
let y = pos_map.get("y").unwrap().unwrap_left().into_i64().unwrap();
let pos_map = map
.get("pos")
.unwrap()
.into_container()
.unwrap()
.into_map()
.unwrap();
let x = pos_map
.get("x")
.unwrap()
.into_value()
.unwrap()
.into_i64()
.unwrap();
let y = pos_map
.get("y")
.unwrap()
.into_value()
.unwrap()
.into_i64()
.unwrap();
pos_map
.insert("x", x.overflowing_add(relative_to.x as i64).0)
.unwrap();
Expand Down
12 changes: 11 additions & 1 deletion crates/fuzz/src/container/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,17 @@ impl ActorTrait for CounterActor {
let counter = loro.get_counter("counter");
let result = counter.get_value();
let tracker = self.tracker.try_lock().unwrap().to_value();
assert_eq!(&result, tracker.into_map().unwrap().get("counter").unwrap());
assert_eq!(
result,
tracker
.into_map()
.unwrap()
.get("counter")
.unwrap()
.clone()
.into_double()
.unwrap()
);

use loro_without_counter::LoroDoc as LoroDocWithoutCounter;
// snapshot to snapshot
Expand Down
2 changes: 1 addition & 1 deletion crates/fuzz/src/container/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl Actionable for TreeAction {
let nodes = tree
.nodes()
.into_iter()
.filter(|x| !tree.is_node_deleted(*x).unwrap())
.filter(|x| !tree.is_node_deleted(x).unwrap())
.collect::<Vec<_>>();
let node_num = nodes.len();
let TreeAction { target, action } = self;
Expand Down
2 changes: 1 addition & 1 deletion crates/fuzz/src/one_doc_fuzzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl OneDocFuzzer {
let nodes = tree
.nodes()
.into_iter()
.filter(|x| !tree.is_node_deleted(*x).unwrap())
.filter(|x| !tree.is_node_deleted(x).unwrap())
.collect::<Vec<_>>();
let node_num = nodes.len();
let crate::container::TreeAction { target, action } = tree_action;
Expand Down
2 changes: 1 addition & 1 deletion crates/fuzz/tests/compatibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ fn snapshot_from_016_can_be_imported_in_cur_version() {
.get_map("map")
.get("new_key")
.unwrap()
.left()
.into_value()
.unwrap(),
loro::LoroValue::String(Arc::new("new_value".into()))
);
Expand Down
14 changes: 3 additions & 11 deletions crates/loro-ffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
[package]
name = "loro-ffi"
version = "0.1.0"
version = "0.16.2"
edition = "2021"
build = "build.rs"
license = "MIT"
publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
loro-internal = { path = "../loro-internal" }

[lib]
crate-type = ["staticlib", "cdylib"]
name = "loro"


[build-dependencies]
cbindgen = "0.24.3"
loro = { path = "../loro", features = ["counter","jsonpath"] }
serde_json = {workspace = true}
47 changes: 0 additions & 47 deletions crates/loro-ffi/README.md
Original file line number Diff line number Diff line change
@@ -1,47 +0,0 @@
# loro-ffi

- `cargo build --release`
- move `libloro.a` and `loro_ffi.h` to directory `examples/lib`
- run

## C++

Read more: [cbindgen](https://github.com/eqrion/cbindgen)

```bash
g++ loro.cpp -Bstatic -framework Security -L. -lloro -o loro
```

## Go

Read more: [cgo](https://pkg.go.dev/cmd/cgo)

```bash
go run main.go
```

## [Python](../loro-python/)

## Java

Candidates:

- [JNR](https://github.com/jnr/jnr-ffi)
- [Panama](https://jdk.java.net/panama/) [blog](https://jornvernee.github.io/java/panama/rust/panama-ffi/2021/09/03/rust-panama-helloworld.html)
- [JNI](https://github.com/jni-rs/jni-rs)

### Panama

install panama-jdk and jextract

```bash
jextract -I /Library/Developer/CommandLineTools/usr/include/c++/v1 -I /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -d loro_java -t org.loro -l loro -- lib/loro_ffi.h
```

### JNR

move `libloro.dylib` into `jnr/app`

```bash
gradle run
```
8 changes: 0 additions & 8 deletions crates/loro-ffi/build.rs

This file was deleted.

7 changes: 0 additions & 7 deletions crates/loro-ffi/cbindgen.toml

This file was deleted.

9 changes: 0 additions & 9 deletions crates/loro-ffi/examples/jnr/.gitattributes

This file was deleted.

5 changes: 0 additions & 5 deletions crates/loro-ffi/examples/jnr/.gitignore

This file was deleted.

32 changes: 0 additions & 32 deletions crates/loro-ffi/examples/jnr/app/build.gradle.kts

This file was deleted.

Binary file removed crates/loro-ffi/examples/jnr/app/libloro.a
Binary file not shown.
Binary file removed crates/loro-ffi/examples/jnr/app/libloro.dylib
Binary file not shown.
32 changes: 0 additions & 32 deletions crates/loro-ffi/examples/jnr/app/src/main/java/jnr/App.java

This file was deleted.

Binary file not shown.

This file was deleted.

Loading
Loading