use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class InvokeArgumentHelper method unpackArgs.
public static Memory[] unpackArgs(Environment env, TraceInfo trace, Memory[] passed, ParameterEntity[] parameters) {
List<Memory> varPassed = null;
if (passed == null) {
return null;
}
int cnt = 0, paramCnt = 0;
ParameterEntity parameterEntity = null;
boolean variadicMemoryExists = false;
for (Memory arg : passed) {
if (arg instanceof VariadicMemory) {
variadicMemoryExists = true;
if (varPassed == null) {
varPassed = new ArrayList<Memory>();
for (int i = 0; i < cnt; i++) {
varPassed.add(passed[i]);
}
}
boolean isGenerator = arg.instanceOf(Generator.class);
ForeachIterator foreachIterator = arg.getNewIterator(env, !isGenerator, false);
if (foreachIterator == null || (!isGenerator && !arg.isTraversable())) {
env.warning(trace, INVALID_TYPE_MESSAGE);
} else {
boolean isRef;
while (foreachIterator.next()) {
if (parameters != null) {
if (parameterEntity == null || !parameterEntity.isVariadic()) {
parameterEntity = paramCnt < parameters.length ? parameters[paramCnt] : null;
}
}
isRef = parameterEntity != null && parameterEntity.isReference();
Memory value = foreachIterator.getValue();
varPassed.add(isRef ? value : value.toImmutable());
paramCnt++;
if (parameterEntity != null && !parameterEntity.isVariadic()) {
parameterEntity = null;
}
}
}
} else {
if (variadicMemoryExists) {
env.error(trace, "Cannot use positional argument after argument unpacking");
}
if (varPassed != null) {
varPassed.add(arg);
}
paramCnt++;
}
cnt++;
}
if (varPassed != null) {
passed = varPassed.toArray(new Memory[varPassed.size()]);
}
return passed;
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class WrapSourceMap method insertLines.
@Signature({ @Arg(value = "inserts", type = HintType.ARRAY), @Arg("allCountLines") })
public Memory insertLines(Environment env, Memory... args) {
ArrayMemory _inserts = args[0].toValue(ArrayMemory.class);
int[][] inserts = new int[_inserts.size()][];
ForeachIterator iterator = _inserts.getNewIterator(env);
int i = 0;
while (iterator.next()) {
Memory value = iterator.getValue();
inserts[i++] = new int[] { value.valueOfIndex(0).toInteger(), value.valueOfIndex(1).toInteger() };
}
getWrappedObject().insertLines(inserts, args[1].toInteger());
return Memory.NULL;
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class LangFunctions method extract.
public static int extract(Environment env, TraceInfo trace, @Runtime.GetLocals ArrayMemory locals, @Runtime.Reference Memory array, int extractType, Memory _prefix) {
if (expecting(env, trace, 1, array, Memory.Type.ARRAY)) {
boolean isRefs = (extractType & LangConstants.EXTR_REFS) == LangConstants.EXTR_REFS;
ForeachIterator iterator = array.getNewIterator(env, isRefs, false);
int count = 0;
if (extractType == LangConstants.EXTR_PREFIX_ALL || extractType == LangConstants.EXTR_PREFIX_IF_EXISTS || extractType == LangConstants.EXTR_PREFIX_INVALID || extractType == LangConstants.EXTR_PREFIX_SAME)
if (_prefix.isNull()) {
env.warning(trace, "extract(): specified extract type requires the prefix parameter");
return 0;
}
String prefix = _prefix.isNull() ? "" : _prefix.concat("_");
if (!prefix.isEmpty())
if (!GrammarUtils.isValidName(prefix)) {
env.warning(trace, "extract(): prefix is not a valid identifier");
return 0;
}
while (iterator.next()) {
Object key = iterator.getKey();
String keyS = key.toString();
String var;
Memory value = iterator.getValue();
if (!isRefs)
value = value.toImmutable();
switch(extractType) {
case LangConstants.EXTR_OVERWRITE:
if (GrammarUtils.isValidName(keyS)) {
locals.refOfIndex(keyS).assign(value);
count++;
}
break;
case LangConstants.EXTR_SKIP:
if (GrammarUtils.isValidName(keyS) && locals.valueOfIndex(keyS).isUndefined()) {
locals.refOfIndex(keyS).assign(value);
count++;
}
break;
case LangConstants.EXTR_PREFIX_SAME:
if (!locals.valueOfIndex(keyS).isUndefined()) {
var = prefix.concat(keyS);
if (GrammarUtils.isValidName(var)) {
locals.refOfIndex(var).assign(value);
count++;
}
} else if (GrammarUtils.isValidName(keyS)) {
locals.refOfIndex(keyS).assign(value);
count++;
}
break;
case LangConstants.EXTR_PREFIX_ALL:
var = prefix.concat(keyS);
if (GrammarUtils.isValidName(var)) {
locals.refOfIndex(prefix.concat(keyS)).assign(value);
count++;
}
break;
case LangConstants.EXTR_PREFIX_INVALID:
if (GrammarUtils.isValidName(keyS)) {
locals.refOfIndex(keyS).assign(value);
count++;
} else {
var = prefix.concat(keyS);
if (GrammarUtils.isValidName(var)) {
locals.refOfIndex(var).assign(value);
count++;
}
}
break;
case LangConstants.EXTR_IF_EXISTS:
if (GrammarUtils.isValidName(keyS))
if (!locals.valueOfIndex(keyS).isUndefined()) {
locals.refOfIndex(keyS).assign(value);
count++;
}
break;
case LangConstants.EXTR_PREFIX_IF_EXISTS:
if (!locals.valueOfIndex(keyS).isUndefined()) {
var = prefix.concat(keyS);
if (GrammarUtils.isValidName(var)) {
locals.refOfIndex(var).assign(value);
count++;
}
}
break;
}
}
return count;
} else
return 0;
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class LangFunctions method get_object_vars.
public static Memory get_object_vars(Environment env, TraceInfo trace, Memory object) {
if (expecting(env, trace, 1, object, Memory.Type.OBJECT)) {
ObjectMemory o = object.toValue(ObjectMemory.class);
ArrayMemory props = o.value.getProperties();
ClassEntity entity = o.getReflection();
ClassEntity context = env.getLastClassOnStack();
ForeachIterator iterator = props.foreachIterator(false, false);
ArrayMemory result = new ArrayMemory();
while (iterator.next()) {
PropertyEntity prop = entity.findProperty(iterator.getKey().toString());
if (prop == null || prop.canAccess(env, context) == 0)
result.refOfIndex(prop == null ? iterator.getKey().toString() : prop.getName()).assign(iterator.getValue());
}
return result.toConstant();
} else
return Memory.NULL;
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class ArrayMemory method toIntMap.
public Map<String, Integer> toIntMap() {
Map<String, Integer> r = new LinkedHashMap<String, Integer>();
ForeachIterator iterator = foreachIterator(false, false);
while (iterator.next()) {
r.put(iterator.getKey().toString(), iterator.getValue().toInteger());
}
return r;
}
Aggregations