wasmoon icon indicating copy to clipboard operation
wasmoon copied to clipboard

loops do not work

Open Drago-Cuven opened this issue 4 months ago • 10 comments

any while loop or for loop seems to strangely enough completely stop JS and freeze the page until done. no errors are reported and i can't find a way around it.

for example:

local count = 1

while count <= 100 do
    print(count)
    count = count + 1
end

print("Loop finished.")

will technically work, but during the time its taking to print it all, it will completely freeze the page, any interactions with the page will go unnoticed, then when complete, everything previously done will just "catch up" all at once. i don't know how to fix this, or get around it.

here is the snippet of code that lua is run from:

async runLua({ CODE }, util) {
    if (!canRunLUA) return '';

    // Initialize Lua commands only once
    if (initWCSCMDS) {
        this.initLuaCommands(util);
        initWCSCMDS = false;
    }

    let result;
    try {

        result = await lua.doString(Cast.toString(CODE));
    } catch (err) {
        console.error(err);
        const msg   = err instanceof Error ? err.message : Cast.toString(err);
        const match = msg.match(/\[string.*?\]:(\d+)/);
        const line  = match ? parseInt(match[1], 10) : -1;
        let linemsg = '';

        if (line > 0) {
            const lines = CODE.split(/\r?\n/);
            linemsg = lines[line - 1] || '';
        }

        luaError.cur  = { msg, line, linemsg };
        luaError.last = { ...luaError.cur };

        // Clean the stack after error
        while (lua.global.getTop() > 0) lua.global.pop();

        util.startHats('DragoLua_onError');
        return '';
    }
    // Clean the stack after execution
    while (lua.global.getTop() > 0) lua.global.pop();

    // Check for invalid returns (functions instead of values)
    if (typeof result === 'function') {
        const msg = `Invalid return: received a function value instead of a result → ${Cast.toString(result)}`;
        luaError.cur  = { msg, line: -1, linemsg: '' };
        luaError.last = { ...luaError.cur };
        util.startHats('DragoLua_onError');
        return '';
    }

    // Reset current error state
    luaError.cur = { msg: '', line: 0, linemsg: '' };

    return result ?? '';

Drago-Cuven avatar Sep 04 '25 17:09 Drago-Cuven