BerryBots forums

Importing/Require external libraries
Page 1 of 1

Author:  sadd [ Thu Jun 20, 2019 7:01 pm ]
Post subject:  Importing/Require external libraries

Hi, I just tweeted the following to Voidious, but I thought I put it here as well for the future:

Hi! Love Berrybots a ton, but I'm kinda stuck and I need an external library (probably numlua) to make things work. Any way to load it in the sandbox?

So the problem is that I think I have some pretty nifty ideas for my bots, but since lua lacks efficient numerical tools in the base version I want to import a numeric library (probably numlua). Since the bots are sandboxed though I'm having trouble to find a way to do this, even when I'm modifying the berrybot source code.

The standard libraries are loaded in bblua.cpp with luaL_openlibs(*shipState);, but I couldn't literally find any easy way to just import a lua library there. luaL_register(...) exists, but then I have to somehow manually register the C functions of numlua I guess?

Any thoughts on this? I'm new to lua unfortunately...

Author:  sadd [ Sun Jun 23, 2019 9:39 pm ]
Post subject:  Re: Importing/Require external libraries

By now I think I understand fairly well how one can use C libraries in lua (with ffi or luaopen), but I can't get numlua to work still. I think I compiled everything correctly (dependencies etc., including luajit of berrybot), but I still get errors.

I actually think GSL ( might be a more fitting choice anyway, and gsl-shell ( basically does the same I would like to do. And my first tests are succesfull in contrast to numlua.

Unfortunately there isn't a way to do this without modifying berrybots, and I'm wondering if it actually makes any sense to do this. I hope someone will actually read this, since I actually love berrybots and think with a change like this (and 2-3 more tweaks) it would be pretty much perfect.


Author:  sadd [ Thu Jul 11, 2019 11:35 pm ]
Post subject:  Re: Importing/Require external libraries

So here is an example how one can use the luajit ffi to use external c functions, in this case self written. I'm obviously on linux btw.

In the bot *.lua file I have

void setVectorZero(double* vec, int nn);
local hl = ffi.load("./bots/myBots/")
local cvec ="double[?]", 100)
helpers.setVectorZero(cvec, 100)

Note that the path in ffi.load is relative to the berrybot executable.
The is a self written dynamic library from the c file

void setVectorZero(double* vec, int nn) {
  for (int ii = 0; ii < nn; ii++) {
    vec[ii] = 0.;

which was just compiled by "gcc -fPIC -shared -o helperlib.c". It's obviously just a stupid example, but anyway.

Some notes:
FFI is enabled by berrybots by default, which I do like, since doing some stuff in C is helpful, even when we're not talking about using external libraries.

FFI can be used to load any dynamic library on the system. Which I personally like as well, even if only for testing stuff with existing libraries inside berrybot (e.g. kdtrees or something).
BUT the problem is that my understanding is that berrybots tries to be "secure" in that regard. I don't see any restrictions on loading iostream for example and manipulating files on the disk. Whether one compiles the shared library oneself or not doesn't really matter, at least if one compiles the helper library oneself, then no other libraries have to be linked, but as I said that doesn't include iostream...

It's already pretty late here, but I just wanted to write stuff down so I don't forget.


Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group