-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvrzno_array.c
131 lines (103 loc) · 2.95 KB
/
vrzno_array.c
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
typedef struct vrzno_array_iterator {
zend_object_iterator it;
zend_long key;
zval value;
} vrzno_array_iterator;
static void vrzno_array_it_dtor(zend_object_iterator *iter)
{
zval_ptr_dtor(&iter->data);
}
static zend_result vrzno_array_it_valid(zend_object_iterator *it)
{
vrzno_array_iterator *iter = (vrzno_array_iterator*)it;
zend_object *object = Z_OBJ(it->data);
vrzno_object *vrzno = vrzno_fetch_object(object);
int valid = EM_ASM_INT({
let target = Module.targets.get($0);
const property = $1;
if(target instanceof ArrayBuffer)
{
if(!Module.bufferMaps.has(target))
{
Module.bufferMaps.set(target, new Uint8Array(target));
}
target = Module.bufferMaps.get(target);
}
if(Array.isArray(target) || ArrayBuffer.isView(target))
{
if(property >=0 && property < target.length)
{
return 1;
}
}
return 0;
}, vrzno->targetId, iter->key);
if(valid)
{
return SUCCESS;
}
return FAILURE;
}
static zval *vrzno_array_it_get_current_data(zend_object_iterator *it)
{
vrzno_array_iterator *iter = (vrzno_array_iterator*)it;
EM_ASM({
let target = Module.targets.get($0);
const property = $1;
const rv = $2;
if(target instanceof ArrayBuffer)
{
if(!Module.bufferMaps.has(target))
{
Module.bufferMaps.set(target, new Uint8Array(target));
}
target = Module.bufferMaps.get(target);
}
return Module.jsToZval(target[property], rv);
}, vrzno_fetch_object(Z_OBJ(it->data))->targetId, iter->key, &iter->value);
return &iter->value;
}
static void vrzno_array_it_get_current_key(zend_object_iterator *it, zval *key)
{
vrzno_array_iterator *iter = (vrzno_array_iterator*)it;
ZVAL_LONG(key, iter->key);
}
static void vrzno_array_it_move_forward(zend_object_iterator *it)
{
vrzno_array_iterator *iter = (vrzno_array_iterator*)it;
iter->key++;
}
static void vrzno_array_it_rewind(zend_object_iterator *it)
{
vrzno_array_iterator *iter = (vrzno_array_iterator*)it;
iter->key = 0;
}
static void vrzno_array_invalidate_current(zend_object_iterator *it)
{
vrzno_array_iterator *iter = (vrzno_array_iterator *)it;
if (Z_TYPE(iter->value) != IS_UNDEF) {
zval_ptr_dtor(&iter->value);
ZVAL_UNDEF(&iter->value);
}
}
// static HashTable *vrzno_array_it_get_gc(zend_object_iterator *iter, zval **table, int *n)
// {}
static const zend_object_iterator_funcs vrzno_array_it_funcs = {
vrzno_array_it_dtor,
vrzno_array_it_valid,
vrzno_array_it_get_current_data,
vrzno_array_it_get_current_key,
vrzno_array_it_move_forward,
vrzno_array_it_rewind,
vrzno_array_invalidate_current,
NULL, // vrzno_array_it_get_gc,
};
static zend_object_iterator *vrzno_array_get_iterator(zend_class_entry *ce, zval *zv, int by_ref)
{
vrzno_array_iterator *iterator = emalloc(sizeof(vrzno_array_iterator)); // @todo: Figure if we need to clear this.
zend_iterator_init(&iterator->it);
ZVAL_OBJ_COPY(&iterator->it.data, Z_OBJ_P(zv));
iterator->it.funcs = &vrzno_array_it_funcs;
// iterator->by_ref = by_ref;
return &iterator->it;
}