package org.jruby.runtime;
import org.jruby.Ruby;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ArraySupport;
public abstract class JavaInternalBlockBody extends BlockBody {
private final ThreadContext originalContext;
private final String methodName;
private final StaticScope dummyScope;
public JavaInternalBlockBody(Ruby runtime, Signature signature) {
this(runtime, null, null, signature);
}
public JavaInternalBlockBody(Ruby runtime, ThreadContext originalContext, String methodName, Signature signature) {
super(signature);
this.originalContext = originalContext;
this.methodName = methodName;
this.dummyScope = runtime.getStaticScopeFactory().getDummyScope();
}
@Deprecated
public JavaInternalBlockBody(Ruby runtime, Arity arity) {
this(runtime, null, null, Signature.from(arity));
}
private void threadCheck(ThreadContext yieldingContext) {
if (originalContext != null && yieldingContext != originalContext) {
throw yieldingContext.runtime.newThreadError(methodName + " cannot be parallelized");
}
}
@Override
public IRubyObject call(ThreadContext context, Block block, IRubyObject[] args) {
return yield(context, block, CallBlock.adjustArgs(block, args), null);
}
@Override
public IRubyObject call(ThreadContext context, Block block, IRubyObject[] args, Block blockArg) {
return yield(context, block, CallBlock.adjustArgs(block, args), null, blockArg);
}
@Override
protected IRubyObject doYield(ThreadContext context, Block block, IRubyObject value) {
threadCheck(context);
return yield(context, value);
}
@Override
protected IRubyObject doYield(ThreadContext context, Block block, IRubyObject[] args, IRubyObject self) {
threadCheck(context);
return yield(context, CallBlock.adjustArgs(block, args));
}
public abstract IRubyObject yield(ThreadContext context, IRubyObject[] args);
public IRubyObject yield(ThreadContext context, IRubyObject arg) {
return yield(context, new IRubyObject[] { arg });
}
@Override
public StaticScope getStaticScope() {
return dummyScope;
}
@Override
public void setStaticScope(StaticScope newScope) {
}
@Override
public String getFile() {
return "(internal)";
}
@Override
public int getLine() {
return -1;
}
}