Beta Release 2611

The past two years of Onyx development has been sporadic and infrequent, but fruitful. This release brings new compilation targets, new integrations, and more stability.

A Worthwhile Wait...

Over the past 2 years, Onyx has undergone a lot of changes and improvements. Every time I finished a major enhancement, it never felt like the right time to release a new version of the language because other aspects of that change were not fully realized. Now, after a lot of iteration, Onyx is more polished and capable than ever before.

Before getting into the changes, the first notable change is the version naming. Instead of continually incrementing a number at the end with no semantic meaning, versions are now named by the year and month they are released, with point releases if multiple versions are released in the same month. This does not mean that there necessarily will be monthly releases.

Onyx is still in beta, but this release is a large step towards stabilization of the first "edition" of Onyx. If Onyx ever becomes big enough to warrant a locked down, critical fixes only release, it will likely be similar to Rust Editions.

Enough preamble, let's talk about what changed this version.

1. Major Refactoring

I won't delve too much into the details here, but the Onyx compiler underwent 2 major refactorings that enabled a lot of new features:

  1. Separate the compiler logic from the command-line interface. You can now use libonyx and onyx.h from any C/C++ project to compile and run an Onyx program from within any other project. This opens the door for embedding Onyx in game-engines, editors, or a plugin system.
  2. Simplify the type-checking phase of the compiler. Type-checking used to be a two-phase process which prohibitted some of the new features discussed below. This was also a huge simplification win.

2. New Language Features

We will get started with new features in the language itself.

For-loops are Macros

For-loops were majorly refactored. Instead of the compiler having a set of known iterable types that it would generate the code for, all for-loops are implicitly converted into a call of __for_expansion with three arguments: the iterable, for-expansion flags, and the body of the code.

// A for-loop like this:
for x, y in iterable {
    // body
}
// Is exactly the same as:
__for_expansion(iterable, .{}, [x, y] {
    // body
})

All of the for-loop types that the compiler supported before are now implemented in the userland standard library. You can see them in core/operations.onyx.

Writing a for-loop for your own custom type is very easy; simply add an overload to the __for_expansion global overload set. You may want to support multiple "shapes" of a for-loop, so you can provide multiple overloads that use a where clause on the number of captures, or the captures' types.

// A custom type we want to be iterable
Pairs_Iter :: struct (T: type_expr) {
    elems: [] T
}

pairs :: (arr: [] $T) => Pair_Iter(T).{arr}

#overload
__for_expansion :: macro (pairs: Pairs_Iter($T), $flags: __For_Expansion_Flags, $body: Code)
    where body.capture_count == 2
{
    elements := pairs.elems
    for i in 0 .. elements.count - 1 {
        for j in i+1 .. elements.count {
            // Use #scope to make sure i, j, and elements are not visible
            #unquote body(elements[i], elements[j]) #scope(body)
        }
    }
}

main :: () {
    arr := .[1, 2, 3]
    for x, y in pairs(arr) {
        printf("({}, {})\n", x, y)
    }
}

// (1, 1)
// (1, 2)
// ...

Improved Optional Usability

When working with optionals, it is very common to want to write: "If there is a value, run this code with the value". Before, the way to do that would be with a switch statement, or Optional.with, but now there is a special syntax on ifs and whiles to extract the value out of an optional if present.

optional_int: ? i32 = 123

if optional_int as value {
    // value is i32
}

Combining this with the <union>.<variant> syntax that turns a union into an optional of a variant, you can now easily write code that works with all union types when you only care about a certain case. In this example, the body of the if only runs when the result was holding the Ok variant.

result: Result(i32, str) = operation()

// result.Ok is ? i32
if result.Ok as value {
    // value is i32
}

This construct also works with while loops, allowing you to create another kind of "iterable" where you repeatedly call a function to get the next value, until it is empty. (much like Zig)

iterable: Iterator(i32) = Iterator.from(1 .. 20)

while Iterator.next(iterable) as value {
    println(value)
}

// 1
// 2
// ...

