github.com/primecitizens/pcz/std@v0.2.1/core/sys/bindings/ffi_bindings.ts (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  
     4  import { Application, Pointer, importModule } from "@ffi";
     5  
     6  importModule("core/sys", (A: Application) => {
     7    let _args: string[] = [];
     8    let _environ: string[] = [];
     9    Object.defineProperty(A, "args", {
    10      get: (): string[] => {
    11        return _args;
    12      },
    13      set: (v: string[]) => {
    14        _args = v;
    15      },
    16    });
    17    Object.defineProperty(A, "environ", {
    18      get: (): string[] => {
    19        return _environ;
    20      },
    21      set: (v: string[]) => {
    22        _environ = v;
    23      },
    24    });
    25  
    26    return {
    27      "sizes": (pArgcAndEnvc: Pointer): number => {
    28        pArgcAndEnvc >>>= 0;
    29        let sum = 0;
    30        _args.forEach((v) => {
    31          sum += A.UTF8Sizeof(v);
    32        });
    33        _environ.forEach((v) => {
    34          sum += A.UTF8Sizeof(v);
    35        });
    36  
    37        A.store.Uint32(pArgcAndEnvc, _args.length + _environ.length);
    38        return sum;
    39      },
    40      "nth": (i: number, buf: Pointer, sz: number): number => {
    41        if (i < _args.length) {
    42          return A.store.String(buf, sz, _args[i]).n;
    43        }
    44        if (i === _args.length || i > _args.length + _environ.length) {
    45          return -1;
    46        }
    47  
    48        return A.store.String(buf, sz, _environ[i - _args.length - 1]).n;
    49      },
    50    };
    51  });