From 27ac2423fdae8e2d569b775448f50a1824b27a5f Mon Sep 17 00:00:00 2001 From: mikera Date: Mon, 2 Dec 2024 13:57:29 +0000 Subject: [PATCH] Make BlockResult work as generic record --- .../java/convex/core/cpos/BlockResult.java | 102 +++++------------- 1 file changed, 27 insertions(+), 75 deletions(-) diff --git a/convex-core/src/main/java/convex/core/cpos/BlockResult.java b/convex-core/src/main/java/convex/core/cpos/BlockResult.java index a5997748f..8c456476d 100644 --- a/convex-core/src/main/java/convex/core/cpos/BlockResult.java +++ b/convex-core/src/main/java/convex/core/cpos/BlockResult.java @@ -2,7 +2,7 @@ import convex.core.ErrorCodes; import convex.core.Result; -import convex.core.cvm.ACVMRecord; +import convex.core.cvm.ARecordGeneric; import convex.core.cvm.CVMTag; import convex.core.cvm.Keywords; import convex.core.cvm.RecordFormat; @@ -12,14 +12,13 @@ import convex.core.data.AVector; import convex.core.data.Blob; import convex.core.data.Cells; -import convex.core.data.Format; import convex.core.data.Hash; -import convex.core.data.IRefFunction; import convex.core.data.Keyword; -import convex.core.data.Ref; import convex.core.data.Vectors; import convex.core.exceptions.BadFormatException; import convex.core.exceptions.InvalidDataException; +import convex.core.lang.RT; +import convex.core.util.ErrorMessages; import convex.core.util.Utils; /** @@ -29,21 +28,23 @@ * either be a valid result or an error. * */ -public class BlockResult extends ACVMRecord { +public class BlockResult extends ARecordGeneric { private State state; private AVector results; private static final Keyword[] BLOCKRESULT_KEYS = new Keyword[] { Keywords.STATE, Keywords.RESULTS}; - private static final RecordFormat FORMAT = RecordFormat.of(BLOCKRESULT_KEYS); - private BlockResult(State state, AVector results) { - super(CVMTag.BLOCK_RESULT,FORMAT.count()); + super(CVMTag.BLOCK_RESULT,FORMAT,Vectors.create(state,results)); this.state = state; this.results = results; } + public BlockResult(AVector values) { + super(CVMTag.BLOCK_RESULT,FORMAT,values); + } + /** * Create a BlockResult * @param state Resulting State @@ -51,12 +52,7 @@ private BlockResult(State state, AVector results) { * @return BlockResult instance */ public static BlockResult create(State state, Result[] results) { - int n=results.length; - Object[] rs=new Object[n]; - for (int i=0; i results) { * @return State after Block is executed */ public State getState() { + if (state==null) state=(State)values.get(0); return state; } @@ -82,6 +79,7 @@ public State getState() { * @return Vector of Results */ public AVector getResults() { + if (results==null) results=RT.ensureVector(values.get(1)); return results; } @@ -100,6 +98,7 @@ public boolean isError(long i) { * @return Result at specified index for the current Block, or null if not available */ public Result getResult(long i) { + AVector results=getResults(); if ((i<0)||(i>=results.count())) return null; return results.get(i); } @@ -121,17 +120,9 @@ public ACell get(Keyword key) { return null; } - @Override - public BlockResult updateRefs(IRefFunction func) { - State newState=(State)state.updateRefs(func); - AVector newResults=results.updateRefs(func); - return create(newState,newResults); - } - @Override public void validateCell() throws InvalidDataException { // TODO Auto-generated method stub - } @Override @@ -142,30 +133,11 @@ public void validate() throws InvalidDataException { long n=results.count(); for (long i=0; i values=Vectors.read(b, pos); + int epos=pos+values.getEncodingLength(); - State newState=Format.read(b,epos); - if (newState==null) throw new BadFormatException("Null state"); - epos+=Cells.getEncodingLength(newState); - - AVector newResults=Format.read(b,epos); - if (newResults==null) throw new BadFormatException("Null results"); - epos+=Cells.getEncodingLength(newResults); + if (values.count()!=BLOCKRESULT_KEYS.length) throw new BadFormatException(ErrorMessages.RECORD_VALUE_NUMBER); - BlockResult result=create(newState,newResults); - result.attachEncoding(b.slice(pos, epos)); + BlockResult result=new BlockResult(values); + result.attachEncoding(b.slice(pos,epos)); return result; } @Override public boolean equals(ACell a) { - if (!(a instanceof BlockResult)) return false; - BlockResult as=(BlockResult)a; - return equals(as); + if (a instanceof BlockResult)return equals((BlockResult)a); + return Cells.equalsGeneric(this,a); } /** @@ -216,26 +182,6 @@ public boolean equals(BlockResult a) { return true; } - @Override - public int getRefCount() { - return state.getRefCount()+results.getRefCount(); - } - - @Override - public Ref getRef(int i) { - int sc=Cells.refCount(state); - if (i newValues) { + if (values==newValues) return this; + return new BlockResult(newValues); + } +