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:
[email protected]


Reply via email to