Janet Programming Language

repository·master·Indexed 26 days ago

https://github.com/janet-lang/janet

A programming language designed for system scripting, expressive automation, and embedding within C/C++ programs. Janet features a robust core library, first-class green threads, and a built-in PEG engine. It provides a balance between the simplicity of Lua and the richness of Python/Guile, with support for FFI, socket networking, and a REPL.

Tokens
3.2K
Snippets
14
Records
18
Agent score
88%

What's inside Janet

  1. Build Janet on macOS and Unix-like systems

    master

    The Makefile requires GNU-flavored make. Follow these steps to build, test, and install:

    cd somewhere/my/projects/janet
    make
    make test
    make repl
    make install

    Optional: To install Spork or JPM from git, use make install-spork-git or make install-jpm-git.

  2. Install Spork and JPM companion tools

    master

    Janet has two companion projects that are optional but useful:

    • Spork: A collection of utility modules and scripts (like janet-format, janet-netrepl, and janet-pm). It requires a C compiler. On POSIX systems, scripts are installed to $JANET_PATH/bin/ by default.
    • JPM: An older, opinionable project manager tool. It is more stable on some systems but less flexible and is not receiving frequent updates. It installs to /usr/local/bin/ on POSIX systems by default.

    You can install them from source using the provided Make targets: make install-spork-git or make install-jpm-git.

  3. Embed Janet in a C project

    master

    Janet can be embedded into host C programs using an amalgamated source file.

    1. Locate Files: Use build/c/janet.c (the amalgamated source), src/include/janet.h, and src/conf/janetconf.h.
    2. Compilation Requirements:
      • Compile with -std=c99.
      • Link with the math library (-lm) and the dynamic linker (-ldl) to support dynamic modules.
    3. Disable Dynamic Modules: If you do not need dynamic modules, add the -DJANET_NO_DYNAMIC_MODULES define to your compiler options.
  4. Install Bash completions for Janet

    master

    You can enable shell completions for the janet interpreter in Bash using one of the following methods:

    Temporary (Current session only)

    Source the script directly in your current terminal session:

    source /path/to/janet.bash

    Permanent (System-wide)

    Copy the script to the system-wide bash-completion directory (requires sudo):

    sudo cp janet.bash /etc/bash_completion.d/janet

    Permanent (User only)

    Copy the script to your local user completions directory:

    cp janet.bash ~/.local/share/bash-completion/completions/janet
    source /path/to/janet.bash
  5. Use the Janet CLI and REPL

    master

    You can interact with Janet via a REPL (Read-Eval-Print Loop) or by running individual scripts.

    • Launch REPL: Run the janet binary with no arguments.
    • Run a script: Use ./janet myscript.janet.
    • Explore bindings: Inside the REPL, run (all-bindings) to print a list of all available macros, functions, and constants.
    $ janet
    janet:1:> (+ 1 2 3)
    6
    janet:2:> (print "Hello, World!")
    Hello, World!
    nil
  6. Access Janet API documentation

    master

    You can access documentation in two ways:

    1. Online: Use the official introduction for tutorials and the core API doc for full function references.
    2. In the REPL: Use the (doc symbol-name) macro to view documentation for a specific symbol. For example, (doc apply) will show the documentation for the apply function. To see all bound symbols in the current environment, use (all-bindings) or call (doc) with no arguments.
    (doc apply)
  7. Build Janet using Meson

    master

    Meson is a cross-platform build system that provides better IDE integration and support for cross-compilation. It requires Python.

    git clone https://github.com/janet-lang/janet.git
    cd janet
    meson setup build \
              --buildtype release \
              --optimization 2 \
              --libdir /usr/local/lib \
              -Dgit_hash=$(git log --pretty=format:'%h' -n 1)
    ninja -C build
    
    # Run the binary
    build/janet
    
    # Installation
    ninja -C build install
  8. Fuzz the Janet unmarshaller using AFL

    master

    To fuzz the unmarshaller, you must have afl and xterm installed. This process requires generating specific unmarshal test cases before running the fuzzer.

    1. Generate the unmarshal test cases using Janet.
    2. Prepare the fuzzing environment.
    3. Set the NFUZZ environment variable to 1.
    4. Run the fuzzer for the unmarshal target.
    5. Once finished (Ctrl+C), aggregate the discovered test cases.
    6. View the aggregated results in ./fuzz_out/unmarshal_aggregated/.
    $ janet ./tools/afl/generate_unmarshal_testcases.janet
    $ sh ./tools/afl/prepare_to_fuzz.sh
    $ export NFUZZ=1
    $ sh ./tools/afl/fuzz.sh unmarshal
    # Ctrl+C to stop
    $ sh ./tools/afl/aggregate_cases.sh unmarshal
    $ ls ./fuzz_out/unmarshal_aggregated/
  9. Fuzz the Janet parser using AFL

    master

    To fuzz the Janet parser, you must have afl and xterm installed. Follow these steps to prepare the environment, run the fuzzer, and aggregate the results. Note that afl may require system configuration; if it quits prematurely, run it manually to diagnose errors.

    1. Prepare the fuzzing environment.
    2. Set the NFUZZ environment variable to 1.
    3. Run the fuzzer for the parser target.
    4. Once finished (Ctrl+C), aggregate the discovered test cases.
    5. View the aggregated results in ./fuzz_out/parser_aggregated/.
    $ sh ./tools/afl/prepare_to_fuzz.sh
    $ export NFUZZ=1
    $ sh ./tools/afl/fuzz.sh parser
    # Ctrl+C to stop
    $ sh ./tools/afl/aggregate_cases.sh parser
    $ ls ./fuzz_out/parser_aggregated/