Repository navigation
Expand file tree
/
Copy pathJavaInternalBlockBody.java
More file actions
120 lines (99 loc) · 3.41 KB
/
Copy pathJavaInternalBlockBody.java
File metadata and controls
120 lines (99 loc) · 3.41 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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.jruby.runtime;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyModule;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.Block.Type;
import org.jruby.runtime.builtin.IRubyObject;
/**
* Represents a special Java implementation of a block.
*/
public abstract class JavaInternalBlockBody extends BlockBody {
private final Arity arity;
private final ThreadContext originalContext;
private final String methodName;
private final StaticScope dummyScope;
/**
* For blocks which can be executed in any thread concurrently.
*/
public JavaInternalBlockBody(Ruby runtime, Arity arity) {
this(runtime, null, null, arity);
}
/**
* For blocks which cannot be executed in parallel.
* @param methodName
* @param arity
*/
public JavaInternalBlockBody(Ruby runtime, ThreadContext originalContext, String methodName, Arity arity) {
super(BlockBody.SINGLE_RESTARG);
this.arity = arity;
this.originalContext = originalContext;
this.methodName = methodName;
this.dummyScope = runtime.getStaticScopeFactory().getDummyScope();
}
// Make sure we are still on the same thread as originator if we care
private void threadCheck(ThreadContext yieldingContext) {
if (originalContext != null && yieldingContext != originalContext) {
throw yieldingContext.runtime.newThreadError("" + methodName + " cannot be parallelized");
}
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject[] args, Binding binding, Block.Type type) {
IRubyObject value;
if (args.length == 1) {
value = args[0];
} else {
value = RubyArray.newArrayNoCopy(context.runtime, args);
}
return yield(context, value, null, null, true, binding, type);
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject[] args, Binding binding,
Block.Type type, Block block) {
IRubyObject value;
if (args.length == 1) {
value = args[0];
} else {
value = RubyArray.newArrayNoCopy(context.runtime, args);
}
return yield(context, value, null, null, true, binding, type, block);
}
@Override
public IRubyObject yield(ThreadContext context, IRubyObject value, Binding binding, Type type) {
threadCheck(context);
return yield(context, value);
}
@Override
public IRubyObject yield(ThreadContext context, IRubyObject value, IRubyObject self, RubyModule klass, boolean aValue, Binding binding, Type type) {
threadCheck(context);
return yield(context, value);
}
public abstract IRubyObject yield(ThreadContext context, IRubyObject value);
@Override
public StaticScope getStaticScope() {
return dummyScope;
}
@Override
public void setStaticScope(StaticScope newScope) {
}
@Override
public Block cloneBlock(Binding binding) {
return new Block(this, binding);
}
@Override
public Arity arity() {
return arity;
}
@Override
public String getFile() {
return "(internal)";
}
@Override
public int getLine() {
return -1;
}
}