LuaJ loading two functions with the same name from two different LuaScripts - lua

I have two Lua Scripts containing functions with the same name:
luaScriptA:
function init()
print( 'This function was run from Script A' )
end
luaScriptB:
function init()
print( 'This function was run from Script B' )
end
I would like to load both these functions using LuaJ into the globals environnment, for one script I usually do it as follows:
LuaValue chunk = globals.load(new FileInputStream(luaScriptA), scriptName, "t",
globals);
chunk.call();
This will load the function init() into globals and I can execute this function from java with:
globals.get("init").call();
The problem comes in when I load a second script, this will overwrite all functions with the same name previously declared. Is there any way I can prevent this and easily distinguish between the two functions? For example something like:
globals.get("luaScriptA").get("init").call(); //Access the init function of script A
globals.get("luaScriptB").get("init").call(); //Access the init function of script B
Please note that the script contains other functions as well and my goal is to run individual functions within the script, not the complete script at once.Working on the JME platform.

Put your functions in a table
luaScriptA:
A = {} -- "module"
function A.init()
print( 'This function was run from Script A' )
end
luaScriptB:
B = {} -- "module"
function B.init()
print( 'This function was run from Script B' )
end
Then you would do
globals.get("A").get("init").call();
globals.get("B").get("init").call();

The code below loads scripts in their own environment, which inherits from the global one for reading but not for writing. In other words, you can call print but each defines its own init. You'll probably have to do something to use it in LuaJ, but I don't know what.
local function myload(f)
local t=setmetatable({},{__index=_G})
assert(loadfile(f,nil,t))()
return t
end
local A=myload("luaScriptA") A.init()
local B=myload("luaScriptA") B.init()

Related

Use curried lua function as custom complete list in neovim

I'm trying to provide a curried lua function as method to call for autocomplete a command. However, it sometimes does nothing and sometimes fails with a memory segmentation error.
This is a minimal reproduction example:
Given this init.lua
vim.cmd(string.format([[set rtp=%s]], vim.fn.getcwd()))
local custom_complete = [[customlist,v:lua.require'split_later'.split_later('a b')]]
local options = { complete = custom_complete, desc = "Test to break", nargs = 1, force = true }
vim.api.nvim_create_user_command("Test", "echo <args>", options)
And this file called split_later under the correct lua/ subfolder:
local M = {}
function M.split_later(str)
print("called split")
return function()
return vim.fn.split(str)
end
end
return M
The idea is to have a generic utility that is able to produce auto-complete functions from lua, but it is not working as expected.
I found a workaround using a lookup table that stores the command name and then uses it, but I don't like it that much.

How to run a string and get a table that has all the variables that the code created in lua?

