It will be interesting to see what the&nbsp; behavior of this issue is in swf9.<br><br><br><div class="gmail_quote">On Jan 9, 2008 5:37 PM, P T Withington &lt;<a href="mailto:ptw@pobox.com">ptw@pobox.com</a>&gt; wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
yes please.<br><br>Simple test case is to compare the behaviour of the attribute and<br>method declarations.<br><br>It&#39;s a swf-only bug.<br><div><div></div><div class="Wj3C7c"><br>On 2008-01-09, at 17:34 EST, André Bargull wrote:
<br><br>&gt; Should we create a JIRA-entry for this bug, so either to create a<br>&gt; fix (somehow), or to document it at least.<br>&gt;<br>&gt; - André<br>&gt;<br>&gt; On 1/9/2008 11:07 PM, P T Withington wrote:<br>&gt;&gt; I take back my explanation regarding `this`.
<br>&gt;&gt;<br>&gt;&gt; What it really is is a bug in the Flash implementation of &quot;function<br>&gt;&gt; scope&quot;. &nbsp;When you define a function, it is supposed to capture the<br>&gt;&gt; scope that it was defined in (and use that scope to look up free
<br>&gt;&gt; references in). &nbsp;The Flash player makes a optimization of never<br>&gt;&gt; capturing the scope of a function defined in the global scope.<br>&gt;&gt; This would be fine, because you call all your functions in the
<br>&gt;&gt; global scope, so it will be there when you call.<br>&gt;&gt;<br>&gt;&gt; BUT, Henry is right (as usual).<br>&gt;&gt;<br>&gt;&gt; There are a bunch of places in the player where as functions are<br>&gt;&gt; being called by player internals (surely C or C++), and the player
<br>&gt;&gt; &quot;forgets&quot; to call the function in the global scope. &nbsp;The most<br>&gt;&gt; common place we run into this is in the idle loop. &nbsp;But apparently<br>&gt;&gt; the Array#sort method is another case. &nbsp;We used to have to write
<br>&gt;&gt; all our code that would be used in the idle loop to explicitly<br>&gt;&gt; reference everything from `_root` (which must somehow be specially<br>&gt;&gt; looked up, not just looked up in the scope chain).<br>
&gt;&gt;<br>&gt;&gt; A while back, we worked around this issue by making sure that every<br>&gt;&gt; method we define is defined in such a way as to trick the runtime<br>&gt;&gt; into capturing the global scope on the function. &nbsp;It seems like
<br>&gt;&gt; that technique is not working 100%.<br>&gt;&gt;<br>&gt;&gt; And it seems that when a free reference is made from a function and<br>&gt;&gt; the scope chain is empty, the function just immediately exits,<br>&gt;&gt; which is why you see no output.
<br>&gt;&gt;<br>&gt;&gt; The (apparent) reason that tracing or closing over the function<br>&gt;&gt; works is that both are invoking the scope-less function inside a<br>&gt;&gt; function that _does_ have a scope, so it all works out.
<br>&gt;&gt;<br>&gt;&gt; So, really, this is a bug in our compiler&#39;s work-around for the<br>&gt;&gt; scope issue. &nbsp;If you rewrite the canvas method as:<br>&gt;&gt;<br>&gt;&gt; &nbsp;&lt;attribute name=&quot;laszloMethod&quot; value=&#39;function (n1, n2)
<br>&gt;&gt; { Debug.write(&quot;%s: %d - %d (%s)&quot;, arguments.callee, n1, n2, Debug);<br>&gt;&gt; return 0}&#39; /&gt;<br>&gt;&gt;<br>&gt;&gt; (which should be equivalent), everything works...<br>&gt;&gt;<br>&gt;&gt; On 2008-01-09, at 16:13 EST, André Bargull wrote:
<br>&gt;&gt;<br>&gt;&gt;&gt; Another riddle for Tucker:<br>&gt;&gt;&gt;<br>&gt;&gt;&gt; I can call:<br>&gt;&gt;&gt; _root.Debug.write(&quot;%s: %d - %d&quot;, arguments.callee, n1, n2);<br>&gt;&gt;&gt;<br>&gt;&gt;&gt; But I cannot call:
<br>&gt;&gt;&gt; _root.Debug.write(&quot;%s: %d - %d (%s)&quot;, arguments.callee, n1, n2,<br>&gt;&gt;&gt; Debug);//or i.e. global, canvas etc. breaks, too<br>&gt;&gt;&gt;<br>&gt;&gt;&gt; So, in which context does get Array#sort(..) called?
<br>&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt; It _is_ getting called. &nbsp;For some reason, your Debug.write is<br>&gt;&gt;&gt;&gt; failing &nbsp;to send any output:<br>&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt; lzx&gt; Debug.trace(canvas, &#39;laszloMethod&#39;)
<br>&gt;&gt;&gt;&gt; lzx&gt; [1,2,3].sort(canvas.laszloMethod)<br>&gt;&gt;&gt;&gt; TRACE: [394491.00] laszloMethod.apply(?undefined?, [1, 2])<br>&gt;&gt;&gt;&gt; laszloMethod( 1 , 2 )<br>&gt;&gt;&gt;&gt; TRACE: [394501.00
] laszloMethod -&gt; 0<br>&gt;&gt;&gt;&gt; TRACE: [394505.00] laszloMethod.apply(?undefined?, [1, 3])<br>&gt;&gt;&gt;&gt; laszloMethod( 1 , 3 )<br>&gt;&gt;&gt;&gt; TRACE: [394512.00] laszloMethod -&gt; 0<br>&gt;&gt;&gt;&gt; TRACE: [
394516.00] laszloMethod.apply(?undefined?, [1, 2])<br>&gt;&gt;&gt;&gt; laszloMethod( 1 , 2 )<br>&gt;&gt;&gt;&gt; TRACE: [394523.00] laszloMethod -&gt; 0<br>&gt;&gt;&gt;&gt; TRACE: [394527.00] laszloMethod.apply(?undefined?, [2, 3])
<br>&gt;&gt;&gt;&gt; laszloMethod( 2 , 3 )<br>&gt;&gt;&gt;&gt; TRACE: [394536.00] laszloMethod -&gt; 0<br>&gt;&gt;&gt;&gt; ?Array(3)#15| [1, 2, 3]?<br>&gt;&gt;&gt;&gt; lzx&gt;<br>&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt; Oh, hah-hah. &nbsp;I see why. &nbsp;Because laslzoMethod is a method, it
<br>&gt;&gt;&gt;&gt; expects &nbsp;a &#39;this&#39; argument, and our compiler inserts an implicit<br>&gt;&gt;&gt;&gt; `with (this)` &nbsp;around your method body, but you are calling it as<br>&gt;&gt;&gt;&gt; a function, and &nbsp;`this` is undefined in that case (see the trace
<br>&gt;&gt;&gt;&gt; output above). &nbsp;So &nbsp;your code is turning into `with (undefined)`<br>&gt;&gt;&gt;&gt; and I bet that causes the &nbsp;global reference to Debug to fail.<br>&gt;&gt;&gt;&gt; Amusingly, tracing the method &nbsp;makes the debug output work. &nbsp;I
<br>&gt;&gt;&gt;&gt; can&#39;t really explain that.<br>&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt; If you were to make a closure to call your method, it should work:<br>&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt; lzx&gt; Debug.untrace(canvas, &#39;laszloMethod&#39;)
<br>&gt;&gt;&gt;&gt; lzx&gt; [1,2,3].sort(function (a, b) { return canvas.laszloMethod(a,<br>&gt;&gt;&gt;&gt; b) })<br>&gt;&gt;&gt;&gt; laszloMethod( 1 , 2 )<br>&gt;&gt;&gt;&gt; laszloMethod( 1 , 3 )<br>&gt;&gt;&gt;&gt; laszloMethod( 1 , 2 )
<br>&gt;&gt;&gt;&gt; laszloMethod( 2 , 3 )<br>&gt;&gt;&gt;&gt; ?Array(3)#57| [1, 2, 3]?<br>&gt;&gt;&gt;&gt; lzx&gt;<br>&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt; On 2008-01-09, at 14:28 EST, Pablo Kang wrote:<br>&gt;&gt;&gt;&gt;
<br>&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt; Btw, I know that I can call a laszloMethod from a Javascript<br>&gt;&gt;&gt;&gt;&gt; function like:<br>&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt; &lt;method name=&quot;javascriptFunction&quot; args=&quot;n1,n2&quot;&gt;
<br>&gt;&gt;&gt;&gt;&gt; &nbsp; &nbsp; return canvas.laszloMethod(n1,n2);<br>&gt;&gt;&gt;&gt;&gt; &lt;/method&gt;<br>&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt; What I want to know is why it is I can&#39;t pass in the the<br>&gt;&gt;&gt;&gt;&gt; laszloMethod directly.
<br>&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt; Thanks,<br>&gt;&gt;&gt;&gt;&gt; pablo<br>&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt; On Wed, 9 Jan 2008, Pablo Kang wrote:<br>&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt; Anyone know why passing in a function defined with an LZX<br>&gt;&gt;&gt;&gt;&gt;&gt; method &nbsp; tag into Array.sort doesn&#39;t work? Here&#39;s a test case:<br>&gt;&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt;&gt; &lt;canvas debug=&quot;true&quot;&gt;
<br>&gt;&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt;&gt; &nbsp;&lt;method name=&quot;laszloMethod&quot; args=&quot;n1,n2&quot;&gt;<br>&gt;&gt;&gt;&gt;&gt;&gt; &nbsp; &nbsp; Debug.write(&#39;laszloMethod(&#39;, n1, &#39;,&#39;, n2, &#39;)&#39;);
<br>&gt;&gt;&gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp;return 0;<br>&gt;&gt;&gt;&gt;&gt;&gt; &nbsp;&lt;/method&gt;<br>&gt;&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt;&gt; &nbsp;&lt;script&gt;<br>&gt;&gt;&gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp;function javascriptFunction(n1,n2) {
<br>&gt;&gt;&gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Debug.write(&#39;javascriptFunction(&#39;, n1, &#39;,&#39;, n2 ,&#39;)&#39;);<br>&gt;&gt;&gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return 0;<br>&gt;&gt;&gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp;}<br>&gt;&gt;&gt;&gt;&gt;&gt; &nbsp;&lt;/script&gt;
<br>&gt;&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt;&gt; &nbsp;&lt;handler name=&quot;oninit&quot;&gt;<br>&gt;&gt;&gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp;var arr = [ 1,2,3 ];<br>&gt;&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp;Debug.write
(&#39;-- Javascript Function --&#39;);<br>&gt;&gt;&gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp;arr.sort( javascriptFunction );<br>&gt;&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp;Debug.write(&#39;-- Laszlo Method --------&#39;);<br>&gt;&gt;&gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp;
arr.sort( canvas.laszloMethod );<br>&gt;&gt;&gt;&gt;&gt;&gt; &nbsp;&lt;/handler&gt;<br>&gt;&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt;&gt; &lt;/canvas&gt;<br>&gt;&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt;&gt; pablo<br>&gt;&gt;&gt;&gt;&gt;&gt;
<br>&gt;&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;<br>&gt;&gt;<br>&gt;&gt;<br>&gt;<br><br><br></div></div></blockquote></div><br><br clear="all">
<br>-- <br>Henry Minsky<br>Software Architect<br><a href="mailto:hminsky@laszlosystems.com">hminsky@laszlosystems.com</a><br><br>