1
- /*
2
- * @author Niklas von Hertzen <niklas at hertzen.com>
3
- * @created 30.6.2012
4
- * @website http://hertzen.com
5
- */
6
1
PHP . Modules . prototype . $foreachInit = function ( expr , ctx ) {
7
2
8
3
var COMPILER = PHP . Compiler . prototype ,
@@ -30,14 +25,14 @@ PHP.Modules.prototype.$foreachInit = function( expr, ctx ) {
30
25
if ( objectValue [ PHP . VM . Class . INTERFACES ] . indexOf ( "Traversable" ) !== - 1 ) {
31
26
32
27
var iterator = objectValue ;
33
-
28
+
34
29
try {
35
30
while ( ( iterator instanceof PHP . VM . ClassPrototype ) && iterator [ PHP . VM . Class . INTERFACES ] . indexOf ( "Iterator" ) === - 1 ) {
36
- iterator = iterator [ COMPILER . METHOD_CALL ] ( this , "getIterator" ) [ COMPILER . VARIABLE_VALUE ] ;
31
+ iterator = iterator [ COMPILER . METHOD_CALL ] ( this , "getIterator" ) [ COMPILER . VARIABLE_VALUE ] ;
37
32
}
38
33
} catch ( e ) {
39
-
40
- }
34
+
35
+ }
41
36
if ( ! ( iterator instanceof PHP . VM . ClassPrototype ) || iterator [ PHP . VM . Class . INTERFACES ] . indexOf ( "Iterator" ) === - 1 ) {
42
37
this [ COMPILER . ERROR ] ( "Objects returned by " + objectValue [ COMPILER . CLASS_NAME ] + "::getIterator() must be traversable or implement interface Iterator" , PHP . Constants . E_ERROR , true ) ;
43
38
return ;
@@ -62,28 +57,28 @@ PHP.Modules.prototype.$foreachInit = function( expr, ctx ) {
62
57
63
58
needReorder = false ;
64
59
for ( var key in objectValue ) {
65
-
60
+
66
61
if ( key . substring ( 0 , classProperty . length ) === classProperty ) {
67
-
62
+
68
63
var name = key . substring ( classProperty . length ) ;
69
-
64
+
70
65
if ( PHP . Utils . Visible . call ( this , name , objectValue , ctx ) ) {
71
66
items . push ( name ) ;
72
67
}
73
68
74
69
}
75
-
70
+
76
71
if ( ( ( objectValue [ PHP . VM . Class . PROPERTY_TYPE + name ] & PHP . VM . Class . PUBLIC ) === PHP . VM . Class . PUBLIC ) || objectValue [ PHP . VM . Class . PROPERTY_TYPE + name ] === undefined ) {
77
72
78
-
73
+
79
74
} else {
80
75
needReorder = true ;
81
76
}
82
77
}
83
78
if ( needReorder ) {
84
- items . sort ( ) ;
79
+ items . sort ( ) ;
85
80
}
86
-
81
+
87
82
return items ;
88
83
} . bind ( this ) ) ( objectValue )
89
84
@@ -125,52 +120,25 @@ PHP.Modules.prototype.foreach = function( iterator, byRef, value, key ) {
125
120
}
126
121
127
122
if ( expr [ VAR . TYPE ] === VAR . ARRAY ) {
128
-
129
-
130
-
131
- /*
132
- if ( iterator.expr[ VAR.IS_REF ] !== true ) {
133
- expr = iterator.clone;
134
- } else {
135
- expr = expr[ COMPILER.VARIABLE_VALUE ];
136
- }
137
- */
138
123
var clonedValues = iterator . clone [ PHP . VM . Class . PROPERTY + ARRAY . VALUES ] [ COMPILER . VARIABLE_VALUE ] ,
139
124
clonedKeys = iterator . clone [ PHP . VM . Class . PROPERTY + ARRAY . KEYS ] [ COMPILER . VARIABLE_VALUE ] ,
140
125
origValues = expr [ COMPILER . VARIABLE_VALUE ] [ PHP . VM . Class . PROPERTY + ARRAY . VALUES ] [ COMPILER . VARIABLE_VALUE ] ,
141
126
origKeys = expr [ COMPILER . VARIABLE_VALUE ] [ PHP . VM . Class . PROPERTY + ARRAY . KEYS ] [ COMPILER . VARIABLE_VALUE ] ,
142
127
len = ( byRef === true || iterator . expr [ VAR . IS_REF ] === true ) ? origValues . length : iterator . len ,
143
128
pointer = ( ( byRef === true || iterator . expr [ VAR . IS_REF ] === true ) ? expr [ COMPILER . VARIABLE_VALUE ] : iterator . clone ) [ PHP . VM . Class . PROPERTY + ARRAY . POINTER ] ;
144
129
145
-
146
- // clean unset elements off array
147
- /*
148
- if ( byRef === true ) {
149
- origValues.forEach(function( variable, index ) {
150
- if ( variable[ VAR.DEFINED ] !== true ) {
151
- origValues.splice( index, 1 );
152
- origKeys.splice( index, 1 );
153
- console.log(origValues);
154
- }
155
- });
156
- }*/
157
-
158
130
var compareTo = ( byRef === true || iterator . expr [ VAR . IS_REF ] === true ) ? origValues : clonedValues ,
159
131
result ;
160
132
161
-
162
133
var index , lowerLoop = function ( index ) {
163
134
while ( compareTo [ -- index ] === undefined && index > 0 ) { }
164
135
return index ;
165
136
}
166
137
167
-
168
138
if ( iterator . breakNext === true ) {
169
-
170
139
return false ;
171
140
}
172
141
173
-
174
142
if ( pointer [ COMPILER . VARIABLE_VALUE ] !== iterator . count ) {
175
143
if ( iterator . last !== undefined && iterator . last !== compareTo [ pointer [ COMPILER . VARIABLE_VALUE ] ] ) {
176
144
index = pointer [ COMPILER . VARIABLE_VALUE ] ;
@@ -293,15 +261,9 @@ PHP.Modules.prototype.foreach = function( iterator, byRef, value, key ) {
293
261
return true ;
294
262
}
295
263
return false ;
296
-
297
264
}
298
-
299
-
300
265
} else {
301
266
this [ COMPILER . ERROR ] ( "Invalid argument supplied for foreach()" , PHP . Constants . E_CORE_WARNING , true ) ;
302
267
return false ;
303
268
}
304
-
305
-
306
-
307
269
} ;
0 commit comments