2014-11-25 20:06:11 +00:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| PHP Version 5 |
|
|
|
|
+----------------------------------------------------------------------+
|
2016-05-22 13:12:44 +00:00
|
|
|
| Copyright (c) 1997-2016 The PHP Group |
|
2014-11-25 20:06:11 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| http://www.opensource.org/licenses/mit-license.php MIT License |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Author: Stefan Siegl <stesie@brokenpipe.de> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "php.h"
|
|
|
|
#include "ext/date/php_date.h"
|
|
|
|
#include "ext/standard/php_string.h"
|
|
|
|
#include "zend_interfaces.h"
|
|
|
|
#include "zend_closures.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "php_v8js_macros.h"
|
|
|
|
#include "v8js_array_access.h"
|
2014-11-29 19:35:32 +00:00
|
|
|
#include "v8js_object_export.h"
|
2014-11-25 20:06:11 +00:00
|
|
|
|
|
|
|
|
2014-12-13 00:30:23 +00:00
|
|
|
static zval *v8js_array_access_dispatch(zval *object, const char *method_name, int param_count,
|
2014-11-30 22:30:28 +00:00
|
|
|
uint32_t index, zval *zvalue_ptr TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
2014-11-25 20:06:11 +00:00
|
|
|
zend_class_entry *ce = Z_OBJCE_P(object);
|
|
|
|
|
|
|
|
/* Okay, let's call offsetGet. */
|
|
|
|
zend_fcall_info fci;
|
|
|
|
zval *php_value;
|
|
|
|
|
|
|
|
zval fmember;
|
|
|
|
INIT_ZVAL(fmember);
|
2014-11-30 22:30:28 +00:00
|
|
|
ZVAL_STRING(&fmember, method_name, 0);
|
2014-11-25 20:06:11 +00:00
|
|
|
|
|
|
|
zval zindex;
|
|
|
|
INIT_ZVAL(zindex);
|
|
|
|
ZVAL_LONG(&zindex, index);
|
|
|
|
|
|
|
|
fci.size = sizeof(fci);
|
|
|
|
fci.function_table = &ce->function_table;
|
|
|
|
fci.function_name = &fmember;
|
|
|
|
fci.symbol_table = NULL;
|
|
|
|
fci.retval_ptr_ptr = &php_value;
|
|
|
|
|
|
|
|
zval *zindex_ptr = &zindex;
|
2014-11-30 22:30:28 +00:00
|
|
|
zval **params[2] = { &zindex_ptr, &zvalue_ptr };
|
|
|
|
|
|
|
|
fci.param_count = param_count;
|
|
|
|
fci.params = params;
|
2014-11-25 20:06:11 +00:00
|
|
|
|
|
|
|
fci.object_ptr = object;
|
|
|
|
fci.no_separation = 0;
|
|
|
|
|
|
|
|
zend_call_function(&fci, NULL TSRMLS_CC);
|
2014-11-30 22:30:28 +00:00
|
|
|
return php_value;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-11-25 20:06:11 +00:00
|
|
|
|
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();
|
|
|
|
|
|
|
|
V8JS_TSRMLS_FETCH();
|
|
|
|
|
2016-05-22 13:12:44 +00:00
|
|
|
zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
|
2014-11-30 22:30:28 +00:00
|
|
|
|
2014-12-13 00:30:23 +00:00
|
|
|
zval *php_value = v8js_array_access_dispatch(object, "offsetGet", 1, index, NULL TSRMLS_CC);
|
2014-11-25 20:06:11 +00:00
|
|
|
v8::Local<v8::Value> ret_value = zval_to_v8js(php_value, isolate TSRMLS_CC);
|
|
|
|
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,
|
2014-11-29 19:35:32 +00:00
|
|
|
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();
|
|
|
|
|
2014-11-25 22:55:17 +00:00
|
|
|
V8JS_TSRMLS_FETCH();
|
|
|
|
|
2016-05-22 13:12:44 +00:00
|
|
|
zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
|
2014-11-25 22:43:21 +00:00
|
|
|
|
|
|
|
zval *zvalue_ptr;
|
|
|
|
MAKE_STD_ZVAL(zvalue_ptr);
|
|
|
|
if (v8js_to_zval(value, zvalue_ptr, 0, isolate TSRMLS_CC) != SUCCESS) {
|
|
|
|
info.GetReturnValue().Set(v8::Handle<v8::Value>());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-13 00:30:23 +00:00
|
|
|
zval *php_value = v8js_array_access_dispatch(object, "offsetSet", 2, index, zvalue_ptr TSRMLS_CC);
|
2014-11-25 22:43:21 +00:00
|
|
|
zval_ptr_dtor(&php_value);
|
|
|
|
|
|
|
|
/* 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_ptr);
|
2014-11-25 22:43:21 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-11-25 22:55:17 +00:00
|
|
|
|
2014-12-13 00:30:23 +00:00
|
|
|
static int v8js_array_access_get_count_result(zval *object TSRMLS_DC) /* {{{ */
|
2014-11-30 20:00:42 +00:00
|
|
|
{
|
2014-12-13 00:30:23 +00:00
|
|
|
zval *php_value = v8js_array_access_dispatch(object, "count", 0, 0, NULL TSRMLS_CC);
|
2014-11-25 20:06:11 +00:00
|
|
|
|
2014-11-30 20:00:42 +00:00
|
|
|
if(Z_TYPE_P(php_value) != IS_LONG) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Non-numeric return value from count() method");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int result = Z_LVAL_P(php_value);
|
2014-11-25 20:06:11 +00:00
|
|
|
zval_ptr_dtor(&php_value);
|
|
|
|
|
2014-11-30 20:00:42 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-12-13 00:30:23 +00:00
|
|
|
static bool v8js_array_access_isset_p(zval *object, int index TSRMLS_DC) /* {{{ */
|
2014-11-30 20:00:42 +00:00
|
|
|
{
|
2014-12-13 00:30:23 +00:00
|
|
|
zval *php_value = v8js_array_access_dispatch(object, "offsetExists", 1, index, NULL TSRMLS_CC);
|
2014-11-30 20:00:42 +00:00
|
|
|
|
|
|
|
if(Z_TYPE_P(php_value) != IS_BOOL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Non-boolean return value from offsetExists() method");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool result = Z_LVAL_P(php_value);
|
|
|
|
zval_ptr_dtor(&php_value);
|
|
|
|
|
|
|
|
return result;
|
2014-11-25 20:06:11 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-11-30 20:00:42 +00:00
|
|
|
|
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) /* {{{ */
|
2014-11-30 20:00:42 +00:00
|
|
|
{
|
|
|
|
v8::Isolate *isolate = info.GetIsolate();
|
|
|
|
v8::Local<v8::Object> self = info.Holder();
|
|
|
|
|
|
|
|
V8JS_TSRMLS_FETCH();
|
|
|
|
|
2016-05-22 13:12:44 +00:00
|
|
|
zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
|
2014-11-30 20:00:42 +00:00
|
|
|
|
2014-12-13 00:30:23 +00:00
|
|
|
int length = v8js_array_access_get_count_result(object TSRMLS_CC);
|
2014-11-30 20:00:42 +00:00
|
|
|
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) /* {{{ */
|
2014-11-30 21:28:08 +00:00
|
|
|
{
|
|
|
|
v8::Isolate *isolate = info.GetIsolate();
|
|
|
|
v8::Local<v8::Object> self = info.Holder();
|
|
|
|
|
|
|
|
V8JS_TSRMLS_FETCH();
|
|
|
|
|
2016-05-22 13:12:44 +00:00
|
|
|
zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
|
2014-11-30 21:28:08 +00:00
|
|
|
|
2014-12-13 00:30:23 +00:00
|
|
|
zval *php_value = v8js_array_access_dispatch(object, "offsetUnset", 1, index, NULL TSRMLS_CC);
|
2014-11-30 21:28:08 +00:00
|
|
|
zval_ptr_dtor(&php_value);
|
2014-11-30 22:30:28 +00:00
|
|
|
|
2014-11-30 21:28:08 +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) /* {{{ */
|
2014-11-30 21:43:56 +00:00
|
|
|
{
|
|
|
|
v8::Isolate *isolate = info.GetIsolate();
|
|
|
|
v8::Local<v8::Object> self = info.Holder();
|
|
|
|
|
|
|
|
V8JS_TSRMLS_FETCH();
|
|
|
|
|
2016-05-22 13:12:44 +00:00
|
|
|
zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
|
2014-11-30 21:43:56 +00:00
|
|
|
|
|
|
|
/* If index is set, then return an integer encoding a v8::PropertyAttribute;
|
|
|
|
* otherwise we're expected to return an empty handle. */
|
2014-12-13 00:30:23 +00:00
|
|
|
if(v8js_array_access_isset_p(object, index TSRMLS_CC)) {
|
2014-11-30 21:43:56 +00:00
|
|
|
info.GetReturnValue().Set(V8JS_UINT(v8::PropertyAttribute::None));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-11-30 21:28:08 +00:00
|
|
|
|
2014-12-13 00:30:23 +00:00
|
|
|
void v8js_array_access_enumerator(const v8::PropertyCallbackInfo<v8::Array>& info) /* {{{ */
|
2014-11-30 20:00:42 +00:00
|
|
|
{
|
|
|
|
v8::Isolate *isolate = info.GetIsolate();
|
|
|
|
v8::Local<v8::Object> self = info.Holder();
|
|
|
|
|
|
|
|
V8JS_TSRMLS_FETCH();
|
|
|
|
|
2016-05-22 13:12:44 +00:00
|
|
|
zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
|
2014-11-30 20:00:42 +00:00
|
|
|
|
2014-12-13 00:30:23 +00:00
|
|
|
int length = v8js_array_access_get_count_result(object TSRMLS_CC);
|
2014-11-30 20:00:42 +00:00
|
|
|
v8::Local<v8::Array> result = v8::Array::New(isolate, length);
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for(int j = 0; j < length; j ++) {
|
2014-12-13 00:30:23 +00:00
|
|
|
if(v8js_array_access_isset_p(object, j TSRMLS_CC)) {
|
2014-11-30 20:00:42 +00:00
|
|
|
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) /* {{{ */
|
2014-11-25 20:06:11 +00:00
|
|
|
{
|
2014-11-29 19:35:32 +00:00
|
|
|
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);
|
2014-11-29 19:35:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-13 00:30:23 +00:00
|
|
|
v8::Local<v8::Value> ret_value = v8js_named_property_callback(property, info, V8JS_PROP_GETTER);
|
2014-11-25 20:06:11 +00:00
|
|
|
|
2014-11-29 19:35:32 +00:00
|
|
|
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();
|
2014-11-25 20:06:11 +00:00
|
|
|
|
2014-11-29 19:35:32 +00:00
|
|
|
if(!prototype->IsObject()) {
|
|
|
|
/* ehh? Array.prototype not an object? strange, stop. */
|
|
|
|
info.GetReturnValue().Set(ret_value);
|
|
|
|
}
|
2014-11-25 20:06:11 +00:00
|
|
|
|
2014-11-29 19:35:32 +00:00
|
|
|
ret_value = prototype->ToObject()->Get(property);
|
|
|
|
}
|
|
|
|
|
|
|
|
info.GetReturnValue().Set(ret_value);
|
2014-11-25 20:06:11 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-11-25 22:43:21 +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-25 22:43:21 +00:00
|
|
|
* End:
|
|
|
|
* vim600: noet sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: noet sw=4 ts=4
|
|
|
|
*/
|