Jump to content




Get caller environment


  • You cannot reply to this topic
6 replies to this topic

#1 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 01 August 2016 - 04: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 by Sewbacca, 01 August 2016 - 06:23 PM.


#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 01 August 2016 - 04: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.

#3 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 01 August 2016 - 06:18 PM

View PostKingofGamesYami, on 01 August 2016 - 04:46 PM, said:

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.

#4 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 01 August 2016 - 06: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.

#5 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 01 August 2016 - 06:47 PM

View PostKingofGamesYami, on 01 August 2016 - 06:36 PM, said:

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

#6 valithor

  • Members
  • 1,053 posts

Posted 01 August 2016 - 09:54 PM

View PostKingofGamesYami, on 01 August 2016 - 06:36 PM, said:

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.

#7 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 02 August 2016 - 11:06 AM

View Postvalithor, on 01 August 2016 - 09:54 PM, said:

View PostKingofGamesYami, on 01 August 2016 - 06:36 PM, said:

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.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users