forked from php/frankenphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
302 lines (251 loc) · 6.77 KB
/
Copy pathtypes.go
File metadata and controls
302 lines (251 loc) · 6.77 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package frankenphp
/*
#include "types.h"
*/
import "C"
import "unsafe"
// EXPERIMENTAL: GoString copies a zend_string to a Go string.
func GoString(s unsafe.Pointer) string {
if s == nil {
return ""
}
zendStr := (*C.zend_string)(s)
return C.GoStringN((*C.char)(unsafe.Pointer(&zendStr.val)), C.int(zendStr.len))
}
// EXPERIMENTAL: PHPString converts a Go string to a zend_string with copy. The string can be
// non-persistent (automatically freed after the request by the ZMM) or persistent. If you choose
// the second mode, it is your repsonsability to free the allocated memory.
func PHPString(s string, persistent bool) unsafe.Pointer {
if s == "" {
return nil
}
zendStr := C.zend_string_init(
(*C.char)(unsafe.Pointer(unsafe.StringData(s))),
C.size_t(len(s)),
C._Bool(persistent),
)
return unsafe.Pointer(zendStr)
}
// PHPKeyType represents the type of PHP hashmap key
type PHPKeyType int
const (
PHPIntKey PHPKeyType = iota
PHPStringKey
)
type PHPKey struct {
Type PHPKeyType
Str string
Int int64
}
// Array represents a PHP array with ordered key-value pairs
type Array struct {
keys []PHPKey
values []interface{}
}
// SetInt sets a value with an integer key
func (arr *Array) SetInt(key int64, value interface{}) {
arr.keys = append(arr.keys, PHPKey{Type: PHPIntKey, Int: key})
arr.values = append(arr.values, value)
}
// SetString sets a value with a string key
func (arr *Array) SetString(key string, value interface{}) {
arr.keys = append(arr.keys, PHPKey{Type: PHPStringKey, Str: key})
arr.values = append(arr.values, value)
}
// Append adds a value to the end of the array with the next available integer key
func (arr *Array) Append(value interface{}) {
nextKey := arr.getNextIntKey()
arr.SetInt(nextKey, value)
}
// getNextIntKey finds the next available integer key
func (arr *Array) getNextIntKey() int64 {
maxKey := int64(-1)
for _, key := range arr.keys {
if key.Type == PHPIntKey && key.Int > maxKey {
maxKey = key.Int
}
}
return maxKey + 1
}
// Len returns the number of elements in the array
func (arr *Array) Len() uint32 {
return uint32(len(arr.keys))
}
// At returns the key and value at the given index
func (arr *Array) At(index uint32) (PHPKey, interface{}) {
if index >= uint32(len(arr.keys)) {
return PHPKey{}, nil
}
return arr.keys[index], arr.values[index]
}
// EXPERIMENTAL: GoArray converts a zend_array to a Go Array
func GoArray(arr unsafe.Pointer) *Array {
result := &Array{
keys: make([]PHPKey, 0),
values: make([]interface{}, 0),
}
if arr == nil {
return result
}
zval := (*C.zval)(arr)
hashTable := (*C.HashTable)(castZval(zval, C.IS_ARRAY))
if hashTable == nil {
return result
}
used := hashTable.nNumUsed
if htIsPacked(hashTable) {
for i := C.uint32_t(0); i < used; i++ {
v := C.get_ht_packed_data(hashTable, i)
if v != nil && C.zval_get_type(v) != C.IS_UNDEF {
value := convertZvalToGo(v)
result.SetInt(int64(i), value)
}
}
return result
}
for i := C.uint32_t(0); i < used; i++ {
bucket := C.get_ht_bucket_data(hashTable, i)
if bucket == nil || C.zval_get_type(&bucket.val) == C.IS_UNDEF {
continue
}
v := convertZvalToGo(&bucket.val)
if bucket.key != nil {
keyStr := GoString(unsafe.Pointer(bucket.key))
result.SetString(keyStr, v)
continue
}
result.SetInt(int64(bucket.h), v)
}
return result
}
// PHPArray converts a Go Array to a PHP zend_array.
func PHPArray(arr *Array) unsafe.Pointer {
if arr == nil || arr.Len() == 0 {
return unsafe.Pointer(createNewArray(0))
}
isList := true
for i, k := range arr.keys {
if k.Type != PHPIntKey || k.Int != int64(i) {
isList = false
break
}
}
var zendArray *C.HashTable
if isList {
zendArray = createNewArray(arr.Len())
for _, v := range arr.values {
zval := convertGoToZval(v)
C.zend_hash_next_index_insert(zendArray, zval)
}
return unsafe.Pointer(zendArray)
}
zendArray = createNewArray(arr.Len())
for i, k := range arr.keys {
zval := convertGoToZval(arr.values[i])
if k.Type == PHPStringKey {
keyStr := PHPString(k.Str, false)
C.zend_hash_update(zendArray, (*C.zend_string)(keyStr), zval)
continue
}
C.zend_hash_index_update(zendArray, C.zend_ulong(k.Int), zval)
}
return unsafe.Pointer(zendArray)
}
// convertZvalToGo converts a PHP zval to a Go interface{}
func convertZvalToGo(zval *C.zval) interface{} {
t := C.zval_get_type(zval)
switch t {
case C.IS_NULL:
return nil
case C.IS_FALSE:
return false
case C.IS_TRUE:
return true
case C.IS_LONG:
longPtr := (*C.zend_long)(castZval(zval, C.IS_LONG))
if longPtr != nil {
return int64(*longPtr)
}
return int64(0)
case C.IS_DOUBLE:
doublePtr := (*C.double)(castZval(zval, C.IS_DOUBLE))
if doublePtr != nil {
return float64(*doublePtr)
}
return float64(0)
case C.IS_STRING:
str := (*C.zend_string)(castZval(zval, C.IS_STRING))
if str == nil {
return ""
}
return GoString(unsafe.Pointer(str))
case C.IS_ARRAY:
return GoArray(unsafe.Pointer(zval))
default:
return nil
}
}
// convertGoToZval converts a Go interface{} to a PHP zval
func convertGoToZval(value interface{}) *C.zval {
zval := (*C.zval)(C.__emalloc__(C.size_t(unsafe.Sizeof(C.zval{}))))
u1 := (*C.uint8_t)(unsafe.Pointer(&zval.u1[0]))
v0 := unsafe.Pointer(&zval.value[0])
switch v := value.(type) {
case nil:
*u1 = C.IS_NULL
case bool:
if v {
*u1 = C.IS_TRUE
} else {
*u1 = C.IS_FALSE
}
case int:
*u1 = C.IS_LONG
*(*C.zend_long)(v0) = C.zend_long(v)
case int64:
*u1 = C.IS_LONG
*(*C.zend_long)(v0) = C.zend_long(v)
case float64:
*u1 = C.IS_DOUBLE
*(*C.double)(v0) = C.double(v)
case string:
*u1 = C.IS_STRING
*(**C.zend_string)(v0) = (*C.zend_string)(PHPString(v, false))
case *Array:
*u1 = C.IS_ARRAY
*(**C.zend_array)(v0) = (*C.zend_array)(PHPArray(v))
default:
*u1 = C.IS_NULL
}
return zval
}
// createNewArray creates a new zend_array with the specified size.
func createNewArray(size uint32) *C.HashTable {
ht := C.__emalloc__(C.size_t(unsafe.Sizeof(C.HashTable{})))
C.__zend_hash_init__((*C.struct__zend_array)(ht), C.uint32_t(size), nil, C._Bool(false))
return (*C.HashTable)(ht)
}
// htIsPacked checks if a HashTable is a list (packed) or hashmap (not packed).
func htIsPacked(ht *C.HashTable) bool {
flags := *(*C.uint32_t)(unsafe.Pointer(&ht.u[0]))
return (flags & C.HASH_FLAG_PACKED) != 0
}
// castZval casts a zval to the expected type and returns a pointer to the value
func castZval(zval *C.zval, expectedType C.uint8_t) unsafe.Pointer {
if zval == nil || C.zval_get_type(zval) != expectedType {
return nil
}
v := unsafe.Pointer(&zval.value[0])
switch expectedType {
case C.IS_LONG:
return v
case C.IS_DOUBLE:
return v
case C.IS_STRING:
return unsafe.Pointer(*(**C.zend_string)(v))
case C.IS_ARRAY:
return unsafe.Pointer(*(**C.zend_array)(v))
default:
return nil
}
}