forked from mozilla/rhino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLambdaSlot.java
More file actions
65 lines (56 loc) · 1.99 KB
/
LambdaSlot.java
File metadata and controls
65 lines (56 loc) · 1.99 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
package org.mozilla.javascript;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* This is a specialization of property access using some lambda functions. It behaves exactly like
* any other slot that has only a value, but instead of getting the value directly, it comes from
* calling the functions. This makes it different from GetterSlot, which lets the user see directly
* that there is a getter or a setter function involved. This makes this class useful for
* implementing properties that behave like any other JavaScript property but which are implemented
* using some native functionality without using reflection.
*/
public class LambdaSlot extends Slot {
private static final long serialVersionUID = -3046681698806493052L;
LambdaSlot(Slot oldSlot) {
super(oldSlot);
}
transient Supplier<Object> getter;
transient Consumer<Object> setter;
@Override
boolean isValueSlot() {
return false;
}
@Override
boolean isSetterSlot() {
return false;
}
@Override
ScriptableObject getPropertyDescriptor(Context cx, Scriptable scope) {
ScriptableObject desc = (ScriptableObject) cx.newObject(scope);
if (getter != null) {
desc.defineProperty("value", getter.get(), ScriptableObject.EMPTY);
} else {
desc.defineProperty("value", value, ScriptableObject.EMPTY);
}
desc.setCommonDescriptorProperties(getAttributes(), true);
return desc;
}
@Override
public boolean setValue(Object value, Scriptable owner, Scriptable start) {
if (setter != null) {
if (owner == start) {
setter.accept(value);
return true;
}
return false;
}
return super.setValue(value, owner, start);
}
@Override
public Object getValue(Scriptable start) {
if (getter != null) {
return getter.get();
}
return super.getValue(start);
}
}