Stof Docs
Star on GitHubStof WebsiteDiscordContact Us
  • 🚀Welcome
  • â„šī¸Resources & Information
  • Book
    • Data Interface
    • Introduction & Design
    • Host Environments
      • Rust
      • TypeScript
        • Extend Stof
        • Import Statement
    • Data Interchange
      • JSON to Stof
      • Importing and Parsing Data
      • Exporting Data
      • REST and Restructure
    • Configuration Files
    • Schemas
      • Renaming Fields
      • Removing Fields
      • Validation & Transformation
        • Example Access
      • Nested Schemas
    • Orchestration
  • Common Concepts
    • Objects
    • Primitive Types
    • Functions
    • Fields
    • Object Types
    • Imports
    • Error Handling
    • Units
    • Casting/Conversions
  • Reference
    • CLI
      • 🏃Run
      • đŸ§ĒTest
      • 📚CLI Libraries
        • Filesystem Library
        • Time Library
        • HTTP Library
    • Stof Language
    • Libraries
      • Standard Library
      • Array/Vector Library
      • Number Library
      • String Library
      • Object Library
      • Function Library
      • Set Library
      • Map Library
      • Data Library
      • Tuple Library
      • Blob Library
      • Boolean Library
    • đŸĒ§Formats
Powered by GitBook
On this page
  • Insert Library
  • Document Context

Was this helpful?

  1. Book
  2. Host Environments
  3. TypeScript

Extend Stof

Extending Stof with custom libraries.

PreviousTypeScriptNextImport Statement

Last updated 3 months ago

Was this helpful?

Stof is extended with . In a TypeScript context, Stof allows you to call your TypeScript functions directly from Stof using this feature.

Insert Library

In this example, we add a single function called "add" in a Stof library named "CustomLibrary". To call this function from Stof, reference the library name and function name just like any other library: CustomLibrary.add(...).

You may build a library with multiple, separate calls to insertLibrary with the same library name. Functions will be added or replaced if the names collide.

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

const doc = await Stof.create();

const magic = 42; // some magic to mix things up
doc.insertLibrary('CustomLibrary', [
    ['add', (a: number, b: number): number => a + b + magic]
]);

doc.importString('stof', `
    #[main]
    fn main() {
        let res = CustomLibrary.add(10, 32);
        console.log(res); // 84
    }
`);
doc.run();
> deno run --allow-all example.ts
84

Document Context

When Stof calls out to TypeScript, it sets the StofDoc as the current context ("this").

In JavaScript, arrow functions do not have a "this" context, so when you define your library functions, do so with the "function" keyword if you intend to work with the document.

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

const doc = await Stof.create();

doc.insertLibrary('Interface', [
    ['insert', function() {
        this.stringImport('stof', `
            fn sayHi() {
                console.log("Was told to say hello...");
            }
        `, 'Interface');
    }]
]);

doc.importString('stof', `
    #[main]
    fn main() {
        Interface.insert();
        Interface.sayHi();
    }
`);
doc.run();
> deno run --allow-all example.ts
Was told to say hello...

The StofDoc is not the same thing as the Stof interface provided by the package. StofDoc is what is held within the Stof interface. Take a look at the for more information and documentation on the StofDoc object.

libraries
JSR package