You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@royale.apache.org by GitBox <gi...@apache.org> on 2020/04/04 08:06:17 UTC

[GitHub] [royale-compiler] yishayw opened a new issue #143: Wrong "this" in Function given as Param

yishayw opened a new issue #143: Wrong "this" in Function given as Param
URL: https://github.com/apache/royale-compiler/issues/143
 
 
   Consider this App
   
   ```
   <mx:Application xmlns:local="*" xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:mx="library://ns.apache.org/royale/mx" 
   	width="600" height="400">
   	<fx:Script>	
   	<![CDATA[
   
   		public function buttonFunction():void
   		{
   			this.perform();
   		}
   
   		private function perform():void
   		{
   			trace('done');
   		}
   	
   	]]>
   	</fx:Script>
   	<local:MyBox>
   		<local:MyButton label="click"/>
   	</local:MyBox>
   </mx:Application>
   ```
   
   Where MyButton is
   
   ```
   package 
   {
   	import mx.controls.Button;
   	import mx.events.MouseEvent;
   
   	public class MyButton extends Button
   	{
   		private var _function:Function;
   		public function MyButton()
   		{
   			super();
   			addEventListener(MouseEvent.CLICK, clickHandler);
   		}
   		
   		public function set clickFunction(value:Function):void
   		{	
   			_function = value;
   		}
   
   		private function clickHandler(e:Event):void
   		{
   			_function();
   		}
   
   	}
   }
   ```
   
   and MyBox is
   
   ```
   package 
   {
   	import mx.containers.Box;
   	import org.apache.royale.core.IChild;
   	public class MyBox extends Box
   	{
   		
   		public function MyBox()
   		{
   		}
   
   		override public function addElement(c:org.apache.royale.core.IChild, dispatchEvent:Boolean = true):void
   		{
   			(c as MyButton).clickFunction = parentApplication.buttonFunction;
   			return super.addElement(c, dispatchEvent);
   		}
   	}
   }
   ```
   
   Clicking the button results in 
   
   ```
   Examples.js:84 Uncaught TypeError: this.Examples_perform is not a function
       at MyButton.Examples.buttonFunction [as MyButton__function] (Examples.js:84)
       at MyButton.MyButton_clickHandler (MyButton.js:46)
       at Function.goog.events.fireListener [as googFireListener] (events.js:744)
       at Object.org.apache.royale.core.ElementWrapper.fireListenerOverride [as fireListener] (ElementWrapper.js:62)
       at HTMLButtonElement.goog.events.handleBrowserEvent_ (events.js:870)
       at HTMLButtonElement.f (events.js:289)
   ```
   
   Because "this" is MyButton instead of Examples
   
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [royale-compiler] aharui commented on issue #143: Wrong "this" in Function given as Param

