Browse Source

Allow to change & reset time limits

Stefan Siegl 10 years ago
parent
commit
daf8788e0f
2 changed files with 56 additions and 0 deletions
  1. 42 0
      tests/set_time_limit_002.phpt
  2. 14 0
      v8js.cc

+ 42 - 0
tests/set_time_limit_002.phpt

@@ -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 - 0
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();
 }
 /* }}} */