3

I have the following lua script :

mydata={}
function update(val)
    mydata["x"] = val
    if (val == 10)
      -- Call C-Api(1)
    else
       --Register callback with C when free or some event happens
       register_callback(callme)
end
function callme()

end

Basically I would like to have two instances of this script running in my C program/process with out having to create a new LUA state per script. And I want to call the function update() with val = 10 from the one instance and call the function update() with val = 20 from another instance. From the second instance it registers a callback function and just waits to be called.

Basically the script file is kind of a RULE that i am trying to achieve. Several events on the system can trigger this rule or script file. I would like to process this rule as per the event that triggered it. There could be multiple events triggering this script to run at the same time. So I need to have multiple instances of this script running differentiated by the kind of event that triggered it.

To Summarize, I want each caller to have separate individual instance of mydata

I would like to achieve something like this. I read some where that we should be able to run multiple instances of a lua script with out having to create a new lua instance by loading a new environment before loading the script

But I am not able to find the exact details.

Could some body help ?

P K
  • 33
  • 4
  • That script just creates a table and defines a function. You don't need to run that more than once. And you don't want to or the second run will destroy the table from the original run. You can call that function from anything else that runs in the lua state where that was run. If you need two threads doing that at the same time then you need to synchronize their access to the lua state. What **exactly** are you trying to do here? – Etan Reisner Oct 07 '15 at 18:36
  • Thanks for your inputs. The table that I mentioned was just a sample example. Basically I am trying to create a lua script file. This file acts as a RULE meaning it dictates how things should work. In my big system there are some events that happens and triggers this rule. meaning i need to run this RULE script against that event. So there could be multiple events that trigger this same rule script at same time. So that means i need to have multiple instances of this lua rule script running per event that triggered it. Lets say MyData.["x"] = event-id – P K Oct 07 '15 at 18:48
  • If you want `mydata` to be shareable, then you want one instance. If you want to use the same global name but not be shared, create more than one Lua state. If you don't need it to be shareable, then just make it local. The actual problem you're trying to solve is very unclear. – Mud Oct 07 '15 at 20:06
  • Thanks Mud. But I do not want to share mydata and also I do not want to create more than one lua state. Is it possible to achieve this ? To be able to run the same script running with multiple instances and each having a different copy of mydata ? – P K Oct 07 '15 at 20:15
  • Calling that script multiple times will *never* do what you want. If you mean you want to be able to call the `update` function multiple times that's a different question. If that is what you mean do you want the `mydata` shared between callers (how do you define a caller)? Do you want individual `mydata` tables for each caller (again how do you define a caller)? Do your callers (whatever they are) need to share anything in the lua state? Because if not, and you want to thread the callers or anything then multiple lua states **is** what you want. You **really** need to clarify your question. – Etan Reisner Oct 08 '15 at 13:39
  • Ok Etan thanks for the followup. I want to invoke update function multiple times from different callers. I want each caller to have individual mydata tables. The callers need not share anything in the lua state and also they need not share mydata as well. The caller is a 'C' Process. It needs to call this update function several times each with a different context/values. Is there a way to do it with out having to create lua state for each caller ? Because this script is kind of a blue print for "What to do" when event 'X,Y...Z' happens in the C process... – P K Oct 08 '15 at 18:07

1 Answers1

1

While I'm still not sure what exactly you are trying to achieve, but if you want to have two instances of the same function that keep the data they use private, you just need to create a closure and return an anonymous function that your C code will use.

Something like this should work:

function createenv(callme)
  local mydata={}
  return function (val) -- return anonymous function
    mydata["x"] = val
    if (val == 10)
      -- Call C-Api(1)
    else
       --Register callback with C when free or some event happens
       register_callback(callme)
    end
  end
end

Now in one part of your (C or Lua) code you can do:

local update = createenv(function() --[[do whatever]] end)
update(10)

And then in another part you can do:

local update = createenv(function() --[[do something else]] end)
update(20)

And they should have nothing in common between each other. Note that they still shared the same Lua state, but their instances of mydata will be independent of each other.

Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56
  • Thanks Paul for your answer. This is what I am looking for. But I need one more thing. I would like to have more than one anonymous function in the closure. Is it possible. Meaning I would like to have like function1 and function2 inside createenv() ? Is it allowed ? – P K Oct 09 '15 at 18:34
  • @PK, sure; you can return multiple functions if you want to: `return function() --[[func1]] end, function() --[[func2]] end`. You can pass multiple functions to use inside the environment as well. – Paul Kulchenko Oct 09 '15 at 18:59