nupp.mem.sharedbytes

Engine-owned immutable byte regions.

A region names an extent of one reference-counted engine block. It crosses worker boundaries by reference, slices without copying, and is read in place through a borrowed nupp.mem.span byte view; converting it to a Lua string is one explicit call.

const sharedbytes = nupp.mem.sharedbytes

const region = sharedbytes.copy("hello, region")
const hello = region:slice(0, 5)
assert(hello:length() == 5 and hello:toString() == "hello")

A region is storage, so its positions are zero-based offsets and counts, as a nupp.io.Buffer's are: slice(offset, count) and view(offset, count) name count bytes starting offset bytes in.

A producer that has bytes only behind a pointer writes into builder storage directly through a reserve-and-commit pair, with no intermediate chunk string:

const sharedbytes = nupp.mem.sharedbytes

local builder = sharedbytes.newBuilder()
with writer = builder:reserve(5) do
    for index = 1, 5 do
        writer[index] = 64 + index
    end
end
builder:commit(5)
builder:append("!")
assert(builder:freeze():toString() == "ABCDE!")

Module contents

Types

TypeKindDescription
BuilderrecordA growable engine allocation that becomes a region when frozen.
BuilderTokeninterfaceAn affine accumulator of engine storage.
RegionrecordAn immutable extent of an engine-owned block.

Functions

FunctionKindDescription
copyfunctionCopies text once into an engine-owned region.
newBuilderfunctionOpens an empty builder.
readFilefunctionReads a whole file directly into an engine-owned region, with no intermediate Lua string for the payload.

Types#

Builderrecord#

record sharedbytes.Builder is sharedbytes.BuilderToken ...

A growable engine allocation that becomes a region when frozen.

Members

NameKindDescription
appendmethodAppends the chunk's bytes to the builder's storage.
reservemethodGrows the storage and lends a checked writer over exactly count uninitialized bytes past what is already committed.
commitmethodCloses the open reservation, keeping its first written bytes as content; a short read commits what arrived, and the...
freezemethodConsumes the builder and transfers its allocation into a region.
dropmethodReleases an unfrozen builder's storage at scope exit.

appendmethod#

append: @nosuspend function(sharedbytes.Builder, string): nil

Appends the chunk's bytes to the builder's storage.

Arguments
NameTypeDescription
?sharedbytes.Builder
?string
Returns
TypeDescription
nil
Raises
TypeCondition
string

when a reservation is open or the engine cannot grow the storage

reservemethod#

reserve: function(exclusive self: sharedbytes.Builder, integer): span.Writable<uint8> borrows (self)

Grows the storage and lends a checked writer over exactly count uninitialized bytes past what is already committed. All growth happens here, before the writer exists, and the borrow keeps every other builder operation a compile error while the writer lives, so the lent pointer cannot be invalidated. One commit closes the reservation after the writer drops.

Arguments
NameTypeDescription
exclusive selfsharedbytes.Builder
?integer
Returns
TypeDescription
span.Writable<uint8> borrows (self)
Raises
TypeCondition
string

when count is not a nonnegative integer, a reservation is already open, or the engine cannot grow the storage

commitmethod#

commit: @nosuspend function(sharedbytes.Builder, integer): nil

Closes the open reservation, keeping its first written bytes as content; a short read commits what arrived, and the surplus returns to capacity for the next reservation.

Arguments
NameTypeDescription
?sharedbytes.Builder
?integer
Returns
TypeDescription
nil
Raises
TypeCondition
string

when written is not a nonnegative integer, no reservation is open, or written exceeds it

freezemethod#

freeze: @nosuspend function(takes self: sharedbytes.Builder): sharedbytes.Region

Consumes the builder and transfers its allocation into a region.

Arguments
NameTypeDescription
takes selfsharedbytes.Builder
Returns
TypeDescription
sharedbytes.Region
Raises
TypeCondition
string

when the builder was already frozen or a reservation is open, because sealing would silently drop bytes a producer may have written

dropmethod#

