use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class Serializer method writeObject.
public void writeObject(ObjectMemory memory, Set<Integer> used) {
if (used.add(memory.getPointer())) {
IObject object = memory.value;
ClassEntity reflection = object.getReflection();
if (object instanceof Serializable) {
Memory result;
env.pushCall(trace, object, "serialize");
try {
result = ((Serializable) object).serialize(env);
if (result.isNull()) {
writeNull();
return;
}
if (result.isString()) {
String value = result.toString();
printer.append("C:").append(reflection.getName().length()).append(":\"").append(reflection.getName()).append("\":").append(value.length()).append(":{").append(value).append("}");
return;
} else {
env.exception(trace, reflection.getName() + "::serialize() must return a string or NULL");
}
} finally {
env.popCall();
}
}
ArrayMemory only = null;
if (reflection.methodMagicSleep != null) {
env.pushCall(trace, object, reflection.methodMagicSleep.getName());
try {
Memory result = reflection.methodMagicSleep.invokeDynamic(object, env);
if (!result.isArray()) {
env.error(ErrorType.E_NOTICE, "serialize(): __sleep() should return an array only containing the names of instance-variables to serialize");
writeNull();
return;
} else {
ForeachIterator iterator = result.getNewIterator(env, false, false);
only = new ArrayMemory(true);
ArrayMemory props = memory.getProperties();
Set<String> need = new LinkedHashSet<String>();
while (iterator.next()) {
if (iterator.getValue().isNumber())
continue;
need.add(iterator.getValue().toString());
}
for (PropertyEntity e : reflection.getProperties()) {
if (need.contains(e.getName())) {
props.refOfIndex(e.getSpecificName());
}
}
iterator = result.getNewIterator(env, false, false);
while (iterator.next()) {
Memory value = iterator.getValue().toValue();
PropertyEntity entity = reflection.findProperty(value.toString());
value = entity == null ? props.valueOfIndex(value).toValue() : props.valueOfIndex(entity.getSpecificName()).toValue();
if (value == Memory.UNDEFINED) {
env.error(trace, ErrorType.E_NOTICE, "serialize(): \"%s\" returned as member variable from __sleep() but does not exist", iterator.getValue().toString());
}
if (entity != null)
only.put(entity.getSpecificName(), value);
else
only.refOfIndex(iterator.getValue()).assign(value);
}
}
} catch (RuntimeException e) {
throw e;
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
} finally {
env.popCall();
}
}
printer.append("O:");
printer.append(String.valueOf(reflection.getName().length()));
printer.append(":\"");
printer.append(reflection.getName());
printer.append("\":");
if (reflection.getProperties() == null)
writeArray(new ArrayMemory(), used, false);
else
writeArray(only == null ? object.getProperties() : only, used, false);
} else
writeNull();
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class MapMemoryOperation method convert.
@Override
@SuppressWarnings("unchecked")
public Map convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
Map result = this.createHashMap();
ForeachIterator iterator = arg.getNewIterator(env);
while (iterator.next()) {
result.put(operations[0].convert(env, trace, iterator.getMemoryKey()), operations[1].convert(env, trace, iterator.getValue()));
}
return result;
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class PrintR method printObject.
@Override
protected void printObject(ObjectMemory value, int level, Set<Integer> used) {
ClassEntity classEntity = value.getReflection();
writeObjectHeader(classEntity.getName());
if (used.contains(value.getPointer())) {
printer.write(" *RECURSION*");
} else {
printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
writeOpen();
level += 1;
used.add(value.getPointer());
ArrayMemory props;
if (env != null && classEntity.methodMagicDebugInfo != null) {
try {
Memory tmp = env.invokeMethod(value.value, classEntity.methodMagicDebugInfo.getName());
if (tmp.isArray()) {
props = tmp.toValue(ArrayMemory.class);
} else {
props = new ArrayMemory();
}
} catch (RuntimeException e) {
throw e;
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
} else {
props = value.getProperties();
}
if (classEntity.methodMagicDebugInfo != null) {
for (PropertyEntity entity : classEntity.getProperties()) {
if (entity.getGetter() != null && !entity.isHiddenInDebugInfo()) {
printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
printer.write("[");
printer.write(entity.getName());
printer.write(":getter] => ");
try {
print(entity.getValue(env, TraceInfo.UNKNOWN, value.value));
} catch (RuntimeException e) {
throw e;
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
writeSeparator(false);
}
}
}
if (props != null) {
ForeachIterator iterator = props.foreachIterator(false, false);
int i = 0;
int size = classEntity.properties.size();
while (iterator.next()) {
printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
Object key = iterator.getKey();
printer.write('[');
String realKey = key.toString();
int pos;
Modifier modifier = Modifier.PUBLIC;
String className = "";
if ((pos = realKey.lastIndexOf("\0")) > -1) {
if (realKey.startsWith("\0*\0")) {
modifier = Modifier.PROTECTED;
} else {
modifier = Modifier.PRIVATE;
className = realKey.substring(1, pos);
}
realKey = realKey.substring(pos + 1);
}
printer.write(realKey);
switch(modifier) {
case PRIVATE:
printer.write(":" + className + ":private");
break;
case PROTECTED:
printer.write(":protected");
}
printer.write("] => ");
print(iterator.getValue(), level + 1, used);
writeSeparator(i == size - 1);
i++;
}
}
level -= 1;
printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
writeClose();
used.remove(value.getPointer());
}
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class VarDump method printArray.
@Override
protected void printArray(ArrayMemory value, int level, Set<Integer> used) {
if (used.contains(value.getPointer())) {
printer.write("*RECURSION*\n");
} else {
printer.write("array(");
printer.write(value.size() + "");
printer.write(") {\n");
level += 1;
used.add(value.getPointer());
ForeachIterator iterator = value.foreachIterator(false, false);
while (iterator.next()) {
Memory el = iterator.getValue();
if (el == Memory.UNDEFINED)
continue;
printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
Memory key = iterator.getMemoryKey();
printer.write('[');
if (key.isString()) {
printer.write('"');
printer.write(key.toString());
printer.write('"');
} else {
printer.write(key.toString());
}
printer.write("]=>\n");
printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
print(el, level, used);
//printer.write('\n');
}
level -= 1;
printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
printer.write("}\n");
used.remove(value.getPointer());
}
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class ArrayMemory method resetCurrentIterator.
public Memory resetCurrentIterator() {
reset();
ForeachIterator iterator = getCurrentIterator();
if (size == 0)
return FALSE;
else {
iterator.next();
Memory tmp = iterator.getValue();
iterator.prev();
return tmp;
}
}
Aggregations