@@ -16,28 +16,38 @@ compiler, or `loadstring` to execute the original source.
1616- ` vm/optimize.lua ` : recursive, conservative basic-block optimizations and
1717 cleanup batching before emission.
1818- ` vm/opcodes.lua ` : semantic instruction IDs.
19- - ` vm/layout.lua ` : a per-emission opcode and operand layout .
19+ - ` vm/layout.lua ` : per-emission opcode, operand, and numeric metadata keys .
2020- ` vm/runtime.lua ` and ` vm/runtime_dispatch.lua ` : runtime templates and the
2121 generated dispatcher, configured for the selected opcode IDs and operand
2222 positions.
2323- ` vm/runtime_calls.lua ` : fixed-arity and packed call, return, and register
2424 cleanup handlers used by the generated runtime.
25- - ` vm/emit.lua ` : instruction encoding, native prototype table literals, and the
26- runtime function IIFE.
25+ - ` vm/data.lua ` : scattered table graph encoding and startup reconstruction.
26+ - ` vm/emit.lua ` : shard literals, the reconstruction helper, and the runtime
27+ function IIFE.
2728- ` vm/strings.lua ` : optional hoisting of generated interpreter string literals
2829 into an outer initializer.
2930- ` vm/flatten.lua ` : the Cff integration for generated VM interpreters.
3031
3132A prototype contains instructions, constants, child prototypes, parameter
32- metadata, and capture descriptors. The emitter passes the native prototype
33- tables as an argument to a runtime function IIFE. The generated interpreter
34- then uses that table data; the prototypes are not encoded as strings.
33+ metadata, and capture descriptors. Prototype fields use unique randomized
34+ numeric keys instead of fixed string keys. The emitter scatters assignments
35+ for the complete table graph across three to five native shard tables,
36+ including individual instruction operands and constant entries. Table IDs and
37+ record order are shuffled. A startup helper allocates all destination tables,
38+ resolves references, and passes the reconstructed root to the runtime function
39+ IIFE. It releases the consumed shards. Reconstruction happens once per chunk
40+ execution; the interpreter uses ordinary direct table lookups afterward.
41+ Scattering increases output size and startup work without adding shard lookups
42+ to the VM loop. The prototypes are not encoded as strings.
3543
3644Each invocation owns its register table; vararg functions also carry a counted
3745vararg tuple. Closures retain only
3846their referenced upvalue cells; leaving a scope detaches its cells without
39- destroying values shared by live closures. Value lists carry an explicit ` n `
40- so trailing nils survive calls, varargs, table constructors, and returns.
47+ destroying values shared by live closures. Value lists carry an explicit count
48+ under a per-emission negative numeric key, keeping it separate from positive
49+ array indices so trailing nils survive calls, varargs, table constructors, and
50+ returns.
4151The compiler keeps ordinary scalar values in individual registers. Small calls
4252and returns pass those registers directly to native Lua instead of first making
4353a value-list table. Temporary registers are cleared after consumption, and
@@ -66,22 +76,24 @@ initialization path, which also builds the vararg tuple when needed.
6676` Cff ` normally flattens control flow throughout its input AST. When it receives
6777the AST emitted by ` Vmify ` , a private ` vmRuntime ` annotation selects a special
6878path: by default, ` vm/flatten.lua ` flattens the interpreter function only. The
69- large native prototype table initialization remains outside the flattened
70- function, keeping it out of the Cff control structure and jump limits used by
71- Lua and LuaJIT.
79+ large native shard initialization and startup reconstruction remain outside
80+ the flattened function, keeping them out of the Cff control structure and jump
81+ limits used by Lua and LuaJIT.
7282
7383Set ` Cff ` 's ` ExcludeVm ` setting to ` true ` to leave both the generated
74- interpreter and the native prototype table literal out of flattening. Cff then
75- flattens the surrounding code, including helpers introduced by intervening
76- steps such as string decoders or tamper checks. The interpreter remains native
77- Lua, so its lexical captures continue to share variables with surrounding
84+ interpreter and the native shard literal out of flattening. Cff then flattens
85+ the startup reconstruction helper and its call along with surrounding code,
86+ including helpers introduced by intervening steps such as string decoders or
87+ tamper checks. The interpreter remains native Lua, so its lexical captures
88+ continue to share variables with surrounding
7889closures according to ordinary Lua upvalue semantics.
7990
8091` cff/native.lua ` emits the excluded expressions directly and bridges their
8192external locals through Cff's shared upvalue cells. Garbage-collected capture
8293proxies keep those cells alive for native closures; captures are not value
83- snapshots. The ` vmData ` annotation identifies the entire prototype initializer,
84- including its child prototypes.
94+ snapshots. The ` vmData ` annotation identifies only the shard literal containing
95+ the complete prototype graph. It does not exclude the reconstruction helper
96+ or its invocation from Cff.
8597
8698With the default ` ExcludeVm = false ` behavior, the special path passes
8799read-only external lexical helpers through function arguments. This supports
@@ -94,8 +106,9 @@ arity and cleanup instructions use the same ID scheme. At emission,
94106` layout.lua ` chooses a permutation of the opcode IDs and a permutation of
95107operand slots 2 through 5; slot 1 always contains the opcode. The emitter
96108encodes each instruction with that layout without changing the compiler
97- prototypes. One layout is shared by the root prototype and all child
98- prototypes in the emitted chunk.
109+ prototypes. The layout also selects numeric prototype field keys and the
110+ packed-value count key. One layout is shared by the root prototype and all
111+ child prototypes in the emitted chunk.
99112
100113Before emission, ` optimize.lua ` visits the root prototype and its children and
101114applies conservative basic-block-local rewrites. At emission,
@@ -111,15 +124,17 @@ the generated interpreter's literal strings into an outer initializer, where
111124normal string encryption and later ` ConstantArray ` processing apply. The
112125initializer decodes them once at startup and binds them to native lexical
113126locals used by the interpreter, avoiding decoder or wrapper calls on hot
114- paths. Strings in ` vmData ` prototype tables remain encrypted by the normal
127+ paths. Strings in the ` vmData ` shards remain encrypted by the normal
115128` EncryptStrings ` behavior. ` vm/strings.lua ` implements this transformation.
116129
117130Medium then applies ` AntiTamper ` , ` ConstantArray ` , and ` WrapInFunction ` ,
118- followed by ` Cff ` with ` ExcludeVm = true ` . The interpreter and prototype table
119- initializers stay native while Cff flattens the surrounding code and generated
120- helpers. ` NumbersToExpressions ` runs after Cff: it adds no helper functions,
121- and this ordering prevents Cff from virtualizing arithmetic constant
122- expressions.
131+ followed by ` Cff ` with ` ExcludeVm = true ` . The interpreter and shard literal
132+ stay native while Cff protects startup reconstruction, surrounding code, and
133+ generated helpers. After generating its helpers, Cff puts repeated string
134+ literals in a shared outer table, including any new diagnostics and counted-value
135+ keys. Each value is embedded only once in Medium output. ` NumbersToExpressions `
136+ runs after Cff: it adds no helper functions, and this ordering prevents Cff
137+ from virtualizing arithmetic constant expressions.
123138
124139` Strong ` applies one ` Vmify ` pass followed by ` EncryptStrings ` , ` AntiTamper ` ,
125140and one default ` Cff ` pass over the generated VM interpreter. It retains the
0 commit comments