Commit 9e67b1bb authored by Olof-Joachim Frahm's avatar Olof-Joachim Frahm
Browse files

Fix printing of RANDOM-STATE.

Not a particularly nice byte vector representation though.
parent ad222ccc
......@@ -68,6 +68,29 @@ public final class RandomState extends LispObject
}
}
public RandomState(SimpleVector v)
{
int length = v.capacity;
byte[] bytes = new byte[length];
for (int i = 0; i < length; ++i) {
LispObject obj = v.data[i];
if (obj instanceof Fixnum) {
bytes[i] = (byte)((Fixnum)obj).value;
} else {
error(type_error(obj, Symbol.FIXNUM));
}
}
try {
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
ObjectInputStream in = new ObjectInputStream(byteIn);
random = (Random) in.readObject();
in.close();
}
catch (Throwable t) { // ANY exception gets converted to a lisp error
error(new LispError("Unable to read random state."));
}
}
@Override
public LispObject typeOf()
{
......@@ -93,7 +116,38 @@ public final class RandomState extends LispObject
@Override
public String printObject()
{
return unreadableString("RANDOM-STATE");
LispThread thread = LispThread.currentThread();
StringBuilder sb = new StringBuilder();
if (Symbol.PRINT_READABLY.symbolValue(thread) != NIL) {
// we need the #. reader macro for now
if (Symbol.READ_EVAL.symbolValue(thread) == NIL) {
error(new PrintNotReadable(list(Keyword.OBJECT, this)));
// Not reached.
return null;
}
int base = Fixnum.getValue(Symbol.PRINT_BASE.symbolValue(thread));
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
try {
sb.append("#.(SYSTEM::READ-RANDOM-STATE #(");
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(random);
out.close();
}
catch (Throwable t) { // ANY exception gets converted to a lisp error
error(new LispError("Unable to copy random state."));
}
byte[] bytes = byteOut.toByteArray();
for (int i = 0; i < bytes.length; ++i) {
if (i != 0) {
sb.append(" ");
}
sb.append(Integer.toString(bytes[i], base).toUpperCase());
}
sb.append("))");
} else {
return unreadableString("RANDOM-STATE");
}
return sb.toString();
}
public LispObject random(LispObject arg)
......@@ -176,6 +230,19 @@ public final class RandomState extends LispObject
}
};
private static final Primitive READ_RANDOM_STATE =
new Primitive(Symbol.READ_RANDOM_STATE, "state")
{
@Override
public LispObject execute(LispObject arg)
{
if (arg instanceof SimpleVector)
return new RandomState((SimpleVector)arg);
return type_error(arg, Symbol.RANDOM_STATE);
}
};
// ### random-state-p
private static final Primitive RANDOM_STATE_P =
new Primitive(Symbol.RANDOM_STATE_P, "object")
......
......@@ -3237,6 +3237,8 @@ public class Symbol extends LispObject implements java.io.Serializable
PACKAGE_SYS.addInternalSymbol("READERS");
public static final Symbol REQUIRED_ARGS =
PACKAGE_SYS.addInternalSymbol("REQUIRED-ARGS");
public static final Symbol READ_RANDOM_STATE =
PACKAGE_SYS.addInternalSymbol("READ-RANDOM-STATE");
// DEPRECATED: to be removed with abcl-1.7
public static final Symbol _SOURCE =
PACKAGE_SYS.addInternalSymbol("%SOURCE");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment