Terra: A brief review

A link that’s gone past me a few times recently is to Terra, which is “a new low-level system programming language that is designed to interoperate seamlessly with the Lua programming language”.

It looks pretty interesting. I think it perhaps make more sense to regard it as a nice library for generating C programs from Lua than as an entirely new language, but that’s both slightly splitting hairs and still pretty exciting in its own right.

Anyway, I occasionally will write low level data processing code in C, and one thing that’s always annoying there is the lack of templated container types (yes, yes, I know I could use C++. But I don’ wanna), so I thought I’ve have a go at writing one in Terra.

In accordance with my observation that I implement a hash table about every 6 months (it’s true, seriously. I don’t know why or how it happens, but if I don’t set out to do it then circumstances force me to), I decided to have a try at implementing one in Terra as a way to get a feel for the language.

Here’s the result. Be warned that it’s pretty far from a work of art – it’s the result of maybe an hour of casual hacking.

It’s a very dumb hash table, but it does the job.

What has the experience or writing it told me about Terra?

A couple things.

As a concept, it has a lot going for it. I really liked how easy it was to integrate C – you can just import C headers and, if necessary, you can inline and compile C code right on the fly (Note in the example I just embedded a C hash function. Why? Well because I couldn’t find the bitwise operators in terra and thought it would be nice to try embedding the C one as a test case). As far as FFIs go, this is a pretty damn cool one.

In terms of templating and composability? It honestly worked exactly well as promised. A++ would use again.

In terms of ease of writing? Well…

The lua bits were lua. It’s a perfectly passable language. If you haven’t used it, imagine javascript but slightly better designed. It has some quirks, but nothing worth complaining about.

The terra bits felt awfully like writing C. That’s kinda the point.

But here’s the thing. Writing C is terrifying. There’s a lot that can go wrong.

The thing that makes writing C not terrifying is that there are good tools for dealing with most of those things that can go wrong. In particular, extensive compiler warnings for static analysis and valgrind for dynamic analysis.

Terra has none of the former and appears to break the latter. As with most languages with garbage collection it’s hard to run the main lua script under valgrind, and when emitting a pure terra executable my first non-trivial terra program seems to have encountered a bug in Terra’s interaction with Valgrind (it’s possible the bug is in Valgrind rather than Terra, but that wouldn’t be my first guess).

Edit: Actually it turns out the problem is that Terra is using AVX instructions which the version of valgrind I had installed doesn’t support. Upgrading valgrind fixes this, which makes me feel much better about using Terra.

Additionally, there are a couple things that cause me to think it’s probably a lot easier to trigger these problems in Terra than it is in C. One thing that bugs me is that the mix of one based indexing (which lua uses) and zero based indexing (which terra uses) is basically an invitation to buffer overflows and off by one errors. Terra also seems very cavalier about the difference between values and pointers to values, which I’m not totally comfortable with but expect I’d get used to.

None of this is damning, but it’s enough to give me pause. It’s a lovely idea, and I hope it does well and improves, and I may well use it for some personal experiments, but right now the idea of writing anything that might be required to have any non-trivial security property in Terra would fill me with dread, which I think rather limits its use cases for me. If this isn’t something you care about, or if you have specific use cases which don’t need it, I’d definitely encourage you to check it out.

This entry was posted in programming on by .

2 thoughts on “Terra: A brief review

  1. Jonathan Graehl

    printed = printed + 1

    lua/terra don’t even give us python’s += ?

    good point about valgrind, which has enabled far larger successful C/C++ projects than would otherwise be possible

    1. david Post author

      Indeed, Lua lacks all the +=, *=, etc. operators. It’s one of those annoying quirks I mentioned that isn’t really worth complaining about. :-)

Comments are closed.