0
0
mirror of https://github.com/phpv8/v8js.git synced 2024-11-08 12:38:41 +00:00

make memory_limit a size_t internally

This commit is contained in:
Stefan Siegl 2017-03-11 14:10:49 +01:00
parent 51335bfa17
commit f02b44b3f8
6 changed files with 29 additions and 11 deletions

View File

@ -654,7 +654,7 @@ static void v8js_compile_script(zval *this_ptr, const zend_string *str, const ze
return; return;
} }
static void v8js_execute_script(zval *this_ptr, v8js_script *res, long flags, long time_limit, long memory_limit, zval **return_value) static void v8js_execute_script(zval *this_ptr, v8js_script *res, long flags, long time_limit, size_t memory_limit, zval **return_value)
{ {
v8js_ctx *c = Z_V8JS_CTX_OBJ_P(this_ptr); v8js_ctx *c = Z_V8JS_CTX_OBJ_P(this_ptr);
@ -702,13 +702,19 @@ static PHP_METHOD(V8Js, executeString)
return; return;
} }
if (memory_limit < 0) {
zend_throw_exception(php_ce_v8js_exception,
"memory_limit must not be negative", 0);
return;
}
v8js_compile_script(getThis(), str, identifier, &res); v8js_compile_script(getThis(), str, identifier, &res);
if (!res) { if (!res) {
RETURN_FALSE; RETURN_FALSE;
} }
zend_try { zend_try {
v8js_execute_script(getThis(), res, flags, time_limit, memory_limit, &return_value); v8js_execute_script(getThis(), res, flags, time_limit, static_cast<size_t>(memory_limit), &return_value);
v8js_script_free(res); v8js_script_free(res);
} }
zend_catch { zend_catch {
@ -757,11 +763,17 @@ static PHP_METHOD(V8Js, executeScript)
return; return;
} }
if (memory_limit < 0) {
zend_throw_exception(php_ce_v8js_exception,
"memory_limit must not be negative", 0);
return;
}
if((res = (v8js_script *)zend_fetch_resource(Z_RES_P(zscript), PHP_V8JS_SCRIPT_RES_NAME, le_v8js_script)) == NULL) { if((res = (v8js_script *)zend_fetch_resource(Z_RES_P(zscript), PHP_V8JS_SCRIPT_RES_NAME, le_v8js_script)) == NULL) {
RETURN_FALSE; RETURN_FALSE;
} }
v8js_execute_script(getThis(), res, flags, time_limit, memory_limit, &return_value); v8js_execute_script(getThis(), res, flags, time_limit, static_cast<size_t>(memory_limit), &return_value);
} }
/* }}} */ /* }}} */
@ -907,14 +919,20 @@ static PHP_METHOD(V8Js, setMemoryLimit)
return; return;
} }
if (memory_limit < 0) {
zend_throw_exception(php_ce_v8js_exception,
"memory_limit must not be negative", 0);
return;
}
c = Z_V8JS_CTX_OBJ_P(getThis()); c = Z_V8JS_CTX_OBJ_P(getThis());
c->memory_limit = memory_limit; c->memory_limit = static_cast<size_t>(memory_limit);
V8JSG(timer_mutex).lock(); V8JSG(timer_mutex).lock();
for (std::deque< v8js_timer_ctx* >::iterator it = V8JSG(timer_stack).begin(); for (std::deque< v8js_timer_ctx* >::iterator it = V8JSG(timer_stack).begin();
it != V8JSG(timer_stack).end(); it ++) { it != V8JSG(timer_stack).end(); it ++) {
if((*it)->ctx == c && !(*it)->killed) { if((*it)->ctx == c && !(*it)->killed) {
(*it)->memory_limit = memory_limit; (*it)->memory_limit = static_cast<size_t>(memory_limit);
} }
} }
V8JSG(timer_mutex).unlock(); V8JSG(timer_mutex).unlock();

View File

@ -44,7 +44,7 @@ struct v8js_ctx {
long time_limit; long time_limit;
bool time_limit_hit; bool time_limit_hit;
long memory_limit; size_t memory_limit;
bool memory_limit_hit; bool memory_limit_hit;
long average_object_size; long average_object_size;

View File

@ -124,7 +124,7 @@ void v8js_timer_thread(zend_v8js_globals *globals) /* {{{ */
/* }}} */ /* }}} */
void v8js_timer_push(long time_limit, long memory_limit, v8js_ctx *c) /* {{{ */ void v8js_timer_push(long time_limit, size_t memory_limit, v8js_ctx *c) /* {{{ */
{ {
V8JSG(timer_mutex).lock(); V8JSG(timer_mutex).lock();

View File

@ -18,14 +18,14 @@
struct v8js_timer_ctx struct v8js_timer_ctx
{ {
long time_limit; long time_limit;
long memory_limit; size_t memory_limit;
std::chrono::time_point<std::chrono::high_resolution_clock> time_point; std::chrono::time_point<std::chrono::high_resolution_clock> time_point;
v8js_ctx *ctx; v8js_ctx *ctx;
bool killed; bool killed;
}; };
void v8js_timer_thread(zend_v8js_globals *globals); void v8js_timer_thread(zend_v8js_globals *globals);
void v8js_timer_push(long time_limit, long memory_limit, v8js_ctx *c); void v8js_timer_push(long time_limit, size_t memory_limit, v8js_ctx *c);
#endif /* V8JS_TIMER_H */ #endif /* V8JS_TIMER_H */

View File

@ -95,7 +95,7 @@ void v8js_v8_init() /* {{{ */
* heap allocated memory). * heap allocated memory).
*/ */
void v8js_v8_call(v8js_ctx *c, zval **return_value, void v8js_v8_call(v8js_ctx *c, zval **return_value,
long flags, long time_limit, long memory_limit, long flags, long time_limit, size_t memory_limit,
std::function< v8::Local<v8::Value>(v8::Isolate *) >& v8_call) /* {{{ */ std::function< v8::Local<v8::Value>(v8::Isolate *) >& v8_call) /* {{{ */
{ {
char *tz = NULL; char *tz = NULL;

View File

@ -53,7 +53,7 @@ static inline const char * ToCString(const v8::String::Utf8Value &value) /* {{{
void v8js_v8_init(); void v8js_v8_init();
void v8js_v8_call(v8js_ctx *c, zval **return_value, void v8js_v8_call(v8js_ctx *c, zval **return_value,
long flags, long time_limit, long memory_limit, long flags, long time_limit, size_t memory_limit,
std::function< v8::Local<v8::Value>(v8::Isolate *) >& v8_call); std::function< v8::Local<v8::Value>(v8::Isolate *) >& v8_call);
void v8js_terminate_execution(v8::Isolate *isolate); void v8js_terminate_execution(v8::Isolate *isolate);