github.com/primecitizens/pcz/std@v0.2.1/plat/js/webext/diagnostics/bindings/ffi_bindings.ts (about)

     1  import { importModule, Application, heap, Pointer } from "@ffi";
     2  
     3  importModule("plat/js/webext/diagnostics", (A: Application) => {
     4    const WEBEXT = typeof globalThis.browser === "undefined" ? globalThis.chrome : globalThis.browser;
     5  
     6    return {
     7      "store_SendPacketResult": (ptr: Pointer, ref: heap.Ref<any>) => {
     8        const x = A.H.get<any>(ref);
     9  
    10        if (typeof x === "undefined") {
    11          A.store.Bool(ptr + 17, false);
    12          A.store.Ref(ptr + 0, undefined);
    13          A.store.Bool(ptr + 16, false);
    14          A.store.Float64(ptr + 8, 0);
    15        } else {
    16          A.store.Bool(ptr + 17, true);
    17          A.store.Ref(ptr + 0, x["ip"]);
    18          A.store.Bool(ptr + 16, "latency" in x ? true : false);
    19          A.store.Float64(ptr + 8, x["latency"] === undefined ? 0 : (x["latency"] as number));
    20        }
    21      },
    22      "load_SendPacketResult": (ptr: Pointer, create: heap.Ref<boolean>, ref: heap.Ref<any>): heap.Ref<any> => {
    23        const x = create === A.H.TRUE ? {} : A.H.get<any>(ref);
    24  
    25        x["ip"] = A.load.Ref(ptr + 0, undefined);
    26        if (A.load.Bool(ptr + 16)) {
    27          x["latency"] = A.load.Float64(ptr + 8);
    28        } else {
    29          delete x["latency"];
    30        }
    31        return create === A.H.TRUE ? A.H.push(x) : ref;
    32      },
    33  
    34      "store_SendPacketOptions": (ptr: Pointer, ref: heap.Ref<any>) => {
    35        const x = A.H.get<any>(ref);
    36  
    37        if (typeof x === "undefined") {
    38          A.store.Bool(ptr + 19, false);
    39          A.store.Ref(ptr + 0, undefined);
    40          A.store.Bool(ptr + 16, false);
    41          A.store.Int32(ptr + 4, 0);
    42          A.store.Bool(ptr + 17, false);
    43          A.store.Int32(ptr + 8, 0);
    44          A.store.Bool(ptr + 18, false);
    45          A.store.Int32(ptr + 12, 0);
    46        } else {
    47          A.store.Bool(ptr + 19, true);
    48          A.store.Ref(ptr + 0, x["ip"]);
    49          A.store.Bool(ptr + 16, "ttl" in x ? true : false);
    50          A.store.Int32(ptr + 4, x["ttl"] === undefined ? 0 : (x["ttl"] as number));
    51          A.store.Bool(ptr + 17, "timeout" in x ? true : false);
    52          A.store.Int32(ptr + 8, x["timeout"] === undefined ? 0 : (x["timeout"] as number));
    53          A.store.Bool(ptr + 18, "size" in x ? true : false);
    54          A.store.Int32(ptr + 12, x["size"] === undefined ? 0 : (x["size"] as number));
    55        }
    56      },
    57      "load_SendPacketOptions": (ptr: Pointer, create: heap.Ref<boolean>, ref: heap.Ref<any>): heap.Ref<any> => {
    58        const x = create === A.H.TRUE ? {} : A.H.get<any>(ref);
    59  
    60        x["ip"] = A.load.Ref(ptr + 0, undefined);
    61        if (A.load.Bool(ptr + 16)) {
    62          x["ttl"] = A.load.Int32(ptr + 4);
    63        } else {
    64          delete x["ttl"];
    65        }
    66        if (A.load.Bool(ptr + 17)) {
    67          x["timeout"] = A.load.Int32(ptr + 8);
    68        } else {
    69          delete x["timeout"];
    70        }
    71        if (A.load.Bool(ptr + 18)) {
    72          x["size"] = A.load.Int32(ptr + 12);
    73        } else {
    74          delete x["size"];
    75        }
    76        return create === A.H.TRUE ? A.H.push(x) : ref;
    77      },
    78      "has_SendPacket": (): heap.Ref<boolean> => {
    79        if (WEBEXT?.diagnostics && "sendPacket" in WEBEXT?.diagnostics) {
    80          return A.H.TRUE;
    81        }
    82        return A.H.FALSE;
    83      },
    84      "func_SendPacket": (fn: Pointer): void => {
    85        A.store.Ref(fn, WEBEXT.diagnostics.sendPacket);
    86      },
    87      "call_SendPacket": (retPtr: Pointer, options: Pointer): void => {
    88        const options_ffi = {};
    89  
    90        options_ffi["ip"] = A.load.Ref(options + 0, undefined);
    91        if (A.load.Bool(options + 16)) {
    92          options_ffi["ttl"] = A.load.Int32(options + 4);
    93        }
    94        if (A.load.Bool(options + 17)) {
    95          options_ffi["timeout"] = A.load.Int32(options + 8);
    96        }
    97        if (A.load.Bool(options + 18)) {
    98          options_ffi["size"] = A.load.Int32(options + 12);
    99        }
   100  
   101        const _ret = WEBEXT.diagnostics.sendPacket(options_ffi);
   102        A.store.Ref(retPtr, _ret);
   103      },
   104      "try_SendPacket": (retPtr: Pointer, errPtr: Pointer, options: Pointer): heap.Ref<boolean> => {
   105        try {
   106          const options_ffi = {};
   107  
   108          options_ffi["ip"] = A.load.Ref(options + 0, undefined);
   109          if (A.load.Bool(options + 16)) {
   110            options_ffi["ttl"] = A.load.Int32(options + 4);
   111          }
   112          if (A.load.Bool(options + 17)) {
   113            options_ffi["timeout"] = A.load.Int32(options + 8);
   114          }
   115          if (A.load.Bool(options + 18)) {
   116            options_ffi["size"] = A.load.Int32(options + 12);
   117          }
   118  
   119          const _ret = WEBEXT.diagnostics.sendPacket(options_ffi);
   120          A.store.Ref(retPtr, _ret);
   121          return A.H.TRUE;
   122        } catch (err: any) {
   123          A.store.Ref(errPtr, err);
   124          return A.H.FALSE;
   125        }
   126      },
   127    };
   128  });