Skip to content

v.embed_file

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.

If the chunk table ends before len bytes, the remaining bytes are zero-filled. 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 is the size of what `data()` returns: the size the file had when the program
	// was compiled, until a development build loads the file, and what it loaded after.
	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

to_string returns an independent, zero-terminated copy of the embedded bytes. Embedded zero bytes are preserved and do not shorten the returned 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.