php_embed 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +2 -1
- data/ext/php_embed/convert.c +222 -231
- data/ext/php_embed/convert.h +5 -9
- data/ext/php_embed/extconf.rb +107 -4
- data/ext/php_embed/php.c +195 -205
- data/ext/php_embed/php.ini +1861 -0
- data/ext/php_embed/php_embed.h +21 -0
- data/ext/php_embed/value.c +191 -130
- data/ext/php_embed/value.h +2 -5
- data/lib/php_embed/version.rb +1 -1
- data/spec/require.php +5 -0
- data/spec/value_spec.rb +22 -0
- metadata +6 -2
data/.gitignore
CHANGED
data/ext/php_embed/convert.c
CHANGED
@@ -1,271 +1,262 @@
|
|
1
|
-
#include "
|
2
|
-
#include "convert.h"
|
1
|
+
#include "php_embed.h"
|
3
2
|
|
3
|
+
VALUE zval_to_array(zval *zv) {
|
4
|
+
HashTable *ht;
|
5
|
+
HashPosition pos;
|
6
|
+
zval **data;
|
7
|
+
VALUE ret;
|
4
8
|
|
5
|
-
|
6
|
-
|
7
|
-
HashPosition pos;
|
8
|
-
zval** data;
|
9
|
-
VALUE ret;
|
10
|
-
|
11
|
-
convert_to_array(zv);
|
12
|
-
ht = Z_ARRVAL_P(zv);
|
9
|
+
convert_to_array(zv);
|
10
|
+
ht = Z_ARRVAL_P(zv);
|
13
11
|
|
14
|
-
|
12
|
+
ret = rb_ary_new2(zend_hash_num_elements(ht));
|
15
13
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
14
|
+
zend_hash_internal_pointer_reset_ex(ht, &pos);
|
15
|
+
while (SUCCESS == zend_hash_get_current_data_ex(ht, (void **)&data, &pos)) {
|
16
|
+
rb_ary_push(ret, new_php_embed_value(*data));
|
17
|
+
zend_hash_move_forward_ex(ht, &pos);
|
18
|
+
}
|
19
|
+
return ret;
|
22
20
|
}
|
23
21
|
|
24
|
-
VALUE zval_to_hash(zval*
|
25
|
-
HashTable*
|
22
|
+
VALUE zval_to_hash(zval *zv) {
|
23
|
+
HashTable *ht;
|
26
24
|
HashPosition pos;
|
27
|
-
zval**
|
25
|
+
zval **data;
|
28
26
|
VALUE ret;
|
29
27
|
|
30
28
|
convert_to_array(zv);
|
31
|
-
ht = Z_ARRVAL_P(zv);
|
29
|
+
ht = Z_ARRVAL_P(zv);
|
32
30
|
|
33
31
|
ret = rb_hash_new();
|
34
32
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
33
|
+
zend_hash_internal_pointer_reset_ex(ht, &pos);
|
34
|
+
while (zend_hash_get_current_data_ex(ht, (void **)&data, &pos) == SUCCESS) {
|
35
|
+
char* key_str;
|
36
|
+
uint key_len;
|
37
|
+
ulong num_index;
|
38
|
+
VALUE key = Qnil;
|
39
|
+
VALUE val = new_php_embed_value(*data);
|
40
|
+
|
41
|
+
switch(zend_hash_get_current_key_ex(ht, &key_str, &key_len, &num_index, 0, &pos)) {
|
42
|
+
case HASH_KEY_IS_STRING:
|
43
|
+
//key = rb_str_new(key_str, key_len);
|
44
|
+
key = rb_str_new_cstr(key_str);
|
45
|
+
break;
|
46
|
+
case HASH_KEY_IS_LONG:
|
47
|
+
key = LONG2NUM(num_index);
|
48
|
+
break;
|
49
|
+
}
|
42
50
|
|
43
|
-
|
44
|
-
|
45
|
-
//key = rb_str_new(key_str, key_len);
|
46
|
-
key = rb_str_new_cstr(key_str);
|
47
|
-
break;
|
48
|
-
case HASH_KEY_IS_LONG:
|
49
|
-
key = LONG2NUM(num_index);
|
50
|
-
break;
|
51
|
+
rb_hash_aset(ret, key, val);
|
52
|
+
zend_hash_move_forward_ex(ht, &pos);
|
51
53
|
}
|
52
|
-
|
53
|
-
rb_hash_aset(ret, key, val);
|
54
|
-
zend_hash_move_forward_ex(ht, &pos);
|
55
|
-
}
|
56
|
-
return ret;
|
54
|
+
return ret;
|
57
55
|
}
|
58
56
|
|
59
|
-
|
60
|
-
|
61
57
|
static int hash_to_php_string_array(VALUE key, VALUE value, VALUE ary) {
|
62
|
-
|
63
|
-
|
58
|
+
VALUE k,v,r;
|
59
|
+
int key_type;
|
64
60
|
|
65
|
-
|
66
|
-
|
67
|
-
|
61
|
+
if (key == Qundef) {
|
62
|
+
return ST_CONTINUE;
|
63
|
+
}
|
68
64
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
65
|
+
key_type = TYPE(key);
|
66
|
+
if (key_type != T_FIXNUM && key_type != T_BIGNUM
|
67
|
+
&& key_type != T_STRING && key_type != T_SYMBOL) {
|
68
|
+
rb_raise(rb_eRuntimeError, "invalid key (%d)", key_type);
|
69
|
+
}
|
74
70
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
71
|
+
k = convert_value_to_php_string(key);
|
72
|
+
r = rb_str_new_cstr(RSTRING_PTR(k));
|
73
|
+
rb_str_cat2(r, "=>");
|
74
|
+
v = convert_value_to_php_string(value);
|
75
|
+
rb_str_cat2(r, RSTRING_PTR(v));
|
80
76
|
|
81
|
-
|
82
|
-
|
77
|
+
rb_ary_push(ary, r);
|
78
|
+
return ST_CONTINUE;
|
83
79
|
}
|
84
80
|
|
85
|
-
|
86
81
|
static int hash_to_zval(VALUE key, VALUE value, VALUE zval_array) {
|
87
|
-
|
88
|
-
|
82
|
+
zval *v;
|
83
|
+
zval *ary;
|
89
84
|
|
90
|
-
|
91
|
-
|
92
|
-
|
85
|
+
if (key == Qundef) {
|
86
|
+
return ST_CONTINUE;
|
87
|
+
}
|
93
88
|
|
94
|
-
|
95
|
-
|
89
|
+
ary = (zval *)zval_array;
|
90
|
+
v = value_to_zval(value);
|
96
91
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
92
|
+
if (key == Qtrue) {
|
93
|
+
zend_hash_index_update(Z_ARRVAL_P(ary), 1, &v, sizeof(zval *), NULL);
|
94
|
+
return ST_CONTINUE;
|
95
|
+
}
|
101
96
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
97
|
+
if (key == Qfalse || key == Qnil) {
|
98
|
+
zend_hash_index_update(Z_ARRVAL_P(ary), 0, &v, sizeof(zval *), NULL);
|
99
|
+
return ST_CONTINUE;
|
100
|
+
}
|
106
101
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
102
|
+
if (TYPE(key) == T_FIXNUM) {
|
103
|
+
int idx = FIX2INT(key);
|
104
|
+
zend_hash_index_update(Z_ARRVAL_P(ary), idx, &v, sizeof(zval *), NULL);
|
105
|
+
return ST_CONTINUE;
|
106
|
+
}
|
112
107
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
108
|
+
switch (TYPE(key)) {
|
109
|
+
case T_BIGNUM:
|
110
|
+
key = rb_big2str(key, 10);
|
111
|
+
break;
|
112
|
+
case T_SYMBOL:
|
113
|
+
key = rb_sym_to_s(key);
|
114
|
+
break;
|
115
|
+
case T_STRING:
|
116
|
+
key = rb_string_value(&key);
|
117
|
+
break;
|
118
|
+
default:
|
119
|
+
rb_raise(rb_eRuntimeError, "invalid key (%d)", TYPE(key));
|
120
|
+
}
|
126
121
|
|
127
|
-
|
128
|
-
|
122
|
+
//ZEND_HANDLE_NUMERIC(RSTRING_PTR(key), RSTRING_LEN(key), zend_hash_index_update(Z_ARRVAL_P(ary), idx, &v, sizeof(zval *), NULL));
|
123
|
+
zend_symtable_update(Z_ARRVAL_P(ary), RSTRING_PTR(key), RSTRING_LEN(key) + 1, &v, sizeof(zval *), NULL);
|
129
124
|
|
130
|
-
|
125
|
+
return ST_CONTINUE;
|
131
126
|
}
|
132
127
|
|
133
|
-
|
134
128
|
VALUE convert_value_to_php_string(VALUE v) {
|
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
|
-
case T_HASH:
|
166
|
-
{
|
167
|
-
VALUE ret = rb_str_new_cstr("array(");
|
168
|
-
VALUE ary = rb_ary_new();
|
169
|
-
|
170
|
-
rb_hash_foreach(v, hash_to_php_string_array, ary);
|
171
|
-
|
172
|
-
{
|
173
|
-
int i;
|
174
|
-
int len = RARRAY_LEN(ary);
|
175
|
-
|
176
|
-
VALUE* p = RARRAY_PTR(ary);
|
177
|
-
for(i=0; i<len; ++i) {
|
178
|
-
rb_str_cat2(ret, StringValuePtr(p[i]));
|
179
|
-
if (i != len-1) {
|
180
|
-
rb_str_cat2(ret, ",");
|
129
|
+
switch (TYPE(v)) {
|
130
|
+
case T_FALSE:
|
131
|
+
return rb_str_new_cstr("false");
|
132
|
+
case T_TRUE:
|
133
|
+
return rb_str_new_cstr("true");
|
134
|
+
case T_UNDEF:
|
135
|
+
case T_NIL:
|
136
|
+
return rb_str_new_cstr("null");
|
137
|
+
case T_FIXNUM:
|
138
|
+
return rb_fix2str(v, 10);
|
139
|
+
case T_BIGNUM:
|
140
|
+
return rb_big2str(v, 10);
|
141
|
+
case T_FLOAT:
|
142
|
+
return rb_funcall(v, rb_intern("to_s"), 0);
|
143
|
+
case T_ARRAY:
|
144
|
+
{
|
145
|
+
int i;
|
146
|
+
VALUE ret = rb_str_new_cstr("array(");
|
147
|
+
for(i=0;i<RARRAY_LEN(v);++i) {
|
148
|
+
VALUE p = convert_value_to_php_string(RARRAY_PTR(v)[i]);
|
149
|
+
if (TYPE(p) == T_STRING) {
|
150
|
+
rb_str_cat2(ret, StringValuePtr(p));
|
151
|
+
}
|
152
|
+
if (i != RARRAY_LEN(v)-1) {
|
153
|
+
rb_str_cat2(ret, ",");
|
154
|
+
}
|
155
|
+
}
|
156
|
+
rb_str_cat2(ret, ")");
|
157
|
+
return ret;
|
181
158
|
}
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
159
|
+
case T_HASH:
|
160
|
+
{
|
161
|
+
VALUE ret = rb_str_new_cstr("array(");
|
162
|
+
VALUE ary = rb_ary_new();
|
163
|
+
int i, len;
|
164
|
+
|
165
|
+
rb_hash_foreach(v, hash_to_php_string_array, ary);
|
166
|
+
|
167
|
+
len = RARRAY_LEN(ary);
|
168
|
+
VALUE* p = RARRAY_PTR(ary);
|
169
|
+
for(i=0; i<len; ++i) {
|
170
|
+
rb_str_cat2(ret, StringValuePtr(p[i]));
|
171
|
+
if (i != len-1) {
|
172
|
+
rb_str_cat2(ret, ",");
|
173
|
+
}
|
174
|
+
}
|
175
|
+
|
176
|
+
rb_str_cat2(ret, ")");
|
177
|
+
return ret;
|
178
|
+
}
|
179
|
+
case T_SYMBOL:
|
180
|
+
{
|
181
|
+
VALUE symbol_str = rb_sym_to_s(v);
|
182
|
+
VALUE ret = rb_str_new_cstr("'");
|
183
|
+
rb_str_cat2(ret, StringValuePtr(symbol_str));
|
184
|
+
rb_str_cat2(ret, "'");
|
185
|
+
return ret;
|
186
|
+
}
|
187
|
+
case T_STRING:
|
188
|
+
{
|
189
|
+
VALUE ret = rb_str_new_cstr("'");
|
190
|
+
rb_str_cat2(ret, StringValuePtr(v));
|
191
|
+
rb_str_cat2(ret, "'");
|
192
|
+
return ret;
|
193
|
+
}
|
194
|
+
default:
|
195
|
+
rb_raise(rb_eRuntimeError, "no implemented");
|
196
|
+
}
|
205
197
|
}
|
206
198
|
|
207
|
-
zval*
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
199
|
+
zval *value_to_zval(VALUE v) {
|
200
|
+
zval *zv;
|
201
|
+
MAKE_STD_ZVAL(zv);
|
202
|
+
|
203
|
+
switch (TYPE(v)) {
|
204
|
+
case T_FALSE:
|
205
|
+
ZVAL_FALSE(zv);
|
206
|
+
return zv;
|
207
|
+
case T_TRUE:
|
208
|
+
ZVAL_TRUE(zv);
|
209
|
+
return zv;
|
210
|
+
case T_UNDEF:
|
211
|
+
case T_NIL:
|
212
|
+
ZVAL_NULL(zv);
|
213
|
+
return zv;
|
214
|
+
case T_FIXNUM:
|
215
|
+
ZVAL_LONG(zv, rb_fix2int(v));
|
216
|
+
return zv;
|
217
|
+
case T_BIGNUM:
|
218
|
+
ZVAL_LONG(zv, rb_big2long(v)); // FIXME: bignum over long
|
219
|
+
return zv;
|
220
|
+
case T_FLOAT:
|
221
|
+
ZVAL_DOUBLE(zv, RFLOAT_VALUE(v));
|
222
|
+
return zv;
|
223
|
+
case T_ARRAY:
|
224
|
+
{
|
225
|
+
int i;
|
226
|
+
array_init(zv);
|
227
|
+
for(i=0;i<RARRAY_LEN(v);++i) {
|
228
|
+
zval *add = value_to_zval(RARRAY_PTR(v)[i]);
|
229
|
+
zend_hash_next_index_insert(Z_ARRVAL_P(zv), &add, sizeof(zval *), NULL);
|
230
|
+
}
|
231
|
+
return zv;
|
232
|
+
}
|
233
|
+
case T_HASH:
|
234
|
+
{
|
235
|
+
array_init(zv);
|
236
|
+
rb_hash_foreach(v, hash_to_zval, (VALUE)zv);
|
237
|
+
return zv;
|
238
|
+
}
|
239
|
+
case T_SYMBOL:
|
240
|
+
{
|
241
|
+
VALUE symbol_str = rb_sym_to_s(v);
|
242
|
+
ZVAL_STRINGL(zv, StringValuePtr(symbol_str), RSTRING_LEN(symbol_str), 1);
|
243
|
+
return zv;
|
244
|
+
}
|
245
|
+
case T_STRING:
|
246
|
+
{
|
247
|
+
ZVAL_STRINGL(zv, StringValuePtr(v), RSTRING_LEN(v), 1);
|
248
|
+
return zv;
|
249
|
+
}
|
250
|
+
default:
|
251
|
+
{
|
252
|
+
if (CLASS_OF(v) == cPhpEmbedValue) {
|
253
|
+
php_value* pv;
|
254
|
+
Data_Get_Struct(v, php_value, pv);
|
255
|
+
MAKE_COPY_ZVAL(&pv->value, zv);
|
256
|
+
return zv;
|
257
|
+
}
|
258
|
+
}
|
259
|
+
FREE_ZVAL(zv);
|
260
|
+
rb_raise(rb_eRuntimeError, "no implemented");
|
261
|
+
}
|
270
262
|
}
|
271
|
-
|