views:

334

answers:

2

I have a function (FunctionA) that is being called by another function (FunctionB). The problem is, I'm not sure which function "FunctionB" is.

I have this snippet of code:

function FunctionA():void {
  trace("This function was called by " + ???);
}

I need to figure out what to put for "???" so FunctionA's trace statement looks like this:

This function was called by FunctionB

What should I put for "???"?

+1  A: 

An idea that comes to mind is looking at the current stack trace. The entry before the currently executing method should be the routine that called in to FunctionA.

Example

(This is for ActionScript 3.0 but I'm pretty sure it should be available in previous versions)

colithium
It's throwing the error: "There is no method with the name 'getStackTrace'."I guess it's AS3-only.
Zachary Lewis
A: 

I don't think stack trace is available in AS2.

For each possible call site, add the line

arguments.callee.__caller="somestr";

where somestr is unique.

In function A

trace(arguments.caller.__caller);

In response to comment:

I guess theoretically, you could walk the _global object recursively looking for functions and tagging them.

I'm assuming that you aren't using the Flash IDE? This has a debugger (fairly slow and bad), but it should give you a stack trace (if memory serves me right)

spender
The only problem with this technique is that I was given this software, and I'm not even sure what could possibly be calling it.Is there a way to tie into the root movie to trace out whenever a function is called?
Zachary Lewis