From daf8788e0fcb4ac68e20276a91e352a3a7c408f7 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sun, 7 Dec 2014 16:05:58 +0100 Subject: [PATCH] Allow to change & reset time limits --- tests/set_time_limit_002.phpt | 42 +++++++++++++++++++++++++++++++++++ v8js.cc | 14 ++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 tests/set_time_limit_002.phpt diff --git a/tests/set_time_limit_002.phpt b/tests/set_time_limit_002.phpt new file mode 100644 index 0000000..63d383a --- /dev/null +++ b/tests/set_time_limit_002.phpt @@ -0,0 +1,42 @@ +--TEST-- +Test V8::setTimeLimit() : Time limit can be changed +--SKIPIF-- + +--FILE-- +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=== diff --git a/v8js.cc b/v8js.cc index de6332f..9d0f9ae 100644 --- a/v8js.cc +++ b/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 from = std::chrono::high_resolution_clock::now(); + (*it)->time_point = from + duration; + } + } + V8JSG(timer_mutex).unlock(); } /* }}} */