Context

Jai has a first class concept of context, which is always present and accessible, but beyond being a simple global state variable, a new context can be “pushed”, changing the operational context for a duration. But before we get into that, let’s look at what a context provides.

In most languages, various system-level services such as memory allocations or logging are done via some common library function. For example, in C, you allocate memory using the malloc function. If you want to create your own custom allocator, it has to have a different name and will only be used when explicitly called. This means that libraries that you call out to won’t know to use your clever custom allocator. Same is true of other features, like loggers, or what have you. Sometimes this will make you sad because a pesky library is messing up your heap or is slinging undesirable commentary at your terminal.

In Jai, the context is there to guide different parts of your program to use whatever services you desire. The default context comes with good defaults, but you can change them as needed. Among these are the allocator, the temporary allocator, the logger, the assertion failure callback, formatting options for print, runtime error logging options, and so on. You can also access your current thread index through the context. But wait, there’s more: if for whatever reason it makes sense for your program to have additional context, you can add it yourself!

push_context

Example:

my_context: Context;
push_context my_context {
    // Do stuff here with this context.
}

Swapping out your allocator

TODO.

#add_context

If you want to add something to your context, you use the #add_context compiler directive, like so:

#add_context this_is_the_way := true;
1 Like