0
0
mirror of https://github.com/phpv8/v8js.git synced 2024-12-23 03:51:51 +00:00

Merge pull request #193 from stesie/fix-cpp-functional-memleak

defer bailout until std::function dtor
This commit is contained in:
Stefan Siegl 2016-01-01 20:50:41 +01:00
commit 5fd9f3d000
3 changed files with 153 additions and 134 deletions

View File

@ -529,6 +529,9 @@ static void v8js_execute_script(zval *this_ptr, v8js_script *res, long flags, lo
memory_limit = c->memory_limit; memory_limit = c->memory_limit;
} }
/* std::function relies on its dtor to be executed, otherwise it leaks
* some memory on bailout. */
{
std::function< v8::Local<v8::Value>(v8::Isolate *) > v8_call = [res](v8::Isolate *isolate) { std::function< v8::Local<v8::Value>(v8::Isolate *) > v8_call = [res](v8::Isolate *isolate) {
v8::Local<v8::Script> script = v8::Local<v8::Script>::New(isolate, *res->script); v8::Local<v8::Script> script = v8::Local<v8::Script>::New(isolate, *res->script);
return script->Run(); return script->Run();
@ -537,6 +540,13 @@ static void v8js_execute_script(zval *this_ptr, v8js_script *res, long flags, lo
v8js_v8_call(c, return_value, flags, time_limit, memory_limit, v8_call TSRMLS_CC); v8js_v8_call(c, return_value, flags, time_limit, memory_limit, v8_call TSRMLS_CC);
} }
if(V8JSG(fatal_error_abort)) {
/* Check for fatal error marker possibly set by v8js_error_handler; just
* rethrow the error since we're now out of V8. */
zend_bailout();
}
}
/* {{{ proto mixed V8Js::executeString(string script [, string identifier [, int flags]]) /* {{{ proto mixed V8Js::executeString(string script [, string identifier [, int flags]])
*/ */
static PHP_METHOD(V8Js, executeString) static PHP_METHOD(V8Js, executeString)

View File

@ -76,13 +76,19 @@ void v8js_v8_init(TSRMLS_D) /* {{{ */
/* }}} */ /* }}} */
/**
* Prepare V8 call trampoline with time & memory limit, exception handling, etc.
*
* The caller MUST check V8JSG(fatal_error_abort) and trigger further bailout
* either immediately after this function returns (or possibly after freeing
* 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, long memory_limit,
std::function< v8::Local<v8::Value>(v8::Isolate *) >& v8_call TSRMLS_DC) /* {{{ */ std::function< v8::Local<v8::Value>(v8::Isolate *) >& v8_call TSRMLS_DC) /* {{{ */
{ {
char *tz = NULL; char *tz = NULL;
{
V8JS_CTX_PROLOGUE(c); V8JS_CTX_PROLOGUE(c);
V8JSG(timer_mutex).lock(); V8JSG(timer_mutex).lock();
@ -197,13 +203,6 @@ void v8js_v8_call(v8js_ctx *c, zval **return_value,
v8js_to_zval(result, *return_value, flags, c->isolate TSRMLS_CC); v8js_to_zval(result, *return_value, flags, c->isolate TSRMLS_CC);
} }
} }
} /* /V8JS_CTX_PROLOGUE */
if(V8JSG(fatal_error_abort)) {
/* Check for fatal error marker possibly set by v8js_error_handler; just
* rethrow the error since we're now out of V8. */
zend_bailout();
}
} }
/* }}} */ /* }}} */

View File

@ -281,6 +281,9 @@ static int v8js_v8object_call_method(zend_string *method, zend_object *object, I
zend_get_parameters_array_ex(argc, argv); zend_get_parameters_array_ex(argc, argv);
} }
/* std::function relies on its dtor to be executed, otherwise it leaks
* some memory on bailout. */
{
std::function< v8::Local<v8::Value>(v8::Isolate *) > v8_call = [obj, method, argc, argv TSRMLS_CC](v8::Isolate *isolate) { std::function< v8::Local<v8::Value>(v8::Isolate *) > v8_call = [obj, method, argc, argv TSRMLS_CC](v8::Isolate *isolate) {
int i = 0; int i = 0;
@ -316,11 +319,18 @@ static int v8js_v8object_call_method(zend_string *method, zend_object *object, I
}; };
v8js_v8_call(obj->ctx, &return_value, obj->flags, obj->ctx->time_limit, obj->ctx->memory_limit, v8_call TSRMLS_CC); v8js_v8_call(obj->ctx, &return_value, obj->flags, obj->ctx->time_limit, obj->ctx->memory_limit, v8_call TSRMLS_CC);
}
if (argc > 0) { if (argc > 0) {
efree(argv); efree(argv);
} }
if(V8JSG(fatal_error_abort)) {
/* Check for fatal error marker possibly set by v8js_error_handler; just
* rethrow the error since we're now out of V8. */
zend_bailout();
}
return SUCCESS; return SUCCESS;
} }
/* }}} */ /* }}} */