Difference between revisions of "Buffer luau"

From GiderosMobile
(wip)
(wip)
Line 6: Line 6:
 
=== Description ===
 
=== Description ===
 
This is the Luau buffer library implemented in Gideros.
 
This is the Luau buffer library implemented in Gideros.
 +
 +
A buffer is an object that represents a fixed-size mutable block of memory. The buffer library provides functions for creation and manipulation of buffer objects, providing all its functions inside the global buffer variable.
 +
 +
Buffer is intended to be used as a low-level binary data storage structure, replacing the uses of string.pack() and string.unpack(). Use cases include reading and writing existing binary formats, working with data in a more compact form, serialization to custom binary formats, and general work with native memory types like fixed-length integers and floats.
 +
 +
Many of the functions accept an offset in bytes from the start of the buffer. Offset of 0 from the start of the buffer memory block accesses the first byte. All offsets, counts and sizes should be non-negative integer numbers. If the bytes that are accessed by any read or write operation are outside the buffer memory, an error is thrown.
 +
 +
The read and write methods that work with integers and floats use little-endian encoding.
  
 
=== Example ===
 
=== Example ===
Line 17: Line 25:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== Reference ===
+
=== Disclaimer ===
 +
Documentation is based on '''create.roblox.com''' documentation.
 +
 
 
https://luau.org/library#buffer-library</br>
 
https://luau.org/library#buffer-library</br>
 
https://create.roblox.com/docs/reference/engine/libraries/buffer
 
https://create.roblox.com/docs/reference/engine/libraries/buffer
Line 24: Line 34:
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
 
=== Methods ===
 
=== Methods ===
[[buffer.create]] ''creates a buffer of the requested size with all bytes initialized to 0''<br/><!--GIDEROSMTD:buffer.create(size) creates a buffer of the requested size with all bytes initialized to 0-->
+
[[buffer.create]] ''creates a buffer''<br/><!--GIDEROSMTD:buffer.create(size) creates a buffer-->
[[collectgarbage]] ''opts: stop, restart, collect, count, step, setpause, setstepmul''<br/><!--GIDEROSMTD:collectgarbage(opt,arg) opts: stop, restart, collect, count, step, setpause, setstepmul-->
+
[[buffer.fromstring]] ''creates a buffer from a string''<br/><!--GIDEROSMTD:buffer.fromstring(string) creates a buffer from a string-->
[[ColorValue]] ''sets a Color the r,g,b,a channel values''<br/><!--GIDEROSMTD:ColorValue(r,g,b,a) sets a Color the r,g,b,a channel values-->
+
[[buffer.tostring]] ''converts a buffer to a string''<br/><!--GIDEROSMTD:buffer.tostring(buffer) converts a buffer to a string-->
[[dofile]] ''executes as Lua chunk, default stdin, returns value''<br/><!--GIDEROSMTD:dofile(filename) executes as Lua chunk, default stdin, returns value-->
+
[[buffer.len]] ''returns the size of the buffer in bytes''<br/><!--GIDEROSMTD:buffer.len(buffer) returns the size of the buffer in bytes-->
[[error]] ''terminates protected func, never returns''<br/><!--GIDEROSMTD:error(message,level) terminates protected func, never returns-->
+
[[buffer.read]] ''reads from the buffer''<br/><!--reads from the buffer-->
[[xpcall]] ''pcall function f with new error handler err''<br/><!--GIDEROSMTD:xpcall(f,err) pcall function f with new error handler err-->
+
[[buffer.write]] ''writes to the buffer''<br/><!--writes to the buffer-->
  
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
Line 35: Line 45:
 
=== Constants ===
 
=== Constants ===
 
|}
 
|}
 +
 +
<!--GIDEROSMTD:buffer.readi8(buffer,offset) reads an 8-bit signed integer from the buffer-->
 +
<!--GIDEROSMTD:buffer.readu8(buffer,offset) reads an 8-bit unsigned integer from the buffer-->
 +
<!--GIDEROSMTD:buffer.readi16(buffer,offset) reads a 16-bit signed integer from the buffer-->
 +
<!--GIDEROSMTD:buffer.readu16(buffer,offset) reads a 16-bit unsigned integer from the buffer-->
 +
<!--GIDEROSMTD:buffer.readi32(buffer,offset) reads a 32-bit signed integer from the buffer-->
 +
<!--GIDEROSMTD:buffer.readu32(buffer,offset) reads a 32-bit unsigned integer from the buffer-->
 +
<!--GIDEROSMTD:buffer.readf32(buffer,offset) reads a 32-bit floating-point value from the buffer-->
 +
<!--GIDEROSMTD:buffer.readf64(buffer,offset) reads a 64-bit floating-point value from the buffer-->
 +
<!--GIDEROSMTD:buffer.writei8(buffer,offset,value) writes an 8-bit signed integer to the buffer-->
 +
<!--GIDEROSMTD:buffer.writeu8(buffer,offset,value) writes an 8-bit unsigned integer to the buffer-->
 +
<!--GIDEROSMTD:buffer.writei16(buffer,offset,value) writes a 16-bit signed integer to the buffer-->
 +
<!--GIDEROSMTD:buffer.writeu16(buffer,offset,value) writes a 16-bit unsigned integer to the buffer-->
 +
<!--GIDEROSMTD:buffer.writei32(buffer,offset,value) writes a 32-bit signed integer to the buffer-->
 +
<!--GIDEROSMTD:buffer.writeu32(buffer,offset,value) writes a 32-bit unsigned integer to the buffer-->
 +
<!--GIDEROSMTD:buffer.writef32(buffer,offset,value) writes a 32-bit floating-point value to the buffer-->
 +
<!--GIDEROSMTD:buffer.writef64(buffer,offset,value) writes a 64-bit floating-point value to the buffer-->
  
 
{{GIDEROS IMPORTANT LINKS}}
 
{{GIDEROS IMPORTANT LINKS}}

Revision as of 01:55, 6 February 2025

Supported platforms: Platform android.pngPlatform ios.pngPlatform mac.pngPlatform pc.pngPlatform linux.png
Available since: Gideros 2025.1

Description

This is the Luau buffer library implemented in Gideros.

A buffer is an object that represents a fixed-size mutable block of memory. The buffer library provides functions for creation and manipulation of buffer objects, providing all its functions inside the global buffer variable.

Buffer is intended to be used as a low-level binary data storage structure, replacing the uses of string.pack() and string.unpack(). Use cases include reading and writing existing binary formats, working with data in a more compact form, serialization to custom binary formats, and general work with native memory types like fixed-length integers and floats.

Many of the functions accept an offset in bytes from the start of the buffer. Offset of 0 from the start of the buffer memory block accesses the first byte. All offsets, counts and sizes should be non-negative integer numbers. If the bytes that are accessed by any read or write operation are outside the buffer memory, an error is thrown.

The read and write methods that work with integers and floats use little-endian encoding.

Example

-- creates a buffer initialized to the contents of the string
local str = "Hello Gideros!"
b = buffer.fromstring(str)
print(buffer.len(b))
-- returns the buffer data as a string
print(buffer.tostring(b))

Disclaimer

Documentation is based on create.roblox.com documentation.

https://luau.org/library#buffer-library
https://create.roblox.com/docs/reference/engine/libraries/buffer

Methods

buffer.create creates a buffer
buffer.fromstring creates a buffer from a string
buffer.tostring converts a buffer to a string
buffer.len returns the size of the buffer in bytes
buffer.read reads from the buffer
buffer.write writes to the buffer

Events

Constants