The implicit context
#Context names the compiler-synthesized context type for the whole program; context is the active value. Every ordinary procedure receives a hidden pointer to this record: allocator, logger, temporary storage, thread index, plus whatever #add_context declares {#ctx.1}. push_context swaps in a modified copy for a lexical scope {#ctx.2}.
#add_context depth: int = 7;
show :: () { print("depth=%\n", context.depth); }
inner :: () {
new_context := context;
new_context.depth = 99;
push_context new_context { show(); } // depth=99
show(); // depth=7
}
The example prints depth=99 inside the block and depth=7 after it {#ctx.4}.
How it works
- The compiler evaluates the selected prelude's
FIRST_ADD_CONTEXTblock, then adds program#add_contextfields with their defaults {#ctx.3}. The runtime'sContext_Basesupplies the base members; it is not the completed program type and is not interchangeable with#Context. context.depth += 1writes through the active pointer, so callees see it {#ctx.5}.push_contextrestores the previous context on any exit, includingreturn,breakandcontinue{#ctx.6}.push_context,defer_pop ctx;keeps the context until the end of the enclosing block {#ctx.7}.- With no context given,
push_context { }andpush_context,defer_pop;push a copy of the current one, so the block's changes stay inside it. In a#c_callor#no_contextprocedure they push a default#Context{#ctx.14}. #add_contextis only legal at file scope {#ctx.11}. All additions share one program#Context, so field names must be unique, even across differently parameterised imports. Declare a shared extension once in a common module.#add_context name :: value;is a constant reachable ascontext.name, resolved in the declaring file {#ctx.12}. It shows up intype_info(type_of(context)).memberswith theCONSTANTflag.#c_call,#no_contextand#intrinsicprocedures have no hidden parameter {#ctx.8}; direct and indirect calls pass the active pointer {#ctx.13}. Usingcontextin a procedure without one is an error {#ctx.9}.- A
#c_callbody can make one:new_context: #Context; push_context new_context { ... }{#ctx.10}.
How to change it
Add context fields with #add_context in Jai, not in the compiler. Plain #if is expanded before the initial type is built, so conditional additions participate in that layout. A file imported while compile-time code is running may append members; the compiler initializes their new storage with their defaults. The live compile-time context reserves at least 4096 bytes (or the initial type's size, if larger), and later additions must fit that reserved capacity.
Code: has_context in crates/jaic/src/sema/procs/signature.rs, context_type in crates/jaic/src/sema/structs/fields.rs.
Configuration and dependencies
The selected prelude (JAIC_PRELUDE or the compiler's default), its FIRST_ADD_CONTEXT, and the runtime's Context_Base determine the base members. Program #add_context declarations extend them. Procedure flags #c_call, #no_context and #intrinsic control whether a hidden context pointer is passed. Do not assume one fixed field layout across programs or preludes.
This guide describes the implicit type. A user declaration named Context is an ordinary declaration with its own API page; it does not define #Context.