@bradthomasbrown/machine
v1.0.0
Published
Abstract machine framework library with dubious usefulness.
Downloads
13
Readme
machine
It seems to help, at least ourselves, to have a framework and foundation with which to make machines. In our case, we are going to create an EVM execution client. This appears to be something one can make with at least a few machine types. For instance, we chose to interpret "execution contexts" as "single-context machines". We will not interpret subcontext creation and execution as recursive execution, but rather the creation of a new machine, a blocking of the old machine, and another "higher" kind of machine will orchestrate the potential creation, destruction, switching or pointing, and general control flow of single-context machines. This may be a "transaction machine". Instead of a program counter in the context of a transaction machine, it may have a "machine pointer". There may later be "block machines" and then later again a "blockchain" machine. Asynchronous machines may be helpful for GPU programming architecture and design.
This library then serves as a collection of various machine frameworks.
Examples
- 6873f5 ("an instruction-parsing-with-artifacts machine")
- EVM single-context machine
- x86_64 machine (instruction artifacts can accumulate information from REX prefixes, ModRM bytes, etc.)
- A "thread"
- a06785 ("an instruction-less machine")
- EVM single-transaction machine
- An "operating system"
Installation
npm i @bradthomasbrown/machineUsage
// NOTE: this is more a less a vague and loose guidelines on how we understand the "machine" concept and how to use it
// it seems to help us work with and build machines, but it may just be more of a "personal style" than particularly useful to anyone
import { createMachine } from "@bradthomasbrown/machine/a06785";
type Context = {
counter:number
}
function execute(context:Context) {
context.counter++;
}
function continuePredicate(context:Context) {
if (context.counter < 10) return true;
return false;
}
function initialize(context:Context) {
context.counter = 0;
}
const Machine = createMachine(execute, continuePredicate, initialize);
const machine = new Machine({ counter: 0 });
// machine at start
console.log(machine.context.counter); // 0
console.log(machine.continue()); // true
// machine can be issued step commands
for (let i = 0; i < 5; i++) machine.step();
console.log(machine.context.counter); // 5
console.log(machine.continue()); // true
// machine does nothing when it shouldn't continue, but allows step commands to be issued
for (let i = 0; i < 10; i++) machine.step();
console.log(machine.context.counter); // 10
console.log(machine.continue()); // false
// this machine has an initialization method
machine.initialize();
console.log(machine.context.counter); // 0
console.log(machine.continue()); // true
// run should be the same as stepping until it shouldn't continue
machine.run();
console.log(machine.context.counter); // 10
console.log(machine.continue()); // false
// one more step to show that nothing changes, but step is permitted
machine.step();
console.log(machine.context.counter); // 10
console.log(machine.continue()); // false