2024-12-03 21:47:12 +08:00
|
|
|
//go:build cgo
|
|
|
|
|
|
2024-04-30 23:38:49 +08:00
|
|
|
package dart_bridge
|
|
|
|
|
|
|
|
|
|
/*
|
2024-06-28 07:45:50 +08:00
|
|
|
#include <stdlib.h>
|
2024-04-30 23:38:49 +08:00
|
|
|
#include "stdint.h"
|
|
|
|
|
#include "include/dart_api_dl.h"
|
|
|
|
|
#include "include/dart_api_dl.c"
|
|
|
|
|
#include "include/dart_native_api.h"
|
|
|
|
|
|
|
|
|
|
bool GoDart_PostCObject(Dart_Port_DL port, Dart_CObject* obj) {
|
|
|
|
|
return Dart_PostCObject_DL(port, obj);
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
import "C"
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"unsafe"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func InitDartApi(api unsafe.Pointer) {
|
|
|
|
|
if C.Dart_InitializeApiDL(api) != 0 {
|
|
|
|
|
panic("failed to create dart bridge")
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Println("Dart Api DL is initialized")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-13 16:36:08 +08:00
|
|
|
func SendToPort(port int64, msg string) bool {
|
2024-04-30 23:38:49 +08:00
|
|
|
var obj C.Dart_CObject
|
|
|
|
|
obj._type = C.Dart_CObject_kString
|
|
|
|
|
msgString := C.CString(msg)
|
2024-06-28 07:45:50 +08:00
|
|
|
defer C.free(unsafe.Pointer(msgString))
|
2024-04-30 23:38:49 +08:00
|
|
|
ptr := unsafe.Pointer(&obj.value[0])
|
|
|
|
|
*(**C.char)(ptr) = msgString
|
|
|
|
|
isSuccess := C.GoDart_PostCObject(C.Dart_Port_DL(port), &obj)
|
|
|
|
|
if !isSuccess {
|
2024-07-13 16:36:08 +08:00
|
|
|
return false
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
2024-07-13 16:36:08 +08:00
|
|
|
return true
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|