Why does calling this function with more than two parameters in ActionScript 3 cause a stack overflow?

function testFunc(val1:int, val2:int, val3:int):int { var returnVal:int = 0; return returnVal; } var val:int = testFunc(1, 2, 3); 

Causes

 locals: Main int int int * 4:dup VerifyError: Error #1023: Qaru occurred. 
+4
source share
3 answers

This page discusses stack overflow issues . Adding trace somewhere in the function seems to fix it.

This is a known bug.

+4
source

Thank you for pointing this fact out. Anyway, that’s what I understand.

Function in AS3 is defined as

function apply(thisArg:*, argArray:*):*

i. any custom function will be displayed on adobe defined as Function.apply as above. I assume this is something similar to environment variables in c. The first argument can be used to send the length of the Array of arguments , followed by the array itself.

So this basically means that if you want to use the above function call, you can define your function as

 function testFunc(...args):int { val1:int = args[0]; val2:int = args[1]; val3:int = args[2]; var returnVal:int = 0; return returnVal; } var val:int = testFunc(1, 2, 3); 

I really did not find anything around Google. It made me go to the page itself. In any case, I am glad that I learned something new.

Edit: Please view the function definition here: http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/Function.html

0
source

Yes, this is an error player, another workaround would be to pass your result to int, so the command generated by the compiler will not be like this:

In the first case:

 function testFunc(val1:int, val2:int, val3:int):int { var returnVal:int = 0; return returnVal; } 

the compiler generates something like this: Note that there is nothing wrong with the generated code

 getlocal 0 pushscope pushbyte 0 // stack = 0 dup // stack = 0 0 setlocal 4 // set returnVal with value on stack, stack = 0 returnvalue // return the value left on the stack ie 0, stack=empty 

and for a workaround:

 function testFunc(val1:int, val2:int, val3:int):int { var returnVal:int = 0; return int(returnVal); } 

generated code

 getlocal 0 pushscope pushbyte 0 // stack = 0 setlocal 4 // set returnValue with the value on the stack, stack=empty findpropstrict int // push on stack the object holding the int property, stack=object getlocal 4 // push on stack returnVal, stack=object 0 callproperty int(param count:1) // call the int property , stack=0 returnvalue // return the value left on stack ie 0, stack=empty 
0
source

All Articles