Skip to content

Commit

Permalink
varinteger
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyProgrammist committed Jan 12, 2024
1 parent 8d78029 commit 1546ee5
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 136 deletions.
7 changes: 7 additions & 0 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ export type TLBAddressType = {
addrType: "Internal" | "External" | "Any"
};

export type TLBVarIntegerType = {
kind: "TLBVarIntegerType";
signed: boolean;
n: TLBMathExpr;
}

export type TLBCellType = {
kind: "TLBCellType";
};
Expand Down Expand Up @@ -130,6 +136,7 @@ export type TLBFieldType =
| TLBBoolType
| TLBCoinsType
| TLBAddressType
| TLBVarIntegerType
| TLBCellType
| TLBMathExprType
| TLBNegatedType
Expand Down
2 changes: 2 additions & 0 deletions src/astbuilder/fill_constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,8 @@ function checkAndRemovePrimitives(
typesToDelete.set("Grams", ["2f73160b"])
typesToDelete.set("MsgAddressExt", ["44163e94", "2e933043"])
typesToDelete.set("MsgAddress", ["606aa05e", "21d0382b"])
typesToDelete.set("VarUInteger", ["11d56c2e"])
typesToDelete.set("VarInteger", ["d466ed5"])

typesToDelete.forEach((opCodesExpected: string[], typeName: string) => {
let typeItems = typeDeclarations.get(typeName);
Expand Down
29 changes: 27 additions & 2 deletions src/astbuilder/handle_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,31 @@ export function getType(
constructor
),
};
} else if (
expr.name == "VarUInteger" &&
(expr.args[0] instanceof MathExpr ||
expr.args[0] instanceof NumberExpr ||
expr.args[0] instanceof NameExpr)
) {
return {
kind: "TLBVarIntegerType",
n: new TLBUnaryOp(
new TLBBinaryOp(convertToMathExpr(expr.args[0]), new TLBNumberExpr(1), '-'),
"."
),
signed: false,
};
} else if (
expr.name == "VarInteger" &&
(expr.args[0] instanceof MathExpr ||
expr.args[0] instanceof NumberExpr ||
expr.args[0] instanceof NameExpr)
) {
return {
kind: "TLBVarIntegerType",
n: convertToMathExpr(expr.args[0]),
signed: true,
};
} else {
let argumentTypes: TLBFieldType[] = [];
expr.args.forEach((arg) => {
Expand Down Expand Up @@ -224,9 +249,9 @@ export function getType(
} else if (expr.name == "MsgAddress") {
return { kind: "TLBAddressType", addrType: "Any" };
} else if (expr.name == "Bit") {
return { kind: "TLBBitsType", bits: new TLBNumberExpr(1) }
return { kind: "TLBBitsType", bits: new TLBNumberExpr(1) };
} else if (expr.name == "Grams") {
return { kind: "TLBCoinsType"}
return { kind: "TLBCoinsType" };
} else {
if (constructor.variablesMap.get(expr.name)?.type == "#") {
return {
Expand Down
12 changes: 12 additions & 0 deletions src/generators/typescript/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,18 @@ export class TypescriptGenerator implements CodeGenerator {
fieldLoadSuffix: "Coins",
fieldStoreSuffix: "Coins",
};
} else if (fieldType.kind == "TLBVarIntegerType") {
exprForParam = {
argLoadExpr: convertToAST(fieldType.n, ctx.constructor),
argStoreExpr: convertToAST(
fieldType.n,
ctx.constructor,
id(ctx.name)
),
paramType: "bigint",
fieldLoadSuffix: fieldType.signed ? "VarIntBig" : "VarUintBig",
fieldStoreSuffix: fieldType.signed ? "VarInt" : "VarUint"
}
} else if (fieldType.kind == "TLBAddressType") {
if (fieldType.addrType == "Internal") {
exprForParam = {
Expand Down
126 changes: 31 additions & 95 deletions test/generated_files/generated_block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,28 +261,14 @@ export interface Anycast {
readonly rewrite_pfx: BitString;
}

export interface VarUInteger {
readonly kind: 'VarUInteger';
readonly n: number;
readonly len: number;
readonly value: bigint;
}

export interface VarInteger {
readonly kind: 'VarInteger';
readonly n: number;
readonly len: number;
readonly value: bigint;
}

export interface Coins {
readonly kind: 'Coins';
readonly grams: bigint;
}

export interface ExtraCurrencyCollection {
readonly kind: 'ExtraCurrencyCollection';
readonly dict: HashmapE<VarUInteger>;
readonly dict: HashmapE<bigint>;
}

export interface CurrencyCollection {
Expand Down Expand Up @@ -584,15 +570,15 @@ export interface OutMsgQueueInfo {

export interface StorageUsed {
readonly kind: 'StorageUsed';
readonly _cells: VarUInteger;
readonly bits: VarUInteger;
readonly public_cells: VarUInteger;
readonly _cells: bigint;
readonly bits: bigint;
readonly public_cells: bigint;
}

export interface StorageUsedShort {
readonly kind: 'StorageUsedShort';
readonly _cells: VarUInteger;
readonly bits: VarUInteger;
readonly _cells: bigint;
readonly bits: bigint;
}

export interface StorageInfo {
Expand Down Expand Up @@ -764,9 +750,9 @@ export interface TrComputePhase_tr_phase_compute_vm {
readonly msg_state_used: boolean;
readonly account_activated: boolean;
readonly gas_fees: bigint;
readonly gas_used: VarUInteger;
readonly gas_limit: VarUInteger;
readonly gas_credit: Maybe<VarUInteger>;
readonly gas_used: bigint;
readonly gas_limit: bigint;
readonly gas_credit: Maybe<bigint>;
readonly mode: number;
readonly exit_code: number;
readonly exit_arg: Maybe<number>;
Expand Down Expand Up @@ -3545,56 +3531,6 @@ export function storeAnycast(anycast: Anycast): (builder: Builder) => void {

}

/*
var_uint$_ {n:#} len:(#< n) value:(uint (len * 8))
= VarUInteger n;
*/

export function loadVarUInteger(slice: Slice, n: number): VarUInteger {
let len: number = slice.loadUint(bitLen((n - 1)));
let value: bigint = slice.loadUintBig((len * 8));
return {
kind: 'VarUInteger',
n: n,
len: len,
value: value,
}

}

export function storeVarUInteger(varUInteger: VarUInteger): (builder: Builder) => void {
return ((builder: Builder) => {
builder.storeUint(varUInteger.len, bitLen((varUInteger.n - 1)));
builder.storeUint(varUInteger.value, (varUInteger.len * 8));
})

}

/*
var_int$_ {n:#} len:(#< n) value:(int (len * 8))
= VarInteger n;
*/

export function loadVarInteger(slice: Slice, n: number): VarInteger {
let len: number = slice.loadUint(bitLen((n - 1)));
let value: bigint = slice.loadIntBig((len * 8));
return {
kind: 'VarInteger',
n: n,
len: len,
value: value,
}

}

export function storeVarInteger(varInteger: VarInteger): (builder: Builder) => void {
return ((builder: Builder) => {
builder.storeUint(varInteger.len, bitLen((varInteger.n - 1)));
builder.storeInt(varInteger.value, (varInteger.len * 8));
})

}

// _ grams:Grams = Coins;

export function loadCoins(slice: Slice): Coins {
Expand All @@ -3619,8 +3555,8 @@ extra_currencies$_ dict:(HashmapE 32 (VarUInteger 32))
*/

export function loadExtraCurrencyCollection(slice: Slice): ExtraCurrencyCollection {
let dict: HashmapE<VarUInteger> = loadHashmapE<VarUInteger>(slice, 32, ((slice: Slice) => {
return loadVarUInteger(slice, 32)
let dict: HashmapE<bigint> = loadHashmapE<bigint>(slice, 32, ((slice: Slice) => {
return slice.loadVarUintBig(bitLen((32 - 1)))

}));
return {
Expand All @@ -3632,9 +3568,9 @@ export function loadExtraCurrencyCollection(slice: Slice): ExtraCurrencyCollecti

export function storeExtraCurrencyCollection(extraCurrencyCollection: ExtraCurrencyCollection): (builder: Builder) => void {
return ((builder: Builder) => {
storeHashmapE<VarUInteger>(extraCurrencyCollection.dict, ((arg: VarUInteger) => {
storeHashmapE<bigint>(extraCurrencyCollection.dict, ((arg: bigint) => {
return ((builder: Builder) => {
storeVarUInteger(arg)(builder);
builder.storeVarUint(arg, bitLen((32 - 1)));
})

}))(builder);
Expand Down Expand Up @@ -5064,9 +5000,9 @@ storage_used$_ cells:(VarUInteger 7) bits:(VarUInteger 7)
*/

export function loadStorageUsed(slice: Slice): StorageUsed {
let _cells: VarUInteger = loadVarUInteger(slice, 7);
let bits: VarUInteger = loadVarUInteger(slice, 7);
let public_cells: VarUInteger = loadVarUInteger(slice, 7);
let _cells: bigint = slice.loadVarUintBig(bitLen((7 - 1)));
let bits: bigint = slice.loadVarUintBig(bitLen((7 - 1)));
let public_cells: bigint = slice.loadVarUintBig(bitLen((7 - 1)));
return {
kind: 'StorageUsed',
_cells: _cells,
Expand All @@ -5078,9 +5014,9 @@ export function loadStorageUsed(slice: Slice): StorageUsed {

export function storeStorageUsed(storageUsed: StorageUsed): (builder: Builder) => void {
return ((builder: Builder) => {
storeVarUInteger(storageUsed._cells)(builder);
storeVarUInteger(storageUsed.bits)(builder);
storeVarUInteger(storageUsed.public_cells)(builder);
builder.storeVarUint(storageUsed._cells, bitLen((7 - 1)));
builder.storeVarUint(storageUsed.bits, bitLen((7 - 1)));
builder.storeVarUint(storageUsed.public_cells, bitLen((7 - 1)));
})

}
Expand All @@ -5091,8 +5027,8 @@ storage_used_short$_ cells:(VarUInteger 7)
*/

export function loadStorageUsedShort(slice: Slice): StorageUsedShort {
let _cells: VarUInteger = loadVarUInteger(slice, 7);
let bits: VarUInteger = loadVarUInteger(slice, 7);
let _cells: bigint = slice.loadVarUintBig(bitLen((7 - 1)));
let bits: bigint = slice.loadVarUintBig(bitLen((7 - 1)));
return {
kind: 'StorageUsedShort',
_cells: _cells,
Expand All @@ -5103,8 +5039,8 @@ export function loadStorageUsedShort(slice: Slice): StorageUsedShort {

export function storeStorageUsedShort(storageUsedShort: StorageUsedShort): (builder: Builder) => void {
return ((builder: Builder) => {
storeVarUInteger(storageUsedShort._cells)(builder);
storeVarUInteger(storageUsedShort.bits)(builder);
builder.storeVarUint(storageUsedShort._cells, bitLen((7 - 1)));
builder.storeVarUint(storageUsedShort.bits, bitLen((7 - 1)));
})

}
Expand Down Expand Up @@ -5858,10 +5794,10 @@ export function loadTrComputePhase(slice: Slice): TrComputePhase {
let account_activated: boolean = slice.loadBoolean();
let gas_fees: bigint = slice.loadCoins();
let slice1 = slice.loadRef().beginParse();
let gas_used: VarUInteger = loadVarUInteger(slice1, 7);
let gas_limit: VarUInteger = loadVarUInteger(slice1, 7);
let gas_credit: Maybe<VarUInteger> = loadMaybe<VarUInteger>(slice1, ((slice: Slice) => {
return loadVarUInteger(slice, 3)
let gas_used: bigint = slice1.loadVarUintBig(bitLen((7 - 1)));
let gas_limit: bigint = slice1.loadVarUintBig(bitLen((7 - 1)));
let gas_credit: Maybe<bigint> = loadMaybe<bigint>(slice1, ((slice: Slice) => {
return slice1.loadVarUintBig(bitLen((3 - 1)))

}));
let mode: number = slice1.loadInt(8);
Expand Down Expand Up @@ -5910,11 +5846,11 @@ export function storeTrComputePhase(trComputePhase: TrComputePhase): (builder: B
builder.storeBit(trComputePhase.account_activated);
builder.storeCoins(trComputePhase.gas_fees);
let cell1 = beginCell();
storeVarUInteger(trComputePhase.gas_used)(cell1);
storeVarUInteger(trComputePhase.gas_limit)(cell1);
storeMaybe<VarUInteger>(trComputePhase.gas_credit, ((arg: VarUInteger) => {
cell1.storeVarUint(trComputePhase.gas_used, bitLen((7 - 1)));
cell1.storeVarUint(trComputePhase.gas_limit, bitLen((7 - 1)));
storeMaybe<bigint>(trComputePhase.gas_credit, ((arg: bigint) => {
return ((builder: Builder) => {
storeVarUInteger(arg)(builder);
builder.storeVarUint(arg, bitLen((3 - 1)));
})

}))(cell1);
Expand Down
Loading

0 comments on commit 1546ee5

Please sign in to comment.