-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support molecule import statement (#648)
- Loading branch information
Showing
20 changed files
with
1,021 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
examples/molecule-codegen-dir/generated/common/basic_types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
examples/molecule-codegen-dir/schemas/common/basic_types.mol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
f2fbef5
There was a problem hiding this comment.
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