ColdFusion Zeus POTW: CallStack

One of the new language additions to ColdFusion Zeus is the ability to get the current callstack. If that sounds Greek to you, it’s simply the ability for a function to know who call it, who called that guy, and so on. It sounds confusing, but a few quick examples will help.

In our first example, I've got a call to a UDF that calls another UDF. That UDF runs one of the two new functions introduced in this blog post, callStackGet.

<cfscript> function top() { return child(); } function child() { return callStackGet(); } </cfscript> <cfdump var="#top()#">

callStackGet returns an array of "invokers", or basically, how in the heck did we get here. Looking at the code it's easy enough to tell that child was called by top, and top was called by our cfdump. When displayed, this is what we get.

Notice the array is ordered from most immediate to the final originator of the caller. Also note that the function is labelled where appropriate. How about a slightly more complex example? Consider the code below.

<cfscript> function grandparent() { return parent(); } function parent() { return child(); } function child() { return new some().test(); } </cfscript> <cfdump var="#grandparent()#">

This is similar to our earlier code, but now child is invoking a CFC as well. Here is some.cfc.

component { function test() { return callStackGet(); } }

And the result...

As expected, it picks up on the different file in the CFC.

Another option is callstackDump. callstackDump returns a string based version of the callstack. It supports logging to a file, logging to console, or simply printing to the browser. Here's a modified form of the CFM we last tested with.

<cfscript> function grandparent() { return parent(); } function parent() { callstackDump(expandPath("./callstack.log")); callstackDump("browser"); return child(); } function child() { return new some().test(); } </cfscript> <cfdump var="#grandparent()#">

Notice I've added two calls to callstackDump. Most likely, this is a more real world example as you won't probably ever return a callstack to the user. Here's an example of how it prints to my browser.

And there you have it folks. I'll be posting another Zeus preview before the Christmas break.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments