1

I am trying to make a method that evaluates a string and returns a LuaFunction object for the function contained in it. These strings will be input by a user so I can't know the names of the functions beforehand. Example string:

function doSomething()
    print('x')
end

I want the LuaFunction to point to doSomething.

I was able to use a regex to capture the name of the function and then use NLua.Lua.GetFunction but that didn't work with functions in functions.

Right now my code uses KeraLua.Lua.LoadString and returns a LuaFunction for the chunk created by LoadString. This sort of works but it means the LuaFunction can't take args.

This answer is similar to what I want, but I can't force the functions to be members of a table like it shows.

VeryGoodDog
  • 335
  • 2
  • 11
  • i do not believe you can get to an otherwise anonymous function, without accessing it through the table. can you accept `sometable.thefucntion` as an input? with the proper you can use that input to get to the function. – Nifim Aug 11 '20 at 13:42
  • they aren't anon functions, I just don't know the name. – VeryGoodDog Aug 11 '20 at 13:43
  • sorry may have misunderstood the question, are you trying to get the name of the function the user has just written, and the input is the whole function? – Nifim Aug 11 '20 at 13:46
  • yeah ok well I mean I need to get the function without the name of the identifier – VeryGoodDog Aug 11 '20 at 13:54
  • is the function definition format enforced? you could define it `doSomething = function()...` with or without a proceeding `local`, you can also define it in a table or even globally `_G['doSomething'] = function()...` are these in-scope for the solution? – Nifim Aug 11 '20 at 13:56
  • if `doSomething` like I have in the question would be accessible `_G` then that would work. – VeryGoodDog Aug 11 '20 at 14:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/219616/discussion-between-verygooddog-and-nifim). – VeryGoodDog Aug 11 '20 at 14:14

1 Answers1

0

You have to use DoString rather than LoadString. LoadString does not execute the code it just compiles it to be run later, so it does not define the function in state. the function must be defined for GetFunction or indexing to work.

If the input string is only a function define then you can do either of these:

Lua state = new Lua();
state.DoString((string)s); //define function in state by executing the input.

LuaFunction f1 = state.GetFunction("doSomething");
f1.Call("f1");

LuaFunction f2 = (LuaFunction)state["doSomething"];
f2.Call("f2");

Example user input:

function doSomething(s)
    print(s)
end

This solution does not cover local functions or functions which are stored in tables.

Nifim
  • 4,758
  • 2
  • 12
  • 31
  • thanks, worked great. I used `NLua.Lua.GetObjectFromPath("_G").Keys` from before and after running the chunk and then did `Enumerable.Except()` to get the stuff that was added, and that means I don't have to know the name before hand! :) – VeryGoodDog Aug 11 '20 at 16:42