forked from mozilla/rhino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccessorSlot.java
More file actions
202 lines (171 loc) · 6.62 KB
/
AccessorSlot.java
File metadata and controls
202 lines (171 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package org.mozilla.javascript;
/**
* This is a specialization of Slot to store various types of values that are retrieved dynamically
* using Java and JavaScript functions. Unlike LambdaSlot, the fact that these values are accessed
* and mutated by functions is visible via the slot's property descriptor.
*/
public class AccessorSlot extends Slot {
private static final long serialVersionUID = 1677840254177335827L;
AccessorSlot(Slot oldSlot) {
super(oldSlot);
}
// The Getter and Setter may each be of a different type (JavaScript function, Java
// function, or neither). So, use an abstraction to distinguish them.
transient Getter getter;
transient Setter setter;
@Override
boolean isValueSlot() {
return false;
}
@Override
boolean isSetterSlot() {
return true;
}
@Override
ScriptableObject getPropertyDescriptor(Context cx, Scriptable scope) {
// It sounds logical that this would be the same as the logic for a normal Slot,
// but the spec is super pedantic about things like the order of properties here,
// so we need special support here.
ScriptableObject desc = (ScriptableObject) cx.newObject(scope);
desc.setCommonDescriptorProperties(getAttributes(), getter == null && setter == null);
String fName = name == null ? "f" : name.toString();
if (getter != null) {
Function f = getter.asGetterFunction(fName, scope);
desc.defineProperty("get", f == null ? Undefined.instance : f, ScriptableObject.EMPTY);
}
if (setter != null) {
Function f = setter.asSetterFunction(fName, scope);
desc.defineProperty("set", f == null ? Undefined.instance : f, ScriptableObject.EMPTY);
}
return desc;
}
@Override
public boolean setValue(Object value, Scriptable owner, Scriptable start) {
if (setter == null) {
if (getter != null) {
throwNoSetterException(start, value);
return true;
}
} else {
return setter.setValue(value, owner, start);
}
return super.setValue(value, owner, start);
}
@Override
public Object getValue(Scriptable start) {
if (getter != null) {
return getter.getValue(start);
}
return super.getValue(start);
}
@Override
Function getSetterFunction(String name, Scriptable scope) {
if (setter == null) {
return null;
}
return setter.asSetterFunction(name, scope);
}
@Override
Function getGetterFunction(String name, Scriptable scope) {
if (getter == null) {
return null;
}
return getter.asGetterFunction(name, scope);
}
interface Getter {
Object getValue(Scriptable start);
Function asGetterFunction(final String name, final Scriptable scope);
}
/** This is a Getter that delegates to a Java function via a MemberBox. */
static final class MemberBoxGetter implements Getter {
final MemberBox member;
MemberBoxGetter(MemberBox member) {
this.member = member;
}
@Override
public Object getValue(Scriptable start) {
if (member.delegateTo == null) {
return member.invoke(start, ScriptRuntime.emptyArgs);
}
return member.invoke(member.delegateTo, new Object[] {start});
}
@Override
public Function asGetterFunction(String name, Scriptable scope) {
return member.asGetterFunction(name, scope);
}
}
/** This is a getter that delegates to a JavaScript function. */
static final class FunctionGetter implements Getter {
// The value of the function might actually be Undefined, so we need an Object here.
final Object target;
FunctionGetter(Object target) {
this.target = target;
}
@Override
public Object getValue(Scriptable start) {
if (target instanceof Function) {
Function t = (Function) target;
Context cx = Context.getContext();
return t.call(cx, t.getParentScope(), start, ScriptRuntime.emptyArgs);
}
return Undefined.instance;
}
@Override
public Function asGetterFunction(String name, Scriptable scope) {
return target instanceof Function ? (Function) target : null;
}
}
interface Setter {
boolean setValue(Object value, Scriptable owner, Scriptable start);
Function asSetterFunction(final String name, final Scriptable scope);
}
/** Invoke the setter on this slot via reflection using MemberBox. */
static final class MemberBoxSetter implements Setter {
final MemberBox member;
MemberBoxSetter(MemberBox member) {
this.member = member;
}
@Override
public boolean setValue(Object value, Scriptable owner, Scriptable start) {
Context cx = Context.getContext();
Class<?>[] pTypes = member.argTypes;
// XXX: cache tag since it is already calculated in
// defineProperty ?
Class<?> valueType = pTypes[pTypes.length - 1];
int tag = FunctionObject.getTypeTag(valueType);
Object actualArg = FunctionObject.convertArg(cx, start, value, tag);
if (member.delegateTo == null) {
member.invoke(start, new Object[] {actualArg});
} else {
member.invoke(member.delegateTo, new Object[] {start, actualArg});
}
return true;
}
@Override
public Function asSetterFunction(String name, Scriptable scope) {
return member.asSetterFunction(name, scope);
}
}
/**
* Invoke the setter as a JavaScript function, taking care that it might actually be Undefined.
*/
static final class FunctionSetter implements Setter {
final Object target;
FunctionSetter(Object target) {
this.target = target;
}
@Override
public boolean setValue(Object value, Scriptable owner, Scriptable start) {
if (target instanceof Function) {
Function t = (Function) target;
Context cx = Context.getContext();
t.call(cx, t.getParentScope(), start, new Object[] {value});
}
return true;
}
@Override
public Function asSetterFunction(String name, Scriptable scope) {
return target instanceof Function ? (Function) target : null;
}
}
}