-
Notifications
You must be signed in to change notification settings - Fork 914
/
DynamicScope.java
612 lines (529 loc) · 17.3 KB
/
DynamicScope.java
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
/*
***** BEGIN LICENSE BLOCK *****
* Version: EPL 2.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 2.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-v20.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.
*
* 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 org.jruby.EvalType;
import org.jruby.ir.IRScopeType;
import org.jruby.ir.JIT;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.scope.ManyVarsDynamicScope;
public abstract class DynamicScope implements Cloneable {
// Static scoping information for this scope
protected final StaticScope staticScope;
// Captured dynamic scopes
protected final DynamicScope parent;
private EvalType evalType;
private boolean lambda;
protected DynamicScope(StaticScope staticScope, DynamicScope parent) {
this.staticScope = staticScope;
this.parent = parent;
this.evalType = EvalType.NONE;
}
public static DynamicScope newDynamicScope(StaticScope staticScope, DynamicScope parent) {
return staticScope.construct(parent);
}
public static DynamicScope newDynamicScope(StaticScope staticScope, DynamicScope parent, EvalType evalType) {
DynamicScope newScope = newDynamicScope(staticScope, parent);
newScope.setEvalType(evalType);
return newScope;
}
public static DynamicScope newDummyScope(StaticScope staticScope, DynamicScope parent) {
return DynamicScope.newDynamicScope(staticScope, parent);
}
/**
* Get parent (capturing) scope. This is used by eval and closures to
* walk up to hard lexical boundary.
*
*/
public final DynamicScope getParentScope() {
return parent;
}
@Deprecated
public DynamicScope getNextCapturedScope() { // Used by ruby-debug-ide
return getParentScope();
}
/**
* Returns the n-th parent scope of this scope.
* May return <code>null</code>.
* @param n - number of levels above to look.
* @return The n-th parent scope or <code>null</code>.
*/
public DynamicScope getNthParentScope(int n) {
DynamicScope scope = this;
for (int i = 0; i < n; i++) {
if (scope == null) break;
scope = scope.getParentScope();
}
return scope;
}
public static DynamicScope newDynamicScope(StaticScope staticScope) {
return newDynamicScope(staticScope, null);
}
/**
* Get the static scope associated with this DynamicScope.
*
* @return static complement to this scope
*/
public final StaticScope getStaticScope() {
return staticScope;
}
/**
* Get all variable names captured (visible) by this scope (sans $~ and $_).
*
* @return a list of variable names
*/
public final String[] getAllNamesInScope() {
return staticScope.getAllNamesInScope();
}
public void growIfNeeded() {
throw new RuntimeException("BUG: scopes of type " + getClass().getName() + " cannot grow");
}
public IRubyObject[] getValues() {
int numberOfVariables = staticScope.getNumberOfVariables();
IRubyObject[] values = new IRubyObject[numberOfVariables];
for (int i = 0; i < numberOfVariables; i++) {
values[i] = getValueDepthZero(i);
}
return values;
}
/**
* Get value from current scope or one of its captured scopes.
*
* @param offset zero-indexed value that represents where variable lives
* @param depth how many captured scopes down this variable should be set
* @return the value here
*/
@JIT
public abstract IRubyObject getValue(int offset, int depth);
/**
* Variation of getValue for depth 0
*/
@JIT
public IRubyObject getValueDepthZero(int offset) {
return getValue(offset, 0);
}
/**
* getValue for index 0, depth 0
*/
@JIT
public IRubyObject getValueZeroDepthZero() {
return getValueDepthZero(0);
}
/**
* getValue for index 1, depth 0
*/
@JIT
public IRubyObject getValueOneDepthZero() {
return getValueDepthZero(1);
}
/**
* getValue for index 2, depth 0
*/
@JIT
public IRubyObject getValueTwoDepthZero() {
return getValueDepthZero(2);
}
/**
* getValue for index 3, depth 0
*/
@JIT
public IRubyObject getValueThreeDepthZero() {
return getValueDepthZero(3);
}
/**
* getValue for index 4, depth 0
*/
@JIT
public IRubyObject getValueFourDepthZero() {
return getValueDepthZero(4);
}
/**
* getValue for index 5, depth 0
*/
@JIT
public IRubyObject getValueFiveDepthZero() {
return getValueDepthZero(5);
}
/**
* getValue for index 6, depth 0
*/
@JIT
public IRubyObject getValueSixDepthZero() {
return getValueDepthZero(6);
}
/**
* getValue for index 7, depth 0
*/
@JIT
public IRubyObject getValueSevenDepthZero() {
return getValueDepthZero(7);
}
/**
* getValue for index 8, depth 0
*/
@JIT
public IRubyObject getValueEightDepthZero() {
return getValueDepthZero(8);
}
/**
* getValue for index 9, depth 0
*/
@JIT
public IRubyObject getValueNineDepthZero() {
return getValueDepthZero(9);
}
/**
* Variation of getValue that checks for nulls, returning and setting the given value (presumably nil)
*/
public IRubyObject getValueOrNil(int offset, int depth, IRubyObject nil) {
if (depth > 0) {
return parent.getValueOrNil(offset, depth - 1, nil);
} else {
return getValueDepthZeroOrNil(offset, nil);
}
}
/**
* getValueOrNil for depth 0
*/
public IRubyObject getValueDepthZeroOrNil(int offset, IRubyObject nil) {
IRubyObject value = getValueDepthZero(offset);
return value == null ? setValueDepthZero(nil, offset) : value;
}
/**
* getValueOrNil for index 0, depth 0
*/
public IRubyObject getValueZeroDepthZeroOrNil(IRubyObject nil) {
IRubyObject value = getValueZeroDepthZero();
return value == null ? setValueDepthZero(nil, 0) : value;
}
/**
* getValueOrNil for index 1, depth 0
*/
public IRubyObject getValueOneDepthZeroOrNil(IRubyObject nil) {
IRubyObject value = getValueOneDepthZero();
return value == null ? setValueDepthZero(nil, 1) : value;
}
/**
* getValueOrNil for index 2, depth 0
*/
public IRubyObject getValueTwoDepthZeroOrNil(IRubyObject nil) {
IRubyObject value = getValueTwoDepthZero();
return value == null ? setValueDepthZero(nil, 2) : value;
}
/**
* getValueOrNil for index 3, depth 0
*/
public IRubyObject getValueThreeDepthZeroOrNil(IRubyObject nil) {
IRubyObject value = getValueThreeDepthZero();
return value == null ? setValueDepthZero(nil, 3) : value;
}
/**
* getValueOrNil for index 4, depth 0
*/
public IRubyObject getValueFourDepthZeroOrNil(IRubyObject nil) {
IRubyObject value = getValueFourDepthZero();
return value == null ? setValueDepthZero(nil, 4) : value;
}
/**
* getValueOrNil for index 5, depth 0
*/
public IRubyObject getValueFiveDepthZeroOrNil(IRubyObject nil) {
IRubyObject value = getValueFiveDepthZero();
return value == null ? setValueDepthZero(nil, 5) : value;
}
/**
* getValueOrNil for index 6, depth 0
*/
public IRubyObject getValueSixDepthZeroOrNil(IRubyObject nil) {
IRubyObject value = getValueSixDepthZero();
return value == null ? setValueDepthZero(nil, 6) : value;
}
/**
* getValueOrNil for index 7, depth 0
*/
public IRubyObject getValueSevenDepthZeroOrNil(IRubyObject nil) {
IRubyObject value = getValueSevenDepthZero();
return value == null ? setValueDepthZero(nil, 7) : value;
}
/**
* getValueOrNil for index 8, depth 0
*/
public IRubyObject getValueEightDepthZeroOrNil(IRubyObject nil) {
IRubyObject value = getValueEightDepthZero();
return value == null ? setValueDepthZero(nil, 8) : value;
}
/**
* getValueOrNil for index 9, depth 0
*/
public IRubyObject getValueNineDepthZeroOrNil(IRubyObject nil) {
IRubyObject value = getValueNineDepthZero();
return value == null ? setValueDepthZero(nil, 9) : value;
}
/**
* Set value in current dynamic scope or one of its captured scopes.
*
* @param offset zero-indexed value that represents where variable lives
* @param value to set
* @param depth how many captured scopes down this variable should be set
*/
public IRubyObject setValue(int offset, IRubyObject value, int depth) {
setValueVoid(value, offset, depth);
return value;
}
/**
* Set value in current dynamic scope or one of its captured scopes.
*
* @param offset zero-indexed value that represents where variable lives
* @param value to set
* @param depth how many captured scopes down this variable should be set
*/
public IRubyObject setValue(IRubyObject value, int offset, int depth) {
setValueVoid(value, offset, depth);
return value;
}
/**
* Set value in current dynamic scope or one of its captured scopes.
*
* @param offset zero-indexed value that represents where variable lives
* @param value to set
* @param depth how many captured scopes down this variable should be set
*/
@JIT
public abstract void setValueVoid(IRubyObject value, int offset, int depth);
/**
* setValue for depth zero
*
* @param value to set
* @param offset zero-indexed value that represents where variable lives
*/
public IRubyObject setValueDepthZero(IRubyObject value, int offset) {
setValueDepthZeroVoid(value, offset);
return value;
}
/**
* setValue for depth zero
*
* @param value to set
* @param offset zero-indexed value that represents where variable lives
*/
@JIT
public void setValueDepthZeroVoid(IRubyObject value, int offset) {
setValueVoid(value, offset, 0);
}
/**
* Set value zero in this scope;
*/
public IRubyObject setValueZeroDepthZero(IRubyObject value) {
setValueZeroDepthZeroVoid(value);
return value;
}
/**
* Set value zero in this scope;
*/
@JIT
public void setValueZeroDepthZeroVoid(IRubyObject value) {
setValueDepthZeroVoid(value, 0);
}
/**
* Set value one in this scope.
*/
public IRubyObject setValueOneDepthZero(IRubyObject value) {
setValueOneDepthZeroVoid(value);
return value;
}
/**
* Set value one in this scope.
*/
@JIT
public void setValueOneDepthZeroVoid(IRubyObject value) {
setValueDepthZeroVoid(value, 1);
}
/**
* Set value two in this scope.
*/
public IRubyObject setValueTwoDepthZero(IRubyObject value) {
setValueTwoDepthZeroVoid(value);
return value;
}
/**
* Set value two in this scope.
*/
@JIT
public void setValueTwoDepthZeroVoid(IRubyObject value) {
setValueDepthZeroVoid(value, 2);
}
/**
* Set value three in this scope.
*/
public IRubyObject setValueThreeDepthZero(IRubyObject value) {
setValueThreeDepthZeroVoid(value);
return value;
}
/**
* Set value three in this scope.
*/
@JIT
public void setValueThreeDepthZeroVoid(IRubyObject value) {
setValueDepthZeroVoid(value, 3);
}
/**
* Set value four in this scope.
*/
@JIT
public void setValueFourDepthZeroVoid(IRubyObject value) {
setValueDepthZeroVoid(value, 4);
}
/**
* Set value five in this scope.
*/
@JIT
public void setValueFiveDepthZeroVoid(IRubyObject value) {
setValueDepthZeroVoid(value, 5);
}
/**
* Set value six in this scope.
*/
@JIT
public void setValueSixDepthZeroVoid(IRubyObject value) {
setValueDepthZeroVoid(value, 6);
}
/**
* Set value seven in this scope.
*/
@JIT
public void setValueSevenDepthZeroVoid(IRubyObject value) {
setValueDepthZeroVoid(value, 7);
}
/**
* Set value eight in this scope.
*/
@JIT
public void setValueEightDepthZeroVoid(IRubyObject value) {
setValueDepthZeroVoid(value, 8);
}
/**
* Set value nine in this scope.
*/
@JIT
public void setValueNineDepthZeroVoid(IRubyObject value) {
setValueDepthZeroVoid(value, 9);
}
@Override
public String toString() {
return toString(new StringBuffer(), "");
}
// Helper function to give a good view of current dynamic scope with captured scopes
public String toString(StringBuffer buf, String indent) {
buf.append(indent).append("Static Type[" + hashCode() + "]: " +
(staticScope.isBlockScope() ? "block" : "local")+" [");
int size = staticScope.getNumberOfVariables();
IRubyObject[] variableValues = getValues();
if (size != 0) {
String names[] = staticScope.getVariables();
for (int i = 0; i < size-1; i++) {
buf.append(names[i]).append("=");
if (variableValues[i] == null) {
buf.append("null");
} else {
buf.append(variableValues[i]);
}
buf.append(",");
}
buf.append(names[size-1]).append("=");
assert variableValues.length == names.length : "V: " + variableValues.length +
" != N: " + names.length + " for " + buf;
if (variableValues[size-1] == null) {
buf.append("null");
} else {
buf.append(variableValues[size-1]);
}
}
buf.append("]");
if (parent != null) {
buf.append("\n");
parent.toString(buf, indent + " ");
}
return buf.toString();
}
public boolean inInstanceEval() {
return evalType == EvalType.INSTANCE_EVAL;
}
public boolean inModuleEval() {
return evalType == EvalType.MODULE_EVAL;
}
public boolean inBindingEval() {
return evalType == EvalType.BINDING_EVAL;
}
public void setEvalType(EvalType evalType) {
this.evalType = evalType == null ? EvalType.NONE : evalType;
}
public EvalType getEvalType() {
return this.evalType;
}
public void clearEvalType() {
evalType = EvalType.NONE;
}
public void setLambda(boolean lambda) {
this.lambda = lambda;
}
public boolean isLambda() {
return lambda;
}
/**
* A DynamicScope is the target of a non-local return if it is one of the following:
*
* * A method scope
* * A top-level script scope
* * A block scope that is either marked as the argument scope (define_method) or a lambda
*
* @return true if this scope is the nearest return scope, false otherwise
*/
public boolean isReturnTarget() {
StaticScope staticScope = this.staticScope;
IRScopeType scopeType = staticScope.getScopeType();
return scopeType.isMethodType() || // Contained within a method
scopeType == IRScopeType.SCRIPT_BODY ||
scopeType.isBlock() && (staticScope.isArgumentScope() || lambda); // Contained within define_method closure
}
@Deprecated
public DynamicScope cloneScope() {
try {
return (DynamicScope) clone();
} catch (CloneNotSupportedException cnse) {
throw new RuntimeException("BUG: failed to clone scope type " + getClass().getName());
}
}
/**
* Binding needs to clone its scope with all the current values.
* @return a duplicate of this scope with all the current values
*/
public DynamicScope dupEvalScope() {
ManyVarsDynamicScope newScope = new ManyVarsDynamicScope(staticScope.duplicate(), parent);
newScope.setVariableValues(getValues());
return newScope;
}
}