mirror of
https://github.com/phpv8/v8js.git
synced 2024-12-22 22:51:51 +00:00
code cleanup, part 1
Splits longish v8js.cc file into pieces. It used to contain the definitions of all V8Js exception classes, the V8Js class itself as well as the V8Object/V8Function classes... besides the module setup code itself. This change factors out all exception class definitions into a seperate pair of files (v8js_exceptions.*). The V8Js class definition itself is moved to v8js_class.* pair, with the v8 init & call code moved to v8js_v8.* pair and the watchdog timer to v8js_timer.* pair. The V8Object/V8Function code was moved to v8js_v8object_class.* pair, not differentiating between the two like before.
This commit is contained in:
parent
af58f4ec9e
commit
5ea36016fe
15
config.m4
15
config.m4
@ -123,7 +123,20 @@ AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <v8-debug.h>]],
|
||||
CPPFLAGS=$old_CPPFLAGS
|
||||
]);
|
||||
|
||||
PHP_NEW_EXTENSION(v8js, v8js.cc v8js_array_access.cc v8js_convert.cc v8js_methods.cc v8js_object_export.cc v8js_variables.cc v8js_commonjs.cc, $ext_shared, , "-std="$ac_cv_v8_cstd)
|
||||
PHP_NEW_EXTENSION(v8js, [ \
|
||||
v8js_array_access.cc \
|
||||
v8js.cc \
|
||||
v8js_class.cc \
|
||||
v8js_commonjs.cc \
|
||||
v8js_convert.cc \
|
||||
v8js_exceptions.cc \
|
||||
v8js_methods.cc \
|
||||
v8js_object_export.cc \
|
||||
v8js_timer.cc \
|
||||
v8js_v8.cc \
|
||||
v8js_v8object_class.cc \
|
||||
v8js_variables.cc \
|
||||
], $ext_shared, , "-std="$ac_cv_v8_cstd)
|
||||
|
||||
PHP_ADD_MAKEFILE_FRAGMENT
|
||||
fi
|
||||
|
@ -13,7 +13,7 @@ if (PHP_V8JS != "no") {
|
||||
AC_DEFINE("PHP_V8_API_VERSION", "3017015", "", false);
|
||||
AC_DEFINE("PHP_V8_VERSION", "3.17.15", "", true);
|
||||
|
||||
EXTENSION("v8js", "v8js.cc v8js_array_access.cc v8js_commonjs.cc v8js_convert.cc v8js_methods.cc v8js_object_export.cc v8js_variables.cc", "yes");
|
||||
EXTENSION("v8js", "v8js_array_access.cc v8js.cc v8js_class.cc v8js_commonjs.cc v8js_convert.cc v8js_exceptions.cc v8js_methods.cc v8js_object_export.cc v8js_timer.cc v8js_v8.cc v8js_v8object_class.cc v8js_variables.cc", "yes");
|
||||
|
||||
} else {
|
||||
WARNING("v8js not enabled, headers or libs not found");
|
||||
|
@ -118,10 +118,6 @@ static inline const char * ToCString(const v8::String::Utf8Value &value) /* {{{
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* Extern Class entries */
|
||||
extern zend_class_entry *php_ce_v8_object;
|
||||
extern zend_class_entry *php_ce_v8_function;
|
||||
|
||||
/* Create PHP V8 object */
|
||||
void php_v8js_create_v8(zval *, v8::Handle<v8::Value>, int, v8::Isolate * TSRMLS_DC);
|
||||
|
||||
|
1142
v8js_class.cc
Normal file
1142
v8js_class.cc
Normal file
File diff suppressed because it is too large
Load Diff
28
v8js_class.h
Normal file
28
v8js_class.h
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| PHP Version 5 |
|
||||
+----------------------------------------------------------------------+
|
||||
| Copyright (c) 1997-2013 The PHP Group |
|
||||
+----------------------------------------------------------------------+
|
||||
| http://www.opensource.org/licenses/mit-license.php MIT License |
|
||||
+----------------------------------------------------------------------+
|
||||
| Author: Jani Taskinen <jani.taskinen@iki.fi> |
|
||||
| Author: Patrick Reilly <preilly@php.net> |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
#ifndef V8JS_CLASS_H
|
||||
#define V8JS_CLASS_H
|
||||
|
||||
PHP_MINIT_FUNCTION(v8js_class);
|
||||
|
||||
#endif /* V8JS_CLASS_H */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim600: noet sw=4 ts=4 fdm=marker
|
||||
* vim<600: noet sw=4 ts=4
|
||||
*/
|
244
v8js_exceptions.cc
Normal file
244
v8js_exceptions.cc
Normal file
@ -0,0 +1,244 @@
|
||||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| PHP Version 5 |
|
||||
+----------------------------------------------------------------------+
|
||||
| Copyright (c) 1997-2013 The PHP Group |
|
||||
+----------------------------------------------------------------------+
|
||||
| http://www.opensource.org/licenses/mit-license.php MIT License |
|
||||
+----------------------------------------------------------------------+
|
||||
| Author: Jani Taskinen <jani.taskinen@iki.fi> |
|
||||
| Author: Patrick Reilly <preilly@php.net> |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
extern "C" {
|
||||
#include "php.h"
|
||||
#include "ext/date/php_date.h"
|
||||
#include "ext/standard/php_string.h"
|
||||
#include "zend_interfaces.h"
|
||||
#include "zend_closures.h"
|
||||
#include "ext/spl/spl_exceptions.h"
|
||||
#include "zend_exceptions.h"
|
||||
}
|
||||
|
||||
#include "php_v8js_macros.h"
|
||||
|
||||
|
||||
/* {{{ Class Entries */
|
||||
zend_class_entry *php_ce_v8js_exception;
|
||||
zend_class_entry *php_ce_v8js_script_exception;
|
||||
zend_class_entry *php_ce_v8js_time_limit_exception;
|
||||
zend_class_entry *php_ce_v8js_memory_limit_exception;
|
||||
/* }}} */
|
||||
|
||||
|
||||
/* {{{ Class: V8JsScriptException */
|
||||
|
||||
void php_v8js_create_script_exception(zval *return_value, v8::TryCatch *try_catch TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
v8::String::Utf8Value exception(try_catch->Exception());
|
||||
const char *exception_string = ToCString(exception);
|
||||
v8::Handle<v8::Message> tc_message = try_catch->Message();
|
||||
const char *filename_string, *sourceline_string;
|
||||
char *message_string;
|
||||
int linenum, start_col, end_col, message_len;
|
||||
|
||||
object_init_ex(return_value, php_ce_v8js_script_exception);
|
||||
|
||||
#define PHPV8_EXPROP(type, name, value) \
|
||||
zend_update_property##type(php_ce_v8js_script_exception, return_value, #name, sizeof(#name) - 1, value TSRMLS_CC);
|
||||
|
||||
if (tc_message.IsEmpty()) {
|
||||
message_len = spprintf(&message_string, 0, "%s", exception_string);
|
||||
}
|
||||
else
|
||||
{
|
||||
v8::String::Utf8Value filename(tc_message->GetScriptResourceName());
|
||||
filename_string = ToCString(filename);
|
||||
PHPV8_EXPROP(_string, JsFileName, filename_string);
|
||||
|
||||
v8::String::Utf8Value sourceline(tc_message->GetSourceLine());
|
||||
sourceline_string = ToCString(sourceline);
|
||||
PHPV8_EXPROP(_string, JsSourceLine, sourceline_string);
|
||||
|
||||
linenum = tc_message->GetLineNumber();
|
||||
PHPV8_EXPROP(_long, JsLineNumber, linenum);
|
||||
|
||||
start_col = tc_message->GetStartColumn();
|
||||
PHPV8_EXPROP(_long, JsStartColumn, start_col);
|
||||
|
||||
end_col = tc_message->GetEndColumn();
|
||||
PHPV8_EXPROP(_long, JsEndColumn, end_col);
|
||||
|
||||
message_len = spprintf(&message_string, 0, "%s:%d: %s", filename_string, linenum, exception_string);
|
||||
|
||||
v8::String::Utf8Value stacktrace(try_catch->StackTrace());
|
||||
if (stacktrace.length() > 0) {
|
||||
const char* stacktrace_string = ToCString(stacktrace);
|
||||
PHPV8_EXPROP(_string, JsTrace, stacktrace_string);
|
||||
}
|
||||
}
|
||||
|
||||
PHPV8_EXPROP(_string, message, message_string);
|
||||
|
||||
efree(message_string);
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
void php_v8js_throw_script_exception(v8::TryCatch *try_catch TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
v8::String::Utf8Value exception(try_catch->Exception());
|
||||
const char *exception_string = ToCString(exception);
|
||||
zval *zexception = NULL;
|
||||
|
||||
if (try_catch->Message().IsEmpty()) {
|
||||
zend_throw_exception(php_ce_v8js_script_exception, (char *) exception_string, 0 TSRMLS_CC);
|
||||
} else {
|
||||
MAKE_STD_ZVAL(zexception);
|
||||
php_v8js_create_script_exception(zexception, try_catch TSRMLS_CC);
|
||||
zend_throw_exception_object(zexception TSRMLS_CC);
|
||||
}
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
#define V8JS_EXCEPTION_METHOD(property) \
|
||||
static PHP_METHOD(V8JsScriptException, get##property) \
|
||||
{ \
|
||||
zval *value; \
|
||||
\
|
||||
if (zend_parse_parameters_none() == FAILURE) { \
|
||||
return; \
|
||||
} \
|
||||
value = zend_read_property(php_ce_v8js_script_exception, getThis(), #property, sizeof(#property) - 1, 0 TSRMLS_CC); \
|
||||
*return_value = *value; \
|
||||
zval_copy_ctor(return_value); \
|
||||
INIT_PZVAL(return_value); \
|
||||
}
|
||||
|
||||
/* {{{ proto string V8JsEScriptxception::getJsFileName()
|
||||
*/
|
||||
V8JS_EXCEPTION_METHOD(JsFileName);
|
||||
/* }}} */
|
||||
|
||||
/* {{{ proto string V8JsScriptException::getJsLineNumber()
|
||||
*/
|
||||
V8JS_EXCEPTION_METHOD(JsLineNumber);
|
||||
/* }}} */
|
||||
|
||||
/* {{{ proto string V8JsScriptException::getJsStartColumn()
|
||||
*/
|
||||
V8JS_EXCEPTION_METHOD(JsStartColumn);
|
||||
/* }}} */
|
||||
|
||||
/* {{{ proto string V8JsScriptException::getJsEndColumn()
|
||||
*/
|
||||
V8JS_EXCEPTION_METHOD(JsEndColumn);
|
||||
/* }}} */
|
||||
|
||||
/* {{{ proto string V8JsScriptException::getJsSourceLine()
|
||||
*/
|
||||
V8JS_EXCEPTION_METHOD(JsSourceLine);
|
||||
/* }}} */
|
||||
|
||||
/* {{{ proto string V8JsScriptException::getJsTrace()
|
||||
*/
|
||||
V8JS_EXCEPTION_METHOD(JsTrace);
|
||||
/* }}} */
|
||||
|
||||
|
||||
ZEND_BEGIN_ARG_INFO(arginfo_v8jsscriptexception_no_args, 0)
|
||||
ZEND_END_ARG_INFO()
|
||||
|
||||
static const zend_function_entry v8js_script_exception_methods[] = { /* {{{ */
|
||||
PHP_ME(V8JsScriptException, getJsFileName, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
|
||||
PHP_ME(V8JsScriptException, getJsLineNumber, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
|
||||
PHP_ME(V8JsScriptException, getJsStartColumn, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
|
||||
PHP_ME(V8JsScriptException, getJsEndColumn, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
|
||||
PHP_ME(V8JsScriptException, getJsSourceLine, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
|
||||
PHP_ME(V8JsScriptException, getJsTrace, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
|
||||
{NULL, NULL, NULL}
|
||||
};
|
||||
/* }}} */
|
||||
|
||||
/* }}} V8JsScriptException */
|
||||
|
||||
|
||||
|
||||
/* {{{ Class: V8JsException */
|
||||
|
||||
static const zend_function_entry v8js_exception_methods[] = { /* {{{ */
|
||||
{NULL, NULL, NULL}
|
||||
};
|
||||
/* }}} */
|
||||
|
||||
/* }}} V8JsException */
|
||||
|
||||
|
||||
|
||||
/* {{{ Class: V8JsTimeLimitException */
|
||||
|
||||
static const zend_function_entry v8js_time_limit_exception_methods[] = { /* {{{ */
|
||||
{NULL, NULL, NULL}
|
||||
};
|
||||
/* }}} */
|
||||
|
||||
/* }}} V8JsTimeLimitException */
|
||||
|
||||
|
||||
|
||||
/* {{{ Class: V8JsMemoryLimitException */
|
||||
|
||||
static const zend_function_entry v8js_memory_limit_exception_methods[] = { /* {{{ */
|
||||
{NULL, NULL, NULL}
|
||||
};
|
||||
/* }}} */
|
||||
|
||||
/* }}} V8JsMemoryLimitException */
|
||||
|
||||
|
||||
|
||||
PHP_MINIT_FUNCTION(v8js_exceptions) /* {{{ */
|
||||
{
|
||||
zend_class_entry ce;
|
||||
|
||||
/* V8JsException Class */
|
||||
INIT_CLASS_ENTRY(ce, "V8JsException", v8js_exception_methods);
|
||||
php_ce_v8js_exception = zend_register_internal_class_ex(&ce, spl_ce_RuntimeException, NULL TSRMLS_CC);
|
||||
|
||||
/* V8JsScriptException Class */
|
||||
INIT_CLASS_ENTRY(ce, "V8JsScriptException", v8js_script_exception_methods);
|
||||
php_ce_v8js_script_exception = zend_register_internal_class_ex(&ce, php_ce_v8js_exception, NULL TSRMLS_CC);
|
||||
php_ce_v8js_script_exception->ce_flags |= ZEND_ACC_FINAL;
|
||||
|
||||
/* Add custom JS specific properties */
|
||||
zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsFileName"), ZEND_ACC_PROTECTED TSRMLS_CC);
|
||||
zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsLineNumber"), ZEND_ACC_PROTECTED TSRMLS_CC);
|
||||
zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsStartColumn"), ZEND_ACC_PROTECTED TSRMLS_CC);
|
||||
zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsEndColumn"), ZEND_ACC_PROTECTED TSRMLS_CC);
|
||||
zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsSourceLine"), ZEND_ACC_PROTECTED TSRMLS_CC);
|
||||
zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsTrace"), ZEND_ACC_PROTECTED TSRMLS_CC);
|
||||
|
||||
/* V8JsTimeLimitException Class */
|
||||
INIT_CLASS_ENTRY(ce, "V8JsTimeLimitException", v8js_time_limit_exception_methods);
|
||||
php_ce_v8js_time_limit_exception = zend_register_internal_class_ex(&ce, php_ce_v8js_exception, NULL TSRMLS_CC);
|
||||
php_ce_v8js_time_limit_exception->ce_flags |= ZEND_ACC_FINAL;
|
||||
|
||||
/* V8JsMemoryLimitException Class */
|
||||
INIT_CLASS_ENTRY(ce, "V8JsMemoryLimitException", v8js_memory_limit_exception_methods);
|
||||
php_ce_v8js_memory_limit_exception = zend_register_internal_class_ex(&ce, php_ce_v8js_exception, NULL TSRMLS_CC);
|
||||
php_ce_v8js_memory_limit_exception->ce_flags |= ZEND_ACC_FINAL;
|
||||
|
||||
} /* }}} */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim600: noet sw=4 ts=4 fdm=marker
|
||||
* vim<600: noet sw=4 ts=4
|
||||
*/
|
36
v8js_exceptions.h
Normal file
36
v8js_exceptions.h
Normal file
@ -0,0 +1,36 @@
|
||||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| PHP Version 5 |
|
||||
+----------------------------------------------------------------------+
|
||||
| Copyright (c) 1997-2013 The PHP Group |
|
||||
+----------------------------------------------------------------------+
|
||||
| http://www.opensource.org/licenses/mit-license.php MIT License |
|
||||
+----------------------------------------------------------------------+
|
||||
| Author: Jani Taskinen <jani.taskinen@iki.fi> |
|
||||
| Author: Patrick Reilly <preilly@php.net> |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
#ifndef V8JS_EXCEPTIONS_H
|
||||
#define V8JS_EXCEPTIONS_H
|
||||
|
||||
extern zend_class_entry *php_ce_v8js_exception;
|
||||
extern zend_class_entry *php_ce_v8js_script_exception;
|
||||
extern zend_class_entry *php_ce_v8js_time_limit_exception;
|
||||
extern zend_class_entry *php_ce_v8js_memory_limit_exception;
|
||||
|
||||
void php_v8js_create_script_exception(zval *return_value, v8::TryCatch *try_catch TSRMLS_DC);
|
||||
void php_v8js_throw_script_exception(v8::TryCatch *try_catch TSRMLS_DC);
|
||||
|
||||
PHP_MINIT_FUNCTION(v8js_exceptions);
|
||||
|
||||
#endif /* V8JS_EXCEPTIONS_H */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim600: noet sw=4 ts=4 fdm=marker
|
||||
* vim<600: noet sw=4 ts=4
|
||||
*/
|
@ -26,6 +26,7 @@ extern "C" {
|
||||
#include "php_v8js_macros.h"
|
||||
#include "v8js_array_access.h"
|
||||
#include "v8js_object_export.h"
|
||||
#include "v8js_v8object_class.h"
|
||||
|
||||
static void php_v8js_weak_object_callback(const v8::WeakCallbackData<v8::Object, zval> &data);
|
||||
|
||||
|
143
v8js_timer.cc
Normal file
143
v8js_timer.cc
Normal file
@ -0,0 +1,143 @@
|
||||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| PHP Version 5 |
|
||||
+----------------------------------------------------------------------+
|
||||
| Copyright (c) 1997-2013 The PHP Group |
|
||||
+----------------------------------------------------------------------+
|
||||
| http://www.opensource.org/licenses/mit-license.php MIT License |
|
||||
+----------------------------------------------------------------------+
|
||||
| Author: Jani Taskinen <jani.taskinen@iki.fi> |
|
||||
| Author: Patrick Reilly <preilly@php.net> |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
extern "C" {
|
||||
#include "php.h"
|
||||
#include "ext/date/php_date.h"
|
||||
#include "ext/standard/php_string.h"
|
||||
#include "zend_interfaces.h"
|
||||
#include "zend_closures.h"
|
||||
#include "ext/spl/spl_exceptions.h"
|
||||
#include "zend_exceptions.h"
|
||||
}
|
||||
|
||||
#include "php_v8js_macros.h"
|
||||
#include "v8js_v8.h"
|
||||
#include "v8js_exceptions.h"
|
||||
|
||||
static void v8js_timer_interrupt_handler(v8::Isolate *isolate, void *data) { /* {{{ */
|
||||
#ifdef ZTS
|
||||
TSRMLS_D = (void ***) data;
|
||||
#endif
|
||||
|
||||
if (!V8JSG(timer_stack).size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
v8::Locker locker(isolate);
|
||||
v8::HeapStatistics hs;
|
||||
isolate->GetHeapStatistics(&hs);
|
||||
|
||||
V8JSG(timer_mutex).lock();
|
||||
|
||||
for (std::deque< php_v8js_timer_ctx* >::iterator it = V8JSG(timer_stack).begin();
|
||||
it != V8JSG(timer_stack).end(); it ++) {
|
||||
php_v8js_timer_ctx *timer_ctx = *it;
|
||||
php_v8js_ctx *c = timer_ctx->v8js_ctx;
|
||||
|
||||
if(c->isolate != isolate || timer_ctx->killed) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (timer_ctx->memory_limit > 0 && hs.used_heap_size() > timer_ctx->memory_limit) {
|
||||
timer_ctx->killed = true;
|
||||
v8js_terminate_execution(c TSRMLS_CC);
|
||||
c->memory_limit_hit = true;
|
||||
}
|
||||
}
|
||||
|
||||
V8JSG(timer_mutex).unlock();
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
void v8js_timer_thread(TSRMLS_D) /* {{{ */
|
||||
{
|
||||
while (!V8JSG(timer_stop)) {
|
||||
|
||||
V8JSG(timer_mutex).lock();
|
||||
if (V8JSG(timer_stack).size()) {
|
||||
php_v8js_timer_ctx *timer_ctx = V8JSG(timer_stack).front();
|
||||
php_v8js_ctx *c = timer_ctx->v8js_ctx;
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now();
|
||||
|
||||
if(timer_ctx->killed) {
|
||||
/* execution already terminated, nothing to check anymore,
|
||||
* but wait for caller to pop this timer context. */
|
||||
}
|
||||
else if(timer_ctx->time_limit > 0 && now > timer_ctx->time_point) {
|
||||
timer_ctx->killed = true;
|
||||
v8js_terminate_execution(c TSRMLS_CC);
|
||||
c->time_limit_hit = true;
|
||||
}
|
||||
else if (timer_ctx->memory_limit > 0) {
|
||||
/* If a memory_limit is set, we need to interrupt execution
|
||||
* and check heap size within the callback. We must *not*
|
||||
* directly call GetHeapStatistics here, since we don't have
|
||||
* a v8::Locker on the isolate, but are expected to hold one,
|
||||
* and cannot aquire it as v8 is executing the script ... */
|
||||
void *data = NULL;
|
||||
#ifdef ZTS
|
||||
data = (void *) TSRMLS_C;
|
||||
#endif
|
||||
c->isolate->RequestInterrupt(v8js_timer_interrupt_handler, data);
|
||||
}
|
||||
}
|
||||
V8JSG(timer_mutex).unlock();
|
||||
|
||||
// Sleep for 10ms
|
||||
#ifdef _WIN32
|
||||
concurrency::wait(10);
|
||||
#else
|
||||
std::chrono::milliseconds duration(10);
|
||||
std::this_thread::sleep_for(duration);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
|
||||
void v8js_timer_push(long time_limit, long memory_limit, php_v8js_ctx *c TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
V8JSG(timer_mutex).lock();
|
||||
|
||||
// Create context for this timer
|
||||
php_v8js_timer_ctx *timer_ctx = (php_v8js_timer_ctx *)emalloc(sizeof(php_v8js_timer_ctx));
|
||||
|
||||
// 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();
|
||||
|
||||
// Push the timer context
|
||||
timer_ctx->time_limit = time_limit;
|
||||
timer_ctx->memory_limit = memory_limit;
|
||||
timer_ctx->time_point = from + duration;
|
||||
timer_ctx->v8js_ctx = c;
|
||||
timer_ctx->killed = false;
|
||||
V8JSG(timer_stack).push_front(timer_ctx);
|
||||
|
||||
V8JSG(timer_mutex).unlock();
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim600: noet sw=4 ts=4 fdm=marker
|
||||
* vim<600: noet sw=4 ts=4
|
||||
*/
|
29
v8js_timer.h
Normal file
29
v8js_timer.h
Normal file
@ -0,0 +1,29 @@
|
||||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| PHP Version 5 |
|
||||
+----------------------------------------------------------------------+
|
||||
| Copyright (c) 1997-2013 The PHP Group |
|
||||
+----------------------------------------------------------------------+
|
||||
| http://www.opensource.org/licenses/mit-license.php MIT License |
|
||||
+----------------------------------------------------------------------+
|
||||
| Author: Jani Taskinen <jani.taskinen@iki.fi> |
|
||||
| Author: Patrick Reilly <preilly@php.net> |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
#ifndef V8JS_TIMER_H
|
||||
#define V8JS_TIMER_H
|
||||
|
||||
void v8js_timer_thread(TSRMLS_D);
|
||||
void v8js_timer_push(long time_limit, long memory_limit, php_v8js_ctx *c TSRMLS_DC);
|
||||
|
||||
#endif /* V8JS_TIMER_H */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim600: noet sw=4 ts=4 fdm=marker
|
||||
* vim<600: noet sw=4 ts=4
|
||||
*/
|
215
v8js_v8.cc
Normal file
215
v8js_v8.cc
Normal file
@ -0,0 +1,215 @@
|
||||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| PHP Version 5 |
|
||||
+----------------------------------------------------------------------+
|
||||
| Copyright (c) 1997-2013 The PHP Group |
|
||||
+----------------------------------------------------------------------+
|
||||
| http://www.opensource.org/licenses/mit-license.php MIT License |
|
||||
+----------------------------------------------------------------------+
|
||||
| Author: Jani Taskinen <jani.taskinen@iki.fi> |
|
||||
| Author: Patrick Reilly <preilly@php.net> |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#if !defined(_WIN32) && PHP_V8_API_VERSION >= 3029036
|
||||
#include <libplatform/libplatform.h>
|
||||
#endif
|
||||
|
||||
extern "C" {
|
||||
#include "php.h"
|
||||
#include "ext/date/php_date.h"
|
||||
#include "ext/standard/php_string.h"
|
||||
#include "zend_interfaces.h"
|
||||
#include "zend_closures.h"
|
||||
#include "zend_exceptions.h"
|
||||
}
|
||||
|
||||
#include "php_v8js_macros.h"
|
||||
#include "v8js_v8.h"
|
||||
#include "v8js_timer.h"
|
||||
#include "v8js_exceptions.h"
|
||||
|
||||
void v8js_v8_init(TSRMLS_D) /* {{{ */
|
||||
{
|
||||
/* Run only once */
|
||||
if (V8JSG(v8_initialized)) {
|
||||
return;
|
||||
}
|
||||
|
||||
#if !defined(_WIN32) && PHP_V8_API_VERSION >= 3029036
|
||||
v8::Platform* platform = v8::platform::CreateDefaultPlatform();
|
||||
v8::V8::InitializePlatform(platform);
|
||||
#endif
|
||||
|
||||
/* Set V8 command line flags (must be done before V8::Initialize()!) */
|
||||
if (V8JSG(v8_flags)) {
|
||||
v8::V8::SetFlagsFromString(V8JSG(v8_flags), strlen(V8JSG(v8_flags)));
|
||||
}
|
||||
|
||||
/* Initialize V8 */
|
||||
v8::V8::Initialize();
|
||||
|
||||
/* Run only once */
|
||||
V8JSG(v8_initialized) = 1;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
|
||||
void v8js_v8_call(php_v8js_ctx *c, zval **return_value,
|
||||
long flags, long time_limit, long memory_limit,
|
||||
std::function< v8::Local<v8::Value>(v8::Isolate *) >& v8_call TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
char *tz = NULL;
|
||||
|
||||
V8JS_CTX_PROLOGUE(c);
|
||||
|
||||
V8JSG(timer_mutex).lock();
|
||||
c->time_limit_hit = false;
|
||||
c->memory_limit_hit = false;
|
||||
V8JSG(timer_mutex).unlock();
|
||||
|
||||
/* Catch JS exceptions */
|
||||
v8::TryCatch try_catch;
|
||||
|
||||
/* Set flags for runtime use */
|
||||
V8JS_GLOBAL_SET_FLAGS(isolate, flags);
|
||||
|
||||
/* Check if timezone has been changed and notify V8 */
|
||||
tz = getenv("TZ");
|
||||
|
||||
if (tz != NULL) {
|
||||
if (c->tz == NULL) {
|
||||
c->tz = strdup(tz);
|
||||
}
|
||||
else if (strcmp(c->tz, tz) != 0) {
|
||||
v8::Date::DateTimeConfigurationChangeNotification(c->isolate);
|
||||
|
||||
free(c->tz);
|
||||
c->tz = strdup(tz);
|
||||
}
|
||||
}
|
||||
|
||||
if (time_limit > 0 || memory_limit > 0) {
|
||||
// If timer thread is not running then start it
|
||||
if (!V8JSG(timer_thread)) {
|
||||
// If not, start timer thread
|
||||
V8JSG(timer_thread) = new std::thread(v8js_timer_thread TSRMLS_CC);
|
||||
}
|
||||
}
|
||||
|
||||
/* Always pass the timer to the stack so there can be follow-up changes to
|
||||
* the time & memory limit. */
|
||||
v8js_timer_push(time_limit, memory_limit, c TSRMLS_CC);
|
||||
|
||||
#ifdef ENABLE_DEBUGGER_SUPPORT
|
||||
if(c == v8js_debug_context && v8js_debug_auto_break_mode != V8JS_DEBUG_AUTO_BREAK_NEVER) {
|
||||
v8::Debug::DebugBreak(c->isolate);
|
||||
|
||||
if(v8js_debug_auto_break_mode == V8JS_DEBUG_AUTO_BREAK_ONCE) {
|
||||
/* If break-once-mode was enabled, reset flag. */
|
||||
v8js_debug_auto_break_mode = V8JS_DEBUG_AUTO_BREAK_NEVER;
|
||||
}
|
||||
}
|
||||
#endif /* ENABLE_DEBUGGER_SUPPORT */
|
||||
|
||||
/* Execute script */
|
||||
c->in_execution++;
|
||||
v8::Local<v8::Value> result = v8_call(c->isolate);
|
||||
c->in_execution--;
|
||||
|
||||
/* Pop our context from the stack and read (possibly updated) limits
|
||||
* into local variables. */
|
||||
V8JSG(timer_mutex).lock();
|
||||
php_v8js_timer_ctx *timer_ctx = V8JSG(timer_stack).front();
|
||||
V8JSG(timer_stack).pop_front();
|
||||
V8JSG(timer_mutex).unlock();
|
||||
|
||||
time_limit = timer_ctx->time_limit;
|
||||
memory_limit = timer_ctx->memory_limit;
|
||||
|
||||
efree(timer_ctx);
|
||||
|
||||
/* Check for fatal error marker possibly set by php_v8js_error_handler; just
|
||||
* rethrow the error since we're now out of V8. */
|
||||
if(V8JSG(fatal_error_abort)) {
|
||||
zend_bailout();
|
||||
}
|
||||
|
||||
char exception_string[64];
|
||||
|
||||
if (c->time_limit_hit) {
|
||||
// Execution has been terminated due to 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 TSRMLS_CC);
|
||||
return;
|
||||
}
|
||||
|
||||
if (c->memory_limit_hit) {
|
||||
// Execution has been terminated due to 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 TSRMLS_CC);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!try_catch.CanContinue()) {
|
||||
// At this point we can't re-throw the exception
|
||||
return;
|
||||
}
|
||||
|
||||
/* There was pending exception left from earlier executions -> throw to PHP */
|
||||
if (c->pending_exception) {
|
||||
zend_throw_exception_object(c->pending_exception TSRMLS_CC);
|
||||
c->pending_exception = NULL;
|
||||
}
|
||||
|
||||
/* Handle runtime JS exceptions */
|
||||
if (try_catch.HasCaught()) {
|
||||
|
||||
/* Pending exceptions are set only in outer caller, inner caller exceptions are always rethrown */
|
||||
if (c->in_execution < 1) {
|
||||
|
||||
/* Report immediately if report_uncaught is true */
|
||||
if (c->report_uncaught) {
|
||||
php_v8js_throw_script_exception(&try_catch TSRMLS_CC);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Exception thrown from JS, preserve it for future execution */
|
||||
if (result.IsEmpty()) {
|
||||
MAKE_STD_ZVAL(c->pending_exception);
|
||||
php_v8js_create_script_exception(c->pending_exception, &try_catch TSRMLS_CC);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* Rethrow back to JS */
|
||||
try_catch.ReThrow();
|
||||
return;
|
||||
}
|
||||
|
||||
/* Convert V8 value to PHP value */
|
||||
if (!result.IsEmpty()) {
|
||||
v8js_to_zval(result, *return_value, flags, c->isolate TSRMLS_CC);
|
||||
}
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
void v8js_terminate_execution(php_v8js_ctx *c TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
// Forcefully terminate the current thread of V8 execution in the isolate
|
||||
v8::V8::TerminateExecution(c->isolate);
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim600: noet sw=4 ts=4 fdm=marker
|
||||
* vim<600: noet sw=4 ts=4
|
||||
*/
|
52
v8js_v8.h
Normal file
52
v8js_v8.h
Normal file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| PHP Version 5 |
|
||||
+----------------------------------------------------------------------+
|
||||
| Copyright (c) 1997-2013 The PHP Group |
|
||||
+----------------------------------------------------------------------+
|
||||
| http://www.opensource.org/licenses/mit-license.php MIT License |
|
||||
+----------------------------------------------------------------------+
|
||||
| Author: Jani Taskinen <jani.taskinen@iki.fi> |
|
||||
| Author: Patrick Reilly <preilly@php.net> |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
#ifndef V8JS_V8_H
|
||||
#define V8JS_V8_H
|
||||
|
||||
void v8js_v8_init(TSRMLS_D);
|
||||
void v8js_v8_call(php_v8js_ctx *c, zval **return_value,
|
||||
long flags, long time_limit, long memory_limit,
|
||||
std::function< v8::Local<v8::Value>(v8::Isolate *) >& v8_call TSRMLS_DC);
|
||||
void v8js_terminate_execution(php_v8js_ctx *c TSRMLS_DC);
|
||||
|
||||
|
||||
#define V8JS_CTX_PROLOGUE(ctx) \
|
||||
if (!V8JSG(v8_initialized)) { \
|
||||
zend_error(E_ERROR, "V8 not initialized"); \
|
||||
return; \
|
||||
} \
|
||||
\
|
||||
v8::Isolate *isolate = (ctx)->isolate; \
|
||||
v8::Locker locker(isolate); \
|
||||
v8::Isolate::Scope isolate_scope(isolate); \
|
||||
v8::HandleScope handle_scope(isolate); \
|
||||
v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(isolate, (ctx)->context); \
|
||||
v8::Context::Scope context_scope(v8_context);
|
||||
|
||||
#define V8JS_BEGIN_CTX(ctx, object) \
|
||||
php_v8js_ctx *(ctx); \
|
||||
(ctx) = (php_v8js_ctx *) zend_object_store_get_object(object TSRMLS_CC); \
|
||||
V8JS_CTX_PROLOGUE(ctx);
|
||||
|
||||
|
||||
#endif /* V8JS_V8_H */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim600: noet sw=4 ts=4 fdm=marker
|
||||
* vim<600: noet sw=4 ts=4
|
||||
*/
|
638
v8js_v8object_class.cc
Normal file
638
v8js_v8object_class.cc
Normal file
@ -0,0 +1,638 @@
|
||||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| PHP Version 5 |
|
||||
+----------------------------------------------------------------------+
|
||||
| Copyright (c) 1997-2013 The PHP Group |
|
||||
+----------------------------------------------------------------------+
|
||||
| http://www.opensource.org/licenses/mit-license.php MIT License |
|
||||
+----------------------------------------------------------------------+
|
||||
| Author: Jani Taskinen <jani.taskinen@iki.fi> |
|
||||
| Author: Patrick Reilly <preilly@php.net> |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
extern "C" {
|
||||
#include "php.h"
|
||||
#include "ext/date/php_date.h"
|
||||
#include "ext/standard/php_string.h"
|
||||
#include "zend_interfaces.h"
|
||||
#include "zend_closures.h"
|
||||
#include "ext/spl/spl_exceptions.h"
|
||||
#include "zend_exceptions.h"
|
||||
}
|
||||
|
||||
#include "php_v8js_macros.h"
|
||||
#include "v8js_exceptions.h"
|
||||
#include "v8js_v8.h"
|
||||
|
||||
/* {{{ Class Entries */
|
||||
zend_class_entry *php_ce_v8_object;
|
||||
zend_class_entry *php_ce_v8_function;
|
||||
/* }}} */
|
||||
|
||||
/* {{{ Object Handlers */
|
||||
static zend_object_handlers v8_object_handlers;
|
||||
/* }}} */
|
||||
|
||||
#define V8JS_V8_INVOKE_FUNC_NAME "V8Js::V8::Invoke"
|
||||
|
||||
|
||||
/* V8 Object handlers */
|
||||
|
||||
static int php_v8js_v8_has_property(zval *object, zval *member, int has_set_exists ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
/* param has_set_exists:
|
||||
* 0 (has) whether property exists and is not NULL - isset()
|
||||
* 1 (set) whether property exists and is true-ish - empty()
|
||||
* 2 (exists) whether property exists - property_exists()
|
||||
*/
|
||||
int retval = false;
|
||||
php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
|
||||
|
||||
if (!obj->ctx) {
|
||||
zend_throw_exception(php_ce_v8js_exception,
|
||||
"Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
|
||||
return retval;
|
||||
}
|
||||
|
||||
v8::Isolate *isolate = obj->ctx->isolate;
|
||||
v8::Locker locker(isolate);
|
||||
v8::Isolate::Scope isolate_scope(isolate);
|
||||
v8::HandleScope local_scope(isolate);
|
||||
v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
|
||||
v8::Context::Scope temp_scope(temp_context);
|
||||
|
||||
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
|
||||
|
||||
if (Z_TYPE_P(member) == IS_STRING && v8obj->IsObject() && !v8obj->IsFunction())
|
||||
{
|
||||
|
||||
v8::Local<v8::Object> jsObj = v8obj->ToObject();
|
||||
v8::Local<v8::String> jsKey = V8JS_STRL(Z_STRVAL_P(member), Z_STRLEN_P(member));
|
||||
v8::Local<v8::Value> jsVal;
|
||||
|
||||
/* Skip any prototype properties */
|
||||
if (jsObj->HasRealNamedProperty(jsKey) || jsObj->HasRealNamedCallbackProperty(jsKey)) {
|
||||
if (has_set_exists == 2) {
|
||||
/* property_exists(), that's enough! */
|
||||
retval = true;
|
||||
} else {
|
||||
/* We need to look at the value. */
|
||||
jsVal = jsObj->Get(jsKey);
|
||||
if (has_set_exists == 0 ) {
|
||||
/* isset(): We make 'undefined' equivalent to 'null' */
|
||||
retval = !( jsVal->IsNull() || jsVal->IsUndefined() );
|
||||
} else {
|
||||
/* empty() */
|
||||
retval = jsVal->BooleanValue();
|
||||
/* for PHP compatibility, [] should also be empty */
|
||||
if (jsVal->IsArray() && retval) {
|
||||
v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(jsVal);
|
||||
retval = (array->Length() != 0);
|
||||
}
|
||||
/* for PHP compatibility, '0' should also be empty */
|
||||
if (jsVal->IsString() && retval) {
|
||||
v8::Local<v8::String> str = jsVal->ToString();
|
||||
if (str->Length() == 1) {
|
||||
uint16_t c = 0;
|
||||
str->Write(&c, 0, 1);
|
||||
if (c == '0') {
|
||||
retval = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
static zval *php_v8js_v8_read_property(zval *object, zval *member, int type ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
zval *retval = NULL;
|
||||
php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
|
||||
|
||||
if (!obj->ctx) {
|
||||
zend_throw_exception(php_ce_v8js_exception,
|
||||
"Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
|
||||
ALLOC_INIT_ZVAL(retval);
|
||||
return retval;
|
||||
}
|
||||
|
||||
v8::Isolate *isolate = obj->ctx->isolate;
|
||||
v8::Locker locker(isolate);
|
||||
v8::Isolate::Scope isolate_scope(isolate);
|
||||
v8::HandleScope local_scope(isolate);
|
||||
v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
|
||||
v8::Context::Scope temp_scope(temp_context);
|
||||
|
||||
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
|
||||
|
||||
if (Z_TYPE_P(member) == IS_STRING && v8obj->IsObject() && !v8obj->IsFunction())
|
||||
{
|
||||
|
||||
v8::Local<v8::Object> jsObj = v8obj->ToObject();
|
||||
v8::Local<v8::String> jsKey = V8JS_STRL(Z_STRVAL_P(member), Z_STRLEN_P(member));
|
||||
v8::Local<v8::Value> jsVal;
|
||||
|
||||
/* Skip any prototype properties */
|
||||
if (jsObj->HasRealNamedProperty(jsKey) || jsObj->HasRealNamedCallbackProperty(jsKey)) {
|
||||
jsVal = jsObj->Get(jsKey);
|
||||
|
||||
if (jsVal->IsObject()) {
|
||||
ALLOC_INIT_ZVAL(retval);
|
||||
Z_SET_REFCOUNT_P(retval, 0);
|
||||
} else {
|
||||
MAKE_STD_ZVAL(retval);
|
||||
}
|
||||
|
||||
if (v8js_to_zval(jsVal, retval, obj->flags, isolate TSRMLS_CC) == SUCCESS) {
|
||||
return retval;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ALLOC_INIT_ZVAL(retval);
|
||||
|
||||
return retval;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
static void php_v8js_v8_write_property(zval *object, zval *member, zval *value ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
|
||||
|
||||
if (!obj->ctx) {
|
||||
zend_throw_exception(php_ce_v8js_exception,
|
||||
"Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
|
||||
return;
|
||||
}
|
||||
|
||||
v8::Isolate *isolate = obj->ctx->isolate;
|
||||
v8::Locker locker(isolate);
|
||||
v8::Isolate::Scope isolate_scope(isolate);
|
||||
v8::HandleScope local_scope(isolate);
|
||||
v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
|
||||
v8::Context::Scope temp_scope(temp_context);
|
||||
|
||||
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
|
||||
|
||||
if (v8obj->IsObject() && !v8obj->IsFunction()) {
|
||||
v8obj->ToObject()->ForceSet(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)), zval_to_v8js(value, isolate TSRMLS_CC));
|
||||
}
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
static void php_v8js_v8_unset_property(zval *object, zval *member ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
|
||||
|
||||
if (!obj->ctx) {
|
||||
zend_throw_exception(php_ce_v8js_exception,
|
||||
"Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
|
||||
return;
|
||||
}
|
||||
|
||||
v8::Isolate *isolate = obj->ctx->isolate;
|
||||
v8::Locker locker(isolate);
|
||||
v8::Isolate::Scope isolate_scope(isolate);
|
||||
v8::HandleScope local_scope(isolate);
|
||||
v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
|
||||
v8::Context::Scope temp_scope(temp_context);
|
||||
|
||||
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
|
||||
|
||||
if (v8obj->IsObject() && !v8obj->IsFunction()) {
|
||||
v8obj->ToObject()->ForceDelete(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)));
|
||||
}
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
int php_v8js_v8_get_properties_hash(v8::Handle<v8::Value> jsValue, HashTable *retval, int flags, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
v8::Local<v8::Object> jsObj = jsValue->ToObject();
|
||||
|
||||
if (!jsObj.IsEmpty()) {
|
||||
v8::Local<v8::Array> jsKeys = jsObj->GetPropertyNames();
|
||||
|
||||
for (unsigned i = 0; i < jsKeys->Length(); i++)
|
||||
{
|
||||
v8::Local<v8::String> jsKey = jsKeys->Get(i)->ToString();
|
||||
|
||||
/* Skip any prototype properties */
|
||||
if (!jsObj->HasOwnProperty(jsKey) && !jsObj->HasRealNamedProperty(jsKey) && !jsObj->HasRealNamedCallbackProperty(jsKey)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> jsVal = jsObj->Get(jsKey);
|
||||
v8::String::Utf8Value cstr(jsKey);
|
||||
const char *key = ToCString(cstr);
|
||||
zval *value = NULL;
|
||||
|
||||
v8::Local<v8::Value> php_object;
|
||||
if (jsVal->IsObject()) {
|
||||
php_object = v8::Local<v8::Object>::Cast(jsVal)->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
|
||||
}
|
||||
if (!php_object.IsEmpty()) {
|
||||
/* This is a PHP object, passed to JS and back. */
|
||||
value = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
|
||||
Z_ADDREF_P(value);
|
||||
}
|
||||
else {
|
||||
MAKE_STD_ZVAL(value);
|
||||
|
||||
if (v8js_to_zval(jsVal, value, flags, isolate TSRMLS_CC) == FAILURE) {
|
||||
zval_ptr_dtor(&value);
|
||||
return FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
if ((flags & V8JS_FLAG_FORCE_ARRAY) || jsValue->IsArray()) {
|
||||
zend_symtable_update(retval, key, strlen(key) + 1, (void *)&value, sizeof(zval *), NULL);
|
||||
} else {
|
||||
zend_hash_update(retval, key, strlen(key) + 1, (void *) &value, sizeof(zval *), NULL);
|
||||
}
|
||||
}
|
||||
return SUCCESS;
|
||||
}
|
||||
return FAILURE;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
static HashTable *php_v8js_v8_get_properties(zval *object TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
|
||||
HashTable *retval;
|
||||
|
||||
if (obj->properties == NULL) {
|
||||
if (GC_G(gc_active)) {
|
||||
/* the garbage collector is running, don't create more zvals */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ALLOC_HASHTABLE(obj->properties);
|
||||
zend_hash_init(obj->properties, 0, NULL, ZVAL_PTR_DTOR, 0);
|
||||
|
||||
if (!obj->ctx) {
|
||||
/* Half-constructed object, probably due to unserialize call.
|
||||
* Just pass back properties hash so unserialize can write to
|
||||
* it (instead of crashing the engine). */
|
||||
return obj->properties;
|
||||
}
|
||||
} else {
|
||||
zend_hash_clean(obj->properties);
|
||||
}
|
||||
|
||||
if (!obj->ctx) {
|
||||
zend_throw_exception(php_ce_v8js_exception,
|
||||
"Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
v8::Isolate *isolate = obj->ctx->isolate;
|
||||
v8::Locker locker(isolate);
|
||||
v8::Isolate::Scope isolate_scope(isolate);
|
||||
v8::HandleScope local_scope(isolate);
|
||||
v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
|
||||
v8::Context::Scope temp_scope(temp_context);
|
||||
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
|
||||
|
||||
if (php_v8js_v8_get_properties_hash(v8obj, obj->properties, obj->flags, isolate TSRMLS_CC) == SUCCESS) {
|
||||
return obj->properties;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
static HashTable *php_v8js_v8_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
*is_temp = 0;
|
||||
return php_v8js_v8_get_properties(object TSRMLS_CC);
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
static zend_function *php_v8js_v8_get_method(zval **object_ptr, char *method, int method_len ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(*object_ptr TSRMLS_CC);
|
||||
zend_function *f;
|
||||
|
||||
if (!obj->ctx) {
|
||||
zend_throw_exception(php_ce_v8js_exception,
|
||||
"Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
v8::Isolate *isolate = obj->ctx->isolate;
|
||||
v8::Locker locker(isolate);
|
||||
v8::Isolate::Scope isolate_scope(isolate);
|
||||
v8::HandleScope local_scope(isolate);
|
||||
v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
|
||||
v8::Context::Scope temp_scope(temp_context);
|
||||
v8::Local<v8::String> jsKey = V8JS_STRL(method, method_len);
|
||||
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
|
||||
|
||||
if (!obj->v8obj.IsEmpty() && v8obj->IsObject() && !v8obj->IsFunction()) {
|
||||
v8::Local<v8::Object> jsObj = v8obj->ToObject();
|
||||
|
||||
if (jsObj->Has(jsKey) && jsObj->Get(jsKey)->IsFunction()) {
|
||||
f = (zend_function *) ecalloc(1, sizeof(*f));
|
||||
f->type = ZEND_OVERLOADED_FUNCTION_TEMPORARY;
|
||||
f->common.function_name = estrndup(method, method_len);
|
||||
return f;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
#if PHP_VERSION_ID >= 50400
|
||||
static int php_v8js_v8_call_method(const char *method, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
|
||||
#else
|
||||
static int php_v8js_v8_call_method(char *method, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
|
||||
#endif
|
||||
{
|
||||
zval *object = this_ptr, ***argv = NULL;
|
||||
int argc = ZEND_NUM_ARGS();
|
||||
php_v8js_object *obj;
|
||||
|
||||
obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
|
||||
|
||||
if (!obj->ctx) {
|
||||
zend_throw_exception(php_ce_v8js_exception,
|
||||
"Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
if (obj->v8obj.IsEmpty()) {
|
||||
zval_ptr_dtor(&object);
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
if (argc > 0) {
|
||||
argv = (zval***)safe_emalloc(sizeof(zval**), argc, 0);
|
||||
zend_get_parameters_array_ex(argc, argv);
|
||||
}
|
||||
|
||||
std::function< v8::Local<v8::Value>(v8::Isolate *) > v8_call = [obj, method, argc, argv TSRMLS_CC](v8::Isolate *isolate) {
|
||||
int i = 0;
|
||||
|
||||
v8::Local<v8::String> method_name = V8JS_SYML(method, strlen(method));
|
||||
v8::Local<v8::Object> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj)->ToObject();
|
||||
v8::Local<v8::Function> cb;
|
||||
|
||||
if (method_name->Equals(V8JS_SYM(V8JS_V8_INVOKE_FUNC_NAME))) {
|
||||
cb = v8::Local<v8::Function>::Cast(v8obj);
|
||||
} else {
|
||||
cb = v8::Local<v8::Function>::Cast(v8obj->Get(method_name));
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> *jsArgv = static_cast<v8::Local<v8::Value> *>(alloca(sizeof(v8::Local<v8::Value>) * argc));
|
||||
v8::Local<v8::Value> js_retval;
|
||||
|
||||
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 TSRMLS_CC));
|
||||
}
|
||||
|
||||
return cb->Call(V8JS_GLOBAL(isolate), argc, jsArgv);
|
||||
};
|
||||
|
||||
v8js_v8_call(obj->ctx, &return_value, obj->flags, obj->ctx->time_limit, obj->ctx->memory_limit, v8_call TSRMLS_CC);
|
||||
zval_ptr_dtor(&object);
|
||||
|
||||
if (argc > 0) {
|
||||
efree(argv);
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
static int php_v8js_v8_get_closure(zval *object, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zval **zobj_ptr TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
zend_function *invoke;
|
||||
|
||||
php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
|
||||
|
||||
if (!obj->ctx) {
|
||||
zend_throw_exception(php_ce_v8js_exception,
|
||||
"Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
v8::Isolate *isolate = obj->ctx->isolate;
|
||||
v8::Locker locker(isolate);
|
||||
v8::Isolate::Scope isolate_scope(isolate);
|
||||
v8::HandleScope local_scope(isolate);
|
||||
v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
|
||||
v8::Context::Scope temp_scope(temp_context);
|
||||
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
|
||||
|
||||
if (!v8obj->IsFunction()) {
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
invoke = (zend_function *) ecalloc(1, sizeof(*invoke));
|
||||
invoke->type = ZEND_OVERLOADED_FUNCTION_TEMPORARY;
|
||||
invoke->common.function_name = estrndup(V8JS_V8_INVOKE_FUNC_NAME, sizeof(V8JS_V8_INVOKE_FUNC_NAME) - 1);
|
||||
|
||||
*fptr_ptr = invoke;
|
||||
|
||||
if (zobj_ptr) {
|
||||
*zobj_ptr = object;
|
||||
}
|
||||
|
||||
*ce_ptr = NULL;
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
static void php_v8js_v8_free_storage(void *object, zend_object_handle handle TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
php_v8js_object *c = (php_v8js_object *) object;
|
||||
|
||||
if (c->properties) {
|
||||
zend_hash_destroy(c->properties);
|
||||
FREE_HASHTABLE(c->properties);
|
||||
c->properties = NULL;
|
||||
}
|
||||
|
||||
zend_object_std_dtor(&c->std TSRMLS_CC);
|
||||
|
||||
if(c->ctx) {
|
||||
c->v8obj.Reset();
|
||||
c->ctx->php_v8js_objects.remove(c);
|
||||
}
|
||||
|
||||
efree(object);
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
static zend_object_value php_v8js_v8_new(zend_class_entry *ce TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
zend_object_value retval;
|
||||
php_v8js_object *c;
|
||||
|
||||
c = (php_v8js_object *) ecalloc(1, sizeof(*c));
|
||||
|
||||
zend_object_std_init(&c->std, ce TSRMLS_CC);
|
||||
new(&c->v8obj) v8::Persistent<v8::Value>();
|
||||
|
||||
retval.handle = zend_objects_store_put(c, NULL, (zend_objects_free_object_storage_t) php_v8js_v8_free_storage, NULL TSRMLS_CC);
|
||||
retval.handlers = &v8_object_handlers;
|
||||
|
||||
return retval;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* NOTE: We could also override v8_object_handlers.get_constructor to throw
|
||||
* an exception when invoked, but doing so causes the half-constructed object
|
||||
* to leak -- this seems to be a PHP bug. So we'll define magic __construct
|
||||
* methods instead. */
|
||||
|
||||
/* {{{ proto V8Object::__construct()
|
||||
*/
|
||||
PHP_METHOD(V8Object,__construct)
|
||||
{
|
||||
zend_throw_exception(php_ce_v8js_exception,
|
||||
"Can't directly construct V8 objects!", 0 TSRMLS_CC);
|
||||
RETURN_FALSE;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ proto V8Object::__sleep()
|
||||
*/
|
||||
PHP_METHOD(V8Object, __sleep)
|
||||
{
|
||||
zend_throw_exception(php_ce_v8js_exception,
|
||||
"You cannot serialize or unserialize V8Object instances", 0 TSRMLS_CC);
|
||||
RETURN_FALSE;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ proto V8Object::__wakeup()
|
||||
*/
|
||||
PHP_METHOD(V8Object, __wakeup)
|
||||
{
|
||||
zend_throw_exception(php_ce_v8js_exception,
|
||||
"You cannot serialize or unserialize V8Object instances", 0 TSRMLS_CC);
|
||||
RETURN_FALSE;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ proto V8Function::__construct()
|
||||
*/
|
||||
PHP_METHOD(V8Function,__construct)
|
||||
{
|
||||
zend_throw_exception(php_ce_v8js_exception,
|
||||
"Can't directly construct V8 objects!", 0 TSRMLS_CC);
|
||||
RETURN_FALSE;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ proto V8Function::__sleep()
|
||||
*/
|
||||
PHP_METHOD(V8Function, __sleep)
|
||||
{
|
||||
zend_throw_exception(php_ce_v8js_exception,
|
||||
"You cannot serialize or unserialize V8Function instances", 0 TSRMLS_CC);
|
||||
RETURN_FALSE;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ proto V8Function::__wakeup()
|
||||
*/
|
||||
PHP_METHOD(V8Function, __wakeup)
|
||||
{
|
||||
zend_throw_exception(php_ce_v8js_exception,
|
||||
"You cannot serialize or unserialize V8Function instances", 0 TSRMLS_CC);
|
||||
RETURN_FALSE;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
void php_v8js_create_v8(zval *res, v8::Handle<v8::Value> value, int flags, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData(0);
|
||||
php_v8js_object *c;
|
||||
|
||||
object_init_ex(res, value->IsFunction() ? php_ce_v8_function : php_ce_v8_object);
|
||||
|
||||
c = (php_v8js_object *) zend_object_store_get_object(res TSRMLS_CC);
|
||||
|
||||
c->v8obj.Reset(isolate, value);
|
||||
c->flags = flags;
|
||||
c->ctx = ctx;
|
||||
c->properties = NULL;
|
||||
|
||||
ctx->php_v8js_objects.push_front(c);
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
|
||||
static const zend_function_entry v8_object_methods[] = { /* {{{ */
|
||||
PHP_ME(V8Object, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
|
||||
PHP_ME(V8Object, __sleep, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
|
||||
PHP_ME(V8Object, __wakeup, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
|
||||
{NULL, NULL, NULL}
|
||||
};
|
||||
/* }}} */
|
||||
|
||||
static const zend_function_entry v8_function_methods[] = { /* {{{ */
|
||||
PHP_ME(V8Function, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
|
||||
PHP_ME(V8Function, __sleep, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
|
||||
PHP_ME(V8Function, __wakeup, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
|
||||
{NULL, NULL, NULL}
|
||||
};
|
||||
/* }}} */
|
||||
|
||||
|
||||
PHP_MINIT_FUNCTION(v8js_v8object_class) /* {{{ */
|
||||
{
|
||||
zend_class_entry ce;
|
||||
|
||||
/* V8Object Class */
|
||||
INIT_CLASS_ENTRY(ce, "V8Object", v8_object_methods);
|
||||
php_ce_v8_object = zend_register_internal_class(&ce TSRMLS_CC);
|
||||
php_ce_v8_object->ce_flags |= ZEND_ACC_FINAL;
|
||||
php_ce_v8_object->create_object = php_v8js_v8_new;
|
||||
|
||||
/* V8Function Class */
|
||||
INIT_CLASS_ENTRY(ce, "V8Function", v8_function_methods);
|
||||
php_ce_v8_function = zend_register_internal_class(&ce TSRMLS_CC);
|
||||
php_ce_v8_function->ce_flags |= ZEND_ACC_FINAL;
|
||||
php_ce_v8_function->create_object = php_v8js_v8_new;
|
||||
|
||||
/* V8<Object|Function> handlers */
|
||||
memcpy(&v8_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
|
||||
v8_object_handlers.clone_obj = NULL;
|
||||
v8_object_handlers.cast_object = NULL;
|
||||
v8_object_handlers.get_property_ptr_ptr = NULL;
|
||||
v8_object_handlers.has_property = php_v8js_v8_has_property;
|
||||
v8_object_handlers.read_property = php_v8js_v8_read_property;
|
||||
v8_object_handlers.write_property = php_v8js_v8_write_property;
|
||||
v8_object_handlers.unset_property = php_v8js_v8_unset_property;
|
||||
v8_object_handlers.get_properties = php_v8js_v8_get_properties;
|
||||
v8_object_handlers.get_method = php_v8js_v8_get_method;
|
||||
v8_object_handlers.call_method = php_v8js_v8_call_method;
|
||||
v8_object_handlers.get_debug_info = php_v8js_v8_get_debug_info;
|
||||
v8_object_handlers.get_closure = php_v8js_v8_get_closure;
|
||||
} /* }}} */
|
||||
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim600: noet sw=4 ts=4 fdm=marker
|
||||
* vim<600: noet sw=4 ts=4
|
||||
*/
|
31
v8js_v8object_class.h
Normal file
31
v8js_v8object_class.h
Normal file
@ -0,0 +1,31 @@
|
||||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| PHP Version 5 |
|
||||
+----------------------------------------------------------------------+
|
||||
| Copyright (c) 1997-2013 The PHP Group |
|
||||
+----------------------------------------------------------------------+
|
||||
| http://www.opensource.org/licenses/mit-license.php MIT License |
|
||||
+----------------------------------------------------------------------+
|
||||
| Author: Jani Taskinen <jani.taskinen@iki.fi> |
|
||||
| Author: Patrick Reilly <preilly@php.net> |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
#ifndef V8JS_V8OBJECT_CLASS_H
|
||||
#define V8JS_V8OBJECT_CLASS_H
|
||||
|
||||
extern zend_class_entry *php_ce_v8_object;
|
||||
extern zend_class_entry *php_ce_v8_function;
|
||||
|
||||
PHP_MINIT_FUNCTION(v8js_v8object_class);
|
||||
|
||||
#endif /* V8JS_V8OBJECT_CLASS_H */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim600: noet sw=4 ts=4 fdm=marker
|
||||
* vim<600: noet sw=4 ts=4
|
||||
*/
|
Loading…
Reference in New Issue
Block a user