diff --git a/src/php/runtime/env/CallStackItem.java b/src/php/runtime/env/CallStackItem.java index e20833521..ef10c3ddc 100644 --- a/src/php/runtime/env/CallStackItem.java +++ b/src/php/runtime/env/CallStackItem.java @@ -126,7 +126,7 @@ else if (clazz != null){ sb.append("("); if (withArgs) { StringWriter writer = new StringWriter(); - PlainPrinter printer = new PlainPrinter(writer); + PlainPrinter printer = new PlainPrinter(null, writer); int i = 0; if (args != null) for(Memory arg : args){ diff --git a/src/php/runtime/ext/core/InfoFunctions.java b/src/php/runtime/ext/core/InfoFunctions.java index cc7bf1066..d868799d0 100644 --- a/src/php/runtime/ext/core/InfoFunctions.java +++ b/src/php/runtime/ext/core/InfoFunctions.java @@ -192,7 +192,7 @@ public static String sys_get_temp_dir(){ public static Memory print_r(Environment env, @Runtime.Reference Memory value, boolean returned){ StringWriter writer = new StringWriter(); - Printer printer = new PrintR(writer); + Printer printer = new PrintR(env, writer); printer.print(value); if (returned){ @@ -209,7 +209,7 @@ public static Memory print_r(Environment env, @Runtime.Reference Memory value){ public static Memory var_dump(Environment env, @Runtime.Reference Memory value, @Runtime.Reference Memory... values){ StringWriter writer = new StringWriter(); - VarDump printer = new VarDump(writer); + VarDump printer = new VarDump(env, writer); printer.print(value); if (values != null) @@ -222,7 +222,7 @@ public static Memory var_dump(Environment env, @Runtime.Reference Memory value, public static Memory var_export(Environment env, TraceInfo trace, @Runtime.Reference Memory value, boolean returned){ StringWriter writer = new StringWriter(); - VarExport printer = new VarExport(writer); + VarExport printer = new VarExport(env, writer); printer.print(value); if (printer.isRecursionExists()){ diff --git a/src/php/runtime/memory/output/PlainPrinter.java b/src/php/runtime/memory/output/PlainPrinter.java index d928e04c0..b84d80b5c 100644 --- a/src/php/runtime/memory/output/PlainPrinter.java +++ b/src/php/runtime/memory/output/PlainPrinter.java @@ -1,11 +1,13 @@ package php.runtime.memory.output; +import php.runtime.env.Environment; + import java.io.Writer; public class PlainPrinter extends PrintR { - public PlainPrinter(Writer writer) { - super(writer); + public PlainPrinter(Environment env, Writer writer) { + super(env, writer); PRINT_INDENT = 0; } diff --git a/src/php/runtime/memory/output/PrintR.java b/src/php/runtime/memory/output/PrintR.java index f3e1f6a5c..cb474d775 100644 --- a/src/php/runtime/memory/output/PrintR.java +++ b/src/php/runtime/memory/output/PrintR.java @@ -2,6 +2,7 @@ import php.runtime.common.Modifier; import php.runtime.common.StringUtils; +import php.runtime.env.Environment; import php.runtime.lang.Closure; import php.runtime.lang.ForeachIterator; import php.runtime.memory.*; @@ -15,8 +16,8 @@ public class PrintR extends Printer { protected int PRINT_INDENT = 4; - public PrintR(Writer writer) { - super(writer); + public PrintR(Environment env, Writer writer) { + super(env, writer); } @Override @@ -147,7 +148,22 @@ protected void printObject(ObjectMemory value, int level, Set used) { used.add(value.getPointer()); - ArrayMemory props = value.getProperties(); + ArrayMemory props; + if (env != null && classEntity.methodMagicDebugInfo != null) { + try { + Memory tmp = classEntity.methodMagicDebugInfo.invokeDynamic(value.value, env); + 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 (props != null){ ForeachIterator iterator = props.foreachIterator(false, false); int i = 0; diff --git a/src/php/runtime/memory/output/Printer.java b/src/php/runtime/memory/output/Printer.java index 543a970b9..4dd6aabe0 100644 --- a/src/php/runtime/memory/output/Printer.java +++ b/src/php/runtime/memory/output/Printer.java @@ -1,5 +1,6 @@ package php.runtime.memory.output; +import php.runtime.env.Environment; import php.runtime.lang.Closure; import php.runtime.memory.*; import php.runtime.Memory; @@ -13,9 +14,11 @@ abstract public class Printer { protected boolean recursionExists = false; protected PrintWriter printer; + protected final Environment env; - public Printer(Writer writer){ + public Printer(Environment env, Writer writer){ printer = new PrintWriter(writer); + this.env = env; } public boolean isRecursionExists() { diff --git a/src/php/runtime/memory/output/VarDump.java b/src/php/runtime/memory/output/VarDump.java index fa2563c97..e705c421f 100644 --- a/src/php/runtime/memory/output/VarDump.java +++ b/src/php/runtime/memory/output/VarDump.java @@ -1,11 +1,12 @@ package php.runtime.memory.output; +import php.runtime.Memory; import php.runtime.common.Modifier; import php.runtime.common.StringUtils; +import php.runtime.env.Environment; import php.runtime.lang.Closure; import php.runtime.lang.ForeachIterator; import php.runtime.memory.*; -import php.runtime.Memory; import php.runtime.reflection.ClassEntity; import java.io.Writer; @@ -15,8 +16,8 @@ public class VarDump extends Printer { private final static int PRINT_INDENT = 2; - public VarDump(Writer writer) { - super(writer); + public VarDump(Environment env, Writer writer) { + super(env, writer); } @Override @@ -144,7 +145,21 @@ protected void printObject(ObjectMemory value, int level, Set used) { if (used.contains(value.getPointer())){ printer.write("*RECURSION*\n"); } else { - ArrayMemory arr = value.getProperties(); + ArrayMemory arr; + if (classEntity.methodMagicDebugInfo != null) { + try { + Memory tmp = classEntity.methodMagicDebugInfo.invokeDynamic(value.value, env); + if (tmp.isArray()) + arr = tmp.toValue(ArrayMemory.class); + else + arr = new ArrayMemory(); + } catch (RuntimeException e) { + throw e; + } catch (Throwable throwable) { + throw new RuntimeException(throwable); + } + } else + arr = value.getProperties(); printer.write("object("); printer.write(classEntity.getName()); diff --git a/src/php/runtime/memory/output/VarExport.java b/src/php/runtime/memory/output/VarExport.java index 8b37652d4..46ef0ba62 100644 --- a/src/php/runtime/memory/output/VarExport.java +++ b/src/php/runtime/memory/output/VarExport.java @@ -1,6 +1,7 @@ package php.runtime.memory.output; import php.runtime.common.StringUtils; +import php.runtime.env.Environment; import php.runtime.lang.Closure; import php.runtime.lang.ForeachIterator; import php.runtime.memory.*; @@ -14,8 +15,8 @@ public class VarExport extends Printer { private final static int PRINT_INDENT = 2; - public VarExport(Writer writer) { - super(writer); + public VarExport(Environment env, Writer writer) { + super(env, writer); } @Override diff --git a/src/php/runtime/reflection/ClassEntity.java b/src/php/runtime/reflection/ClassEntity.java index 59ad37be4..2697518a9 100644 --- a/src/php/runtime/reflection/ClassEntity.java +++ b/src/php/runtime/reflection/ClassEntity.java @@ -67,6 +67,7 @@ public enum Type { CLASS, INTERFACE, TRAIT } public MethodEntity methodMagicClone; public MethodEntity methodMagicSleep; public MethodEntity methodMagicWakeup; + public MethodEntity methodMagicDebugInfo; protected MethodEntity constructor; @@ -282,6 +283,8 @@ public void doneDeclare(){ methodMagicSleep = methods.get("__sleep"); methodMagicWakeup = methods.get("__wakeup"); + + methodMagicDebugInfo = methods.get("__debuginfo"); } public Extension getExtension() { diff --git a/tests/resources/zend/classes/__debug_info_001.php b/tests/resources/zend/classes/__debug_info_001.php new file mode 100644 index 000000000..30967af6c --- /dev/null +++ b/tests/resources/zend/classes/__debug_info_001.php @@ -0,0 +1,34 @@ +--TEST-- +Testing __debugInfo() magic method +--FILE-- +1, "\0*\0b"=>2, "\0Foo\0c"=>3]; + } +} + +$f = new Foo; +var_dump($f); +print_r($f); +?> +--EXPECTF-- +object(Foo)#%d (3) { + ["a"]=> + int(1) + ["b":protected]=> + int(2) + ["c":"Foo":private]=> + int(3) +} +Foo Object +( + [a] => 1 + [b:protected] => 2 + [c:Foo:private] => 3 +) \ No newline at end of file diff --git a/tests/ru/regenix/jphp/compiler/jvm/zend/ClassesTest.java b/tests/ru/regenix/jphp/compiler/jvm/zend/ClassesTest.java index a748d8d46..a3a3c7bf6 100644 --- a/tests/ru/regenix/jphp/compiler/jvm/zend/ClassesTest.java +++ b/tests/ru/regenix/jphp/compiler/jvm/zend/ClassesTest.java @@ -422,4 +422,9 @@ public void testAutoload(){ check("zend/classes/autoload_021.php",true); } + + @Test + public void testDebugInfo() { + check("zend/classes/__debug_info_001.php"); + } }