|
| 1 | +# WASM wrapper for TON Tolk Language |
| 2 | + |
| 3 | +**Tolk** is a new language for writing smart contracts in TON. Think of Tolk as the "next‑generation FunC". |
| 4 | +Tolk compiler is literally a fork of FunC compiler, introducing familiar syntax similar to TypeScript, |
| 5 | +but leaving all low-level optimizations untouched. |
| 6 | + |
| 7 | +**tolk-js** is a WASM wrapper for Tolk compiler. |
| 8 | +[Blueprint](https://github.com/ton-org/blueprint) uses tolk-js to compile `.tolk` files, |
| 9 | +so if you develop contracts with blueprint, you don't have to install tolk-js directly. |
| 10 | +However, you can use tolk-js without blueprint, it has a simple and straightforward API. |
| 11 | + |
| 12 | +tolk-js works both in Node.js and browser (does not depend on filesystem). |
| 13 | + |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +``` |
| 18 | +yarn add @ton/tolk-js |
| 19 | +// or |
| 20 | +npm install @ton/tolk-js |
| 21 | +``` |
| 22 | + |
| 23 | + |
| 24 | +## CLI mode |
| 25 | + |
| 26 | +Its purpose is to launch a Tolk compiler from command-line, without compiling ton repo from sources, |
| 27 | +without installing apt/homebrew packages, etc. Just run |
| 28 | + |
| 29 | +``` |
| 30 | +npx @ton/tolk-js --output-json out.json contract.tolk |
| 31 | +``` |
| 32 | + |
| 33 | +Output JSON will contain `fiftCode`, `codeBoc64`, `codeHashHex`, and other fields (launch to see). |
| 34 | + |
| 35 | +There are some flags like `--cwd`, `--output-fift`, and others (run `npx @ton/tolk-js --help`). |
| 36 | + |
| 37 | + |
| 38 | +## Usage example |
| 39 | + |
| 40 | +```js |
| 41 | +import {runTolkCompiler, getTolkCompilerVersion} from "@ton/tolk-js" |
| 42 | + |
| 43 | +async function compileMainTolk() { |
| 44 | + // for example, file `main.tolk` is saved nearby |
| 45 | + // fsReadCallback (below) is called for both main.tolk and all its imports |
| 46 | + let result = await runTolkCompiler({ |
| 47 | + entrypointFileName: 'main.tolk', |
| 48 | + fsReadCallback: path => fs.readFileSync(__dirname + '/' + path, 'utf-8') |
| 49 | + }) |
| 50 | + if (result.status === 'error') { |
| 51 | + throw result.message |
| 52 | + } |
| 53 | + |
| 54 | + console.log(result.fiftCode) |
| 55 | + // using result.codeBoc64, you can construct a cell |
| 56 | + let codeCell = Cell.fromBoc(Buffer.from(result.codeBoc64, "base64"))[0] |
| 57 | + // result has several (probably useful) fields, look up TolkResultSuccess |
| 58 | +} |
| 59 | + |
| 60 | +async function showTolkVersion() { |
| 61 | + let version = await getTolkCompilerVersion() |
| 62 | + console.log(`Tolk v${version}`) |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +The only point to pay attention to is `fsReadCallback`. It's called for every `.tolk` file, input or imported, and you should synchronously return file contents. |
| 67 | +tolk-js does not access filesystem itself, it just provides a flexible callback, so you can make it easily work if you have file contents in memory, for example: |
| 68 | +```js |
| 69 | +let sources = { |
| 70 | + 'main.tolk': 'import "utils/math.tolk"', |
| 71 | + 'utils/math.tolk': '...', |
| 72 | +} |
| 73 | + |
| 74 | +runTolkCompiler({ |
| 75 | + entrypointFileName: 'main.tolk', |
| 76 | + fsReadCallback: path => sources[path], |
| 77 | +}) |
| 78 | +``` |
| 79 | + |
| 80 | +The function `runTolkCompiler()` accepts the following properties (look up `TolkCompilerConfig`): |
| 81 | +* `entrypointFileName` — obvious |
| 82 | +* `fsReadCallback` — explained above |
| 83 | +* `optimizationLevel` (default 2) — controls Tolk compiler stack optimizer |
| 84 | +* `withStackComments` (default false) — Fift output will contain comments, if you wish to debug its output |
| 85 | +* `experimentalOptions` (default '') — you can pass experimental compiler options here |
| 86 | + |
| 87 | + |
| 88 | +## Embedded stdlib functions |
| 89 | + |
| 90 | +Tolk standard functions (`beginCell`, `assertEndOfSlice`, and lots of others) are available out of the box *(if you worked with FunC earlier, you had to download stdlib.fc and store in your project; in Tolk, you don't need any additional files)*. |
| 91 | + |
| 92 | +It works, because all stdlib files are embedded into JS, placed near wasm. If you `import "@stdlib/tvm-dicts"` for example, tolk-js will handle it, `fsReadCallback` won't be called. |
| 93 | + |
| 94 | +Note, that folder `tolk-stdlib/` and files within it exist only for IDE purposes. For example, if you use blueprint or tolk-js directly, JetBrains and VS Code plugins locate this folder and auto-complete stdlib functions. |
0 commit comments