functi开发者_JAVA百科on 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: Stack overflow occurred.
This page discusses a similar stack overflow issue. It seems adding a trace
somewhere in the function will fix it.
It's a known bug
Thank you for pointing this fact out. Anyways here is what I realize.
A function in AS3 is defined as
function apply(thisArg:*, argArray:*):*
i.e any user function will be mapped into adobe defined Function.apply as above. I guess this is something similar to the 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, if you wish 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 did not really find anything around Google though. Which lead me to go to aobe's page itself. Anyways glad that I learned something new.
Edit: Please do look over the function definition here : http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/Function.html
Yes it's a bug player, another workaround will be to cast your result to int, so the instruction generated by the compiler will not be the same:
in the first case:
function testFunc(val1:int, val2:int, val3:int):int {
var returnVal:int = 0;
return returnVal;
}
the compiler generate something like that: Note that there is nothing wrong with the code generated
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 i.e. 0, stack=empty
and for the workaround:
function testFunc(val1:int, val2:int, val3:int):int {
var returnVal:int = 0;
return int(returnVal);
}
the code generated is
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 i.e 0, stack=empty
精彩评论