2012-04-27 16:26:15 +00:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| PHP Version 5 |
|
|
|
|
+----------------------------------------------------------------------+
|
2013-09-28 17:17:33 +00:00
|
|
|
| Copyright (c) 1997-2013 The PHP Group |
|
2012-04-27 16:26:15 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2013-09-28 17:17:33 +00:00
|
|
|
| http://www.opensource.org/licenses/mit-license.php MIT License |
|
2012-04-27 16:26:15 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Author: Jani Taskinen <jani.taskinen@iki.fi> |
|
2012-04-27 16:41:32 +00:00
|
|
|
| Author: Patrick Reilly <preilly@php.net> |
|
2012-04-27 16:26:15 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2016-01-06 09:01:35 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
#include "php_v8js_macros.h"
|
|
|
|
#include "v8js_object_export.h"
|
|
|
|
#include "v8js_v8object_class.h"
|
|
|
|
#include "v8js_v8.h"
|
|
|
|
|
2012-04-27 16:26:15 +00:00
|
|
|
extern "C" {
|
|
|
|
#include "php.h"
|
|
|
|
#include "ext/date/php_date.h"
|
2013-10-11 06:15:35 +00:00
|
|
|
#include "ext/standard/php_string.h"
|
2012-04-27 16:26:15 +00:00
|
|
|
#include "zend_interfaces.h"
|
|
|
|
#include "zend_closures.h"
|
|
|
|
}
|
|
|
|
|
2014-12-13 00:30:23 +00:00
|
|
|
static int v8js_is_assoc_array(HashTable *myht TSRMLS_DC) /* {{{ */
|
2012-04-27 16:26:15 +00:00
|
|
|
{
|
2015-08-28 19:05:16 +00:00
|
|
|
zend_string *key;
|
2012-04-27 16:26:15 +00:00
|
|
|
ulong index, idx = 0;
|
|
|
|
uint key_len;
|
|
|
|
HashPosition pos;
|
|
|
|
|
2015-08-28 19:05:16 +00:00
|
|
|
ZEND_HASH_FOREACH_KEY(myht, index, key) {
|
|
|
|
if(key) {
|
|
|
|
// HASH_KEY_IS_STRING
|
2012-04-27 16:26:15 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2015-08-28 19:05:16 +00:00
|
|
|
|
|
|
|
if(index != idx) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
idx ++;
|
|
|
|
} ZEND_HASH_FOREACH_END();
|
|
|
|
|
2012-04-27 16:26:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
|
2014-12-13 00:30:23 +00:00
|
|
|
static v8::Handle<v8::Value> v8js_hash_to_jsarr(zval *value, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
|
2012-04-27 16:26:15 +00:00
|
|
|
{
|
|
|
|
HashTable *myht = HASH_OF(value);
|
|
|
|
int i = myht ? zend_hash_num_elements(myht) : 0;
|
|
|
|
|
|
|
|
/* Return object if dealing with assoc array */
|
2014-12-13 00:30:23 +00:00
|
|
|
if (i > 0 && v8js_is_assoc_array(myht TSRMLS_CC)) {
|
|
|
|
return v8js_hash_to_jsobj(value, isolate TSRMLS_CC);
|
2012-04-27 16:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
v8::Local<v8::Array> newarr;
|
|
|
|
|
|
|
|
/* Prevent recursion */
|
2015-08-28 19:05:16 +00:00
|
|
|
if (myht && ZEND_HASH_GET_APPLY_COUNT(myht) > 1) {
|
2012-04-27 16:26:15 +00:00
|
|
|
return V8JS_NULL;
|
|
|
|
}
|
|
|
|
|
2014-10-19 11:58:21 +00:00
|
|
|
newarr = v8::Array::New(isolate, i);
|
2012-04-27 16:26:15 +00:00
|
|
|
|
|
|
|
if (i > 0)
|
|
|
|
{
|
2015-08-28 19:05:16 +00:00
|
|
|
zval *data;
|
2012-04-27 16:26:15 +00:00
|
|
|
ulong index = 0;
|
|
|
|
HashTable *tmp_ht;
|
|
|
|
|
2015-08-28 19:05:16 +00:00
|
|
|
ZEND_HASH_FOREACH_VAL(myht, data) {
|
|
|
|
tmp_ht = HASH_OF(data);
|
2012-04-27 16:26:15 +00:00
|
|
|
|
|
|
|
if (tmp_ht) {
|
2015-08-28 19:05:16 +00:00
|
|
|
ZEND_HASH_INC_APPLY_COUNT(myht);
|
2012-04-27 16:26:15 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 19:05:16 +00:00
|
|
|
newarr->Set(index++, zval_to_v8js(data, isolate TSRMLS_CC));
|
2012-04-27 16:26:15 +00:00
|
|
|
|
|
|
|
if (tmp_ht) {
|
2015-08-28 19:05:16 +00:00
|
|
|
ZEND_HASH_DEC_APPLY_COUNT(myht);
|
2012-04-27 16:26:15 +00:00
|
|
|
}
|
2015-08-28 19:05:16 +00:00
|
|
|
} ZEND_HASH_FOREACH_END();
|
2012-04-27 16:26:15 +00:00
|
|
|
}
|
|
|
|
return newarr;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2013-04-13 23:36:05 +00:00
|
|
|
v8::Handle<v8::Value> zval_to_v8js(zval *value, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
|
2012-04-27 16:26:15 +00:00
|
|
|
{
|
|
|
|
v8::Handle<v8::Value> jsValue;
|
2014-03-20 10:00:45 +00:00
|
|
|
long v;
|
2014-03-20 14:06:18 +00:00
|
|
|
zend_class_entry *ce;
|
2012-04-27 16:26:15 +00:00
|
|
|
|
2015-10-02 21:19:32 +00:00
|
|
|
if(Z_TYPE_P(value) == IS_INDIRECT)
|
|
|
|
value = Z_INDIRECT_P(value);
|
|
|
|
|
2012-04-27 16:26:15 +00:00
|
|
|
switch (Z_TYPE_P(value))
|
|
|
|
{
|
|
|
|
case IS_ARRAY:
|
2014-12-13 00:30:23 +00:00
|
|
|
jsValue = v8js_hash_to_jsarr(value, isolate TSRMLS_CC);
|
2012-04-27 16:26:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IS_OBJECT:
|
2014-03-20 14:06:18 +00:00
|
|
|
if (V8JSG(use_date)) {
|
|
|
|
ce = php_date_get_date_ce();
|
|
|
|
if (instanceof_function(Z_OBJCE_P(value), ce TSRMLS_CC)) {
|
2015-08-28 19:05:16 +00:00
|
|
|
zval dtval;
|
|
|
|
zend_call_method_with_0_params(value, NULL, NULL, "getTimestamp", &dtval);
|
|
|
|
jsValue = V8JS_DATE(((double)Z_LVAL(dtval) * 1000.0));
|
|
|
|
zval_dtor(&dtval);
|
2014-03-20 14:06:18 +00:00
|
|
|
} else
|
2014-12-13 00:30:23 +00:00
|
|
|
jsValue = v8js_hash_to_jsobj(value, isolate TSRMLS_CC);
|
2014-03-20 14:06:18 +00:00
|
|
|
} else
|
2014-12-13 00:30:23 +00:00
|
|
|
jsValue = v8js_hash_to_jsobj(value, isolate TSRMLS_CC);
|
2012-04-27 16:26:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IS_STRING:
|
2015-12-04 19:14:47 +00:00
|
|
|
jsValue = V8JS_ZSTR(Z_STR_P(value));
|
2012-04-27 16:26:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IS_LONG:
|
2014-03-20 10:00:45 +00:00
|
|
|
v = Z_LVAL_P(value);
|
2014-11-12 23:58:32 +00:00
|
|
|
/* On Windows there are max and min macros, which would clobber the
|
|
|
|
* method names of std::numeric_limits< > otherwise. */
|
|
|
|
#undef max
|
|
|
|
#undef min
|
2014-03-20 10:00:45 +00:00
|
|
|
if (v < - std::numeric_limits<int32_t>::min() || v > std::numeric_limits<int32_t>::max()) {
|
|
|
|
jsValue = V8JS_FLOAT((double)v);
|
|
|
|
} else {
|
|
|
|
jsValue = V8JS_INT(v);
|
|
|
|
}
|
2012-04-27 16:26:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IS_DOUBLE:
|
|
|
|
jsValue = V8JS_FLOAT(Z_DVAL_P(value));
|
|
|
|
break;
|
|
|
|
|
2015-08-28 19:05:16 +00:00
|
|
|
case IS_TRUE:
|
|
|
|
jsValue = V8JS_TRUE();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IS_FALSE:
|
|
|
|
jsValue = V8JS_FALSE();
|
2012-04-27 16:26:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IS_NULL:
|
|
|
|
jsValue = V8JS_NULL;
|
|
|
|
break;
|
2015-10-02 21:19:32 +00:00
|
|
|
|
|
|
|
case IS_UNDEF:
|
|
|
|
default:
|
|
|
|
/* undefined -> return v8::Value left empty */
|
|
|
|
break;
|
2012-04-27 16:26:15 +00:00
|
|
|
}
|
2013-03-18 03:46:03 +00:00
|
|
|
return jsValue;
|
2012-04-27 16:26:15 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2013-04-13 23:36:05 +00:00
|
|
|
int v8js_to_zval(v8::Handle<v8::Value> jsValue, zval *return_value, int flags, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
|
2012-04-27 16:26:15 +00:00
|
|
|
{
|
|
|
|
if (jsValue->IsString())
|
|
|
|
{
|
|
|
|
v8::String::Utf8Value str(jsValue);
|
|
|
|
const char *cstr = ToCString(str);
|
2015-08-23 19:14:49 +00:00
|
|
|
RETVAL_STRINGL(cstr, jsValue->ToString()->Utf8Length());
|
2012-04-27 16:26:15 +00:00
|
|
|
}
|
|
|
|
else if (jsValue->IsBoolean())
|
|
|
|
{
|
|
|
|
RETVAL_BOOL(jsValue->Uint32Value());
|
|
|
|
}
|
|
|
|
else if (jsValue->IsInt32() || jsValue->IsUint32())
|
|
|
|
{
|
|
|
|
RETVAL_LONG((long) jsValue->IntegerValue());
|
|
|
|
}
|
|
|
|
else if (jsValue->IsNumber())
|
|
|
|
{
|
|
|
|
RETVAL_DOUBLE(jsValue->NumberValue());
|
|
|
|
}
|
|
|
|
else if (jsValue->IsDate()) /* Return as a PHP DateTime object */
|
|
|
|
{
|
|
|
|
v8::String::Utf8Value str(jsValue);
|
|
|
|
const char *cstr = ToCString(str);
|
2014-11-16 21:33:32 +00:00
|
|
|
|
|
|
|
/* cstr has two timezone specifications:
|
|
|
|
*
|
|
|
|
* example from Linux:
|
|
|
|
* Mon Sep 08 1975 09:00:00 GMT+0000 (UTC)
|
|
|
|
*
|
|
|
|
* example from Windows:
|
|
|
|
* Mon Sep 08 1975 11:00:00 GMT+0200 (W. Europe Daylight Time)
|
|
|
|
*
|
|
|
|
* ... problem is, that PHP can't parse the second timezone
|
|
|
|
* specification as returned by v8 running on Windows. And as a
|
|
|
|
* matter of that fails due to inconsistent second timezone spec
|
|
|
|
*/
|
|
|
|
char *date_str = estrdup(cstr);
|
|
|
|
char *paren_ptr = strchr(date_str, '(');
|
|
|
|
|
|
|
|
if (paren_ptr != NULL) {
|
|
|
|
*paren_ptr = 0;
|
|
|
|
}
|
|
|
|
|
2012-04-27 16:26:15 +00:00
|
|
|
zend_class_entry *ce = php_date_get_date_ce();
|
|
|
|
php_date_instantiate(ce, return_value TSRMLS_CC);
|
2015-08-28 19:05:16 +00:00
|
|
|
if (!php_date_initialize(Z_PHPDATE_P(return_value), date_str, strlen(date_str), NULL, NULL, 0 TSRMLS_CC)) {
|
2014-11-16 21:33:32 +00:00
|
|
|
efree(date_str);
|
2012-04-27 16:26:15 +00:00
|
|
|
return FAILURE;
|
|
|
|
}
|
2014-11-16 21:33:32 +00:00
|
|
|
|
|
|
|
efree(date_str);
|
2012-04-27 16:26:15 +00:00
|
|
|
}
|
|
|
|
else if (jsValue->IsObject())
|
|
|
|
{
|
2013-10-21 19:03:45 +00:00
|
|
|
v8::Handle<v8::Object> self = v8::Handle<v8::Object>::Cast(jsValue);
|
|
|
|
// if this is a wrapped PHP object, then just unwrap it.
|
2013-10-25 18:05:51 +00:00
|
|
|
v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
|
|
|
|
if (!php_object.IsEmpty()) {
|
2015-08-24 19:13:35 +00:00
|
|
|
zend_object *object = reinterpret_cast<zend_object *>(v8::External::Cast(*php_object)->Value());
|
|
|
|
zval zval_object;
|
2015-08-28 19:05:16 +00:00
|
|
|
ZVAL_OBJ(&zval_object, object);
|
|
|
|
RETVAL_ZVAL(&zval_object, 1, 0);
|
2013-10-21 19:03:45 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
2015-08-26 16:05:53 +00:00
|
|
|
if ((flags & V8JS_FLAG_FORCE_ARRAY && !jsValue->IsFunction()) || jsValue->IsArray()) {
|
2012-04-27 16:26:15 +00:00
|
|
|
array_init(return_value);
|
2014-12-12 23:28:09 +00:00
|
|
|
return v8js_get_properties_hash(jsValue, Z_ARRVAL_P(return_value), flags, isolate TSRMLS_CC);
|
2012-04-27 16:26:15 +00:00
|
|
|
} else {
|
2014-12-13 00:21:02 +00:00
|
|
|
v8js_v8object_create(return_value, jsValue, flags, isolate TSRMLS_CC);
|
2012-04-27 16:26:15 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* types External, RegExp, Undefined and Null are considered NULL */
|
|
|
|
{
|
|
|
|
RETVAL_NULL();
|
|
|
|
}
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
* vim600: noet sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: noet sw=4 ts=4
|
|
|
|
*/
|