Skip to content

Commit

Permalink
More CAD3 encoding updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Oct 12, 2024
1 parent 9219595 commit 2a18c06
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 54 deletions.
6 changes: 3 additions & 3 deletions convex-core/src/main/java/convex/core/data/ACell.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ protected static boolean genericEquals(ACell a, ACell b) {
* @param pos The offset into the byte array
* @return New position after writing
*/
protected abstract int encodeRaw(byte[] bs, int pos);
public abstract int encodeRaw(byte[] bs, int pos);

/**
* Creates the encoding for this cell. Cell must be canonical, or else an error may occur.
Expand Down Expand Up @@ -399,11 +399,11 @@ protected <R extends ACell> Ref<R> createRef() {

/**
* Gets the number of Refs contained within this Cell. This number is
* final / immutable for any given instance and is defined by the Cell encoding rules.
* final / immutable for any given instance and is defined by the Cell,s CAD3 encoding rules.
*
* Contained Refs may be either external or embedded.
*
* @return The number of Refs in this Cell
* @return The number of Refs in this Cell (0-63)
*/
public int getRefCount() {
ACell canonical=getCanonical();
Expand Down
21 changes: 20 additions & 1 deletion convex-core/src/main/java/convex/core/data/ACode.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
package convex.core.data;

import convex.core.data.impl.ALongBlob;
import convex.core.util.Utils;
import convex.core.exceptions.InvalidDataException;

/**
* Abstract base class for CVM code data types
*/
public abstract class ACode extends ACell {
public abstract class ACode extends ALongBlob {

protected byte tag;

protected ACode(long value) {
super(value);
}

@Override
public void validateCell() throws InvalidDataException {
if ((byte)(this.tag&0xF0)!=Tag.CODE_BASE) {
throw new InvalidDataException("Invalide Code tag: 0x"+Utils.toHexString(tag),this);
}
if (this.value<0) {
throw new InvalidDataException("Negaitive code value",this);
}
}
}
2 changes: 1 addition & 1 deletion convex-core/src/main/java/convex/core/data/ANonCVM.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public int encode(byte[] bs, int pos) {
}

@Override
protected int encodeRaw(byte[] bs, int pos) {
public int encodeRaw(byte[] bs, int pos) {
encoding.slice(1).getBytes(bs, pos);
return pos+encoding.size()-1;
}
Expand Down
13 changes: 9 additions & 4 deletions convex-core/src/main/java/convex/core/data/Format.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@
import convex.core.util.Utils;

/**
* Static utility class for message format encoding
* Static utility class for CAD3 encoding format
*
* "Standards are always out of date. That's what makes them standards." - Alan
* Bennett
* "Standards are always out of date. That's what makes them standards." - Alan Bennett
*/
public class Format {

Expand All @@ -57,7 +56,8 @@ public class Format {
* <ul>
* <li>This is the maximum length that can be VLQ encoded in 2 bytes. This simplifies message encoding and decoding.</li>
* <li>It is big enough to include a 4096-byte Blob</li>
* <li>It is small enough to fit in a UDP message</li>
* <li>It is big enough to include a Record with 63 directly referenced fields</li>
* <li>It is small enough to guarantee fitting in a UDP message</li>
* </ul>
*/
public static final int LIMIT_ENCODING_LENGTH = 0x3FFF;
Expand Down Expand Up @@ -97,6 +97,11 @@ public class Format {
*/
public static final long FULL_EMBEDDED_MEMORY_SIZE = 0L;

/**
* Maximum number of Refs for any single Cell
*/
public static final int MAX_REF_COUNT = 63;

/**
* Gets the length in bytes of VLQ encoding for the given long value
* @param x Long value to encode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public int encode(byte[] bs, int pos) {
}

@Override
protected int encodeRaw(byte[] bs, int pos) {
public int encodeRaw(byte[] bs, int pos) {
// Nothing to encode
return pos;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public boolean equals(ACell a) {
}

@Override
protected int encodeRaw(byte[] bs, int pos) {
public int encodeRaw(byte[] bs, int pos) {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public int encode(byte[] bs, int pos) {
}

@Override
protected int encodeRaw(byte[] bs, int pos) {
public int encodeRaw(byte[] bs, int pos) {
ABlob b=blob();
return b.encodeRaw(bs, pos);
}
Expand Down
14 changes: 14 additions & 0 deletions convex-core/src/main/java/convex/core/lang/ACVMCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package convex.core.lang;

import convex.core.data.ACell;

/**
* Abstract base class for CVM code constructs
*/
public abstract class ACVMCode extends ACell {

@Override public final boolean isCVMValue() {
// CVM code objects are CVM values by definition
return true;
}
}
8 changes: 1 addition & 7 deletions convex-core/src/main/java/convex/core/lang/AFn.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package convex.core.lang;

import convex.core.data.ACell;
import convex.core.data.ACode;
import convex.core.data.IRefFunction;
import convex.core.data.Tag;
import convex.core.data.type.AType;
Expand All @@ -15,7 +14,7 @@
*
* @param <T> Return type of functions.
*/
public abstract class AFn<T extends ACell> extends ACode implements IFn<T> {
public abstract class AFn<T extends ACell> extends ACVMCode implements IFn<T> {

@Override
public abstract Context invoke(Context context, ACell[] args);
Expand Down Expand Up @@ -48,11 +47,6 @@ public boolean supportsArgs(ACell[] args) {
*/
public abstract boolean hasArity(int arity);

@Override
public boolean isCVMValue() {
return true;
}

@Override
public byte getTag() {
return Tag.FN;
Expand Down
7 changes: 1 addition & 6 deletions convex-core/src/main/java/convex/core/lang/AOp.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package convex.core.lang;

import convex.core.data.ACell;
import convex.core.data.ACode;
import convex.core.data.Format;
import convex.core.data.IRefFunction;
import convex.core.data.Tag;
Expand All @@ -20,7 +19,7 @@
*
* @param <T> the type of the operation return value
*/
public abstract class AOp<T extends ACell> extends ACode {
public abstract class AOp<T extends ACell> extends ACVMCode {

/**
* Executes this op with the given context. Must preserve depth unless an
Expand Down Expand Up @@ -51,10 +50,6 @@ public boolean isCanonical() {
public ACell toCanonical() {
return this;
}

@Override public final boolean isCVMValue() {
return true;
}

/**
* Returns the opcode for this op
Expand Down
39 changes: 11 additions & 28 deletions convex-core/src/main/java/convex/core/lang/impl/ADataFn.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,45 @@
import convex.core.data.IRefFunction;
import convex.core.exceptions.InvalidDataException;
import convex.core.lang.AFn;
import convex.core.lang.Context;
import convex.core.lang.RT;

/**
* Abstract base class for data structure lookup functions.
* Abstract base wrapper class for data structure lookup functions.
*
* Not a canonical object, can't exist as CVM value.
* Not a canonical object, essentially a wrapper for a data structure interpreted as a function
*
* @param <T> Type of function return value
*/
public abstract class ADataFn<T extends ACell> extends AFn<T> {

@Override
public int estimatedEncodingSize() {
throw new UnsupportedOperationException();
}

@Override
public Context invoke(Context context, ACell[] args) {
throw new UnsupportedOperationException();
return getCanonical().estimatedEncodingSize();
}

@Override
public AFn<T> updateRefs(IRefFunction func) {
throw new UnsupportedOperationException();
return RT.castFunction(getCanonical().updateRefs(func));
}

@Override
public boolean hasArity(int n) {
throw new UnsupportedOperationException();
return (n==1)||(n==2);
}

@Override
public void validateCell() throws InvalidDataException {
throw new UnsupportedOperationException();
getCanonical().validateCell();;
}

@Override
public int encode(byte[] bs, int pos) {
throw new UnsupportedOperationException();
return getCanonical().encode(bs, pos);
}

@Override
public int encodeRaw(byte[] bs, int pos) {
throw new UnsupportedOperationException();
return getCanonical().encodeRaw(bs, pos);
}

@Override
Expand All @@ -56,22 +51,10 @@ public boolean isCanonical() {
}

@Override
public ACell toCanonical() {
throw new UnsupportedOperationException("Can't make canonical!");
}

@Override
public boolean isCVMValue() {
return false;
}
public abstract ACell toCanonical();

@Override
public byte getTag() {
throw new UnsupportedOperationException();
}

@Override
public int getRefCount() {
throw new UnsupportedOperationException();
return getCanonical().getTag();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,10 @@ public boolean print(BlobBuilder sb,long limit) {
return key.print(sb,limit);
}

@Override
public ACell toCanonical() {
return key.getCanonical();
}


}
5 changes: 5 additions & 0 deletions convex-core/src/main/java/convex/core/lang/impl/MapFn.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public boolean print(BlobBuilder sb,long limit) {
return map.print(sb,limit);
}

@Override
public ACell toCanonical() {
return map.getCanonical();
}

}
5 changes: 5 additions & 0 deletions convex-core/src/main/java/convex/core/lang/impl/SeqFn.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ public boolean print(BlobBuilder sb,long limit) {
return seq.print(sb,limit);
}

@Override
public ACell toCanonical() {
return seq.getCanonical();
}

}
7 changes: 6 additions & 1 deletion convex-core/src/main/java/convex/core/lang/impl/SetFn.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class SetFn<T extends ACell> extends ADataFn<CVMBool> {

private ASet<T> set;

public SetFn(ASet<T> m) {
SetFn(ASet<T> m) {
this.set = m;
}

Expand All @@ -35,4 +35,9 @@ public boolean print(BlobBuilder sb,long limit) {
return set.print(sb,limit);
}

@Override
public ACell toCanonical() {
return set.getCanonical();
}

}
2 changes: 2 additions & 0 deletions convex-core/src/test/java/convex/core/data/ObjectsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,9 @@ private static void doCellStorageTest(ACell a) throws InvalidDataException, IOEx
private static void doRefContainerTests(ACell a) {
if (!a.isCanonical()) return;
int rc=a.getRefCount();

assertTrue(rc>=0);
assertTrue(rc<=Format.MAX_REF_COUNT);
assertEquals(rc,Cells.refCount(a));
if (rc>0) {
long tcount = Refs.totalRefCount(a);
Expand Down

0 comments on commit 2a18c06

Please sign in to comment.