Skip to content

Commit fe15ab2

Browse files
committed
Bugfixes. Add reader & writer memory operations.
1 parent 52e962f commit fe15ab2

File tree

6 files changed

+68
-31
lines changed

6 files changed

+68
-31
lines changed

jphp-core/src/org/develnext/jphp/core/compiler/jvm/statement/ExpressionStmtCompiler.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,6 @@ public void writePushMemory(Memory memory) {
452452
code.add(new MethodInsnNode(INVOKESPECIAL, Type.getInternalName(ReferenceMemory.class), Constants.INIT_METHOD, "()V", false));
453453
} else if (memory instanceof ArrayMemory) {
454454
ArrayMemory array = (ArrayMemory) memory;
455-
456-
457455
if (!array.isList()) {
458456
if (array.size() == 0) {
459457
writeSysStaticCall(ArrayMemory.class, "emptyMap", ArrayMemory.class);

jphp-core/src/org/develnext/jphp/core/compiler/jvm/statement/expr/value/StringBuilderValueCompiler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken;
1010
import php.runtime.Memory;
1111
import php.runtime.memory.BinaryMemory;
12+
import php.runtime.memory.StringMemory;
1213

1314
import java.util.List;
1415

jphp-runtime/src/php/runtime/memory/support/MemoryOperation.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ public int hashCode() {
274274
register(new OutputStreamMemoryOperation());
275275
register(new FileMemoryOperation());
276276
register(new ByteArrayInputStreamMemoryOperation());
277+
register(new ReaderMemoryStream());
278+
register(new WriterMemoryStream());
277279

278280
register(new PatternMemoryOperation());
279281

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,40 @@
11
package php.runtime.memory.support.operation;
22

3-
public class ReaderMemoryStream {
3+
import php.runtime.Memory;
4+
import php.runtime.env.Environment;
5+
import php.runtime.env.TraceInfo;
6+
import php.runtime.exceptions.CriticalException;
7+
import php.runtime.ext.core.classes.stream.FileObject;
8+
import php.runtime.ext.core.classes.stream.Stream;
9+
import php.runtime.memory.support.MemoryOperation;
10+
11+
import java.io.*;
12+
13+
public class ReaderMemoryStream extends MemoryOperation<Reader> {
14+
@Override
15+
public Class<?>[] getOperationClasses() {
16+
return new Class[] { Reader.class };
17+
}
18+
19+
@Override
20+
public Reader convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
21+
if (arg.isNull()) return null;
22+
23+
if (arg.instanceOf(Stream.class)) {
24+
InputStream inputStream = Stream.getInputStream(env, arg);
25+
if (inputStream == null) return null;
26+
27+
return new InputStreamReader(inputStream, env.getDefaultCharset());
28+
} else if (arg.instanceOf(FileObject.class)) {
29+
Stream stream = Stream.create(env, arg.toString(), "r");
30+
return new InputStreamReader(Stream.getInputStream(env, stream), env.getDefaultCharset());
31+
}
32+
33+
return new StringReader(arg.toString());
34+
}
35+
36+
@Override
37+
public Memory unconvert(Environment env, TraceInfo trace, Reader arg) throws Throwable {
38+
throw new CriticalException("Unsupported operation");
39+
}
440
}

jphp-runtime/src/php/runtime/memory/support/operation/WriterMemoryStream.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,29 @@
1010

1111
import java.io.*;
1212

13-
public class ReaderMemoryStream extends MemoryOperation<Reader> {
13+
public class WriterMemoryStream extends MemoryOperation<Writer> {
1414
@Override
1515
public Class<?>[] getOperationClasses() {
16-
return new Class[] { Reader.class };
16+
return new Class[] { Writer.class };
1717
}
1818

1919
@Override
20-
public Reader convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
20+
public Writer convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
2121
if (arg.isNull()) return null;
2222

2323
if (arg.instanceOf(Stream.class)) {
24-
InputStream inputStream = Stream.getInputStream(env, arg);
25-
if (inputStream == null) return null;
24+
OutputStream outputStream = Stream.getOutputStream(env, arg);
25+
if (outputStream == null) return null;
2626

27-
return new InputStreamReader(inputStream, env.getDefaultCharset());
28-
} else if (arg.instanceOf(FileObject.class)) {
29-
Stream stream = Stream.create(env, arg.toString(), "r");
30-
return new InputStreamReader(Stream.getInputStream(env, stream), env.getDefaultCharset());
27+
return new OutputStreamWriter(outputStream, env.getDefaultCharset());
28+
} else {
29+
Stream stream = Stream.create(env, arg.toString(), "w+");
30+
return new OutputStreamWriter(Stream.getOutputStream(env, stream), env.getDefaultCharset());
3131
}
32-
33-
return new StringReader(arg.toString());
3432
}
3533

3634
@Override
37-
public Memory unconvert(Environment env, TraceInfo trace, Reader arg) throws Throwable {
35+
public Memory unconvert(Environment env, TraceInfo trace, Writer arg) throws Throwable {
3836
throw new CriticalException("Unsupported operation");
3937
}
4038
}

jphp-runtime/src/php/runtime/reflection/CompileMethodEntity.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424
import java.util.Arrays;
2525

2626
public class CompileMethodEntity extends MethodEntity {
27+
public static final InjectMemoryOperation INJECT_ENV_MEMORY_OPERATION = new InjectMemoryOperation() {
28+
@Override
29+
public Object convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
30+
return env;
31+
}
32+
};
33+
public static final InjectMemoryOperation INJECT_TRACE_MEMORY_OPERATION = new InjectMemoryOperation() {
34+
@Override
35+
public Object convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
36+
return trace;
37+
}
38+
};
2739
protected CompileMethod function;
2840

2941
{
@@ -88,17 +100,17 @@ public boolean addMethod(Method method, boolean skipConflicts, CallbackW<MemoryO
88100
Class<?>[] parameterTypes = method.getParameterTypes();
89101
int paramCount = parameterTypes.length;
90102

91-
for (int x = 0; x < paramCount; x++) {
92-
//for (Parameter el : method.getParameters()) { // FIX for Android!
93-
Class<?> elType = parameterTypes[x];
103+
//for (int x = 0; x < paramCount; x++) {
104+
for (Parameter el : method.getParameters()) {
105+
Class<?> elType = el.getType();
94106

95107
if (elType == Environment.class || elType == TraceInfo.class) {
96108
k++;
97109
continue;
98110
}
99111

100112
ParameterEntity param = new ParameterEntity(context);
101-
param.setName(/*el.isNamePresent() ? el.getName() : */"arg" + i); // Fix for Android!
113+
param.setName(el.isNamePresent() ? el.getName() : "arg" + i);
102114
param.setTrace(TraceInfo.UNKNOWN);
103115

104116
Annotation[] argAnnotations = annotations[k];
@@ -379,19 +391,9 @@ public void setParameters(ParameterEntity[] parameters) {
379391
k++;
380392
} else {
381393
if (parameterType == Environment.class) {
382-
argumentOperations[i] = new InjectMemoryOperation() {
383-
@Override
384-
public Object convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
385-
return env;
386-
}
387-
};
394+
argumentOperations[i] = INJECT_ENV_MEMORY_OPERATION;
388395
} else if (parameterType == TraceInfo.class) {
389-
argumentOperations[i] = new InjectMemoryOperation() {
390-
@Override
391-
public Object convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
392-
return trace;
393-
}
394-
};
396+
argumentOperations[i] = INJECT_TRACE_MEMORY_OPERATION;
395397
} else {
396398
if (unknownTypeFetcher != null) {
397399
op = unknownTypeFetcher.call(parameterType, genericTypes);

0 commit comments

Comments
 (0)