mirror of
https://github.com/phpv8/v8js.git
synced 2024-12-23 00:51:51 +00:00
Merge branch 'php7' of https://github.com/phpv8/v8js into php8
This commit is contained in:
commit
748310c894
30
tests/issue_472_basic.phpt
Normal file
30
tests/issue_472_basic.phpt
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
--TEST--
|
||||||
|
Test V8::executeString() : Issue #472 Destroy V8Js object which V8 isolate entered
|
||||||
|
--SKIPIF--
|
||||||
|
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
|
||||||
|
--FILE--
|
||||||
|
<?php
|
||||||
|
class myjs extends \V8Js
|
||||||
|
{
|
||||||
|
public function bosh()
|
||||||
|
{
|
||||||
|
$GLOBALS['v8test'] = null;
|
||||||
|
unset($GLOBALS['v8test']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$GLOBALS['v8test'] = new myjs('myjs');
|
||||||
|
$ret = $GLOBALS['v8test']->executeString('
|
||||||
|
(() => {
|
||||||
|
myjs.bosh()
|
||||||
|
})
|
||||||
|
');
|
||||||
|
|
||||||
|
$ret();
|
||||||
|
var_dump($ret);
|
||||||
|
?>
|
||||||
|
===EOF===
|
||||||
|
--EXPECTF--
|
||||||
|
object(V8Function)#%d (0) {
|
||||||
|
}
|
||||||
|
===EOF===
|
@ -83,6 +83,10 @@ static inline struct v8js_ctx *v8js_ctx_fetch_object(zend_object *obj) {
|
|||||||
return (struct v8js_ctx *)((char *)obj - XtOffsetOf(struct v8js_ctx, std));
|
return (struct v8js_ctx *)((char *)obj - XtOffsetOf(struct v8js_ctx, std));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline zend_object *v8js_ctx_to_zend_object(struct v8js_ctx *ctx) {
|
||||||
|
return (zend_object *)((char *)ctx + XtOffsetOf(struct v8js_ctx, std));
|
||||||
|
}
|
||||||
|
|
||||||
#define Z_V8JS_CTX_OBJ_P(zv) v8js_ctx_fetch_object(Z_OBJ_P(zv));
|
#define Z_V8JS_CTX_OBJ_P(zv) v8js_ctx_fetch_object(Z_OBJ_P(zv));
|
||||||
#define Z_V8JS_CTX_OBJ(zv) v8js_ctx_fetch_object(zv);
|
#define Z_V8JS_CTX_OBJ(zv) v8js_ctx_fetch_object(zv);
|
||||||
|
|
||||||
|
16
v8js_v8.cc
16
v8js_v8.cc
@ -120,6 +120,13 @@ void v8js_v8_call(v8js_ctx *c, zval **return_value,
|
|||||||
{
|
{
|
||||||
char *tz = NULL;
|
char *tz = NULL;
|
||||||
|
|
||||||
|
// hold extra reference on v8 instance as long as we call into V8 (issue #472)
|
||||||
|
zend_object *obj = v8js_ctx_to_zend_object(c);
|
||||||
|
zval zv_v8inst;
|
||||||
|
ZVAL_OBJ(&zv_v8inst, obj);
|
||||||
|
Z_ADDREF_P(&zv_v8inst);
|
||||||
|
|
||||||
|
{
|
||||||
V8JS_CTX_PROLOGUE(c);
|
V8JS_CTX_PROLOGUE(c);
|
||||||
|
|
||||||
V8JSG(timer_mutex).lock();
|
V8JSG(timer_mutex).lock();
|
||||||
@ -184,6 +191,7 @@ void v8js_v8_call(v8js_ctx *c, zval **return_value,
|
|||||||
// Execution has been terminated due to time limit
|
// Execution has been terminated due to time limit
|
||||||
sprintf(exception_string, "Script time limit of %lu milliseconds exceeded", time_limit);
|
sprintf(exception_string, "Script time limit of %lu milliseconds exceeded", time_limit);
|
||||||
zend_throw_exception(php_ce_v8js_time_limit_exception, exception_string, 0);
|
zend_throw_exception(php_ce_v8js_time_limit_exception, exception_string, 0);
|
||||||
|
zval_ptr_dtor(&zv_v8inst);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,11 +214,13 @@ void v8js_v8_call(v8js_ctx *c, zval **return_value,
|
|||||||
// Execution has been terminated due to memory limit
|
// Execution has been terminated due to memory limit
|
||||||
sprintf(exception_string, "Script memory limit of %lu bytes exceeded", memory_limit);
|
sprintf(exception_string, "Script memory limit of %lu bytes exceeded", memory_limit);
|
||||||
zend_throw_exception(php_ce_v8js_memory_limit_exception, exception_string, 0);
|
zend_throw_exception(php_ce_v8js_memory_limit_exception, exception_string, 0);
|
||||||
|
zval_ptr_dtor(&zv_v8inst);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!try_catch.CanContinue()) {
|
if (!try_catch.CanContinue()) {
|
||||||
// At this point we can't re-throw the exception
|
// At this point we can't re-throw the exception
|
||||||
|
zval_ptr_dtor(&zv_v8inst);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -229,18 +239,21 @@ void v8js_v8_call(v8js_ctx *c, zval **return_value,
|
|||||||
/* Report immediately if report_uncaught is true */
|
/* Report immediately if report_uncaught is true */
|
||||||
if (c->report_uncaught) {
|
if (c->report_uncaught) {
|
||||||
v8js_throw_script_exception(c->isolate, &try_catch);
|
v8js_throw_script_exception(c->isolate, &try_catch);
|
||||||
|
zval_ptr_dtor(&zv_v8inst);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Exception thrown from JS, preserve it for future execution */
|
/* Exception thrown from JS, preserve it for future execution */
|
||||||
if (result.IsEmpty()) {
|
if (result.IsEmpty()) {
|
||||||
v8js_create_script_exception(&c->pending_exception, c->isolate, &try_catch);
|
v8js_create_script_exception(&c->pending_exception, c->isolate, &try_catch);
|
||||||
|
zval_ptr_dtor(&zv_v8inst);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Rethrow back to JS */
|
/* Rethrow back to JS */
|
||||||
try_catch.ReThrow();
|
try_catch.ReThrow();
|
||||||
|
zval_ptr_dtor(&zv_v8inst);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -250,6 +263,9 @@ void v8js_v8_call(v8js_ctx *c, zval **return_value,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
zval_ptr_dtor(&zv_v8inst);
|
||||||
|
}
|
||||||
/* }}} */
|
/* }}} */
|
||||||
|
|
||||||
void v8js_terminate_execution(v8::Isolate *isolate) /* {{{ */
|
void v8js_terminate_execution(v8::Isolate *isolate) /* {{{ */
|
||||||
|
@ -276,6 +276,14 @@ static HashTable *v8js_v8object_get_properties(zend_object *object) /* {{{ */
|
|||||||
}
|
}
|
||||||
/* }}} */
|
/* }}} */
|
||||||
|
|
||||||
|
static HashTable *v8js_v8object_get_gc(zend_object *object, zval **table, int *n) /* {{{ */
|
||||||
|
{
|
||||||
|
*table = NULL;
|
||||||
|
*n = 0;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
/* }}} */
|
||||||
|
|
||||||
static HashTable *v8js_v8object_get_debug_info(zend_object *object, int *is_temp) /* {{{ */
|
static HashTable *v8js_v8object_get_debug_info(zend_object *object, int *is_temp) /* {{{ */
|
||||||
{
|
{
|
||||||
*is_temp = 0;
|
*is_temp = 0;
|
||||||
@ -456,118 +464,6 @@ static zend_function *v8js_v8object_get_method(zend_object **object_ptr, zend_st
|
|||||||
}
|
}
|
||||||
/* }}} */
|
/* }}} */
|
||||||
|
|
||||||
static int v8js_v8object_call_method(zend_string *method, zend_object *object, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
|
|
||||||
{
|
|
||||||
zval *argv = NULL;
|
|
||||||
int argc = ZEND_NUM_ARGS();
|
|
||||||
|
|
||||||
v8js_v8object *obj = v8js_v8object_fetch_object(object);
|
|
||||||
|
|
||||||
if (!obj->ctx)
|
|
||||||
{
|
|
||||||
zend_throw_exception(php_ce_v8js_exception,
|
|
||||||
"Can't access V8Object after V8Js instance is destroyed!", 0);
|
|
||||||
return FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (obj->v8obj.IsEmpty())
|
|
||||||
{
|
|
||||||
return FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ZSTR_LEN(method) > std::numeric_limits<int>::max())
|
|
||||||
{
|
|
||||||
zend_throw_exception(php_ce_v8js_exception,
|
|
||||||
"Method name length exceeds maximum supported length", 0);
|
|
||||||
return FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (argc > 0)
|
|
||||||
{
|
|
||||||
argv = (zval *)safe_emalloc(sizeof(zval), argc, 0);
|
|
||||||
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::MaybeLocal<v8::Value>(v8::Isolate *)> v8_call = [obj, method, argc, argv, object, &return_value](v8::Isolate *isolate)
|
|
||||||
{
|
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
v8::Local<v8::Context> v8_context = isolate->GetEnteredOrMicrotaskContext();
|
|
||||||
v8::Local<v8::String> method_name = V8JS_SYML(ZSTR_VAL(method), static_cast<int>(ZSTR_LEN(method)));
|
|
||||||
v8::Local<v8::Object> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj)->ToObject(v8_context).ToLocalChecked();
|
|
||||||
v8::Local<v8::Object> thisObj;
|
|
||||||
v8::Local<v8::Function> cb;
|
|
||||||
|
|
||||||
if (method_name->Equals(v8_context, V8JS_SYM(V8JS_V8_INVOKE_FUNC_NAME)).FromMaybe(false))
|
|
||||||
{
|
|
||||||
cb = v8::Local<v8::Function>::Cast(v8obj);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
v8::Local<v8::Value> slot;
|
|
||||||
|
|
||||||
if (!v8obj->Get(v8_context, method_name).ToLocal(&slot))
|
|
||||||
{
|
|
||||||
return v8::MaybeLocal<v8::Value>();
|
|
||||||
}
|
|
||||||
|
|
||||||
cb = v8::Local<v8::Function>::Cast(slot);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If a method is invoked on V8Object, then set the object itself as
|
|
||||||
// "this" on JS side. Otherwise fall back to global object.
|
|
||||||
if (obj->std.ce == php_ce_v8object)
|
|
||||||
{
|
|
||||||
thisObj = v8obj;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
thisObj = V8JS_GLOBAL(isolate);
|
|
||||||
}
|
|
||||||
|
|
||||||
v8::Local<v8::Value> *jsArgv = static_cast<v8::Local<v8::Value> *>(alloca(sizeof(v8::Local<v8::Value>) * argc));
|
|
||||||
|
|
||||||
for (i = 0; i < argc; i++)
|
|
||||||
{
|
|
||||||
new (&jsArgv[i]) v8::Local<v8::Value>;
|
|
||||||
jsArgv[i] = v8::Local<v8::Value>::New(isolate, zval_to_v8js(&argv[i], isolate));
|
|
||||||
}
|
|
||||||
|
|
||||||
v8::MaybeLocal<v8::Value> result = cb->Call(v8_context, thisObj, argc, jsArgv);
|
|
||||||
|
|
||||||
if (obj->std.ce == php_ce_v8object && !result.IsEmpty() && result.ToLocalChecked()->StrictEquals(thisObj))
|
|
||||||
{
|
|
||||||
/* JS code did "return this", retain object identity */
|
|
||||||
ZVAL_OBJ(return_value, object);
|
|
||||||
zval_copy_ctor(return_value);
|
|
||||||
result = v8::MaybeLocal<v8::Value>();
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
};
|
|
||||||
|
|
||||||
v8js_v8_call(obj->ctx, &return_value, obj->flags, obj->ctx->time_limit, obj->ctx->memory_limit, v8_call);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (argc > 0)
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
/* }}} */
|
|
||||||
|
|
||||||
static int v8js_v8object_get_closure(zend_object *object, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **zobj_ptr, bool call) /* {{{ */
|
static int v8js_v8object_get_closure(zend_object *object, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **zobj_ptr, bool call) /* {{{ */
|
||||||
{
|
{
|
||||||
zend_internal_function *invoke;
|
zend_internal_function *invoke;
|
||||||
@ -1023,6 +919,7 @@ PHP_MINIT_FUNCTION(v8js_v8object_class) /* {{{ */
|
|||||||
v8js_v8object_handlers.unset_property = v8js_v8object_unset_property;
|
v8js_v8object_handlers.unset_property = v8js_v8object_unset_property;
|
||||||
v8js_v8object_handlers.get_properties = v8js_v8object_get_properties;
|
v8js_v8object_handlers.get_properties = v8js_v8object_get_properties;
|
||||||
v8js_v8object_handlers.get_method = v8js_v8object_get_method;
|
v8js_v8object_handlers.get_method = v8js_v8object_get_method;
|
||||||
|
v8js_v8object_handlers.get_gc = v8js_v8object_get_gc;
|
||||||
v8js_v8object_handlers.get_debug_info = v8js_v8object_get_debug_info;
|
v8js_v8object_handlers.get_debug_info = v8js_v8object_get_debug_info;
|
||||||
v8js_v8object_handlers.get_closure = v8js_v8object_get_closure;
|
v8js_v8object_handlers.get_closure = v8js_v8object_get_closure;
|
||||||
v8js_v8object_handlers.offset = XtOffsetOf(struct v8js_v8object, std);
|
v8js_v8object_handlers.offset = XtOffsetOf(struct v8js_v8object, std);
|
||||||
|
Loading…
Reference in New Issue
Block a user