Polymorphic Return Types

You can have a polymorphic variable introduced in the return-type. This forces the polymorph solver to use the expected type from the call-site to fill in the variable. If there is no expected return type, you will get a compile error.

// Solve R from the call-site
give_123 :: () -> $R {
    return 123
}

give_result :: (v: R) -> Result($R, str) {
    return .{Ok = v}
}

main :: () {
    i123: i32 = give_123()
    f123 := cast(f32) give_123()

    float_result: Result(f32, str) = give_result(123)

    println(i123)
    println(f123)
    println(float_result)
}

// 123
// 123.0000
// Ok(123.0000)

This change also influences overloaded functions. If multiple overload options work, the result type will be used as a tie-breaker. You can combine this with where for some interesting results.

use core {intrinsics, println}

produce_value :: #match {
    () -> str { return "a string" }

    () -> $R where intrinsics.types.type_is_number(R) {
        return 123
    }
}

main :: () {
    println(cast(str) produce_value())
    println(cast(i32) produce_value())
}

// a string
// 123

This feature is used by a new interface called From and a overloaded procedure called From.from. This interface is very similar to Rust's "From" trait. When you write x: i32 = From.from(y), it sees if there is a definition of i32.from that takes the given type.

Package Parameters

The final feature worth mentioning is package parameters. This feature came about from noticing that many times when I'm writing packages, there are some natural options that I want the end user to be able to control, such as printing debug information, or even an injectable hook into the processing. Since this was such a common thing I was noticing, I decided to make it a language feature instead of using slightly hacky #if #defined() everywhere. Here is how #package_parameters work.

// foo.onyx
package foo

// Declare package parameters and their default values.
// If a parameter doesn't have a default value, one must be supplied by the user.
#package_parameters(
    log_level: Log_Level
    event_hook: (Event) -> void = (ev: Event) {}
) {
    // This block is for things to be compiled before the parameters are known.
    // Use this block for types needed by the parameters.
    Log_Level :: enum { Debug; Info; Warning; Error }

    Event :: union {
        Event1: i32
        Event2: str
    }
}


// main.onyx
#load "foo"

use foo

// Supply package parameters to `foo`.
// At most 1 #package_parameters can be used per package.
#package_parameters foo(log_level=.Info, event_hook=main_event_hook)

main_event_hook :: (ev: foo.Event) {
    // ...
}

// Some of the core libraries support parameters too.
#package_parameters core.alloc.heap(Debug = true)

You can also support package parameters from the command-line using --param. You can pass arbitrary strings as expressions, even function bodies, but be reasonable.

onyx run --param 'foo.log_level=.Info' --param 'foo.event_hook=(ev) => {}' main.onyx

3. New Targets and Integrations

Compiler as a Library

The compiler's command-line interface was completely rewritten to split out a linkable, standalone C-library. The libonyx library can be used to integrate Onyx within any existing native project. Here is an example of using the library to compile a simple Onyx program and write the WASM to a file.

#include <stdio.h>
#include <stdlib.h>
#include "onyx.h"