So i'm making my own OS for LIKO-12 and I need to run .lk12 files but i need to have a function to run a string as code and return a table that has all of the variables that code created
Example:
function foo()
return "Hello,"
end
function bar()
return " World!"
end
hello = foo()..bar()
should return a table with 2 functions:
A function called foo that returns "Hello,"
A function called bar that returns " World!"
and a variable called hello that has "Hello, World!"
You should be able to call a function from the code using
vars["foo"]() or vars.foo()
Can anyone help me?
When you create or use a global variable it's actually stored in a table called "the environment", or _ENV (which is not global, it's a local that you get automatically)
So your code:
function foo()
return "Hello,"
end
function bar()
return " World!"
end
hello = foo()..bar()
is really doing:
function _ENV.foo()
return "Hello,"
end
function _ENV.bar()
return " World!"
end
_ENV.hello = _ENV.foo().._ENV.bar()
so if we just set _ENV to a new table, and return it, then this is exactly the table you want. You can do this with load (which is how you run strings) by passing it as the 4th argument. This function will do that:
function loadAndReturnEnv(codeString)
local env = {}
load(codeString, nil, nil, env)() -- error checking skipped for demonstration
return env
end
However, note that all the usual global stuff like string.rep, table.sort, load, print don't exist in this new _ENV. If that's what you want, that's great. But I guess you probably do want them. In that case we can use the metatable __index feature, so that if the code looks for something in its _ENV and it's not there, it looks in our (the caller's) _ENV instead.
function loadAndReturnEnvWithGlobals(codeString)
-- note: env is the environment for the code we're about to run
-- (which will be called _ENV in that code), and _ENV is the environment
-- of the loadAndReturnEnvWithGlobals function (i.e. the "real" environment)
local env = {}
setmetatable(env, {__index=_ENV})
load(codeString, nil, nil, env)() -- error checking skipped for demonstration
setmetatable(env, nil) -- set the table back to normal before we return it
return env
end
But wait... the program can now call print when you run it, but if it defines a function, that function can't call print since we deleted the link back to the real environment after the code returned. So I think the best way to fix that is to leave the environment linked, and just copy the functions etc.
function loadAndReturnEnvWithGlobals2(codeString)
local env = {}
setmetatable(env, {__index=_ENV})
load(codeString, nil, nil, env)() -- error checking skipped for demonstration
local result = {}
for k,v in pairs(env) do result[k]=v end
return result
end
although this copies the values, so if you have any functions that change the variables, you won't see the changed variables.
Which version you use is up to you. They each have pros and cons.

How to make global variables "immutable" in Lua/LuaJ?

Description
I'm doing a LuaJ program, and here's a lib script like this:
function foo()
print("foo");
end
I want the foo function can be invoked in other scripts directly (no require), but performs immutable in different scripts. ( Even a script overrides it, it performs as the original way in other scripts. )
For example, here's the script 1:
foo = function()
print("bar");
end
and here is the script 2:
foo();
What's done
I have saw these two questions. They do work but not the solution to this problem.
LuaJ How to avoid overriding existing entries in Globals table
Preventing Function Overriding in Lua Table
Making global environment access-only (Lua)
I tried loading lib every time exec a script, or set local _ENV, but because there may be further callbacks from Java to Lua, it doesn't work correctly.
I now handle it by create a Globals and load lib script every time load a script in Java like this:
public static void main(String[] args) {
loadAndCallViaDifferentEnv(libPath, script1);
loadAndCallViaDifferentEnv(libPath, script2);
}
static void loadAndCallViaDifferentEnv(String libPath, String scriptPath) {
Globals globals = JsePlatform.standardGlobals();
globals.loadfile(libPath).call();
globals.loadfile(scriptPath).call();
}
It works well, but costs much. Is there a better way?
I assume you want to protect three functions from overwriting: foo1, foo2 and print
-- define foo1 and foo2 inside protected table instead of as usual globals
local protected = {}
function protected.foo1()
print("foo1");
end
function protected.foo2()
print("foo2");
end
-- if a function to be protected already exists, remove it from globals:
protected.print = print
print = nil
-- Now set the metatable on globals
setmetatable(_G, {
__index = protected,
__newindex =
function(t, k, v)
if not protected[k] then
rawset(t, k, v)
end
end
})
Now you can invoke foo1, foo2 and print from other modules without require, but you can not overwrite them:
-- the script 1:
foo1 = function()
print("bar");
end
foo1(); -- original protected.foo1() is invoked
-- the script 2:
foo1(); -- original protected.foo1() is invoked

Lua (require) invoke an not intended print of required file name

When require is called in testt.lua which is one of two files the return is movee and movee.lua.
movee are for the most part a class to be required, but should be able to accept to be called direct with parameter.
movee.lua
local lib = {} --this is class array
function lib.moveAround( ... )
for i,direction in ipairs(arg) do
print(direction)
end
end
function lib.hello()
print("Hello water jump")
end
lib.moveAround(...)
return lib
testt.la
local move = require("movee")
Expected result is not to call lib.moveAround or print of file name when require is called.
Your expectations are incorrect. Lua, and most scripting languages for that matter, does not recognize much of a distinction between including a module and executing the Lua file which provides that module. Every function statement is a statement whose execution creates a function object. Until those statements are executed, those functions don't exist. Same goes for your local lib = {}. And so on.
Now, if you want to make a distinction between when a user tries to require your script as a module and when a user tries to execute your script on the command line (or via just loadfile or similar), then I would suggest doing the following.
Check the number of arguments the script was given. If no arguments were given, then your script was probably required, so don't do the stuff you don't want to do when the user requires your script:
local nargs = select("#", ...)
if(nargs > 0) then
lib.moveAround(...)
end
Solved by replacing
lib.moveAround(...)
with
local argument = {...}
if argument[1] ~= "movee" and argument[2] ~= "movee" then
lib.moveAround(...)
end
require("movee")
will execute the code within movee.lua
lib.moveAround(...)
is part of that code. Hence if you require "movee" you call lib.moveAround
If the expected result is not to call it, remove that line from your code or don't require that file.

How to run multiple instances of a lua script from C

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

Resources