TypeScript

Embedding Stof in JS/TS.

We've provided a package on JSR to embed Stof in JavaScript. This can be used in the browser or a JavaScript runtime like Deno or Bun.

In these examples, we're using the Deno JavaScript runtime.

Hello, World!

Much like other libraries in the JS world, Stof uses a "parse" function to parse a string into a Stof document, with a given format.

As mentioned above, the normal print function "pln" will not work in this WebAssembly context. However, when the Stof document is created using the provided interface, it adds a few "console" library functions (log, trace, info, error, and warn).

import { Stof } from 'jsr:@formata/stof';

const doc = await Stof.parse('stof', `
    #[main]
    fn hello() {
        console.log('Hello, world!');
    }
`);

doc.run();
> deno run --allow-net example.ts
Hello, world!

Last updated

Was this helpful?