mirror of
https://github.com/loro-dev/loro.git
synced 2025-01-22 12:57:20 +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/cpp/*.a
|
||||
examples/loro
|
||||
examples/lib/*
|
||||
examples/loro_java
|
|
@ -1,9 +1,39 @@
|
|||
# loro-ffi
|
||||
|
||||
- `cargo build --release`
|
||||
- move `libloro.a` to directory `examples/cpp`
|
||||
- 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:
|
||||
|
||||
- [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>
|
||||
extern "C" {
|
||||
#include "../../target/loro_ffi.h"
|
||||
#include "../target/loro_ffi.h"
|
||||
};
|
||||
|
||||
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};
|
||||
|
||||
pub type LoroCore = loro_internal::LoroCore;
|
||||
pub type Text = loro_internal::Text;
|
||||
pub type List = loro_internal::List;
|
||||
pub type Map = loro_internal::Map;
|
||||
use loro_internal::{LoroCore, Text};
|
||||
|
||||
/// create Loro with a random unique client id
|
||||
#[no_mangle]
|
||||
|
@ -47,7 +44,7 @@ pub unsafe extern "C" fn text_insert(
|
|||
let text = text.as_mut().unwrap();
|
||||
let ctx = ctx.as_ref().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]
|
||||
|
|
|
@ -9,32 +9,6 @@ struct Loro(LoroCore);
|
|||
#[pyclass]
|
||||
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]
|
||||
impl Loro {
|
||||
#[new]
|
||||
|
|
Loading…
Reference in a new issue