Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
csnewman committed Jan 28, 2024
1 parent 8110eb2 commit de549f0
Show file tree
Hide file tree
Showing 13 changed files with 242 additions and 211 deletions.
3 changes: 2 additions & 1 deletion exampleapp/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ void main(List<String> args) async {
final gobuulder = GoBuilder(
name: packageName,
assetId: 'package:$packageName/bridge.gen.dart',
bridgePath: 'go/bridge'
bridgePath: 'go/bridge',
sources: ['go/']
);

await gobuulder.run(
Expand Down
207 changes: 52 additions & 155 deletions exampleapp/go/bridge/bridge.gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ package main
import (
"errors"
"fmt"
"sync"
"sync/atomic"
"unsafe"

orig "github.com/csnewman/flutter-go-bridge/exampleapp/go"
Expand Down Expand Up @@ -38,7 +36,7 @@ typedef struct {
} fgb_ret_add_error;
typedef struct {
void* res;
uintptr_t res;
void* err;
} fgb_ret_new_obj;
Expand All @@ -53,37 +51,11 @@ typedef struct {
*/
import "C"

var (
handles = sync.Map{}
handleIdx uint64
ErrDart = errors.New("dart")
)
var ErrDart = errors.New("dart")

// Required by cgo
func main() {}

//export fgbinternal_init
func fgbinternal_init(p unsafe.Pointer) unsafe.Pointer {
err := runtime.InitializeApi(p)

var cerr unsafe.Pointer
if err != nil {
cerr = unsafe.Pointer(C.CString(err.Error()))
}

return cerr
}

//export fgbinternal_alloc
func fgbinternal_alloc(size C.intptr_t) unsafe.Pointer {
return C.malloc((C.uintptr_t)(size))
}

//export fgbinternal_free
func fgbinternal_free(ptr unsafe.Pointer) {
C.free(ptr)
}

func mapToString(from unsafe.Pointer) string {
res := C.GoString((*C.char)(from))
C.free(from)
Expand Down Expand Up @@ -123,33 +95,12 @@ func mapFromPoint(from orig.Point) (res C.fgb_vt_point) {
return
}

func mapToObj(from unsafe.Pointer) *orig.Obj {
h := uint64(uintptr(from))

v, ok := handles.Load(h)
if !ok {
panic(fmt.Sprintf("invalid handle: %v", h))
}

return v.(*orig.Obj)
}

func mapFromObj(from *orig.Obj) unsafe.Pointer {
h := atomic.AddUint64(&handleIdx, 1)
if h == 0 {
panic("ran out of handle space")
}

handles.Store(h, from)

return unsafe.Pointer(uintptr(h))
func mapToObj(from C.uintptr_t) *orig.Obj {
return runtime.GetPin[*orig.Obj](uintptr(from))
}

//export fgbfree_obj
func fgbfree_obj(from unsafe.Pointer) {
h := uint64(uintptr(from))

handles.Delete(h)
func mapFromObj(from *orig.Obj) C.uintptr_t {
return (C.uintptr_t)(runtime.Pin(from))
}

//export fgb_add
Expand Down Expand Up @@ -179,32 +130,23 @@ func fgb_add(arg_a C.int, arg_b C.int) (resw C.fgb_ret_add) {
//export fgbasync_add
func fgbasync_add(arg_a C.int, arg_b C.int, fgbPort int64) {
go func() {
h := atomic.AddUint64(&handleIdx, 1)
if h == 0 {
panic("ran out of handle space")
}

handles.Store(h, fgb_add(arg_a, arg_b))
value := fgb_add(arg_a, arg_b)
ptr := runtime.Pin(value)
h := uint64(ptr)

sent := runtime.Send(fgbPort, []uint64{h}, func() {
handles.LoadAndDelete(h)
runtime.FreePin(ptr)
})
if !sent {
handles.LoadAndDelete(h)
runtime.FreePin(ptr)
}
}()
}

//export fgbasyncres_add
func fgbasyncres_add(h uint64) C.fgb_ret_add {
v, ok := handles.LoadAndDelete(h)
if !ok {
return C.fgb_ret_add{
err: unsafe.Pointer(C.CString("result handle is not valid")),
}
}

return (v).(C.fgb_ret_add)
ptr := uintptr(h)
return runtime.GetPin[C.fgb_ret_add](ptr)
}

//export fgb_add_points
Expand Down Expand Up @@ -234,32 +176,23 @@ func fgb_add_points(arg_a C.fgb_vt_point, arg_b C.fgb_vt_point) (resw C.fgb_ret_
//export fgbasync_add_points
func fgbasync_add_points(arg_a C.fgb_vt_point, arg_b C.fgb_vt_point, fgbPort int64) {
go func() {
h := atomic.AddUint64(&handleIdx, 1)
if h == 0 {
panic("ran out of handle space")
}

handles.Store(h, fgb_add_points(arg_a, arg_b))
value := fgb_add_points(arg_a, arg_b)
ptr := runtime.Pin(value)
h := uint64(ptr)

sent := runtime.Send(fgbPort, []uint64{h}, func() {
handles.LoadAndDelete(h)
runtime.FreePin(ptr)
})
if !sent {
handles.LoadAndDelete(h)
runtime.FreePin(ptr)
}
}()
}

//export fgbasyncres_add_points
func fgbasyncres_add_points(h uint64) C.fgb_ret_add_points {
v, ok := handles.LoadAndDelete(h)
if !ok {
return C.fgb_ret_add_points{
err: unsafe.Pointer(C.CString("result handle is not valid")),
}
}

return (v).(C.fgb_ret_add_points)
ptr := uintptr(h)
return runtime.GetPin[C.fgb_ret_add_points](ptr)
}

//export fgb_add_error
Expand Down Expand Up @@ -294,32 +227,23 @@ func fgb_add_error(arg_a C.int, arg_b C.int) (resw C.fgb_ret_add_error) {
//export fgbasync_add_error
func fgbasync_add_error(arg_a C.int, arg_b C.int, fgbPort int64) {
go func() {
h := atomic.AddUint64(&handleIdx, 1)
if h == 0 {
panic("ran out of handle space")
}

handles.Store(h, fgb_add_error(arg_a, arg_b))
value := fgb_add_error(arg_a, arg_b)
ptr := runtime.Pin(value)
h := uint64(ptr)

sent := runtime.Send(fgbPort, []uint64{h}, func() {
handles.LoadAndDelete(h)
runtime.FreePin(ptr)
})
if !sent {
handles.LoadAndDelete(h)
runtime.FreePin(ptr)
}
}()
}

//export fgbasyncres_add_error
func fgbasyncres_add_error(h uint64) C.fgb_ret_add_error {
v, ok := handles.LoadAndDelete(h)
if !ok {
return C.fgb_ret_add_error{
err: unsafe.Pointer(C.CString("result handle is not valid")),
}
}

return (v).(C.fgb_ret_add_error)
ptr := uintptr(h)
return runtime.GetPin[C.fgb_ret_add_error](ptr)
}

//export fgb_new_obj
Expand Down Expand Up @@ -349,36 +273,27 @@ func fgb_new_obj(arg_name unsafe.Pointer, arg_other C.int) (resw C.fgb_ret_new_o
//export fgbasync_new_obj
func fgbasync_new_obj(arg_name unsafe.Pointer, arg_other C.int, fgbPort int64) {
go func() {
h := atomic.AddUint64(&handleIdx, 1)
if h == 0 {
panic("ran out of handle space")
}

handles.Store(h, fgb_new_obj(arg_name, arg_other))
value := fgb_new_obj(arg_name, arg_other)
ptr := runtime.Pin(value)
h := uint64(ptr)

sent := runtime.Send(fgbPort, []uint64{h}, func() {
handles.LoadAndDelete(h)
runtime.FreePin(ptr)
})
if !sent {
handles.LoadAndDelete(h)
runtime.FreePin(ptr)
}
}()
}

//export fgbasyncres_new_obj
func fgbasyncres_new_obj(h uint64) C.fgb_ret_new_obj {
v, ok := handles.LoadAndDelete(h)
if !ok {
return C.fgb_ret_new_obj{
err: unsafe.Pointer(C.CString("result handle is not valid")),
}
}

return (v).(C.fgb_ret_new_obj)
ptr := uintptr(h)
return runtime.GetPin[C.fgb_ret_new_obj](ptr)
}

//export fgb_modify_obj
func fgb_modify_obj(arg_o unsafe.Pointer) (resw C.fgb_ret_modify_obj) {
func fgb_modify_obj(arg_o C.uintptr_t) (resw C.fgb_ret_modify_obj) {
defer func() {
r := recover()
if r == nil {
Expand All @@ -399,38 +314,29 @@ func fgb_modify_obj(arg_o unsafe.Pointer) (resw C.fgb_ret_modify_obj) {
}

//export fgbasync_modify_obj
func fgbasync_modify_obj(arg_o unsafe.Pointer, fgbPort int64) {
func fgbasync_modify_obj(arg_o C.uintptr_t, fgbPort int64) {
go func() {
h := atomic.AddUint64(&handleIdx, 1)
if h == 0 {
panic("ran out of handle space")
}

handles.Store(h, fgb_modify_obj(arg_o))
value := fgb_modify_obj(arg_o)
ptr := runtime.Pin(value)
h := uint64(ptr)

sent := runtime.Send(fgbPort, []uint64{h}, func() {
handles.LoadAndDelete(h)
runtime.FreePin(ptr)
})
if !sent {
handles.LoadAndDelete(h)
runtime.FreePin(ptr)
}
}()
}

//export fgbasyncres_modify_obj
func fgbasyncres_modify_obj(h uint64) C.fgb_ret_modify_obj {
v, ok := handles.LoadAndDelete(h)
if !ok {
return C.fgb_ret_modify_obj{
err: unsafe.Pointer(C.CString("result handle is not valid")),
}
}

return (v).(C.fgb_ret_modify_obj)
ptr := uintptr(h)
return runtime.GetPin[C.fgb_ret_modify_obj](ptr)
}

//export fgb_format_obj
func fgb_format_obj(arg_o unsafe.Pointer) (resw C.fgb_ret_format_obj) {
func fgb_format_obj(arg_o C.uintptr_t) (resw C.fgb_ret_format_obj) {
defer func() {
r := recover()
if r == nil {
Expand All @@ -453,32 +359,23 @@ func fgb_format_obj(arg_o unsafe.Pointer) (resw C.fgb_ret_format_obj) {
}

//export fgbasync_format_obj
func fgbasync_format_obj(arg_o unsafe.Pointer, fgbPort int64) {
func fgbasync_format_obj(arg_o C.uintptr_t, fgbPort int64) {
go func() {
h := atomic.AddUint64(&handleIdx, 1)
if h == 0 {
panic("ran out of handle space")
}

handles.Store(h, fgb_format_obj(arg_o))
value := fgb_format_obj(arg_o)
ptr := runtime.Pin(value)
h := uint64(ptr)

sent := runtime.Send(fgbPort, []uint64{h}, func() {
handles.LoadAndDelete(h)
runtime.FreePin(ptr)
})
if !sent {
handles.LoadAndDelete(h)
runtime.FreePin(ptr)
}
}()
}

//export fgbasyncres_format_obj
func fgbasyncres_format_obj(h uint64) C.fgb_ret_format_obj {
v, ok := handles.LoadAndDelete(h)
if !ok {
return C.fgb_ret_format_obj{
err: unsafe.Pointer(C.CString("result handle is not valid")),
}
}

return (v).(C.fgb_ret_format_obj)
ptr := uintptr(h)
return runtime.GetPin[C.fgb_ret_format_obj](ptr)
}
2 changes: 1 addition & 1 deletion exampleapp/ios/Flutter/AppFrameworkInfo.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>11.0</string>
<string>12.0</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion exampleapp/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Uncomment this line to define a global platform for your project
# platform :ios, '11.0'
# platform :ios, '12.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
Expand Down
22 changes: 22 additions & 0 deletions exampleapp/ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
PODS:
- Flutter (1.0.0)
- integration_test (0.0.1):
- Flutter

DEPENDENCIES:
- Flutter (from `Flutter`)
- integration_test (from `.symlinks/plugins/integration_test/ios`)

EXTERNAL SOURCES:
Flutter:
:path: Flutter
integration_test:
:path: ".symlinks/plugins/integration_test/ios"

SPEC CHECKSUMS:
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
integration_test: 13825b8a9334a850581300559b8839134b124670

PODFILE CHECKSUM: 819463e6a0290f5a72f145ba7cde16e8b6ef0796

COCOAPODS: 1.14.3
Loading

0 comments on commit de549f0

Please sign in to comment.