为什么在 Actionscript 3 中使用超过 2 个参数调用此函数会导致堆栈溢出?

发布于 2024-09-11 21:40:41 字数 285 浏览 7 评论 0原文

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

var val:int = testFunc(1, 2, 3);

原因

locals: Main int int int * 
4:dup VerifyError: Error #1023: Stack overflow occurred.
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: Stack overflow occurred.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲歌长辞 2024-09-18 21:40:41

本页面讨论类似的堆栈溢出问题。似乎在函数中的某处添加 trace 可以修复它。

这是一个已知错误

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

少跟Wǒ拽 2024-09-18 21:40:41

感谢您指出这一事实。无论如何,这是我所意识到的。

AS3 中的函数定义为

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

,即任何用户函数都将映射到 adobe 定义的 Function.apply ,如上所述。我猜这与c中的环境变量类似。第一个参数可用于发送参数数组的长度,后跟数组本身。

所以这基本上意味着,如果你想使用上面的函数调用,你可以定义你的函数,因为

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);

我在谷歌上并没有真正找到任何东西。这让我去了 aobe 的页面本身。不管怎样,很高兴我学到了新东西。

编辑:请查看此处的函数定义:
http://www.adobe.com/livedocs/flash/9.0/ ActionScriptLangRefV3/Function.html

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

江湖正好 2024-09-18 21:40:41

是的,这是一个错误播放器,另一种解决方法是将结果转换为 int,因此编译器生成的指令将不一样:

在第一种情况下:

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

编译器生成类似的内容:
请注意,生成的代码

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

和解决方法没有任何问题:

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

生成的代码是

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文