Skip to content

Commit

Permalink
Implement __debugInfo magic method (php 5.6 rfc)
Browse files Browse the repository at this point in the history
  • Loading branch information
dim-s committed Feb 12, 2014
1 parent 07929c1 commit af84ab3
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/php/runtime/env/CallStackItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
6 changes: 3 additions & 3 deletions src/php/runtime/ext/core/InfoFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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)
Expand All @@ -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()){
Expand Down
6 changes: 4 additions & 2 deletions src/php/runtime/memory/output/PlainPrinter.java
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
22 changes: 19 additions & 3 deletions src/php/runtime/memory/output/PrintR.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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
Expand Down Expand Up @@ -147,7 +148,22 @@ protected void printObject(ObjectMemory value, int level, Set<Integer> 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;
Expand Down
5 changes: 4 additions & 1 deletion src/php/runtime/memory/output/Printer.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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() {
Expand Down
23 changes: 19 additions & 4 deletions src/php/runtime/memory/output/VarDump.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -144,7 +145,21 @@ protected void printObject(ObjectMemory value, int level, Set<Integer> 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());
Expand Down
5 changes: 3 additions & 2 deletions src/php/runtime/memory/output/VarExport.java
Original file line number Diff line number Diff line change
@@ -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.*;
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/php/runtime/reflection/ClassEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -282,6 +283,8 @@ public void doneDeclare(){

methodMagicSleep = methods.get("__sleep");
methodMagicWakeup = methods.get("__wakeup");

methodMagicDebugInfo = methods.get("__debuginfo");
}

public Extension getExtension() {
Expand Down
34 changes: 34 additions & 0 deletions tests/resources/zend/classes/__debug_info_001.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
Testing __debugInfo() magic method
--FILE--
<?php

class Foo {
public $d = 4;
protected $e = 5;
private $f = 6;

public function __debugInfo() {
return ['a'=>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
)
5 changes: 5 additions & 0 deletions tests/ru/regenix/jphp/compiler/jvm/zend/ClassesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

0 comments on commit af84ab3

Please sign in to comment.