|
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.debug.core.model; |
|
6 |
|
7 import org.chromium.debug.core.util.ChromiumDebugPluginUtil; |
|
8 import org.chromium.sdk.JsValue; |
|
9 import org.chromium.sdk.JsVariable; |
|
10 import org.eclipse.core.runtime.CoreException; |
|
11 import org.eclipse.debug.core.DebugException; |
|
12 import org.eclipse.debug.core.model.IValue; |
|
13 import org.eclipse.debug.core.model.IVariable; |
|
14 import org.eclipse.debug.ui.actions.IWatchExpressionFactoryAdapter; |
|
15 |
|
16 /** |
|
17 * An IVariable implementation over a JsVariable instance. |
|
18 */ |
|
19 public class Variable extends DebugElementImpl implements IVariable { |
|
20 |
|
21 private final JsVariable variable; |
|
22 |
|
23 /** |
|
24 * Specifies whether this variable is internal property (__proto__ etc). |
|
25 * TODO(peter.rybin): use it in UI. |
|
26 */ |
|
27 private final boolean isInternalProperty; |
|
28 |
|
29 public Variable(IChromiumDebugTarget debugTarget, JsVariable variable, boolean isInternalProperty) { |
|
30 super(debugTarget); |
|
31 this.variable = variable; |
|
32 this.isInternalProperty = isInternalProperty; |
|
33 } |
|
34 |
|
35 public String getName() throws DebugException { |
|
36 return variable.getName(); |
|
37 } |
|
38 |
|
39 public String getReferenceTypeName() throws DebugException { |
|
40 return variable.getValue().getType().toString(); |
|
41 } |
|
42 |
|
43 public IValue getValue() throws DebugException { |
|
44 JsValue value = variable.isReadable() |
|
45 ? variable.getValue() |
|
46 : null; |
|
47 if (value == null) { |
|
48 return null; |
|
49 } |
|
50 return wrapValue(value); |
|
51 } |
|
52 |
|
53 public boolean hasValueChanged() throws DebugException { |
|
54 return false; |
|
55 } |
|
56 |
|
57 @SuppressWarnings("unchecked") |
|
58 @Override |
|
59 public Object getAdapter(Class adapter) { |
|
60 if (IWatchExpressionFactoryAdapter.class == adapter) { |
|
61 return new IWatchExpressionFactoryAdapter() { |
|
62 public String createWatchExpression(IVariable variable) throws CoreException { |
|
63 String expression = ((Variable) variable).getJsVariable().getFullyQualifiedName(); |
|
64 if (expression == null) { |
|
65 expression = variable.getName(); |
|
66 } |
|
67 return expression; |
|
68 } |
|
69 }; |
|
70 } |
|
71 return super.getAdapter(adapter); |
|
72 } |
|
73 |
|
74 public void setValue(String expression) throws DebugException { |
|
75 variable.setValue(expression, null); |
|
76 } |
|
77 |
|
78 public void setValue(IValue value) throws DebugException { |
|
79 variable.setValue(((Value) value).getJsValue().getValueString(), null); |
|
80 } |
|
81 |
|
82 public boolean supportsValueModification() { |
|
83 return false; // TODO(apavlov): fix once V8 supports it |
|
84 } |
|
85 |
|
86 public boolean verifyValue(IValue value) throws DebugException { |
|
87 return verifyValue(value.getValueString()); |
|
88 } |
|
89 |
|
90 public boolean verifyValue(String expression) { |
|
91 switch (variable.getValue().getType()) { |
|
92 case TYPE_NUMBER: |
|
93 return ChromiumDebugPluginUtil.isInteger(expression); |
|
94 default: |
|
95 return true; |
|
96 } |
|
97 } |
|
98 |
|
99 public boolean verifyValue(JsValue value) { |
|
100 return verifyValue(value.getValueString()); |
|
101 } |
|
102 |
|
103 private IValue wrapValue(JsValue value) { |
|
104 return Value.create(getDebugTarget(), value); |
|
105 } |
|
106 |
|
107 public JsVariable getJsVariable() { |
|
108 return variable; |
|
109 } |
|
110 |
|
111 } |