=============== LIBRARY RULES =============== From library maintainers: - capnp_get_u32(const capnp_ptr_t *p, uint32_t off, uint32_t dflt) returns uint32_t. It is not an out-parameter API. - Allocate and set fields in schema declaration order for official capnp encode byte parity. - Both a Janet module (import capnp) and a plain C API (capnp-janet/capnp_message.h) exist. Embedders do not need the Janet VM. - Packed and canonical forms are byte-identical with Cap'n C++ / capnp convert on AddressBook goldens. - Sibling runtimes: HaoZeke/c-capnproto and HaoZeke/capnp-fortran. ### Build commands with pixi Source: https://github.com/haozeke/capnp-janet/blob/main/CONTRIBUTING.md Run these commands in order to install dependencies, configure, build, and test the project. Keep changes small and ensure `meson test` stays green. ```console pixi install pixi run configure pixi run build pixi run test ``` -------------------------------- ### Janet usage example Source: https://github.com/haozeke/capnp-janet/blob/main/docs/orgmode/install.org Basic Janet usage: create a builder, set fields, finish, and read back. Demonstrates set-u32, set-text, get-u32, and get-text. ```janet (import capnp) (def builder (capnp/new-builder)) (def body (capnp/init-root builder 1 1)) (capnp/set-u32 body 0 42) (capnp/set-text body 0 "hello") (def buf (capnp/finish-builder builder)) (def root (capnp/root (capnp/message-from-buffer buf))) (print (capnp/get-u32 root 0)) # 42 (print (capnp/get-text root 0)) # hello ``` -------------------------------- ### Install and run via pixi Source: https://github.com/haozeke/capnp-janet/blob/main/docs/orgmode/install.org Use pixi to install dependencies, configure, build, and test the project. ```console $ pixi install $ pixi run configure $ pixi run build $ pixi run test ``` -------------------------------- ### C embed example Source: https://github.com/haozeke/capnp-janet/blob/main/docs/orgmode/install.org Example of embedding capnp-janet in C. Includes the header, initializes a message, views a flat buffer, gets the root, and reads text. Link against libcapnp_janet (pkg-config: capnp-janet). ```c #include capnp_message_t msg = {0}; 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, /*ptr*/0, &s, &n); ``` -------------------------------- ### Install via bundle (Janet 1.36+) Source: https://github.com/haozeke/capnp-janet/blob/main/docs/orgmode/install.org Install using Janet's bundle feature. Run 'janet --install .' in the project directory. Requires Janet 1.36 or newer. ```console $ janet --install . ``` -------------------------------- ### Install via jpm Source: https://github.com/haozeke/capnp-janet/blob/main/docs/orgmode/install.org Install the capnp-janet library using jpm. After installation, import with (import capnp). Requires a system Janet with jpm and a C11 compiler. ```console $ jpm install https://github.com/HaoZeke/capnp-janet.git # after it is listed on janet-lang/pkgs: $ jpm install capnp ``` -------------------------------- ### Install capnp-janet with pixi Source: https://github.com/haozeke/capnp-janet/blob/main/docs/orgmode/index.org Clones the repository and runs the full build/test pipeline via pixi. Requires pixi installed. ```console $ git clone https://github.com/HaoZeke/capnp-janet.git $ cd capnp-janet $ pixi install && pixi run configure && pixi run build && pixi run test ``` -------------------------------- ### Install via jpm Source: https://github.com/haozeke/capnp-janet/blob/main/README.md Install the native Janet module with jpm. The second command works only after the package is listed in janet-lang/pkgs. ```console $ jpm install https://github.com/HaoZeke/capnp-janet.git # after janet-lang/pkgs lists it: $ jpm install capnp ``` -------------------------------- ### Address and leak sanitizer wrapper for Janet module Source: https://github.com/haozeke/capnp-janet/blob/main/CONTRIBUTING.md Creates a wrapper script that preloads the ASan and UBSan runtimes before launching `janet`, then runs `meson test` with leak detection and halt-on-error. Required because `capnp.so` is built with sanitizers but the Janet interpreter is not; preloading into the shell or a binary already linked with the runtime causes failures. ```console JANET=$(command -v janet) ASAN=$(gcc-13 -print-file-name=libasan.so) UBSAN=$(gcc-13 -print-file-name=libubsan.so) mkdir -p /tmp/jwrap-both printf '#!/bin/sh\nexport LD_PRELOAD=%s:%s\nexec %s "$@"\n' \ "$ASAN" "$UBSAN" "$JANET" > /tmp/jwrap-both/janet chmod +x /tmp/jwrap-both/janet PATH="/tmp/jwrap-both:$PATH" \ ASAN_OPTIONS=detect_leaks=1:halt_on_error=1 \ UBSAN_OPTIONS=halt_on_error=1:print_stacktrace=1 \ meson test -C build-janet-asan ``` -------------------------------- ### Janet list access example Source: https://github.com/haozeke/capnp-janet/blob/main/docs/orgmode/install.org Reads a list from a decoded message, using a golden fixture file. Shows how to get a list field and access elements with :text. ```janet (def people (capnp/getp (capnp/root (capnp/message-from-buffer (slurp "test/fixtures/addressbook_alice_bob.bin"))) 0)) (print (length people)) # 2 (print (:text (in people 0) 0)) # Alice ``` -------------------------------- ### Generate and install pkg-config file Source: https://github.com/haozeke/capnp-janet/blob/main/CMakeLists.txt Sets up prefix, exec_prefix, libdir, bindir, and includedir variables (handling absolute paths) and configures capnp-janet.pc.in into capnp-janet.pc, then installs it to the pkgconfig directory. ```CMake # pkg-config, matching the file meson's pkg.generate writes. set(prefix "${CMAKE_INSTALL_PREFIX}") set(exec_prefix "\${prefix}") if(IS_ABSOLUTE "${CMAKE_INSTALL_LIBDIR}") set(libdir "${CMAKE_INSTALL_LIBDIR}") else() set(libdir "\${prefix}/${CMAKE_INSTALL_LIBDIR}") endif() if(IS_ABSOLUTE "${CMAKE_INSTALL_BINDIR}") set(bindir "${CMAKE_INSTALL_BINDIR}") else() set(bindir "\${prefix}/${CMAKE_INSTALL_BINDIR}") endif() if(IS_ABSOLUTE "${CMAKE_INSTALL_INCLUDEDIR}") set(includedir "${CMAKE_INSTALL_INCLUDEDIR}") else() set(includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}") endif() configure_file( "${CMAKE_CURRENT_SOURCE_DIR}/capnp-janet.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/capnp-janet.pc" @ONLY ) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/capnp-janet.pc" DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig ) ``` -------------------------------- ### C embed example Source: https://github.com/haozeke/capnp-janet/blob/main/docs/orgmode/tutorial.org Register capnp-janet with the Janet environment, view a flat message, get the root, and read a u32 field. Link libcapnp_janet (pkg-config: capnp-janet). Compiled .jimage packs cannot store native capnp/* CFUNS; the host re-registers them. ```c #include #include /* after janet_init(env): */ capnp_janet_register(env); capnp_message_t msg = {0}; capnp_message_view_flat(&msg, bytes, len); capnp_ptr_t root; capnp_root(&msg, &root); uint32_t id = capnp_get_u32(&root, 0, 0); ``` -------------------------------- ### Build with bare Meson Source: https://github.com/haozeke/capnp-janet/blob/main/docs/orgmode/install.org Build and test using Meson. Requires Meson 1.2 or newer; Debian bookworm's apt package (1.0.1) cannot configure this tree. Optionally enable the Janet module by having a janet pkg-config package visible or using -Djanet=enabled. ```console $ meson setup build $ meson compile -C build $ meson test -C build ``` -------------------------------- ### Install capnp-janet with pixi Source: https://github.com/haozeke/capnp-janet/blob/main/docs/source/llms.txt Clones the repository and runs the standard pixi-based build and test workflow. ```sh git clone https://github.com/HaoZeke/capnp-janet.git cd capnp-janet pixi install && pixi run configure && pixi run build && pixi run test ``` -------------------------------- ### Install targets and export Source: https://github.com/haozeke/capnp-janet/blob/main/CMakeLists.txt Installs capnp_janet and optionally capnpc-janet, exporting them under capnp_janetTargets. ```cmake if(CAPNP_JANET_INSTALL) install(TARGETS capnp_janet EXPORT capnp_janetTargets ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) if(CAPNP_JANET_BUILD_PLUGIN) install(TARGETS capnpc-janet EXPORT capnp_janetTargets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) endif() ``` -------------------------------- ### Install headers and Janet policy file Source: https://github.com/haozeke/capnp-janet/blob/main/CMakeLists.txt Installs the public C headers under include/capnp-janet/ and the Janet policy file janet/policy.janet into the appropriate install directories. ```CMake install(FILES include/capnp-janet/capnp_kinds.h include/capnp-janet/capnp_pointer.h include/capnp-janet/capnp_message.h include/capnp-janet/capnp_builder.h include/capnp-janet/capnp_packed.h include/capnp-janet/capnp_canonical.h include/capnp-janet/capnp_copy.h include/capnp-janet/capnp_janet.h include/capnp-janet/capnp_rpc.h include/capnp-janet/capnp_transport.h include/capnp-janet/capnp_dyn.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/capnp-janet ) install(FILES janet/policy.janet DESTINATION ${CMAKE_INSTALL_DATADIR}/capnp-janet/janet ) ``` -------------------------------- ### CMake options and project setup Source: https://github.com/haozeke/capnp-janet/blob/main/CMakeLists.txt Defines build options CAPNP_JANET_BUILD_PLUGIN and CAPNP_JANET_INSTALL, sets C standard to 11, and finds Threads. The options default to the top-level project status. ```cmake # Prefix CAPNP_JANET_* — Cap'n Proto C++ already uses CAPNP_* (CAPNP_EXECUTABLE, …). option(CAPNP_JANET_BUILD_PLUGIN "Build the capnpc-janet schema plugin" ${_capnp_janet_is_toplevel}) option(CAPNP_JANET_INSTALL "Install targets and package config" ${_capnp_janet_is_toplevel}) set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD_REQUIRED ON) # pthread_sigmask lives in libpthread on glibc before 2.34. Meson links # dependency('threads'); the same edge belongs on this target so a static # archive and an old shared library both pull it in. find_package(Threads REQUIRED) ``` -------------------------------- ### Janet arena builder example Source: https://github.com/haozeke/capnp-janet/blob/main/docs/orgmode/tutorial.org Build a message with nested values using capnp/new-builder, set fields, finish, and read them back. message-from-buffer copies the buffer; message-view-buffer aliases and GC-marks it. Typed setters store value XOR default. ```janet (import capnp) (def builder (capnp/new-builder)) (def body (capnp/init-root builder 1 2)) (capnp/set-u32 body 0 42) (capnp/set-text body 0 "hello") (capnp/set-list-text body 1 @["a" "bb"]) (def buf (capnp/finish-builder builder)) (def msg (capnp/message-from-buffer buf)) (def root (capnp/root msg)) (print (capnp/get-u32 root 0)) # 42 (print (capnp/get-text root 0)) # hello (def items (capnp/getp root 1)) (print (capnp/list-get-text items 1)) # bb ``` -------------------------------- ### Regenerate golden fixtures Source: https://github.com/haozeke/capnp-janet/blob/main/docs/orgmode/install.org Run the script to regenerate test fixtures. Requires system capnp. ```console $ ./scripts/gen-sample-fixtures.sh ``` -------------------------------- ### Install CMake package export and config files Source: https://github.com/haozeke/capnp-janet/blob/main/CMakeLists.txt Installs the exported CMake targets file with namespace capnp_janet:: and generates/installs the package config and version files for find_package(capnp_janet). ```CMake install(EXPORT capnp_janetTargets FILE capnp_janetTargets.cmake NAMESPACE capnp_janet:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/capnp_janet ) configure_package_config_file( "${CMAKE_CURRENT_SOURCE_DIR}/cmake/capnp_janetConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/capnp_janetConfig.cmake" INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/capnp_janet ) write_basic_package_version_file( "${CMAKE_CURRENT_BINARY_DIR}/capnp_janetConfigVersion.cmake" VERSION ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion ) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/capnp_janetConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/capnp_janetConfigVersion.cmake" DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/capnp_janet ) ``` -------------------------------- ### FetchContent usage example for capnp-janet Source: https://github.com/haozeke/capnp-janet/blob/main/CMakeLists.txt Shows how to consume capnp-janet as a CMake subproject via FetchContent. Declares the repository and tag v0.2.0, then links the target capnp_janet::capnp_janet to your application. This is the recommended way for C embedders to use the runtime. ```cmake include(FetchContent) FetchContent_Declare( capnp_janet GIT_REPOSITORY https://github.com/HaoZeke/capnp-janet.git GIT_TAG v0.2.0 ) FetchContent_MakeAvailable(capnp_janet) target_link_libraries(myapp PRIVATE capnp_janet::capnp_janet) ``` -------------------------------- ### Include GNUInstallDirs and CMakePackageConfigHelpers Source: https://github.com/haozeke/capnp-janet/blob/main/CMakeLists.txt Includes standard CMake modules for install directories and package configuration helpers, needed for generating and installing package config files. ```cmake include(GNUInstallDirs) include(CMakePackageConfigHelpers) ``` -------------------------------- ### Set include directories and link Threads Source: https://github.com/haozeke/capnp-janet/blob/main/CMakeLists.txt Exposes the include directory for build and install interfaces, and links the Threads::Threads target publicly. ```cmake target_include_directories(capnp_janet PUBLIC $ $ ) target_link_libraries(capnp_janet PUBLIC Threads::Threads) ``` -------------------------------- ### Use generated helpers in Janet Source: https://github.com/haozeke/capnp-janet/blob/main/docs/orgmode/tutorial.org Use the generated codegen-features module to build and read a message. Requires build on JANET_PATH. The example sets count to -70001 and asserts the decoded value matches. ```janet (import capnp) (import codegen-features) (def builder (capnp/new-builder)) (def root (codegen-features/CodegenFeatures-init-root builder)) (codegen-features/CodegenFeatures-set-count root -70001) (def decoded (:root (capnp/message-from-buffer (capnp/finish-builder builder)))) (assert (= -70001 (codegen-features/CodegenFeatures-get-count decoded))) ``` -------------------------------- ### Define capnp_janet project Source: https://github.com/haozeke/capnp-janet/blob/main/CMakeLists.txt Declares the capnp_janet project with the extracted version, a description, homepage URL, and C language only. This project name and namespace differ from the official Cap'n Proto C++ to allow side-by-side installation. ```cmake project(capnp_janet VERSION ${CAPNP_JANET_VERSION} DESCRIPTION "Native Cap'n Proto runtime for Janet (wire, packed, canonical)" HOMEPAGE_URL "https://capnp-janet.rgoswami.me" LANGUAGES C ) ``` -------------------------------- ### CMakeLists.txt for add_subdirectory consumer smoke test Source: https://github.com/haozeke/capnp-janet/blob/main/cmake/consumer_smoke/CMakeLists.txt Builds a tiny program against capnp_janet via add_subdirectory, the same path FetchContent_MakeAvailable takes. Disables plugin and install options, then links the smoke executable to capnp_janet::capnp_janet. ```CMake # Smoke: build a tiny program against capnp_janet via add_subdirectory, # the same path FetchContent_MakeAvailable takes. cmake_minimum_required(VERSION 3.16) project(capnp_janet_consumer_smoke LANGUAGES C) set(CAPNP_JANET_BUILD_PLUGIN OFF CACHE BOOL "" FORCE) set(CAPNP_JANET_INSTALL OFF CACHE BOOL "" FORCE) add_subdirectory("${CAPNP_JANET_SOURCE_DIR}" "${CMAKE_BINARY_DIR}/capnp_janet-build") add_executable(smoke smoke.c) target_link_libraries(smoke PRIVATE capnp_janet::capnp_janet) ``` -------------------------------- ### capnp/getp Source: https://github.com/haozeke/capnp-janet/blob/main/README.md Gets a pointer field from a struct view. Returns a list view for list fields or a struct view for struct fields. ```APIDOC ## capnp/getp ### Description Gets a pointer field from a struct view. ### Method Function ### Endpoint `capnp/getp` ### Parameters - **struct** (view) - Required - The struct view. - **index** (integer) - Required - The pointer field index. ### Response Returns a list view or struct view depending on the field type. ### Example ```janet (def people (capnp/getp (:root msg) 0)) ``` ``` -------------------------------- ### capnp/list-getp Source: https://github.com/haozeke/capnp-janet/blob/main/README.md Gets a pointer element from a list view at a given index. Returns a struct view or list view. ```APIDOC ## capnp/list-getp ### Description Gets a pointer element from a list view at a given index. ### Method Function ### Endpoint `capnp/list-getp` ### Parameters - **list** (list view) - Required - The list view. - **index** (integer) - Required - The index. ### Response Returns a struct view or list view. ### Example ```janet (:text (in people 0) 0) # "Alice" ``` ``` -------------------------------- ### capnp_root Source: https://github.com/haozeke/capnp-janet/blob/main/README.md Gets the root struct pointer from a message. The pointer can be used with getter functions. ```APIDOC ## capnp_root ### Description Gets the root struct pointer from a message. ### Method Function ### Endpoint `capnp_root` ### Parameters - **msg** (capnp_message_t*) - Required - The message struct. - **root** (capnp_ptr_t*) - Required - Output pointer to the root struct. ### Response Returns an error code (0 on success). ### Example ```c capnp_ptr_t root; capnp_root(&msg, &root); ``` ``` -------------------------------- ### capnp/build-message Source: https://github.com/haozeke/capnp-janet/blob/main/README.md A compact compatibility helper for building flat demo structs. It remains available for convenience. ```APIDOC ## capnp/build-message ### Description A compact compatibility helper for building flat demo structs. ### Method Function ### Endpoint `capnp/build-message` ### Parameters None documented. ### Response Returns a message buffer. ### Example No example provided in source. ``` -------------------------------- ### Build and read a demo message in Janet Source: https://github.com/haozeke/capnp-janet/blob/main/docs/source/llms.txt Creates a demo buffer, wraps it in a message, and reads a u32, text, and a list item. Uses capnp/build-demo, capnp/message-from-buffer, capnp/root, capnp/get-u32, capnp/get-text, capnp/getp, and capnp/list-get-text. ```janet (import capnp) (def buf (capnp/build-demo 42 "hello" ["a" "bb"])) (def msg (capnp/message-from-buffer buf)) (def root (capnp/root msg)) (print (capnp/get-u32 root 0)) (print (capnp/get-text root 0)) (def items (capnp/getp root 1)) (print (capnp/list-get-text items 1)) ``` -------------------------------- ### Create and read a Cap'n Proto message in Janet Source: https://github.com/haozeke/capnp-janet/blob/main/docs/orgmode/index.org Builds a message with a root struct containing a u32 and text, then reads them back. Uses capnp/new-builder, capnp/init-root, capnp/set-u32, capnp/set-text, capnp/finish-builder, capnp/message-from-buffer, capnp/root, capnp/get-u32, capnp/get-text. ```janet (import capnp) (def builder (capnp/new-builder)) (def body (capnp/init-root builder 1 1)) (capnp/set-u32 body 0 42) (capnp/set-text body 0 "hello") (def buf (capnp/finish-builder builder)) (def root (capnp/root (capnp/message-from-buffer buf))) (print (capnp/get-u32 root 0)) # 42 (print (capnp/get-text root 0)) # hello ``` -------------------------------- ### Project layout tree Source: https://github.com/haozeke/capnp-janet/blob/main/README.md Shows the top-level directory structure of the repository. ```text src/ C runtime + Janet module test/ C wire + sample tests test/fixtures/ capnp encode goldens schema/ demo + addressbook + calculator app/ capnpc-janet schema compiler plugin interop/ official CLI and live capnp-C++ interop docs/orgmode/ architecture notes ``` -------------------------------- ### Generate helpers with capnpc-janet Source: https://github.com/haozeke/capnp-janet/blob/main/docs/orgmode/tutorial.org Compile a schema using capnp compile with the capnpc-janet plugin. The plugin emits an importable Janet module with typed readers, builders, union selectors, group initializers, scalar defaults, interfaces, and list helpers. ```console $ cp schema/codegen-features.capnp build/ $ cd build $ capnp compile -o./capnpc-janet codegen-features.capnp ``` -------------------------------- ### Set library properties Source: https://github.com/haozeke/capnp-janet/blob/main/CMakeLists.txt Configures output name, version, SOVERSION, position-independent code, and Windows symbol export for capnp_janet. ```cmake set_target_properties(capnp_janet PROPERTIES OUTPUT_NAME capnp_janet VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR} POSITION_INDEPENDENT_CODE ON WINDOWS_EXPORT_ALL_SYMBOLS ON EXPORT_NAME capnp_janet ) ``` -------------------------------- ### Define capnp_janet library target Source: https://github.com/haozeke/capnp-janet/blob/main/CMakeLists.txt Creates the capnp_janet library from the same source set as meson.build lib_sources, and provides an alias capnp_janet::capnp_janet. ```cmake # Same source set as meson.build lib_sources. add_library(capnp_janet src/capnp_message.c src/capnp_builder.c src/capnp_packed.c src/capnp_canonical.c src/capnp_rpc.c src/capnp_transport.c src/capnp_dyn.c ) add_library(capnp_janet::capnp_janet ALIAS capnp_janet) ``` -------------------------------- ### Embed sketch: C host and Janet pack Source: https://github.com/haozeke/capnp-janet/blob/main/docs/orgmode/architecture.org Illustrates embedding the runtime: a C host builds a message, registers the Janet environment, and loads a pack; the Janet pack reads the Cap'n message via capnp/root and returns a decision. The two blocks are complementary parts of one embedding workflow. ```text C host: build an application schema with c-capnproto or this builder serialize or pass segment pointer janet_init; capnp_janet_register(env); load pack pack reads Cap'n; returns decision (Janet values or Cap'n builder) ``` ```janet Janet pack: (defn check-shell [msg] (def root (capnp/root msg)) ... read argv / pathProbes via getp + get-text ... @{:action :deny :reason "..."}) ``` -------------------------------- ### Build capnpc-janet plugin Source: https://github.com/haozeke/capnp-janet/blob/main/CMakeLists.txt Conditionally builds the capnpc-janet executable from app/capnpc_janet.c and links it privately to capnp_janet::capnp_janet. ```cmake if(CAPNP_JANET_BUILD_PLUGIN) add_executable(capnpc-janet app/capnpc_janet.c) target_link_libraries(capnpc-janet PRIVATE capnp_janet::capnp_janet) endif() ``` -------------------------------- ### Run C++ RPC interop test Source: https://github.com/haozeke/capnp-janet/blob/main/interop/README.md Runs the isolated gate for the live capnp-C++ RPC peer. Requires the C++ toolchain, capnp-rpc, and compiler tools; the client calls add(-20, 22) and expects the peer to return 2. ```console meson test -C build rpc_interop_cpp --print-errorlogs ``` -------------------------------- ### Set compile definitions for shared/static builds Source: https://github.com/haozeke/capnp-janet/blob/main/CMakeLists.txt Adds CAPNP_JANET_BUILDING when BUILD_SHARED_LIBS is on, otherwise CAPNP_JANET_STATIC. This avoids inconsistent dll linkage on MSVC with WINDOWS_EXPORT_ALL_SYMBOLS. ```cmake # The headers say dllexport while the library is being built and # dllimport to a consumer; a static archive wants neither. Without this # MSVC sees inconsistent dll linkage against WINDOWS_EXPORT_ALL_SYMBOLS. if(BUILD_SHARED_LIBS) target_compile_definitions(capnp_janet PRIVATE CAPNP_JANET_BUILDING) else() target_compile_definitions(capnp_janet PUBLIC CAPNP_JANET_STATIC) endif() ``` -------------------------------- ### Read and sanitize VERSION.txt for project version Source: https://github.com/haozeke/capnp-janet/blob/main/CMakeLists.txt Reads the version from VERSION.txt, strips whitespace, and extracts the numeric MAJOR.MINOR.PATCH part. The -dev suffix is removed because CMake project() rejects it. ```cmake file(READ "${CMAKE_CURRENT_SOURCE_DIR}/VERSION.txt" _capnp_janet_version_raw) string(STRIP "${_capnp_janet_version_raw}" _capnp_janet_version_raw) # CMake project() rejects a -dev suffix; keep the numeric part. string(REGEX MATCH "^[0-9]+\\.[0-9]+\\.[0-9]+" CAPNP_JANET_VERSION "${_capnp_janet_version_raw}") ``` -------------------------------- ### Embed capnp-janet in C without Janet VM Source: https://github.com/haozeke/capnp-janet/blob/main/docs/orgmode/index.org Parses a flat buffer and reads a text field from the root. Requires zeroing the message or calling capnp_message_init before parsing/freeing; a second parse releases the previous buffer. ```c #include capnp_message_t msg = {0}; 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); ``` -------------------------------- ### Read a list from a decoded Cap'n Proto message in Janet Source: https://github.com/haozeke/capnp-janet/blob/main/docs/orgmode/index.org Loads a binary fixture and reads a list of structs. The list pointer supports length, indexing, iteration, and map. Uses capnp/message-from-buffer, capnp/getp, :text, :u32. ```janet (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))) ``` -------------------------------- ### CMake minimum version requirement Source: https://github.com/haozeke/capnp-janet/blob/main/CMakeLists.txt Sets the minimum CMake version required to build the project. Must be at least 3.16. ```cmake cmake_minimum_required(VERSION 3.16) ``` -------------------------------- ### capnp/new-builder Source: https://github.com/haozeke/capnp-janet/blob/main/README.md Creates a new growable arena for building a Cap'n Proto message. Returns a builder object that can be used to initialize a root struct. ```APIDOC ## capnp/new-builder ### Description Creates a new growable arena for building a Cap'n Proto message. ### Method Function ### Endpoint `capnp/new-builder` ### Parameters None ### Response Returns a builder object. ### Example ```janet (def builder (capnp/new-builder)) ``` ``` -------------------------------- ### capnp_message_init Source: https://github.com/haozeke/capnp-janet/blob/main/README.md Initializes a `capnp_message_t` struct. Must be called before using `from_flat`, `view_flat`, `from_segments`, or `capnp_message_free` if the struct was not zero-initialized. ```APIDOC ## capnp_message_init ### Description Initializes a `capnp_message_t` struct. ### Method Function ### Endpoint `capnp_message_init` ### Parameters - **msg** (capnp_message_t*) - Required - The message struct to initialize. ### Response None. ### Example ```c capnp_message_t msg; capnp_message_init(&msg); ``` ``` -------------------------------- ### capnp_janet_register Source: https://github.com/haozeke/capnp-janet/blob/main/README.md Registers the capnp-janet module with a Janet environment. Must be called after `janet_init` for embedders that amalgamate Janet. ```APIDOC ## capnp_janet_register ### Description Registers the capnp-janet module with a Janet environment. ### Method Function ### Endpoint `capnp_janet_register` ### Parameters - **env** (JanetTable*) - Required - The Janet environment. ### Response None. ### Example ```c capnp_janet_register(env); ``` ``` -------------------------------- ### capnp/message-from-buffer Source: https://github.com/haozeke/capnp-janet/blob/main/README.md Creates a message object from a buffer containing a serialized Cap'n Proto message. The message can be used to access the root struct. ```APIDOC ## capnp/message-from-buffer ### Description Creates a message object from a buffer containing a serialized Cap'n Proto message. ### Method Function ### Endpoint `capnp/message-from-buffer` ### Parameters - **buffer** (buffer) - Required - The buffer containing the serialized message. ### Response Returns a message object. ### Example ```janet (def msg (capnp/message-from-buffer buf)) ``` ``` -------------------------------- ### capnp_message_view_flat Source: https://github.com/haozeke/capnp-janet/blob/main/README.md Views a flat buffer as a Cap'n Proto message. The message struct must be initialized or zeroed before calling. ```APIDOC ## capnp_message_view_flat ### Description Views a flat buffer as a Cap'n Proto message. ### Method Function ### Endpoint `capnp_message_view_flat` ### Parameters - **msg** (capnp_message_t*) - Required - The message struct. - **bytes** (const uint8_t*) - Required - The buffer containing the serialized message. - **len** (size_t) - Required - The length of the buffer. ### Response None. ### Example ```c capnp_message_view_flat(&msg, bytes, len); ``` ``` -------------------------------- ### Detect top-level build Source: https://github.com/haozeke/capnp-janet/blob/main/CMakeLists.txt Sets a variable indicating whether this project is the top-level CMake source directory. This is used to conditionally configure behavior when consumed via add_subdirectory. ```cmake if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) set(_capnp_janet_is_toplevel ON) else() set(_capnp_janet_is_toplevel OFF) endif() ``` -------------------------------- ### capnp/finish-builder Source: https://github.com/haozeke/capnp-janet/blob/main/README.md Finalizes the builder and returns the serialized message buffer. The buffer can be used to create a message for reading. ```APIDOC ## capnp/finish-builder ### Description Finalizes the builder and returns the serialized message buffer. ### Method Function ### Endpoint `capnp/finish-builder` ### Parameters - **builder** (builder) - Required - The builder object. ### Response Returns a buffer containing the serialized message. ### Example ```janet (def buf (capnp/finish-builder builder)) ``` ``` -------------------------------- ### Architecture layers diagram Source: https://github.com/haozeke/capnp-janet/blob/main/docs/orgmode/architecture.org Shows the layered design from schema compiler down to constants and word codecs. The top layer is capnpc-janet, followed by janet_mod, capnp_builder, capnp_message, and finally capnp_kinds/capnp_pointer. ```text capnpc-janet (schema -> typed Janet helpers) | janet_mod (native module + register for embeds) | capnp_builder (multi-segment arena + far/double-far) | capnp_message (segment arena view, resolve, readers) | capnp_kinds / capnp_pointer (constants + word codecs) ``` -------------------------------- ### capnp/get-text Source: https://github.com/haozeke/capnp-janet/blob/main/README.md Reads a text field from a struct view. Returns the string value. ```APIDOC ## capnp/get-text ### Description Reads a text field from a struct view. ### Method Function ### Endpoint `capnp/get-text` ### Parameters - **struct** (view) - Required - The struct view. - **index** (integer) - Required - The field index. ### Response Returns the string value. ### Example ```janet (print (capnp/get-text root 0)) # hello ``` ``` -------------------------------- ### capnp/set-text Source: https://github.com/haozeke/capnp-janet/blob/main/README.md Sets a text field on a struct view. Accepts an optional schema default as the final argument and stores the required XOR wire value. ```APIDOC ## capnp/set-text ### Description Sets a text field on a struct view. ### Method Function ### Endpoint `capnp/set-text` ### Parameters - **struct** (view) - Required - The struct view to modify. - **index** (integer) - Required - The field index. - **value** (string) - Required - The text value to set. - **default** (string) - Optional - The schema default value. ### Response None. ### Example ```janet (capnp/set-text body 0 "hello") ``` ``` -------------------------------- ### capnp/list-len Source: https://github.com/haozeke/capnp-janet/blob/main/README.md Returns the length of a list view. The list also supports Janet's length protocol, so `length` can be used directly. ```APIDOC ## capnp/list-len ### Description Returns the length of a list view. ### Method Function ### Endpoint `capnp/list-len` ### Parameters - **list** (list view) - Required - The list view. ### Response Returns the number of elements. ### Example ```janet (length people) # 2 ``` ``` -------------------------------- ### capnp/get-u32 Source: https://github.com/haozeke/capnp-janet/blob/main/README.md Reads a 32-bit unsigned integer field from a struct view. Returns the value. ```APIDOC ## capnp/get-u32 ### Description Reads a 32-bit unsigned integer field from a struct view. ### Method Function ### Endpoint `capnp/get-u32` ### Parameters - **struct** (view) - Required - The struct view. - **index** (integer) - Required - The field index. ### Response Returns the integer value. ### Example ```janet (print (capnp/get-u32 root 0)) # 42 ``` ``` -------------------------------- ### capnp/set-u32 Source: https://github.com/haozeke/capnp-janet/blob/main/README.md Sets a 32-bit unsigned integer field on a struct view. Accepts an optional schema default as the final argument and stores the required XOR wire value. ```APIDOC ## capnp/set-u32 ### Description Sets a 32-bit unsigned integer field on a struct view. ### Method Function ### Endpoint `capnp/set-u32` ### Parameters - **struct** (view) - Required - The struct view to modify. - **index** (integer) - Required - The field index. - **value** (integer) - Required - The value to set. - **default** (integer) - Optional - The schema default value. ### Response None. ### Example ```janet (capnp/set-u32 body 0 42) ``` ``` -------------------------------- ### capnp/struct-list-at Source: https://github.com/haozeke/capnp-janet/blob/main/README.md Returns a mutable view of a struct list at a given pointer index in a struct. The list is in the same arena. ```APIDOC ## capnp/struct-list-at ### Description Returns a mutable view of a struct list at a given pointer index in a struct. ### Method Function ### Endpoint `capnp/struct-list-at` ### Parameters - **struct** (view) - Required - The struct view. - **index** (integer) - Required - The pointer field index. ### Response Returns a mutable list view. ### Example ```janet (def lst (capnp/struct-list-at body 0)) ``` ``` -------------------------------- ### Janet: read lists from a decoded message Source: https://github.com/haozeke/capnp-janet/blob/main/README.md Lists from a decoded message carry Janet's length, index, and iteration protocols, so they answer to the same forms as an array. The :verb syntax reaches the same accessors as capnp/ functions. ```janet (import capnp) (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" (:u32 (people 1) 0) # 456 (get people 99) # nil (map |(string (:text $ 0)) people) # @["Alice" "Bob"] (seq [p :in people] (:u32 p 0)) # @[123 456] (each p people (print (:text p 0))) ``` -------------------------------- ### capnp/root Source: https://github.com/haozeke/capnp-janet/blob/main/README.md Returns the root struct of a message. The returned view can be used with getters to read fields. ```APIDOC ## capnp/root ### Description Returns the root struct of a message. ### Method Function ### Endpoint `capnp/root` ### Parameters - **message** (message) - Required - The message object. ### Response Returns a struct view for the root. ### Example ```janet (def root (capnp/root msg)) ``` ``` -------------------------------- ### capnp_get_text Source: https://github.com/haozeke/capnp-janet/blob/main/README.md Reads a text field from a struct pointer. Returns the string and its length. ```APIDOC ## capnp_get_text ### Description Reads a text field from a struct pointer. ### Method Function ### Endpoint `capnp_get_text` ### Parameters - **root** (capnp_ptr_t*) - Required - The struct pointer. - **ptr** (uint32_t) - Required - The field index. - **s** (const char**) - Required - Output pointer to the text string. - **n** (size_t*) - Required - Output pointer to the length. ### Response Returns an error code (0 on success). ### Example ```c const char *s; size_t n; capnp_get_text(&root, 0, &s, &n); ``` ``` -------------------------------- ### capnp_message_free Source: https://github.com/haozeke/capnp-janet/blob/main/README.md Frees the resources associated with a message struct. Must be called when done with the message. ```APIDOC ## capnp_message_free ### Description Frees the resources associated with a message struct. ### Method Function ### Endpoint `capnp_message_free` ### Parameters - **msg** (capnp_message_t*) - Required - The message struct. ### Response None. ### Example ```c capnp_message_free(&msg); ``` ```