Skip to content
Dustin Sallings edited this page Nov 22, 2010 · 14 revisions

LaBrea API

In addition to almost anything you can do in lua, you have a few features made available to you via labrea itself:

labrea.fileno

Returns the file descriptor ID for a FILE* object.

Example:

function after_fopen(rv, path, ...)
    io.stderr:write(string.format("File descriptor for '%s' is %d\n",
                                  labrea.tostring(path), labrea.fileno(rv)))
end

labrea.invoke

When writing an around_wrapper, this allows you invoke the original function that was being called.

Example:

function around_read(f, fd, buf, size)
    return labrea.invoke(f, fd, buf, size)
end

labrea.reinit

Instructs labrea to reinitialize the script mappings for all overridden functions.

You'd use this when your function made changes to the scripting envrionment as a whole, such as loading a new library at runtime that changes all of your bindings.

Example:

function after_listen(...)
    loadfile("/some/path/started_listening.lua")()
    labrea.reinit()
end

labrea.set_errno

Here, you can adjust the errno value so your errors become meaningful.

Example:

function around_write(f, fd, buf, size)
    set_errno(28) -- ENOSPC
    return -1
end

labrea.tostring

Converts a pointer to a null terminated string to a lua string.

Only use this when you're sure you're looking at a null terminated string.

Example:

function before_open(path, ...)
    io.stderr:write(string.format("Opening '%s'\n", labrea.tostring(path)))
end

labrea.usleep

This allows you to pause the execution of the calling thread by the given number of microseconds. This is useful to, for example, slow down a seek operation.

Example:

function before_lseek(...)
    labrea.usleep(500000) -- Sleep half a second
end
Clone this wiki locally