Skip to content

Commit

Permalink
feat(cli): unboc utility to expose TVM disassembler via @tact-lang/…
Browse files Browse the repository at this point in the history
…opcode library (#1259)
  • Loading branch information
anton-trunov authored Dec 26, 2024
1 parent 136665e commit 911e7f0
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/tact.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ jobs:
run: |
tact -e '(1 + 2 * (pow(3,4) - 2) << 1 & 0x54 | 33 >> 1) * 2 + 2'
- name: CLI Test | Check TVM disassembler flag
if: runner.os != 'Windows'
run: |
tact bin/test/success.tact
unboc bin/test/success_HelloWorld.code.boc
- name: Test compatibility with tact-template
run: |
git clone https://github.com/tact-lang/tact-template.git
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Utility for logging errors in code that was supposed to be unreachable: PR [#991](https://github.com/tact-lang/tact/pull/991)
- Ability to specify a compile-time message opcode expression: PR [#1188](https://github.com/tact-lang/tact/pull/1188)
- The `VarInt16`, `VarInt32`, `VarUint16`, `VarUint32` integer serialization types: PR [#1186](https://github.com/tact-lang/tact/pull/1186)
- `unboc`: a standalone CLI utility to expose Tact's TVM disassembler: PR [#1259](https://github.com/tact-lang/tact/pull/1259)

### Changed

Expand Down
76 changes: 76 additions & 0 deletions bin/unboc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env node
/* eslint-disable @typescript-eslint/no-var-requires */

const meowModule = import("meow");
const { decompileAll } = require("@tact-lang/opcode");
const { readFileSync } = require("fs");

const unbocVersion = "0.0.1";

void meowModule.then(
/** @param meow {import('meow/build/index')} */
(meow) => {
const cli = meow.default(
`
Usage
$ unboc [...flags] BOC-FILE
Flags
-v, --version Print unboc version and exit
-h, --help Display this text and exit
Examples
$ unboc --version
${unbocVersion}`,
{
importMeta: {
url: new URL("file://" + __dirname + __filename).toString(),
},
description: `Command-line utility to disassemble a BoC (bag of cells) file with code and output TVM instructions to stdout`,
autoVersion: false,
flags: {
version: { shortFlag: "v", type: "boolean" },
help: { shortFlag: "h", type: "boolean" },
},
allowUnknownFlags: false,
},
);

// Show help regardless of other flags
if (cli.flags.help) {
cli.showHelp(0);
}

// Show version regardless of other flags
if (cli.flags.version) {
console.log(unbocVersion);
process.exit(0);
}

// Disallow specifying more than one boc file
if (cli.input.length > 1) {
console.error(
"Error: Only one BoC file can be specified at a time.",
);
cli.showHelp(30);
}

// Show help when all flags and inputs are empty
// Note, that version/help flags are already processed above and don't need to be mentioned here
if (cli.input.length === 0) {
cli.showHelp(0);
}

// Main command
try {
const boc = readFileSync(cli.input.at(0));
const disasmResult = decompileAll({ src: Buffer.from(boc) });
console.log(disasmResult);
process.exit(0);
} catch (error) {
console.error(error.message);
// https://nodejs.org/docs/v20.12.1/api/process.html#exit-codes
process.exit(30);
}
},
);
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"Trunov",
"typechecker",
"uintptr",
"unboc",
"uninit",
"unixfs",
"untypable",
Expand Down
10 changes: 8 additions & 2 deletions knip.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
{
"$schema": "https://unpkg.com/knip@5/schema.json",
"entry": ["src/index.ts", "src/main.ts", "src/node.ts", "bin/tact.js"],
"project": ["src/**/*.ts", "bin/tact.js"],
"entry": [
"src/index.ts",
"src/main.ts",
"src/node.ts",
"bin/tact.js",
"bin/unboc.js"
],
"project": ["src/**/*.ts", "bin/tact.js", "bin/unboc.js"],
"ignore": [
"src/grammar/ast.ts",
"src/prettyPrinter.ts",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
],
"main": "./dist/main.js",
"bin": {
"tact": "bin/tact.js"
"tact": "bin/tact.js",
"unboc": "bin/unboc.js"
},
"dependencies": {
"@tact-lang/opcode": "^0.0.16",
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"examples/",
"scripts/",
"./jest.config.js",
"bin/tact.js"
"bin/tact.js",
"bin/unboc.js"
],
"exclude": ["**/*.bind.ts", "src/test/**/output/**/*"]
}

0 comments on commit 911e7f0

Please sign in to comment.