// popFrame clears the frame before returning, getFrames clones them for backtrace
// i.e. only name and backtrace data is copied...
Stack<Frame> frames = new Stack<>();
Stack<IRubyObject> selfs = new Stack<>();
for (Frame cur = context.getCurrentFrame(); cur != null; ) {
Frame clone = new Frame();
clone.updateFrame(cur); // clone...
frames.push(clone);
selfs.push(context.getFrameSelf());
context.popFrame();
try {
cur = context.getCurrentFrame();
} catch (ArrayIndexOutOfBoundsException e) {
break;
}
}
Stack<DynamicScope> scopes = new Stack<>();
for (DynamicScope scope = context.getCurrentScope(); scopes.size() != frames.size(); ) {
scopes.push(scope);
context.popScope();
try {
scope = context.getCurrentScope();
} catch (ArrayIndexOutOfBoundsException aioe) {
break;
}
}
RubyStackTraceElement[] trace = TraceType.Gather.NORMAL.getBacktraceData(context, false).getBacktrace(runtime);
/*int traceEnd = trace.length;
for (int i = 0; i < traceEnd; i++) {
RubyStackTraceElement rste = trace[i];
if (!rste.getFileName().equals(getName())) {
trace[i] = null;
if (i + 1 < traceEnd)
System.arraycopy(trace, i + 1, trace, i, (traceEnd - i) - 1);
traceEnd--;
trace[traceEnd] = null;
}
}*/
assert frames.size() == scopes.size() : "Frames don't match scopes :S";
for (int i = 0; i < frames.size(); i++) {
Frame frame = frames.get(i);
IRubyObject s = selfs.get(i);
DynamicScope scope = scopes.get(i);
RubyStackTraceElement stackElement = trace[i];
StaticScope sScope = scope.getStaticScope();
Map<String, Object> var = new HashMap<>();