drop: @nosuspend function(takes self: sharedbytes.Builder): nil

Releases an unfrozen builder's storage at scope exit.

Arguments
NameTypeDescription
takes selfsharedbytes.Builder
Returns
TypeDescription
nil

BuilderTokeninterface#

interface sharedbytes.BuilderToken is nupp.Affine<self.drop> ...

An affine accumulator of engine storage. Appending grows one private allocation, and freezing transfers it into a region without copying.

Members

NameKindDescription
dropmethod

dropmethod#

drop: @nosuspend function(takes self: sharedbytes.BuilderToken): nil
Arguments
NameTypeDescription
takes selfsharedbytes.BuilderToken
Returns
TypeDescription
nil

Regionrecord#

record sharedbytes.Region ...

An immutable extent of an engine-owned block. Crossing a worker boundary moves a reference, never the bytes; reading happens in place through a borrowed byte view, and toString is the one explicit copy into a Lua state.

Members

NameKindDescription
lengthmethodHow many bytes the region holds.
slicemethodA zero-copy region over count bytes starting offset bytes into this one.
viewmethodA read-only borrowed view over count bytes starting offset bytes in, the whole region by default.
viewAsmethodA checked zero-copy view of the complete region as physical elements.
toStringmethodThe region's bytes copied and interned as an ordinary string.

lengthmethod#

length: @nosuspend function(sharedbytes.Region): integer

How many bytes the region holds.

Arguments
NameTypeDescription
?sharedbytes.Region
Returns
TypeDescription
integer

slicemethod#

slice: function(sharedbytes.Region, integer, integer): sharedbytes.Region

A zero-copy region over count bytes starting offset bytes into this one.

Arguments
NameTypeDescription
?sharedbytes.Region
?integer
?integer
Returns
TypeDescription
sharedbytes.Region
Raises
TypeCondition
string

when the extent falls outside the region

viewmethod#

view: function(borrows self: sharedbytes.Region, integer?, integer?): span.ByteSpan borrows (self)

A read-only borrowed view over count bytes starting offset bytes in, the whole region by default.

Arguments
NameTypeDescription
borrows selfsharedbytes.Region
?integer?
?integer?
Returns
TypeDescription
span.ByteSpan borrows (self)
Raises
TypeCondition
string

when the extent falls outside the region

viewAsmethod#

viewAs: function<T>(borrows self: sharedbytes.Region, ctype<T>): span.Span<T> borrows (self)

A checked zero-copy view of the complete region as physical elements.

Arguments
NameTypeDescription
borrows selfsharedbytes.Region
?ctype<T>
Returns
TypeDescription
span.Span<T> borrows (self)
Raises
TypeCondition
string

when the region is not aligned or is not a whole number of elements

toStringmethod#

toString: @nosuspend function(sharedbytes.Region): string

The region's bytes copied and interned as an ordinary string.

Arguments
NameTypeDescription
?sharedbytes.Region
Returns
TypeDescription
string
Raises
TypeCondition
string

when the region's engine storage has been released

Functions#

sharedbytes.copyfunction#

function sharedbytes.copy(text: string): sharedbytes.Region

Copies text once into an engine-owned region.

Arguments

NameTypeDescription
textstring

Returns

TypeDescription
sharedbytes.Region

Raises

TypeCondition
string

when the engine cannot allocate the region

sharedbytes.newBuilderfunction#

function sharedbytes.newBuilder(): affine(sharedbytes.Builder)

Opens an empty builder.

Returns

TypeDescription
affine(sharedbytes.Builder)

Raises

TypeCondition
string

when the engine cannot allocate the builder

sharedbytes.readFilefunction#

function sharedbytes.readFile(path: string): sharedbytes.Region?, string?

Reads a whole file directly into an engine-owned region, with no intermediate Lua string for the payload.

Arguments

NameTypeDescription
pathstring

the native path of the file to read

Returns

TypeDescription
sharedbytes.Region?

the region, or nil when the file cannot be opened, sized, or read

string?

why it could not be read, when unsuccessful