package org.jruby.ir.instructions;
import org.jruby.RubyProc;
import org.jruby.ir.IRScope;
import org.jruby.ir.IRVisitor;
import org.jruby.ir.Operation;
import org.jruby.ir.operands.*;
import org.jruby.ir.persistence.IRReaderDecoder;
import org.jruby.ir.persistence.IRWriterEncoder;
import org.jruby.ir.transformations.inlining.CloneInfo;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.Block;
import org.jruby.runtime.DynamicScope;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.ir.IRFlags;
public class BuildLambdaInstr extends OneOperandResultBaseInstr implements FixedArityInstr, ClosureAcceptingInstr {
public BuildLambdaInstr(Variable result, WrappedIRClosure lambdaBody) {
super(Operation.LAMBDA, result, lambdaBody);
}
@Override
public boolean computeScopeFlags(IRScope scope) {
scope.getFlags().add(IRFlags.BINDING_HAS_ESCAPED);
return true;
}
@Override
public Instr clone(CloneInfo ii) {
return new BuildLambdaInstr(ii.getRenamedVariable(getResult()), (WrappedIRClosure) getLambdaBody().cloneForInlining(ii));
}
public WrappedIRClosure getLambdaBody() {
return (WrappedIRClosure) getOperand1();
}
public Operand getClosureArg() {
return getOperand1();
}
public boolean hasLiteralClosure() { return getClosureArg() instanceof WrappedIRClosure; }
@Override
public void encode(IRWriterEncoder e) {
super.encode(e);
e.encode(getLambdaBody());
}
public static BuildLambdaInstr decode(IRReaderDecoder d) {
return new BuildLambdaInstr(d.decodeVariable(), (WrappedIRClosure) d.decodeOperand());
}
@Override
public Object interpret(ThreadContext context, StaticScope currScope, DynamicScope currDynScope, IRubyObject self, Object[] temp) {
getLambdaBody().getClosure().getStaticScope().determineModule();
Block block = (Block) getLambdaBody().retrieve(context, self, currScope, currDynScope, temp);
return RubyProc.newProc(context.runtime, block, Block.Type.LAMBDA);
}
@Override
public void visit(IRVisitor visitor) {
visitor.BuildLambdaInstr(this);
}
}