Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
berendsliedrecht opened this issue Jun 14, 2022 · 0 comments
Open

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

berendsliedrecht opened this issue Jun 14, 2022 · 0 comments

Comments

@berendsliedrecht
Copy link

(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!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant