Skip to main content

stripkshdef

Function stripkshdef 

Source
pub fn stripkshdef(prog: Option<Box<eprog>>, name: &str) -> Option<Box<eprog>>
Expand description

Port of Eprog stripkshdef(Eprog prog, char *name) from Src/exec.c:6286-6364. Given an Eprog read from an autoload file plus the function name being defined, check whether the file consists of exactly one function NAME { … } definition for that name. If so, return a new Eprog whose prog/strs/ pats slice out just the function body (so calling code can invoke the body directly instead of re-parsing). Otherwise return the input untouched.

Header word layout consumed (matches C pc[…] reads): pc[0] = WC_LIST with Z_SYNC|Z_END|Z_SIMPLE flags pc[1] = (sublist header, skipped) pc[2] = WC_FUNCDEF pc[3] = 1 (single-name funcdef) pc[4] = name-string slot (compared to name) pc[5] = sbeg (offset into strs table) pc[6] = nstrs (bytes of strs to copy) pc[7] = npats (number of pattern slots to allocate) pc[8] = WC_FUNCDEF_SKIP target (end-of-funcdef pc) pc[9] = (unused header word — pc += 6 lands here as the start of the body wordcode stream)

Returns None only when the input was None (matches C return NULL). Equivalence between the original prog and a successfully stripped prog is not preserved at the pointer level (C may return the original Eprog when the file fails the single-funcdef shape check; this Rust port does the same by passing the box back through).

EF_MAP (zcompiled / mmap’d Eprog) path: C mutates the existing Eprog in place, swapping its prog / strs / pats to slice into the funcdef body. Rust mirrors this on the moved-in Box<eprog> (no separate free() needed — Vec drop handles the old pats).