Repository navigation
Expand file tree
/
Copy pathThreadContext.java
More file actions
1358 lines (1122 loc) · 44 KB
/
Copy pathThreadContext.java
File metadata and controls
1358 lines (1122 loc) · 44 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
***** BEGIN LICENSE BLOCK *****
* Version: EPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
* Copyright (C) 2002-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
* Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
* Copyright (C) 2004-2005 Charles O Nutter <headius@headius.com>
* Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
* Copyright (C) 2006 Michael Studman <codehaus@michaelstudman.com>
* Copyright (C) 2006 Miguel Covarrubias <mlcovarrubias@gmail.com>
* Copyright (C) 2007 William N Dortch <bill.dortch@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby.runtime;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyClass;
import org.jruby.RubyContinuation.Continuation;
import org.jruby.RubyInstanceConfig;
import org.jruby.RubyModule;
import org.jruby.RubyString;
import org.jruby.RubyThread;
import org.jruby.ast.executable.RuntimeCache;
import org.jruby.exceptions.JumpException.ReturnJump;
import org.jruby.internal.runtime.methods.DynamicMethod;
import org.jruby.lexer.yacc.ISourcePosition;
import org.jruby.ext.fiber.Fiber;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.backtrace.BacktraceElement;
import org.jruby.runtime.backtrace.RubyStackTraceElement;
import org.jruby.runtime.backtrace.TraceType;
import org.jruby.runtime.backtrace.TraceType.Gather;
import org.jruby.runtime.backtrace.BacktraceElement;
import org.jruby.runtime.backtrace.RubyStackTraceElement;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.profile.ProfileData;
import org.jruby.runtime.scope.ManyVarsDynamicScope;
import org.jruby.util.RecursiveComparator;
import org.jruby.util.RubyDateFormat;
import org.jruby.util.log.Logger;
import org.jruby.util.log.LoggerFactory;
public final class ThreadContext {
private static final Logger LOG = LoggerFactory.getLogger("ThreadContext");
public static ThreadContext newContext(Ruby runtime) {
ThreadContext context = new ThreadContext(runtime);
return context;
}
private final static int INITIAL_SIZE = 10;
private final static int INITIAL_FRAMES_SIZE = 10;
/** The number of calls after which to do a thread event poll */
private final static int CALL_POLL_COUNT = 0xFFF;
// runtime, nil, and runtimeCache cached here for speed of access from any thread
public final Ruby runtime;
public final IRubyObject nil;
public final RuntimeCache runtimeCache;
public final boolean is19;
// Is this thread currently with in a function trace?
private boolean isWithinTrace;
private RubyThread thread;
private Fiber fiber;
// Cache format string because it is expensive to create on demand
private RubyDateFormat dateFormat;
private RubyModule[] parentStack = new RubyModule[INITIAL_SIZE];
private int parentIndex = -1;
private Frame[] frameStack = new Frame[INITIAL_FRAMES_SIZE];
private int frameIndex = -1;
private BacktraceElement[] backtrace = new BacktraceElement[INITIAL_FRAMES_SIZE];
private int backtraceIndex = -1;
// List of active dynamic scopes. Each of these may have captured other dynamic scopes
// to implement closures.
private DynamicScope[] scopeStack = new DynamicScope[INITIAL_SIZE];
private int scopeIndex = -1;
private static final Continuation[] EMPTY_CATCHTARGET_STACK = new Continuation[0];
private Continuation[] catchStack = EMPTY_CATCHTARGET_STACK;
private int catchIndex = -1;
private boolean isProfiling = false;
// The flat profile data for this thread
private ProfileData profileData;
// In certain places, like grep, we don't use real frames for the
// call blocks. This has the effect of not setting the backref in
// the correct frame - this delta is activated to the place where
// the grep is running in so that the backref will be set in an
// appropriate place.
private int rubyFrameDelta = 0;
private boolean eventHooksEnabled = true;
CallType lastCallType;
Visibility lastVisibility;
IRubyObject lastExitStatus;
/**
* Constructor for Context.
*/
private ThreadContext(Ruby runtime) {
this.runtime = runtime;
this.nil = runtime.getNil();
this.is19 = runtime.is1_9();
if (runtime.getInstanceConfig().isProfilingEntireRun())
startProfiling();
this.runtimeCache = runtime.getRuntimeCache();
// TOPLEVEL self and a few others want a top-level scope. We create this one right
// away and then pass it into top-level parse so it ends up being the top level.
StaticScope topStaticScope = runtime.getStaticScopeFactory().newLocalScope(null);
pushScope(new ManyVarsDynamicScope(topStaticScope, null));
Frame[] stack = frameStack;
int length = stack.length;
for (int i = 0; i < length; i++) {
stack[i] = new Frame();
}
BacktraceElement[] stack2 = backtrace;
int length2 = stack2.length;
for (int i = 0; i < length2; i++) {
stack2[i] = new BacktraceElement();
}
ThreadContext.pushBacktrace(this, "", "", 0);
ThreadContext.pushBacktrace(this, "", "", 0);
fiber = (Fiber) runtime.getRootFiber();
}
@Override
protected void finalize() throws Throwable {
if (thread != null) {
thread.dispose();
}
}
/**
* Retrieve the runtime associated with this context.
*
* Note that there's no reason to call this method rather than accessing the
* runtime field directly.
*
* @see ThreadContext#runtime
*
* @return the runtime associated with this context
*/
public final Ruby getRuntime() {
return runtime;
}
public IRubyObject getErrorInfo() {
return thread.getErrorInfo();
}
public IRubyObject setErrorInfo(IRubyObject errorInfo) {
thread.setErrorInfo(errorInfo);
return errorInfo;
}
public ReturnJump returnJump(IRubyObject value) {
return new ReturnJump(getFrameJumpTarget(), value);
}
/**
* Returns the lastCallStatus.
* @return LastCallStatus
*/
public void setLastCallStatus(CallType callType) {
lastCallType = callType;
}
public CallType getLastCallType() {
return lastCallType;
}
public void setLastVisibility(Visibility visibility) {
lastVisibility = visibility;
}
public Visibility getLastVisibility() {
return lastVisibility;
}
public void setLastCallStatusAndVisibility(CallType callType, Visibility visibility) {
lastCallType = callType;
lastVisibility = visibility;
}
public IRubyObject getLastExitStatus() {
return lastExitStatus;
}
public void setLastExitStatus(IRubyObject lastExitStatus) {
this.lastExitStatus = lastExitStatus;
}
public void printScope() {
LOG.debug("SCOPE STACK:");
for (int i = 0; i <= scopeIndex; i++) {
LOG.debug("{}", scopeStack[i]);
}
}
public DynamicScope getCurrentScope() {
return scopeStack[scopeIndex];
}
public StaticScope getCurrentStaticScope() {
return scopeStack[scopeIndex].getStaticScope();
}
public DynamicScope getPreviousScope() {
return scopeStack[scopeIndex - 1];
}
private void expandFramesIfNecessary() {
int newSize = frameStack.length * 2;
frameStack = fillNewFrameStack(new Frame[newSize], newSize);
}
private Frame[] fillNewFrameStack(Frame[] newFrameStack, int newSize) {
System.arraycopy(frameStack, 0, newFrameStack, 0, frameStack.length);
for (int i = frameStack.length; i < newSize; i++) {
newFrameStack[i] = new Frame();
}
return newFrameStack;
}
private void expandParentsIfNecessary() {
int newSize = parentStack.length * 2;
RubyModule[] newParentStack = new RubyModule[newSize];
System.arraycopy(parentStack, 0, newParentStack, 0, parentStack.length);
parentStack = newParentStack;
}
public void pushScope(DynamicScope scope) {
int index = ++scopeIndex;
DynamicScope[] stack = scopeStack;
stack[index] = scope;
if (index + 1 == stack.length) {
expandScopesIfNecessary();
}
}
public void popScope() {
scopeStack[scopeIndex--] = null;
}
private void expandScopesIfNecessary() {
int newSize = scopeStack.length * 2;
DynamicScope[] newScopeStack = new DynamicScope[newSize];
System.arraycopy(scopeStack, 0, newScopeStack, 0, scopeStack.length);
scopeStack = newScopeStack;
}
public RubyThread getThread() {
return thread;
}
public RubyDateFormat getRubyDateFormat() {
if (dateFormat == null) dateFormat = new RubyDateFormat("-", Locale.US, runtime.is1_9());
return dateFormat;
}
public void setThread(RubyThread thread) {
this.thread = thread;
// associate the thread with this context, unless we're clearing the reference
if (thread != null) {
thread.setContext(this);
}
}
public Fiber getFiber() {
return fiber;
}
public void setFiber(Fiber fiber) {
this.fiber = fiber;
}
//////////////////// CATCH MANAGEMENT ////////////////////////
private void expandCatchIfNecessary() {
int newSize = catchStack.length * 2;
if (newSize == 0) newSize = 1;
Continuation[] newCatchStack = new Continuation[newSize];
System.arraycopy(catchStack, 0, newCatchStack, 0, catchStack.length);
catchStack = newCatchStack;
}
public void pushCatch(Continuation catchTarget) {
int index = ++catchIndex;
if (index == catchStack.length) {
expandCatchIfNecessary();
}
catchStack[index] = catchTarget;
}
public void popCatch() {
catchIndex--;
}
/**
* Find the active Continuation for the given tag. Must be called with an
* interned string.
*
* @param tag The interned string to search for
* @return The continuation associated with this tag
*/
public Continuation getActiveCatch(Object tag) {
for (int i = catchIndex; i >= 0; i--) {
Continuation c = catchStack[i];
if (runtime.is1_9()) {
if (c.tag == tag) return c;
} else {
if (c.tag.equals(tag)) return c;
}
}
return null;
}
//////////////////// FRAME MANAGEMENT ////////////////////////
private void pushFrameCopy() {
int index = ++this.frameIndex;
Frame[] stack = frameStack;
Frame currentFrame = stack[index - 1];
stack[index].updateFrame(currentFrame);
if (index + 1 == stack.length) {
expandFramesIfNecessary();
}
}
private Frame pushFrame(Frame frame) {
int index = ++this.frameIndex;
Frame[] stack = frameStack;
stack[index] = frame;
if (index + 1 == stack.length) {
expandFramesIfNecessary();
}
return frame;
}
private void pushCallFrame(RubyModule clazz, String name,
IRubyObject self, Block block) {
int index = ++this.frameIndex;
Frame[] stack = frameStack;
stack[index].updateFrame(clazz, self, name, block, callNumber);
if (index + 1 == stack.length) {
expandFramesIfNecessary();
}
}
private void pushEvalFrame(IRubyObject self) {
int index = ++this.frameIndex;
Frame[] stack = frameStack;
stack[index].updateFrameForEval(self, callNumber);
if (index + 1 == stack.length) {
expandFramesIfNecessary();
}
}
private void pushFrame(String name) {
int index = ++this.frameIndex;
Frame[] stack = frameStack;
stack[index].updateFrame(name);
if (index + 1 == stack.length) {
expandFramesIfNecessary();
}
}
public void pushFrame() {
int index = ++this.frameIndex;
Frame[] stack = frameStack;
if (index + 1 == stack.length) {
expandFramesIfNecessary();
}
}
public void popFrame() {
Frame frame = frameStack[frameIndex--];
frame.clear();
}
private void popFrameReal(Frame oldFrame) {
frameStack[frameIndex--] = oldFrame;
}
public Frame getCurrentFrame() {
return frameStack[frameIndex];
}
public int getRubyFrameDelta() {
return this.rubyFrameDelta;
}
public void setRubyFrameDelta(int newDelta) {
this.rubyFrameDelta = newDelta;
}
public Frame getCurrentRubyFrame() {
return frameStack[frameIndex-rubyFrameDelta];
}
public Frame getNextFrame() {
int index = frameIndex;
Frame[] stack = frameStack;
if (index + 1 == stack.length) {
expandFramesIfNecessary();
}
return stack[index + 1];
}
public Frame getPreviousFrame() {
int index = frameIndex;
return index < 1 ? null : frameStack[index - 1];
}
public int getFrameCount() {
return frameIndex + 1;
}
public Frame[] getFrames(int delta) {
int top = frameIndex + delta;
Frame[] frames = new Frame[top + 1];
for (int i = 0; i <= top; i++) {
frames[i] = frameStack[i].duplicateForBacktrace();
}
return frames;
}
/////////////////// BACKTRACE ////////////////////
private static void expandBacktraceIfNecessary(ThreadContext context) {
int newSize = context.backtrace.length * 2;
context.backtrace = fillNewBacktrace(context, new BacktraceElement[newSize], newSize);
}
private static BacktraceElement[] fillNewBacktrace(ThreadContext context, BacktraceElement[] newBacktrace, int newSize) {
System.arraycopy(context.backtrace, 0, newBacktrace, 0, context.backtrace.length);
for (int i = context.backtrace.length; i < newSize; i++) {
newBacktrace[i] = new BacktraceElement();
}
return newBacktrace;
}
public static void pushBacktrace(ThreadContext context, String method, ISourcePosition position) {
int index = ++context.backtraceIndex;
BacktraceElement[] stack = context.backtrace;
BacktraceElement.update(stack[index], method, position);
if (index + 1 == stack.length) {
ThreadContext.expandBacktraceIfNecessary(context);
}
}
public static void pushBacktrace(ThreadContext context, String method, String file, int line) {
int index = ++context.backtraceIndex;
BacktraceElement[] stack = context.backtrace;
BacktraceElement.update(stack[index], method, file, line);
if (index + 1 == stack.length) {
ThreadContext.expandBacktraceIfNecessary(context);
}
}
public static void popBacktrace(ThreadContext context) {
context.backtraceIndex--;
}
/**
* Search the frame stack for the given JumpTarget. Return true if it is
* found and false otherwise. Skip the given number of frames before
* beginning the search.
*
* @param target The JumpTarget to search for
* @param skipFrames The number of frames to skip before searching
* @return
*/
public boolean isJumpTargetAlive(int target, int skipFrames) {
for (int i = frameIndex - skipFrames; i >= 0; i--) {
if (frameStack[i].getJumpTarget() == target) return true;
}
return false;
}
/**
* Check if a static scope is present on the call stack.
* This is the IR equivalent of isJumpTargetAlive
*
* @param s the static scope to look for
* @return true if it exists
* false if not
**/
public boolean scopeExistsOnCallStack(StaticScope s) {
DynamicScope[] stack = scopeStack;
for (int i = scopeIndex; i >= 0; i--) {
if (stack[i].getStaticScope() == s) return true;
}
return false;
}
public String getFrameName() {
return getCurrentFrame().getName();
}
public IRubyObject getFrameSelf() {
return getCurrentFrame().getSelf();
}
public int getFrameJumpTarget() {
return getCurrentFrame().getJumpTarget();
}
public RubyModule getFrameKlazz() {
return getCurrentFrame().getKlazz();
}
public Block getFrameBlock() {
return getCurrentFrame().getBlock();
}
public String getFile() {
return backtrace[backtraceIndex].filename;
}
public int getLine() {
return backtrace[backtraceIndex].line;
}
public void setLine(int line) {
backtrace[backtraceIndex].line = line;
}
public void setFileAndLine(String file, int line) {
BacktraceElement b = backtrace[backtraceIndex];
b.filename = file;
b.line = line;
}
public void setFileAndLine(ISourcePosition position) {
BacktraceElement b = backtrace[backtraceIndex];
b.filename = position.getFile();
b.line = position.getStartLine();
}
public Visibility getCurrentVisibility() {
return getCurrentFrame().getVisibility();
}
public Visibility getPreviousVisibility() {
return getPreviousFrame().getVisibility();
}
public void setCurrentVisibility(Visibility visibility) {
getCurrentFrame().setVisibility(visibility);
}
public void pollThreadEvents() {
thread.pollThreadEvents(this);
}
public int callNumber = 0;
public int getCurrentTarget() {
return callNumber;
}
public void callThreadPoll() {
if ((callNumber++ & CALL_POLL_COUNT) == 0) pollThreadEvents();
}
public static void callThreadPoll(ThreadContext context) {
if ((context.callNumber++ & CALL_POLL_COUNT) == 0) context.pollThreadEvents();
}
public void trace(RubyEvent event, String name, RubyModule implClass) {
trace(event, name, implClass, backtrace[backtraceIndex].filename, backtrace[backtraceIndex].line);
}
public void trace(RubyEvent event, String name, RubyModule implClass, String file, int line) {
runtime.callEventHooks(this, event, file, line, name, implClass);
}
public void pushRubyClass(RubyModule currentModule) {
// FIXME: this seems like a good assertion, but it breaks compiled code and the code seems
// to run without it...
//assert currentModule != null : "Can't push null RubyClass";
int index = ++parentIndex;
RubyModule[] stack = parentStack;
stack[index] = currentModule;
if (index + 1 == stack.length) {
expandParentsIfNecessary();
}
}
public RubyModule popRubyClass() {
int index = parentIndex;
RubyModule[] stack = parentStack;
RubyModule ret = stack[index];
stack[index] = null;
parentIndex = index - 1;
return ret;
}
public RubyModule getRubyClass() {
assert parentIndex != -1 : "Trying to get RubyClass from empty stack";
RubyModule parentModule = parentStack[parentIndex];
return parentModule.getNonIncludedClass();
}
public RubyModule getPreviousRubyClass() {
assert parentIndex != 0 : "Trying to get RubyClass from too-shallow stack";
RubyModule parentModule = parentStack[parentIndex - 1];
return parentModule.getNonIncludedClass();
}
@Deprecated
public boolean getConstantDefined(String internedName) {
return getCurrentStaticScope().isConstantDefined(internedName);
}
/**
* Used by the evaluator and the compiler to look up a constant by name
*/
@Deprecated
public IRubyObject getConstant(String internedName) {
return getCurrentStaticScope().getConstant(internedName);
}
/**
* Used by the evaluator and the compiler to set a constant by name
* This is for a null const decl
*/
@Deprecated
public IRubyObject setConstantInCurrent(String internedName, IRubyObject result) {
return getCurrentStaticScope().setConstant(internedName, result);
}
/**
* Used by the evaluator and the compiler to set a constant by name.
* This is for a Colon2 const decl
*/
@Deprecated
public IRubyObject setConstantInModule(String internedName, IRubyObject target, IRubyObject result) {
if (!(target instanceof RubyModule)) {
throw runtime.newTypeError(target.toString() + " is not a class/module");
}
RubyModule module = (RubyModule)target;
module.setConstant(internedName, result);
return result;
}
/**
* Used by the evaluator and the compiler to set a constant by name
* This is for a Colon2 const decl
*/
@Deprecated
public IRubyObject setConstantInObject(String internedName, IRubyObject result) {
runtime.getObject().setConstant(internedName, result);
return result;
}
private static void addBackTraceElement(Ruby runtime, RubyArray backtrace, RubyStackTraceElement element) {
RubyString str = RubyString.newString(runtime, element.mriStyleString());
backtrace.append(str);
}
/**
* Create an Array with backtrace information for Kernel#caller
* @param runtime
* @param level
* @return an Array with the backtrace
*/
public IRubyObject createCallerBacktrace(Ruby runtime, int level) {
runtime.incrementCallerCount();
RubyStackTraceElement[] trace = gatherCallerBacktrace();
RubyArray newTrace = runtime.newArray(trace.length - level);
for (int i = level; i < trace.length; i++) {
addBackTraceElement(runtime, newTrace, trace[i]);
}
if (RubyInstanceConfig.LOG_CALLERS) TraceType.dumpCaller(newTrace);
return newTrace;
}
/**
* Create an Array with backtrace information for a built-in warning
* @param runtime
* @return an Array with the backtrace
*/
public RubyStackTraceElement[] createWarningBacktrace(Ruby runtime) {
runtime.incrementWarningCount();
RubyStackTraceElement[] trace = gatherCallerBacktrace();
if (RubyInstanceConfig.LOG_WARNINGS) TraceType.dumpWarning(trace);
return trace;
}
public RubyStackTraceElement[] gatherCallerBacktrace() {
Thread nativeThread = thread.getNativeThread();
// Future thread or otherwise unforthgiving thread impl.
if (nativeThread == null) return new RubyStackTraceElement[] {};
BacktraceElement[] copy = new BacktraceElement[backtraceIndex + 1];
System.arraycopy(backtrace, 0, copy, 0, backtraceIndex + 1);
RubyStackTraceElement[] trace = Gather.CALLER.getBacktraceData(this, false).getBacktrace(runtime);
return trace;
}
/**
* Create an Array with backtrace information.
* @param level
* @param nativeException
* @return an Array with the backtrace
*/
public Frame[] createBacktrace(int level, boolean nativeException) {
int traceSize = frameIndex - level + 1;
Frame[] traceFrames;
if (traceSize <= 0) return null;
if (nativeException) {
// assert level == 0;
traceFrames = new Frame[traceSize + 1];
traceFrames[traceSize] = frameStack[frameIndex];
} else {
traceFrames = new Frame[traceSize];
}
System.arraycopy(frameStack, 0, traceFrames, 0, traceSize);
return traceFrames;
}
public boolean isEventHooksEnabled() {
return eventHooksEnabled;
}
public void setEventHooksEnabled(boolean flag) {
eventHooksEnabled = flag;
}
/**
* Create an Array with backtrace information.
* @param level
* @param nativeException
* @return an Array with the backtrace
*/
public BacktraceElement[] createBacktrace2(int level, boolean nativeException) {
BacktraceElement[] newTrace = new BacktraceElement[backtraceIndex + 1];
for (int i = 0; i <= backtraceIndex; i++) {
newTrace[i] = backtrace[i].clone();
}
return newTrace;
}
private static String createRubyBacktraceString(StackTraceElement element) {
return element.getFileName() + ":" + element.getLineNumber() + ":in `" + element.getMethodName() + "'";
}
public static String createRawBacktraceStringFromThrowable(Throwable t) {
StackTraceElement[] javaStackTrace = t.getStackTrace();
StringBuilder buffer = new StringBuilder();
if (javaStackTrace != null && javaStackTrace.length > 0) {
StackTraceElement element = javaStackTrace[0];
buffer
.append(createRubyBacktraceString(element))
.append(": ")
.append(t.toString())
.append("\n");
for (int i = 1; i < javaStackTrace.length; i++) {
element = javaStackTrace[i];
buffer
.append("\tfrom ")
.append(createRubyBacktraceString(element));
if (i + 1 < javaStackTrace.length) buffer.append("\n");
}
}
return buffer.toString();
}
public static RubyStackTraceElement[] gatherRawBacktrace(Ruby runtime, StackTraceElement[] stackTrace) {
List trace = new ArrayList(stackTrace.length);
for (int i = 0; i < stackTrace.length; i++) {
StackTraceElement element = stackTrace[i];
trace.add(new RubyStackTraceElement(element));
}
RubyStackTraceElement[] rubyStackTrace = new RubyStackTraceElement[trace.size()];
return (RubyStackTraceElement[])trace.toArray(rubyStackTrace);
}
private Frame pushFrameForBlock(Binding binding) {
Frame lastFrame = getNextFrame();
Frame f = pushFrame(binding.getFrame());
f.setVisibility(binding.getVisibility());
return lastFrame;
}
private Frame pushFrameForEval(Binding binding) {
Frame lastFrame = getNextFrame();
Frame f = pushFrame(binding.getFrame());
f.setVisibility(binding.getVisibility());
return lastFrame;
}
public void preAdoptThread() {
pushFrame();
pushRubyClass(runtime.getObject());
getCurrentFrame().setSelf(runtime.getTopSelf());
}
public void preExtensionLoad(IRubyObject self) {
pushFrame();
pushRubyClass(runtime.getObject());
getCurrentFrame().setSelf(self);
getCurrentFrame().setVisibility(Visibility.PUBLIC);
}
public void postExtensionLoad() {
popFrame();
popRubyClass();
}
public void preCompiledClass(RubyModule type, StaticScope staticScope) {
pushRubyClass(type);
pushFrameCopy();
getCurrentFrame().setSelf(type);
getCurrentFrame().setVisibility(Visibility.PUBLIC);
staticScope.setModule(type);
pushScope(DynamicScope.newDynamicScope(staticScope));
}
public void preCompiledClassDummyScope(RubyModule type, StaticScope staticScope) {
pushRubyClass(type);
pushFrameCopy();
getCurrentFrame().setSelf(type);
getCurrentFrame().setVisibility(Visibility.PUBLIC);
staticScope.setModule(type);
pushScope(staticScope.getDummyScope());
}
public void postCompiledClass() {
popScope();
popRubyClass();
popFrame();
}
public void preScopeNode(StaticScope staticScope) {
pushScope(DynamicScope.newDynamicScope(staticScope, getCurrentScope()));
}
public void postScopeNode() {
popScope();
}
public void preClassEval(StaticScope staticScope, RubyModule type) {
pushRubyClass(type);
pushFrameCopy();
getCurrentFrame().setSelf(type);
getCurrentFrame().setVisibility(Visibility.PUBLIC);
pushScope(DynamicScope.newDynamicScope(staticScope, null));
}
public void postClassEval() {
popScope();
popRubyClass();
popFrame();
}
public void preBsfApply(String[] names) {
// FIXME: I think we need these pushed somewhere?
StaticScope staticScope = runtime.getStaticScopeFactory().newLocalScope(null);
staticScope.setVariables(names);
pushFrame();
}
public void postBsfApply() {
popFrame();
}
public void preMethodFrameAndClass(RubyModule implClass, String name, IRubyObject self, Block block, StaticScope staticScope) {
RubyModule ssModule = staticScope.getModule();
// FIXME: This is currently only here because of some problems with IOOutputStream writing to a "bare" runtime without a proper scope
if (ssModule == null) ssModule = implClass;
pushRubyClass(ssModule);
pushCallFrame(implClass, name, self, block);
}
public void preMethodFrameAndScope(RubyModule clazz, String name, IRubyObject self, Block block,
StaticScope staticScope) {
RubyModule implementationClass = staticScope.getModule();
// FIXME: This is currently only here because of some problems with IOOutputStream writing to a "bare" runtime without a proper scope
if (implementationClass == null) {
implementationClass = clazz;
}
pushCallFrame(clazz, name, self, block);
pushScope(DynamicScope.newDynamicScope(staticScope));
pushRubyClass(implementationClass);
}
public void preMethodFrameAndDummyScope(RubyModule clazz, String name, IRubyObject self, Block block,
StaticScope staticScope) {
RubyModule implementationClass = staticScope.getModule();
// FIXME: This is currently only here because of some problems with IOOutputStream writing to a "bare" runtime without a proper scope
if (implementationClass == null) {
implementationClass = clazz;
}
pushCallFrame(clazz, name, self, block);
pushScope(staticScope.getDummyScope());
pushRubyClass(implementationClass);
}
public void preMethodNoFrameAndDummyScope(RubyModule clazz, StaticScope staticScope) {
RubyModule implementationClass = staticScope.getModule();
// FIXME: This is currently only here because of some problems with IOOutputStream writing to a "bare" runtime without a proper scope
if (implementationClass == null) {
implementationClass = clazz;
}
pushScope(staticScope.getDummyScope());
pushRubyClass(implementationClass);
}
public void postMethodFrameAndScope() {
popRubyClass();
popScope();
popFrame();
}
public void preMethodFrameOnly(RubyModule clazz, String name, IRubyObject self, Block block) {
pushRubyClass(clazz);
pushCallFrame(clazz, name, self, block);
}
public void postMethodFrameOnly() {