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

     1  import { importModule, Application, heap, Pointer } from "@ffi";
     2  
     3  importModule("plat/js/webext/system/memory", (A: Application) => {
     4    const WEBEXT = typeof globalThis.browser === "undefined" ? globalThis.chrome : globalThis.browser;
     5  
     6    return {
     7      "store_MemoryInfo": (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 + 18, false);
    12          A.store.Bool(ptr + 16, false);
    13          A.store.Float64(ptr + 0, 0);
    14          A.store.Bool(ptr + 17, false);
    15          A.store.Float64(ptr + 8, 0);
    16        } else {
    17          A.store.Bool(ptr + 18, true);
    18          A.store.Bool(ptr + 16, "capacity" in x ? true : false);
    19          A.store.Float64(ptr + 0, x["capacity"] === undefined ? 0 : (x["capacity"] as number));
    20          A.store.Bool(ptr + 17, "availableCapacity" in x ? true : false);
    21          A.store.Float64(ptr + 8, x["availableCapacity"] === undefined ? 0 : (x["availableCapacity"] as number));
    22        }
    23      },
    24      "load_MemoryInfo": (ptr: Pointer, create: heap.Ref<boolean>, ref: heap.Ref<any>): heap.Ref<any> => {
    25        const x = create === A.H.TRUE ? {} : A.H.get<any>(ref);
    26  
    27        if (A.load.Bool(ptr + 16)) {
    28          x["capacity"] = A.load.Float64(ptr + 0);
    29        } else {
    30          delete x["capacity"];
    31        }
    32        if (A.load.Bool(ptr + 17)) {
    33          x["availableCapacity"] = A.load.Float64(ptr + 8);
    34        } else {
    35          delete x["availableCapacity"];
    36        }
    37        return create === A.H.TRUE ? A.H.push(x) : ref;
    38      },
    39      "has_GetInfo": (): heap.Ref<boolean> => {
    40        if (WEBEXT?.system?.memory && "getInfo" in WEBEXT?.system?.memory) {
    41          return A.H.TRUE;
    42        }
    43        return A.H.FALSE;
    44      },
    45      "func_GetInfo": (fn: Pointer): void => {
    46        A.store.Ref(fn, WEBEXT.system.memory.getInfo);
    47      },
    48      "call_GetInfo": (retPtr: Pointer): void => {
    49        const _ret = WEBEXT.system.memory.getInfo();
    50        A.store.Ref(retPtr, _ret);
    51      },
    52      "try_GetInfo": (retPtr: Pointer, errPtr: Pointer): heap.Ref<boolean> => {
    53        try {
    54          const _ret = WEBEXT.system.memory.getInfo();
    55          A.store.Ref(retPtr, _ret);
    56          return A.H.TRUE;
    57        } catch (err: any) {
    58          A.store.Ref(errPtr, err);
    59          return A.H.FALSE;
    60        }
    61      },
    62    };
    63  });