int main(void) {
    // Create a compilation context.
    onyx_context_t* ctx = onyx_context_create();

    // Set some compilation options.
    onyx_set_option_int(ctx, ONYX_OPTION_MULTI_THREADING, 1);
    onyx_set_option_int(ctx, ONYX_OPTION_PLATFORM, ONYX_PLATFORM_ONYX);
    onyx_add_mapped_dir(ctx, "core", -1, "/home/you/.onyx/core", -1);

    // For this example, just inject raw code.
    // You would probably use onyx_add_source().
    onyx_inject_code(ctx,
        "package main\n"
        "use core {println}\n"
        "main :: () {\n"
        "    println(\"Hello, World!\");\n"
        "}",
        -1
    );

    // Pump the compilation until it completes.
    // You can handle some compilation events here like
    // ONYX_EVENT_LOG or ONYX_EVENT_ALL_TYPES_CHECKED
    while (onyx_pump(ctx) == ONYX_PUMP_CONTINUE) {
        // Maybe do something with onyx_event_*
    }

    // Print errors, if any.
    int errors = onyx_error_count(ctx);
    if (errors > 0) {
        for (int i = 0; i < errors; i++) {
            printf("Error at %s:(%d, %d) | %s\n",
                onyx_error_filename(ctx, i),
                onyx_error_line(ctx, i),
                onyx_error_column(ctx, i),
                onyx_error_message(ctx, i)
            );
        }
        return 1;
    }

    // Create and write output file.
    int wasm_file_size = onyx_output_length(ctx, ONYX_OUTPUT_TYPE_WASM);
    char *wasm_buffer = malloc(wasm_file_size);
    onyx_output_write(ctx, ONYX_OUTPUT_TYPE_WASM, wasm_buffer);

    FILE *wasm_file = fopen("hello.wasm", "wb");
    fwrite(wasm_buffer, wasm_file_size, 1, wasm_file);
    fclose(wasm_file);

    free(wasm_buffer);
    onyx_context_free(ctx);
}

Compiling is very easy on GCC and Clang. The header file and library are included in the distribution of the toolchain.

gcc -I$ONYX_PATH/include -L$ONYX_PATH/lib -lonyx program.c

Just-In-Time and Ahead-Of-Time Native Compilation

Onyx's WebAssembly runtime saw a lot of improvements over the past couple months. The interpreter itself is about 40% faster thanks to shrinking internal data structure sizes and changing from a tail-call approach to goto *instructions[index];.

There was also a lot of work done to transpile the Onyx virtual machine bytecode instructions to native x86-64 and ARM64. This was done very naïvely in a non-optimizing single-pass, so there will be more work done in the future to make the resulting machine code even faster. That being said, it is still 2-4x faster than the bytecode interpreter.

You can take advantage of the new native compilation by passing --jit. You can also set ONYX_JIT=1 in your environment to enable it everywhere.

onyx run --jit program.onyx

With native compilation underway, it was easy to add the ability to compile Onyx to a native executable on MacOS and Linux. You can use onyx build-exe to produce the executable. Note, this does require a C compiler/linker on your system that is accessible by running the cc command. This is just used to link the object file with a native-helper archive.

$ onyx build-exe -o hello hello_world.onyx
$ ./hello
Hello World
$ ldd ./hello
    linux-vdso.so.1 (0x000071d5b86c8000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x000071d5b853c000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x000071d5b8348000)
    /lib64/ld-linux-x86-64.so.2 (0x000071d5b86ca000)

The resulting executable does not have many external dependencies, just the C standard library and math library, as well as the runtime dynamic linking libraries on Linux. Getting rid of the libc and libm would be relatively easy in the future, and the dynamic linking libraries are necessary for linking to other libraries at runtime, like GLFW or Raylib.

Native compilation is still under a lot of development, but it is a promising avenue for making Onyx more practical for real-world applications.

WASM Object Linking

In the past, if you wanted to integrate with other WASM code, there was no way to do so in Onyx. Now, you can use the #object directive to statically link a WASM file produced by Clang or GCC.

use core.libc

#object "some_library.wasm" {
    add :: (x, y: i32) -> i32 ---

    // Use #provide to provide externs to the library
    #provide {
        malloc :: (size: i32) -> rawptr ---
    }

    // Use #namespace to provide a whole package or structure
    #provide { #namespace libc }
}

main :: () {
    // Called just like a normal procedure.
    println(add(1, 2))
}

WIT Bindgen

The final integration to mention is the WASM Component Model. The missing piece here was being able to generate Onyx bindings for a WIT world. Now, you can use the provided onyx wit-bindgen tool to generate these bindings. Internally it uses wasm-tools command to parse WIT files into a digestable format, so that needs to be installed to work.

onyx wit-bindgen wit/ --world command --out generated

There is also a new platform supported for WASI Preview 2, called wasip2.

4. Compilation Info and the Language Server

