public static IRubyObject INTERPRET_EVAL( Ruby runtime, ThreadContext context, Node node, String name, IRubyObject self, Block block) { try { ThreadContext.pushBacktrace(context, name, node.getPosition()); return node.interpret(runtime, context, self, block); } finally { ThreadContext.popBacktrace(context); } }
private void processMethod(String methodName, Node argsNode, Node body) { sb.append("(method ").append(methodName).append(' '); // JRUBY-4301, include filename and line in sexp sb.append("(file ").append(new File(body.getPosition().getFile()).getName()).append(") "); sb.append("(line ").append(body.getPosition().getStartLine()).append(") "); process(argsNode); sb.append(" "); process(body); sb.append(")"); }
@Override public IRubyObject interpret( Ruby runtime, ThreadContext context, IRubyObject self, Block aBlock) { return context .getCurrentFrame() .getBlock() .yieldSpecific( context, argument1.interpret(runtime, context, self, aBlock), argument2.interpret(runtime, context, self, aBlock)); }
@Override public IRubyObject interpret( Ruby runtime, ThreadContext context, IRubyObject self, Block aBlock) { IRubyObject result = firstNode.interpret(runtime, context, self, aBlock); if (!result.isTrue()) { result = secondNode.interpret(runtime, context, self, aBlock); } return result; }
private static Block getBlockPassBlock( Node blockNode, Ruby runtime, ThreadContext context, IRubyObject self, Block currentBlock) { Node bodyNode = ((BlockPassNode) blockNode).getBodyNode(); IRubyObject proc; if (bodyNode == null) { proc = runtime.getNil(); } else { proc = bodyNode.interpret(runtime, context, self, currentBlock); } return Helpers.getBlockFromBlockPassBody(proc, currentBlock); }
@Override public IRubyObject interpret( Ruby runtime, ThreadContext context, IRubyObject self, Block aBlock) { IRubyObject receiver = getReceiverNode().interpret(runtime, context, self, aBlock); return callAdapter.callIter( context, self, receiver, arg1.interpret(runtime, context, self, aBlock), arg2.interpret(runtime, context, self, aBlock), Helpers.getBlock(context, self, iterNode)); }
@Specialization public Object parseTree() { notDesignedForCompilation(); final org.jruby.ast.Node parseTree = RubyCallStack.getCurrentMethod().getSharedMethodInfo().getParseTree(); if (parseTree == null) { return NilPlaceholder.INSTANCE; } else { return getContext().makeString(parseTree.toString(true, 0)); } }
public void setFlag(Node node, Flag modifier) { if (dump) { LOG.info( "[ASTInspector] " + name + "\n\tset flag " + modifier + " because of " + node.getNodeType() + " at " + node.getPosition()); } flags |= modifier.flag; }
@Override public void assignArray( Ruby runtime, ThreadContext context, IRubyObject self, IRubyObject arg, Block block) { RubyArray values = (RubyArray) arg; int valueLength = values.getLength(); switch (valueLength) { case 0: assign(runtime, context, self, block); break; case 1: assign(runtime, context, self, values.eltInternal(0), block); break; case 2: assign(runtime, context, self, values.eltInternal(0), values.eltInternal(1), block); break; case 3: assign( runtime, context, self, values.eltInternal(0), values.eltInternal(1), values.eltInternal(2), block); break; } // Populate up to shorter of calling arguments or local parameters in the block for (int i = 0; i < preLength && i < valueLength; i++) { pre.get(i).assign(runtime, context, self, values.eltInternal(i), block, false); } // nil pad since we provided less values than block parms if (valueLength < preLength) { assignNilTo(runtime, context, self, block, valueLength); } else if (valueLength == preLength) { // no extra args for rest rest.assign( runtime, context, self, runtime.newArrayNoCopyLight(IRubyObject.NULL_ARRAY), block, true); } else { // extra args for rest rest.assign( runtime, context, self, values.subseqLight(preLength, valueLength - preLength), block, true); } }
@Override public IRubyObject interpret( Ruby runtime, ThreadContext context, IRubyObject self, Block aBlock) { // Serialization killed our dynamic scope. We can just create an empty one // since serialization cannot serialize an eval (which is the only thing // which is capable of having a non-empty dynamic scope). if (scope == null) { scope = DynamicScope.newDynamicScope(staticScope); } StaticScope theStaticScope = scope.getStaticScope(); // Each root node has a top-level scope that we need to push context.preScopedBody(scope); if (theStaticScope.getModule() == null) { theStaticScope.setModule(runtime.getObject()); } try { return bodyNode.interpret(runtime, context, self, aBlock); } finally { context.postScopedBody(); } }
private RubyNode translateLocalAssignment( ISourcePosition sourcePosition, String name, org.jruby.ast.Node valueNode) { final SourceSection sourceSection = translate(sourcePosition); final RubyNode readNode; if (valueNode instanceof org.jruby.ast.NilImplicitNode) { // Multiple assignment if (useArray()) { readNode = ArrayIndexNodeFactory.create(context, sourceSection, index, loadArray(sourceSection)); } else { readNode = readArgument(sourceSection, index); } } else { // Optional argument final RubyNode defaultValue = valueNode.accept(this); readNode = new ReadOptionalArgumentNode( context, sourceSection, index, index + argsNode.getPostCount() + 1, defaultValue); } final FrameSlot slot = methodBodyTranslator.getEnvironment().getFrameDescriptor().findFrameSlot(name); return WriteLocalVariableNodeFactory.create(context, sourceSection, slot, readNode); }
@Override public IRubyObject interpret( Ruby runtime, ThreadContext context, IRubyObject self, Block aBlock) { Block block = SharedScopeBlock.newInterpretedSharedScopeClosure( context, this, context.getCurrentScope(), self); try { while (true) { try { String savedFile = context.getFile(); int savedLine = context.getLine(); IRubyObject recv = null; try { recv = iterNode.interpret(runtime, context, self, aBlock); } finally { context.setFileAndLine(savedFile, savedLine); } return callAdapter.call(context, self, recv, block); } catch (JumpException.RetryJump rj) { // do nothing, allow loop to retry } } } catch (JumpException.BreakJump bj) { return (IRubyObject) bj.getValue(); } }
/** * Find the first {@link ClassNode} under the supplied {@link Node}. * * @return the found <code>ClassNode</code>, or <code>null</code> if no {@link ClassNode} is found */ private static ClassNode findClassNode(Node node) { if (node instanceof ClassNode) { return (ClassNode) node; } List children = node.childNodes(); for (int i = 0; i < children.size(); i++) { Node child = (Node) children.get(i); if (child instanceof ClassNode) { return (ClassNode) child; } else if (child instanceof NewlineNode) { NewlineNode nn = (NewlineNode) child; Node found = findClassNode(nn.getNextNode()); if (found instanceof ClassNode) { return (ClassNode) found; } } } for (int i = 0; i < children.size(); i++) { Node child = (Node) children.get(i); Node found = findClassNode(child); if (found instanceof ClassNode) { return (ClassNode) found; } } return null; }
public static IRubyObject INTERPRET_METHOD( Ruby runtime, ThreadContext context, String file, int line, RubyModule implClass, Node node, String name, IRubyObject self, Block block, boolean isTraceable) { try { ThreadContext.pushBacktrace(context, name, file, line); if (isTraceable) methodPreTrace(runtime, context, name, implClass); return node.interpret(runtime, context, self, block); } finally { if (isTraceable) { try { methodPostTrace(runtime, context, name, implClass); } finally { ThreadContext.popBacktrace(context); } } else { ThreadContext.popBacktrace(context); } } }
@Override public void assign(Ruby runtime, ThreadContext context, IRubyObject self, Block block) { assignNilTo(runtime, context, self, block, 0); rest.assign( runtime, context, self, runtime.newArrayNoCopyLight(IRubyObject.NULL_ARRAY), block, true); }
@Override public void assign( Ruby runtime, ThreadContext context, IRubyObject self, IRubyObject value1, Block block) { pre.get(0).assign(runtime, context, self, value1, block, false); assignNilTo(runtime, context, self, block, 1); rest.assign( runtime, context, self, runtime.newArrayNoCopyLight(IRubyObject.NULL_ARRAY), block, true); }
@Override public void assign( Ruby runtime, ThreadContext context, IRubyObject self, IRubyObject values[], Block block) { int valueLength = values == null ? 0 : values.length; switch (valueLength) { case 0: assign(runtime, context, self, block); return; case 1: assign(runtime, context, self, values[0], block); return; case 2: assign(runtime, context, self, values[0], values[1], block); return; case 3: assign(runtime, context, self, values[0], values[1], values[2], block); return; } // Populate up to shorter of calling arguments or local parameters in the block for (int i = 0; i < preLength && i < valueLength; i++) { pre.get(i).assign(runtime, context, self, values[i], block, false); } // nil pad since we provided less values than block parms if (valueLength < preLength) { assignNilTo(runtime, context, self, block, valueLength); } else if (valueLength == preLength) { // no extra args for rest rest.assign( runtime, context, self, runtime.newArrayNoCopyLight(IRubyObject.NULL_ARRAY), block, true); } else { // extra args for rest rest.assign( runtime, context, self, runtime.newArrayNoCopyLight(shiftedArray(values, valueLength - preLength)), block, true); } }
@Override public Object visitIfNode(IfNode iVisited) { String src = getSource(iVisited); Node elseBody; Node thenBody; if (src.startsWith("unless")) { // then we need to "swap" the then and else nodes elseBody = iVisited.getThenBody(); thenBody = iVisited.getElseBody(); } else { elseBody = iVisited.getElseBody(); thenBody = iVisited.getThenBody(); } boolean isUnlessModifier = (iVisited.getThenBody() == null); if (elseBody != null && !isUnlessModifier) { if (alwaysExplicitReturn(thenBody)) { createProblem(elseBody.getPosition(), "Unnecessary else"); // $NON-NLS-1$ } } return super.visitIfNode(iVisited); }
// FIXME: This is a hot mess and I think we will still have some extra nulls inserted @Override public List<Node> childNodes() { ListNode post = getPost(); ListNode keywords = getKeywords(); ListNode pre = getPre(); ListNode optArgs = getOptArgs(); if (post != null) { if (keywords != null) { if (keyRest != null) return Node.createList(pre, optArgs, restArgNode, post, keywords, keyRest, blockArgNode); return Node.createList(pre, optArgs, restArgNode, post, keywords, blockArgNode); } return Node.createList(pre, optArgs, restArgNode, post, blockArgNode); } if (keywords != null) { if (keyRest != null) return Node.createList(pre, optArgs, restArgNode, keywords, keyRest, blockArgNode); return Node.createList(pre, optArgs, restArgNode, keywords, blockArgNode); } return Node.createList(pre, optArgs, restArgNode, blockArgNode); }
private void shortName(Node node) { String className = node.getClass().getName(); if (className.endsWith("Node")) { className = className.substring(0, className.length() - 4); int index = className.lastIndexOf('.'); if (index != -1) { className = className.substring(index + 1); } } sb.append(className.toLowerCase()); }
/** * process each node by printing out '(' name data child* ')' * * @param node */ private void process(Node node) { if (node == null) { sb.append("null"); return; } sb.append("("); shortName(node); leafInfo(node); for (Node child : node.childNodes()) { sb.append(" "); process(child); } sb.append(")"); }
public static IRubyObject INTERPRET_BLOCK( Ruby runtime, ThreadContext context, String file, int line, Node node, String name, IRubyObject self, Block block) { try { ThreadContext.pushBacktrace(context, name, file, line); blockPreTrace(runtime, context, name, self.getType()); return node.interpret(runtime, context, self, block); } finally { blockPostTrace(runtime, context, name, self.getType()); ThreadContext.popBacktrace(context); } }
public MethodDefinitionNode compileClassNode( SourceSection sourceSection, String name, org.jruby.ast.Node bodyNode) { environment.addMethodDeclarationSlots(); RubyNode body; if (bodyNode != null) { parentSourceSection = sourceSection; try { body = bodyNode.accept(this); } finally { parentSourceSection = null; } } else { body = new ObjectLiteralNode(context, sourceSection, context.getCoreLibrary().getNilObject()); } if (environment.getFlipFlopStates().size() > 0) { body = SequenceNode.sequence(context, sourceSection, initFlipFlopStates(sourceSection), body); } body = new CatchReturnPlaceholderNode(context, sourceSection, body, environment.getReturnID()); final RubyRootNode rootNode = new RubyRootNode( context, sourceSection, environment.getFrameDescriptor(), environment.getSharedMethodInfo(), body); return new MethodDefinitionNode( context, sourceSection, environment.getSharedMethodInfo().getName(), environment.getSharedMethodInfo(), environment.needsDeclarationFrame(), rootNode, false); }
public static RubyString getArgumentDefinition( Ruby runtime, ThreadContext context, Node node, RubyString type, IRubyObject self, Block block) { if (node == null) return type; if (node instanceof ArrayNode) { ArrayNode list = (ArrayNode) node; int size = list.size(); for (int i = 0; i < size; i++) { if (list.get(i).definition(runtime, context, self, block) == null) return null; } } else if (node.definition(runtime, context, self, block) == null) { return null; } return type; }
public static IRubyObject[] setupArgs( Ruby runtime, ThreadContext context, Node node, IRubyObject self, Block aBlock) { if (node == null) return IRubyObject.NULL_ARRAY; if (node instanceof ArrayNode) { ArrayNode argsArrayNode = (ArrayNode) node; String savedFile = context.getFile(); int savedLine = context.getLine(); int size = argsArrayNode.size(); IRubyObject[] argsArray = new IRubyObject[size]; for (int i = 0; i < size; i++) { argsArray[i] = argsArrayNode.get(i).interpret(runtime, context, self, aBlock); } context.setFileAndLine(savedFile, savedLine); return argsArray; } return ArgsUtil.convertToJavaArray(node.interpret(runtime, context, self, aBlock)); }
@Override public RubyNode visitArgsNode(org.jruby.ast.ArgsNode node) { argsNode = node; final SourceSection sourceSection = translate(node.getPosition()); final List<RubyNode> sequence = new ArrayList<>(); int localIndex = 0; if (node.getPre() != null) { state = State.PRE; for (org.jruby.ast.Node arg : node.getPre().childNodes()) { sequence.add(arg.accept(this)); index = ++localIndex; } } if (node.getOptArgs() != null) { // (BlockNode 0, (OptArgNode:a 0, (LocalAsgnNode:a 0, (FixnumNode 0))), ...) state = State.OPT; for (org.jruby.ast.Node arg : node.getOptArgs().childNodes()) { sequence.add(arg.accept(this)); index = ++localIndex; } } if (node.getPost() != null) { state = State.POST; for (org.jruby.ast.Node arg : node.getPost().childNodes()) { sequence.add(arg.accept(this)); index = ++localIndex; } } if (node.getRestArgNode() != null) { methodBodyTranslator.getEnvironment().hasRestParameter = true; sequence.add(node.getRestArgNode().accept(this)); } if (node.getBlock() != null) { sequence.add(node.getBlock().accept(this)); } return SequenceNode.sequence(context, sourceSection, sequence); }
@Override public IRubyObject interpret( Ruby runtime, ThreadContext context, IRubyObject self, Block aBlock) { RubyString definition = expressionNode.definition(runtime, context, self, aBlock); return definition != null ? definition : runtime.getNil(); }
public List<Node> childNodes() { return Node.createList(varNode, bodyNode); }
@Override public List<Node> childNodes() { return Node.createList(getVarNode(), getBodyNode(), iterNode); }
/** * Extra information that is not child nodes, but actual leaf data. * * @param node */ private void leafInfo(Node node) { switch (node.getNodeType()) { case ALIASNODE: aliasNode((AliasNode) node); break; case ANDNODE: noDataContents(node); break; case ARGSCATNODE: noDataContents(node); break; case ARGSPUSHNODE: noDataContents(node); break; case ARGUMENTNODE: argumentNode((ArgumentNode) node); break; case ARRAYNODE: noDataContents(node); break; case ATTRASSIGNNODE: attrAssignNode((AttrAssignNode) node); break; case BACKREFNODE: backRefNode((BackRefNode) node); break; case BEGINNODE: noDataContents(node); break; case BIGNUMNODE: bignumNode((BignumNode) node); break; case BLOCKARGNODE: blockArgNode((BlockArgNode) node); break; case BLOCKNODE: noDataContents(node); break; case BLOCKPASSNODE: noDataContents(node); break; case BREAKNODE: noDataContents(node); break; case CALLNODE: callNode((CallNode) node); break; case CASENODE: noDataContents(node); break; case CLASSNODE: noDataContents(node); break; case CLASSVARASGNNODE: classVarAsgnNode((ClassVarAsgnNode) node); break; case CLASSVARDECLNODE: classVarDeclNode((ClassVarDeclNode) node); break; case CLASSVARNODE: classVarNode((ClassVarNode) node); break; case COLON2NODE: colon2Node((Colon2Node) node); break; case COLON3NODE: colon3Node((Colon3Node) node); break; case CONSTDECLNODE: constDeclNode((ConstDeclNode) node); break; case CONSTNODE: constNode((ConstNode) node); break; case DASGNNODE: dAsgnNode((DAsgnNode) node); break; case DEFINEDNODE: noDataContents(node); break; case DEFNNODE: noDataContents(node); break; case DEFSNODE: noDataContents(node); break; case DOTNODE: dotNode((DotNode) node); break; case DREGEXPNODE: dRegexpNode((DRegexpNode) node); break; case DSTRNODE: noDataContents(node); break; case DSYMBOLNODE: noDataContents(node); break; case DVARNODE: dVarNode((DVarNode) node); break; case DXSTRNODE: noDataContents(node); break; case ENSURENODE: noDataContents(node); break; case EVSTRNODE: noDataContents(node); break; case FALSENODE: noDataContents(node); break; case FCALLNODE: fCallNode((FCallNode) node); break; case FIXNUMNODE: fixnumNode((FixnumNode) node); break; case FLIPNODE: flipNode((FlipNode) node); break; case FLOATNODE: floatNode((FloatNode) node); break; case FORNODE: noDataContents(node); break; case GLOBALASGNNODE: globalAsgnNode((GlobalAsgnNode) node); break; case GLOBALVARNODE: globalVarNode((GlobalVarNode) node); break; case HASHNODE: noDataContents(node); break; case IFNODE: noDataContents(node); break; case INSTASGNNODE: noDataContents(node); instAsgnNode((InstAsgnNode) node); break; case INSTVARNODE: noDataContents(node); instVarNode((InstVarNode) node); break; case ITERNODE: noDataContents(node); break; case LOCALASGNNODE: localAsgnNode((LocalAsgnNode) node); break; case LOCALVARNODE: localVarNode((LocalVarNode) node); break; case MATCH2NODE: noDataContents(node); break; case MATCH3NODE: noDataContents(node); break; case MATCHNODE: noDataContents(node); break; case MODULENODE: noDataContents(node); break; case MULTIPLEASGNNODE: noDataContents(node); break; case NEWLINENODE: noDataContents(node); break; case NEXTNODE: noDataContents(node); break; case NILNODE: noDataContents(node); break; case NOTNODE: noDataContents(node); break; case NTHREFNODE: nthRefNode((NthRefNode) node); break; case OPASGNANDNODE: noDataContents(node); break; case OPASGNNODE: opAsgnNode((OpAsgnNode) node); break; case OPASGNORNODE: noDataContents(node); break; case OPELEMENTASGNNODE: opElementAsgnNode((OpElementAsgnNode) node); break; case ORNODE: noDataContents(node); break; case PREEXENODE: noDataContents(node); break; case POSTEXENODE: noDataContents(node); break; case REDONODE: noDataContents(node); break; case REGEXPNODE: regexpNode((RegexpNode) node); break; case RESCUEBODYNODE: noDataContents(node); break; case RESCUENODE: noDataContents(node); break; case RETRYNODE: noDataContents(node); break; case RETURNNODE: noDataContents(node); break; case ROOTNODE: noDataContents(node); break; case SCLASSNODE: noDataContents(node); break; case SELFNODE: noDataContents(node); break; case SPLATNODE: noDataContents(node); break; case STRNODE: strNode((StrNode) node); break; case SUPERNODE: noDataContents(node); break; case SVALUENODE: noDataContents(node); break; case SYMBOLNODE: symbolNode((SymbolNode) node); break; case TOARYNODE: noDataContents(node); break; case TRUENODE: noDataContents(node); break; case UNDEFNODE: undefNode((UndefNode) node); break; case UNTILNODE: noDataContents(node); break; case VALIASNODE: valiasNode((VAliasNode) node); break; case VCALLNODE: vcallNode((VCallNode) node); break; case WHENNODE: noDataContents(node); break; case WHILENODE: noDataContents(node); break; case XSTRNODE: xStrNode((XStrNode) node); break; case YIELDNODE: noDataContents(node); break; case ZARRAYNODE: noDataContents(node); break; case ZSUPERNODE: noDataContents(node); break; default: } }