All files / js gstate.js

59.39% Statements 98/165
31.25% Branches 10/32
66.66% Functions 8/12
59.39% Lines 98/165

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 16673x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 59612x 59612x 59612x 59612x   59612x 73x       73x       73x 65792x 65792x 65792x 65792x 72x 72x 72x 72x 72x 72x 72x 72x 72x 65792x 65792x 73x                         73x 73x 14426x 14426x 14426x 14426x     14426x 14426x 14426x 14426x             14426x 14426x 73x 73x 14426x 14426x 14426x 14426x     14426x 14426x 14426x 14426x 14426x             14426x 73x 73x 6180x 6180x 6180x 6180x 6180x 6180x 6180x 6180x 73x 73x 73x 15380x 15380x 15380x 15380x           15380x 15380x 15380x 15380x 15380x 15380x 15380x           15380x 15380x 73x 73x 15380x 15380x 15380x 15380x 15380x           15380x           15380x           15380x 15380x 73x 73x                
// gstate.js — global game state reference and execution/origin guard state.
// C uses global `u`, `level`, `flags`, `svm` — a single well-known state reference.
// JS modules read gstate.game.u, gstate.game.map, gstate.game.display, etc.
 
import { getEnv, writeStderr } from './runtime_env.js';
 
export let game = null;
export function setGame(g) { game = g; }
 
const stateByGame = new WeakMap();
 
function modeValue() {
    const raw = String(getEnv('WEBHACK_STRICT_SINGLE_THREAD', '') || '').trim().toLowerCase();
    if (!raw || raw === '0' || raw === 'off' || raw === 'false') return 'off';
    if (raw === 'warn' || raw === '2') return 'warn';
    return 'strict';
}
 
function shouldWarn(mode) {
    return mode === 'warn' || mode === 'strict';
}
 
function shouldThrow(mode) {
    return mode === 'strict';
}
 
function gameState(g) {
    if (!g || typeof g !== 'object') return null;
    let st = stateByGame.get(g);
    if (!st) {
        st = {
            seq: 0,
            activeToken: null,
            depth: 0,
            activeOrigin: null,
            originSeq: 0,
        };
        stateByGame.set(g, st);
    }
    return st;
}
 
function report(g, mode, kind, details = {}) {
    const payload = { kind, ...details };
    if (typeof g?.emitDiagnosticEvent === 'function') {
        g.emitDiagnosticEvent('synclock.guard', payload);
    }
    if (shouldWarn(mode)) {
        writeStderr(`[SYNCLOCK] ${kind} ${JSON.stringify(payload)}\n`);
    }
    if (shouldThrow(mode)) {
        throw new Error(`SYNCLOCK ${kind}`);
    }
}
 
export function beginCommandExec(g, details = {}) {
    const mode = modeValue();
    const st = gameState(g);
    if (!st) return null;
    if (st.depth > 0) {
        report(g, mode, 'nested-command', details);
    }
    st.depth += 1;
    st.seq += 1;
    st.activeToken = st.seq;
    if (typeof g?.emitDiagnosticEvent === 'function') {
        g.emitDiagnosticEvent('synclock.command.begin', {
            token: st.activeToken,
            depth: st.depth,
            ...details,
        });
    }
    return st.activeToken;
}
 
export function endCommandExec(g, token, details = {}) {
    const mode = modeValue();
    const st = gameState(g);
    if (!st) return;
    if (token !== st.activeToken) {
        report(g, mode, 'end-token-mismatch', { expected: st.activeToken, got: token, ...details });
    }
    st.depth = Math.max(0, st.depth - 1);
    if (st.depth === 0) {
        st.activeToken = null;
    }
    if (typeof g?.emitDiagnosticEvent === 'function') {
        g.emitDiagnosticEvent('synclock.command.end', {
            token,
            depth: st.depth,
            ...details,
        });
    }
}
 
export function getCommandExecState(g) {
    const st = gameState(g);
    if (!st) return { activeToken: null, depth: 0, seq: 0 };
    return {
        activeToken: st.activeToken,
        depth: st.depth,
        seq: st.seq,
    };
}
 
// New origin registration API used by canonical async origins.
export function beginOriginAwait(g, type) {
    const mode = modeValue();
    const st = gameState(g);
    if (!st) return null;
    if (st.activeOrigin) {
        report(g, mode, 'nested-origin-await', {
            existing: st.activeOrigin,
            attempted: type,
        });
    }
    st.originSeq += 1;
    const token = st.originSeq;
    st.activeOrigin = {
        token,
        type: String(type || 'origin'),
    };
    if (typeof g?.emitDiagnosticEvent === 'function') {
        g.emitDiagnosticEvent('synclock.origin.begin', {
            token,
            type: st.activeOrigin.type,
        });
    }
    return { token, type: st.activeOrigin.type };
}
 
export function endOriginAwait(g, snapshot) {
    if (!snapshot) return;
    const mode = modeValue();
    const st = gameState(g);
    if (!st) return;
    if (!st.activeOrigin) {
        report(g, mode, 'origin-end-without-active', {
            got: snapshot,
        });
        return;
    }
    if (snapshot.token !== st.activeOrigin.token) {
        report(g, mode, 'origin-token-mismatch', {
            expected: st.activeOrigin,
            got: snapshot,
        });
    }
    if (typeof g?.emitDiagnosticEvent === 'function') {
        g.emitDiagnosticEvent('synclock.origin.end', {
            token: snapshot.token,
            type: snapshot.type,
        });
    }
    st.activeOrigin = null;
}
 
export function getOriginAwaitState(g) {
    const st = gameState(g);
    if (!st || !st.activeOrigin) return null;
    return {
        token: st.activeOrigin.token,
        type: st.activeOrigin.type,
    };
}