A simple stack based Bytecode Interpreter / VM for Unity and C#
All in one file, but includes plenty comments on how things work. This isn't a fully fledged framework but it should give you a solid base to play around with bytecode machines.
- Simple function call ( Hello World )
- Setting bytes to stack
- Simple function with two parameters ( Add )
- Printing a float using 4 bytes ( either using stack or without )
- Wait ( Pauses execution for a while )
- Repeat ( Jumps to first "code line" and clears the stack )
vm.Load(new byte[] {
// Print Hello World
(byte)VM.INST.PRINT_HELLO_WORLD,
// Add(a,b) => push result,
(byte)VM.INST.SET, (byte)2, // A
(byte)VM.INST.SET, (byte)1, // B
(byte)VM.INST.ADD,
// Print float from 4 bytes (1234567 or 0x4996b438 or 49 96 b4 38)
(byte)VM.INST.SET, 0x49,
(byte)VM.INST.SET, 0x96,
(byte)VM.INST.SET, 0xb4,
(byte)VM.INST.SET, 0x38,
(byte)VM.INST.PRINT_FLOAT,
// Print float from 4 bytes WITHOUT USING STACK
(byte)VM.INST.PRINT_FLOAT_NO_STACK, 0x49, 0x96, 0xb4, 0x38,
// Wait using result from Add(a,b) since it's still in the stack
(byte)VM.INST.WAIT,
// Repeat
(byte)VM.INST.REPEAT,
});
vm.Process(Time.time);This project is for all the poor souls who're looking for a simple scripting solution for Unity. Take it as an inspiration.
Brought to you with ❤ by UnityList