mirror of
https://github.com/loro-dev/loro.git
synced 2025-02-02 02:59:51 +00:00
feat: add go bindgen
This commit is contained in:
parent
83972746a6
commit
8f739178e7
6 changed files with 58 additions and 35 deletions
5
crates/loro-ffi/.gitignore
vendored
5
crates/loro-ffi/.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
examples/cpp/loro
|
examples/loro
|
||||||
examples/cpp/*.a
|
examples/lib/*
|
||||||
|
examples/loro_java
|
|
@ -1,9 +1,39 @@
|
||||||
# loro-ffi
|
# loro-ffi
|
||||||
|
|
||||||
- `cargo build --release`
|
- `cargo build --release`
|
||||||
- move `libloro.a` to directory `examples/cpp`
|
- move `libloro.a` and `loro_ffi.h` to directory `examples/lib`
|
||||||
- run
|
- run
|
||||||
|
|
||||||
|
## C++
|
||||||
|
|
||||||
|
Read more: [cbindgen](https://github.com/eqrion/cbindgen)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
g++ loro.cpp -Bstatic -framework Security -L. -lloro -o loro
|
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:
|
||||||
|
|
||||||
|
- [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)
|
||||||
|
- [JNR](https://github.com/jnr/jnr-ffi)
|
||||||
|
|
||||||
|
### 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
|
||||||
|
```
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include "../../target/loro_ffi.h"
|
#include "../target/loro_ffi.h"
|
||||||
};
|
};
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
21
crates/loro-ffi/examples/main.go
Normal file
21
crates/loro-ffi/examples/main.go
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
package main;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
#cgo LDFLAGS: -L./lib -framework Security -lloro
|
||||||
|
#include "./lib/loro_ffi.h"
|
||||||
|
*/
|
||||||
|
import "C"
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
loro := C.loro_new();
|
||||||
|
text := C.loro_get_text(loro, C.CString("text"));
|
||||||
|
pos := C.uint(0);
|
||||||
|
C.text_insert(text, loro, &pos, C.CString("abc"));
|
||||||
|
value := C.text_value(text);
|
||||||
|
fmt.Println(C.GoString(value));
|
||||||
|
C.text_free(text);
|
||||||
|
C.loro_free(loro);
|
||||||
|
}
|
|
@ -1,9 +1,6 @@
|
||||||
use std::ffi::{c_char, c_uint, CStr, CString};
|
use std::ffi::{c_char, c_uint, CStr, CString};
|
||||||
|
|
||||||
pub type LoroCore = loro_internal::LoroCore;
|
use loro_internal::{LoroCore, Text};
|
||||||
pub type Text = loro_internal::Text;
|
|
||||||
pub type List = loro_internal::List;
|
|
||||||
pub type Map = loro_internal::Map;
|
|
||||||
|
|
||||||
/// create Loro with a random unique client id
|
/// create Loro with a random unique client id
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
@ -47,7 +44,7 @@ pub unsafe extern "C" fn text_insert(
|
||||||
let text = text.as_mut().unwrap();
|
let text = text.as_mut().unwrap();
|
||||||
let ctx = ctx.as_ref().unwrap();
|
let ctx = ctx.as_ref().unwrap();
|
||||||
let value = CStr::from_ptr(value).to_str().unwrap();
|
let value = CStr::from_ptr(value).to_str().unwrap();
|
||||||
text.insert(ctx, pos as usize, value).unwrap();
|
text.insert(ctx, *pos as usize, value).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
|
|
@ -9,32 +9,6 @@ struct Loro(LoroCore);
|
||||||
#[pyclass]
|
#[pyclass]
|
||||||
struct LoroText(Text);
|
struct LoroText(Text);
|
||||||
|
|
||||||
impl Deref for Loro {
|
|
||||||
type Target = LoroCore;
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DerefMut for Loro {
|
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
||||||
&mut self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Deref for LoroText {
|
|
||||||
type Target = Text;
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DerefMut for LoroText {
|
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
||||||
&mut self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[pymethods]
|
#[pymethods]
|
||||||
impl Loro {
|
impl Loro {
|
||||||
#[new]
|
#[new]
|
||||||
|
|
Loading…
Reference in a new issue