mirror of
https://github.com/phpv8/v8js.git
synced 2025-01-03 16:51:52 +00:00
Allow to change & reset time limits
This commit is contained in:
parent
ab6df6f14f
commit
daf8788e0f
42
tests/set_time_limit_002.phpt
Normal file
42
tests/set_time_limit_002.phpt
Normal 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
14
v8js.cc
@ -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();
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user