5

I am using LuaJava and C Code for Lua. What I am trying to do is read Lua source stored as a resource String in an Android application so that the Lua source read in can be executed. I need to know how to do this using LuaJava or C language.

I want to know how I can create a Lua module in Lua by using a String.

In other words, I am storing Lua source that would be stored in a .lua file in a String instead. I then want to load the contents of this string into Lua as an available module that can be called.

I see there is a loadstring() function but not sure how this would be invoked for LuaJava or C.

I don't want Lua to search the file system for this file, I will find the file and convert it to a string. After I have the string, I need to know how to load the string copy of the file contents into Lua as a module that I can then call.

I also want to know if after calling loadstring(s) if the module will remain available for subsequent function calls without having to reload do the loadstring() again.

Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
Androider
  • 21,125
  • 36
  • 99
  • 158

2 Answers2

3

If you need to load/compile a string from LuaJava, you can use the function LuaState.LloadString(String source).

If you do not want to load a "module" from source multiple times, you have to assign it a name and save some flag in a table. You can even provide "unloading" so that you could load a module from source again. It could be reimplemented in Lua as follows:

do
  local loadedModules = {} -- local so it won't leak to _G
  function loadModule(name, source)
    if loadedModules[name] then return loadedModules[name] end
    loadedModules[name] = assert(loadstring(source))() or true
  end
  function unloadModule(name)
    loadedModules[name] = nil
  end
end
Michal Kottman
  • 16,375
  • 3
  • 47
  • 62
  • Thanks. I have another issue which is calling a Lua function from java after it has been loaded and getting a result value back. Please take a look at this: http://stackoverflow.com/questions/8611948/lua-getting-result-back-in-luajava-from-lua-function-call – Androider Dec 23 '11 at 04:54
1

I'm not sure I understand the question but here goes:

local f = io.open(filename)
local s = f:read '*a' -- read the entire contents of the file
f:close()
assert(loadstring(s))() -- parse and run string `s` as Lua code
Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
  • 1
    I'm fairly sure he's talking about in the host language, not in Lua. – Nicol Bolas Dec 22 '11 at 06:24
  • 1
    yes, what I am exactly trying to do is read the Lua file as a resource String in an Android application. So Lua will never read a file but only take in a String. So I guess I need to know how to run loadstring(s) from LuaJava or C language. – Androider Dec 22 '11 at 07:32
  • Moreover I need to confirm that after you run the code above you have above, then subsequent lua code can do a require 'module_defined_in_filename' successfully without looking for that module in a file. – Androider Dec 22 '11 at 07:35
  • 1
    So essentially I am trying to drive all Lua inputs of custom or additional modules from Strings rather than files. So I need to know if there was a module in filename above would it be available in a subsequent require 'module_name' after running the loadstring(s) without Lua looking for to reload from a file. Thanks – Androider Dec 22 '11 at 07:38
  • Or is it that after loadstring(s) all the functions are available for use to the next Lua function call, and so there is no need to do a require at this point? – Androider Dec 22 '11 at 07:47