Search code examples
lua

is it possible to call require from C


I have a module compiled in a shared object (I followed the library part of this article https://chsasank.github.io/lua-c-wrapping.html) and I want to load it from C not from the interpreter.

Is it possible ? If so how to do it ?


Solution

  • Yes, it's possible, as require is a function stored in a global environment. Lua does the same in standalone interpreter when it needs to process the -l option, see the dolibrary function.

    You do this the same way as with any other global function - in simplest case calling lua_getglobal(), then pushing the name of the file to require, and calling lua_call/lua_pcall/whatever.