Reputation: 21335
I am using the C Fuzzy API and I want to load function module contained in a file lets say mycalculator.lua. This seem to run fine however when I later try to run another file A.lua that requires 'mycalculator' it does not work unless the mycalculator.lua file is available on the file system to reload. I am trying to just load it into the system and then have it available without having the mycalculator.lua in the file system. It there any way to have lua system keep the definition without loading it again? Basically I convert the mycalculator.lua into a string and then run it. I don't want to put mycalculator.lua file into the file system, I just want to hand it over as a string and then be able to require it in the next string I pass to the stack Thanks
Upvotes: 1
Views: 1093
Reputation: 473447
There is a difference between simply executing a Lua script and loading a Lua module. If you wish to load a Lua module, then you must actually load a Lua module exactly as a script would: by calling require
.
Since you appear to be new to Lua, I should probably explain this. You've probably seen code like this in Lua scripts:
require 'mycalculator'
That is not some special statement to Lua. That is a function call. It is just some syntactic sugar for:
require('mycalculator')
Functions in Lua can be called with NAME VALUE
syntax instead of NAME(...)
syntax, but it only allows you to send one parameter. And the parameter must be a literal (or table constructor).
In order to call the Lua require
function from C, you must use the Lua stack. You must fetch the function from the global table by using lua_getfield(L, LUA_GLOBALSINDEX, "require");
Then, you push a string onto the stack containing the name of the module to load. Then, you use lua_pcall
or whatever Lua function calling function to call it.
Upvotes: 2