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

262 lines
7.3 KiB
C++
Raw Normal View History

/*
+----------------------------------------------------------------------+
2017-03-11 11:33:49 +00:00
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2017 The PHP Group |
+----------------------------------------------------------------------+
| http://www.opensource.org/licenses/mit-license.php MIT License |
+----------------------------------------------------------------------+
| Author: Stefan Siegl <stesie@php.net> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php_v8js_macros.h"
#include "v8js_array_access.h"
#include "v8js_exceptions.h"
#include "v8js_object_export.h"
extern "C" {
#include "php.h"
#include "ext/date/php_date.h"
#include "ext/standard/php_string.h"
#include "zend_interfaces.h"
#include "zend_closures.h"
#include "zend_exceptions.h"
}
2015-08-24 18:43:24 +00:00
static zval v8js_array_access_dispatch(zend_object *object, const char *method_name, int param_count,
2017-03-11 12:35:21 +00:00
uint32_t index, zval zvalue) /* {{{ */
2014-11-30 22:30:28 +00:00
{
zend_fcall_info fci;
2015-08-24 18:43:24 +00:00
zval php_value;
fci.size = sizeof(fci);
2016-07-02 22:25:42 +00:00
#if (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION == 0)
2015-08-24 18:43:24 +00:00
fci.function_table = &object->ce->function_table;
fci.symbol_table = NULL;
2016-07-02 22:25:42 +00:00
#endif
ZVAL_STRING(&fci.function_name, method_name);
2015-08-24 18:43:24 +00:00
fci.retval = &php_value;
2015-08-24 18:43:24 +00:00
zval params[2];
ZVAL_LONG(&params[0], index);
params[1] = zvalue;
2014-11-30 22:30:28 +00:00
fci.param_count = param_count;
fci.params = params;
2015-08-24 18:43:24 +00:00
fci.object = object;
fci.no_separation = 0;
2017-03-11 12:35:21 +00:00
zend_call_function(&fci, NULL);
zval_dtor(&fci.function_name);
2014-11-30 22:30:28 +00:00
return php_value;
}
/* }}} */
2014-11-30 22:30:28 +00:00
2014-12-13 00:30:23 +00:00
void v8js_array_access_getter(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
2014-11-30 22:30:28 +00:00
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
2015-08-24 18:43:24 +00:00
zval zvalue;
ZVAL_UNDEF(&zvalue);
2014-11-30 22:30:28 +00:00
2017-03-11 12:35:21 +00:00
zval php_value = v8js_array_access_dispatch(object, "offsetGet", 1, index, zvalue);
v8::Local<v8::Value> ret_value = zval_to_v8js(&php_value, isolate);
zval_ptr_dtor(&php_value);
info.GetReturnValue().Set(ret_value);
}
/* }}} */
2014-12-13 00:30:23 +00:00
void v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
2014-11-25 22:43:21 +00:00
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
2015-08-24 18:43:24 +00:00
zval zvalue;
ZVAL_UNDEF(&zvalue);
2014-11-25 22:43:21 +00:00
2017-03-11 12:35:21 +00:00
if (v8js_to_zval(value, &zvalue, 0, isolate) != SUCCESS) {
2017-03-11 12:31:40 +00:00
info.GetReturnValue().Set(v8::Local<v8::Value>());
2014-11-25 22:43:21 +00:00
return;
}
2017-03-11 12:35:21 +00:00
zval php_value = v8js_array_access_dispatch(object, "offsetSet", 2, index, zvalue);
zval_ptr_dtor(&php_value);
2014-11-25 22:43:21 +00:00
/* simply pass back the value to tell we intercepted the call
* as the offsetSet function returns void. */
info.GetReturnValue().Set(value);
2014-11-25 22:55:17 +00:00
/* if PHP wanted to hold on to this value, zend_call_function would
* have bumped the refcount. */
zval_ptr_dtor(&zvalue);
2014-11-25 22:43:21 +00:00
}
/* }}} */
2014-11-25 22:55:17 +00:00
2017-03-11 12:35:21 +00:00
static int v8js_array_access_get_count_result(zend_object *object) /* {{{ */
{
2015-08-24 18:43:24 +00:00
zval zvalue;
ZVAL_UNDEF(&zvalue);
2017-03-11 12:35:21 +00:00
zval php_value = v8js_array_access_dispatch(object, "count", 0, 0, zvalue);
2015-08-24 18:43:24 +00:00
if(Z_TYPE(php_value) != IS_LONG) {
2017-03-11 12:35:21 +00:00
php_error_docref(NULL, E_WARNING, "Non-numeric return value from count() method");
zval_ptr_dtor(&php_value);
return 0;
}
zend_long result = Z_LVAL(php_value);
if (result > std::numeric_limits<int>::max()) {
zend_throw_exception(php_ce_v8js_exception,
"Array size/offset exceeds maximum supported length", 0);
return 0;
}
return static_cast<int>(result);
}
/* }}} */
2017-03-11 12:35:21 +00:00
static bool v8js_array_access_isset_p(zend_object *object, int index) /* {{{ */
{
2015-08-24 18:43:24 +00:00
zval zvalue;
ZVAL_UNDEF(&zvalue);
2017-03-11 12:35:21 +00:00
zval php_value = v8js_array_access_dispatch(object, "offsetExists", 1, index, zvalue);
2015-08-24 18:43:24 +00:00
if(Z_TYPE(php_value) != IS_TRUE && Z_TYPE(php_value) != IS_FALSE) {
2017-03-11 12:35:21 +00:00
php_error_docref(NULL, E_WARNING, "Non-boolean return value from offsetExists() method");
zval_ptr_dtor(&php_value);
return false;
}
2015-08-24 18:43:24 +00:00
return Z_TYPE(php_value) == IS_TRUE;
}
/* }}} */
2014-12-13 00:30:23 +00:00
static void v8js_array_access_length(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
2017-03-11 12:35:21 +00:00
int length = v8js_array_access_get_count_result(object);
info.GetReturnValue().Set(V8JS_INT(length));
}
/* }}} */
2014-12-13 00:30:23 +00:00
void v8js_array_access_deleter(uint32_t index, const v8::PropertyCallbackInfo<v8::Boolean>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
2015-08-24 18:43:24 +00:00
zval zvalue;
ZVAL_UNDEF(&zvalue);
2017-03-11 12:35:21 +00:00
zval php_value = v8js_array_access_dispatch(object, "offsetUnset", 1, index, zvalue);
zval_ptr_dtor(&php_value);
2014-11-30 22:30:28 +00:00
info.GetReturnValue().Set(V8JS_BOOL(true));
}
/* }}} */
2014-12-13 00:30:23 +00:00
void v8js_array_access_query(uint32_t index, const v8::PropertyCallbackInfo<v8::Integer>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
/* If index is set, then return an integer encoding a v8::PropertyAttribute;
* otherwise we're expected to return an empty handle. */
2017-03-11 12:35:21 +00:00
if(v8js_array_access_isset_p(object, index)) {
info.GetReturnValue().Set(V8JS_UINT(v8::PropertyAttribute::None));
}
}
/* }}} */
2014-12-13 00:30:23 +00:00
void v8js_array_access_enumerator(const v8::PropertyCallbackInfo<v8::Array>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
2017-03-11 12:35:21 +00:00
int length = v8js_array_access_get_count_result(object);
v8::Local<v8::Array> result = v8::Array::New(isolate, length);
int i = 0;
for(int j = 0; j < length; j ++) {
2017-03-11 12:35:21 +00:00
if(v8js_array_access_isset_p(object, j)) {
result->Set(i ++, V8JS_INT(j));
}
}
result->Set(V8JS_STR("length"), V8JS_INT(i));
info.GetReturnValue().Set(result);
}
/* }}} */
2014-12-13 00:30:23 +00:00
void v8js_array_access_named_getter(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */
{
v8::String::Utf8Value cstr(property);
const char *name = ToCString(cstr);
if(strcmp(name, "length") == 0) {
2014-12-13 00:30:23 +00:00
v8js_array_access_length(property, info);
return;
}
2014-12-13 00:30:23 +00:00
v8::Local<v8::Value> ret_value = v8js_named_property_callback(property, info, V8JS_PROP_GETTER);
if(ret_value.IsEmpty()) {
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Array> arr = v8::Array::New(isolate);
v8::Local<v8::Value> prototype = arr->GetPrototype();
if(!prototype->IsObject()) {
/* ehh? Array.prototype not an object? strange, stop. */
info.GetReturnValue().Set(ret_value);
}
ret_value = prototype->ToObject()->Get(property);
}
info.GetReturnValue().Set(ret_value);
}
/* }}} */
2014-11-25 22:43:21 +00:00
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: t
2014-11-25 22:43:21 +00:00
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/