Skip to content

Commit

Permalink
fix(molecule): incorrect fallback Bytes codec
Browse files Browse the repository at this point in the history
  • Loading branch information
homura committed Aug 5, 2024
1 parent 241c244 commit 42ed757
Show file tree
Hide file tree
Showing 9 changed files with 1,221 additions and 6 deletions.
4 changes: 3 additions & 1 deletion packages/molecule/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
},
"devDependencies": {
"@ckb-lumos/base": "0.24.0-next.1",
"jsbi": "^4.1.0"
"jsbi": "^4.1.0",
"js-yaml": "^4.1.0",
"@types/js-yaml": "^4.0.9"
}
}
7 changes: 2 additions & 5 deletions packages/molecule/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,9 @@ export function codegen(schema: string, options: Options = {}): CodegenResult {
import { bytes, createBytesCodec, createFixedBytesCodec, molecule } from "@ckb-lumos/codec";
${options.prepend || ""}
const { array, vector, union, option, struct, table } = molecule;
const { array, vector, union, option, struct, table, byteVecOf } = molecule;
const fallbackBytesCodec = createBytesCodec({
pack: bytes.bytify,
unpack: bytes.hexify,
});
const fallbackBytesCodec = byteVecOf({ pack: bytes.bytify, unpack: bytes.hexify });
function createFallbackFixedBytesCodec(byteLength: number) {
return createFixedBytesCodec({
Expand Down
5 changes: 5 additions & 0 deletions packages/molecule/tests/codegen/customization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { molecule } from "@ckb-lumos/codec";

// the compiler does not support empty block declaration
// so the Table0 is manually declared
export const Table0 = molecule.table({}, []);
336 changes: 336 additions & 0 deletions packages/molecule/tests/codegen/generated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,336 @@
// This file is generated by @ckb-lumos/molecule, please do not modify it manually.
/* eslint-disable */
import { bytes, createBytesCodec, createFixedBytesCodec, molecule } from "@ckb-lumos/codec";
import { Table0 } from './customization'

const { array, vector, union, option, struct, table, byteVecOf } = molecule;

const fallbackBytesCodec = byteVecOf({ pack: bytes.bytify, unpack: bytes.hexify });

function createFallbackFixedBytesCodec(byteLength: number) {
return createFixedBytesCodec({
pack: bytes.bytify,
unpack: bytes.hexify,
byteLength,
});
}

const byte = createFallbackFixedBytesCodec(1);

export const Byte2 = createFallbackFixedBytesCodec(2);

export const Byte3 = createFallbackFixedBytesCodec(3);

export const Byte4 = createFallbackFixedBytesCodec(4);

export const Byte5 = createFallbackFixedBytesCodec(5);

export const Byte6 = createFallbackFixedBytesCodec(6);

export const Byte7 = createFallbackFixedBytesCodec(7);

export const Byte8 = createFallbackFixedBytesCodec(8);

export const Byte9 = createFallbackFixedBytesCodec(9);

export const Byte10 = createFallbackFixedBytesCodec(10);

export const Byte11 = createFallbackFixedBytesCodec(11);

export const Byte12 = createFallbackFixedBytesCodec(12);

export const Byte13 = createFallbackFixedBytesCodec(13);

export const Byte14 = createFallbackFixedBytesCodec(14);

export const Byte15 = createFallbackFixedBytesCodec(15);

export const Byte16 = createFallbackFixedBytesCodec(16);

export const Word = createFallbackFixedBytesCodec(2);

export const Word2 = array(Word, 2);

export const Word3 = array(Word, 3);

export const Word4 = array(Word, 4);

export const Word5 = array(Word, 5);

export const Word6 = array(Word, 6);

export const Word7 = array(Word, 7);

export const Word8 = array(Word, 8);

export const Byte3x3 = array(Byte3, 3);

export const Byte5x3 = array(Byte5, 3);

export const Byte7x3 = array(Byte7, 3);

export const Byte9x3 = array(Byte9, 3);

export const StructA = struct({
f1: byte,
f2: byte,
f3: Byte2,
f4: Byte2
}, ['f1', 'f2', 'f3', 'f4']);

export const StructB = struct({
f1: byte,
f2: byte,
f3: Byte2,
f4: Byte3
}, ['f1', 'f2', 'f3', 'f4']);

export const StructC = struct({
f1: byte,
f2: byte,
f3: Byte2,
f4: Byte4
}, ['f1', 'f2', 'f3', 'f4']);

export const StructD = struct({
f1: byte,
f2: byte,
f3: Byte2,
f4: Byte5
}, ['f1', 'f2', 'f3', 'f4']);

export const StructE = struct({
f1: byte,
f2: Byte2,
f3: byte,
f4: Byte2
}, ['f1', 'f2', 'f3', 'f4']);

export const StructF = struct({
f1: byte,
f2: Byte3,
f3: byte
}, ['f1', 'f2', 'f3']);

export const StructG = struct({
f1: Byte3,
f2: byte,
f3: Byte2,
f4: Word2
}, ['f1', 'f2', 'f3', 'f4']);

export const StructH = struct({
f1: Byte3,
f2: byte,
f3: Byte2,
f4: Byte4
}, ['f1', 'f2', 'f3', 'f4']);

export const StructI = struct({
f1: Byte3,
f2: byte
}, ['f1', 'f2']);

export const StructJ = struct({
f1: Byte6,
f2: byte
}, ['f1', 'f2']);

export const StructIx3 = array(StructI, 3);

export const StructO = struct({
f1: StructIx3,
f2: byte
}, ['f1', 'f2']);

export const StructP = struct({
f1: StructJ,
f2: byte
}, ['f1', 'f2']);

export const Bytes = fallbackBytesCodec;

export const Words = vector(Word);

export const Byte3Vec = vector(Byte3);

export const Byte7Vec = vector(Byte7);

export const StructIVec = vector(StructI);

export const StructJVec = vector(StructJ);

export const StructPVec = vector(StructP);

export const BytesVec = vector(Bytes);

export const WordsVec = vector(Words);

export const Table1 = table({
f1: byte
}, ['f1']);

export const Table2 = table({
f1: byte,
f2: Word2
}, ['f1', 'f2']);

export const Table3 = table({
f1: byte,
f2: Word2,
f3: StructA
}, ['f1', 'f2', 'f3']);

export const Table4 = table({
f1: byte,
f2: Word2,
f3: StructA,
f4: Bytes
}, ['f1', 'f2', 'f3', 'f4']);

export const Table5 = table({
f1: byte,
f2: Word2,
f3: StructA,
f4: Bytes,
f5: BytesVec
}, ['f1', 'f2', 'f3', 'f4', 'f5']);

export const Table6 = table({
f1: byte,
f2: Word2,
f3: StructA,
f4: Bytes,
f5: BytesVec,
f6: Table5
}, ['f1', 'f2', 'f3', 'f4', 'f5', 'f6']);

export const ByteOpt = option(byte);

export const WordOpt = option(Word);

export const StructAOpt = option(StructA);

export const StructPOpt = option(StructP);

export const BytesOpt = option(Bytes);

export const WordsOpt = option(Words);

export const BytesVecOpt = option(BytesVec);

export const WordsVecOpt = option(WordsVec);

export const Table0Opt = option(Table0);

export const Table6Opt = option(Table6);

export const Table6OptOpt = option(Table6Opt);

export const ByteOptVec = vector(ByteOpt);

export const WordOptVec = vector(WordOpt);

export const WordsOptVec = vector(WordsOpt);

export const BytesOptVec = vector(BytesOpt);

export const UnionA = union({
byte,
Word,
StructA,
Bytes,
Words,
Table0,
Table6,
Table6Opt
}, ['byte', 'Word', 'StructA', 'Bytes', 'Words', 'Table0', 'Table6', 'Table6Opt']);

export const TableA = table({
f1: Word2,
f2: StructA,
f3: Bytes,
f4: BytesVec,
f5: Table1,
f6: BytesOpt,
f7: UnionA,
f8: byte
}, ['f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8']);

export const AllInOne = table({
f0: byte,
f1: Byte2,
f2: Byte3,
f3: Byte4,
f4: Byte5,
f5: Byte6,
f6: Byte7,
f7: Byte8,
f8: Byte9,
f9: Byte10,
f10: Byte11,
f11: Byte12,
f12: Byte13,
f13: Byte14,
f14: Byte15,
f15: Byte16,
f16: Word,
f17: Word2,
f18: Word3,
f19: Word4,
f20: Word5,
f21: Word6,
f22: Word7,
f23: Word8,
f24: Byte3x3,
f25: Byte5x3,
f26: Byte7x3,
f27: Byte9x3,
f28: StructA,
f29: StructB,
f30: StructC,
f31: StructD,
f32: StructE,
f33: StructF,
f34: StructG,
f35: StructH,
f36: StructI,
f37: StructJ,
f38: StructIx3,
f39: StructO,
f40: StructP,
f41: Bytes,
f42: Words,
f43: Byte3Vec,
f44: Byte7Vec,
f45: StructIVec,
f46: StructJVec,
f47: StructPVec,
f48: BytesVec,
f49: WordsVec,
f50: Table0,
f51: Table1,
f52: Table2,
f53: Table3,
f54: Table4,
f55: Table5,
f56: Table6,
f57: ByteOpt,
f58: WordOpt,
f59: StructAOpt,
f60: StructPOpt,
f61: BytesOpt,
f62: WordsOpt,
f63: BytesVecOpt,
f64: WordsVecOpt,
f65: Table0Opt,
f66: Table6Opt,
f67: Table6OptOpt,
f68: ByteOptVec,
f69: WordOptVec,
f70: WordsOptVec,
f71: BytesOptVec,
f72: UnionA,
f73: TableA
}, ['f0', 'f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'f10', 'f11', 'f12', 'f13', 'f14', 'f15', 'f16', 'f17', 'f18', 'f19', 'f20', 'f21', 'f22', 'f23', 'f24', 'f25', 'f26', 'f27', 'f28', 'f29', 'f30', 'f31', 'f32', 'f33', 'f34', 'f35', 'f36', 'f37', 'f38', 'f39', 'f40', 'f41', 'f42', 'f43', 'f44', 'f45', 'f46', 'f47', 'f48', 'f49', 'f50', 'f51', 'f52', 'f53', 'f54', 'f55', 'f56', 'f57', 'f58', 'f59', 'f60', 'f61', 'f62', 'f63', 'f64', 'f65', 'f66', 'f67', 'f68', 'f69', 'f70', 'f71', 'f72', 'f73']);

4 changes: 4 additions & 0 deletions packages/molecule/tests/codegen/lumos-molecule-codegen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"schemaFile": "types.mol",
"prepend": "import { Table0 } from './customization'"
}
Loading

0 comments on commit 42ed757

Please sign in to comment.