NativeScript / ios

NativeScript for iOS and visionOS using V8
https://docs.nativescript.org/guide/ios-marshalling
130 stars 33 forks source link

interop.Reference returns undefined value #134

Open DeepakArora76 opened 2 years ago

DeepakArora76 commented 2 years ago

Hi everyone,

I need your help to understand why the vmStatRef.value.free_count in the below code returns undefine value.

I am using NativeScript 8.1. The platform is iOS.

  "devDependencies": {
    "@nativescript/android": "~8.1.1",
    "@nativescript/core": "~8.1.4",
    "@nativescript/ios": "~8.1.0",
    "@nativescript/types": "~8.1.1",
    "@nativescript/webpack": "^3.0.0",
    "prompt": "^1.2.0",
    "rimraf": "^3.0.2",
    "semver": "^7.3.5",
    "ts-node": "^10.4.0",
    "ts-patch": "^1.4.4",
    "tslint": "^6.1.3",
    "typescript": "~4.4.4"
  }

The git repository is https://github.com/DeepakArora76/nativescript-dna-deviceinfo

Here is a 64-bit implementation

  static freeMemory(): number {
    const KERN_SUCCESS = 0;
    const HOST_VM_INFO = 2;
    const hostSize = new interop.Reference(interop.sizeof(vm_statistics64)/interop.sizeof(interop.types.int64));
    const hostPort = mach_host_self();

    const pageSize = new interop.Reference(0);
    host_page_size(hostPort, pageSize);

    const vmStat = interop.alloc(interop.sizeof(vm_statistics64));

    let freeMem = -1;
    if (host_statistics64(hostPort, HOST_VM_INFO, vmStat, hostSize) === KERN_SUCCESS) {
      const vmStatRef = new interop.Reference<vm_statistics64>(vm_statistics64, vmStat);
      freeMem = vmStatRef.value.free_count * pageSize.value;
    }
    return freeMem;
  }

I also tried vm_statistics instead of vm_statistics64 and that too failed as well!

32-bit implementation

  static freeMemory(): number {
    const KERN_SUCCESS = 0;
    const HOST_VM_INFO = 2;
    const hostSize = new interop.Reference(interop.sizeof(vm_statistics)/interop.sizeof(interop.types.int32));
    const hostPort = mach_host_self();

    const pageSize = new interop.Reference(0);
    host_page_size(hostPort, pageSize);

    const vmStat = interop.alloc(interop.sizeof(vm_statistics));

    let freeMem = -1;
    if (host_statistics(hostPort, HOST_VM_INFO, vmStat, hostSize) === KERN_SUCCESS) {
      const vmStatRef = new interop.Reference<vm_statistics>(vm_statistics, vmStat);
      freeMem = vmStatRef.value.free_count * pageSize.value;
    }
    return freeMem;
  }
DeepakArora76 commented 2 years ago

@rigor789 A similar issue is https://github.com/NativeScript/ns-v8ios-runtime/issues/103

I would highly appreciate it if these issues could be prioritized. I hope we do not have to wait for many months to see a fix.

If there is a workaround for it, please suggest it.