forked from jruby/jruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColon2ConstNode.java
More file actions
81 lines (65 loc) · 2.74 KB
/
Copy pathColon2ConstNode.java
File metadata and controls
81 lines (65 loc) · 2.74 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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.jruby.ast;
import org.jruby.Ruby;
import org.jruby.RubyModule;
import org.jruby.exceptions.JumpException;
import org.jruby.javasupport.util.RuntimeHelpers;
import org.jruby.lexer.yacc.ISourcePosition;
import org.jruby.runtime.Block;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
/**
*
* @author enebo
*/
public class Colon2ConstNode extends Colon2Node {
private volatile transient IRubyObject cachedValue = null;
private volatile Object generation = -1;
private volatile int hash = -1;
public Colon2ConstNode(ISourcePosition position, Node leftNode, String name) {
super(position, leftNode, name);
assert leftNode != null: "Colon2ConstNode cannot have null leftNode";
}
@Override
public IRubyObject interpret(Ruby runtime, ThreadContext context, IRubyObject self, Block aBlock) {
RubyModule target = RuntimeHelpers.checkIsModule(leftNode.interpret(runtime, context, self, aBlock));
IRubyObject value = getValue(context, target);
return value != null ? value : target.getConstantFromConstMissing(name);
}
@Override
public ByteList definition(Ruby runtime, ThreadContext context, IRubyObject self, Block aBlock) {
IRubyObject lastError = context.getErrorInfo();
try {
if (RuntimeHelpers.isModuleAndHasConstant(leftNode.interpret(runtime, context, self, aBlock), name)) return CONSTANT_BYTELIST;
} catch (JumpException e) {
// replace lastError
context.setErrorInfo(lastError);
}
return null;
}
public IRubyObject getValue(ThreadContext context, RubyModule target) {
IRubyObject value = cachedValue; // Store to temp so it does null out on us mid-stream
return isCached(context, target, value) ? value : reCache(context, target);
}
private boolean isCached(ThreadContext context, RubyModule target, IRubyObject value) {
// We could probably also detect if LHS value came out of cache and avoid some of this
return
value != null &&
generation == context.getRuntime().getConstantInvalidator().getData() &&
hash == target.hashCode();
}
public IRubyObject reCache(ThreadContext context, RubyModule target) {
Object newGeneration = context.getRuntime().getConstantInvalidator().getData();
IRubyObject value = target.getConstantFromNoConstMissing(name);
cachedValue = value;
if (value != null) {
generation = newGeneration;
hash = target.hashCode();
}
return value;
}
}