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. See NEP 20.

const sharedbytes = nupp.mem.sharedbytes

const region = sharedbytes.copy("hello, region")
const hello = region:slice(1, 5)
assert(hello:size() == 5 and hello:text() == "hello")

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.builder()
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():text() == "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 one engine-owned block.

Functions

FunctionKindDescription
builderfunctionOpens an empty builder.
copyfunctionCopies text once into an engine-owned region.
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
    append: nosuspend function(sharedbytes.Builder, string): nil
    reserve: function(exclusive self: sharedbytes.Builder, integer): span.Writable<uint8> borrows (self)
    commit: nosuspend function(sharedbytes.Builder, integer): nil
    freeze: nosuspend function(takes self: sharedbytes.Builder): sharedbytes.Region
    drop: nosuspend function(takes self: sharedbytes.Builder): nil
end

A growable engine allocation that becomes a region when frozen.

Methods

append#
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
  • when a reservation is open or the engine cannot grow the storage

reserve#
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
  • when the count is negative, a reservation is already open, or the engine cannot grow the storage

commit#
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
  • when no reservation is open or written exceeds it

freeze#
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
  • when the builder was already frozen or a reservation is open, because sealing would silently drop bytes a producer may have written

drop#
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#

affine interface sharedbytes.BuilderToken
    terminal drop: nosuspend function(takes self: sharedbytes.BuilderToken): nil
end

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

Methods

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

Regionrecord#

record sharedbytes.Region
    size: nosuspend function(sharedbytes.Region): integer
    slice: function(sharedbytes.Region, integer, integer): sharedbytes.Region
    view: function(borrows self: sharedbytes.Region, integer?, integer?): span.ByteSpan borrows (self)
    viewAs: function<T>(borrows self: sharedbytes.Region, ctype<T>): span.Span<T> borrows (self)
    text: nosuspend function(sharedbytes.Region): string
end

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

Methods

size#
size: nosuspend function(sharedbytes.Region): integer

How many bytes the region holds.

Arguments
NameTypeDescription
?sharedbytes.Region
Returns
TypeDescription
integer
slice#
slice: function(sharedbytes.Region, integer, integer): sharedbytes.Region

A zero-copy region over bytes first through last of this one.

Arguments
NameTypeDescription
?sharedbytes.Region
?integer
?integer
Returns
TypeDescription
sharedbytes.Region
Raises
  • when the bounds fall outside the region

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

A read-only borrowed view over the selected bytes, whole by default.

Arguments
NameTypeDescription
borrows selfsharedbytes.Region
?integer?
?integer?
Returns
TypeDescription
span.ByteSpan borrows (self)
Raises
  • when the bounds fall outside the region

viewAs#
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
  • when the region is not aligned or is not a whole number of elements

text#
text: nosuspend function(sharedbytes.Region): string

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

Arguments
NameTypeDescription
?sharedbytes.Region
Returns
TypeDescription
string
Raises
  • when the region's engine storage has been released

Functions#

sharedbytes.builderfunction#

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

Opens an empty builder.

Returns

TypeDescription
affine(sharedbytes.Builder)

Raises

  • when the engine cannot allocate the builder

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

  • when the engine cannot allocate the region

sharedbytes.readFilefunction#

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

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

Arguments

NameTypeDescription
pathstring

Returns

TypeDescription
sharedbytes.Region

Raises

  • when the file cannot be opened, sized, or read