0
0
mirror of https://github.com/phpv8/v8js.git synced 2025-01-03 15:41:55 +00:00

Allow to change & reset time limits

This commit is contained in:
Stefan Siegl 2014-12-07 16:05:58 +01:00
parent ab6df6f14f
commit daf8788e0f
2 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,42 @@
--TEST--
Test V8::setTimeLimit() : Time limit can be changed
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php
$JS = <<< EOT
var jsfunc = function() {
PHP.incrTimeLimit();
var text = "abcdefghijklmnopqrstuvwyxz0123456789";
for (var i = 0; i < 10000000; ++i) {
var encoded = encodeURI(text);
}
};
jsfunc;
EOT;
$v8 = new V8Js();
$v8->setTimeLimit(10);
$v8->incrTimeLimit = function() use ($v8) {
$v8->setTimeLimit(100);
};
$func = $v8->executeString($JS);
var_dump($func);
try {
$func();
} catch (V8JsTimeLimitException $e) {
print get_class($e); print PHP_EOL;
print $e->getMessage(); print PHP_EOL;
}
?>
===EOF===
--EXPECTF--
object(V8Function)#%d (0) {
}
V8JsTimeLimitException
Script time limit of 100 milliseconds exceeded
===EOF===

14
v8js.cc
View File

@ -1545,6 +1545,20 @@ static PHP_METHOD(V8Js, setTimeLimit)
c = (php_v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC);
c->time_limit = time_limit;
V8JSG(timer_mutex).lock();
for (std::deque< php_v8js_timer_ctx* >::iterator it = V8JSG(timer_stack).begin();
it != V8JSG(timer_stack).end(); it ++) {
if((*it)->v8js_ctx == c && !(*it)->killed) {
(*it)->time_limit = time_limit;
// Calculate the time point when the time limit is exceeded
std::chrono::milliseconds duration(time_limit);
std::chrono::time_point<std::chrono::high_resolution_clock> from = std::chrono::high_resolution_clock::now();
(*it)->time_point = from + duration;
}
}
V8JSG(timer_mutex).unlock();
}
/* }}} */