2014-12-12 22:59:28 +00:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2017-03-11 11:33:49 +00:00
|
|
|
| PHP Version 7 |
|
2014-12-12 22:59:28 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2017-03-11 09:17:10 +00:00
|
|
|
| Copyright (c) 1997-2017 The PHP Group |
|
2014-12-12 22:59:28 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| http://www.opensource.org/licenses/mit-license.php MIT License |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Author: Jani Taskinen <jani.taskinen@iki.fi> |
|
|
|
|
| Author: Patrick Reilly <preilly@php.net> |
|
2015-08-23 13:09:21 +00:00
|
|
|
| Author: Stefan Siegl <stesie@php.net> |
|
2014-12-12 22:59:28 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2016-01-06 09:01:35 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "php_v8js_macros.h"
|
|
|
|
#include "v8js_v8.h"
|
|
|
|
#include "v8js_exceptions.h"
|
|
|
|
#include "v8js_v8object_class.h"
|
2016-03-05 21:21:42 +00:00
|
|
|
#include "v8js_object_export.h"
|
2016-01-06 09:01:35 +00:00
|
|
|
#include "v8js_timer.h"
|
|
|
|
|
2014-12-12 22:59:28 +00:00
|
|
|
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"
|
2023-02-18 13:18:36 +00:00
|
|
|
#include "zend_attributes.h"
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define PHP_V8JS_SCRIPT_RES_NAME "V8Js script"
|
|
|
|
|
|
|
|
/* {{{ Class Entries */
|
|
|
|
static zend_class_entry *php_ce_v8js;
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ Object Handlers */
|
|
|
|
static zend_object_handlers v8js_object_handlers;
|
|
|
|
/* }}} */
|
|
|
|
|
2016-01-09 18:02:58 +00:00
|
|
|
/* Forward declare v8js_methods, actually "static" but not possible in C++ */
|
|
|
|
extern const zend_function_entry v8js_methods[];
|
|
|
|
|
2014-12-13 00:30:23 +00:00
|
|
|
typedef struct _v8js_script {
|
|
|
|
char *name;
|
2015-08-02 21:35:47 +00:00
|
|
|
v8js_ctx *ctx;
|
2024-09-27 21:27:59 +00:00
|
|
|
v8::Global<v8::Script> *script;
|
2014-12-13 00:30:23 +00:00
|
|
|
} v8js_script;
|
|
|
|
|
2015-08-02 20:36:23 +00:00
|
|
|
static void v8js_script_free(v8js_script *res);
|
2014-12-13 00:30:23 +00:00
|
|
|
|
2014-12-12 22:59:28 +00:00
|
|
|
int le_v8js_script;
|
|
|
|
|
2017-05-29 19:41:07 +00:00
|
|
|
#ifdef USE_INTERNAL_ALLOCATOR
|
2015-07-25 13:34:19 +00:00
|
|
|
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
|
|
|
|
public:
|
|
|
|
virtual void* Allocate(size_t length) {
|
|
|
|
void* data = AllocateUninitialized(length);
|
|
|
|
return data == NULL ? data : memset(data, 0, length);
|
|
|
|
}
|
|
|
|
virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
|
|
|
|
virtual void Free(void* data, size_t) { free(data); }
|
|
|
|
};
|
2017-05-29 19:41:07 +00:00
|
|
|
#endif /** USE_INTERNAL_ALLOCATOR */
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2015-08-24 20:19:15 +00:00
|
|
|
|
2017-03-11 12:35:21 +00:00
|
|
|
static void v8js_free_storage(zend_object *object) /* {{{ */
|
2014-12-12 22:59:28 +00:00
|
|
|
{
|
2015-08-24 22:24:04 +00:00
|
|
|
v8js_ctx *c = v8js_ctx_fetch_object(object);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2017-03-11 12:35:21 +00:00
|
|
|
zend_object_std_dtor(&c->std);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-08-11 23:02:56 +00:00
|
|
|
zval_ptr_dtor(&c->module_normaliser);
|
|
|
|
zval_ptr_dtor(&c->module_loader);
|
2022-06-24 10:04:11 +00:00
|
|
|
zval_ptr_dtor(&c->exception_filter);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
|
|
|
/* Delete PHP global object from JavaScript */
|
|
|
|
if (!c->context.IsEmpty()) {
|
|
|
|
v8::Locker locker(c->isolate);
|
|
|
|
v8::Isolate::Scope isolate_scope(c->isolate);
|
|
|
|
v8::HandleScope handle_scope(c->isolate);
|
|
|
|
v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(c->isolate, c->context);
|
|
|
|
v8::Context::Scope context_scope(v8_context);
|
|
|
|
v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(c->isolate, c->object_name);
|
2018-09-13 12:01:15 +00:00
|
|
|
V8JS_GLOBAL(c->isolate)->Delete(v8_context, object_name_js);
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c->object_name.Reset();
|
2024-09-27 21:27:59 +00:00
|
|
|
c->object_name.~Global();
|
2014-12-12 22:59:28 +00:00
|
|
|
c->global_template.Reset();
|
2024-09-27 21:27:59 +00:00
|
|
|
c->global_template.~Global();
|
2015-09-01 13:53:21 +00:00
|
|
|
c->array_tmpl.Reset();
|
2024-09-27 21:27:59 +00:00
|
|
|
c->array_tmpl.~Global();
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2015-08-27 12:46:27 +00:00
|
|
|
/* Clear persistent call_impl & method_tmpls templates */
|
2016-07-09 09:31:15 +00:00
|
|
|
for (std::map<v8js_function_tmpl_t *, v8js_function_tmpl_t>::iterator it = c->call_impls.begin();
|
2015-08-27 12:46:27 +00:00
|
|
|
it != c->call_impls.end(); ++it) {
|
|
|
|
// No need to free it->first, as it is stored in c->template_cache and freed below
|
|
|
|
it->second.Reset();
|
|
|
|
}
|
|
|
|
c->call_impls.~map();
|
|
|
|
|
2019-06-22 17:13:45 +00:00
|
|
|
for (std::map<std::pair<zend_class_entry *, zend_function *>, v8js_function_tmpl_t>::iterator it = c->method_tmpls.begin();
|
2015-08-27 12:46:27 +00:00
|
|
|
it != c->method_tmpls.end(); ++it) {
|
|
|
|
it->second.Reset();
|
|
|
|
}
|
|
|
|
c->method_tmpls.~map();
|
2014-12-12 22:59:28 +00:00
|
|
|
|
|
|
|
/* Clear persistent handles in template cache */
|
2016-07-09 09:31:15 +00:00
|
|
|
for (std::map<const zend_string *,v8js_function_tmpl_t>::iterator it = c->template_cache.begin();
|
2014-12-12 22:59:28 +00:00
|
|
|
it != c->template_cache.end(); ++it) {
|
|
|
|
it->second.Reset();
|
|
|
|
}
|
|
|
|
c->template_cache.~map();
|
|
|
|
|
|
|
|
/* Clear contexts */
|
2014-12-13 00:15:29 +00:00
|
|
|
for (std::vector<v8js_accessor_ctx*>::iterator it = c->accessor_list.begin();
|
2014-12-12 22:59:28 +00:00
|
|
|
it != c->accessor_list.end(); ++it) {
|
2017-03-11 12:35:21 +00:00
|
|
|
v8js_accessor_ctx_dtor(*it);
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
c->accessor_list.~vector();
|
|
|
|
|
|
|
|
/* Clear global object, dispose context */
|
|
|
|
if (!c->context.IsEmpty()) {
|
|
|
|
c->context.Reset();
|
|
|
|
}
|
2024-09-27 21:27:59 +00:00
|
|
|
c->context.~Global();
|
2014-12-12 22:59:28 +00:00
|
|
|
|
|
|
|
/* Dispose yet undisposed weak refs */
|
2015-08-24 20:05:27 +00:00
|
|
|
for (std::map<zend_object *, v8js_persistent_obj_t>::iterator it = c->weak_objects.begin();
|
2014-12-12 22:59:28 +00:00
|
|
|
it != c->weak_objects.end(); ++it) {
|
2015-08-24 20:05:27 +00:00
|
|
|
zend_object *object = it->first;
|
|
|
|
zval value;
|
|
|
|
ZVAL_OBJ(&value, object);
|
2016-08-11 23:02:56 +00:00
|
|
|
zval_ptr_dtor(&value);
|
2016-03-08 08:28:16 +00:00
|
|
|
c->isolate->AdjustAmountOfExternalAllocatedMemory(-c->average_object_size);
|
2014-12-12 22:59:28 +00:00
|
|
|
it->second.Reset();
|
|
|
|
}
|
|
|
|
c->weak_objects.~map();
|
|
|
|
|
2016-07-09 09:31:15 +00:00
|
|
|
for (std::map<v8js_function_tmpl_t *, v8js_persistent_obj_t>::iterator it = c->weak_closures.begin();
|
2014-12-12 22:59:28 +00:00
|
|
|
it != c->weak_closures.end(); ++it) {
|
2016-07-09 09:31:15 +00:00
|
|
|
v8js_function_tmpl_t *persist_tpl_ = it->first;
|
2014-12-12 22:59:28 +00:00
|
|
|
persist_tpl_->Reset();
|
|
|
|
delete persist_tpl_;
|
|
|
|
it->second.Reset();
|
|
|
|
}
|
|
|
|
c->weak_closures.~map();
|
|
|
|
|
2014-12-12 23:13:03 +00:00
|
|
|
for (std::list<v8js_v8object *>::iterator it = c->v8js_v8objects.begin();
|
|
|
|
it != c->v8js_v8objects.end(); it ++) {
|
2014-12-12 22:59:28 +00:00
|
|
|
(*it)->v8obj.Reset();
|
|
|
|
(*it)->ctx = NULL;
|
|
|
|
}
|
2014-12-12 23:13:03 +00:00
|
|
|
c->v8js_v8objects.~list();
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2015-08-02 21:35:47 +00:00
|
|
|
for (std::vector<v8js_script *>::iterator it = c->script_objects.begin();
|
|
|
|
it != c->script_objects.end(); it ++) {
|
|
|
|
(*it)->ctx = NULL;
|
|
|
|
(*it)->script->Reset();
|
|
|
|
}
|
|
|
|
c->script_objects.~vector();
|
|
|
|
|
2014-12-12 22:59:28 +00:00
|
|
|
/* Clear persistent handles in module cache */
|
2017-11-12 15:25:59 +00:00
|
|
|
for (std::map<char *, v8js_persistent_value_t>::iterator it = c->modules_loaded.begin();
|
2014-12-12 22:59:28 +00:00
|
|
|
it != c->modules_loaded.end(); ++it) {
|
2015-08-01 15:49:11 +00:00
|
|
|
efree(it->first);
|
2014-12-12 22:59:28 +00:00
|
|
|
it->second.Reset();
|
|
|
|
}
|
|
|
|
c->modules_loaded.~map();
|
|
|
|
|
|
|
|
if(c->isolate) {
|
|
|
|
/* c->isolate is initialized by V8Js::__construct, but __wakeup calls
|
|
|
|
* are not fully constructed and hence this would cause a NPE. */
|
|
|
|
c->isolate->Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(c->tz != NULL) {
|
|
|
|
free(c->tz);
|
|
|
|
}
|
|
|
|
|
|
|
|
c->modules_stack.~vector();
|
|
|
|
|
2016-08-11 23:02:56 +00:00
|
|
|
zval_ptr_dtor(&c->zval_snapshot_blob);
|
2017-05-29 19:41:07 +00:00
|
|
|
|
|
|
|
#ifndef USE_INTERNAL_ALLOCATOR
|
|
|
|
delete c->create_params.array_buffer_allocator;
|
|
|
|
#endif
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2017-03-11 12:35:21 +00:00
|
|
|
static zend_object* v8js_new(zend_class_entry *ce) /* {{{ */
|
2014-12-12 22:59:28 +00:00
|
|
|
{
|
2014-12-13 00:15:29 +00:00
|
|
|
v8js_ctx *c;
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2015-08-24 20:19:15 +00:00
|
|
|
c = (v8js_ctx *) ecalloc(1, sizeof(*c) + zend_object_properties_size(ce));
|
2017-03-11 12:35:21 +00:00
|
|
|
zend_object_std_init(&c->std, ce);
|
2015-09-03 21:12:19 +00:00
|
|
|
object_properties_init(&c->std, ce);
|
|
|
|
|
2015-08-29 16:52:30 +00:00
|
|
|
c->std.handlers = &v8js_object_handlers;
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2024-09-27 21:27:59 +00:00
|
|
|
new(&c->object_name) v8::Global<v8::String>();
|
|
|
|
new(&c->context) v8::Global<v8::Context>();
|
|
|
|
new(&c->global_template) v8::Global<v8::FunctionTemplate>();
|
|
|
|
new(&c->array_tmpl) v8::Global<v8::FunctionTemplate>();
|
2014-12-12 22:59:28 +00:00
|
|
|
|
|
|
|
new(&c->modules_stack) std::vector<char*>();
|
2017-11-12 15:25:59 +00:00
|
|
|
new(&c->modules_loaded) std::map<char *, v8js_persistent_value_t, cmp_str>;
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-07-09 09:31:15 +00:00
|
|
|
new(&c->template_cache) std::map<const zend_string *,v8js_function_tmpl_t>();
|
2014-12-13 00:15:29 +00:00
|
|
|
new(&c->accessor_list) std::vector<v8js_accessor_ctx *>();
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-07-09 09:31:15 +00:00
|
|
|
new(&c->weak_closures) std::map<v8js_function_tmpl_t *, v8js_persistent_obj_t>();
|
2015-08-24 20:05:27 +00:00
|
|
|
new(&c->weak_objects) std::map<zend_object *, v8js_persistent_obj_t>();
|
2016-07-09 09:31:15 +00:00
|
|
|
new(&c->call_impls) std::map<v8js_function_tmpl_t *, v8js_function_tmpl_t>();
|
2019-06-22 17:13:45 +00:00
|
|
|
new(&c->method_tmpls) std::map<std::pair<zend_class_entry *, zend_function *>, v8js_function_tmpl_t>();
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2014-12-12 23:13:03 +00:00
|
|
|
new(&c->v8js_v8objects) std::list<v8js_v8object *>();
|
2015-08-02 21:35:47 +00:00
|
|
|
new(&c->script_objects) std::vector<v8js_script *>();
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2015-08-24 20:19:15 +00:00
|
|
|
// @fixme following is const, run on startup
|
|
|
|
v8js_object_handlers.offset = XtOffsetOf(struct v8js_ctx, std);
|
|
|
|
v8js_object_handlers.free_obj = v8js_free_storage;
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-05-22 20:30:19 +00:00
|
|
|
c->average_object_size = 1024;
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2015-08-24 20:19:15 +00:00
|
|
|
return &c->std;
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-12-13 00:30:23 +00:00
|
|
|
static void v8js_fatal_error_handler(const char *location, const char *message) /* {{{ */
|
2014-12-12 22:59:28 +00:00
|
|
|
{
|
|
|
|
if (location) {
|
2015-12-30 16:53:46 +00:00
|
|
|
zend_error(E_WARNING, "Fatal V8 error in %s: %s", location, message);
|
2014-12-12 22:59:28 +00:00
|
|
|
} else {
|
2015-12-30 16:53:46 +00:00
|
|
|
zend_error(E_WARNING, "Fatal V8 error: %s", message);
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2016-01-08 14:42:43 +00:00
|
|
|
#define IS_MAGIC_FUNC(mname) \
|
2016-03-05 21:21:42 +00:00
|
|
|
((ZSTR_LEN(key) == sizeof(mname) - 1) && \
|
|
|
|
!strncasecmp(ZSTR_VAL(key), mname, ZSTR_LEN(key)))
|
2016-01-08 14:42:43 +00:00
|
|
|
|
2022-05-31 07:53:09 +00:00
|
|
|
/* {{{ proto void V8Js::__construct([string object_name [, array variables [, string snapshot_blob]]])
|
2014-12-12 22:59:28 +00:00
|
|
|
__construct for V8Js */
|
|
|
|
static PHP_METHOD(V8Js, __construct)
|
|
|
|
{
|
2015-10-02 20:29:54 +00:00
|
|
|
zend_string *object_name = NULL;
|
2022-05-31 07:35:09 +00:00
|
|
|
zval *vars_arr = NULL;
|
2016-03-05 15:54:06 +00:00
|
|
|
zval *snapshot_blob = NULL;
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2015-08-24 22:24:04 +00:00
|
|
|
v8js_ctx *c = Z_V8JS_CTX_OBJ_P(getThis())
|
2014-12-12 22:59:28 +00:00
|
|
|
|
|
|
|
if (!c->context.IsEmpty()) {
|
|
|
|
/* called __construct() twice, bail out */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-31 07:53:09 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S!az", &object_name, &vars_arr, &snapshot_blob) == FAILURE) {
|
2014-12-12 22:59:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize V8 */
|
2017-03-11 12:45:23 +00:00
|
|
|
v8js_v8_init();
|
2014-12-12 22:59:28 +00:00
|
|
|
|
|
|
|
/* Throw PHP exception if uncaught exceptions exist */
|
|
|
|
c->in_execution = 0;
|
2015-07-25 13:34:19 +00:00
|
|
|
|
2016-03-05 13:22:48 +00:00
|
|
|
new (&c->create_params) v8::Isolate::CreateParams();
|
|
|
|
|
2017-05-29 19:41:07 +00:00
|
|
|
#ifdef USE_INTERNAL_ALLOCATOR
|
2015-07-25 13:34:19 +00:00
|
|
|
static ArrayBufferAllocator array_buffer_allocator;
|
2016-02-28 16:00:47 +00:00
|
|
|
c->create_params.array_buffer_allocator = &array_buffer_allocator;
|
2017-05-29 19:41:07 +00:00
|
|
|
#else
|
|
|
|
c->create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
|
|
|
|
#endif
|
2016-02-28 16:00:47 +00:00
|
|
|
|
2016-02-28 19:29:53 +00:00
|
|
|
new (&c->snapshot_blob) v8::StartupData();
|
2016-03-05 15:54:06 +00:00
|
|
|
if (snapshot_blob) {
|
|
|
|
if (Z_TYPE_P(snapshot_blob) == IS_STRING) {
|
2016-03-05 21:21:42 +00:00
|
|
|
ZVAL_COPY(&c->zval_snapshot_blob, snapshot_blob);
|
2016-03-05 15:54:06 +00:00
|
|
|
|
2017-03-10 06:39:31 +00:00
|
|
|
if (Z_STRLEN_P(snapshot_blob) > std::numeric_limits<int>::max()) {
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
"Snapshot size exceeds maximum supported length", 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-05 15:54:06 +00:00
|
|
|
c->snapshot_blob.data = Z_STRVAL_P(snapshot_blob);
|
2017-03-10 06:39:31 +00:00
|
|
|
c->snapshot_blob.raw_size = static_cast<int>(Z_STRLEN_P(snapshot_blob));
|
2016-03-05 15:54:06 +00:00
|
|
|
c->create_params.snapshot_blob = &c->snapshot_blob;
|
|
|
|
} else {
|
2017-03-11 12:35:21 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Argument snapshot_blob expected to be of string type");
|
2016-03-05 15:54:06 +00:00
|
|
|
}
|
2016-02-28 16:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c->isolate = v8::Isolate::New(c->create_params);
|
2014-12-12 22:59:28 +00:00
|
|
|
c->isolate->SetData(0, c);
|
|
|
|
|
|
|
|
c->time_limit = 0;
|
|
|
|
c->time_limit_hit = false;
|
|
|
|
c->memory_limit = 0;
|
|
|
|
c->memory_limit_hit = false;
|
|
|
|
|
2015-12-31 19:25:24 +00:00
|
|
|
ZVAL_NULL(&c->module_normaliser);
|
2015-08-24 20:05:27 +00:00
|
|
|
ZVAL_NULL(&c->module_loader);
|
2022-06-24 10:04:11 +00:00
|
|
|
ZVAL_NULL(&c->exception_filter);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
|
|
|
// Isolate execution
|
|
|
|
v8::Isolate *isolate = c->isolate;
|
|
|
|
v8::Locker locker(isolate);
|
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
|
|
|
|
|
|
|
/* Handle scope */
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
|
|
|
|
/* Redirect fatal errors to PHP error handler */
|
2017-04-23 20:50:00 +00:00
|
|
|
isolate->SetFatalErrorHandler(v8js_fatal_error_handler);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
|
|
|
/* Create global template for global object */
|
|
|
|
// Now we are using multiple isolates this needs to be created for every context
|
|
|
|
|
2016-07-09 13:52:45 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(c->isolate);
|
|
|
|
c->global_template.Reset(isolate, global_template);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
|
|
|
/* Register builtin methods */
|
2016-07-09 13:52:45 +00:00
|
|
|
v8js_register_methods(global_template, c);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
|
|
|
/* Create context */
|
2022-05-31 07:35:09 +00:00
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate, nullptr, global_template);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2015-05-01 09:43:24 +00:00
|
|
|
if (context.IsEmpty()) {
|
2022-05-31 07:35:09 +00:00
|
|
|
zend_throw_exception(php_ce_v8js_exception, "Failed to create V8 context.", 0);
|
2014-12-12 22:59:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-01 09:43:24 +00:00
|
|
|
context->SetAlignedPointerInEmbedderData(1, c);
|
2016-07-09 13:52:45 +00:00
|
|
|
context->Global()->Set(context, V8JS_SYM("global"), context->Global());
|
2015-05-01 09:43:24 +00:00
|
|
|
c->context.Reset(isolate, context);
|
|
|
|
|
2014-12-12 22:59:28 +00:00
|
|
|
/* Enter context */
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
|
|
|
/* Create the PHP container object's function template */
|
|
|
|
v8::Local<v8::FunctionTemplate> php_obj_t = v8::FunctionTemplate::New(isolate, 0);
|
|
|
|
|
|
|
|
/* Set class name for PHP object */
|
2015-08-24 22:24:04 +00:00
|
|
|
zend_class_entry *ce = Z_OBJCE_P(getThis());
|
2017-03-10 06:39:31 +00:00
|
|
|
|
|
|
|
if (ZSTR_LEN(ce->name) > std::numeric_limits<int>::max()) {
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
"PHP object class name exceeds maximum supported length", 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
php_obj_t->SetClassName(V8JS_SYML(ZSTR_VAL(ce->name), static_cast<int>(ZSTR_LEN(ce->name))));
|
2014-12-12 22:59:28 +00:00
|
|
|
|
|
|
|
/* Register Get accessor for passed variables */
|
|
|
|
if (vars_arr && zend_hash_num_elements(Z_ARRVAL_P(vars_arr)) > 0) {
|
2017-03-11 12:35:21 +00:00
|
|
|
v8js_register_accessors(&c->accessor_list, php_obj_t, vars_arr, isolate);
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set name for the PHP JS object */
|
2017-03-10 06:39:31 +00:00
|
|
|
v8::Local<v8::String> object_name_js;
|
|
|
|
|
|
|
|
if (object_name && ZSTR_LEN(object_name)) {
|
|
|
|
if (ZSTR_LEN(object_name) > std::numeric_limits<int>::max()) {
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
"PHP JS object class name exceeds maximum supported length", 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-13 12:01:15 +00:00
|
|
|
object_name_js = V8JS_ZSYM(object_name);
|
2017-03-10 06:39:31 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
object_name_js = V8JS_SYM("PHP");
|
|
|
|
}
|
|
|
|
|
2014-12-12 22:59:28 +00:00
|
|
|
c->object_name.Reset(isolate, object_name_js);
|
|
|
|
|
|
|
|
/* Add the PHP object into global object */
|
2016-05-22 13:12:44 +00:00
|
|
|
php_obj_t->InstanceTemplate()->SetInternalFieldCount(2);
|
2018-09-13 12:01:15 +00:00
|
|
|
v8::Local<v8::Object> php_obj = php_obj_t->InstanceTemplate()->NewInstance(context).ToLocalChecked();
|
2017-04-23 20:50:00 +00:00
|
|
|
V8JS_GLOBAL(isolate)->DefineOwnProperty(context, object_name_js, php_obj, v8::ReadOnly);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
|
|
|
/* Export public property values */
|
2022-05-30 12:25:37 +00:00
|
|
|
HashTable *properties = zend_std_get_properties(Z_OBJ_P(getThis()));
|
2015-08-24 22:24:04 +00:00
|
|
|
zval *value;
|
|
|
|
zend_string *member;
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2015-09-03 21:12:19 +00:00
|
|
|
ZEND_HASH_FOREACH_STR_KEY(properties, member) {
|
2017-03-11 12:35:21 +00:00
|
|
|
zend_property_info *property_info = zend_get_property_info(c->std.ce, member, 1);
|
2015-08-30 00:08:38 +00:00
|
|
|
if(property_info &&
|
|
|
|
property_info != ZEND_WRONG_PROPERTY_INFO &&
|
2016-05-26 19:37:12 +00:00
|
|
|
(property_info->flags & ZEND_ACC_PUBLIC)) {
|
2017-03-10 06:39:31 +00:00
|
|
|
if (ZSTR_LEN(member) > std::numeric_limits<int>::max()) {
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
"Property name exceeds maximum supported length", 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-13 12:01:15 +00:00
|
|
|
v8::Local<v8::Name> key = V8JS_ZSYM(member);
|
2017-03-10 06:39:31 +00:00
|
|
|
|
2014-12-12 22:59:28 +00:00
|
|
|
/* Write value to PHP JS object */
|
2015-09-03 21:12:19 +00:00
|
|
|
value = OBJ_PROP(Z_OBJ_P(getThis()), property_info->offset);
|
2017-04-23 20:50:00 +00:00
|
|
|
php_obj->DefineOwnProperty(context, key, zval_to_v8js(value, isolate), v8::ReadOnly);
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
2015-09-03 21:12:19 +00:00
|
|
|
} ZEND_HASH_FOREACH_END();
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-01-08 14:42:43 +00:00
|
|
|
/* Add pointer to zend object */
|
2016-05-22 20:30:19 +00:00
|
|
|
php_obj->SetAlignedPointerInInternalField(1, Z_OBJ_P(getThis()));
|
2016-01-08 14:42:43 +00:00
|
|
|
|
|
|
|
/* Export public methods */
|
2016-03-05 21:21:42 +00:00
|
|
|
void *ptr;
|
|
|
|
zend_string *key;
|
2016-01-08 14:42:43 +00:00
|
|
|
|
2016-03-05 21:21:42 +00:00
|
|
|
ZEND_HASH_FOREACH_STR_KEY_PTR(&c->std.ce->function_table, key, ptr) {
|
|
|
|
zend_function *method_ptr = reinterpret_cast<zend_function *>(ptr);
|
2016-01-08 14:42:43 +00:00
|
|
|
|
|
|
|
if ((method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) == 0) {
|
|
|
|
/* Allow only public methods */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-06-13 16:14:12 +00:00
|
|
|
if ((method_ptr->common.fn_flags & (ZEND_ACC_CTOR|ZEND_ACC_DTOR)) != 0) {
|
2016-01-08 14:42:43 +00:00
|
|
|
/* no __construct, __destruct(), or __clone() functions */
|
|
|
|
continue;
|
|
|
|
}
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-01-08 14:42:43 +00:00
|
|
|
/* hide (do not export) other PHP magic functions */
|
|
|
|
if (IS_MAGIC_FUNC(ZEND_CALLSTATIC_FUNC_NAME) ||
|
|
|
|
IS_MAGIC_FUNC(ZEND_SLEEP_FUNC_NAME) ||
|
|
|
|
IS_MAGIC_FUNC(ZEND_WAKEUP_FUNC_NAME) ||
|
|
|
|
IS_MAGIC_FUNC(ZEND_SET_STATE_FUNC_NAME) ||
|
|
|
|
IS_MAGIC_FUNC(ZEND_GET_FUNC_NAME) ||
|
|
|
|
IS_MAGIC_FUNC(ZEND_SET_FUNC_NAME) ||
|
|
|
|
IS_MAGIC_FUNC(ZEND_UNSET_FUNC_NAME) ||
|
|
|
|
IS_MAGIC_FUNC(ZEND_CALL_FUNC_NAME) ||
|
|
|
|
IS_MAGIC_FUNC(ZEND_INVOKE_FUNC_NAME) ||
|
|
|
|
IS_MAGIC_FUNC(ZEND_TOSTRING_FUNC_NAME) ||
|
|
|
|
IS_MAGIC_FUNC(ZEND_ISSET_FUNC_NAME)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-01-09 18:02:58 +00:00
|
|
|
const zend_function_entry *fe;
|
|
|
|
for (fe = v8js_methods; fe->fname; fe ++) {
|
2016-03-05 21:21:42 +00:00
|
|
|
if (strcmp(fe->fname, ZSTR_VAL(method_ptr->common.function_name)) == 0) {
|
2016-01-09 18:02:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fe->fname) {
|
|
|
|
/* Method belongs to \V8Js class itself, never export to V8, even if
|
|
|
|
* it is overriden in a derived class. */
|
|
|
|
continue;
|
|
|
|
}
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2017-03-10 06:39:31 +00:00
|
|
|
if (ZSTR_LEN(method_ptr->common.function_name) > std::numeric_limits<int>::max()) {
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
"Method name exceeds maximum supported length", 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-13 12:01:15 +00:00
|
|
|
v8::Local<v8::String> method_name = V8JS_ZSYM(method_ptr->common.function_name);
|
2016-01-08 14:42:43 +00:00
|
|
|
v8::Local<v8::FunctionTemplate> ft;
|
|
|
|
|
2018-09-13 12:01:15 +00:00
|
|
|
ft = v8::FunctionTemplate::New(isolate, v8js_php_callback,
|
|
|
|
v8::External::New((isolate), method_ptr));
|
|
|
|
// @fixme add/check Signature v8::Signature::New((isolate), tmpl));
|
2019-06-22 17:13:45 +00:00
|
|
|
v8js_function_tmpl_t *persistent_ft = &c->method_tmpls[std::make_pair(ce, method_ptr)];
|
2018-09-13 12:01:15 +00:00
|
|
|
persistent_ft->Reset(isolate, ft);
|
2016-01-08 14:42:43 +00:00
|
|
|
|
2018-09-13 12:01:15 +00:00
|
|
|
php_obj->CreateDataProperty(context, method_name, ft->GetFunction(context).ToLocalChecked());
|
2016-03-05 21:21:42 +00:00
|
|
|
} ZEND_HASH_FOREACH_END();
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto V8JS::__sleep()
|
|
|
|
*/
|
|
|
|
PHP_METHOD(V8Js, __sleep)
|
|
|
|
{
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
2017-03-11 12:35:21 +00:00
|
|
|
"You cannot serialize or unserialize V8Js instances", 0);
|
2014-12-12 22:59:28 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto V8JS::__wakeup()
|
|
|
|
*/
|
|
|
|
PHP_METHOD(V8Js, __wakeup)
|
|
|
|
{
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
2017-03-11 12:35:21 +00:00
|
|
|
"You cannot serialize or unserialize V8Js instances", 0);
|
2014-12-12 22:59:28 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2017-03-11 12:35:21 +00:00
|
|
|
static void v8js_compile_script(zval *this_ptr, const zend_string *str, const zend_string *identifier, v8js_script **ret)
|
2014-12-12 22:59:28 +00:00
|
|
|
{
|
2014-12-13 00:15:29 +00:00
|
|
|
v8js_script *res = NULL;
|
2014-12-12 22:59:28 +00:00
|
|
|
|
|
|
|
V8JS_BEGIN_CTX(c, this_ptr)
|
|
|
|
|
|
|
|
/* Catch JS exceptions */
|
2017-04-23 20:50:00 +00:00
|
|
|
v8::TryCatch try_catch(isolate);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
|
|
|
/* Set script identifier */
|
2017-03-10 06:39:31 +00:00
|
|
|
if (identifier && ZSTR_LEN(identifier) > std::numeric_limits<int>::max()) {
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
"Script identifier exceeds maximum supported length", 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Local<v8::String> sname = identifier
|
2018-09-13 12:01:15 +00:00
|
|
|
? V8JS_ZSTR(identifier)
|
2017-03-10 06:39:31 +00:00
|
|
|
: V8JS_SYM("V8Js::compileString()");
|
2024-09-28 16:09:43 +00:00
|
|
|
#if PHP_V8_API_VERSION >= 12002000
|
2024-09-27 22:20:44 +00:00
|
|
|
v8::ScriptOrigin origin(sname);
|
2024-09-28 16:09:43 +00:00
|
|
|
#else
|
|
|
|
v8::ScriptOrigin origin(c->isolate, sname);
|
|
|
|
#endif
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2017-03-10 06:39:31 +00:00
|
|
|
if (ZSTR_LEN(str) > std::numeric_limits<int>::max()) {
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
"Script source exceeds maximum supported length", 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-13 12:01:15 +00:00
|
|
|
v8::Local<v8::String> source = V8JS_ZSTR(str);
|
2018-09-12 13:43:40 +00:00
|
|
|
v8::MaybeLocal<v8::Script> script = v8::Script::Compile(v8::Local<v8::Context>::New(isolate, c->context), source, &origin);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
|
|
|
/* Compile errors? */
|
|
|
|
if (script.IsEmpty()) {
|
2017-03-11 12:35:21 +00:00
|
|
|
v8js_throw_script_exception(c->isolate, &try_catch);
|
2014-12-12 22:59:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-12-13 00:15:29 +00:00
|
|
|
res = (v8js_script *)emalloc(sizeof(v8js_script));
|
2024-09-27 21:27:59 +00:00
|
|
|
res->script = new v8::Global<v8::Script>(c->isolate, script.ToLocalChecked());
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2018-09-12 13:43:40 +00:00
|
|
|
v8::String::Utf8Value _sname(isolate, sname);
|
2014-12-12 22:59:28 +00:00
|
|
|
res->name = estrndup(ToCString(_sname), _sname.length());
|
2015-08-02 21:35:47 +00:00
|
|
|
res->ctx = c;
|
2014-12-12 22:59:28 +00:00
|
|
|
*ret = res;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-11 13:10:49 +00:00
|
|
|
static void v8js_execute_script(zval *this_ptr, v8js_script *res, long flags, long time_limit, size_t memory_limit, zval **return_value)
|
2014-12-12 22:59:28 +00:00
|
|
|
{
|
2015-08-24 22:24:04 +00:00
|
|
|
v8js_ctx *c = Z_V8JS_CTX_OBJ_P(this_ptr);
|
2015-08-02 21:35:47 +00:00
|
|
|
if (res->ctx != c) {
|
2014-12-12 22:59:28 +00:00
|
|
|
zend_error(E_WARNING, "Script resource from wrong V8Js object passed");
|
|
|
|
ZVAL_BOOL(*return_value, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!c->in_execution && time_limit == 0) {
|
|
|
|
time_limit = c->time_limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!c->in_execution && memory_limit == 0) {
|
|
|
|
memory_limit = c->memory_limit;
|
|
|
|
}
|
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
/* std::function relies on its dtor to be executed, otherwise it leaks
|
|
|
|
* some memory on bailout. */
|
|
|
|
{
|
2018-09-12 13:43:40 +00:00
|
|
|
std::function< v8::MaybeLocal<v8::Value>(v8::Isolate *) > v8_call = [c, res](v8::Isolate *isolate) {
|
2016-01-01 18:10:04 +00:00
|
|
|
v8::Local<v8::Script> script = v8::Local<v8::Script>::New(isolate, *res->script);
|
2018-09-12 13:43:40 +00:00
|
|
|
return script->Run(v8::Local<v8::Context>::New(isolate, c->context));
|
2016-01-01 18:10:04 +00:00
|
|
|
};
|
|
|
|
|
2017-03-11 12:35:21 +00:00
|
|
|
v8js_v8_call(c, return_value, flags, time_limit, memory_limit, v8_call);
|
2016-01-01 18:10:04 +00:00
|
|
|
}
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2016-01-01 18:10:04 +00:00
|
|
|
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();
|
|
|
|
}
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* {{{ proto mixed V8Js::executeString(string script [, string identifier [, int flags]])
|
|
|
|
*/
|
|
|
|
static PHP_METHOD(V8Js, executeString)
|
|
|
|
{
|
2015-08-29 18:19:04 +00:00
|
|
|
zend_string *str = NULL, *identifier = NULL;
|
2014-12-12 22:59:28 +00:00
|
|
|
long flags = V8JS_FLAG_NONE, time_limit = 0, memory_limit = 0;
|
2014-12-13 00:15:29 +00:00
|
|
|
v8js_script *res = NULL;
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2022-05-27 02:47:44 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|S!lll", &str, &identifier, &flags, &time_limit, &memory_limit) == FAILURE) {
|
2014-12-12 22:59:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-11 13:10:49 +00:00
|
|
|
if (memory_limit < 0) {
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
"memory_limit must not be negative", 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-11 12:35:21 +00:00
|
|
|
v8js_compile_script(getThis(), str, identifier, &res);
|
2014-12-12 22:59:28 +00:00
|
|
|
if (!res) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2015-12-31 23:01:49 +00:00
|
|
|
|
|
|
|
zend_try {
|
2017-03-11 13:10:49 +00:00
|
|
|
v8js_execute_script(getThis(), res, flags, time_limit, static_cast<size_t>(memory_limit), &return_value);
|
2015-12-31 23:01:49 +00:00
|
|
|
v8js_script_free(res);
|
|
|
|
}
|
|
|
|
zend_catch {
|
|
|
|
v8js_script_free(res);
|
|
|
|
zend_bailout();
|
|
|
|
}
|
|
|
|
zend_end_try()
|
|
|
|
|
2014-12-12 22:59:28 +00:00
|
|
|
efree(res);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
|
|
|
|
/* {{{ proto mixed V8Js::compileString(string script [, string identifier])
|
|
|
|
*/
|
|
|
|
static PHP_METHOD(V8Js, compileString)
|
|
|
|
{
|
2015-08-29 18:19:04 +00:00
|
|
|
zend_string *str = NULL, *identifier = NULL;
|
2014-12-13 00:15:29 +00:00
|
|
|
v8js_script *res = NULL;
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2017-03-11 12:35:21 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|S", &str, &identifier) == FAILURE) {
|
2014-12-12 22:59:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-11 12:35:21 +00:00
|
|
|
v8js_compile_script(getThis(), str, identifier, &res);
|
2014-12-12 22:59:28 +00:00
|
|
|
if (res) {
|
2015-08-29 19:56:29 +00:00
|
|
|
RETVAL_RES(zend_register_resource(res, le_v8js_script));
|
2015-08-02 21:35:47 +00:00
|
|
|
|
|
|
|
v8js_ctx *ctx;
|
2015-08-24 22:24:04 +00:00
|
|
|
ctx = Z_V8JS_CTX_OBJ_P(getThis());
|
2015-08-02 21:35:47 +00:00
|
|
|
ctx->script_objects.push_back(res);
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto mixed V8Js::executeScript(resource script [, int flags]])
|
|
|
|
*/
|
|
|
|
static PHP_METHOD(V8Js, executeScript)
|
|
|
|
{
|
|
|
|
long flags = V8JS_FLAG_NONE, time_limit = 0, memory_limit = 0;
|
|
|
|
zval *zscript;
|
2014-12-13 00:15:29 +00:00
|
|
|
v8js_script *res;
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2017-03-11 12:35:21 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|lll", &zscript, &flags, &time_limit, &memory_limit) == FAILURE) {
|
2014-12-12 22:59:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-11 13:10:49 +00:00
|
|
|
if (memory_limit < 0) {
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
"memory_limit must not be negative", 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-24 22:24:04 +00:00
|
|
|
if((res = (v8js_script *)zend_fetch_resource(Z_RES_P(zscript), PHP_V8JS_SCRIPT_RES_NAME, le_v8js_script)) == NULL) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2017-03-11 13:10:49 +00:00
|
|
|
v8js_execute_script(getThis(), res, flags, time_limit, static_cast<size_t>(memory_limit), &return_value);
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2015-12-06 12:55:13 +00:00
|
|
|
/* {{{ proto void V8Js::setModuleNormaliser(string base, string module_id)
|
|
|
|
*/
|
|
|
|
static PHP_METHOD(V8Js, setModuleNormaliser)
|
|
|
|
{
|
|
|
|
v8js_ctx *c;
|
|
|
|
zval *callable;
|
|
|
|
|
2017-03-11 12:35:21 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &callable) == FAILURE) {
|
2015-12-06 12:55:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-31 19:25:24 +00:00
|
|
|
c = Z_V8JS_CTX_OBJ_P(getThis());
|
|
|
|
ZVAL_COPY(&c->module_normaliser, callable);
|
2015-12-06 12:55:13 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-12-12 22:59:28 +00:00
|
|
|
/* {{{ proto void V8Js::setModuleLoader(string module)
|
|
|
|
*/
|
|
|
|
static PHP_METHOD(V8Js, setModuleLoader)
|
|
|
|
{
|
2014-12-13 00:15:29 +00:00
|
|
|
v8js_ctx *c;
|
2015-08-29 19:00:13 +00:00
|
|
|
zval *callable;
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2017-03-11 12:35:21 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &callable) == FAILURE) {
|
2014-12-12 22:59:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-24 22:24:04 +00:00
|
|
|
c = Z_V8JS_CTX_OBJ_P(getThis());
|
2015-08-29 19:00:13 +00:00
|
|
|
ZVAL_COPY(&c->module_loader, callable);
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2022-06-24 10:04:11 +00:00
|
|
|
/* {{{ proto void V8Js::setExceptionFilter(callable factory)
|
2022-05-31 12:21:32 +00:00
|
|
|
*/
|
2022-06-24 10:04:11 +00:00
|
|
|
static PHP_METHOD(V8Js, setExceptionFilter)
|
2022-05-31 12:21:32 +00:00
|
|
|
{
|
|
|
|
zval *callable;
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &callable) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
v8js_ctx *c = Z_V8JS_CTX_OBJ_P(getThis());
|
2022-06-24 10:04:11 +00:00
|
|
|
ZVAL_COPY(&c->exception_filter, callable);
|
2022-05-31 12:21:32 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-12-12 22:59:28 +00:00
|
|
|
/* {{{ proto void V8Js::setTimeLimit(int time_limit)
|
|
|
|
*/
|
|
|
|
static PHP_METHOD(V8Js, setTimeLimit)
|
|
|
|
{
|
2014-12-13 00:15:29 +00:00
|
|
|
v8js_ctx *c;
|
2014-12-12 22:59:28 +00:00
|
|
|
long time_limit = 0;
|
|
|
|
|
2017-03-11 12:35:21 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &time_limit) == FAILURE) {
|
2014-12-12 22:59:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-24 22:24:04 +00:00
|
|
|
c = Z_V8JS_CTX_OBJ_P(getThis());
|
2014-12-12 22:59:28 +00:00
|
|
|
c->time_limit = time_limit;
|
|
|
|
|
|
|
|
V8JSG(timer_mutex).lock();
|
2014-12-13 00:15:29 +00:00
|
|
|
for (std::deque< v8js_timer_ctx* >::iterator it = V8JSG(timer_stack).begin();
|
2014-12-12 22:59:28 +00:00
|
|
|
it != V8JSG(timer_stack).end(); it ++) {
|
2014-12-13 00:15:29 +00:00
|
|
|
if((*it)->ctx == c && !(*it)->killed) {
|
2014-12-12 22:59:28 +00:00
|
|
|
(*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();
|
|
|
|
|
|
|
|
if (c->in_execution && time_limit && !V8JSG(timer_thread)) {
|
|
|
|
/* If timer thread is not started already and we now impose a time limit
|
|
|
|
* finally install the timer. */
|
2015-09-28 20:03:24 +00:00
|
|
|
V8JSG(timer_thread) = new std::thread(v8js_timer_thread, ZEND_MODULE_GLOBALS_BULK(v8js));
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto void V8Js::setMemoryLimit(int memory_limit)
|
|
|
|
*/
|
|
|
|
static PHP_METHOD(V8Js, setMemoryLimit)
|
|
|
|
{
|
2014-12-13 00:15:29 +00:00
|
|
|
v8js_ctx *c;
|
2014-12-12 22:59:28 +00:00
|
|
|
long memory_limit = 0;
|
|
|
|
|
2017-03-11 12:35:21 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &memory_limit) == FAILURE) {
|
2014-12-12 22:59:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-11 13:10:49 +00:00
|
|
|
if (memory_limit < 0) {
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
"memory_limit must not be negative", 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-24 22:24:04 +00:00
|
|
|
c = Z_V8JS_CTX_OBJ_P(getThis());
|
2017-03-11 13:10:49 +00:00
|
|
|
c->memory_limit = static_cast<size_t>(memory_limit);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
|
|
|
V8JSG(timer_mutex).lock();
|
2014-12-13 00:15:29 +00:00
|
|
|
for (std::deque< v8js_timer_ctx* >::iterator it = V8JSG(timer_stack).begin();
|
2014-12-12 22:59:28 +00:00
|
|
|
it != V8JSG(timer_stack).end(); it ++) {
|
2014-12-13 00:15:29 +00:00
|
|
|
if((*it)->ctx == c && !(*it)->killed) {
|
2017-03-11 13:10:49 +00:00
|
|
|
(*it)->memory_limit = static_cast<size_t>(memory_limit);
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
V8JSG(timer_mutex).unlock();
|
|
|
|
|
|
|
|
if (c->in_execution && memory_limit && !V8JSG(timer_thread)) {
|
|
|
|
/* If timer thread is not started already and we now impose a memory limit
|
|
|
|
* finally install the timer. */
|
2015-09-28 20:03:24 +00:00
|
|
|
V8JSG(timer_thread) = new std::thread(v8js_timer_thread, ZEND_MODULE_GLOBALS_BULK(v8js));
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2016-03-08 08:28:16 +00:00
|
|
|
/* {{{ proto void V8Js::setAverageObjectSize(average_object_size)
|
|
|
|
*/
|
|
|
|
static PHP_METHOD(V8Js, setAverageObjectSize)
|
|
|
|
{
|
|
|
|
v8js_ctx *c;
|
|
|
|
long average_object_size = 0;
|
|
|
|
|
2017-03-11 12:35:21 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &average_object_size) == FAILURE) {
|
2016-03-08 08:28:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-22 20:30:19 +00:00
|
|
|
c = Z_V8JS_CTX_OBJ_P(getThis());
|
2016-03-08 08:28:16 +00:00
|
|
|
c->average_object_size = average_object_size;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2015-08-25 21:38:09 +00:00
|
|
|
static void v8js_persistent_zval_ctor(zval *p) /* {{{ */
|
2014-12-12 22:59:28 +00:00
|
|
|
{
|
2015-08-25 21:38:09 +00:00
|
|
|
assert(Z_TYPE_P(p) == IS_STRING);
|
|
|
|
Z_STR_P(p) = zend_string_dup(Z_STR_P(p), 1);
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2015-08-25 21:38:09 +00:00
|
|
|
static void v8js_persistent_zval_dtor(zval *p) /* {{{ */
|
2014-12-12 22:59:28 +00:00
|
|
|
{
|
2015-08-25 21:38:09 +00:00
|
|
|
assert(Z_TYPE_P(p) == IS_STRING);
|
2018-07-18 21:36:13 +00:00
|
|
|
|
|
|
|
if (!ZSTR_IS_INTERNED(Z_STR_P(p))) {
|
|
|
|
free(Z_STR_P(p));
|
|
|
|
}
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2015-08-02 20:36:23 +00:00
|
|
|
static void v8js_script_free(v8js_script *res)
|
2014-12-12 22:59:28 +00:00
|
|
|
{
|
2015-08-02 20:36:23 +00:00
|
|
|
efree(res->name);
|
|
|
|
delete res->script; // does Reset()
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
|
2015-08-25 21:38:09 +00:00
|
|
|
static void v8js_script_dtor(zend_resource *rsrc) /* {{{ */
|
2014-12-12 22:59:28 +00:00
|
|
|
{
|
2014-12-13 00:15:29 +00:00
|
|
|
v8js_script *res = (v8js_script *)rsrc->ptr;
|
2014-12-12 22:59:28 +00:00
|
|
|
if (res) {
|
2015-08-02 21:35:47 +00:00
|
|
|
if(res->ctx) {
|
|
|
|
std::vector<v8js_script *>::iterator it = std::find(res->ctx->script_objects.begin(), res->ctx->script_objects.end(), res);
|
|
|
|
res->ctx->script_objects.erase(it);
|
|
|
|
}
|
|
|
|
|
2015-08-02 20:36:23 +00:00
|
|
|
v8js_script_free(res);
|
2014-12-12 22:59:28 +00:00
|
|
|
efree(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2016-02-28 16:00:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* ## Static methods ## */
|
|
|
|
|
2018-09-12 13:43:40 +00:00
|
|
|
static v8::StartupData createSnapshotDataBlob(v8::SnapshotCreator *snapshot_creator, zend_string *str) /* {{{ */
|
|
|
|
{
|
|
|
|
v8::Isolate *isolate = snapshot_creator->GetIsolate();
|
|
|
|
|
|
|
|
{
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate);
|
|
|
|
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
v8::TryCatch try_catch(isolate);
|
|
|
|
|
2018-09-13 12:01:15 +00:00
|
|
|
v8::Local<v8::String> source = V8JS_ZSTR(str);
|
2018-09-12 13:43:40 +00:00
|
|
|
v8::MaybeLocal<v8::Script> script = v8::Script::Compile(context, source);
|
|
|
|
|
|
|
|
if (script.IsEmpty() || script.ToLocalChecked()->Run(context).IsEmpty())
|
|
|
|
{
|
|
|
|
return {nullptr, 0};
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshot_creator->SetDefaultContext(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
return snapshot_creator->CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear);
|
|
|
|
} /* }}} */
|
|
|
|
|
2016-05-11 14:50:37 +00:00
|
|
|
|
2016-02-28 16:00:47 +00:00
|
|
|
/* {{{ proto string|bool V8Js::createSnapshot(string embed_source)
|
|
|
|
*/
|
|
|
|
static PHP_METHOD(V8Js, createSnapshot)
|
|
|
|
{
|
2016-03-05 21:43:57 +00:00
|
|
|
zend_string *script;
|
2016-02-28 16:00:47 +00:00
|
|
|
|
2017-03-11 12:35:21 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &script) == FAILURE) {
|
2016-02-28 16:00:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-05 21:43:57 +00:00
|
|
|
if (!ZSTR_LEN(script)) {
|
2017-03-11 12:35:21 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Script cannot be empty");
|
2016-02-28 16:00:47 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize V8, if not already done. */
|
2017-03-11 12:45:23 +00:00
|
|
|
v8js_v8_init();
|
2016-02-28 16:00:47 +00:00
|
|
|
|
2018-09-12 13:43:40 +00:00
|
|
|
v8::Isolate *isolate = v8::Isolate::Allocate();
|
|
|
|
v8::SnapshotCreator snapshot_creator(isolate);
|
|
|
|
v8::StartupData snapshot_blob = createSnapshotDataBlob(&snapshot_creator, script);
|
2016-02-28 16:00:47 +00:00
|
|
|
|
|
|
|
if (!snapshot_blob.data) {
|
2017-03-11 12:35:21 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Failed to create V8 heap snapshot. Check $embed_source for errors.");
|
2016-02-28 16:00:47 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2016-03-05 21:21:42 +00:00
|
|
|
RETVAL_STRINGL(snapshot_blob.data, snapshot_blob.raw_size);
|
2016-02-28 16:00:47 +00:00
|
|
|
delete[] snapshot_blob.data;
|
|
|
|
}
|
|
|
|
/* }}} */
|
2016-02-28 16:54:00 +00:00
|
|
|
|
2016-02-28 16:00:47 +00:00
|
|
|
|
2014-12-12 22:59:28 +00:00
|
|
|
/* {{{ arginfo */
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_construct, 0, 0, 0)
|
|
|
|
ZEND_ARG_INFO(0, object_name)
|
|
|
|
ZEND_ARG_INFO(0, variables)
|
2016-03-05 13:24:08 +00:00
|
|
|
ZEND_ARG_INFO(0, snapshot_blob)
|
2014-12-12 22:59:28 +00:00
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO(arginfo_v8js_sleep, 0)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO(arginfo_v8js_wakeup, 0)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_executestring, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, script)
|
|
|
|
ZEND_ARG_INFO(0, identifier)
|
|
|
|
ZEND_ARG_INFO(0, flags)
|
|
|
|
ZEND_ARG_INFO(0, time_limit)
|
|
|
|
ZEND_ARG_INFO(0, memory_limit)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_compilestring, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, script)
|
|
|
|
ZEND_ARG_INFO(0, identifier)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_executescript, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, script)
|
|
|
|
ZEND_ARG_INFO(0, flags)
|
|
|
|
ZEND_ARG_INFO(0, time_limit)
|
|
|
|
ZEND_ARG_INFO(0, memory_limit)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_checkstring, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, script)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
2015-12-06 12:55:13 +00:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setmodulenormaliser, 0, 0, 2)
|
|
|
|
ZEND_ARG_INFO(0, base)
|
|
|
|
ZEND_ARG_INFO(0, module_id)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
2014-12-12 22:59:28 +00:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setmoduleloader, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, callable)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
2022-06-24 10:04:11 +00:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setexceptionfilter, 0, 0, 1)
|
2022-05-31 12:21:32 +00:00
|
|
|
ZEND_ARG_INFO(0, callable)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
2016-03-08 08:28:16 +00:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setaverageobjectsize, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, average_object_size)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
2016-02-28 16:00:47 +00:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_createsnapshot, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, script)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
2014-12-12 22:59:28 +00:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_settimelimit, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, time_limit)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setmemorylimit, 0, 0, 1)
|
|
|
|
ZEND_ARG_INFO(0, memory_limit)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
|
|
|
|
|
2016-01-09 18:02:58 +00:00
|
|
|
const zend_function_entry v8js_methods[] = { /* {{{ */
|
2014-12-12 22:59:28 +00:00
|
|
|
PHP_ME(V8Js, __construct, arginfo_v8js_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
|
|
|
|
PHP_ME(V8Js, __sleep, arginfo_v8js_sleep, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
|
2016-05-26 19:36:54 +00:00
|
|
|
PHP_ME(V8Js, __wakeup, arginfo_v8js_wakeup, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
|
2014-12-12 22:59:28 +00:00
|
|
|
PHP_ME(V8Js, executeString, arginfo_v8js_executestring, ZEND_ACC_PUBLIC)
|
|
|
|
PHP_ME(V8Js, compileString, arginfo_v8js_compilestring, ZEND_ACC_PUBLIC)
|
|
|
|
PHP_ME(V8Js, executeScript, arginfo_v8js_executescript, ZEND_ACC_PUBLIC)
|
2015-12-06 12:55:13 +00:00
|
|
|
PHP_ME(V8Js, setModuleNormaliser, arginfo_v8js_setmodulenormaliser, ZEND_ACC_PUBLIC)
|
2014-12-12 22:59:28 +00:00
|
|
|
PHP_ME(V8Js, setModuleLoader, arginfo_v8js_setmoduleloader, ZEND_ACC_PUBLIC)
|
2022-06-24 10:04:11 +00:00
|
|
|
PHP_ME(V8Js, setExceptionFilter, arginfo_v8js_setexceptionfilter, ZEND_ACC_PUBLIC)
|
2014-12-12 22:59:28 +00:00
|
|
|
PHP_ME(V8Js, setTimeLimit, arginfo_v8js_settimelimit, ZEND_ACC_PUBLIC)
|
|
|
|
PHP_ME(V8Js, setMemoryLimit, arginfo_v8js_setmemorylimit, ZEND_ACC_PUBLIC)
|
2016-03-08 08:28:16 +00:00
|
|
|
PHP_ME(V8Js, setAverageObjectSize, arginfo_v8js_setaverageobjectsize, ZEND_ACC_PUBLIC)
|
2016-02-28 16:00:47 +00:00
|
|
|
PHP_ME(V8Js, createSnapshot, arginfo_v8js_createsnapshot, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
|
2014-12-12 22:59:28 +00:00
|
|
|
{NULL, NULL, NULL}
|
|
|
|
};
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* V8Js object handlers */
|
2022-05-30 12:25:37 +00:00
|
|
|
static zval* v8js_write_property(zend_object *object, zend_string *member, zval *value, void **cache_slot) /* {{{ */
|
2021-06-03 01:33:51 +00:00
|
|
|
{
|
|
|
|
v8js_ctx *c = Z_V8JS_CTX_OBJ(object);
|
2022-05-30 12:25:37 +00:00
|
|
|
V8JS_CTX_PROLOGUE_EX(c, value);
|
2021-06-03 01:33:51 +00:00
|
|
|
|
|
|
|
/* Check whether member is public, if so, export to V8. */
|
|
|
|
zend_property_info *property_info = zend_get_property_info(c->std.ce, member, 1);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2021-06-03 01:33:51 +00:00
|
|
|
if(!property_info ||
|
|
|
|
(property_info != ZEND_WRONG_PROPERTY_INFO &&
|
|
|
|
(property_info->flags & ZEND_ACC_PUBLIC))) {
|
|
|
|
/* Global PHP JS object */
|
|
|
|
v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(isolate, c->object_name);
|
|
|
|
v8::Local<v8::Object> jsobj = V8JS_GLOBAL(isolate)->Get(v8_context, object_name_js).ToLocalChecked()->ToObject(v8_context).ToLocalChecked();
|
|
|
|
|
|
|
|
if (ZSTR_LEN(member) > std::numeric_limits<int>::max()) {
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
"Property name exceeds maximum supported length", 0);
|
2022-05-30 12:25:37 +00:00
|
|
|
return value;
|
2021-06-03 01:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write value to PHP JS object */
|
|
|
|
v8::Local<v8::Name> key = V8JS_SYML(ZSTR_VAL(member), static_cast<int>(ZSTR_LEN(member)));
|
|
|
|
jsobj->DefineOwnProperty(v8_context, key, zval_to_v8js(value, isolate), v8::ReadOnly);
|
|
|
|
}
|
2014-12-12 22:59:28 +00:00
|
|
|
|
|
|
|
/* Write value to PHP object */
|
2022-05-30 12:25:37 +00:00
|
|
|
return std_object_handlers.write_property(object, member, value, NULL);
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2022-05-30 12:25:37 +00:00
|
|
|
static void v8js_unset_property(zend_object *object, zend_string *member, void **cache_slot) /* {{{ */
|
2021-06-03 01:33:51 +00:00
|
|
|
{
|
2021-08-20 12:08:45 +00:00
|
|
|
V8JS_BEGIN_CTX_OBJ(c, object);
|
2021-06-03 01:33:51 +00:00
|
|
|
/* Global PHP JS object */
|
|
|
|
v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(isolate, c->object_name);
|
|
|
|
v8::Local<v8::Object> jsobj = V8JS_GLOBAL(isolate)->Get(v8_context, object_name_js).ToLocalChecked()->ToObject(v8_context).ToLocalChecked();
|
|
|
|
|
|
|
|
if (ZSTR_LEN(member) > std::numeric_limits<int>::max()) {
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
"Property name exceeds maximum supported length", 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete value from PHP JS object */
|
|
|
|
v8::Local<v8::Value> key = V8JS_SYML(ZSTR_VAL(member), static_cast<int>(ZSTR_LEN(member)));
|
|
|
|
|
2018-09-13 12:01:15 +00:00
|
|
|
jsobj->Delete(v8_context, key);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
|
|
|
/* Unset from PHP object */
|
2022-05-30 12:25:37 +00:00
|
|
|
std_object_handlers.unset_property(object, member, NULL);
|
2014-12-12 22:59:28 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
PHP_MINIT_FUNCTION(v8js_class) /* {{{ */
|
|
|
|
{
|
|
|
|
zend_class_entry ce;
|
|
|
|
|
|
|
|
/* V8Js Class */
|
|
|
|
INIT_CLASS_ENTRY(ce, "V8Js", v8js_methods);
|
2017-03-11 12:35:21 +00:00
|
|
|
php_ce_v8js = zend_register_internal_class(&ce);
|
2014-12-13 00:30:23 +00:00
|
|
|
php_ce_v8js->create_object = v8js_new;
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2023-02-18 13:18:36 +00:00
|
|
|
#if PHP_VERSION_ID >= 80200
|
|
|
|
php_ce_v8js->ce_flags |= ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES;
|
|
|
|
|
|
|
|
zend_string *attribute_name_AllowDynamicProperties_class_V8Js = zend_string_init_interned("AllowDynamicProperties", sizeof("AllowDynamicProperties") - 1, 1);
|
|
|
|
zend_add_class_attribute(php_ce_v8js, attribute_name_AllowDynamicProperties_class_V8Js, 0);
|
|
|
|
zend_string_release(attribute_name_AllowDynamicProperties_class_V8Js);
|
|
|
|
#endif
|
|
|
|
|
2014-12-12 22:59:28 +00:00
|
|
|
/* V8Js handlers */
|
|
|
|
memcpy(&v8js_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
|
|
|
|
v8js_object_handlers.clone_obj = NULL;
|
2014-12-13 00:30:23 +00:00
|
|
|
v8js_object_handlers.write_property = v8js_write_property;
|
|
|
|
v8js_object_handlers.unset_property = v8js_unset_property;
|
2014-12-12 22:59:28 +00:00
|
|
|
|
|
|
|
/* V8Js Class Constants */
|
2017-03-11 12:45:23 +00:00
|
|
|
zend_declare_class_constant_string(php_ce_v8js, ZEND_STRL("V8_VERSION"), PHP_V8_VERSION);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2017-03-11 12:45:23 +00:00
|
|
|
zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_NONE"), V8JS_FLAG_NONE);
|
|
|
|
zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_FORCE_ARRAY"), V8JS_FLAG_FORCE_ARRAY);
|
2017-03-11 12:35:21 +00:00
|
|
|
zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_PROPAGATE_PHP_EXCEPTIONS"), V8JS_FLAG_PROPAGATE_PHP_EXCEPTIONS);
|
2014-12-12 22:59:28 +00:00
|
|
|
|
2014-12-13 00:15:29 +00:00
|
|
|
le_v8js_script = zend_register_list_destructors_ex(v8js_script_dtor, NULL, PHP_V8JS_SCRIPT_RES_NAME, module_number);
|
2015-03-12 11:13:33 +00:00
|
|
|
|
|
|
|
return SUCCESS;
|
2014-12-12 22:59:28 +00:00
|
|
|
} /* }}} */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
2016-07-02 20:41:01 +00:00
|
|
|
* indent-tabs-mode: t
|
2014-12-12 22:59:28 +00:00
|
|
|
* End:
|
|
|
|
* vim600: noet sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: noet sw=4 ts=4
|
|
|
|
*/
|