124 lines
No EOL
4.2 KiB
Text
124 lines
No EOL
4.2 KiB
Text
;; Auto-generated by speckl-compile from KafkaKRaft
|
|
;; Compile: wat2wasm KafkaKRaft.wat -o KafkaKRaft.wasm
|
|
|
|
(module
|
|
;; Host imports for Map/Set/alloc
|
|
(import "speckl" "map_create" (func $map_create (result i32)))
|
|
(import "speckl" "map_set" (func $map_set (param i32 i32 i32)))
|
|
(import "speckl" "map_get" (func $map_get (param i32 i32) (result i32)))
|
|
(import "speckl" "map_has" (func $map_has (param i32 i32) (result i32)))
|
|
(import "speckl" "map_remove" (func $map_remove (param i32 i32)))
|
|
(import "speckl" "set_create" (func $set_create (result i32)))
|
|
(import "speckl" "set_add" (func $set_add (param i32 i32)))
|
|
(import "speckl" "set_has" (func $set_has (param i32 i32) (result i32)))
|
|
(import "speckl" "set_size" (func $set_size (param i32) (result i32)))
|
|
(import "speckl" "alloc" (func $alloc (param i32) (result i32)))
|
|
(import "speckl" "write_i32" (func $write_i32 (param i32 i32)))
|
|
(import "speckl" "write_f64" (func $write_f64 (param i32 f64)))
|
|
|
|
;; Linear memory (exported so host can read state)
|
|
(memory (export "memory") 1)
|
|
|
|
;; State globals
|
|
(global $currentTerm (mut i32) (i32.const 0))
|
|
(global $state (mut i32) (i32.const 0))
|
|
(global $votedFor (mut i32) (i32.const 0))
|
|
(global $log (mut i32) (i32.const 0))
|
|
(global $commitIndex (mut i32) (i32.const 0))
|
|
(global $nextIndex (mut i32) (i32.const 0))
|
|
(global $matchIndex (mut i32) (i32.const 0))
|
|
(global $votesGranted (mut i32) (i32.const 0))
|
|
|
|
;; ---- init ----
|
|
(func $init (export "init")
|
|
(i32.const 0)
|
|
(global.set $currentTerm)
|
|
(i32.const 0)
|
|
(global.set $state)
|
|
(i32.const 0)
|
|
(global.set $votedFor)
|
|
(i32.const 0)
|
|
(global.set $commitIndex)
|
|
(call $map_create)
|
|
(global.set $nextIndex)
|
|
(call $map_create)
|
|
(global.set $matchIndex)
|
|
(call $set_create)
|
|
(global.set $votesGranted)
|
|
)
|
|
|
|
;; ---- Actions ----
|
|
;; action: RequestVote
|
|
(func $RequestVote (export "RequestVote") (param $candidate i32) (param $lastLogTerm i32) (param $lastLogIndex i32) (result i32)
|
|
|
|
;; require: state == Candidate
|
|
;; Complex right-hand side not compiled: state == Candidate
|
|
;; require: currentTerm >= lastLogTerm
|
|
;; Complex right-hand side not compiled: currentTerm >= lastLogTerm
|
|
;; votesGranted := votesGranted union {candidate}
|
|
;; Unrecognized assignment target: votesGranted
|
|
(i32.const 1)
|
|
)
|
|
|
|
;; action: HandleRequestVoteRequest
|
|
(func $HandleRequestVoteRequest (export "HandleRequestVoteRequest") (param $candidate i32) (param $candidateTerm i32) (param $lastLogTerm i32) (param $lastLogIndex i32) (result i32)
|
|
|
|
;; require: currentTerm >= candidateTerm
|
|
;; Complex right-hand side not compiled: currentTerm >= candidateTerm
|
|
;; votedFor := Some(candidate)
|
|
(i32.const 0) ;; uncompiled expr: Some(candidate)
|
|
(global.set $votedFor)
|
|
(i32.const 1)
|
|
)
|
|
|
|
;; action: BecomeLeader
|
|
(func $BecomeLeader (export "BecomeLeader") (result i32)
|
|
|
|
;; require: state == Candidate
|
|
;; Complex right-hand side not compiled: state == Candidate
|
|
;; require: votesGranted.size() >= 3
|
|
;; Complex guard not compiled: votesGranted.size() >= 3
|
|
;; state := Leader
|
|
(i32.const 0) ;; uncompiled expr: Leader
|
|
(global.set $state)
|
|
;; commitIndex := length(log)
|
|
(i32.const 0) ;; uncompiled expr: length(log)
|
|
(global.set $commitIndex)
|
|
(i32.const 1)
|
|
)
|
|
|
|
;; action: AppendEntries
|
|
(func $AppendEntries (export "AppendEntries") (param $follower i32) (result i32)
|
|
|
|
;; require: state == Leader
|
|
;; Complex right-hand side not compiled: state == Leader
|
|
;; log := append(log, newEntry)
|
|
;; Unrecognized assignment target: log
|
|
(i32.const 1)
|
|
)
|
|
|
|
;; ---- getState ----
|
|
(func $getState (export "getState") (result i32 i32)
|
|
(i32.const 0)
|
|
(global.get $currentTerm)
|
|
(i32.store)
|
|
(i32.const 4)
|
|
(global.get $state)
|
|
(i32.store)
|
|
(i32.const 8)
|
|
(global.get $votedFor)
|
|
(i32.store)
|
|
(i32.const 12)
|
|
(global.get $commitIndex)
|
|
(i32.store)
|
|
(i32.const 0)
|
|
(i32.const 16)
|
|
)
|
|
|
|
|
|
;; checkConstraints — runtime constraint evaluation stub
|
|
(func $checkConstraints (export "checkConstraints") (result i32)
|
|
;; TODO: evaluate constraint expressions at runtime
|
|
(i32.const 1)
|
|
)
|
|
) |