mirror of
https://github.com/phpv8/v8js.git
synced 2025-01-03 11:21:51 +00:00
Added new "v8js.use_date" ini setting to convert PHP DateTime objects to Javascript Date objects
when set as V8JS object properties.
This commit is contained in:
parent
66dbe7d323
commit
da53c90f88
@ -92,6 +92,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
|
||||
<file name="array_pass_flags.phpt" role="test" />
|
||||
<file name="timezones.phpt" role="test" />
|
||||
<file name="long.phpt" role="test" />
|
||||
<file name="datetime_pass.phpt" role="test" />
|
||||
</dir>
|
||||
</dir>
|
||||
</contents>
|
||||
|
@ -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<php_v8js_timer_ctx *> timer_stack;
|
||||
|
23
tests/datetime_pass.phpt
Normal file
23
tests/datetime_pass.phpt
Normal file
@ -0,0 +1,23 @@
|
||||
--TEST--
|
||||
Test V8::executeString() : Pass JS date to PHP
|
||||
--SKIPIF--
|
||||
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
ini_set('v8js.use_date', 1);
|
||||
$a = new V8Js();
|
||||
$a->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===
|
17
v8js.cc
17
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()
|
||||
/* }}} */
|
||||
|
||||
|
@ -854,6 +854,7 @@ v8::Handle<v8::Value> zval_to_v8js(zval *value, v8::Isolate *isolate TSRMLS_DC)
|
||||
{
|
||||
v8::Handle<v8::Value> jsValue;
|
||||
long v;
|
||||
zend_class_entry *ce;
|
||||
|
||||
switch (Z_TYPE_P(value))
|
||||
{
|
||||
@ -862,6 +863,18 @@ v8::Handle<v8::Value> zval_to_v8js(zval *value, v8::Isolate *isolate TSRMLS_DC)
|
||||
break;
|
||||
|
||||
case IS_OBJECT:
|
||||
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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user