Revision: 13823
http://sourceforge.net/p/foray/code/13823
Author: victormote
Date: 2025-06-06 11:59:19 +0000 (Fri, 06 Jun 2025)
Log Message:
-----------
Fix problems in currentfile() and readstring() related to casting of input streams.
Modified Paths:
--------------
trunk/foray/foray-ps/src/main/java/org/foray/ps/PsSystemDict4a.java
Modified: trunk/foray/foray-ps/src/main/java/org/foray/ps/PsSystemDict4a.java
===================================================================
--- trunk/foray/foray-ps/src/main/java/org/foray/ps/PsSystemDict4a.java 2025-06-06 00:28:54 UTC (rev 13822)
+++ trunk/foray/foray-ps/src/main/java/org/foray/ps/PsSystemDict4a.java 2025-06-06 11:59:19 UTC (rev 13823)
@@ -1793,19 +1793,27 @@
*/
private void currentfile() {
/* The "currentfile" doc says this operator returns "the topmost file object on the execution stack."
- * TODO: This method is therefore possibly wrong in two ways.
- * 1. We understand "file" to mean "stream" which could include a PsString.
- * 2. We are not sure what to do if the topmost item is not a "file" or other stream. Should the stack be
- * traversed until one is found? Should non-file items on the stack be popped and then pushed to the operand
- * stack? */
- final PsObject psObject = getInterpreter().getExecutionStack().peek();
- if (psObject instanceof PsInputStream) {
- final PsInputStream psInputStream = (PsInputStream) psObject;
- pushOperand(psInputStream);
- } else {
- /* Per "currentfile" doc, this never occurs during execution of ordinary user programs. */
- pushOperand(new PsString(""));
+ * We have made two assumptions related to this.
+ * 1. We understand this to mean that there may be other things on the execution stack (such as procedures) that
+ * should be ignored.
+ * In other words, the stack needs to be traversed until a "file" is found.
+ * We are pretty confident that this understanding is correct, practice showing that there are sometimes
+ * procedures at the top of the stack when "currentfile" is called.
+ * Since "currentfile" is frequently followed by "readstring" and a procedure could not be read from a
+ * procedure for "readsting" purposes, we conclude that traversing the stack is correct.
+ * 2. We understand "file" to mean "stream" which could include a PsString.
+ * We are much less confident that this is correct, but have found no practical evidence either way .*/
+ final PsExecutionStack executionStack = getInterpreter().getExecutionStack();
+ for (int index = 0; index < executionStack.size(); index ++) {
+ final PsObject psObject = executionStack.peek(index);
+ if (psObject instanceof PsInputStream) {
+ final PsInputStream psInputStream = (PsInputStream) psObject;
+ pushOperand(psInputStream);
+ return;
+ }
}
+ /* Per "currentfile" doc, this never occurs during execution of ordinary user programs. */
+ pushOperand(new PsString(""));
}
/**
@@ -1920,17 +1928,17 @@
@SuppressWarnings("resource")
final PsString string = (PsString) object;
object = popOperand(PsOperator.READSTRING);
- if (! (object instanceof PsFile)) {
+ if (! (object instanceof PsInputStream)) {
throw new PsOperatorException(PsError.TYPECHECK, PsOperator.READSTRING);
}
@SuppressWarnings("resource")
- final PsFile file = (PsFile) object;
+ final PsInputStream file = (PsInputStream) object;
final ByteArrayBuilder builder = new ByteArrayBuilder(string.getValue().length());
builder.append(string.getValue());
int charsRead = 0;
- for (int i = 0; i < string.getValue().length() && file.isOpen(); i++) {
+ for (int i = 0; i < string.getValue().length(); i++) {
try {
builder.setByteAt(i, (byte) file.read());
} catch (final IOException e) {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|