result), an optional..."> result), an optional..."> result), an optional..."> result), an optional...">

CFunction

← Reference
Io

C implementation of the CFunction primitive. An IoCFunctionData holds a C function pointer (IoUserFunction: target/locals/message -> result), an optional typeTag gate (only activate if target's tag matches, else raise a typed error), a uniqueName symbol for introspection, and profiler timing. Like Block, CFunction is self-activating: the tag's activateFunc is IoCFunction_activate, so fetching a CFunction from an activatable slot calls through immediately. This is how every C-backed Io method (List append, Number +, Object if/while/for, etc.) is installed — IoObject_addMethodTable_ creates one CFunction per entry and stores it in the target proto. CFunctions do not pre-evaluate their arguments; the recipient uses IoMessage_locals_valueArgAt_ or the pre-eval fast path (IoState_preEvalArgAt_) to fetch them lazily.

IoCFunction_activate(self, target, locals, m, slotContext)

Tag dispatch entry point: validates that the target's tag matches the CFunction's optional typeTag (raising a VM error on mismatch) then invokes the stored C function pointer with (target, locals, message). Unlike IoBlock_activate there is no fresh locals object — the C function inspects the caller's locals and the message directly, which is why CFunctions can be cheap. The commented-out retain-pool pair is a historical concern; pools are managed by callers today.

IoCFunction_activateWithProfiler(self, target, locals, m, slotContext)

Profiler-enabled wrapper around IoCFunction_activate. setProfilerOn(true) swaps this in as the tag's activateFunc so every call accumulates elapsed clock() time into the CFunction's profilerTime.

IoCFunction_free(self)

Registered as the tag's freeFunc. Frees the IoCFunctionData payload. The uniqueName symbol is interned on state and GC-managed.

IoCFunction_mark(self)

Registered as the tag's markFunc. Only the uniqueName symbol is a GC root — the C function pointer and typeTag are not IoObjects.

IoCFunction_newTag(state)

Builds the CFunction tag and wires clone/mark/free plus — crucially — activateFunc, which is what makes CFunctions self-executing when looked up through a slot.

IoCFunction_newWithFunctionPointer_tag_name_(state, func, typeTag, funcName)

Primary constructor used by IoObject_addMethodTable_: clones the CFunction proto and stamps in the C function pointer, the optional type gate (tag), and the uniqueName symbol for introspection. Every entry in a C method table produces one of these.

IoCFunction_print(self)

Debug printer dumping pointer, C function pointer, owning type name, and uniqueName. Called from diagnostic paths only; not part of the Io-visible print protocol.

IoCFunction_proto(state)

Creates the CFunction proto with a zeroed IoCFunctionData whose func points at IoObject_self (the identity function) as a safe default. The proto is not marked activatable — only its clones are, so the proto itself can be fetched via getSlot. IoCFunction_protoFinish adds the method table later once prerequisite protos exist.

IoCFunction_protoFinish(state)

Deferred init: installs the Io-visible method table on the CFunction proto after the Symbol/Sequence protos exist (method registration needs IOSYMBOL to work). Called late in IoState_init so earlier proto construction can use CFunctions without forcing a circular init order.

IoCFunction_rawClone(proto)

Registered as the tag's cloneFunc. Copies the proto's data block and flips isActivatable on — this is the point at which a CFunction becomes self-executing when placed in a slot.