org.chromium.sdk/src/org/chromium/sdk/internal/JsScopeImpl.java
changeset 2 e4420d2515f1
child 52 f577ea64429e
equal deleted inserted replaced
1:ef76fc2ac88c 2:e4420d2515f1
       
     1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
       
     2 // Use of this source code is governed by a BSD-style license that can be
       
     3 // found in the LICENSE file.
       
     4 
       
     5 package org.chromium.sdk.internal;
       
     6 
       
     7 import java.util.ArrayList;
       
     8 import java.util.HashMap;
       
     9 import java.util.List;
       
    10 import java.util.Map;
       
    11 
       
    12 import org.chromium.sdk.JsScope;
       
    13 import org.chromium.sdk.JsVariable;
       
    14 
       
    15 /**
       
    16  * A generic implementation of the JsScope interface.
       
    17  */
       
    18 public class JsScopeImpl implements JsScope {
       
    19 
       
    20   private final CallFrameImpl callFrameImpl;
       
    21   private final ScopeMirror mirror;
       
    22   private List<JsVariable> properties = null;
       
    23 
       
    24   public JsScopeImpl(CallFrameImpl callFrameImpl, ScopeMirror mirror) {
       
    25     this.callFrameImpl = callFrameImpl;
       
    26     this.mirror = mirror;
       
    27   }
       
    28 
       
    29   public Type getType() {
       
    30     Type type = CODE_TO_TYPE.get(mirror.getType());
       
    31     if (type == null) {
       
    32       type = Type.UNKNOWN;
       
    33     }
       
    34     return type;
       
    35   }
       
    36 
       
    37   public synchronized List<? extends JsVariable> getVariables() {
       
    38     if (properties == null) {
       
    39       ValueLoader valueLoader = callFrameImpl.getInternalContext().getValueLoader();
       
    40       List<? extends PropertyReference> propertyRefs =
       
    41           valueLoader.loadScopeFields(mirror.getIndex(), callFrameImpl.getIdentifier());
       
    42       List<ValueMirror> propertyMirrors = valueLoader.getOrLoadValueFromRefs(propertyRefs);
       
    43 
       
    44       properties = new ArrayList<JsVariable>(propertyMirrors.size());
       
    45       for (int i = 0; i < propertyMirrors.size(); i++) {
       
    46         properties.add(new JsVariableImpl(callFrameImpl, propertyMirrors.get(i),
       
    47             propertyRefs.get(i).getName()));
       
    48       }
       
    49     }
       
    50     return properties;
       
    51   }
       
    52 
       
    53   private static final Map<Integer, Type> CODE_TO_TYPE;
       
    54   static {
       
    55     CODE_TO_TYPE = new HashMap<Integer, Type>();
       
    56     CODE_TO_TYPE.put(0, Type.GLOBAL);
       
    57     CODE_TO_TYPE.put(1, Type.LOCAL);
       
    58     CODE_TO_TYPE.put(2, Type.WITH);
       
    59     CODE_TO_TYPE.put(3, Type.CLOSURE);
       
    60     CODE_TO_TYPE.put(4, Type.CATCH);
       
    61   }
       
    62 }