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

fix: example change to verify the parameterless function fix #10

Merged
merged 4 commits into from
Oct 8, 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
9 changes: 8 additions & 1 deletion examples/example-monoio/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
use rust2go::RegenArgs;

fn main() {
rust2go::Builder::new()
.with_go_src("./go")
// .with_regen("./src/user.rs", "./go/gen.go")
.with_regen_arg(RegenArgs {
src: "./src/user.rs".into(),
dst: "./go/gen.go".into(),
go118: true,
..Default::default()
})
.build();
}
50 changes: 41 additions & 9 deletions examples/example-monoio/go/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ package main
#include <stdint.h>
#include <stdlib.h>

typedef struct StringRef {
const uint8_t *ptr;
uintptr_t len;
} StringRef;

typedef struct ListRef {
const void *ptr;
uintptr_t len;
Expand All @@ -23,15 +18,20 @@ typedef struct DemoComplicatedRequestRef {
struct ListRef balabala;
} DemoComplicatedRequestRef;

typedef struct DemoResponseRef {
bool pass;
} DemoResponseRef;

typedef struct StringRef {
const uint8_t *ptr;
uintptr_t len;
} StringRef;

typedef struct DemoUserRef {
struct StringRef name;
uint8_t age;
} DemoUserRef;

typedef struct DemoResponseRef {
bool pass;
} DemoResponseRef;

// hack from: https://stackoverflow.com/a/69904977
__attribute__((weak))
inline void DemoCall_demo_check_cb(const void *f_ptr, struct DemoResponseRef resp, const void *slot) {
Expand All @@ -49,6 +49,18 @@ __attribute__((weak))
inline void DemoCall_demo_check_async_safe_cb(const void *f_ptr, struct DemoResponseRef resp, const void *slot) {
((void (*)(struct DemoResponseRef, const void*))f_ptr)(resp, slot);
}

// hack from: https://stackoverflow.com/a/69904977
__attribute__((weak))
inline void DemoCall_demo_get_n_cb(const void *f_ptr, int32_t resp, const void *slot) {
((void (*)(int32_t, const void*))f_ptr)(resp, slot);
}

// hack from: https://stackoverflow.com/a/69904977
__attribute__((weak))
inline void DemoCall_demo_sum_cb(const void *f_ptr, int32_t resp, const void *slot) {
((void (*)(int32_t, const void*))f_ptr)(resp, slot);
}
*/
import "C"
import (
Expand All @@ -64,6 +76,8 @@ type DemoCall interface {
demo_check(req DemoComplicatedRequest) DemoResponse
demo_check_async(req DemoComplicatedRequest) DemoResponse
demo_check_async_safe(req DemoComplicatedRequest) DemoResponse
demo_get_n() int32
demo_sum(a int32, b int32) int32
}

//export CDemoCall_demo_oneway
Expand Down Expand Up @@ -104,6 +118,24 @@ func CDemoCall_demo_check_async_safe(req C.DemoComplicatedRequestRef, slot *C.vo
}()
}

//export CDemoCall_demo_get_n
func CDemoCall_demo_get_n(slot *C.void, cb *C.void) {
resp := DemoCallImpl.demo_get_n()
resp_ref, buffer := cvt_ref(cntC_int32_t, refC_int32_t)(&resp)
C.DemoCall_demo_get_n_cb(unsafe.Pointer(cb), resp_ref, unsafe.Pointer(slot))
runtime.KeepAlive(resp)
runtime.KeepAlive(buffer)
}

//export CDemoCall_demo_sum
func CDemoCall_demo_sum(a C.int32_t, b C.int32_t, slot *C.void, cb *C.void) {
resp := DemoCallImpl.demo_sum(newC_int32_t(a), newC_int32_t(b))
resp_ref, buffer := cvt_ref(cntC_int32_t, refC_int32_t)(&resp)
C.DemoCall_demo_sum_cb(unsafe.Pointer(cb), resp_ref, unsafe.Pointer(slot))
runtime.KeepAlive(resp)
runtime.KeepAlive(buffer)
}

// An alternative impl of unsafe.String for go1.18
func unsafeString(ptr *byte, length int) string {
sliceHeader := &reflect.SliceHeader{
Expand Down
8 changes: 8 additions & 0 deletions examples/example-monoio/go/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@ func (Demo) demo_check_async_safe(req DemoComplicatedRequest) DemoResponse {
fmt.Printf("[Go-call async drop_safe] Golang returned result, pass: %v\n", req.balabala[0] == 1)
return resp
}

func (Demo) demo_get_n() int32 {
return 8
}

func (Demo) demo_sum(a int32, b int32) int32 {
return a + b
}
6 changes: 6 additions & 0 deletions examples/example-monoio/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ async fn main() {
name: "chihai".to_string(),
age: 28,
};
println!("========== Start get_n demo ==========");
let n = DemoCallImpl::demo_get_n();
println!("[Rust-get_n] n={n}");
println!("========== Start sum demo ==========");
let sum = DemoCallImpl::demo_sum(1, 2);
println!("[Rust-get_n] 1+2={sum}");
println!("========== Start oneway demo ==========");
DemoCallImpl::demo_oneway(&user);
println!("[Rust-oneway] done");
Expand Down
2 changes: 2 additions & 0 deletions examples/example-monoio/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@ pub trait DemoCall {
fn demo_check_async_safe(
req: DemoComplicatedRequest,
) -> impl std::future::Future<Output = DemoResponse>;
fn demo_get_n() -> i32;
fn demo_sum(a: i32, b: i32) -> i32;
}
Loading