How does newDataView work?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
UnixRoot
Citizen
Posts: 80
Joined: Mon Nov 08, 2021 8:10 am

How does newDataView work?

Post by UnixRoot »

Hey guys. I'm trying to use

Code: Select all

love.data.newDataView( data, offset, size )

How does the size parameter work?

According to the wiki, it's the length of the subsection in bytes. Unfortunately, it doesn't work. It doesn't matter if I set the size to 1 or 1000 bytes, I still have access to the whole data object and not just to the subsection I want. The offset value works like it should, but the size value does absolutely nothing.

https://love2d.org/wiki/love.data.newDataView
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: How does newDataView work?

Post by slime »

What code are you using to access the data? The size parameter sets the size that Data:getSize reports, which is the value used when you pass the Data object into various love APIs.

Since it's just a view into the same memory as the original Data object, it doesn't copy the subsection to a new location in memory so nothing stops you from reading past the given offset and size if you're using the FFI or similar tools (similar to how you can read past the end of any allocated array with the FFI).
User avatar
UnixRoot
Citizen
Posts: 80
Joined: Mon Nov 08, 2021 8:10 am

Re: How does newDataView work?

Post by UnixRoot »

I'm using an FFI pointer to access the data.

Code: Select all

view = love.data.newDataView( data, offset, 128*128 )
tex  = ffi.cast( 'uint8_t*', view:getFFIPointer() )
The wiki states "Data:getFFIPointer will return the original Data object's contents, with the view's offset and size applied"

If all the data outside the "view" is still accessible, the wiki text is slightly misleading if you ask me.
slime wrote: Sat Jun 10, 2023 1:20 pm (similar to how you can read past the end of any allocated array with the FFI).
Of course, but then I only get garbage and not the contents of the array.
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: How does newDataView work?

Post by slime »

UnixRoot wrote: Sat Jun 10, 2023 1:31 pm The wiki states "Data:getFFIPointer will return the original Data object's contents, with the view's offset and size applied"
This is actually the full sentence:
"Data:getString, Data:getPointer and Data:getFFIPointer will return the original Data object's contents, with the view's offset and size applied."

The size is only applicable to one of those (Data:getString) and has no meaning for the getPointer APIs, so I've updated the text there to add "where applicable" at the end of the sentence.
UnixRoot wrote: Sat Jun 10, 2023 1:31 pm If all the data outside the "view" is still accessible, the wiki text is slightly misleading if you ask me.
slime wrote: Sat Jun 10, 2023 1:20 pm (similar to how you can read past the end of any allocated array with the FFI).
Of course, but then I only get garbage and not the contents of the array.
When you use the FFI, you are 100% obligated to handle bounds checking yourself no matter what. You cannot rely on crashes or garbage data to detect bounds. Even reading past the end of an array with an explicitly allocated size is not guaranteed to crash or produce garbage, it's just likely to.

So ultimately it's not love's responsibility (and nor is it possible for it to be love's responsibility) to make your own FFI code do its own bounds-checking.

https://luajit.org/ext_ffi_semantics.html#policy
User avatar
UnixRoot
Citizen
Posts: 80
Joined: Mon Nov 08, 2021 8:10 am

Re: How does newDataView work?

Post by UnixRoot »

Yeah, an out-of-bounds check isn't a problem, I already do it on my data objects with raw palette indices. I only found the "newDataView" function while browsing the wiki, and it sounded nice.
Post Reply

Who is online

Users browsing this forum: Yolwoocle and 21 guests