Newshot

Jolt:能与 C 交互的 Clojure

Translation(model translation, not the source's own text)

为什么选择 Jolt?

原生 C FFI

绑定任意 C 共享库:声明它,用 defcfn 包装函数,用 Clojure 值调用它们。db 库正是以这种方式接入 SQLite 和 Postgres;http-client 绑定 OpenSSL 和 zlib。

独立二进制文件

jolt build 会提前编译(ahead-of-time)一个项目(运行时、标准库、应用及其依赖)为单个自包含的可执行文件,C 依赖以静态方式链接。无需 Chez,无需 JVM,运行时也不需要源码。

自托管编译器

读取 Clojure,将其分析为宿主中立的 IR,生成 Scheme,然后运行它:在 Chez 上原生运行,或在 Gambit 上编译为 JavaScript 运行。编译器用 Clojure 编写,并能编译自身;Chez 以完全相同的方式运行解释代码和编译代码,因此 REPL 和构建出的二进制文件行为一致。

真正的并发

future/promise/agent/pmap 运行在共享堆上的 OS 线程上,与 JVM 语义一致。core.async 提供通道和 go 块。

持久化数据结构

不可变向量(32 路 trie)、cons 列表,以及具有 Clojure 值语义的 HAMT map/set。Transients 是真正可变的临时集合。

与 Clojure 兼容

惰性/无限 seq、transducer、解构、multimethod、protocol/record、元数据、命名空间、运行时 eval,以及完整的 reader。

快速开始

安装自包含的 jolt 二进制文件:它捆绑了运行时、编译器和标准库,因此无需安装其他任何东西。


        # Homebrew
brew install jolt-lang/jolt/jolt

# or the install script (Linux / macOS)
curl -sL https://raw.githubusercontent.com/jolt-lang/jolt/main/install | bash

jolt -e '(+ 1 2)'        # => 3
      

或从克隆的仓库运行(需要 Chez Scheme);引导种子已检入,因此没有构建步骤:


        git clone --recurse-submodules https://github.com/jolt-lang/jolt.git
cd jolt
jolt -e '(+ 1 2)'        # => 3
      

用法

求值一个表达式


        $ jolt -e '(->> (range 10) (filter even?) (map (fn [x] (* x x))) (reduce +))'
# 120
$ jolt -e '(/ 1 2)'
# 1/2
      

运行一个项目


        jolt run -m myapp.core   # resolve deps.edn, then run -main
jolt -M:test [args]      # run an alias's :main-opts
jolt path                # print the resolved source roots
      

编译一个独立二进制文件


        jolt build -m myapp.core -o myapp   # single self-contained executable
./myapp arg1 arg2                        # runs anywhere; args reach -main
# --opt for the optimized build, --dev for an unoptimized one
      

绑定一个 C 库


        (require '[jolt.ffi :as ffi])
(ffi/load-library "libsqlite3.dylib")  ; or declare :jolt/native in deps.edn

(ffi/defcfn sqlite3-libversion "sqlite3_libversion" [] :string)
(sqlite3-libversion)             ; => "3.51.0"
      

任何 C 库都以相同方式绑定:原生互操作指南涵盖类型、内存、结构体和静态链接。

nREPL


        jolt --nrepl-server      # auto-resolves deps.edn, writes .nrepl-port
# connect CIDER / Calva / Cursive via the .nrepl-port file, then
# redefine a var and the next call sees it; no restart
      

针对一个运行中的进程进行开发:参见 REPL 驱动的开发。

阅读文档以了解安装、编写库和 C 互操作。

与 Clojure 的差异

Jolt 以 Clojure 语义为目标,但运行在 Scheme 上,而不是 JVM 上。大多数可移植的 Clojure 代码无需修改即可运行:持久化集合、数值塔、惰性 seq、transducer、multimethod、protocol/record、atom、future/agent/pmap、core.async、运行时 eval,以及完整的 reader 的行为都与在 JVM 上一致。真正的差异如下:

方面差异
Java 互操作没有 JVM,因此没有通用的 Java 互操作、反射或 gen-class / proxy;取而代之的是原生 C FFI,以及对 java.* 子集的 shim
BigDecimal不可用( decimal? 始终为 false);数值塔的其余部分保持一致
正则表达式由 irregex 编译,而非 java.util.regex;常见模式可用,某些 Java 特有特性有所不同

The translation is machine-generated and individual terms may be off. Nothing is rewritten or cut; the original is authoritative.

Published by Hacker News
This site only summarises and aggregates; copyright stays with the original author. Headlines and summaries follow the source — where they differ, the original wins.
Read the original ↗