Skip to content

v.embed_file #

join_chunks_raw_alloc reserves size bytes that do not come from V's allocator, and returns nil if the request cannot be met.

It exists for the one case in join_chunks_buffer that must not go through the preallocator; see the comment there for why. The raw allocator is reached from here rather than from the plain .v file so that the module stays free of C. symbols outside a .c.v one.

fn find_index_entry_by_path #

@[markused]
fn find_index_entry_by_path(start voidptr, path string, algo string) &EmbedFileIndexEntry

find_index_entry_by_path is used internally by the V compiler:

fn join_chunks #

@[markused]
fn join_chunks(chunks &EmbedFileChunk, len int) &u8

join_chunks copies a payload that was embedded in pieces into one buffer, and is called by generated code while the EmbedFileData is being built, not when its bytes are first read. Materializing it there and not later is what keeps data() free of the lazy mutation that two threads reading the same embedded constant would race on.

The buffer lives as long as the program, like the static bytes it replaces.

fn register_decoder #

fn register_decoder(compression_type string, decoder Decoder)

interface Decoder #

interface Decoder {
	decompress([]u8) ![]u8
}

struct EmbedFileChunk #

struct EmbedFileChunk {
pub:
	data &u8 = unsafe { nil }
	len  int
}

EmbedFileChunk is one piece of an embedded file. C only guarantees that an implementation accepts 65535 bytes in a single object, so a payload past that is emitted as several of these, and join_chunks puts them back together.

A table of them ends with a zero length entry. If that entry still carries a pointer, it is not the end but a link to the next table, which is how a table stays an acceptable size itself.

struct EmbedFileData #

struct EmbedFileData {
	apath            string
	compression_type string
mut:
	compressed        &u8 = unsafe { nil }
	compressed_len    int
	uncompressed      &u8 = unsafe { nil }
	free_compressed   bool
	free_uncompressed bool
pub:
	len  int
	path string
}

EmbedFileData encapsulates functionality for the $embed_file() compile time call.

fn (EmbedFileData) str #

fn (ed EmbedFileData) str() string

fn (EmbedFileData) free #

@[unsafe]
fn (mut ed EmbedFileData) free()

fn (EmbedFileData) to_string #

fn (original &EmbedFileData) to_string() string

fn (EmbedFileData) to_bytes #

fn (original &EmbedFileData) to_bytes() []u8

fn (EmbedFileData) data #

fn (original &EmbedFileData) data() &u8

data returns the embedded bytes, loading or decompressing them on the first call if needed.

struct EmbedFileIndexEntry #

struct EmbedFileIndexEntry {
	id   int
	path string
	algo string
	data &u8 = unsafe { nil }
}

//////////////////////////////////////////////////////////////////////////// EmbedFileIndexEntry is used internally by the V compiler when you compile a program that uses $embed_file('file.bin') in -prod mode. V will generate a static index of all embedded files, and will call the find_index_entry_by_path over the index and the relative paths of the embeds.

Note: these are public on purpose, to help -usecache.