« LuaSandbox::registerLibrary
LuaSandbox::setMemoryLimit »

LuaSandbox::setCPULimit

(PECL luasandbox >= 1.0.0)

LuaSandbox::setCPULimit — Set the CPU time limit for the Lua environment

説明

public function LuaSandbox::setCPULimit(float|bool $limit): void

Sets the CPU time limit for the Lua environment.

If the total user and system time used by the environment after the call to this method exceeds this limit, a LuaSandboxTimeoutError exception is thrown.

Time used in PHP callbacks is included in the limit.

Setting the time limit from a callback while Lua is running causes the timer to be reset, or started if it was not already running.

注意:

On Windows, the CPU limit will be ignored. On operating systems that do not support CLOCK_THREAD_CPUTIME_ID, such as FreeBSD and Mac OS X, wall-clock time rather than CPU time will be limited.

パラメータ

limit

Limit as a float in seconds, or false for no limit.

戻り値

値を返しません。

例

例1 Calling a Lua function

<?php

// create a new LuaSandbox
$sandbox = new LuaSandbox();

// set a time limit
$sandbox->setCPULimit( 2 );

// Run Lua code
$sandbox->loadString( 'while true do end' )->call();

?>

上の例の出力は、 たとえば以下のようになります。

PHP Fatal error:  Uncaught LuaSandboxTimeoutError: The maximum execution time for this script was exceeded

参考

関連キーワード:
LuaSandbox   environment   limit   time limit   time   setCPULimit   sandbox   setMemoryLimit   float   used   call   LuaSandboxTimeoutError   while   running   registerLibrary   luasandbox   Sets   total   user   system   after   method   exceeds   exception   thrown
関連する関数・ページ:
luasandbox.setmemorylimit   luasandbox.getcpuusage   luasandbox.getmemoryusage   luasandbox.getpeakmemoryusage   book.luasandbox   class.luasandbox