diff --git a/package.xml b/package.xml index c5205e4..07345b4 100644 --- a/package.xml +++ b/package.xml @@ -92,6 +92,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + diff --git a/php_v8js_macros.h b/php_v8js_macros.h index 7d383da..2dfdf62 100644 --- a/php_v8js_macros.h +++ b/php_v8js_macros.h @@ -54,6 +54,7 @@ extern "C" { #define V8JS_INT(v) v8::Integer::New(v, isolate) #define V8JS_FLOAT(v) v8::Number::New(isolate, v) #define V8JS_BOOL(v) ((v)?v8::True(isolate):v8::False(isolate)) +#define V8JS_DATE(v) v8::Date::New(isolate, v) #define V8JS_NULL v8::Null(isolate) #define V8JS_UNDEFINED v8::Undefined(isolate) #define V8JS_MN(name) v8js_method_##name @@ -230,6 +231,7 @@ ZEND_BEGIN_MODULE_GLOBALS(v8js) /* Ini globals */ char *v8_flags; /* V8 command line flags */ + bool use_date; /* Generate JS Date objects instead of PHP DateTime */ // Timer thread globals std::stack timer_stack; diff --git a/tests/datetime_pass.phpt b/tests/datetime_pass.phpt new file mode 100644 index 0000000..ea071f8 --- /dev/null +++ b/tests/datetime_pass.phpt @@ -0,0 +1,23 @@ +--TEST-- +Test V8::executeString() : Pass JS date to PHP +--SKIPIF-- + +--FILE-- +var = new \DateTime("Wed, 19 Mar 2014 14:37:11 +0000"); +$a->executeString('print(PHP.var.toGMTString()); print("\n");'); +ini_set('v8js.use_date', 0); + +$a = new V8Js(); +$a->var = new \DateTime("Wed, 19 Mar 2014 14:37:11 +0000"); +$a->executeString('print(PHP.var.toString()); print("\n");'); + +?> +===EOF=== +--EXPECT-- +Wed, 19 Mar 2014 14:37:11 GMT +[object DateTime] +===EOF=== diff --git a/v8js.cc b/v8js.cc index a6ecb4c..86df001 100644 --- a/v8js.cc +++ b/v8js.cc @@ -54,10 +54,27 @@ static ZEND_INI_MH(v8js_OnUpdateV8Flags) /* {{{ */ return SUCCESS; } + +static ZEND_INI_MH(v8js_OnUpdateUseDate) /* {{{ */ +{ + bool value; + if (new_value_length==2 && strcasecmp("on", new_value)==0) { + value = (bool) 1; + } else if (new_value_length==3 && strcasecmp("yes", new_value)==0) { + value = (bool) 1; + } else if (new_value_length==4 && strcasecmp("true", new_value)==0) { + value = (bool) 1; + } else { + value = (bool) atoi(new_value); + } + V8JSG(use_date) = value; + return SUCCESS; +} /* }}} */ ZEND_INI_BEGIN() /* {{{ */ ZEND_INI_ENTRY("v8js.flags", NULL, ZEND_INI_ALL, v8js_OnUpdateV8Flags) + ZEND_INI_ENTRY("v8js.use_date", "0", ZEND_INI_ALL, v8js_OnUpdateUseDate) ZEND_INI_END() /* }}} */ diff --git a/v8js_convert.cc b/v8js_convert.cc index 6d7b639..3c031f6 100644 --- a/v8js_convert.cc +++ b/v8js_convert.cc @@ -854,6 +854,7 @@ v8::Handle zval_to_v8js(zval *value, v8::Isolate *isolate TSRMLS_DC) { v8::Handle jsValue; long v; + zend_class_entry *ce; switch (Z_TYPE_P(value)) { @@ -862,7 +863,19 @@ v8::Handle zval_to_v8js(zval *value, v8::Isolate *isolate TSRMLS_DC) break; case IS_OBJECT: - jsValue = php_v8js_hash_to_jsobj(value, isolate TSRMLS_CC); + if (V8JSG(use_date)) { + ce = php_date_get_date_ce(); + if (instanceof_function(Z_OBJCE_P(value), ce TSRMLS_CC)) { + zval *dtval; + zend_call_method_with_0_params(&value, NULL, NULL, "getTimestamp", &dtval); + if (dtval) + jsValue = V8JS_DATE(((double)Z_LVAL_P(dtval) * 1000.0)); + else + jsValue = V8JS_NULL; + } else + jsValue = php_v8js_hash_to_jsobj(value, isolate TSRMLS_CC); + } else + jsValue = php_v8js_hash_to_jsobj(value, isolate TSRMLS_CC); break; case IS_STRING: