1
votes

I'm trying to embed Lua 5.2 into my C program.

I want to let the Lua script to be able to require and load another script. How to do it?

Suppose the Lua part has such file structure:

lua_script
  - main.lua
  + utils
    - custom_loader.lua
  + globals
    - scene_globals.lua
    - scene_levels.lua

The main.lua will try to require and import functions from custom_loader.lua, and so on...

Is it possible to do this without writing a kind-of wrapper in C? Can the Lua script automagically just load everything it needs?

(Ps. I don't really need sandboxing for now, so it's okay for the script to do what it want.)

1
I think require is just a function that you can define as you please. - Kerrek SB
@KerrekSB so, I should just push a C function as require and do luaL_loadfile() for every files that the script want? - T.N.
Yes, one possibility is to write your own searcher system that resolves require calls. But there may be simpler solutions using built-in facilities. - Kerrek SB
require('lua_script.utils.custom_loader') - FareakyGnome

1 Answers

2
votes

As decribed by the Lua require man page, it searches for the file in a path.

This path can be defined in C. Have a look to this post : "Setting the global LUA_PATH variable from C++/C"

The require function is very pratical to load modules and libraries defined in .lua files.