-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv8_conv.cpp
More file actions
185 lines (156 loc) · 6.23 KB
/
Copy pathv8_conv.cpp
File metadata and controls
185 lines (156 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2010 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: metagoto <runpac314 gmail com> |
+----------------------------------------------------------------------+
*/
#include "v8_conv.h"
#include "v8_xref.h"
#include "v8_v8context.h"
using namespace v8;
Handle<Value> to_jsval(v8context* v8c, zval* val TSRMLS_DC)
{
if (!val) {
return Undefined();
}
switch(Z_TYPE_P(val)) {
case IS_NULL:
return Null();
case IS_BOOL:
return Boolean::New(Z_BVAL_P(val) ? true : false);
case IS_LONG:
return Integer::New(Z_LVAL_P(val));
case IS_DOUBLE:
return Number::New(Z_DVAL_P(val));
case IS_STRING:
return String::New(Z_STRVAL_P(val), Z_STRLEN_P(val));
case IS_ARRAY:
{
Context::Scope context_scope(v8c->get_context());
HashTable* ht = HASH_OF(val);
Handle<Array> arr = Array::New(ht->nNumOfElements);
for(zend_hash_internal_pointer_reset(ht); zend_hash_has_more_elements(ht) == SUCCESS
; zend_hash_move_forward(ht))
{
char* key;
uint key_len;
ulong index;
zval** data;
int type = zend_hash_get_current_key_ex(ht, &key, &key_len, &index, 0, NULL);
if (FAILURE == zend_hash_get_current_data(ht, (void**)&data)) {
continue;
}
if (type == HASH_KEY_IS_LONG) {
arr->Set(Integer::New(static_cast<double>(index)), to_jsval(v8c, *data TSRMLS_CC));
}
else {
arr->Set(String::New(key, key_len-1), to_jsval(v8c, *data TSRMLS_CC));
}
}
return arr;
}
case IS_OBJECT:
{
Handle<ObjectTemplate> tpl = ObjectTemplate::New();
tpl->SetInternalFieldCount(1);
Handle<Object> obj = tpl->NewInstance();
v8c->register_ze_obj(obj, val TSRMLS_CC);
return obj;
}
default:
break;
}
return Undefined();
}
void to_zval(zval* return_value, const Handle<Value>& jval TSRMLS_DC)
{
if (jval->IsInt32()) {
RETURN_LONG(jval->Int32Value());
}
else if (jval->IsNumber()) {
RETURN_DOUBLE(jval->NumberValue());
}
else if (jval->IsString()) {
String::AsciiValue ascii(jval);
size_t ascii_len = strlen(*ascii);
RETURN_STRINGL(estrndup(*ascii, ascii_len), ascii_len, 0);
}
else if (jval->IsBoolean()) {
RETURN_BOOL(jval->IsFalse() ? 0 : 1);
}
else if (jval->IsArray()) {
Handle<Array> arr = Handle<Array>::Cast(jval);
array_init_size(return_value, arr->Length());
Handle<Array> props = arr->GetPropertyNames();
for (uint32_t i = 0; i<props->Length(); ++i) {
zval* data;
MAKE_STD_ZVAL(data);
Local<Value> prop = props->Get(Integer::New(i));
Local<Value> val = arr->Get(prop);
to_zval(data, val TSRMLS_CC);
if (prop->IsInt32()) {
add_index_zval(return_value, static_cast<ulong>(prop->Uint32Value()), data);
}
else {
String::AsciiValue ascii(prop);
add_assoc_zval_ex(return_value, *ascii, ascii.length()+1, data);
}
}
return;
}
// mettre IsFunction, IsDate avant IsObject() test
else if (jval->IsObject()) {
Handle<Object> obj = jval->ToObject();
ze_obj* zeo = 0;
if (obj->InternalFieldCount()) { // obj as a corresponding ZE object
zeo = static_cast<ze_obj*>(Handle<External>::Cast(obj->GetInternalField(0))->Value());
zval* php_obj = zeo->obj;
if (php_obj) {
zval_ptr_dtor(&return_value); // ????
*return_value = *php_obj;
return_value = php_obj;
zval_copy_ctor(return_value);
/*
Context::Scope context_scope(zeo->context->get_context());
Handle<Array> props = obj->GetPropertyNames();
for (uint32_t i = 0; i<props->Length(); ++i) {
Local<Value> prop = props->Get(Integer::New(i));
Local<Value> propval = obj->GetRealNamedProperty(prop->ToString());
zval* data;
MAKE_STD_ZVAL(data);
to_zval(data, propval TSRMLS_CC);
String::AsciiValue ascii(prop);
zend_update_property(zeo->ce, return_value, *ascii, ascii.length(), data TSRMLS_CC);
//zval_ptr_dtor(&data); ///?
}
*/
return;
}
}
object_init(return_value);
Handle<Array> props = obj->GetPropertyNames();
for (uint32_t i = 0; i<props->Length(); ++i) {
Local<Value> prop = props->Get(Integer::New(i));
Local<Value> propval = obj->Get(prop);
zval* data;
MAKE_STD_ZVAL(data);
to_zval(data, propval TSRMLS_CC);
String::AsciiValue ascii(prop);
zend_update_property(0, return_value, *ascii, ascii.length(), data TSRMLS_CC);
zval_ptr_dtor(&data); ///?
}
return;
}
RETVAL_NULL();
}