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/12/16 10:57:31 UTC

[GitHub] [royale-compiler] mrchnk opened a new issue #169: Problems with initialization code for `static const` and `static var`

mrchnk opened a new issue #169:
URL: https://github.com/apache/royale-compiler/issues/169


   
   Problems with lazy initialization code for static members referencing other classes.
   
   Simple ActionScript3 code
   
   ```
   public class Enum {
     
     private static const _registry:EnumRegistry = new EnumRegistry();
     public function Enum(name:String) {
       _name = name;
       _registry.add((this as Object).constructor, this);
     }
     // ...
   }
   ```
   
   Will generate lazy-initialization const using properties (JSDoc was omitted):
   ```
   Enum.get___registry = function() {
     var value = new EnumRegistry();
     Object.defineProperty(Enum, '_registry', { 
       value: value, 
       writable: false 
     });
     return value;
   };
   
   Object.defineProperties(Enum, {
     _registry: {
       get: Enum.get___registry,
       configurable: true
     }
   });
   ```
   
   Looks OK. Even with property redefinition, that code runs correctly.
   
   But after the minimization process by Closure Compiler problem pops up.
   ```
   // Enum becomes `v`, EnumRegistry becomes `x`, _registry becomes `m`
   Object.defineProperties(v, {
     m: {
       get: function () {
         var a = new x;
         // '_registry' is not renamed, because it is a string value
         Object.defineProperty(v, '_registry', {
           value: a,
           writable: !1
         });
         return a
       },
       configurable: !0
     }
   });
   ```
   
   That will cause `TypeError: Cannot redefine property: _registry` on second access of `v.m`;
   
   That is not a problem with references to itself:
   ```
   Target.JSRoyale = new Target('JSRoyale');
   Target.JS = new Target('JS');
   Target.JSNode = new Target('JSNode');
   Target.JSNodeModule = new Target('JSNodeModule');
   Target.SWF = new Target('SWF');
   ```
   
   That was not a problem on 0.9.6 since `static const` and `static var` was initialized without properties, just like regular members:
   
   ```
   Enum._registry = new EnumRegistry();
   ```
   
   I created a minimal project reproducing the issue at [repo](https://github.com/mrchnk/royale-compiler-problems/tree/master/static-initialization)
   
   I wanted to mention one more problem with such initialization on larger codebases.
   Seems like that lazy-initialization reference to EnumRegistry is not counted properly during compilation. 
   And that can cause EnumRegistry class is not fully initialized during another static initialization (like Target.* const's)
   


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



[GitHub] [royale-compiler] joshtynjala commented on issue #169: Problems with initialization code for `static const` and `static var`

Posted by GitBox <gi...@apache.org>.
joshtynjala commented on issue #169:
URL: https://github.com/apache/royale-compiler/issues/169#issuecomment-750485977


   Thanks for the great bug report! Your suggested solution works very nicely.


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



[GitHub] [royale-compiler] mrchnk commented on issue #169: Problems with initialization code for `static const` and `static var`

Posted by GitBox <gi...@apache.org>.
mrchnk commented on issue #169:
URL: https://github.com/apache/royale-compiler/issues/169#issuecomment-747019062


   I think a simple solution could be replace `Object.defineProperty` by `Object.defineProperties`
   ```
   Enum.get___registry = function() {
     var value = new EnumRegistry();
     Object.defineProperties(Enum, {
       _registry: { 
         value: value, 
         writable: false 
       }
     });
     return value;
   };
   ```
   somewhere at
   https://github.com/apache/royale-compiler/blob/6f75df78d08d3c4a7f81a8d25d96b978972ee480/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/FieldEmitter.java#L220-L228


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



[GitHub] [royale-compiler] asfgit closed issue #169: Problems with initialization code for `static const` and `static var`

Posted by GitBox <gi...@apache.org>.
asfgit closed issue #169:
URL: https://github.com/apache/royale-compiler/issues/169


   


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



[GitHub] [royale-compiler] carlosrovira commented on issue #169: Problems with initialization code for `static const` and `static var`

Posted by GitBox <gi...@apache.org>.
carlosrovira commented on issue #169:
URL: https://github.com/apache/royale-compiler/issues/169#issuecomment-751225993


   cool! Thanks! :)
   


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