Esempio n. 1
0
  @Override
  public RubyNode visitArgumentNode(org.jruby.ast.ArgumentNode node) {
    final SourceSection sourceSection = translate(node.getPosition());

    final RubyNode readNode;

    if (useArray()) {
      readNode = readArgument(sourceSection, index);
    } else {
      if (state == State.PRE) {
        readNode = readArgument(sourceSection, index);
      } else if (state == State.POST) {
        readNode =
            readArgument(
                sourceSection,
                (argsNode.getPreCount() + argsNode.getOptionalArgsCount() + argsNode.getPostCount())
                    - index
                    - 1);
      } else {
        throw new IllegalStateException();
      }
    }

    final FrameSlot slot =
        methodBodyTranslator.getEnvironment().getFrameDescriptor().findFrameSlot(node.getName());
    return WriteLocalVariableNodeFactory.create(context, sourceSection, slot, readNode);
  }
Esempio n. 2
0
  public static Arity getArity(org.jruby.ast.ArgsNode argsNode) {
    final String[] keywordArguments;

    if (argsNode.hasKwargs() && argsNode.getKeywords() != null) {
      final org.jruby.ast.Node[] keywordNodes = argsNode.getKeywords().children();
      final int keywordsCount = keywordNodes.length;

      keywordArguments = new String[keywordsCount];
      for (int i = 0; i < keywordsCount; i++) {
        final KeywordArgNode kwarg = (KeywordArgNode) keywordNodes[i];
        final AssignableNode assignableNode = kwarg.getAssignable();

        if (assignableNode instanceof LocalAsgnNode) {
          keywordArguments[i] = ((LocalAsgnNode) assignableNode).getName();
        } else if (assignableNode instanceof DAsgnNode) {
          keywordArguments[i] = ((DAsgnNode) assignableNode).getName();
        } else {
          throw new UnsupportedOperationException("unsupported keyword arg " + kwarg);
        }
      }
    } else {
      keywordArguments = Arity.NO_KEYWORDS;
    }

    return new Arity(
        argsNode.getPreCount(),
        argsNode.getOptionalArgsCount(),
        argsNode.hasRestArg(),
        argsNode.getPostCount(),
        keywordArguments,
        argsNode.hasKeyRest());
  }
Esempio n. 3
0
  @Override
  public RubyNode visitRestArgNode(org.jruby.ast.RestArgNode node) {
    final SourceSection sourceSection = translate(node.getPosition());

    final RubyNode readNode;

    if (useArray()) {
      readNode =
          ArrayGetTailNodeFactory.create(
              context, sourceSection, argsNode.getPreCount(), loadArray(sourceSection));
    } else {
      readNode = new ReadRestArgumentNode(context, sourceSection, argsNode.getPreCount());
    }

    final FrameSlot slot =
        methodBodyTranslator.getEnvironment().getFrameDescriptor().findFrameSlot(node.getName());
    return WriteLocalVariableNodeFactory.create(context, sourceSection, slot, readNode);
  }
Esempio n. 4
0
 private RubyNode readArgument(SourceSection sourceSection, int index) {
   if (useArray()) {
     return ArrayIndexNodeFactory.create(context, sourceSection, index, loadArray(sourceSection));
   } else {
     if (state == State.PRE) {
       return new ReadPreArgumentNode(
           context,
           sourceSection,
           index,
           isBlock ? MissingArgumentBehaviour.NIL : MissingArgumentBehaviour.RUNTIME_ERROR);
     } else if (state == State.POST) {
       return new ReadPostArgumentNode(
           context,
           sourceSection,
           (argsNode.getPreCount() + argsNode.getOptionalArgsCount() + argsNode.getPostCount())
               - index
               - 1);
     } else {
       throw new IllegalStateException();
     }
   }
 }
Esempio n. 5
0
  @JRubyMethod(name = "parameters", compat = CompatVersion.RUBY1_9)
  public IRubyObject parameters(ThreadContext context) {
    Ruby runtime = context.getRuntime();
    RubyArray parms = RubyArray.newEmptyArray(runtime);
    ArgsNode args;

    if (!(this.getBlock().getBody() instanceof Interpreted19Block)) {
      return parms;
    }

    // argument names are easily accessible from interpreter
    RubyArray elem = RubyArray.newEmptyArray(runtime);
    args = ((Interpreted19Block) this.getBlock().getBody()).getArgs();

    // required parameters
    List<Node> children = new ArrayList();
    if (args.getPreCount() > 0) children.addAll(args.getPre().childNodes());
    if (args.getPostCount() > 0) children.addAll(args.getPost().childNodes());

    Iterator iter = children.iterator();
    while (iter.hasNext()) {
      Node node = (Node) iter.next();
      elem = RubyArray.newEmptyArray(runtime);
      elem.add(RubySymbol.newSymbol(runtime, this.isLambda() ? "req" : "opt"));
      if (node instanceof ArgumentNode) {
        elem.add(RubySymbol.newSymbol(runtime, ((ArgumentNode) node).getName()));
      }
      parms.add(elem);
    }

    // optional parameters
    if (args.getOptArgs() != null) {
      children = args.getOptArgs().childNodes();
      if (!children.isEmpty()) {
        iter = children.iterator();
        while (iter.hasNext()) {
          Node node = (Node) iter.next();
          elem = RubyArray.newEmptyArray(runtime);
          elem.add(RubySymbol.newSymbol(runtime, "opt"));
          if (node instanceof OptArgNode) {
            elem.add(RubySymbol.newSymbol(runtime, ((OptArgNode) node).getName()));
          }
          parms.add(elem);
        }
      }
    }

    ArgumentNode rest = args.getRestArgNode();
    if (rest != null) {
      elem = RubyArray.newEmptyArray(runtime);
      elem.add(RubySymbol.newSymbol(runtime, "rest"));
      elem.add(RubySymbol.newSymbol(runtime, rest.getName()));
      parms.add(elem);
    }

    BlockArgNode blockArg = args.getBlock();
    if (blockArg != null) {
      elem = RubyArray.newEmptyArray(runtime);
      elem.add(RubySymbol.newSymbol(runtime, "block"));
      elem.add(RubySymbol.newSymbol(runtime, blockArg.getName()));
      parms.add(elem);
    }

    return parms;
  }