annotate src/capnproto-git-20161025/doc/otherlang.md @ 157:570d27da3fb5

Update exclusion list
author Chris Cannam <cannam@all-day-breakfast.com>
date Fri, 25 Jan 2019 13:49:22 +0000
parents 1ac99bfc383d
children
rev   line source
cannam@133 1 ---
cannam@133 2 layout: page
cannam@133 3 title: Other Languages
cannam@133 4 ---
cannam@133 5
cannam@133 6 # Other Languages
cannam@133 7
cannam@133 8 Cap'n Proto's reference implementation is in C++. Implementations in other languages are
cannam@133 9 maintained by respective authors and have not been reviewed by me
cannam@133 10 ([@kentonv](https://github.com/kentonv)). Below are the implementations I'm aware
cannam@133 11 of. Some of these projects are more "ready" than others; please consult each
cannam@133 12 project's documentation for details.
cannam@133 13
cannam@133 14 ##### Serialization + RPC
cannam@133 15
cannam@133 16 * [C++](cxx.html) by [@kentonv](https://github.com/kentonv)
cannam@133 17 * [Erlang](http://ecapnp.astekk.se/) by [@kaos](https://github.com/kaos)
cannam@133 18 * [Go](https://github.com/zombiezen/go-capnproto2) by [@zombiezen](https://github.com/zombiezen) (forked from [@glycerine](https://github.com/glycerine)'s serialization-only version, below)
cannam@133 19 * [Javascript (Node.js only)](https://github.com/kentonv/node-capnp) by [@kentonv](https://github.com/kentonv)
cannam@133 20 * [Python](http://jparyani.github.io/pycapnp/) by [@jparyani](https://github.com/jparyani)
cannam@133 21 * [Rust](https://github.com/dwrensha/capnproto-rust) by [@dwrensha](https://github.com/dwrensha)
cannam@133 22
cannam@133 23 ##### Serialization only
cannam@133 24
cannam@133 25 * [C](https://github.com/opensourcerouting/c-capnproto) by [OpenSourceRouting](https://www.opensourcerouting.org/) / [@eqvinox](https://github.com/eqvinox) (originally by [@jmckaskill](https://github.com/jmckaskill))
cannam@133 26 * [C#](https://github.com/mgravell/capnproto-net) by [@mgravell](https://github.com/mgravell)
cannam@133 27 * [Go](https://github.com/glycerine/go-capnproto) by [@glycerine](https://github.com/glycerine) (originally by [@jmckaskill](https://github.com/jmckaskill))
cannam@133 28 * [Java](https://github.com/dwrensha/capnproto-java/) by [@dwrensha](https://github.com/dwrensha)
cannam@133 29 * [Javascript](https://github.com/popham/capnp-js-base) by [@popham](https://github.com/popham)
cannam@133 30 * [Javascript](https://github.com/jscheid/capnproto-js) (older, abandoned) by [@jscheid](https://github.com/jscheid)
cannam@133 31 * [Lua](https://github.com/cloudflare/lua-capnproto) by [CloudFlare](http://www.cloudflare.com/) / [@calio](https://github.com/calio)
cannam@133 32 * [Nim](https://github.com/zielmicha/capnp.nim) by [@zielmicha](https://github.com/zielmicha)
cannam@133 33 * [OCaml](https://github.com/pelzlpj/capnp-ocaml) by [@pelzlpj](https://github.com/pelzlpj)
cannam@133 34 * [Ruby](https://github.com/cstrahan/capnp-ruby) by [@cstrahan](https://github.com/cstrahan)
cannam@133 35
cannam@133 36 ##### Tools
cannam@133 37
cannam@133 38 These are other misc projects related to Cap'n Proto that are not actually implementations in
cannam@133 39 new languages.
cannam@133 40
cannam@133 41 * [Common Test Framework](https://github.com/kaos/capnp_test) by [@kaos](https://github.com/kaos)
cannam@133 42 * [Sublime Syntax Highlighting](https://github.com/joshuawarner32/capnproto-sublime) by
cannam@133 43 [@joshuawarner32](https://github.com/joshuawarner32)
cannam@133 44 * [Vim Syntax Highlighting](https://github.com/peter-edge/vim-capnp) by [@peter-edge](https://github.com/peter-edge)
cannam@133 45 (originally by [@cstrahan](https://github.com/cstrahan))
cannam@133 46 * [Wireshark Dissector Plugin](https://github.com/kaos/wireshark-plugins) by [@kaos](https://github.com/kaos)
cannam@133 47
cannam@133 48 ## Contribute Your Own!
cannam@133 49
cannam@133 50 We'd like to support many more languages in the future!
cannam@133 51
cannam@133 52 If you'd like to own the implementation of Cap'n Proto in some particular language,
cannam@133 53 [let us know](https://groups.google.com/group/capnproto)!
cannam@133 54
cannam@133 55 **You should e-mail the list _before_ you start hacking.** We don't bite, and we'll probably have
cannam@133 56 useful tips that will save you time. :)
cannam@133 57
cannam@133 58 **Do not implement your own schema parser.** The schema language is more complicated than it
cannam@133 59 looks, and the algorithm to determine offsets of fields is subtle. If you reuse the official
cannam@133 60 parser, you won't risk getting these wrong, and you won't have to spend time keeping your parser
cannam@133 61 up-to-date. In fact, you can still write your code generator in any language you want, using
cannam@133 62 compiler plugins!
cannam@133 63
cannam@133 64 ### How to Write Compiler Plugins
cannam@133 65
cannam@133 66 The Cap'n Proto tool, `capnp`, does not actually know how to generate code. It only parses schemas,
cannam@133 67 then hands the parse tree off to another binary -- known as a "plugin" -- which generates the code.
cannam@133 68 Plugins are independent executables (written in any language) which read a description of the
cannam@133 69 schema from standard input and then generate the necessary code. The description is itself a
cannam@133 70 Cap'n Proto message, defined by
cannam@133 71 [schema.capnp](https://github.com/sandstorm-io/capnproto/blob/master/c%2B%2B/src/capnp/schema.capnp).
cannam@133 72 Specifically, the plugin receives a `CodeGeneratorRequest`, using
cannam@133 73 [standard serialization](encoding.html#serialization-over-a-stream)
cannam@133 74 (not packed). (Note that installing the C++ runtime causes schema.capnp to be placed in
cannam@133 75 `$PREFIX/include/capnp` -- `/usr/local/include/capnp` by default).
cannam@133 76
cannam@133 77 Of course, because the input to a plugin is itself in Cap'n Proto format, if you write your
cannam@133 78 plugin directly in the language you wish to support, you may have a bootstrapping problem: you
cannam@133 79 somehow need to generate code for `schema.capnp` before you write your code generator. Luckily,
cannam@133 80 because of the simplicity of the Cap'n Proto format, it is generally not too hard to do this by
cannam@133 81 hand. Remember that you can use `capnp compile -ocapnp schema.capnp` to get a dump of the sizes
cannam@133 82 and offsets of all structs and fields defined in the file.
cannam@133 83
cannam@133 84 `capnp compile` normally looks for plugins in `$PATH` with the name `capnpc-[language]`, e.g.
cannam@133 85 `capnpc-c++` or `capnpc-capnp`. However, if the language name given on the command line contains
cannam@133 86 a slash character, `capnp` assumes that it is an exact path to the plugin executable, and does not
cannam@133 87 search `$PATH`. Examples:
cannam@133 88
cannam@133 89 # Searches $PATH for executable "capnpc-mylang".
cannam@133 90 capnp compile -o mylang addressbook.capnp
cannam@133 91
cannam@133 92 # Uses plugin executable "myplugin" from the current directory.
cannam@133 93 capnp compile -o ./myplugin addressbook.capnp
cannam@133 94
cannam@133 95 If the user specifies an output directory, the compiler will run the plugin with that directory
cannam@133 96 as the working directory, so you do not need to worry about this.
cannam@133 97
cannam@133 98 For examples of plugins, take a look at
cannam@133 99 [capnpc-capnp](https://github.com/sandstorm-io/capnproto/blob/master/c%2B%2B/src/capnp/compiler/capnpc-capnp.c%2B%2B)
cannam@133 100 or [capnpc-c++](https://github.com/sandstorm-io/capnproto/blob/master/c%2B%2B/src/capnp/compiler/capnpc-c%2B%2B.c%2B%2B).
cannam@133 101
cannam@133 102 ### Supporting Dynamic Languages
cannam@133 103
cannam@133 104 Dynamic languages have no compile step. This makes it difficult to work `capnp compile` into the
cannam@133 105 workflow for such languages. Additionally, dynamic languages are often scripting languages that do
cannam@133 106 not support pointer arithmetic or any reasonably-performant alternative.
cannam@133 107
cannam@133 108 Fortunately, dynamic languages usually have facilities for calling native code. The best way to
cannam@133 109 support Cap'n Proto in a dynamic language, then, is to wrap the C++ library, in particular the
cannam@133 110 [C++ dynamic API](cxx.html#dynamic-reflection). This way you get reasonable performance while
cannam@133 111 still avoiding the need to generate any code specific to each schema.
cannam@133 112
cannam@133 113 To parse the schema files, use the `capnp::SchemaParser` class (defined in `capnp/schema-parser.h`).
cannam@133 114 This way, schemas are loaded at the same time as all the rest of the program's code -- at startup.
cannam@133 115 An advanced implementation might consider caching the compiled schemas in binary format, then
cannam@133 116 loading the cached version using `capnp::SchemaLoader`, similar to the way e.g. Python caches
cannam@133 117 compiled source files as `.pyc` bytecode, but that's up to you.
cannam@133 118
cannam@133 119 ### Testing Your Implementation
cannam@133 120
cannam@133 121 The easiest way to test that you've implemented the spec correctly is to use the `capnp` tool
cannam@133 122 to [encode](capnp-tool.html#encoding-messages) test inputs and
cannam@133 123 [decode](capnp-tool.html#decoding-messages) outputs.