Lack of randomness sometimes when compiling for Glulx

Here is an evil method to replace the system random() function in Ver 10.

It relies on the fact that one of the few remaining ways to directly inject I6 code into the final I6 source (auto.inf) is with the Include (- ... -) when defining <a kind> | <an object> syntax.

The insertion parasitises the compiler’s random-kind class declaration header to inject a Replace random; statement, then declares a dummy-kind class declaration header to absorb the orphaned remainder of the compiler’s random-kind class declaration:

A random-kind is a kind of object.

Include (-; 

Replace random; 
	
Class dummy_kind 
	class K0_kind
-) when defining a random-kind.

which leads to this in the final I6:

Class K16_random_kind
  class K0_kind
; 

Replace random; 
	
Class dummy_kind 
	class K0_kind
    with plural bc_U167
    with short_name bc_U168
    with article bc_U169
    with list_together bc_U170
;

This works because class declarations come early enough in the final I6 source that no code calling random() (which would otherwise compile the system random() function) has yet been compiled. The I6 compiler is therefore happy to accept the directive to replace the system random() function with our own.

EDIT Evil scientists among you will immediately notice that this provides a method to directly inject ANY undigested code into the final I6 source (auto.inf), bypassing the I6->Inter->I6 compilation process that spits out some otherwise valid I6 code, either because

i) it isn’t supported by the I6->Inter compiler, but is supported by the final I6->story compiler
ii) it has dependencies on things only created late in the I6->Inter->I6 compilation chain

4 Likes