github.com/hsfzxjy/dgo/go@v0.2.0/ffi.c (about)

     1  #include "ffi.h"
     2  
     3  #include <stdlib.h>
     4  
     5  extern void dgo__HandleNativeMessage(Dart_Port_DL, Dart_CObject *);
     6  extern void dgo__GoFinalizer(uintptr_t, uintptr_t);
     7  
     8  uintptr_t dgo__pGoFinalizer = (uintptr_t)(&dgo__GoFinalizer);
     9  
    10  void dgo__InitFFI(void *data) { Dart_InitializeApiDL(data); }
    11  
    12  Dart_Port_DL dgo__InitPort(Dart_Port_DL send_port_id) {
    13    Dart_Port_DL receive_port_id =
    14        Dart_NewNativePort_DL("dgo:go:", dgo__HandleNativeMessage, false);
    15    Dart_CObject arg;
    16    arg.type = Dart_CObject_kSendPort;
    17    arg.value.as_send_port.id = receive_port_id;
    18    Dart_PostCObject_DL(send_port_id, &arg);
    19    return receive_port_id;
    20  }
    21  
    22  bool dgo__PostCObject(Dart_Port_DL port_id, dgo__Dart_CObject *cobj) {
    23    return Dart_PostCObject_DL(port_id, (Dart_CObject *)cobj);
    24  }
    25  
    26  bool dgo__PostInt(Dart_Port_DL port_id, int64_t value) {
    27    return Dart_PostInteger_DL(port_id, value);
    28  }
    29  
    30  bool dgo__PostCObjects(
    31      Dart_Port_DL       port_id,
    32      int                cnt,
    33      dgo__Dart_CObject *cobjs) {
    34    return dgo__PostCObjects2(port_id, cnt, cobjs, 0, NULL);
    35  }
    36  
    37  bool dgo__PostCObjects2(
    38      Dart_Port_DL       port_id,
    39      int                cnt1,
    40      dgo__Dart_CObject *cobjs1,
    41      int                cnt2,
    42      dgo__Dart_CObject *cobjs2) {
    43  
    44    const int DEFAULT_ARGS_SIZE = 10;
    45  
    46    dgo__Dart_CObject   arg;
    47    dgo__Dart_CObject * pargs[DEFAULT_ARGS_SIZE];
    48    dgo__Dart_CObject **ppargs, **allocated = NULL;
    49  
    50    int cnt = cnt1 + cnt2;
    51    if (cnt <= DEFAULT_ARGS_SIZE) {
    52      ppargs = &pargs[0];
    53      allocated = false;
    54    } else {
    55      allocated = ppargs = calloc(cnt, sizeof(dgo__Dart_CObject *));
    56    }
    57    arg.Type = Dart_CObject_kArray;
    58    arg.Value.as_array.length = cnt;
    59    arg.Value.as_array.values = ppargs;
    60  
    61    for (int i = 0; i < cnt1; i++) {
    62      *ppargs = &cobjs1[0];
    63      ppargs++;
    64      cobjs1++;
    65    }
    66  
    67    for (int i = cnt1; i < cnt; i++) {
    68      *ppargs = &cobjs2[0];
    69      ppargs++;
    70      cobjs2++;
    71    }
    72  
    73    Dart_PostCObject_DL(port_id, (Dart_CObject *)&arg);
    74  
    75    if (allocated != NULL)
    76      free(allocated);
    77  
    78    return true;
    79  }
    80  
    81  bool dgo__CloseNativePort(Dart_Port_DL port_id) {
    82    return Dart_CloseNativePort_DL(port_id);
    83  }
    84  
    85  #include "dart_api_dl.c"