Jump to content
  • 0

How to get name of file that executes other file


Zulfilham

Question

Posted

So, this is the case.  I have file one.lua:

loadfile("two.lua")();

But in file two.lua, I want to know which script is executing it.

two.lua:

-- how to get file name one.lua from here

How?

7 answers to this question

Recommended Posts

Posted
6 hours ago, MarioRossi93i said:

file 1:

local filename = 'foo1'

loadfile('foo2')()

file 2:

print(filename)

 

That'll only work if we know the variable name in advance.  But our script could also be run by other unknown scripts or maybe we just want dynamic value, that would be more useful than hard-coding it.

Posted
4 hours ago, MarioRossi93i said:

file1:

loadfile('file2')()

file2:

print( debug.getinfo(3,'S').short_src )

 

Thanks, it works.  But could you also explain how it works? Or might be references?

Edit:

I did my homework, I found a good reference explaining it.

Posted

@Zulfilham

Could you share your reference? I'm not finding much on the stack levels, why does the value 3 work? 

  • Stack level 0 always corresponds to the debug.getinfo call
  • Stack level 1 would be the function calling debug.getinfo
  • Stack level 2 ?
  • Stack level 3 would be the function that loaded the file?

I guess I'll debug and find out

 

Posted
Just now, darklinux said:

@Zulfilham

Could you share your reference? I'm not finding much on the stack levels, why does the value 3 work? 

  • Stack level 0 always corresponds to the debug.getinfo call
  • Stack level 1 would be the function calling debug.getinfo
  • Stack level 2 ?
  • Stack level 3 would be the function that loaded the file?

I guess I'll debug and find out

 

https://www.lua.org/pil/23.1.html

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.