This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Minesoft's profile picture

Passing an object to a file

Started by Minesoft, 14 March 2013 - 06:38 AM
Minesoft #1
Posted 14 March 2013 - 07:38 AM
Hi! I ran into this problem, which show an example:
loader.lua:

local api = {};
api.ololoFunc = function() {
term.write("hello");
}
local func = loadfile("ololo.lua");

– function to add object named "api" into file

local cor = coroutine.create(func);
coroutine.resume(cor);



ololo.lua:

api:ololoFunc();

What I need to write instead "– function to add object named "api" into file"

Sorry for my bad English
MysticT #2
Posted 14 March 2013 - 07:59 AM
Is that the actual code or just an example?
It looks like you're using java (or similar) syntax. Remove the {} from the function definition, and the ; at the end of every line (although that's optional).
Anyway, I'm not sure what you want, I think you are trying to use the "api" table from another program. If that's the case, there's a few ways to do it.
The easiest would be to save the api to a file, and then use os.loadAPI, like this:
API file:

function someApiFunction()
  -- do something here
end
Program:

os.loadAPI("myApi")
myApi.someApiFunction()

But if you want to keep the api as a table in the program, you can use os.run:

local myApi = {}
function api.someFunc()
  -- some code here
end

local env = {
api = myApi
}
setmetatable(env, { __index = getfenv() }) -- this is so you can use other apis and global functions

os.run(env, "someProgram") -- you can also pass arguments for the program if you want
then, in the other program, you can use that "api".
JokerRH #3
Posted 14 March 2013 - 08:01 AM
#1 In lua "{" and "}" are for tables, not for functions.

#2

local api = {}
api.o|o|oFunc = function()
  write("Hello")
end

local func= loadFile("asdf")
local env = setmetatable(api, {__index = _G}) --Use setmetatable to ensure that you can access the global enviroment for functions like print

setfenv(func, env)
local cor = coroutine.create(func)
coroutine.resume(cor)

#3 I wouldn't recommend using coroutines, because then you cannot use pullEvent, rednet.receive or sleep (They all yield the coroutine)
You would have to write your own coroutine handler. Not that bad if you know how, but I don't know if you want to do that… :D/>

#4 This will not insert the api's functions into the global enviroment, but it's really simple…

Edit: Typed too slow…Again…:D/>
JokerRH #4
Posted 14 March 2013 - 08:09 AM

local env = {
api = myApi
}
[...]
setmetatable(env, {__index = getfenv()})

why not

setmetatable(myAPI, {__Index = getfenv()})

and 2. getfenv returns the current function enviroment, or any function down the stack, but as you don't know wich one I have no idea how you think it'll work… :P/>
Minesoft #5
Posted 14 March 2013 - 08:17 AM
For diferent files i would add different objects(tables)
I wouldnt to insert the api's functions into the global enviroment
MysticT #6
Posted 14 March 2013 - 08:39 AM
why not

setmetatable(myAPI, {__Index = getfenv()})
Well, that way you will access the functions in the api table as global functions, not as an api.

For diferent files i would add different objects(tables)
I wouldnt to insert the api's functions into the global enviroment
Using the os.run way will do what you want. There's other ways, but that's the easier one.
JokerRH #7
Posted 14 March 2013 - 09:50 AM
Well, that way you will access the functions in the api table as global functions, not as an api.
But thats exactly what he wants to do… Otherwise there is no need to do that, because he could simply type api.func (That's the result of your code, too :P/>)

Using the os.run way will do what you want. There's other ways, but that's the easier one.
Might be the easier one, but you'll have the native functions of the api as a metatable, so you can't look them up with pairs or anything like that…Practically the funciton enviroment would then become an extention…