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

Getting caller function

Started by Konlab, 07 November 2015 - 11:03 AM
Konlab #1
Posted 07 November 2015 - 12:03 PM
How can you get the place where your function was called? (Like in error messages)
I know I could pcall(error) but for that I need to know how many callers the function has.
Creator #2
Posted 07 November 2015 - 12:29 PM
error(msg,2) This allows you to blame the function that called the function that errored.
Konlab #3
Posted 07 November 2015 - 12:34 PM
error(msg,2) This allows you to blame the function that called the function that errored.
Thanks for replying, but I said in the original post that pcall("test",2) is just the caller. But I need the caller's caller, the caller's caller's caller etc.
KingofGamesYami #4
Posted 07 November 2015 - 01:16 PM
You can blame whatever layer you want.


error( "hi", 2 ) --#blames the function that called this one
error( "hi", 3 ) --#blames the function that called the function that called this one
error( "hi", 4 ) --#blames the function that called the function that called the function that called this one
error( "hi", 5 ) --#...

I assume the same applies to the pcall thing. I wouldn't know.
Creator #5
Posted 07 November 2015 - 01:21 PM
You can blame whatever layer you want.


error( "hi", 2 ) --#blames the function that called this one
error( "hi", 3 ) --#blames the function that called the function that called this one
error( "hi", 4 ) --#blames the function that called the function that called the function that called this one
error( "hi", 5 ) --#...

I assume the same applies to the pcall thing. I wouldn't know.

Wouldn't the code be aborted before the other errors are called?
H4X0RZ #6
Posted 07 November 2015 - 01:35 PM
You can blame whatever layer you want.


error( "hi", 2 ) --#blames the function that called this one
error( "hi", 3 ) --#blames the function that called the function that called this one
error( "hi", 4 ) --#blames the function that called the function that called the function that called this one
error( "hi", 5 ) --#...

I assume the same applies to the pcall thing. I wouldn't know.

Wouldn't the code be aborted before the other errors are called?

That's why you have to use pcall.
Creator #7
Posted 07 November 2015 - 01:37 PM
–snip–
That's why you have to use pcall.

This makes sense! It gives you already two levels.
Konlab #8
Posted 07 November 2015 - 02:42 PM
So you can do any number?
(Thanks, but what happens if I try to pcall error with a very big level, like 500 or math.huge? Or negative?)
I will try it
Thanks for helping!
Edited on 07 November 2015 - 01:43 PM
KingofGamesYami #9
Posted 07 November 2015 - 03:02 PM
If you get above the number of layers, I believe it acts like 0 does - erring without a line number or file name.
Bomb Bloke #10
Posted 07 November 2015 - 10:05 PM
… which means you still need to know how many callers there were.

If you want every error to be able to produce a full trace then you'll need to pcall every single function, have those functions pcall every function they want to call, etc.

Lua has a debug library that (among other things) would deal with this for you, but that's not available within distributable copies of ComputerCraft. Still, it's entirely possible to set up your own Lua environment with it enabled.
Creator #11
Posted 07 November 2015 - 10:11 PM
You can use CCTweaks for the debug library.
Wojbie #12
Posted 07 November 2015 - 10:11 PM
There is a program that does error tracing. http://www.computercraft.info/forums2/index.php?/topic/24618-stack-trace-for-cc/page__fromsearch__1