Posted by GitBox <gi...@apache.org>.
aharui commented on issue #143: Wrong "this" in Function given as Param
URL: https://github.com/apache/royale-compiler/issues/143#issuecomment-612106386
 
 
   Apologies for arriving late to this issue.  Can someone explain the need for conditionalBind() and when it would be used?  The issue from looking over the source (I haven't debugged into it), is that expressions like `parentApplication.buttonFunction;` must be strongly typed.  Properties that are of type `Object` or `*` like `parentApplication` cause the compiler to lose track of the types of things so the code paths you want in the transpiler don't get hit.  The correct code would look more like:
   
   `(parentApplication as Example).buttonFunction;`

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [royale-compiler] yishayw commented on issue #143: Wrong "this" in Function given as Param

Posted by GitBox <gi...@apache.org>.
yishayw commented on issue #143: Wrong "this" in Function given as Param
URL: https://github.com/apache/royale-compiler/issues/143#issuecomment-612165044
 
 
   Yes, casting is the better solution.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [royale-compiler] yishayw closed issue #143: Wrong "this" in Function given as Param

Posted by GitBox <gi...@apache.org>.
yishayw closed issue #143: Wrong "this" in Function given as Param
URL: https://github.com/apache/royale-compiler/issues/143
 
 
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [royale-compiler] aharui commented on issue #143: Wrong "this" in Function given as Param

Posted by GitBox <gi...@apache.org>.
aharui commented on issue #143: Wrong "this" in Function given as Param
URL: https://github.com/apache/royale-compiler/issues/143#issuecomment-612208466
 
 
   It may be useful to warn on all (or some) access of Object.  Feel free to open an issue to have the compiler do that.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [royale-compiler] yishayw commented on issue #143: Wrong "this" in Function given as Param

Posted by GitBox <gi...@apache.org>.
yishayw commented on issue #143: Wrong "this" in Function given as Param
URL: https://github.com/apache/royale-compiler/issues/143#issuecomment-612354260
 
 
   I've moved on from the test case to the main application I'm porting. The test case doesn't use modules... Yes, removing that line makes the Main app load. The problem was before the module load. It does seem strange that code in the module affects the main application initialization.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [royale-compiler] yishayw commented on issue #143: Wrong "this" in Function given as Param

Posted by GitBox <gi...@apache.org>.
yishayw commented on issue #143: Wrong "this" in Function given as Param
URL: https://github.com/apache/royale-compiler/issues/143#issuecomment-609448479
 
 
   Workaround is to replace
   			`(c as MyButton).clickFunction = parentApplication.buttonFunction;`
   with
   				`(c as MyButton).clickFunction = goog.bind(parentApplication.buttonFunction, parentApplication);`
   
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [royale-compiler] yishayw commented on issue #143: Wrong "this" in Function given as Param

Posted by GitBox <gi...@apache.org>.
yishayw commented on issue #143: Wrong "this" in Function given as Param
URL: https://github.com/apache/royale-compiler/issues/143#issuecomment-610960282
 
 
   Maybe add Language.conditionalBind() and use that instead of goog.bind() ?
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [royale-compiler] yishayw commented on issue #143: Wrong "this" in Function given as Param

Posted by GitBox <gi...@apache.org>.
yishayw commented on issue #143: Wrong "this" in Function given as Param
URL: https://github.com/apache/royale-compiler/issues/143#issuecomment-612352271
 
 
   > Apologies for arriving late to this issue. Can someone explain the need for conditionalBind() and when it would be used? The issue from looking over the source (I haven't debugged into it), is that expressions like `parentApplication.buttonFunction;` must be strongly typed. Properties that are of type `Object` or `*` like `parentApplication` cause the compiler to lose track of the types of things so the code paths you want in the transpiler don't get hit. The correct code would look more like:
   > 
   > `(parentApplication as Example).buttonFunction;`
   
   This doesn't seem to work when parentApplication is referenced from a module.
   
   Getting
   
   SystemManager.js:232 Uncaught TypeError: Cannot read property 'mixins' of undefined
       at Main_mx_managers_SystemManager.mx.managers.SystemManager.start (SystemManager.js:232)
       at index.html:491

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [royale-compiler] aharui commented on issue #143: Wrong "this" in Function given as Param

Posted by GitBox <gi...@apache.org>.
aharui commented on issue #143: Wrong "this" in Function given as Param
URL: https://github.com/apache/royale-compiler/issues/143#issuecomment-612353745
 
 
   Are you saying you can comment out that line and the module will load?  The module should not have Example in it.  It looks the code is failing really early, before the application loads and would have a chance to load a module.  It also looks like the application is called "Main" not "Example".

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [royale-compiler] yishayw commented on issue #143: Wrong "this" in Function given as Param

Posted by GitBox <gi...@apache.org>.
yishayw commented on issue #143: Wrong "this" in Function given as Param
URL: https://github.com/apache/royale-compiler/issues/143#issuecomment-612354486
 
 
   > The module should not have Example in it.
   
   That's probably the problem, but the legacy code does, and I guess because it's not explicit in the original code, Flex was ok with that.
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [royale-compiler] yishayw edited a comment on issue #143: Wrong "this" in Function given as Param

Posted by GitBox <gi...@apache.org>.
yishayw edited a comment on issue #143: Wrong "this" in Function given as Param
URL: https://github.com/apache/royale-compiler/issues/143#issuecomment-612165044
 
 
   Yes, casting is the better solution. Verified it works.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [royale-compiler] yishayw commented on issue #143: Wrong "this" in Function given as Param

Posted by GitBox <gi...@apache.org>.
yishayw commented on issue #143: Wrong "this" in Function given as Param
URL: https://github.com/apache/royale-compiler/issues/143#issuecomment-610947462
 
 
   > Workaround is to replace
   > `(c as MyButton).clickFunction = parentApplication.buttonFunction;`
   > with
   > `(c as MyButton).clickFunction = goog.bind(parentApplication.buttonFunction, parentApplication);`
   
   Looks like changing the compiler to automatically do this is not a good idea because the first argument in goog.bind could be null which would cause a runtime exception.
   
   Suggestions on how to fix this are welcome.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [royale-compiler] yishayw commented on issue #143: Wrong "this" in Function given as Param

Posted by GitBox <gi...@apache.org>.
yishayw commented on issue #143: Wrong "this" in Function given as Param
URL: https://github.com/apache/royale-compiler/issues/143#issuecomment-612403237
 
 
   I can't reproduce the problem in ModuleExample. Leaving this closed for now.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [royale-compiler] aharui commented on issue #143: Wrong "this" in Function given as Param

Posted by GitBox <gi...@apache.org>.
aharui commented on issue #143: Wrong "this" in Function given as Param
URL: https://github.com/apache/royale-compiler/issues/143#issuecomment-612355503
 
 
   The source code of the module can reference Main or whatever the loading application is.  My point is that the compiled results of the module should not include the transpiled code for Main.  The compiler options should prevent Main from being linked into the output.
   
   Maybe see if ModuleExample is still working, then modify it to reference the main app in the module and see if you get the same issue.  I'm done with work for tonight so won't respond again for many hours.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [royale-compiler] yishayw edited a comment on issue #143: Wrong "this" in Function given as Param

Posted by GitBox <gi...@apache.org>.
yishayw edited a comment on issue #143: Wrong "this" in Function given as Param
URL: https://github.com/apache/royale-compiler/issues/143#issuecomment-612354486
 
 
   > The module should not have Example in it.
   
   That's probably the problem, but the legacy code does have a reference to Main via mainApplication, and I guess because it's not explicit in the original code, Flex was ok with that.
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services