forked from jruby/jruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIREvalScript.java
More file actions
147 lines (124 loc) · 5.14 KB
/
Copy pathIREvalScript.java
File metadata and controls
147 lines (124 loc) · 5.14 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
package org.jruby.ir;
import java.util.ArrayList;
import java.util.List;
import org.jruby.RubyModule;
import org.jruby.ir.operands.ClosureLocalVariable;
import org.jruby.ir.operands.Label;
import org.jruby.ir.operands.LocalVariable;
import org.jruby.ir.operands.Operand;
import org.jruby.ir.interpreter.Interpreter;
import org.jruby.ir.runtime.IRRuntimeHelpers;
import org.jruby.parser.IRStaticScope;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.Block;
import org.jruby.runtime.DynamicScope;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.log.Logger;
import org.jruby.util.log.LoggerFactory;
public class IREvalScript extends IRClosure {
private static final Logger LOG = LoggerFactory.getLogger("IREvalScript");
private IRScope nearestNonEvalScope;
private int nearestNonEvalScopeDepth;
private List<IRClosure> beginBlocks;
private List<IRClosure> endBlocks;
public IREvalScript(IRManager manager, IRScope lexicalParent, String fileName,
int lineNumber, StaticScope staticScope) {
super(manager, lexicalParent, fileName, lineNumber, staticScope, "EVAL_");
int n = 0;
IRScope s = lexicalParent;
while (s instanceof IREvalScript) {
n++;
s = s.getLexicalParent();
}
this.nearestNonEvalScope = s;
this.nearestNonEvalScopeDepth = n;
this.nearestNonEvalScope.initEvalScopeVariableAllocator(false);
}
@Override
public Label getNewLabel() {
return getNewLabel("EV" + closureId + "_LBL");
}
@Override
public String getScopeName() {
return "EvalScript";
}
@Override
public Operand[] getBlockArgs() {
return new Operand[0];
}
/* Record a begin block -- not all scope implementations can handle them */
@Override
public void recordBeginBlock(IRClosure beginBlockClosure) {
if (beginBlocks == null) beginBlocks = new ArrayList<IRClosure>();
beginBlocks.add(beginBlockClosure);
}
/* Record an end block -- not all scope implementations can handle them */
@Override
public void recordEndBlock(IRClosure endBlockClosure) {
if (endBlocks == null) endBlocks = new ArrayList<IRClosure>();
endBlocks.add(endBlockClosure);
}
public List<IRClosure> getBeginBlocks() {
return beginBlocks;
}
public List<IRClosure> getEndBlocks() {
return endBlocks;
}
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, DynamicScope evalScope, Block block, String backtraceName) {
if (IRRuntimeHelpers.isDebug()) {
LOG.info("CFG:\n" + cfg());
}
try {
context.pushScope(evalScope);
// Since IR introduces additional local vars, we may need to grow the dynamic scope.
// To do that, IREvalScript has to tell the dyn-scope how many local vars there are.
// Since the same static scope (the scope within which the eval string showed up)
// might be shared by multiple eval-scripts, we cannot 'setIRScope(this)' once and
// forget about it. We need to set this right before we are ready to grow the
// dynamic scope local var space.
((IRStaticScope)getStaticScope()).setIRScope(this);
evalScope.growIfNeeded();
// FIXME: Do not push new empty arg array in every time
return Interpreter.INTERPRET_EVAL(context, self, this, clazz, new IRubyObject[] {}, backtraceName, block, null);
}
finally {
context.popScope();
}
}
@Override
public LocalVariable findExistingLocalVariable(String name, int scopeDepth) {
// Look in the nearest non-eval scope's shared eval scope vars first.
// If you dont find anything there, look in the nearest non-eval scope's regular vars.
LocalVariable lvar = nearestNonEvalScope.evalScopeVars.getVariable(name);
if ((lvar != null) || scopeDepth == 0) return lvar;
else return nearestNonEvalScope.findExistingLocalVariable(name, scopeDepth-nearestNonEvalScopeDepth-1);
}
@Override
public LocalVariable getNewLocalVariable(String name, int depth) {
assert depth == nearestNonEvalScopeDepth: "Local variable depth in IREvalScript:getNewLocalVariable must be " + nearestNonEvalScopeDepth + ". Got " + depth;
LocalVariable lvar = new ClosureLocalVariable(this, name, 0, nearestNonEvalScope.evalScopeVars.nextSlot);
nearestNonEvalScope.evalScopeVars.putVariable(name, lvar);
return lvar;
}
@Override
public LocalVariable getNewFlipStateVariable() {
return getLocalVariable("%flip_" + allocateNextPrefixedName("%flip"), 0);
}
@Override
public int getUsedVariablesCount() {
return 1 + nearestNonEvalScope.evalScopeVars.nextSlot + getPrefixCountSize("%flip");
}
@Override
public boolean isScriptScope() {
return true;
}
@Override
public boolean isTopLocalVariableScope() {
return false;
}
@Override
public boolean isFlipScope() {
return true;
}
}