How to use a dynamic library with Defold?

I think Ive done something like this before (with a C dll lib). However, there are problems going this route.
The Defold native extension is compiled with specific build settings - make sure yours are matching and that you have the right import name mangling otherwise it wont work.

Other problems you will run into:

  • On windows, the security system may stop access to the dll. The search path is always the exe run path, and system paths. You can add your dll path to it, or just make sure it outputs to the exe path.
  • On other systems pathing is also important, and OSX, IOS, Android and Linux all have their own nuances when loading so’s. Make sure you have both security access and that its in the right place.
  • Dependencies - if your dll has non-system dependencies, make sure they are included in your path. On Windows this can especially be a pain.
  • Name mangling. Make sure you are using CDECL or STDCALL exports for your methods or they wont be accessible.
  • When building your rust dll make sure it is linked against the same runtime libs as the extension uses. You cant use vanilla Lua or Luajit because Defolds Lua is a modified Luajit/Lua runtime.

Another way around this, if you are not using html platforms (and Android?), you can use Luajit FFI directly (alot easier in this case). This is where you do something like:

ffi.load("mydll.dll")  -- note: change this dep on platform
ffi.cdef[[
       int luaopen_rust_module(lua_State* L);
]]

ffi.C.luaopen_rust_module(mystate)

Have a look at: FFI Library
Ask me if you have any specific q’s about it :slight_smile: Have done a little bit with luajit ffi.

However you cant really pass lua_State to this method from Luajit itself, since you would need a lua_State type that matches as a userdata. And I think this will be your main problem here. Note: you could make a getter that fetches it. Youd still need to build the dll against the Defold lua/luajit libs.

Whats the aim of getting the lua_State into rust? If you want to do that, it would be better to build a more direct compilation of the library (static) as you mention.

I should also note. Lua extensions method calling has performance penalties - the stack system for traditional Lua calling is very very slow. Any benefits of Rust perf wise will be limited by this. If you use the FFI route, there is no callthrough overhead, so you dont lose any perf, and in fact the LuaJit can often optimize the calling behavior to be even faster than normal C code :slight_smile:

The main drawbacks with FFI are you need to have per platform implementation of the interfaces, and there are some limitations on platform use (I think its html and Android - that use pure Lua).

Im guessing you want to make a Rust language interface (hence the dll). Probably best examine the Fennel and C# projects for more information about how they did their integrations?

Have a look in the forums, there are a couple of others too (that I cant remember from the top of my head).

1 Like