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

Get caller environment

Started by Sewbacca, 01 August 2016 - 03:36 PM
Sewbacca #1
Posted 01 August 2016 - 05:36 PM
I want to achieve this without arguments, is this possible?
Example:

print(APIname) --> nil
API.name('A text')
print(APIname) --> 'A text'
or

print(API) --> nil
using('API')
print(API) --> table: xxxxx
So I could do this:

tEnv.APIname = sText -- tEnv is the Environment of the caller, sText is the argument
or

tEnv[sKey] = tAPIs[sKey] -- tEnv is the Environment of the caller, sKey is the argument, tAPIs are internal APIs
I need it also for identification: 'Who call me?
–> You get this
–> And you get this2'
Edited on 01 August 2016 - 05:23 PM
KingofGamesYami #2
Posted 01 August 2016 - 05:46 PM
You want the API to know it's own name? Or do you want the environment of the caller? The former seems to fit with your example; the latter does not.
Sewbacca #3
Posted 01 August 2016 - 07:18 PM
You want the API to know it's own name? Or do you want the environment of the caller? The former seems to fit with your example; the latter does not.

I updated the post to specify my goal.
KingofGamesYami #4
Posted 01 August 2016 - 07:36 PM
You can kind of do this using getfenv( nStackLevel ). For example,


local function something()
  print( getfenv( 1 ).i )
end
something() --# nil
i = 1
something() --# 1

As for "who called it", you can't know that. The information literally isn't there.
Sewbacca #5
Posted 01 August 2016 - 07:47 PM
You can kind of do this using getfenv( nStackLevel ). For example,


local function something()
  print( getfenv( 1 ).i )
end
something() --# nil
i = 1
something() --# 1

As for "who called it", you can't know that. The information literally isn't there.

I think, I have to change the working wise of my program…
valithor #6
Posted 01 August 2016 - 10:54 PM
As for "who called it", you can't know that. The information literally isn't there.

Luckily a lot of the built in functions that run files set a name for the file when it is loadstringed, and that is fairly easy to obtain. If I understand correctly he should be able to get the who called me information by pcalling error with a certain level to error at. He would then have to find the information he wants out of the error message. I would give a example, but I don't have much time right now.
Sewbacca #7
Posted 02 August 2016 - 12:06 PM
As for "who called it", you can't know that. The information literally isn't there.

Luckily a lot of the built in functions that run files set a name for the file when it is loadstringed, and that is fairly easy to obtain. If I understand correctly he should be able to get the who called me information by pcalling error with a certain level to error at. He would then have to find the information he wants out of the error message. I would give a example, but I don't have much time right now.

I think that would be bad code, but the solution with the top-level is the best one.