展示 HN:Orbit,一种系统级编程语言,可以将 .sh 编译到 LLVM。
Show HN: Orbit a systems level programming language that compiles .sh to LLVM

原始链接: https://github.com/SIE-Libraries/orbit

## 飞船:一种现代系统自动化语言 飞船是一种新的系统自动化语言,旨在取代传统的 shell 脚本,优先考虑性能、安全性和可靠性。它具有严格的、受 Go 启发的语法和固定宽度的类型系统——需要显式类型声明(例如,`i64` 用于整数,`u8[]` 用于字符串)。 主要特性包括一个基于 LLVM 的新型 JIT(即时编译)编译模型,将 POSIX 命令转换为本机代码,以显著提高速度。安全性通过直接的操作系统系统调用接口得到增强,从而消除了 shell 注入漏洞。错误处理是显式的,利用 POSIX 退出代码通过 `!i32` 合同系统和 `check {} except {}` 块来实现。 飞船采用延迟执行流水线模型,使用 `.then()` 和 `.run()`,并提供 `@jit` 指令来编译现有的 shell 脚本以提高性能。其标准库专注于 JIT 编译的 POSIX 层和一个 `Syscalls` 运行时,用于高效的系统操作。目标是在常见的任务(如日志文件分析)中实现高达 14 倍于 Bash 的性能提升。

## Orbit:一种用于 Shell 脚本的新系统语言 Orbit 是一种新的系统级编程语言,旨在弥合快速 shell 脚本和健壮系统代码之间的差距。它将 `.sh` 文件编译为 LLVM,以实现本机机器代码执行,从而在性能上可能优于传统的 shell 解释器。 主要特性包括 JIT 编译器、"通过省略实现安全" 模型(除非明确允许,否则限制功能)、对任意位宽的支持以及受 Go 启发的错误处理系统。该语言利用 Boost 作为其标准库,确保 POSIX 兼容性。 然而,最初对该项目的反应褒贬不一。人们对所呈现的基准测试的有效性(被描述为“假设的”)、实现复杂性(部分代码似乎不完整)以及取代 Bash 等广泛使用的工具的实用性表示担忧。 Deno 和 Bun 等替代方案提供类似的处理流程功能,并具有改进的语法,而 FreedomLang 则采用不同的方法,侧重于直接二进制输出和基于文件系统的并发性,以增强安全性和可审计性。关于 Orbit 如何处理 Bash 的错误模型和语义怪癖,仍然存在疑问。
相关文章

原文

Spaceship is a high-performance systems automation language designed to replace legacy shell scripting. It features a strict, Go-inspired syntax, a powerful fixed-width type system, and a novel JIT (Just-In-Time) compilation model for POSIX commands, all built on top of LLVM.

  • Performance: Statically typed and JIT-compiled for maximum execution speed.
  • Security: Eliminates shell injection vulnerabilities through a strict Process API powered by direct OS-level syscalls.
  • Reliability: A clear, explicit error handling model based on POSIX exit codes.
  • Modern Syntax: A clean, Go-inspired syntax that is easy to read and write.

Spaceship uses a strict, fixed-width type system. There is no type inference; all types must be explicitly declared.

Type Description Syntax Example
i1 Boolean type var is_active i1 = true
i8 - i128 Fixed-width signed integers var user_id i64
f32, f64 Floating-point numbers const PI f64 = 3.14159
u8[] Raw byte array (string) var message u8[] = "hello"
[<size>]<type> Fixed-size array var buffer [1024]i8
map[<k>]<v> Hash map var scores map[u8[]]i32

Error Handling: The !i32 Contract

Spaceship uses an explicit error handling mechanism that maps directly to POSIX exit codes and errno. Any function that can fail must declare its return type with a ! prefix, indicating that it returns an error contract.

Example: fn readFile(path u8[]) !i32

  • On success, the function returns a non-zero value.
  • On failure, it returns a standard POSIX error code (e.g., ENOENT for "No such file or directory").

This contract is enforced by the check {} except {} block.

check {
    // Code that might fail
    var fd = readFile("my_file.txt")
} except {
    // This block executes if readFile() returns an error code
    // The error code is implicitly available in the `err` variable
    Posix.write(stdout, "Error reading file: " + err)
}

Execution and Pipeline Model

Secure Process Execution with the Syscalls Library

All external commands are executed using the Process API. This API is powered by a backend Syscalls runtime library that interfaces directly with the host operating system's process creation APIs (e.g., fork/execve on Linux/macOS, CreateProcess on Windows).

This is a critical security feature that prevents shell injection vulnerabilities by design, as arguments are passed as a structured array, not a raw string to be parsed by a shell.

Example: Process("ls", ["-l", "/home/user"])

Spaceship uses a deferred execution model for command pipelines, inspired by fluent APIs. Operations are chained together using .then(), but no execution occurs until .run() is called.

Example:

var pipeline = Process("grep", ["-r", "keyword", "."])
    .then(Process("wc", ["-l"]))

// Nothing has executed yet.

var result = pipeline.run() // The pipeline is now executed.

The @jit Directive: Shell-to-Native Translation

The @jit directive is a powerful compiler feature that translates shell scripts into native POSIX logic and JIT-compiles them directly into the LLVM execution path. This provides a significant performance and security advantage over traditional shell script execution.

Example: @jit("deploy.sh")

This allows developers to leverage existing shell scripts while benefiting from the performance and security of the Spaceship runtime.

The primary standard library for Spaceship is the JIT-compiled POSIX layer, along with the Syscalls runtime library. This ensures that all file I/O, process management, and other system-level operations are as fast and efficient as possible.

// Declare a fixed-size array of 4 64-bit integers
var vector [4]i64

// Declare a map with string keys and 32-bit integer values
var user_ages map[u8[]]i32

// Accessing an element (syntax)
vector[2] = 100
user_ages["jules"] = 32

Function Definition and Error Handling

// Definition for a function that can fail
fn open_or_fail(path u8[]) !i32 {
    // ... low-level POSIX call to open the file ...
    // Returns a file descriptor (i32) on success or an error code on failure.
}

fn main() {
    check {
        var file_descriptor = open_or_fail("/etc/hosts")
    } except {
        // The 'err' variable is implicitly available and holds the i32 error code.
        Posix.write(stdout, "Failed to open file with error code: " + err)
    }
}
// Find all .log files, count their lines, sort numerically, and get the top 5.
var pipeline = Process("find", [".", "-name", "*.log"])
    .then(Process("xargs", ["wc", "-l"]))
    .then(Process("sort", ["-n"]))
    .then(Process("tail", ["-n", "5"]))

// Execute the entire pipeline.
var top_five_logs = pipeline.run()

Posix.write(stdout, top_five_logs)

The primary goal of Spaceship is to provide a significant performance improvement over traditional, interpreted shell scripting languages like Bash. By using a JIT-compiler with LLVM, we aim to execute common systems administration and automation tasks an order of magnitude faster.

The following benchmark is hypothetical and serves to illustrate the performance goals of the project. The task is to count the total number of lines in all .log files within a large directory structure.

Language / Method Time (seconds) Performance Multiplier Notes
bash (find + xargs + wc) ~12.5s 1x (Baseline) High process creation overhead.
python (os.walk) ~7.8s ~1.6x Faster, but still interpreted.
Spaceship (Goal) ~0.9s ~14x JIT-compiled native code with minimal overhead.

This theoretical benchmark highlights the advantages of avoiding interpreter overhead and leveraging direct, compiled POSIX calls for process management and I/O. Note : The standard library is still under development .

联系我们 contact @ memedata.com