Skip to content

Commit

Permalink
feat: support molecule import statement (#648)
Browse files Browse the repository at this point in the history
  • Loading branch information
homura authored Mar 11, 2024
1 parent b6a3229 commit f2fbef5
Show file tree
Hide file tree
Showing 20 changed files with 1,021 additions and 42 deletions.
55 changes: 55 additions & 0 deletions examples/molecule-codegen-dir/customized.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { createFixedBytesCodec, number } from "@ckb-lumos/codec";

const { Uint32, Uint64, Uint128 } = number;

/**
* <pre>
* 0b0000000 0
* ───┬─── │
* │ ▼
* │ type - the last bit indicates locating contract(script) via type hash and runs in the latest version of the CKB-VM
* │
* ▼
* data* - the first 7 bits indicate locating contract(script) via code hash and runs in the specified version of the CKB-VM
* </pre>
*
*/
const HashType = createFixedBytesCodec<"data" | "type" | "data1" | "data2">({
byteLength: 1,
// prettier-ignore
pack: (hashType) => {
if (hashType === "type") return new Uint8Array([0b0000000_1]);
if (hashType === "data") return new Uint8Array([0b0000000_0]);
if (hashType === "data1") return new Uint8Array([0b0000001_0]);
if (hashType === "data2") return new Uint8Array([0b0000010_0]);

throw new Error('Unknown hash type')
},
unpack: (byte) => {
if (byte[0] === 0b0000000_1) return "type";
if (byte[0] === 0b0000000_0) return "data";
if (byte[0] === 0b0000001_0) return "data1";
if (byte[0] === 0b0000010_0) return "data2";

throw new Error("Unknown hash type");
},
});

const DepType = createFixedBytesCodec<"code" | "depGroup">({
byteLength: 1,
// prettier-ignore
pack: (depType) => {
if (depType === "code") return new Uint8Array([0]);
if (depType === "depGroup") return new Uint8Array([1]);

throw new Error("Unknown dep type");
},
unpack: (byte) => {
if (byte[0] === 0) return "code";
if (byte[0] === 1) return "depGroup";

throw new Error("Unknown dep type");
},
});

export { Uint32, Uint64, Uint128, DepType, HashType };
29 changes: 29 additions & 0 deletions examples/molecule-codegen-dir/generated/common/basic_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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 { Uint32, Uint64, Uint128, DepType, HashType } from '../../customized'

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

const fallbackBytesCodec = createBytesCodec({
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 AttrValue = createFallbackFixedBytesCodec(1);

export const SkillLevel = createFallbackFixedBytesCodec(1);

export const Uint8 = createFallbackFixedBytesCodec(1);

export const Uint16 = createFallbackFixedBytesCodec(2);
57 changes: 57 additions & 0 deletions examples/molecule-codegen-dir/generated/skills.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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 { Uint32, Uint64, Uint128, DepType, HashType } from '../customized'
import { AttrValue, SkillLevel, Uint8, Uint16 } from './common/basic_types'

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

const fallbackBytesCodec = createBytesCodec({
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 ArmorLight = option(SkillLevel);

export const ArmorHeavy = option(SkillLevel);

export const ArmorShields = option(SkillLevel);

export const WeaponSwords = option(SkillLevel);

export const WeaponBows = option(SkillLevel);

export const WeaponBlunt = option(SkillLevel);

export const Dodge = option(SkillLevel);

export const PickLocks = option(SkillLevel);

export const Mercantile = option(SkillLevel);

export const Survival = option(SkillLevel);

export const Skill = union({
ArmorLight,
ArmorHeavy,
ArmorShields,
WeaponSwords,
WeaponBows,
WeaponBlunt,
Dodge,
PickLocks,
Mercantile,
Survival
}, ['ArmorLight', 'ArmorHeavy', 'ArmorShields', 'WeaponSwords', 'WeaponBows', 'WeaponBlunt', 'Dodge', 'PickLocks', 'Mercantile', 'Survival']);

export const Skills = vector(Skill);
6 changes: 6 additions & 0 deletions examples/molecule-codegen-dir/lumos-molecule-codegen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"objectKeyFormat": "camelcase",
"prepend": "import { Uint32, Uint64, Uint128, DepType, HashType } from './customized'",
"schemaDir": "schemas",
"outDir": "generated"
}
12 changes: 12 additions & 0 deletions examples/molecule-codegen-dir/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@lumos-example/molecule-codegen-dir",
"private": true,
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"@ckb-lumos/codec": "canary"
},
"devDependencies": {
"@ckb-lumos/molecule": "canary"
}
}
26 changes: 26 additions & 0 deletions examples/molecule-codegen-dir/schemas/common/basic_types.mol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// https://github.com/nervosnetwork/molecule/blob/master/docs/schemas/common/basic_types.mol
// AttrValue is an alias of `byte`.
//
// Since Molecule data are strongly-typed, it can gives compile time guarantees
// that the right type of value is supplied to a method.
//
// In this example, we use this alias to define an unsigned integer which
// has an upper limit: 100.
// So it's easy to distinguish between this type and a real `byte`.
// Of course, the serialization wouldn't do any checks for this upper limit
// automatically. You have to implement it by yourself.
//
// **NOTE**:
// - This feature is dependent on the exact implementation.
// In official Rust generated code, we use new type to implement this feature.
array AttrValue [byte; 1];

// SkillLevel is an alias of `byte`, too.
//
// Each skill has only 10 levels, so we use another alias of `byte` to distinguish.
array SkillLevel [byte; 1];

// Define several unsigned integers.
array Uint8 [byte; 1];
array Uint16 [byte; 2];
array Uint32 [byte; 4];
33 changes: 33 additions & 0 deletions examples/molecule-codegen-dir/schemas/skills.mol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// https://github.com/nervosnetwork/molecule/blob/master/docs/schemas/skills.mol
import common/basic_types;

// We define several skills.
// None means the role can learn a skill but he/she doesn't learn it.
option ArmorLight (SkillLevel);
option ArmorHeavy (SkillLevel); // only Fighter can learn this
option ArmorShields (SkillLevel); // only Fighter can learn this
option WeaponSwords (SkillLevel); // only Mage can't learn this
option WeaponBows (SkillLevel); // only Ranger can learn this
option WeaponBlunt (SkillLevel);
option Dodge (SkillLevel);
option PickLocks (SkillLevel);
option Mercantile (SkillLevel);
option Survival (SkillLevel);
// ... omit other skills ...

// Any skill which is defined above.
union Skill {
ArmorLight,
ArmorHeavy,
ArmorShields,
WeaponSwords,
WeaponBows,
WeaponBlunt,
Dodge,
PickLocks,
Mercantile,
Survival,
}

// A hero can learn several skills. The size of learned skills is dynamic.
vector Skills <Skill>;
2 changes: 1 addition & 1 deletion examples/molecule-codegen/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@lumos-example/misc",
"name": "@lumos-example/molecule-codegen",
"private": true,
"version": "1.0.0",
"license": "MIT",
Expand Down
Loading

1 comment on commit f2fbef5

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 New canary release: 0.0.0-canary-f2fbef5-20240311091511

npm install @ckb-lumos/[email protected]

Please sign in to comment.