2014-11-29 14:42:41 +00:00
|
|
|
|
/*
|
|
|
|
|
+----------------------------------------------------------------------+
|
2017-03-11 11:33:49 +00:00
|
|
|
|
| PHP Version 7 |
|
2014-11-29 14:42:41 +00:00
|
|
|
|
+----------------------------------------------------------------------+
|
2017-03-10 22:49:18 +00:00
|
|
|
|
| Copyright (c) 1997-2017 The PHP Group |
|
2014-11-29 14:42:41 +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> |
|
2016-05-22 13:12:44 +00:00
|
|
|
|
| Author: Stefan Siegl <stesie@php.net> |
|
2014-11-29 14:42:41 +00:00
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-01-06 09:01:35 +00:00
|
|
|
|
#include "php_v8js_macros.h"
|
|
|
|
|
#include "v8js_array_access.h"
|
2017-03-10 22:49:18 +00:00
|
|
|
|
#include "v8js_exceptions.h"
|
2016-01-09 11:50:40 +00:00
|
|
|
|
#include "v8js_generator_export.h"
|
2016-01-06 09:01:35 +00:00
|
|
|
|
#include "v8js_object_export.h"
|
|
|
|
|
#include "v8js_v8object_class.h"
|
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
extern "C" {
|
|
|
|
|
#include "ext/date/php_date.h"
|
|
|
|
|
#include "ext/standard/php_string.h"
|
|
|
|
|
#include "zend_interfaces.h"
|
|
|
|
|
#include "zend_closures.h"
|
2015-08-22 13:16:02 +00:00
|
|
|
|
#include "zend_exceptions.h"
|
2016-01-09 11:50:40 +00:00
|
|
|
|
#include "zend_generators.h"
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-22 20:30:19 +00:00
|
|
|
|
static void v8js_weak_object_callback(const v8::WeakCallbackInfo<zend_object> &data);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2022-05-31 12:21:32 +00:00
|
|
|
|
v8::Local<v8::Value> v8js_propagate_exception(v8js_ctx *ctx) /* {{{ */
|
|
|
|
|
{
|
|
|
|
|
v8::Local<v8::Value> return_value = v8::Null(ctx->isolate);
|
|
|
|
|
|
2023-02-08 13:04:03 +00:00
|
|
|
|
#if PHP_VERSION_ID < 80100
|
|
|
|
|
if (!(ctx->flags & V8JS_FLAG_PROPAGATE_PHP_EXCEPTIONS) || zend_is_unwind_exit(EG(exception))) {
|
|
|
|
|
#else
|
2023-02-03 18:31:07 +00:00
|
|
|
|
if (!(ctx->flags & V8JS_FLAG_PROPAGATE_PHP_EXCEPTIONS) || zend_is_graceful_exit(EG(exception)) || zend_is_unwind_exit(EG(exception))) {
|
2023-02-08 13:04:03 +00:00
|
|
|
|
#endif
|
2022-05-31 12:21:32 +00:00
|
|
|
|
v8js_terminate_execution(ctx->isolate);
|
|
|
|
|
return return_value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
zval tmp_zv;
|
|
|
|
|
|
2022-06-24 10:04:11 +00:00
|
|
|
|
if (Z_TYPE(ctx->exception_filter) != IS_NULL) {
|
2022-05-31 12:21:32 +00:00
|
|
|
|
zval params[1];
|
|
|
|
|
ZVAL_OBJ(¶ms[0], EG(exception));
|
|
|
|
|
Z_ADDREF_P(¶ms[0]);
|
|
|
|
|
zend_clear_exception();
|
2022-06-24 10:04:11 +00:00
|
|
|
|
call_user_function(EG(function_table), NULL, &ctx->exception_filter, &tmp_zv, 1, params);
|
2022-05-31 12:21:32 +00:00
|
|
|
|
zval_ptr_dtor(¶ms[0]);
|
|
|
|
|
|
2022-05-31 14:07:57 +00:00
|
|
|
|
if(EG(exception)) {
|
|
|
|
|
// exception proxy threw exception itself, don't forward, just stop execution.
|
|
|
|
|
v8js_terminate_execution(ctx->isolate);
|
|
|
|
|
} else {
|
|
|
|
|
return_value = ctx->isolate->ThrowException(zval_to_v8js(&tmp_zv, ctx->isolate));
|
|
|
|
|
}
|
2022-05-31 12:21:32 +00:00
|
|
|
|
} else {
|
|
|
|
|
ZVAL_OBJ(&tmp_zv, EG(exception));
|
|
|
|
|
return_value = ctx->isolate->ThrowException(zval_to_v8js(&tmp_zv, ctx->isolate));
|
|
|
|
|
zend_clear_exception();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return return_value;
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
/* Callback for PHP methods and functions */
|
2018-09-13 12:01:15 +00:00
|
|
|
|
static void v8js_call_php_func(zend_object *object, zend_function *method_ptr, const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
|
2014-11-29 14:42:41 +00:00
|
|
|
|
{
|
2018-09-13 12:01:15 +00:00
|
|
|
|
v8::Isolate *isolate = info.GetIsolate();
|
2021-03-21 02:07:53 +00:00
|
|
|
|
v8::Local<v8::Context> v8_context = isolate->GetEnteredOrMicrotaskContext();
|
2017-03-11 12:31:40 +00:00
|
|
|
|
v8::Local<v8::Value> return_value = V8JS_NULL;
|
2014-11-29 14:42:41 +00:00
|
|
|
|
zend_fcall_info fci;
|
|
|
|
|
zend_fcall_info_cache fcc;
|
2016-07-03 20:31:09 +00:00
|
|
|
|
zval fname, retval;
|
2015-08-29 09:57:53 +00:00
|
|
|
|
unsigned int argc = info.Length(), min_num_args = 0, max_num_args = 0;
|
2014-11-29 14:42:41 +00:00
|
|
|
|
char *error;
|
2017-03-10 22:49:18 +00:00
|
|
|
|
zend_ulong error_len;
|
2016-07-03 20:31:09 +00:00
|
|
|
|
unsigned int i;
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2014-12-13 00:15:29 +00:00
|
|
|
|
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
/* Set parameter limits */
|
|
|
|
|
min_num_args = method_ptr->common.required_num_args;
|
|
|
|
|
max_num_args = method_ptr->common.num_args;
|
|
|
|
|
|
|
|
|
|
/* Function name to call */
|
2015-08-29 22:26:55 +00:00
|
|
|
|
ZVAL_STR_COPY(&fname, method_ptr->common.function_name);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
/* zend_fcall_info */
|
|
|
|
|
fci.size = sizeof(fci);
|
2016-07-02 22:25:42 +00:00
|
|
|
|
fci.function_name = fname;
|
2015-08-24 19:13:35 +00:00
|
|
|
|
fci.object = object;
|
2015-08-29 09:57:53 +00:00
|
|
|
|
fci.retval = &retval;
|
2014-11-29 14:42:41 +00:00
|
|
|
|
fci.param_count = 0;
|
|
|
|
|
|
|
|
|
|
/* Check for passed vs required number of arguments */
|
|
|
|
|
if (argc < min_num_args)
|
|
|
|
|
{
|
|
|
|
|
error_len = spprintf(&error, 0,
|
|
|
|
|
"%s::%s() expects %s %d parameter%s, %d given",
|
2015-09-28 20:44:30 +00:00
|
|
|
|
ZSTR_VAL(object->ce->name),
|
|
|
|
|
ZSTR_VAL(method_ptr->common.function_name),
|
2014-11-29 14:42:41 +00:00
|
|
|
|
min_num_args == max_num_args ? "exactly" : argc < min_num_args ? "at least" : "at most",
|
|
|
|
|
argc < min_num_args ? min_num_args : max_num_args,
|
|
|
|
|
(argc < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
|
|
|
|
|
argc);
|
|
|
|
|
|
2017-03-10 22:49:18 +00:00
|
|
|
|
if (error_len > std::numeric_limits<int>::max()) {
|
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
|
"Generated error message length exceeds maximum supported length", 0);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return_value = V8JS_THROW(isolate, TypeError, error, static_cast<int>(error_len));
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-24 19:13:35 +00:00
|
|
|
|
if (object->ce == zend_ce_closure) {
|
2015-08-29 09:57:53 +00:00
|
|
|
|
zend_string_release(method_ptr->internal_function.function_name);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
efree(method_ptr);
|
|
|
|
|
}
|
|
|
|
|
efree(error);
|
|
|
|
|
info.GetReturnValue().Set(return_value);
|
2016-08-11 23:02:56 +00:00
|
|
|
|
zval_ptr_dtor(&fname);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Convert parameters passed from V8 */
|
2018-09-13 12:01:15 +00:00
|
|
|
|
if (argc)
|
|
|
|
|
{
|
2015-08-29 09:57:53 +00:00
|
|
|
|
fci.params = (zval *) safe_emalloc(argc, sizeof(zval), 0);
|
2018-09-13 12:01:15 +00:00
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
|
{
|
|
|
|
|
v8::Local<v8::Object> param_object;
|
|
|
|
|
|
|
|
|
|
if (info[i]->IsObject() && info[i]->ToObject(v8_context).ToLocal(¶m_object) && param_object->InternalFieldCount() == 2)
|
|
|
|
|
{
|
2014-11-29 14:42:41 +00:00
|
|
|
|
/* This is a PHP object, passed to JS and back. */
|
2018-09-13 12:01:15 +00:00
|
|
|
|
zend_object *object = reinterpret_cast<zend_object *>(param_object->GetAlignedPointerFromInternalField(1));
|
2015-08-29 09:57:53 +00:00
|
|
|
|
ZVAL_OBJ(&fci.params[i], object);
|
|
|
|
|
Z_ADDREF_P(&fci.params[i]);
|
2018-09-13 12:01:15 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (v8js_to_zval(info[i], &fci.params[i], ctx->flags, isolate) == FAILURE)
|
|
|
|
|
{
|
2020-01-03 17:22:39 +00:00
|
|
|
|
error_len = spprintf(&error, 0, "converting parameter #%d passed to %s() failed", i + 1, ZSTR_VAL(method_ptr->common.function_name));
|
2017-03-10 22:49:18 +00:00
|
|
|
|
|
|
|
|
|
if (error_len > std::numeric_limits<int>::max()) {
|
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
|
"Generated error message length exceeds maximum supported length", 0);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return_value = V8JS_THROW(isolate, Error, error, static_cast<int>(error_len));
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
efree(error);
|
|
|
|
|
goto failure;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-29 09:57:53 +00:00
|
|
|
|
fci.param_count++;
|
2014-12-10 19:02:33 +00:00
|
|
|
|
}
|
2014-11-29 14:42:41 +00:00
|
|
|
|
} else {
|
|
|
|
|
fci.params = NULL;
|
|
|
|
|
}
|
2021-06-03 01:33:51 +00:00
|
|
|
|
fci.named_params = NULL;
|
2014-11-29 14:42:41 +00:00
|
|
|
|
info.GetReturnValue().Set(V8JS_NULL);
|
|
|
|
|
|
2015-12-31 22:42:01 +00:00
|
|
|
|
{
|
|
|
|
|
isolate->Exit();
|
|
|
|
|
v8::Unlocker unlocker(isolate);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2015-12-31 22:42:01 +00:00
|
|
|
|
zend_try {
|
2014-12-10 19:02:33 +00:00
|
|
|
|
/* zend_fcall_info_cache */
|
|
|
|
|
fcc.function_handler = method_ptr;
|
2015-08-29 09:57:53 +00:00
|
|
|
|
fcc.calling_scope = object->ce;
|
|
|
|
|
fcc.called_scope = object->ce;
|
2015-08-24 19:13:35 +00:00
|
|
|
|
fcc.object = object;
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2017-03-11 12:35:21 +00:00
|
|
|
|
zend_call_function(&fci, &fcc);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
2015-12-31 22:42:01 +00:00
|
|
|
|
zend_catch {
|
|
|
|
|
v8js_terminate_execution(isolate);
|
|
|
|
|
V8JSG(fatal_error_abort) = 1;
|
|
|
|
|
}
|
|
|
|
|
zend_end_try();
|
2014-12-10 19:02:33 +00:00
|
|
|
|
}
|
2015-12-31 22:42:01 +00:00
|
|
|
|
isolate->Enter();
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
failure:
|
|
|
|
|
/* Cleanup */
|
|
|
|
|
if (argc) {
|
|
|
|
|
for (i = 0; i < fci.param_count; i++) {
|
2016-08-11 23:02:56 +00:00
|
|
|
|
zval_ptr_dtor(&fci.params[i]);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
efree(fci.params);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-26 10:48:52 +00:00
|
|
|
|
if(EG(exception)) {
|
2022-05-31 12:21:32 +00:00
|
|
|
|
return_value = v8js_propagate_exception(ctx);
|
2016-05-22 20:30:19 +00:00
|
|
|
|
} else if (Z_TYPE(retval) == IS_OBJECT && Z_OBJ(retval) == object) {
|
2016-03-25 18:15:25 +00:00
|
|
|
|
// special case: "return $this"
|
|
|
|
|
return_value = info.Holder();
|
2015-10-11 00:09:59 +00:00
|
|
|
|
} else {
|
2017-03-11 12:35:21 +00:00
|
|
|
|
return_value = zval_to_v8js(&retval, isolate);
|
2015-08-22 13:16:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-11 23:02:56 +00:00
|
|
|
|
zval_ptr_dtor(&retval);
|
|
|
|
|
zval_ptr_dtor(&fname);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
info.GetReturnValue().Set(return_value);
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
|
|
/* Callback for PHP methods and functions */
|
2016-01-08 14:42:43 +00:00
|
|
|
|
void v8js_php_callback(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
|
2014-11-29 14:42:41 +00:00
|
|
|
|
{
|
|
|
|
|
v8::Local<v8::Object> self = info.Holder();
|
|
|
|
|
|
2016-05-22 20:30:19 +00:00
|
|
|
|
zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
|
2014-11-29 14:42:41 +00:00
|
|
|
|
zend_function *method_ptr;
|
|
|
|
|
|
|
|
|
|
/* Set method_ptr from v8::External or fetch the closure invoker */
|
|
|
|
|
if (!info.Data().IsEmpty() && info.Data()->IsExternal()) {
|
|
|
|
|
method_ptr = static_cast<zend_function *>(v8::External::Cast(*info.Data())->Value());
|
|
|
|
|
} else {
|
2017-03-11 12:35:21 +00:00
|
|
|
|
method_ptr = zend_get_closure_invoke_method(object);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 12:01:15 +00:00
|
|
|
|
return v8js_call_php_func(object, method_ptr, info);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Callback for PHP constructor calls */
|
2014-12-13 00:30:23 +00:00
|
|
|
|
static void v8js_construct_callback(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
|
2014-11-29 14:42:41 +00:00
|
|
|
|
{
|
|
|
|
|
v8::Isolate *isolate = info.GetIsolate();
|
|
|
|
|
info.GetReturnValue().Set(V8JS_UNDEFINED);
|
|
|
|
|
|
2017-03-11 12:31:40 +00:00
|
|
|
|
v8::Local<v8::Object> newobj = info.This();
|
2015-08-24 20:05:27 +00:00
|
|
|
|
zval value;
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
if (!info.IsConstructCall()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v8::Local<v8::Array> cons_data = v8::Local<v8::Array>::Cast(info.Data());
|
2018-09-13 12:01:15 +00:00
|
|
|
|
v8::Local<v8::Value> cons_tmpl, cons_ce;
|
|
|
|
|
|
2021-03-21 02:07:53 +00:00
|
|
|
|
if (!cons_data->Get(isolate->GetEnteredOrMicrotaskContext(), 0).ToLocal(&cons_tmpl)
|
|
|
|
|
||!cons_data->Get(isolate->GetEnteredOrMicrotaskContext(), 1).ToLocal(&cons_ce))
|
2018-09-13 12:01:15 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v8::Local<v8::External> ext_tmpl = v8::Local<v8::External>::Cast(cons_tmpl);
|
|
|
|
|
v8::Local<v8::External> ext_ce = v8::Local<v8::External>::Cast(cons_ce);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2014-12-13 00:15:29 +00:00
|
|
|
|
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
if (info[0]->IsExternal()) {
|
2014-12-13 00:30:23 +00:00
|
|
|
|
// Object created by v8js in v8js_hash_to_jsobj, PHP object passed as v8::External.
|
2016-05-22 13:12:44 +00:00
|
|
|
|
v8::Local<v8::External> php_object = v8::Local<v8::External>::Cast(info[0]);
|
2015-08-29 09:57:53 +00:00
|
|
|
|
zend_object *object = reinterpret_cast<zend_object *>(php_object->Value());
|
2015-08-24 20:05:27 +00:00
|
|
|
|
ZVAL_OBJ(&value, object);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2015-08-24 20:05:27 +00:00
|
|
|
|
if(ctx->weak_objects.count(object)) {
|
2014-11-29 14:42:41 +00:00
|
|
|
|
// We already exported this object, hence no need to add another
|
|
|
|
|
// ref, v8 won't give us a second weak-object callback anyways.
|
|
|
|
|
newobj->SetAlignedPointerInInternalField(0, ext_tmpl->Value());
|
2016-05-22 20:30:19 +00:00
|
|
|
|
newobj->SetAlignedPointerInInternalField(1, object);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Increase the reference count of this value because we're storing it internally for use later
|
2016-11-01 20:16:01 +00:00
|
|
|
|
// See https://github.com/phpv8/v8js/issues/6
|
2015-08-29 09:57:53 +00:00
|
|
|
|
Z_ADDREF_P(&value);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
} else {
|
|
|
|
|
// Object created from JavaScript context. Need to create PHP object first.
|
|
|
|
|
zend_class_entry *ce = static_cast<zend_class_entry *>(ext_ce->Value());
|
|
|
|
|
zend_function *ctor_ptr = ce->constructor;
|
|
|
|
|
|
|
|
|
|
// Check access on __construct function, if any
|
|
|
|
|
if (ctor_ptr != NULL && (ctor_ptr->common.fn_flags & ZEND_ACC_PUBLIC) == 0) {
|
|
|
|
|
info.GetReturnValue().Set(isolate->ThrowException(V8JS_SYM("Call to protected __construct() not allowed")));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-29 09:57:53 +00:00
|
|
|
|
object_init_ex(&value, ce);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
// Call __construct function
|
|
|
|
|
if (ctor_ptr != NULL) {
|
2018-09-13 12:01:15 +00:00
|
|
|
|
v8js_call_php_func(Z_OBJ(value), ctor_ptr, info);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newobj->SetAlignedPointerInInternalField(0, ext_tmpl->Value());
|
2016-05-22 20:30:19 +00:00
|
|
|
|
newobj->SetAlignedPointerInInternalField(1, Z_OBJ(value));
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
// Since we got to decrease the reference count again, in case v8 garbage collector
|
|
|
|
|
// decides to dispose the JS object, we add a weak persistent handle and register
|
|
|
|
|
// a callback function that removes the reference.
|
2015-08-24 20:05:27 +00:00
|
|
|
|
ctx->weak_objects[Z_OBJ(value)].Reset(isolate, newobj);
|
2016-05-22 20:30:19 +00:00
|
|
|
|
ctx->weak_objects[Z_OBJ(value)].SetWeak(Z_OBJ(value), v8js_weak_object_callback, v8::WeakCallbackType::kParameter);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
// Just tell v8 that we're allocating some external memory
|
|
|
|
|
// (for the moment we just always tell 1k instead of trying to find out actual values)
|
2016-03-08 08:28:16 +00:00
|
|
|
|
isolate->AdjustAmountOfExternalAllocatedMemory(ctx->average_object_size);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
|
|
|
2016-05-22 20:30:19 +00:00
|
|
|
|
static void v8js_weak_object_callback(const v8::WeakCallbackInfo<zend_object> &data) {
|
2014-11-29 14:42:41 +00:00
|
|
|
|
v8::Isolate *isolate = data.GetIsolate();
|
|
|
|
|
|
2015-08-24 20:05:27 +00:00
|
|
|
|
zend_object *object = data.GetParameter();
|
|
|
|
|
zval value;
|
|
|
|
|
ZVAL_OBJ(&value, object);
|
2016-08-11 23:02:56 +00:00
|
|
|
|
zval_ptr_dtor(&value);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2014-12-13 00:15:29 +00:00
|
|
|
|
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2015-08-24 20:05:27 +00:00
|
|
|
|
ctx->weak_objects.at(object).Reset();
|
|
|
|
|
ctx->weak_objects.erase(object);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2016-03-08 08:28:16 +00:00
|
|
|
|
isolate->AdjustAmountOfExternalAllocatedMemory(-ctx->average_object_size);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-09 09:31:15 +00:00
|
|
|
|
static void v8js_weak_closure_callback(const v8::WeakCallbackInfo<v8js_function_tmpl_t> &data) {
|
2014-11-29 14:42:41 +00:00
|
|
|
|
v8::Isolate *isolate = data.GetIsolate();
|
|
|
|
|
|
2016-07-09 09:31:15 +00:00
|
|
|
|
v8js_function_tmpl_t *persist_tpl_ = data.GetParameter();
|
2014-11-29 14:42:41 +00:00
|
|
|
|
persist_tpl_->Reset();
|
|
|
|
|
delete persist_tpl_;
|
|
|
|
|
|
2014-12-13 00:15:29 +00:00
|
|
|
|
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
ctx->weak_closures.at(persist_tpl_).Reset();
|
|
|
|
|
ctx->weak_closures.erase(persist_tpl_);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define IS_MAGIC_FUNC(mname) \
|
2015-08-29 09:57:53 +00:00
|
|
|
|
((ZSTR_LEN(key) == sizeof(mname) - 1) && \
|
|
|
|
|
!strncasecmp(ZSTR_VAL(key), mname, ZSTR_LEN(key)))
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
#define PHP_V8JS_CALLBACK(isolate, mptr, tmpl) \
|
2021-03-21 02:07:53 +00:00
|
|
|
|
(v8::FunctionTemplate::New((isolate), v8js_php_callback, v8::External::New((isolate), mptr), v8::Signature::New((isolate), tmpl))->GetFunction(isolate->GetEnteredOrMicrotaskContext()).ToLocalChecked())
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
|
2014-12-13 00:30:23 +00:00
|
|
|
|
static void v8js_named_property_enumerator(const v8::PropertyCallbackInfo<v8::Array> &info) /* {{{ */
|
2014-11-29 14:42:41 +00:00
|
|
|
|
{
|
|
|
|
|
// note: 'special' properties like 'constructor' are not enumerated.
|
|
|
|
|
v8::Isolate *isolate = info.GetIsolate();
|
2021-03-21 02:07:53 +00:00
|
|
|
|
v8::Local<v8::Context> v8_context = isolate->GetEnteredOrMicrotaskContext();
|
2018-09-13 12:01:15 +00:00
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
v8::Local<v8::Object> self = info.Holder();
|
|
|
|
|
v8::Local<v8::Array> result = v8::Array::New(isolate, 0);
|
|
|
|
|
uint32_t result_len = 0;
|
|
|
|
|
|
|
|
|
|
zend_class_entry *ce;
|
2015-08-29 09:57:53 +00:00
|
|
|
|
void *ptr;
|
2014-11-29 14:42:41 +00:00
|
|
|
|
HashTable *proptable;
|
2015-08-29 09:57:53 +00:00
|
|
|
|
zend_string *key;
|
2017-03-10 22:49:18 +00:00
|
|
|
|
zend_ulong index;
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2016-05-22 20:30:19 +00:00
|
|
|
|
zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
|
2015-08-24 19:13:35 +00:00
|
|
|
|
ce = object->ce;
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
/* enumerate all methods */
|
2015-08-29 09:57:53 +00:00
|
|
|
|
ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->function_table, key, ptr) {
|
|
|
|
|
zend_function *method_ptr = reinterpret_cast<zend_function *>(ptr);
|
2014-11-29 14:42:41 +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) {
|
2014-11-29 14:42:41 +00:00
|
|
|
|
/* no __construct, __destruct(), or __clone() functions */
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// hide (do not enumerate) 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) ||
|
2023-02-20 11:09:13 +00:00
|
|
|
|
IS_MAGIC_FUNC(ZEND_ISSET_FUNC_NAME) ||
|
|
|
|
|
IS_MAGIC_FUNC("__serialize") ||
|
|
|
|
|
IS_MAGIC_FUNC("__unserialize")) {
|
2014-11-29 14:42:41 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-03-10 22:49:18 +00:00
|
|
|
|
|
|
|
|
|
v8::Local<v8::String> method_name;
|
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
// rename PHP special method names to JS equivalents.
|
|
|
|
|
if (IS_MAGIC_FUNC(ZEND_TOSTRING_FUNC_NAME)) {
|
|
|
|
|
method_name = V8JS_SYM("toString");
|
|
|
|
|
}
|
2017-03-10 22:49:18 +00:00
|
|
|
|
else {
|
|
|
|
|
if (ZSTR_LEN(method_ptr->common.function_name) > std::numeric_limits<int>::max()) {
|
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
|
"Method name length exceeds maximum supported length", 0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
method_name = V8JS_STRL(ZSTR_VAL(method_ptr->common.function_name), static_cast<int>(ZSTR_LEN(method_ptr->common.function_name)));
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 12:01:15 +00:00
|
|
|
|
result->Set(v8_context, result_len++, method_name);
|
2015-08-29 09:57:53 +00:00
|
|
|
|
} ZEND_HASH_FOREACH_END();
|
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
/* enumerate all properties */
|
|
|
|
|
/* Z_OBJPROP uses the get_properties handler */
|
2015-08-29 09:57:53 +00:00
|
|
|
|
zval tmp_zv;
|
|
|
|
|
ZVAL_OBJ(&tmp_zv, object);
|
|
|
|
|
proptable = Z_OBJPROP(tmp_zv);
|
|
|
|
|
|
2017-03-11 12:55:54 +00:00
|
|
|
|
ZEND_HASH_FOREACH_KEY(proptable, index, key) {
|
2015-08-29 09:57:53 +00:00
|
|
|
|
if(key) {
|
2014-11-29 14:42:41 +00:00
|
|
|
|
/* skip protected and private members */
|
2015-08-29 09:57:53 +00:00
|
|
|
|
if(ZSTR_VAL(key)[0] == '\0') {
|
2014-11-29 14:42:41 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-03-10 22:49:18 +00:00
|
|
|
|
|
|
|
|
|
if (ZSTR_LEN(key) + 1 > std::numeric_limits<int>::max()) {
|
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
|
"Object key length exceeds maximum supported length", 0);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
// prefix enumerated property names with '$' so they can be
|
|
|
|
|
// dereferenced unambiguously (ie, don't conflict with method
|
|
|
|
|
// names)
|
2015-08-29 09:57:53 +00:00
|
|
|
|
char *prefixed = static_cast<char *>(emalloc(ZSTR_LEN(key) + 2));
|
2014-11-29 14:42:41 +00:00
|
|
|
|
prefixed[0] = '$';
|
2015-08-29 09:57:53 +00:00
|
|
|
|
strncpy(prefixed + 1, ZSTR_VAL(key), ZSTR_LEN(key) + 1);
|
2018-09-13 12:01:15 +00:00
|
|
|
|
result->Set(v8_context, result_len++, V8JS_STRL(prefixed, static_cast<int>(ZSTR_LEN(key) + 1)));
|
2014-11-29 14:42:41 +00:00
|
|
|
|
efree(prefixed);
|
|
|
|
|
} else {
|
|
|
|
|
// even numeric indices are enumerated as strings in JavaScript
|
2018-09-13 12:01:15 +00:00
|
|
|
|
result->Set(v8_context, result_len++, V8JS_FLOAT((double) index)->ToString(v8_context).ToLocalChecked());
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
2015-08-29 09:57:53 +00:00
|
|
|
|
} ZEND_HASH_FOREACH_END();
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
/* done */
|
|
|
|
|
info.GetReturnValue().Set(result);
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
2014-12-13 00:30:23 +00:00
|
|
|
|
static void v8js_invoke_callback(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
|
2014-11-29 14:42:41 +00:00
|
|
|
|
{
|
|
|
|
|
v8::Isolate *isolate = info.GetIsolate();
|
2021-03-21 02:07:53 +00:00
|
|
|
|
v8::Local<v8::Context> v8_context = isolate->GetEnteredOrMicrotaskContext();
|
2018-09-13 12:01:15 +00:00
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
v8::Local<v8::Object> self = info.Holder();
|
|
|
|
|
v8::Local<v8::Function> cb = v8::Local<v8::Function>::Cast(info.Data());
|
|
|
|
|
int argc = info.Length(), i;
|
|
|
|
|
v8::Local<v8::Value> *argv = static_cast<v8::Local<v8::Value> *>(alloca(sizeof(v8::Local<v8::Value>) * argc));
|
|
|
|
|
v8::Local<v8::Value> result;
|
|
|
|
|
|
|
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
|
new(&argv[i]) v8::Local<v8::Value>;
|
|
|
|
|
argv[i] = info[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (info.IsConstructCall()) {
|
2014-12-13 00:15:29 +00:00
|
|
|
|
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2018-09-13 12:01:15 +00:00
|
|
|
|
v8::Local<v8::String> str = self->GetConstructorName()->ToString(v8_context).ToLocalChecked();
|
|
|
|
|
v8::String::Utf8Value str_value(isolate, str);
|
|
|
|
|
zend_string *constructor_name = zend_string_init(ToCString(str_value), str_value.length(), 0);
|
2017-03-11 12:35:21 +00:00
|
|
|
|
zend_class_entry *ce = zend_lookup_class(constructor_name);
|
2015-08-29 09:57:53 +00:00
|
|
|
|
zend_string_release(constructor_name);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
v8::Local<v8::FunctionTemplate> new_tpl;
|
|
|
|
|
new_tpl = v8::Local<v8::FunctionTemplate>::New
|
2015-08-29 09:57:53 +00:00
|
|
|
|
(isolate, ctx->template_cache.at(ce->name));
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2018-09-13 12:01:15 +00:00
|
|
|
|
v8::Local<v8::Function> fn;
|
|
|
|
|
if (!new_tpl->GetFunction(v8_context).ToLocal(&fn) || !fn->NewInstance(v8_context, argc, argv).ToLocal(&result))
|
|
|
|
|
{
|
2017-04-24 18:43:52 +00:00
|
|
|
|
result = V8JS_UNDEFINED;
|
|
|
|
|
}
|
2014-11-29 14:42:41 +00:00
|
|
|
|
} else {
|
2018-09-13 12:01:15 +00:00
|
|
|
|
cb->Call(v8_context, self, argc, argv).ToLocal(&result);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
2017-04-24 18:43:52 +00:00
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
info.GetReturnValue().Set(result);
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
|
|
// this is a magic '__call' implementation for PHP classes which don't actually
|
|
|
|
|
// have a '__call' magic function. This way we can always force a method
|
|
|
|
|
// call (as opposed to a property get) from JavaScript using __call.
|
2014-12-13 00:30:23 +00:00
|
|
|
|
static void v8js_fake_call_impl(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
|
2014-11-29 14:42:41 +00:00
|
|
|
|
{
|
|
|
|
|
v8::Isolate *isolate = info.GetIsolate();
|
2021-03-21 02:07:53 +00:00
|
|
|
|
v8::Local<v8::Context> v8_context = isolate->GetEnteredOrMicrotaskContext();
|
2018-09-13 12:01:15 +00:00
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
v8::Local<v8::Object> self = info.Holder();
|
2017-03-11 12:31:40 +00:00
|
|
|
|
v8::Local<v8::Value> return_value = V8JS_NULL;
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
char *error;
|
2017-03-10 22:49:18 +00:00
|
|
|
|
size_t error_len;
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
zend_class_entry *ce;
|
2016-05-22 20:30:19 +00:00
|
|
|
|
zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
|
2015-08-24 19:13:35 +00:00
|
|
|
|
ce = object->ce;
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
// first arg is method name, second arg is array of args.
|
|
|
|
|
if (info.Length() < 2) {
|
|
|
|
|
error_len = spprintf(&error, 0,
|
|
|
|
|
"%s::__call expects 2 parameters, %d given",
|
2020-01-03 17:17:04 +00:00
|
|
|
|
ZSTR_VAL(ce->name), (int) info.Length());
|
2017-03-10 22:49:18 +00:00
|
|
|
|
|
|
|
|
|
if (error_len > std::numeric_limits<int>::max()) {
|
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
|
"Generated error message length exceeds maximum supported length", 0);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return_value = V8JS_THROW(isolate, TypeError, error, static_cast<int>(error_len));
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
efree(error);
|
|
|
|
|
info.GetReturnValue().Set(return_value);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-03-10 22:49:18 +00:00
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
if (!info[1]->IsArray()) {
|
|
|
|
|
error_len = spprintf(&error, 0,
|
|
|
|
|
"%s::__call expects 2nd parameter to be an array",
|
|
|
|
|
ce->name);
|
2017-03-10 22:49:18 +00:00
|
|
|
|
|
|
|
|
|
if (error_len > std::numeric_limits<int>::max()) {
|
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
|
"Generated error message length exceeds maximum supported length", 0);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return_value = V8JS_THROW(isolate, TypeError, error, static_cast<int>(error_len));
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
efree(error);
|
|
|
|
|
info.GetReturnValue().Set(return_value);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-08-29 09:57:53 +00:00
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
v8::Local<v8::Array> args = v8::Local<v8::Array>::Cast(info[1]);
|
|
|
|
|
if (args->Length() > 1000000) {
|
|
|
|
|
// prevent overflow, since args->Length() is a uint32_t and args
|
|
|
|
|
// in the Function->Call method below is a (signed) int.
|
|
|
|
|
error_len = spprintf(&error, 0,
|
|
|
|
|
"%s::__call expects fewer than a million arguments",
|
2020-01-03 17:17:04 +00:00
|
|
|
|
ZSTR_VAL(ce->name));
|
2017-03-10 22:49:18 +00:00
|
|
|
|
|
|
|
|
|
if (error_len > std::numeric_limits<int>::max()) {
|
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
|
"Generated error message length exceeds maximum supported length", 0);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return_value = V8JS_THROW(isolate, TypeError, error, static_cast<int>(error_len));
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
efree(error);
|
|
|
|
|
info.GetReturnValue().Set(return_value);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-08-29 09:57:53 +00:00
|
|
|
|
|
2018-09-13 12:01:15 +00:00
|
|
|
|
v8::MaybeLocal<v8::String> str = info[0]->ToString(v8_context);
|
2018-09-12 13:43:40 +00:00
|
|
|
|
|
|
|
|
|
if (str.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
error_len = spprintf(&error, 0,
|
2020-01-03 17:17:04 +00:00
|
|
|
|
"%s::__call expects 1st parameter to be valid function name, toString() invocation failed.",
|
|
|
|
|
ZSTR_VAL(ce->name));
|
2018-09-12 13:43:40 +00:00
|
|
|
|
|
|
|
|
|
if (error_len > std::numeric_limits<int>::max()) {
|
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
|
"Generated error message length exceeds maximum supported length", 0);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return_value = V8JS_THROW(isolate, TypeError, error, static_cast<int>(error_len));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
efree(error);
|
|
|
|
|
info.GetReturnValue().Set(return_value);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v8::String::Utf8Value str_value(isolate, str.ToLocalChecked());
|
2015-08-29 09:57:53 +00:00
|
|
|
|
const char *method_c_name = ToCString(str_value);
|
2018-09-13 12:01:15 +00:00
|
|
|
|
zend_string *method_name = zend_string_init(method_c_name, str_value.length(), 0);
|
2015-08-29 09:57:53 +00:00
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
// okay, look up the method name and manually invoke it.
|
2015-08-29 09:57:53 +00:00
|
|
|
|
const zend_object_handlers *h = object->handlers;
|
2017-03-11 12:35:21 +00:00
|
|
|
|
zend_function *method_ptr = h->get_method(&object, method_name, NULL);
|
2015-08-29 09:57:53 +00:00
|
|
|
|
zend_string_release(method_name);
|
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
if (method_ptr == NULL ||
|
|
|
|
|
(method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) == 0 ||
|
2017-06-13 16:14:12 +00:00
|
|
|
|
(method_ptr->common.fn_flags & (ZEND_ACC_CTOR|ZEND_ACC_DTOR)) != 0) {
|
2014-11-29 14:42:41 +00:00
|
|
|
|
error_len = spprintf(&error, 0,
|
2020-01-03 17:17:04 +00:00
|
|
|
|
"%s::__call to %s method %s", ZSTR_VAL(ce->name),
|
2014-11-29 14:42:41 +00:00
|
|
|
|
(method_ptr == NULL) ? "undefined" : "non-public", method_name);
|
2017-03-10 22:49:18 +00:00
|
|
|
|
|
|
|
|
|
if (error_len > std::numeric_limits<int>::max()) {
|
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
|
"Generated error message length exceeds maximum supported length", 0);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return_value = V8JS_THROW(isolate, TypeError, error, static_cast<int>(error_len));
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
efree(error);
|
|
|
|
|
info.GetReturnValue().Set(return_value);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v8::Local<v8::FunctionTemplate> tmpl =
|
|
|
|
|
v8::Local<v8::FunctionTemplate>::New
|
2016-07-09 09:31:15 +00:00
|
|
|
|
(isolate, *reinterpret_cast<v8js_function_tmpl_t *>(self->GetAlignedPointerFromInternalField(0)));
|
2014-12-13 00:30:23 +00:00
|
|
|
|
// use v8js_php_callback to actually execute the method
|
2014-11-29 14:42:41 +00:00
|
|
|
|
v8::Local<v8::Function> cb = PHP_V8JS_CALLBACK(isolate, method_ptr, tmpl);
|
|
|
|
|
uint32_t i, argc = args->Length();
|
|
|
|
|
v8::Local<v8::Value> *argv = static_cast<v8::Local<v8::Value> *>(alloca(sizeof(v8::Local<v8::Value>) * argc));
|
|
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
|
new(&argv[i]) v8::Local<v8::Value>;
|
2018-09-13 12:01:15 +00:00
|
|
|
|
args->Get(v8_context, i).ToLocal(&argv[i]);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
2018-09-13 12:01:15 +00:00
|
|
|
|
cb->Call(v8_context, info.This(), (int) argc, argv).ToLocal(&return_value);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
info.GetReturnValue().Set(return_value);
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
|
|
/* This method handles named property and method get/set/query/delete. */
|
|
|
|
|
template<typename T>
|
2018-09-12 13:43:40 +00:00
|
|
|
|
v8::Local<v8::Value> v8js_named_property_callback(v8::Local<v8::Name> property_name, const v8::PropertyCallbackInfo<T> &info, property_op_t callback_type, v8::Local<v8::Value> set_value) /* {{{ */
|
2014-11-29 14:42:41 +00:00
|
|
|
|
{
|
2018-09-12 13:43:40 +00:00
|
|
|
|
v8::Local<v8::String> property = v8::Local<v8::String>::Cast(property_name);
|
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
v8::Isolate *isolate = info.GetIsolate();
|
2021-03-21 02:07:53 +00:00
|
|
|
|
v8::Local<v8::Context> v8_context = isolate->GetEnteredOrMicrotaskContext();
|
2015-08-27 12:46:27 +00:00
|
|
|
|
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
|
2018-09-12 13:43:40 +00:00
|
|
|
|
v8::String::Utf8Value cstr(isolate, property);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
const char *name = ToCString(cstr);
|
2020-04-05 04:24:58 +00:00
|
|
|
|
uint32_t name_len = cstr.length();
|
2014-11-29 14:42:41 +00:00
|
|
|
|
char *lower = estrndup(name, name_len);
|
2015-08-29 09:57:53 +00:00
|
|
|
|
zend_string *method_name;
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
v8::Local<v8::Object> self = info.Holder();
|
|
|
|
|
v8::Local<v8::Value> ret_value;
|
|
|
|
|
v8::Local<v8::Function> cb;
|
|
|
|
|
|
|
|
|
|
zend_class_entry *scope, *ce;
|
|
|
|
|
zend_function *method_ptr = NULL;
|
2015-08-29 09:57:53 +00:00
|
|
|
|
zval php_value;
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2016-05-22 20:30:19 +00:00
|
|
|
|
zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
|
2021-08-20 12:08:45 +00:00
|
|
|
|
zend_object &zobject = *object;
|
2015-08-29 09:57:53 +00:00
|
|
|
|
|
2016-07-09 09:31:15 +00:00
|
|
|
|
v8js_function_tmpl_t *tmpl_ptr = reinterpret_cast<v8js_function_tmpl_t *>(self->GetAlignedPointerFromInternalField(0));
|
2015-08-27 12:46:27 +00:00
|
|
|
|
v8::Local<v8::FunctionTemplate> tmpl = v8::Local<v8::FunctionTemplate>::New(isolate, *tmpl_ptr);
|
2015-08-24 19:13:35 +00:00
|
|
|
|
ce = scope = object->ce;
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
/* First, check the (case-insensitive) method table */
|
|
|
|
|
php_strtolower(lower, name_len);
|
2015-08-29 09:57:53 +00:00
|
|
|
|
method_name = zend_string_init(lower, name_len, 0);
|
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
// toString() -> __tostring()
|
|
|
|
|
if (name_len == 8 && strcmp(name, "toString") == 0) {
|
2015-08-29 09:57:53 +00:00
|
|
|
|
zend_string_release(method_name);
|
|
|
|
|
method_name = zend_string_init(ZEND_TOSTRING_FUNC_NAME, sizeof(ZEND_TOSTRING_FUNC_NAME) - 1, 0);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
2015-08-29 09:57:53 +00:00
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
bool is_constructor = (name_len == 11 && strcmp(name, "constructor") == 0);
|
2015-08-29 09:57:53 +00:00
|
|
|
|
bool is_magic_call = (ZSTR_LEN(method_name) == 6 && strcmp(ZSTR_VAL(method_name), "__call") == 0);
|
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
if (is_constructor ||
|
|
|
|
|
(name[0] != '$' /* leading '$' means property, not method */ &&
|
2015-08-29 09:57:53 +00:00
|
|
|
|
(method_ptr = reinterpret_cast<zend_function *>
|
|
|
|
|
(zend_hash_find_ptr(&ce->function_table, method_name))) &&
|
2014-11-29 14:42:41 +00:00
|
|
|
|
((method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) != 0) && /* Allow only public methods */
|
2017-06-13 16:14:12 +00:00
|
|
|
|
((method_ptr->common.fn_flags & (ZEND_ACC_CTOR|ZEND_ACC_DTOR)) == 0) /* no __construct, __destruct(), or __clone() functions */
|
2014-11-29 14:42:41 +00:00
|
|
|
|
) || (method_ptr=NULL, is_magic_call)
|
|
|
|
|
) {
|
|
|
|
|
if (callback_type == V8JS_PROP_GETTER) {
|
|
|
|
|
if (is_constructor) {
|
|
|
|
|
// Don't set a return value here, i.e. indicate that we don't
|
|
|
|
|
// have a special value. V8 "knows" the constructor anyways
|
|
|
|
|
// (from the template) and will use that.
|
|
|
|
|
} else {
|
|
|
|
|
if (is_magic_call && method_ptr==NULL) {
|
|
|
|
|
// Fake __call implementation
|
|
|
|
|
// (only use this if method_ptr==NULL, which means
|
|
|
|
|
// there is no actual PHP __call() implementation)
|
2015-08-27 12:46:27 +00:00
|
|
|
|
v8::Local<v8::FunctionTemplate> ft;
|
|
|
|
|
try {
|
|
|
|
|
ft = v8::Local<v8::FunctionTemplate>::New
|
|
|
|
|
(isolate, ctx->call_impls.at(tmpl_ptr));
|
|
|
|
|
}
|
|
|
|
|
catch (const std::out_of_range &) {
|
|
|
|
|
ft = v8::FunctionTemplate::New(isolate,
|
|
|
|
|
v8js_fake_call_impl, V8JS_NULL,
|
|
|
|
|
v8::Signature::New(isolate, tmpl));
|
2016-07-09 09:31:15 +00:00
|
|
|
|
v8js_function_tmpl_t *persistent_ft = &ctx->call_impls[tmpl_ptr];
|
2015-08-27 12:46:27 +00:00
|
|
|
|
persistent_ft->Reset(isolate, ft);
|
|
|
|
|
}
|
2018-09-13 12:01:15 +00:00
|
|
|
|
|
|
|
|
|
v8::Local<v8::Function> fn;
|
|
|
|
|
if (ft->GetFunction(v8_context).ToLocal(&fn))
|
|
|
|
|
{
|
|
|
|
|
fn->SetName(property);
|
|
|
|
|
ret_value = fn;
|
|
|
|
|
}
|
2014-11-29 14:42:41 +00:00
|
|
|
|
} else {
|
2015-08-27 12:46:27 +00:00
|
|
|
|
v8::Local<v8::FunctionTemplate> ft;
|
|
|
|
|
try {
|
|
|
|
|
ft = v8::Local<v8::FunctionTemplate>::New
|
2019-06-22 17:13:45 +00:00
|
|
|
|
(isolate, ctx->method_tmpls.at(std::make_pair(ce, method_ptr)));
|
2015-08-27 12:46:27 +00:00
|
|
|
|
}
|
|
|
|
|
catch (const std::out_of_range &) {
|
|
|
|
|
ft = v8::FunctionTemplate::New(isolate, v8js_php_callback,
|
|
|
|
|
v8::External::New((isolate), method_ptr),
|
|
|
|
|
v8::Signature::New((isolate), tmpl));
|
2019-06-22 17:13:45 +00:00
|
|
|
|
v8js_function_tmpl_t *persistent_ft = &ctx->method_tmpls[std::make_pair(ce, method_ptr)];
|
2015-08-27 12:46:27 +00:00
|
|
|
|
persistent_ft->Reset(isolate, ft);
|
|
|
|
|
}
|
2018-09-13 12:01:15 +00:00
|
|
|
|
ft->GetFunction(v8_context).ToLocal(&ret_value);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (callback_type == V8JS_PROP_QUERY) {
|
2017-09-24 11:59:34 +00:00
|
|
|
|
ret_value = V8JS_UINT(v8::ReadOnly|v8::DontDelete);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
} else if (callback_type == V8JS_PROP_SETTER) {
|
|
|
|
|
ret_value = set_value; // lie. this field is read-only.
|
|
|
|
|
} else if (callback_type == V8JS_PROP_DELETER) {
|
|
|
|
|
ret_value = V8JS_BOOL(false);
|
|
|
|
|
} else {
|
|
|
|
|
/* shouldn't reach here! but bail safely */
|
2017-03-11 12:31:40 +00:00
|
|
|
|
ret_value = v8::Local<v8::Value>();
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (name[0]=='$') {
|
|
|
|
|
// this is a property (not a method)
|
|
|
|
|
name++; name_len--;
|
|
|
|
|
}
|
2015-08-29 09:57:53 +00:00
|
|
|
|
|
|
|
|
|
zval zname;
|
|
|
|
|
ZVAL_STRINGL(&zname, name, name_len);
|
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
if (callback_type == V8JS_PROP_GETTER) {
|
|
|
|
|
/* Nope, not a method -- must be a (case-sensitive) property */
|
2017-03-11 12:35:21 +00:00
|
|
|
|
zend_property_info *property_info = zend_get_property_info(ce, Z_STR(zname), 1);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2015-09-28 20:57:00 +00:00
|
|
|
|
if(!property_info ||
|
|
|
|
|
(property_info != ZEND_WRONG_PROPERTY_INFO &&
|
|
|
|
|
property_info->flags & ZEND_ACC_PUBLIC)) {
|
2017-03-11 12:35:21 +00:00
|
|
|
|
zval *property_val = zend_read_property(NULL, &zobject, name, name_len, true, &php_value);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
// special case uninitialized_zval_ptr and return an empty value
|
|
|
|
|
// (indicating that we don't intercept this property) if the
|
|
|
|
|
// property doesn't exist.
|
2015-08-29 09:57:53 +00:00
|
|
|
|
if (property_val == &EG(uninitialized_zval)) {
|
2017-03-11 12:31:40 +00:00
|
|
|
|
ret_value = v8::Local<v8::Value>();
|
2014-11-29 14:42:41 +00:00
|
|
|
|
} else {
|
|
|
|
|
// wrap it
|
2017-03-11 12:35:21 +00:00
|
|
|
|
ret_value = zval_to_v8js(property_val, isolate);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
/* We don't own the reference to php_value... unless the
|
|
|
|
|
* returned refcount was 0, in which case the below code
|
|
|
|
|
* will free it. */
|
2015-08-29 09:57:53 +00:00
|
|
|
|
zval_add_ref(property_val);
|
|
|
|
|
zval_ptr_dtor(property_val);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-29 09:57:53 +00:00
|
|
|
|
else if (ce->__get
|
2014-11-29 14:42:41 +00:00
|
|
|
|
/* Allow only public methods */
|
2015-08-29 09:57:53 +00:00
|
|
|
|
&& ((ce->__get->common.fn_flags & ZEND_ACC_PUBLIC) != 0)) {
|
2014-11-29 14:42:41 +00:00
|
|
|
|
/* Okay, let's call __get. */
|
2015-08-29 09:57:53 +00:00
|
|
|
|
zend_call_method_with_1_params(&zobject, ce, &ce->__get, ZEND_GET_FUNC_NAME, &php_value, &zname);
|
2017-03-11 12:35:21 +00:00
|
|
|
|
ret_value = zval_to_v8js(&php_value, isolate);
|
2016-08-11 23:02:56 +00:00
|
|
|
|
zval_ptr_dtor(&php_value);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
2015-08-29 09:57:53 +00:00
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
} else if (callback_type == V8JS_PROP_SETTER) {
|
2017-03-11 12:35:21 +00:00
|
|
|
|
if (v8js_to_zval(set_value, &php_value, ctx->flags, isolate) != SUCCESS) {
|
2017-03-11 12:31:40 +00:00
|
|
|
|
ret_value = v8::Local<v8::Value>();
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
2017-03-11 12:35:21 +00:00
|
|
|
|
zend_property_info *property_info = zend_get_property_info(ce, Z_STR(zname), 1);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2015-09-28 20:57:00 +00:00
|
|
|
|
if(!property_info ||
|
|
|
|
|
(property_info != ZEND_WRONG_PROPERTY_INFO &&
|
|
|
|
|
property_info->flags & ZEND_ACC_PUBLIC)) {
|
2017-03-11 12:35:21 +00:00
|
|
|
|
zend_update_property(scope, &zobject, name, name_len, &php_value);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
ret_value = set_value;
|
|
|
|
|
}
|
2015-08-29 09:57:53 +00:00
|
|
|
|
else if (ce->__set
|
2014-11-29 14:42:41 +00:00
|
|
|
|
/* Allow only public methods */
|
2015-08-29 09:57:53 +00:00
|
|
|
|
&& ((ce->__set->common.fn_flags & ZEND_ACC_PUBLIC) != 0)) {
|
2014-11-29 14:42:41 +00:00
|
|
|
|
/* Okay, let's call __set. */
|
2015-08-29 09:57:53 +00:00
|
|
|
|
zval php_ret_value;
|
|
|
|
|
zend_call_method_with_2_params(&zobject, ce, &ce->__set, ZEND_SET_FUNC_NAME, &php_ret_value, &zname, &php_value);
|
2017-03-11 12:35:21 +00:00
|
|
|
|
ret_value = zval_to_v8js(&php_ret_value, isolate);
|
2015-12-30 14:54:47 +00:00
|
|
|
|
zval_ptr_dtor(&php_ret_value);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if PHP wanted to hold on to this value, update_property would
|
|
|
|
|
// have bumped the refcount
|
|
|
|
|
zval_ptr_dtor(&php_value);
|
|
|
|
|
} else if (callback_type == V8JS_PROP_QUERY ||
|
|
|
|
|
callback_type == V8JS_PROP_DELETER) {
|
2015-08-24 19:13:35 +00:00
|
|
|
|
const zend_object_handlers *h = object->handlers;
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
if (callback_type == V8JS_PROP_QUERY) {
|
2022-05-30 12:25:37 +00:00
|
|
|
|
if (h->has_property(&zobject, Z_STR_P(&zname), 0, NULL)) {
|
2014-11-29 14:42:41 +00:00
|
|
|
|
ret_value = V8JS_UINT(v8::None);
|
|
|
|
|
} else {
|
2017-03-11 12:31:40 +00:00
|
|
|
|
ret_value = v8::Local<v8::Value>(); // empty handle
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2017-03-11 12:35:21 +00:00
|
|
|
|
zend_property_info *property_info = zend_get_property_info(ce, Z_STR(zname), 1);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2015-09-28 20:57:00 +00:00
|
|
|
|
if(!property_info ||
|
|
|
|
|
(property_info != ZEND_WRONG_PROPERTY_INFO &&
|
|
|
|
|
property_info->flags & ZEND_ACC_PUBLIC)) {
|
2022-05-30 12:25:37 +00:00
|
|
|
|
h->unset_property(&zobject, Z_STR_P(&zname), NULL);
|
2015-08-29 09:57:53 +00:00
|
|
|
|
ret_value = V8JS_TRUE();
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
2017-03-11 12:31:40 +00:00
|
|
|
|
ret_value = v8::Local<v8::Value>(); // empty handle
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/* shouldn't reach here! but bail safely */
|
2017-03-11 12:31:40 +00:00
|
|
|
|
ret_value = v8::Local<v8::Value>();
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
2015-08-29 09:57:53 +00:00
|
|
|
|
|
2016-08-11 23:02:56 +00:00
|
|
|
|
zval_ptr_dtor(&zname);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-29 09:57:53 +00:00
|
|
|
|
zend_string_release(method_name);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
efree(lower);
|
|
|
|
|
return ret_value;
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
2018-09-12 13:43:40 +00:00
|
|
|
|
static void v8js_named_property_getter(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */
|
2014-11-29 14:42:41 +00:00
|
|
|
|
{
|
2014-12-13 00:30:23 +00:00
|
|
|
|
info.GetReturnValue().Set(v8js_named_property_callback(property, info, V8JS_PROP_GETTER));
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
2018-09-12 13:43:40 +00:00
|
|
|
|
static void v8js_named_property_setter(v8::Local<v8::Name> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */
|
2014-11-29 14:42:41 +00:00
|
|
|
|
{
|
2014-12-13 00:30:23 +00:00
|
|
|
|
info.GetReturnValue().Set(v8js_named_property_callback(property, info, V8JS_PROP_SETTER, value));
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
2018-09-12 13:43:40 +00:00
|
|
|
|
static void v8js_named_property_query(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Integer> &info) /* {{{ */
|
2014-11-29 14:42:41 +00:00
|
|
|
|
{
|
2014-12-13 00:30:23 +00:00
|
|
|
|
v8::Local<v8::Value> r = v8js_named_property_callback(property, info, V8JS_PROP_QUERY);
|
2018-09-12 13:43:40 +00:00
|
|
|
|
if (r.IsEmpty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v8::Isolate *isolate = info.GetIsolate();
|
2021-03-21 02:07:53 +00:00
|
|
|
|
v8::MaybeLocal<v8::Integer> value = r->ToInteger(isolate->GetEnteredOrMicrotaskContext());
|
2018-09-12 13:43:40 +00:00
|
|
|
|
if (!value.IsEmpty()) {
|
|
|
|
|
info.GetReturnValue().Set(value.ToLocalChecked());
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
2018-09-12 13:43:40 +00:00
|
|
|
|
static void v8js_named_property_deleter(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Boolean> &info) /* {{{ */
|
2014-11-29 14:42:41 +00:00
|
|
|
|
{
|
2014-12-13 00:30:23 +00:00
|
|
|
|
v8::Local<v8::Value> r = v8js_named_property_callback(property, info, V8JS_PROP_DELETER);
|
2018-09-12 13:43:40 +00:00
|
|
|
|
if (r.IsEmpty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v8::Isolate *isolate = info.GetIsolate();
|
2020-02-14 00:42:40 +00:00
|
|
|
|
v8::Local<v8::Boolean> value = r->ToBoolean(isolate);
|
2018-09-12 13:43:40 +00:00
|
|
|
|
if (!value.IsEmpty()) {
|
2020-02-14 00:42:40 +00:00
|
|
|
|
info.GetReturnValue().Set(value);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-24 18:43:52 +00:00
|
|
|
|
static v8::MaybeLocal<v8::Object> v8js_wrap_object(v8::Isolate *isolate, zend_class_entry *ce, zval *value) /* {{{ */
|
2014-11-29 15:38:45 +00:00
|
|
|
|
{
|
2014-12-13 00:15:29 +00:00
|
|
|
|
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
|
2018-09-13 12:01:15 +00:00
|
|
|
|
v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(isolate, ctx->context);
|
2014-11-29 15:38:45 +00:00
|
|
|
|
v8::Local<v8::FunctionTemplate> new_tpl;
|
2016-07-09 09:31:15 +00:00
|
|
|
|
v8js_function_tmpl_t *persist_tpl_;
|
2014-11-29 15:38:45 +00:00
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
new_tpl = v8::Local<v8::FunctionTemplate>::New
|
|
|
|
|
(isolate, ctx->template_cache.at(ce->name));
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
2014-11-29 15:38:45 +00:00
|
|
|
|
catch (const std::out_of_range &) {
|
|
|
|
|
/* No cached v8::FunctionTemplate available as of yet, create one. */
|
|
|
|
|
new_tpl = v8::FunctionTemplate::New(isolate, 0);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2017-03-10 22:49:18 +00:00
|
|
|
|
if (ZSTR_LEN(ce->name) > std::numeric_limits<int>::max()) {
|
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
|
"Class name length exceeds maximum supported length", 0);
|
|
|
|
|
return v8::Local<v8::Object>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new_tpl->SetClassName(V8JS_STRL(ZSTR_VAL(ce->name), static_cast<int>(ZSTR_LEN(ce->name))));
|
2016-05-22 13:12:44 +00:00
|
|
|
|
new_tpl->InstanceTemplate()->SetInternalFieldCount(2);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2014-11-29 15:38:45 +00:00
|
|
|
|
if (ce == zend_ce_closure) {
|
|
|
|
|
/* Got a closure, mustn't cache ... */
|
2016-07-09 09:31:15 +00:00
|
|
|
|
persist_tpl_ = new v8js_function_tmpl_t(isolate, new_tpl);
|
2014-12-13 00:30:23 +00:00
|
|
|
|
/* We'll free persist_tpl_ via v8js_weak_closure_callback, below */
|
|
|
|
|
new_tpl->InstanceTemplate()->SetCallAsFunctionHandler(v8js_php_callback);
|
2014-11-29 15:38:45 +00:00
|
|
|
|
} else {
|
|
|
|
|
/* Add new v8::FunctionTemplate to tpl_map, as long as it is not a closure. */
|
|
|
|
|
persist_tpl_ = &ctx->template_cache[ce->name];
|
|
|
|
|
persist_tpl_->Reset(isolate, new_tpl);
|
|
|
|
|
/* We'll free persist_tpl_ when template_cache is destroyed */
|
2014-11-29 19:35:32 +00:00
|
|
|
|
|
|
|
|
|
v8::Local<v8::ObjectTemplate> inst_tpl = new_tpl->InstanceTemplate();
|
2018-09-12 13:43:40 +00:00
|
|
|
|
v8::GenericNamedPropertyGetterCallback getter = v8js_named_property_getter;
|
|
|
|
|
v8::GenericNamedPropertyEnumeratorCallback enumerator = v8js_named_property_enumerator;
|
2014-11-29 19:35:32 +00:00
|
|
|
|
|
|
|
|
|
/* Check for ArrayAccess object */
|
|
|
|
|
if (V8JSG(use_array_access) && ce) {
|
|
|
|
|
bool has_array_access = false;
|
|
|
|
|
bool has_countable = false;
|
|
|
|
|
|
2016-07-03 20:31:09 +00:00
|
|
|
|
for (unsigned int i = 0; i < ce->num_interfaces; i ++) {
|
2015-08-29 09:57:53 +00:00
|
|
|
|
if (strcmp (ZSTR_VAL(ce->interfaces[i]->name), "ArrayAccess") == 0) {
|
2014-11-29 19:35:32 +00:00
|
|
|
|
has_array_access = true;
|
|
|
|
|
}
|
2015-08-29 09:57:53 +00:00
|
|
|
|
else if (strcmp (ZSTR_VAL(ce->interfaces[i]->name), "Countable") == 0) {
|
2014-11-29 19:35:32 +00:00
|
|
|
|
has_countable = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(has_array_access && has_countable) {
|
2014-12-13 00:30:23 +00:00
|
|
|
|
inst_tpl->SetIndexedPropertyHandler(v8js_array_access_getter,
|
|
|
|
|
v8js_array_access_setter,
|
|
|
|
|
v8js_array_access_query,
|
|
|
|
|
v8js_array_access_deleter,
|
|
|
|
|
v8js_array_access_enumerator);
|
2014-11-29 19:35:32 +00:00
|
|
|
|
|
|
|
|
|
/* Switch to special ArrayAccess getter, which falls back to
|
2014-12-13 00:30:23 +00:00
|
|
|
|
* v8js_named_property_getter, but possibly bridges the
|
2014-11-29 19:35:32 +00:00
|
|
|
|
* call to Array.prototype functions. */
|
2014-12-13 00:30:23 +00:00
|
|
|
|
getter = v8js_array_access_named_getter;
|
2014-11-30 20:00:42 +00:00
|
|
|
|
|
|
|
|
|
/* Overwrite enumerator, since for(... in ...) loop should
|
|
|
|
|
* not see the methods but iterate over the elements. */
|
|
|
|
|
enumerator = 0;
|
2014-11-29 19:35:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-11-29 15:38:45 +00:00
|
|
|
|
// Finish setup of new_tpl
|
2018-09-12 13:43:40 +00:00
|
|
|
|
inst_tpl->SetHandler(v8::NamedPropertyHandlerConfiguration
|
2014-11-29 19:35:32 +00:00
|
|
|
|
(getter, /* getter */
|
2014-12-13 00:30:23 +00:00
|
|
|
|
v8js_named_property_setter, /* setter */
|
|
|
|
|
v8js_named_property_query, /* query */
|
|
|
|
|
v8js_named_property_deleter, /* deleter */
|
2014-11-30 20:00:42 +00:00
|
|
|
|
enumerator, /* enumerator */
|
2014-11-29 15:38:45 +00:00
|
|
|
|
V8JS_NULL /* data */
|
2018-09-12 13:43:40 +00:00
|
|
|
|
));
|
2014-11-29 15:38:45 +00:00
|
|
|
|
// add __invoke() handler
|
2015-08-29 09:57:53 +00:00
|
|
|
|
zend_string *invoke_str = zend_string_init
|
|
|
|
|
(ZEND_INVOKE_FUNC_NAME, sizeof(ZEND_INVOKE_FUNC_NAME) - 1, 0);
|
|
|
|
|
zend_function *invoke_method_ptr = reinterpret_cast<zend_function *>
|
2015-08-29 22:53:56 +00:00
|
|
|
|
(zend_hash_find_ptr(&ce->function_table, invoke_str));
|
2015-08-29 09:57:53 +00:00
|
|
|
|
if (invoke_method_ptr &&
|
2014-11-29 15:38:45 +00:00
|
|
|
|
invoke_method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) {
|
2014-12-13 00:30:23 +00:00
|
|
|
|
new_tpl->InstanceTemplate()->SetCallAsFunctionHandler(v8js_invoke_callback, PHP_V8JS_CALLBACK(isolate, invoke_method_ptr, new_tpl));
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
2015-08-29 09:57:53 +00:00
|
|
|
|
zend_string_release(invoke_str);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
2014-11-29 15:38:45 +00:00
|
|
|
|
v8::Local<v8::Array> call_handler_data = v8::Array::New(isolate, 2);
|
2018-09-13 12:01:15 +00:00
|
|
|
|
call_handler_data->Set(v8_context, 0, v8::External::New(isolate, persist_tpl_));
|
|
|
|
|
call_handler_data->Set(v8_context, 1, v8::External::New(isolate, ce));
|
2014-12-13 00:30:23 +00:00
|
|
|
|
new_tpl->SetCallHandler(v8js_construct_callback, call_handler_data);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-29 15:38:45 +00:00
|
|
|
|
// Create v8 wrapper object
|
2017-03-11 12:31:40 +00:00
|
|
|
|
v8::Local<v8::Value> external = v8::External::New(isolate, Z_OBJ_P(value));
|
2018-09-13 12:01:15 +00:00
|
|
|
|
|
|
|
|
|
v8::Local<v8::Function> constr;
|
|
|
|
|
if (!new_tpl->GetFunction(v8_context).ToLocal(&constr)) {
|
|
|
|
|
return v8::MaybeLocal<v8::Object>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v8::MaybeLocal<v8::Object> newobj = constr->NewInstance(v8_context, 1, &external);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2017-04-24 18:43:52 +00:00
|
|
|
|
if (ce == zend_ce_closure && !newobj.IsEmpty()) {
|
2014-11-29 15:38:45 +00:00
|
|
|
|
// free uncached function template when object is freed
|
2017-04-24 18:43:52 +00:00
|
|
|
|
ctx->weak_closures[persist_tpl_].Reset(isolate, newobj.ToLocalChecked());
|
2016-05-22 18:08:28 +00:00
|
|
|
|
ctx->weak_closures[persist_tpl_].SetWeak(persist_tpl_, v8js_weak_closure_callback, v8::WeakCallbackType::kParameter);
|
2014-11-29 15:38:45 +00:00
|
|
|
|
}
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2014-11-29 15:38:45 +00:00
|
|
|
|
return newobj;
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
|
2017-03-11 12:35:21 +00:00
|
|
|
|
static v8::Local<v8::Object> v8js_wrap_array_to_object(v8::Isolate *isolate, zval *value) /* {{{ */
|
2014-11-29 15:38:45 +00:00
|
|
|
|
{
|
|
|
|
|
int i;
|
2015-08-29 09:57:53 +00:00
|
|
|
|
zend_string *key;
|
2017-03-10 22:49:18 +00:00
|
|
|
|
zend_ulong index;
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2015-09-01 13:53:21 +00:00
|
|
|
|
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
|
2018-09-13 12:01:15 +00:00
|
|
|
|
v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(isolate, ctx->context);
|
2015-09-01 13:53:21 +00:00
|
|
|
|
v8::Local<v8::FunctionTemplate> new_tpl;
|
|
|
|
|
|
|
|
|
|
if(ctx->array_tmpl.IsEmpty()) {
|
|
|
|
|
new_tpl = v8::FunctionTemplate::New(isolate, 0);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2015-09-01 13:53:21 +00:00
|
|
|
|
/* Call it Array, but it is not a native array, especially it doesn't have
|
|
|
|
|
* have the typical Array.prototype functions. */
|
|
|
|
|
new_tpl->SetClassName(V8JS_SYM("Array"));
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2015-09-01 13:53:21 +00:00
|
|
|
|
/* Store for later re-use */
|
|
|
|
|
ctx->array_tmpl.Reset(isolate, new_tpl);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
new_tpl = v8::Local<v8::FunctionTemplate>::New(isolate, ctx->array_tmpl);
|
|
|
|
|
}
|
2014-11-29 15:38:45 +00:00
|
|
|
|
|
2018-09-13 12:01:15 +00:00
|
|
|
|
v8::Local<v8::Object> newobj = new_tpl->InstanceTemplate()->NewInstance(v8_context).ToLocalChecked();
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2014-11-29 15:38:45 +00:00
|
|
|
|
HashTable *myht = HASH_OF(value);
|
2014-11-29 14:42:41 +00:00
|
|
|
|
i = myht ? zend_hash_num_elements(myht) : 0;
|
|
|
|
|
|
2014-11-29 15:38:45 +00:00
|
|
|
|
if (i > 0)
|
2014-11-29 14:42:41 +00:00
|
|
|
|
{
|
2015-08-29 09:57:53 +00:00
|
|
|
|
zval *data;
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2018-07-05 09:41:32 +00:00
|
|
|
|
if (myht && !(GC_FLAGS(myht) & GC_IMMUTABLE)) {
|
|
|
|
|
GC_PROTECT_RECURSION(myht);
|
|
|
|
|
}
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2018-07-05 09:41:32 +00:00
|
|
|
|
ZEND_HASH_FOREACH_KEY_VAL(myht, index, key, data) {
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2015-08-29 09:57:53 +00:00
|
|
|
|
if (key) {
|
|
|
|
|
if (ZSTR_VAL(key)[0] == '\0' && Z_TYPE_P(value) == IS_OBJECT) {
|
|
|
|
|
/* Skip protected and private members. */
|
|
|
|
|
continue;
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
2017-03-10 22:49:18 +00:00
|
|
|
|
|
|
|
|
|
if (ZSTR_LEN(key) > std::numeric_limits<int>::max()) {
|
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
|
"Object key length exceeds maximum supported length", 0);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 12:01:15 +00:00
|
|
|
|
newobj->Set(v8_context, V8JS_STRL(ZSTR_VAL(key), static_cast<int>(ZSTR_LEN(key))),
|
2017-03-10 22:49:18 +00:00
|
|
|
|
zval_to_v8js(data, isolate));
|
2015-08-29 09:57:53 +00:00
|
|
|
|
} else {
|
2017-03-10 22:49:18 +00:00
|
|
|
|
if (index > std::numeric_limits<uint32_t>::max()) {
|
|
|
|
|
zend_throw_exception(php_ce_v8js_exception,
|
|
|
|
|
"Array index exceeds maximum supported bound", 0);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 12:01:15 +00:00
|
|
|
|
newobj->Set(v8_context, static_cast<uint32_t>(index), zval_to_v8js(data, isolate));
|
2015-08-29 09:57:53 +00:00
|
|
|
|
}
|
2014-11-29 14:42:41 +00:00
|
|
|
|
|
2015-08-29 09:57:53 +00:00
|
|
|
|
} ZEND_HASH_FOREACH_END();
|
2018-07-05 09:41:32 +00:00
|
|
|
|
|
|
|
|
|
if (myht && !(GC_FLAGS(myht) & GC_IMMUTABLE)) {
|
|
|
|
|
GC_UNPROTECT_RECURSION(myht);
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
}
|
2014-11-29 15:38:45 +00:00
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
return newobj;
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
2014-11-29 15:38:45 +00:00
|
|
|
|
|
2017-03-11 12:35:21 +00:00
|
|
|
|
v8::Local<v8::Value> v8js_hash_to_jsobj(zval *value, v8::Isolate *isolate) /* {{{ */
|
2014-11-29 15:38:45 +00:00
|
|
|
|
{
|
|
|
|
|
HashTable *myht;
|
|
|
|
|
zend_class_entry *ce = NULL;
|
|
|
|
|
|
|
|
|
|
if (Z_TYPE_P(value) == IS_ARRAY) {
|
|
|
|
|
myht = HASH_OF(value);
|
|
|
|
|
} else {
|
|
|
|
|
myht = Z_OBJPROP_P(value);
|
|
|
|
|
ce = Z_OBJCE_P(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Prevent recursion */
|
2018-07-05 09:30:20 +00:00
|
|
|
|
if (myht && GC_IS_RECURSIVE(myht)) {
|
2014-11-29 15:38:45 +00:00
|
|
|
|
return V8JS_NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Special case, passing back object originating from JS to JS */
|
2016-05-22 21:47:36 +00:00
|
|
|
|
if (ce == php_ce_v8function || ce == php_ce_v8object || ce == php_ce_v8generator) {
|
2015-08-29 14:47:37 +00:00
|
|
|
|
v8js_v8object *c = Z_V8JS_V8OBJECT_OBJ_P(value);
|
2014-11-29 15:38:45 +00:00
|
|
|
|
|
|
|
|
|
if(isolate != c->ctx->isolate) {
|
2017-03-11 12:35:21 +00:00
|
|
|
|
php_error_docref(NULL, E_WARNING, "V8Function object passed to wrong V8Js instance");
|
2014-11-29 15:38:45 +00:00
|
|
|
|
return V8JS_NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return v8::Local<v8::Value>::New(isolate, c->v8obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If it's a PHP object, wrap it */
|
|
|
|
|
if (ce) {
|
2017-04-24 18:43:52 +00:00
|
|
|
|
v8::MaybeLocal<v8::Object> wrapped_object = v8js_wrap_object(isolate, ce, value);
|
|
|
|
|
|
|
|
|
|
if (wrapped_object.IsEmpty()) {
|
|
|
|
|
return V8JS_UNDEFINED;
|
|
|
|
|
}
|
2016-01-09 11:50:40 +00:00
|
|
|
|
|
|
|
|
|
if (ce == zend_ce_generator) {
|
|
|
|
|
/* Wrap PHP Generator object in a wrapper function that provides
|
|
|
|
|
* ES6 style behaviour. */
|
2017-04-24 18:43:52 +00:00
|
|
|
|
return v8js_wrap_generator(isolate, wrapped_object.ToLocalChecked());
|
2016-01-09 11:50:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-24 18:43:52 +00:00
|
|
|
|
return wrapped_object.ToLocalChecked();
|
2014-11-29 15:38:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Associative PHP arrays cannot be wrapped to JS arrays, convert them to
|
|
|
|
|
* JS objects and attach all their array keys as properties. */
|
2017-03-11 12:35:21 +00:00
|
|
|
|
return v8js_wrap_array_to_object(isolate, value);
|
2014-11-29 15:38:45 +00:00
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
2014-11-29 14:42:41 +00:00
|
|
|
|
/*
|
|
|
|
|
* Local variables:
|
|
|
|
|
* tab-width: 4
|
|
|
|
|
* c-basic-offset: 4
|
2016-07-02 20:41:01 +00:00
|
|
|
|
* indent-tabs-mode: t
|
2014-11-29 14:42:41 +00:00
|
|
|
|
* End:
|
|
|
|
|
* vim600: noet sw=4 ts=4 fdm=marker
|
|
|
|
|
* vim<600: noet sw=4 ts=4
|
|
|
|
|
*/
|