org.chromium.sdk/src/org/chromium/sdk/internal/JsVariableImpl.java
changeset 276 f2f4a1259de8
parent 52 f577ea64429e
equal deleted inserted replaced
275:12c2ea2194c7 276:f2f4a1259de8
    21 
    21 
    22   /** The context this variable belongs in. */
    22   /** The context this variable belongs in. */
    23   private final InternalContext context;
    23   private final InternalContext context;
    24 
    24 
    25   /** The fully qualified name of this variable. */
    25   /** The fully qualified name of this variable. */
    26   private final String variableFqn;
    26   private final String qualifiedName;
    27 
       
    28   private final NameDecorator nameDecorator;
       
    29 
    27 
    30   /** The lazily constructed value of this variable. */
    28   /** The lazily constructed value of this variable. */
    31   private final JsValueImpl value;
    29   private final JsValueImpl value;
    32 
    30 
    33   /** Variable name. */
    31   /** Variable name. */
    34   private final String rawName;
    32   private final Object rawName;
       
    33 
       
    34   /** Variable name. */
       
    35   private final String decoratedName;
    35 
    36 
    36   /**
    37   /**
    37    * Constructs a variable contained in the given context with the given
    38    * Constructs a variable contained in the given context with the given
    38    * value mirror.
    39    * value mirror.
    39    *
    40    *
    40    * @param context that owns this variable
    41    * @param context that owns this variable
    41    * @param valueData value data for this variable
    42    * @param valueData value data for this variable
    42    */
    43    */
    43   JsVariableImpl(InternalContext context, ValueMirror valueData, String name) {
    44   JsVariableImpl(InternalContext context, ValueMirror valueData, String name) {
    44     this(context, valueData, name, null, NameDecorator.NOOP);
    45     this(context, valueData, name, name, name);
    45   }
    46   }
    46 
    47 
    47   /**
    48   /**
    48    * Constructs a variable contained in the given context with the given
    49    * Constructs a variable contained in the given context with the given
    49    * value mirror.
    50    * value mirror.
    50    *
    51    *
    51    * @param context that owns this variable
    52    * @param context that owns this variable
    52    * @param valueData for this variable
    53    * @param valueData for this variable
    53    * @param variableFqn the fully qualified name of this variable
    54    * @param qualifiedName the fully qualified name of this variable
    54    */
    55    */
    55   JsVariableImpl(InternalContext context, ValueMirror valueData, String name, String variableFqn,
    56   JsVariableImpl(InternalContext context, ValueMirror valueData, Object rawName,
    56       NameDecorator nameDecorator) {
    57       String decoratedName, String qualifiedName) {
    57     this.context = context;
    58     this.context = context;
    58     this.valueData = valueData;
    59     this.valueData = valueData;
    59     this.rawName = name;
    60     this.rawName = rawName;
    60     this.variableFqn = variableFqn;
    61     this.decoratedName = decoratedName;
    61     this.nameDecorator = nameDecorator;
    62     this.qualifiedName = qualifiedName;
    62 
    63 
    63     Type type = this.valueData.getType();
    64     Type type = this.valueData.getType();
    64     switch (type) {
    65     switch (type) {
    65       case TYPE_FUNCTION:
    66       case TYPE_FUNCTION:
    66         this.value = new JsFunctionImpl(context, this.variableFqn, this.valueData);
    67         this.value = new JsFunctionImpl(context, this.qualifiedName, this.valueData);
    67         break;
    68         break;
    68       case TYPE_ERROR:
    69       case TYPE_ERROR:
    69       case TYPE_OBJECT:
    70       case TYPE_OBJECT:
    70         this.value = new JsObjectImpl(context, this.variableFqn, this.valueData);
    71         this.value = new JsObjectImpl(context, this.qualifiedName, this.valueData);
    71         break;
    72         break;
    72       case TYPE_ARRAY:
    73       case TYPE_ARRAY:
    73         this.value = new JsArrayImpl(context, this.variableFqn, this.valueData);
    74         this.value = new JsArrayImpl(context, this.qualifiedName, this.valueData);
    74         break;
    75         break;
    75       default:
    76       default:
    76         this.value = new JsValueImpl(this.valueData);
    77         this.value = new JsValueImpl(this.valueData);
    77     }
    78     }
    78   }
    79   }
    84   public JsValueImpl getValue() {
    85   public JsValueImpl getValue() {
    85     return value;
    86     return value;
    86   }
    87   }
    87 
    88 
    88   public String getName() {
    89   public String getName() {
    89     return nameDecorator.decorateVarName(rawName);
    90     return decoratedName;
    90   }
    91   }
    91 
    92 
    92   public String getRawName() {
    93   public String getRawName() {
       
    94     return this.rawName.toString();
       
    95   }
       
    96 
       
    97   Object getRawNameAsObject() {
    93     return this.rawName;
    98     return this.rawName;
    94   }
    99   }
    95 
   100 
    96   public boolean isMutable() {
   101   public boolean isMutable() {
    97     return false; // TODO(apavlov): fix once V8 supports it
   102     return false; // TODO(apavlov): fix once V8 supports it
   130   public ValueMirror getMirror() {
   135   public ValueMirror getMirror() {
   131     return valueData;
   136     return valueData;
   132   }
   137   }
   133 
   138 
   134   public String getFullyQualifiedName() {
   139   public String getFullyQualifiedName() {
   135     return variableFqn != null
   140     return qualifiedName != null
   136         ? variableFqn
   141         ? qualifiedName
   137         : getName();
   142         : getName();
   138   }
   143   }
   139 
   144 
   140   static abstract class NameDecorator {
   145   static class NameDecorator {
   141     static final NameDecorator NOOP = new NameDecorator() {
   146     static String decorateVarName(Object rawName) {
   142       @Override
   147       if (rawName instanceof Number) {
   143       String decorateVarName(String rawName) {
   148         return OPEN_BRACKET + rawName + CLOSE_BRACKET;
   144         return rawName;
   149       } else {
       
   150         return rawName.toString();
   145       }
   151       }
   146       @Override
   152     }
   147       String buildAccessSuffix(String rawName) {
   153     static String buildAccessSuffix(Object rawName) {
       
   154       if (rawName instanceof Number) {
       
   155         return OPEN_BRACKET + rawName + CLOSE_BRACKET;
       
   156       } else {
   148         return "." + rawName;
   157         return "." + rawName;
   149       }
   158       }
   150     };
   159     }
   151     abstract String decorateVarName(String rawName);
   160     private static final String OPEN_BRACKET = "[";
   152     abstract String buildAccessSuffix(String rawName);
   161     private static final String CLOSE_BRACKET = "]";
   153   }
   162   }
   154 }
   163 }