2
|
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 |
|
|
8 |
import org.chromium.sdk.JsVariable;
|
|
9 |
import org.chromium.sdk.JsValue.Type;
|
|
10 |
|
|
11 |
/**
|
|
12 |
* A generic implementation of the JsVariable interface.
|
|
13 |
*/
|
|
14 |
public class JsVariableImpl implements JsVariable {
|
|
15 |
|
|
16 |
/**
|
|
17 |
* The variable value data as reported by the JavaScript VM (is used to
|
|
18 |
* construct the variable value.)
|
|
19 |
*/
|
|
20 |
private final ValueMirror valueData;
|
|
21 |
|
|
22 |
/** The call frame this variable belongs in. */
|
|
23 |
private final CallFrameImpl callFrame;
|
|
24 |
|
|
25 |
/** The fully qualified name of this variable. */
|
|
26 |
private final String variableFqn;
|
|
27 |
|
|
28 |
private final NameDecorator nameDecorator;
|
|
29 |
|
|
30 |
/** The lazily constructed value of this variable. */
|
|
31 |
private final JsValueImpl value;
|
|
32 |
|
|
33 |
/** Variable name. */
|
|
34 |
private final String rawName;
|
|
35 |
|
|
36 |
/**
|
|
37 |
* Constructs a variable contained in the given call frame with the given
|
|
38 |
* value mirror.
|
|
39 |
*
|
|
40 |
* @param callFrame that owns this variable
|
|
41 |
* @param valueData value data for this variable
|
|
42 |
*/
|
|
43 |
JsVariableImpl(CallFrameImpl callFrame, ValueMirror valueData, String name) {
|
|
44 |
this(callFrame, valueData, name, null, NameDecorator.NOOP);
|
|
45 |
}
|
|
46 |
|
|
47 |
/**
|
|
48 |
* Constructs a variable contained in the given call frame with the given
|
|
49 |
* value mirror.
|
|
50 |
*
|
|
51 |
* @param callFrame that owns this variable
|
|
52 |
* @param valueData for this variable
|
|
53 |
* @param variableFqn the fully qualified name of this variable
|
|
54 |
*/
|
|
55 |
JsVariableImpl(CallFrameImpl callFrame, ValueMirror valueData, String name, String variableFqn,
|
|
56 |
NameDecorator nameDecorator) {
|
|
57 |
this.callFrame = callFrame;
|
|
58 |
this.valueData = valueData;
|
|
59 |
this.rawName = name;
|
|
60 |
this.variableFqn = variableFqn;
|
|
61 |
this.nameDecorator = nameDecorator;
|
|
62 |
|
|
63 |
Type type = this.valueData.getType();
|
|
64 |
switch (type) {
|
|
65 |
case TYPE_FUNCTION:
|
|
66 |
this.value = new JsFunctionImpl(callFrame, this.variableFqn, this.valueData);
|
|
67 |
break;
|
|
68 |
case TYPE_ERROR:
|
|
69 |
case TYPE_OBJECT:
|
|
70 |
this.value = new JsObjectImpl(callFrame, this.variableFqn, this.valueData);
|
|
71 |
break;
|
|
72 |
case TYPE_ARRAY:
|
|
73 |
this.value = new JsArrayImpl(callFrame, this.variableFqn, this.valueData);
|
|
74 |
break;
|
|
75 |
default:
|
|
76 |
this.value = new JsValueImpl(this.valueData);
|
|
77 |
}
|
|
78 |
}
|
|
79 |
|
|
80 |
/**
|
|
81 |
* @return a [probably compound] JsValue corresponding to this variable.
|
|
82 |
* {@code null} if there was an error lazy-loading the value data.
|
|
83 |
*/
|
|
84 |
public JsValueImpl getValue() {
|
|
85 |
return value;
|
|
86 |
}
|
|
87 |
|
|
88 |
public String getName() {
|
|
89 |
return nameDecorator.decorateVarName(rawName);
|
|
90 |
}
|
|
91 |
|
|
92 |
public String getRawName() {
|
|
93 |
return this.rawName;
|
|
94 |
}
|
|
95 |
|
|
96 |
public boolean isMutable() {
|
|
97 |
return false; // TODO(apavlov): fix once V8 supports it
|
|
98 |
}
|
|
99 |
|
|
100 |
public boolean isReadable() {
|
|
101 |
// TODO(apavlov): implement once the readability metadata are available
|
|
102 |
return true;
|
|
103 |
}
|
|
104 |
|
|
105 |
public synchronized void setValue(String newValue, SetValueCallback callback) {
|
|
106 |
// TODO(apavlov): currently V8 does not support it
|
|
107 |
if (!isMutable()) {
|
|
108 |
throw new UnsupportedOperationException();
|
|
109 |
}
|
|
110 |
}
|
|
111 |
|
|
112 |
@Override
|
|
113 |
public String toString() {
|
|
114 |
return new StringBuilder()
|
|
115 |
.append("[JsVariable: name=")
|
|
116 |
.append(getName())
|
|
117 |
.append(",value=")
|
|
118 |
.append(getValue())
|
|
119 |
.append(']')
|
|
120 |
.toString();
|
|
121 |
}
|
|
122 |
|
|
123 |
/**
|
|
124 |
* Returns the call frame owning this variable.
|
|
125 |
*/
|
|
126 |
protected CallFrameImpl getCallFrame() {
|
|
127 |
return callFrame;
|
|
128 |
}
|
|
129 |
|
|
130 |
public ValueMirror getMirror() {
|
|
131 |
return valueData;
|
|
132 |
}
|
|
133 |
|
|
134 |
public String getFullyQualifiedName() {
|
|
135 |
return variableFqn != null
|
|
136 |
? variableFqn
|
|
137 |
: getName();
|
|
138 |
}
|
|
139 |
|
|
140 |
static abstract class NameDecorator {
|
|
141 |
static final NameDecorator NOOP = new NameDecorator() {
|
|
142 |
@Override
|
|
143 |
String decorateVarName(String rawName) {
|
|
144 |
return rawName;
|
|
145 |
}
|
|
146 |
@Override
|
|
147 |
String buildAccessSuffix(String rawName) {
|
|
148 |
return "." + rawName;
|
|
149 |
}
|
|
150 |
};
|
|
151 |
abstract String decorateVarName(String rawName);
|
|
152 |
abstract String buildAccessSuffix(String rawName);
|
|
153 |
}
|
|
154 |
}
|