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 ?

6
  • 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? Commented Oct 7, 2015 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 Commented Oct 7, 2015 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. Commented Oct 7, 2015 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 ? Commented Oct 7, 2015 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. Commented Oct 8, 2015 at 13:39

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 ?
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.