Another large feature this release is "compilation info", which is the compiler exporting about symbols, types, and more to your build program or compiler extension so you can do interesting things with it. Under the hood, compilation info is serialized to a protobuf, which can be deserialized by the supplied onyx.toolchain.CompilationInfo library, or by any protobuf toolchain in any language. The reason for exporting a protobuf is because the compilation info has to be accessible both to the consumer of libonyx, which could be any language (in principle), and a compiler extension, which gets its input over a socket from the compiler. Protobuf was a natural choice for this due to its relative decoding ease and mature cross language tooling.

Here is an contrived example of printing all of the function names and types in the main package. It is also using the provided onyx.toolchain.Builder which links the Onyx compiler library into an Onyx program, so you can write an Onyx program to compile another Onyx program; very useful if you have a complicated build process with multiple steps and options.

#load "core:onyx/toolchain/module"

use core {*}
use onyx.toolchain {Builder, CompilationInfo}

program :: #code_as_str {
    use core {*}

    func1 :: (x: i32) {
        println(x)
    }

    func2 :: (x: str) -> i32 {
        if conv.parse(i32, x) as v {
            return v
        }
        return 0
    }

    main :: () {
        func1(10)
        func1(func2("123"))
    }
}

main :: () {
    b := Builder.make()
    b->enable(.MULTI_THREADING)
    b->set_platform(.ONYX)

    core_path := os.Path.join(os.env("ONYX_PATH") ?? "", "core")
    b->map_dir("core", core_path)

    // Need to skip the '{' and '}' surrounding the code
    prog := program[1 .. program.count - 1]
    b->inject_source_as("program.onyx", prog)

    b->compile()

    // Should do some error checking...

    // Get the compilation info
    info: CompilationInfo = b->compilation_info()

    // Find the package named `main`
    if info->package_named("main") as main_package {
        // Get the symbol ids inside the package public scope
        main_symbols := info->symbols_in_scope(main_package.scope_id)
        for symbol_id in main_symbols {
            // Get the actual symbol information
            if info->get_symbol(symbol_id) as symbol {
                // Only print for functions
                if symbol.kind != .Function do continue

                // If the symbol doesn't have a type (polymorphic), skip it
                if info->get_type(symbol.type_id) as type {
                    eprintf("{}: {}\n", symbol.name, type.name)
                }
            }
        }
    }
}

Compilation info does not tell you everything about your program. It does not, for example, tell you anything about the syntax tree nodes of functions. This may be added in the future, but that would require a lot more events to be sent which could slow down compilation even more. The information that is already there is enough to make the language server for Onyx, onyx lsp, smart enough to do simple auto-completion of struct fields, methods, and package symbols. It is far from perfect, but will continue to get better with further releases.

5. Deprecation Cleanup

There was a large cleanup in the core library to standardize the shape of the APIs. There was an old convention to put functions that operate on a type in a package with that type. Now, the convention is to put functions that operate on a type directly on the type.

For example, core.map was used to operate on Maps but now you just use the Map type directly.

// old
use core.map

main :: () {
    m: Map(str, i32)
    map.put(&m, "test", 123)
    println(map.get(&m, "test"))
}

// new
main :: () {
    // Map is in the global scope
    m: Map(str, i32)
    Map.put(&m, "test", 123)
    println(Map.get(&m, "test"))
}

The reason this is the convention now is to allow for using the method-call operator in more places. This is the same code using the method call operator.

main :: () {
    m: Map(str, i32)
    m->put("test", 123)
    println(m->get("test"))
}

If you have existing Onyx codebases, you may need to update them to use this new convention. Most types had aliases for their methods for several years, so there is a chance you were already using this convention. This release just fully removes the old top-level functions.

Try it out!

Like usual, to update to the newest version of Onyx simply use the same install script found on the homepage. It will automatically detect your previous install and will override it with the new version.

$ sh <(curl https://get.onyxlang.io -sSfL)

You can also run onyx self-upgrade if you are on MacOS or Linux!

Happy programming!