comparison src/capnproto-git-20161025/doc/otherlang.md @ 48:9530b331f8c1

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