| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 | 
							- /*
 
-   +----------------------------------------------------------------------+
 
-   | PHP Version 5                                                        |
 
-   +----------------------------------------------------------------------+
 
-   | Copyright (c) 1997-2016 The PHP Group                                |
 
-   +----------------------------------------------------------------------+
 
-   | http://www.opensource.org/licenses/mit-license.php  MIT License      |
 
-   +----------------------------------------------------------------------+
 
-   | Author: Stefan Siegl <[email protected]>                          |
 
-   +----------------------------------------------------------------------+
 
- */
 
- #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"
 
- }
 
- static zval v8js_array_access_dispatch(zend_object *object, const char *method_name, int param_count,
 
- 									   uint32_t index, zval zvalue TSRMLS_DC) /* {{{ */
 
- {
 
- 	zend_fcall_info fci;
 
- 	zval php_value;
 
- 	fci.size = sizeof(fci);
 
- #if (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION == 0)
 
- 	fci.function_table = &object->ce->function_table;
 
- 	fci.symbol_table = NULL;
 
- #endif
 
- 	ZVAL_STRING(&fci.function_name, method_name);
 
- 	fci.retval = &php_value;
 
- 	zval params[2];
 
- 	ZVAL_LONG(¶ms[0], index);
 
- 	params[1] = zvalue;
 
- 	fci.param_count = param_count;
 
- 	fci.params = params;
 
- 	fci.object = object;
 
- 	fci.no_separation = 0;
 
- 	zend_call_function(&fci, NULL TSRMLS_CC);
 
- 	zval_dtor(&fci.function_name);
 
- 	return php_value;
 
- }
 
- /* }}} */
 
- void v8js_array_access_getter(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
 
- {
 
- 	v8::Isolate *isolate = info.GetIsolate();
 
- 	v8::Local<v8::Object> self = info.Holder();
 
- 	V8JS_TSRMLS_FETCH();
 
- 	zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
 
- 	zval zvalue;
 
- 	ZVAL_UNDEF(&zvalue);
 
- 	zval php_value = v8js_array_access_dispatch(object, "offsetGet", 1, index, zvalue TSRMLS_CC);
 
- 	v8::Local<v8::Value> ret_value = zval_to_v8js(&php_value, isolate TSRMLS_CC);
 
- 	zval_ptr_dtor(&php_value);
 
- 	info.GetReturnValue().Set(ret_value);
 
- }
 
- /* }}} */
 
- void v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value,
 
- 								  const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
 
- {
 
- 	v8::Isolate *isolate = info.GetIsolate();
 
- 	v8::Local<v8::Object> self = info.Holder();
 
- 	V8JS_TSRMLS_FETCH();
 
- 	zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
 
- 	zval zvalue;
 
- 	ZVAL_UNDEF(&zvalue);
 
- 	if (v8js_to_zval(value, &zvalue, 0, isolate TSRMLS_CC) != SUCCESS) {
 
- 		info.GetReturnValue().Set(v8::Handle<v8::Value>());
 
- 		return;
 
- 	}
 
- 	zval php_value = v8js_array_access_dispatch(object, "offsetSet", 2, index, zvalue TSRMLS_CC);
 
- 	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);
 
- 	/* if PHP wanted to hold on to this value, zend_call_function would
 
- 	 * have bumped the refcount. */
 
- 	zval_ptr_dtor(&zvalue);
 
- }
 
- /* }}} */
 
- static int v8js_array_access_get_count_result(zend_object *object TSRMLS_DC) /* {{{ */
 
- {
 
- 	zval zvalue;
 
- 	ZVAL_UNDEF(&zvalue);
 
- 	zval php_value = v8js_array_access_dispatch(object, "count", 0, 0, zvalue TSRMLS_CC);
 
- 	if(Z_TYPE(php_value) != IS_LONG) {
 
- 		php_error_docref(NULL TSRMLS_CC, 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);
 
- }
 
- /* }}} */
 
- static bool v8js_array_access_isset_p(zend_object *object, int index TSRMLS_DC) /* {{{ */
 
- {
 
- 	zval zvalue;
 
- 	ZVAL_UNDEF(&zvalue);
 
- 	zval php_value = v8js_array_access_dispatch(object, "offsetExists", 1, index, zvalue TSRMLS_CC);
 
- 	if(Z_TYPE(php_value) != IS_TRUE && Z_TYPE(php_value) != IS_FALSE) {
 
- 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Non-boolean return value from offsetExists() method");
 
- 		zval_ptr_dtor(&php_value);
 
- 		return false;
 
- 	}
 
- 	return Z_TYPE(php_value) == IS_TRUE;
 
- }
 
- /* }}} */
 
- 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();
 
- 	V8JS_TSRMLS_FETCH();
 
- 	zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
 
- 	int length = v8js_array_access_get_count_result(object TSRMLS_CC);
 
- 	info.GetReturnValue().Set(V8JS_INT(length));
 
- }
 
- /* }}} */
 
- 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();
 
- 	V8JS_TSRMLS_FETCH();
 
- 	zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
 
- 	zval zvalue;
 
- 	ZVAL_UNDEF(&zvalue);
 
- 	zval php_value = v8js_array_access_dispatch(object, "offsetUnset", 1, index, zvalue TSRMLS_CC);
 
- 	zval_ptr_dtor(&php_value);
 
- 	info.GetReturnValue().Set(V8JS_BOOL(true));
 
- }
 
- /* }}} */
 
- 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();
 
- 	V8JS_TSRMLS_FETCH();
 
- 	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. */
 
- 	if(v8js_array_access_isset_p(object, index TSRMLS_CC)) {
 
- 		info.GetReturnValue().Set(V8JS_UINT(v8::PropertyAttribute::None));
 
- 	}
 
- }
 
- /* }}} */
 
- void v8js_array_access_enumerator(const v8::PropertyCallbackInfo<v8::Array>& info) /* {{{ */
 
- {
 
- 	v8::Isolate *isolate = info.GetIsolate();
 
- 	v8::Local<v8::Object> self = info.Holder();
 
- 	V8JS_TSRMLS_FETCH();
 
- 	zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
 
- 	int length = v8js_array_access_get_count_result(object TSRMLS_CC);
 
- 	v8::Local<v8::Array> result = v8::Array::New(isolate, length);
 
- 	int i = 0;
 
- 	for(int j = 0; j < length; j ++) {
 
- 		if(v8js_array_access_isset_p(object, j TSRMLS_CC)) {
 
- 			result->Set(i ++, V8JS_INT(j));
 
- 		}
 
- 	}
 
- 	result->Set(V8JS_STR("length"), V8JS_INT(i));
 
- 	info.GetReturnValue().Set(result);
 
- }
 
- /* }}} */
 
- 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) {
 
- 		v8js_array_access_length(property, info);
 
- 		return;
 
- 	}
 
- 	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);
 
- }
 
- /* }}} */
 
- /*
 
-  * Local variables:
 
-  * tab-width: 4
 
-  * c-basic-offset: 4
 
-  * indent-tabs-mode: t
 
-  * End:
 
-  * vim600: noet sw=4 ts=4 fdm=marker
 
-  * vim<600: noet sw=4 ts=4
 
-  */
 
 
  |