Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unboc: standalone CLI utility instead of tact --disasm
Browse files Browse the repository at this point in the history
anton-trunov committed Dec 26, 2024

Unverified

This user has not yet uploaded their public signing key.
1 parent fba0cd8 commit 969194f
Showing 8 changed files with 94 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tact.yml
Original file line number Diff line number Diff line change
@@ -177,7 +177,7 @@ jobs:
if: runner.os != 'Windows'
run: |
tact bin/test/success.tact
tact --disasm bin/test/success_HelloWorld.code.boc
unboc bin/test/success_HelloWorld.code.boc
- name: Test compatibility with tact-template
run: |
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -18,7 +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)
- The `--disasm` CLI flag to expose Tact's TVM disassembler: PR [#1259](https://github.com/tact-lang/tact/pull/1259)
- `unboc`: a standalone CLI utility to expose Tact's TVM disassembler: PR [#1259](https://github.com/tact-lang/tact/pull/1259)

### Changed

21 changes: 3 additions & 18 deletions bin/tact.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
#!/usr/bin/env node
/* eslint-disable @typescript-eslint/no-var-requires */

const pkg = require("../package.json");
// eslint-disable-next-line @typescript-eslint/no-var-requires
const main = require("../dist/node.js");
const meowModule = import("meow");
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { execFileSync } = require("child_process");
const { decompileAll } = require("@tact-lang/opcode");
const { readFileSync } = require("fs");

void meowModule.then(
/** @param meow {import('meow/build/index')} */
(meow) => {
const cli = meow.default(
`
Usage
$ tact [...flags] (--config CONFIG | --disasm BOC-FILE | TACT-FILE)
$ tact [...flags] (--config CONFIG | FILE)
Flags
-c, --config CONFIG Specify path to config file (tact.config.json)
@@ -23,7 +22,6 @@ void meowModule.then(
--with-decompilation Full compilation followed by decompilation of produced binary code
--func Output intermediate FunC code and exit
--check Perform syntax and type checking, then exit
--disasm BOC-FILE Disassemble a BoC (bag of cells) file and output TVM instructions to stdout
-e, --eval EXPRESSION Evaluate a Tact expression and exit
-v, --version Print Tact compiler version and exit
-h, --help Display this text and exit
@@ -64,7 +62,6 @@ void meowModule.then(
type: "string",
isMultiple: true,
},
disasm: { type: "string" },
quiet: { shortFlag: "q", type: "boolean", default: false },
withDecompilation: { type: "boolean", default: false },
func: { type: "boolean", default: false },
@@ -120,18 +117,6 @@ void meowModule.then(
}
}

if (cli.flags.disasm !== undefined) {
try {
const boc = readFileSync(cli.flags.disasm);
const disasmResult = decompileAll({ src: Buffer.from(boc) });
console.log(disasmResult);
process.exit(0);
} catch (error) {
console.error(error.message);
process.exit(30);
}
}

// Disallow specifying both config or Tact source file at the same time
if (cli.flags.config !== undefined && cli.input.length > 0) {
console.log(
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
@@ -120,6 +120,7 @@
"Trunov",
"typechecker",
"uintptr",
"unboc",
"uninit",
"unixfs",
"untypable",
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",
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
3 changes: 2 additions & 1 deletion tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -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 969194f

Please sign in to comment.