capnp-janet#

Native Cap’n Proto runtime for Janet, plus a C library embedders can link without the Janet VM.

Sibling runtimes: c-capnproto and capnp-fortran. Status: 0.2.0-dev. AddressBook encode matches official capnp encode when fields are set in schema order.

Install (shortest path)#

$ git clone https://github.com/HaoZeke/capnp-janet.git
$ cd capnp-janet
$ pixi install && pixi run configure && pixi run build && pixi run test

First message (Janet)#

build-message takes a data-word count, a pointer-word count, and an array of [kind byte-offset value] fields:

(import capnp)

(def buf (capnp/build-message 1 1 @[[:u32 0 42] [:text 0 "hello"]]))
(def root (capnp/root (capnp/message-from-buffer buf)))
(print (capnp/get-u32 root 0))        # 42
(print (capnp/get-text root 0))       # hello

Lists come out of a decoded message, such as the shipped capnp encode golden. A list pointer carries Janet’s length, index and iteration protocols, so it answers to the same forms as an array:

(def msg (capnp/message-from-buffer
           (slurp "test/fixtures/addressbook_alice_bob.bin")))
(def people (capnp/getp (:root msg) 0))

(length people)                       # 2
(:text (in people 0) 0)               # "Alice"
(map |(string (:text $ 0)) people)    # @["Alice" "Bob"]
(each p people (print (:u32 p 0)))

(:verb receiver ...) reaches the same accessors as the capnp/ functions: :kind, :ptr, :u16, :u32, :u64, :f64, :bool, :text, and :text-at for an element of a List(Text). A message answers to :root.

C embed (no Janet VM):

#include <capnp-janet/capnp_message.h>

capnp_message_t msg;
capnp_message_view_flat(&msg, bytes, len);
capnp_ptr_t root;
capnp_root(&msg, &root);
const char *s; size_t n;
capnp_get_text(&root, 0, &s, &n);

The tutorial walks AddressBook encode and the embed register path.

Documentation map#

Install

pixi, Meson, C embed, Janet module.

Install
Tutorial

First buffer, AddressBook, capnp_janet_register.

Tutorial
Architecture

C layers, zero-copy views, compiled .jimage packs.

capnp-janet architecture
Wire

Packed, canonical, list evolution.

Wire format