Skip to content

Unable to create ref-array-di inside ref-struct-di #27

Description

@berendsliedrecht

(I created the same issue here as it concerns both libraries)

I am trying to create a structure, ByteBuffer which contains a len as the length of the array and a reference to an array. The array is created with ref-array-di and the struct with ref-struct-di.

When passing the array into the struct, and dereferencing it afterwards, gives an empty array back. While dereferencing the referenced array gives the complete data back.

When I manually assign the type of array to byteBuffer.data, but this can not be done outside of this example (as I do not have the array object there).

import refArray from "ref-array-di"
import refStruct from "ref-struct-di"
import * as ref from "ref-napi"

const CArray = refArray(ref)
const CStruct = refStruct(ref)

const ByteBufferArray = CArray(ref.types.uint8)

const ByteBufferStruct = CStruct({
  data: ref.refType(ByteBufferArray),
  len: ref.types.int64,
})

const array = new ByteBufferArray(Array(10).fill(10))

// @ts-ignore
const byteBuffer = new ByteBufferStruct({ len: array.length, data: array.ref() })

const fromStruct = new Uint8Array(byteBuffer.data.deref())
const fromArray = new Uint8Array(array)

const isSameLength = fromStruct.length === fromArray.length
if (!isSameLength) {
  console.error(`fromStruct: [${fromStruct}]`)
  console.error(`fromArray: [${fromArray}]`)
} else {
  console.log("it works!")
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions