Here is a debugging function that many REBOLers might find useful:
USAGE:
STACK offset /block /word /func /args /size /depth /limit
DESCRIPTION:
Returns stack backtrace or other values.
STACK is a native value.
ARGUMENTS:
offset -- Relative backward offset (Type: integer!)
REFINEMENTS:
/block -- Block evaluation position
/word -- Function or object name, if known
/func -- Function value
/args -- Block of args (may be modified)
/size -- Current stack size (in value units)
/depth -- Stack depth (frames)
/limit -- Stack bounds (auto expanding)
(Currently, it's called stack, but it may be renamed because that
word may be used in a number of scripts already.)
The offset is the number of stack frames to go back on the stack.
For, example, take this code:
my-func: does [
loop 1 [
if true [
probe stack 0
print "there"
]
]
]
The zero offset indicates the top of the stack. When you run the code, you'll see:
[stack if loop my-func]
That's a function invocation backtrace, with the most recent functions
shown first.
I you only wanted to see functions prior to the stack call, you would
use an offset of 1 and get:
[if loop my-func]
Other Information
The stack function can give you other useful information...
To get the code block where the function was called:
>> stack/block 0
== [0 print "there"]
This is normally used by error functions to show where the error
occurred. Notice that it does not show the function name. That's
because functions don't strictly require names (can be lambda functions, that is unnamed) and functions can also be applied (with apply).
To get the name of a function in a stack (here it's just the stack function):
>> stack/word 0
== stack
To get a function value from the stack:
>> stack/func 0
== make native! [
{Returns stack backtrace or other values.}
offset [integer!] "Relative backward offset"
/block "Block evaluation position"
/word "Function or object name, if known"
/func "Function value"
/args "Block of args (may be modified)"
/size "Current stack size (in value units)"
/depth "Stack depth (frames)"
/limit "Stack bounds (auto expanding)"
]
That's a fully qualified function value, so you can use first, second, and even do it.
To find out the arguments to a function (including the refinements):