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

     1  import { importModule, Application, heap, Pointer } from "@ffi";
     2  
     3  importModule("plat/js/webext/system/cpu", (A: Application) => {
     4    const WEBEXT = typeof globalThis.browser === "undefined" ? globalThis.chrome : globalThis.browser;
     5  
     6    return {
     7      "store_CpuTime": (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 + 36, false);
    12          A.store.Bool(ptr + 32, false);
    13          A.store.Float64(ptr + 0, 0);
    14          A.store.Bool(ptr + 33, false);
    15          A.store.Float64(ptr + 8, 0);
    16          A.store.Bool(ptr + 34, false);
    17          A.store.Float64(ptr + 16, 0);
    18          A.store.Bool(ptr + 35, false);
    19          A.store.Float64(ptr + 24, 0);
    20        } else {
    21          A.store.Bool(ptr + 36, true);
    22          A.store.Bool(ptr + 32, "user" in x ? true : false);
    23          A.store.Float64(ptr + 0, x["user"] === undefined ? 0 : (x["user"] as number));
    24          A.store.Bool(ptr + 33, "kernel" in x ? true : false);
    25          A.store.Float64(ptr + 8, x["kernel"] === undefined ? 0 : (x["kernel"] as number));
    26          A.store.Bool(ptr + 34, "idle" in x ? true : false);
    27          A.store.Float64(ptr + 16, x["idle"] === undefined ? 0 : (x["idle"] as number));
    28          A.store.Bool(ptr + 35, "total" in x ? true : false);
    29          A.store.Float64(ptr + 24, x["total"] === undefined ? 0 : (x["total"] as number));
    30        }
    31      },
    32      "load_CpuTime": (ptr: Pointer, create: heap.Ref<boolean>, ref: heap.Ref<any>): heap.Ref<any> => {
    33        const x = create === A.H.TRUE ? {} : A.H.get<any>(ref);
    34  
    35        if (A.load.Bool(ptr + 32)) {
    36          x["user"] = A.load.Float64(ptr + 0);
    37        } else {
    38          delete x["user"];
    39        }
    40        if (A.load.Bool(ptr + 33)) {
    41          x["kernel"] = A.load.Float64(ptr + 8);
    42        } else {
    43          delete x["kernel"];
    44        }
    45        if (A.load.Bool(ptr + 34)) {
    46          x["idle"] = A.load.Float64(ptr + 16);
    47        } else {
    48          delete x["idle"];
    49        }
    50        if (A.load.Bool(ptr + 35)) {
    51          x["total"] = A.load.Float64(ptr + 24);
    52        } else {
    53          delete x["total"];
    54        }
    55        return create === A.H.TRUE ? A.H.push(x) : ref;
    56      },
    57  
    58      "store_ProcessorInfo": (ptr: Pointer, ref: heap.Ref<any>) => {
    59        const x = A.H.get<any>(ref);
    60  
    61        if (typeof x === "undefined") {
    62          A.store.Bool(ptr + 37, false);
    63  
    64          A.store.Bool(ptr + 0 + 36, false);
    65          A.store.Bool(ptr + 0 + 32, false);
    66          A.store.Float64(ptr + 0 + 0, 0);
    67          A.store.Bool(ptr + 0 + 33, false);
    68          A.store.Float64(ptr + 0 + 8, 0);
    69          A.store.Bool(ptr + 0 + 34, false);
    70          A.store.Float64(ptr + 0 + 16, 0);
    71          A.store.Bool(ptr + 0 + 35, false);
    72          A.store.Float64(ptr + 0 + 24, 0);
    73        } else {
    74          A.store.Bool(ptr + 37, true);
    75  
    76          if (typeof x["usage"] === "undefined") {
    77            A.store.Bool(ptr + 0 + 36, false);
    78            A.store.Bool(ptr + 0 + 32, false);
    79            A.store.Float64(ptr + 0 + 0, 0);
    80            A.store.Bool(ptr + 0 + 33, false);
    81            A.store.Float64(ptr + 0 + 8, 0);
    82            A.store.Bool(ptr + 0 + 34, false);
    83            A.store.Float64(ptr + 0 + 16, 0);
    84            A.store.Bool(ptr + 0 + 35, false);
    85            A.store.Float64(ptr + 0 + 24, 0);
    86          } else {
    87            A.store.Bool(ptr + 0 + 36, true);
    88            A.store.Bool(ptr + 0 + 32, "user" in x["usage"] ? true : false);
    89            A.store.Float64(ptr + 0 + 0, x["usage"]["user"] === undefined ? 0 : (x["usage"]["user"] as number));
    90            A.store.Bool(ptr + 0 + 33, "kernel" in x["usage"] ? true : false);
    91            A.store.Float64(ptr + 0 + 8, x["usage"]["kernel"] === undefined ? 0 : (x["usage"]["kernel"] as number));
    92            A.store.Bool(ptr + 0 + 34, "idle" in x["usage"] ? true : false);
    93            A.store.Float64(ptr + 0 + 16, x["usage"]["idle"] === undefined ? 0 : (x["usage"]["idle"] as number));
    94            A.store.Bool(ptr + 0 + 35, "total" in x["usage"] ? true : false);
    95            A.store.Float64(ptr + 0 + 24, x["usage"]["total"] === undefined ? 0 : (x["usage"]["total"] as number));
    96          }
    97        }
    98      },
    99      "load_ProcessorInfo": (ptr: Pointer, create: heap.Ref<boolean>, ref: heap.Ref<any>): heap.Ref<any> => {
   100        const x = create === A.H.TRUE ? {} : A.H.get<any>(ref);
   101  
   102        if (A.load.Bool(ptr + 0 + 36)) {
   103          x["usage"] = {};
   104          if (A.load.Bool(ptr + 0 + 32)) {
   105            x["usage"]["user"] = A.load.Float64(ptr + 0 + 0);
   106          } else {
   107            delete x["usage"]["user"];
   108          }
   109          if (A.load.Bool(ptr + 0 + 33)) {
   110            x["usage"]["kernel"] = A.load.Float64(ptr + 0 + 8);
   111          } else {
   112            delete x["usage"]["kernel"];
   113          }
   114          if (A.load.Bool(ptr + 0 + 34)) {
   115            x["usage"]["idle"] = A.load.Float64(ptr + 0 + 16);
   116          } else {
   117            delete x["usage"]["idle"];
   118          }
   119          if (A.load.Bool(ptr + 0 + 35)) {
   120            x["usage"]["total"] = A.load.Float64(ptr + 0 + 24);
   121          } else {
   122            delete x["usage"]["total"];
   123          }
   124        } else {
   125          delete x["usage"];
   126        }
   127        return create === A.H.TRUE ? A.H.push(x) : ref;
   128      },
   129  
   130      "store_CpuInfo": (ptr: Pointer, ref: heap.Ref<any>) => {
   131        const x = A.H.get<any>(ref);
   132  
   133        if (typeof x === "undefined") {
   134          A.store.Bool(ptr + 25, false);
   135          A.store.Bool(ptr + 24, false);
   136          A.store.Int32(ptr + 0, 0);
   137          A.store.Ref(ptr + 4, undefined);
   138          A.store.Ref(ptr + 8, undefined);
   139          A.store.Ref(ptr + 12, undefined);
   140          A.store.Ref(ptr + 16, undefined);
   141          A.store.Ref(ptr + 20, undefined);
   142        } else {
   143          A.store.Bool(ptr + 25, true);
   144          A.store.Bool(ptr + 24, "numOfProcessors" in x ? true : false);
   145          A.store.Int32(ptr + 0, x["numOfProcessors"] === undefined ? 0 : (x["numOfProcessors"] as number));
   146          A.store.Ref(ptr + 4, x["archName"]);
   147          A.store.Ref(ptr + 8, x["modelName"]);
   148          A.store.Ref(ptr + 12, x["features"]);
   149          A.store.Ref(ptr + 16, x["processors"]);
   150          A.store.Ref(ptr + 20, x["temperatures"]);
   151        }
   152      },
   153      "load_CpuInfo": (ptr: Pointer, create: heap.Ref<boolean>, ref: heap.Ref<any>): heap.Ref<any> => {
   154        const x = create === A.H.TRUE ? {} : A.H.get<any>(ref);
   155  
   156        if (A.load.Bool(ptr + 24)) {
   157          x["numOfProcessors"] = A.load.Int32(ptr + 0);
   158        } else {
   159          delete x["numOfProcessors"];
   160        }
   161        x["archName"] = A.load.Ref(ptr + 4, undefined);
   162        x["modelName"] = A.load.Ref(ptr + 8, undefined);
   163        x["features"] = A.load.Ref(ptr + 12, undefined);
   164        x["processors"] = A.load.Ref(ptr + 16, undefined);
   165        x["temperatures"] = A.load.Ref(ptr + 20, undefined);
   166        return create === A.H.TRUE ? A.H.push(x) : ref;
   167      },
   168      "has_GetInfo": (): heap.Ref<boolean> => {
   169        if (WEBEXT?.system?.cpu && "getInfo" in WEBEXT?.system?.cpu) {
   170          return A.H.TRUE;
   171        }
   172        return A.H.FALSE;
   173      },
   174      "func_GetInfo": (fn: Pointer): void => {
   175        A.store.Ref(fn, WEBEXT.system.cpu.getInfo);
   176      },
   177      "call_GetInfo": (retPtr: Pointer): void => {
   178        const _ret = WEBEXT.system.cpu.getInfo();
   179        A.store.Ref(retPtr, _ret);
   180      },
   181      "try_GetInfo": (retPtr: Pointer, errPtr: Pointer): heap.Ref<boolean> => {
   182        try {
   183          const _ret = WEBEXT.system.cpu.getInfo();
   184          A.store.Ref(retPtr, _ret);
   185          return A.H.TRUE;
   186        } catch (err: any) {
   187          A.store.Ref(errPtr, err);
   188          return A.H.FALSE;
   189        }
   190      },
   191    };
   192  });