如何在Lua Extension for PHP中处理无限循环?

I use the Lua Extension with PHP 7.1. Unfortunately, I did not found a really documentation. Only this. I know already that I can use lua sandbox for restricting the access to lua functions.

But how can i restrict the lua duration time? I want to abort lua parsing after x seconds or after x calculation cycles or x lines of code.

If I parse something like this (endless loop):

<?php
try {
    $lua = new Lua();
    $lua->eval("
        while 1 do
            -- something
        end
    ");
} catch (Exception $e) {
    $e->getMessage();
}?>

the php script runs forever.

It's not possible to override while loops like you want, but you could make your own function (say, While) that takes a function and uses the count method. Something like

local limit = 500

function While( condition, dofunc )
    local count = 0
    repeat
        count = count + 1
        if count > limit then
            print( 'Aborting loop: limit (' .. limit .. ') reached.' )
            break
        end
        dofunc()
        var = condition()
    until not var
end

And some example usage.

local i = 1
local tab = { 'a', 's', 'd', 'f' }
While( function() return tab[i] end, function() 
    print( tab[i] )
    i = i + 1
end )
-- Prints 'a', 's', 'd', 'f'

While( function() return true end, function() print( 'test' ) end )
-- Prints 'test' 500 times, then quits