Exemplo n.º 1
0
 @ExplodeLoop
 @Override
 public void executeVoid(VirtualFrame frame) {
   for (RubyNode value : values) {
     value.executeVoid(frame);
   }
 }
Exemplo n.º 2
0
  @Override
  public Object execute(VirtualFrame frame) {
    if (optimizedProfile.profile(RubyArguments.isKwOptimized(frame.getArguments()))) {
      Object kwarg =
          argumentValueProfile.profile(
              RubyArguments.getOptimizedKeywordArgument(frame.getArguments(), kwIndex));

      if (defaultProfile.profile(
          kwarg instanceof OptionalKeywordArgMissingNode.OptionalKeywordArgMissing)) {
        return defaultValue.execute(frame);
      } else {
        return kwarg;
      }
    } else {
      final RubyHash hash = RubyArguments.getUserKeywordsHash(frame.getArguments(), minimum);

      if (defaultProfile.profile(hash == null)) {
        return defaultValue.execute(frame);
      }

      Object value = lookupKeywordInHash(hash);

      if (defaultProfile.profile(value == null)) {
        return defaultValue.execute(frame);
      }

      return value;
    }
  }
Exemplo n.º 3
0
 @Override
 public void executeVoid(VirtualFrame frame) {
   try {
     tryPart.executeVoid(frame);
   } finally {
     ensurePart.executeVoid(frame);
   }
 }
Exemplo n.º 4
0
 @Override
 public Object execute(VirtualFrame frame) {
   try {
     return tryPart.execute(frame);
   } finally {
     ensurePart.executeVoid(frame);
   }
 }
Exemplo n.º 5
0
  // TODO CS 20-Aug-15 this needs to go
  public static boolean verySlowIsFrozen(RubyContext context, Object object) {
    final RubyNode node =
        IsFrozenNodeGen.create(context, null, new LiteralNode(context, null, object));
    new Node() {
      @Child RubyNode child = node;
    }.adoptChildren();

    return (boolean) node.execute(null);
  }
Exemplo n.º 6
0
  @ExplodeLoop
  @Override
  public Object isDefined(VirtualFrame frame) {
    for (RubyNode value : values) {
      if (value.isDefined(frame) == getContext().getCoreLibrary().getNilObject()) {
        return getContext().getCoreLibrary().getNilObject();
      }
    }

    return super.isDefined(frame);
  }
Exemplo n.º 7
0
  private RubyNode composeBody(RubyNode prelude, RubyNode body) {
    final SourceSection sourceSection =
        SequenceNode.enclosing(prelude.getSourceSection(), body.getSourceSection());

    body = SequenceNode.sequence(context, sourceSection, prelude, body);

    if (environment.getFlipFlopStates().size() > 0) {
      body = SequenceNode.sequence(context, sourceSection, initFlipFlopStates(sourceSection), body);
    }

    return body;
  }
Exemplo n.º 8
0
  public RubyNode doCompileMethodBody(
      SourceSection sourceSection,
      String methodName,
      org.jruby.ast.Node bodyNode,
      SharedMethodInfo sharedMethodInfo) {
    declareArguments(sourceSection, methodName, sharedMethodInfo);
    final Arity arity = getArity(argsNode);

    final LoadArgumentsTranslator loadArgumentsTranslator =
        new LoadArgumentsTranslator(currentNode, context, source, false, this);
    final RubyNode loadArguments = argsNode.accept(loadArgumentsTranslator);

    RubyNode body;

    parentSourceSection.push(sourceSection);
    try {
      body = translateNodeOrNil(sourceSection, bodyNode);
    } finally {
      parentSourceSection.pop();
    }

    final RubyNode prelude;

    if (usesRubiniusPrimitive) {
      // Use Rubinius.primitive seems to turn off arity checking. See Time.from_array for example.
      prelude = loadArguments;
    } else {
      prelude =
          SequenceNode.sequence(
              context,
              sourceSection,
              CheckArityNode.create(context, sourceSection, arity),
              loadArguments);
    }

    body = SequenceNode.sequence(context, body.getSourceSection(), prelude, body);

    if (environment.getFlipFlopStates().size() > 0) {
      body =
          SequenceNode.sequence(
              context, body.getSourceSection(), initFlipFlopStates(sourceSection), body);
    }

    body =
        new CatchForMethodNode(context, body.getSourceSection(), body, environment.getReturnID());

    // TODO(CS, 10-Jan-15) why do we only translate exceptions in methods and not blocks?
    body = new ExceptionTranslatingNode(context, body.getSourceSection(), body);
    return body;
  }
Exemplo n.º 9
0
  @Override
  public Object execute(VirtualFrame frame) {
    // TODO(cs): can module ever not evaluate to a RubyModule?

    final RubyModule moduleObject = (RubyModule) module.execute(frame);

    final Object rhsValue = rhs.execute(frame);

    assert rhsValue != null;
    assert !(rhsValue instanceof String);

    moduleObject.setModuleConstant(name, rhsValue);

    return rhsValue;
  }
Exemplo n.º 10
0
  @Override
  public Object execute(VirtualFrame frame) {
    while (true) {
      getContext().getSafepointManager().poll();

      Object result;

      try {
        result = tryPart.execute(frame);
      } catch (ControlFlowException exception) {
        controlFlowProfile.enter();
        throw exception;
      } catch (RaiseException exception) {
        raiseExceptionProfile.enter();

        try {
          return handleException(frame, exception);
        } catch (RetryException e) {
          continue;
        }
      } finally {
        clearExceptionVariableNode.execute(frame);
      }

      elseProfile.enter();
      elsePart.executeVoid(frame);
      return result;
    }
  }
Exemplo n.º 11
0
  /**
   * Send the Java thread that represents this fiber to sleep until it recieves a resume or exit
   * message. On entry, assumes that the GIL is not held. On exit, holding the GIL.
   */
  public Object waitForResume() {
    RubyNode.notDesignedForCompilation();

    FiberMessage message = null;

    do {
      try {
        // TODO(cs) what is a suitable timeout?
        message = messageQueue.poll(1, TimeUnit.SECONDS);
      } catch (InterruptedException e) {
        // Poll again
      }
    } while (message == null);

    if (message instanceof FiberExitMessage) {
      throw new FiberExitException();
    }

    final FiberResumeMessage resumeMessage = (FiberResumeMessage) message;

    threadManager.enterGlobalLock(resumeMessage.getThread());

    fiberManager.setCurrentFiber(this);

    lastResumedByFiber = resumeMessage.getSendingFiber();
    return resumeMessage.getArg();
  }
Exemplo n.º 12
0
  public void initialize(RubyProc block) {
    RubyNode.notDesignedForCompilation();

    final RubyFiber finalFiber = this;
    final RubyProc finalBlock = block;

    new Thread(
            new Runnable() {

              @Override
              public void run() {
                fiberManager.registerFiber(finalFiber);

                try {
                  try {
                    final Object arg = finalFiber.waitForResume();
                    final Object result = finalBlock.rootCall(arg);
                    finalFiber.lastResumedByFiber.resume(finalFiber, result);
                  } catch (FiberExitException e) {
                    // Naturally exit the thread on catching this
                  }
                } finally {
                  fiberManager.unregisterFiber(finalFiber);
                }
              }
            })
        .start();
  }
Exemplo n.º 13
0
  @Override
  public RubySymbol execute(VirtualFrame frame) {
    notDesignedForCompilation();

    final Object receiverObject = receiver.execute(frame);

    final RubyMethod methodObject = (RubyMethod) method.execute(frame);

    RubyModule module;

    if (receiverObject instanceof RubyModule) {
      module = (RubyModule) receiverObject;
    } else {
      module = ((RubyBasicObject) receiverObject).getSingletonClass(this);
    }

    final RubyMethod methodWithDeclaringModule = methodObject.withDeclaringModule(module);

    if (moduleFunctionFlag(frame)) {
      module.addMethod(this, methodWithDeclaringModule.withVisibility(Visibility.PRIVATE));
      module
          .getSingletonClass(this)
          .addMethod(this, methodWithDeclaringModule.withVisibility(Visibility.PUBLIC));
    } else {
      module.addMethod(this, methodWithDeclaringModule);
    }

    return getContext().newSymbol(method.getName());
  }
Exemplo n.º 14
0
  @Override
  public DynamicObject execute(VirtualFrame frame) {
    CompilerDirectives.transferToInterpreter();

    final Object receiverObject = receiver.execute(frame);

    final InternalMethod methodObject = (InternalMethod) methodNode.execute(frame);

    final DynamicObject module = (DynamicObject) receiverObject;
    assert RubyGuards.isRubyModule(module);

    final Visibility visibility = getVisibility(frame, methodObject.getName());
    final InternalMethod method =
        methodObject.withDeclaringModule(module).withVisibility(visibility);

    if (method.getVisibility() == Visibility.MODULE_FUNCTION) {
      Layouts.MODULE.getFields(module).addMethod(this, method.withVisibility(Visibility.PRIVATE));
      Layouts.MODULE
          .getFields(singletonClassNode.executeSingletonClass(module))
          .addMethod(this, method.withVisibility(Visibility.PUBLIC));
    } else {
      Layouts.MODULE.getFields(module).addMethod(this, method);
    }

    return getSymbol(method.getName());
  }
Exemplo n.º 15
0
 @Override
 public Object execute(VirtualFrame frame) {
   if (CompilerDirectives.injectBranchProbability(
       getBranchProbability(), condition.executeBoolean(frame))) {
     if (CompilerDirectives.inInterpreter()) {
       thenCount++;
     }
     thenProfile.enter();
     return thenBody.execute(frame);
   } else {
     if (CompilerDirectives.inInterpreter()) {
       elseCount++;
     }
     elseProfile.enter();
     return elseBody.execute(frame);
   }
 }
Exemplo n.º 16
0
  @ExplodeLoop
  @Override
  public final Object execute(VirtualFrame frame) {
    final Object self = RubyArguments.getSelf(frame.getArguments());

    // Execute the arguments

    final Object[] argumentsObjects = new Object[arguments.length];

    CompilerAsserts.compilationConstant(arguments.length);
    for (int i = 0; i < arguments.length; i++) {
      argumentsObjects[i] = arguments[i].execute(frame);
    }

    // Execute the block

    RubyProc blockObject;

    if (block != null) {
      final Object blockTempObject = block.execute(frame);

      if (blockTempObject instanceof RubyNilClass) {
        blockObject = null;
      } else {
        blockObject = (RubyProc) blockTempObject;
      }
    } else {
      blockObject = null;
    }

    // Check we have a method and the module is unmodified

    if (!guard(frame, self)) {
      CompilerDirectives.transferToInterpreterAndInvalidate();
      lookup(frame);
    }

    // Call the method

    if (isSplatted) {
      // TODO(CS): need something better to splat the arguments array
      notDesignedForCompilation();
      final RubyArray argumentsArray = (RubyArray) argumentsObjects[0];
      return callNode.call(
          frame,
          RubyArguments.pack(
              superMethod,
              superMethod.getDeclarationFrame(),
              self,
              blockObject,
              argumentsArray.slowToArray()));
    } else {
      return callNode.call(
          frame,
          RubyArguments.pack(
              superMethod, superMethod.getDeclarationFrame(), self, blockObject, argumentsObjects));
    }
  }
Exemplo n.º 17
0
  /** Given a reference, produce either {@code nil} or the object. . */
  public static Object instanceOrNil(Object object) {
    RubyNode.notDesignedForCompilation();

    if (object == null) {
      return NilPlaceholder.INSTANCE;
    } else {
      return object;
    }
  }
Exemplo n.º 18
0
 @Override
 public Object execute(VirtualFrame frame) {
   if (RubyArguments.getUserArgumentsCount(frame.getArguments()) < minimum) {
     defaultValueProfile.enter();
     return defaultValue.execute(frame);
   } else {
     return RubyArguments.getUserArgument(frame.getArguments(), index);
   }
 }
Exemplo n.º 19
0
  public MethodDefinitionNode compileMethodNode(
      SourceSection sourceSection,
      String methodName,
      org.jruby.ast.Node bodyNode,
      SharedMethodInfo sharedMethodInfo) {
    final RubyNode body = compileMethodBody(sourceSection, methodName, bodyNode, sharedMethodInfo);
    final RubyRootNode rootNode =
        new RubyRootNode(
            context,
            considerExtendingMethodToCoverEnd(body.getSourceSection()),
            environment.getFrameDescriptor(),
            environment.getSharedMethodInfo(),
            body,
            environment.needsDeclarationFrame());

    final CallTarget callTarget = Truffle.getRuntime().createCallTarget(rootNode);
    return new MethodDefinitionNode(
        context, sourceSection, methodName, environment.getSharedMethodInfo(), callTarget);
  }
Exemplo n.º 20
0
  @Override
  public Object execute(VirtualFrame frame) {
    while (true) {
      try {
        final Object result = tryPart.execute(frame);
        elsePart.executeVoid(frame);
        return result;
      } catch (ControlFlowException exception) {
        controlFlowProfile.enter();

        throw exception;
      } catch (RuntimeException exception) {
        CompilerDirectives.transferToInterpreter();

        try {
          return handleException(frame, exception);
        } catch (RetryException e) {
          continue;
        }
      }
    }
  }
Exemplo n.º 21
0
  @Override
  public Object execute(VirtualFrame frame) {
    try {
      return body.execute(frame);
    } catch (BreakException e) {
      breakProfile.enter();

      if (matchingBreakProfile.profile(e.getBreakID() == breakID)) {
        return e.getResult();
      } else {
        throw e;
      }
    }
  }
Exemplo n.º 22
0
  @Override
  public RubyBasicObject executeRubyBasicObject(VirtualFrame frame) {
    final Object object = child.execute(frame);

    RubyBasicObject boxedObject;

    if (object instanceof RubyBasicObject) {
      boxedObject = (RubyBasicObject) object;
    } else {
      boxBranch.enter();
      boxedObject = getContext().getCoreLibrary().box(object);
    }

    return boxedObject;
  }
Exemplo n.º 23
0
  /**
   * Send a message to a fiber by posting into a message queue. Doesn't explicitly notify the Java
   * thread (although the queue implementation may) and doesn't wait for the message to be received.
   * On entry, assumes the the GIL is held. On exit, not holding the GIL.
   */
  public void resume(RubyFiber sendingFiber, Object... args) {
    RubyNode.notDesignedForCompilation();

    Object arg;

    if (args.length == 0) {
      arg = getContext().getCoreLibrary().getNilObject();
    } else if (args.length == 1) {
      arg = args[0];
    } else {
      arg = RubyArray.fromObjects(getContext().getCoreLibrary().getArrayClass(), args);
    }

    final RubyThread runningThread = threadManager.leaveGlobalLock();

    messageQueue.add(new FiberResumeMessage(runningThread, sendingFiber, arg));
  }
Exemplo n.º 24
0
  @Override
  public Object execute(VirtualFrame frame) {
    CompilerDirectives.transferToInterpreter();

    // TODO(CS): cast
    final DynamicObject module = (DynamicObject) definingModule.execute(frame);

    lexicalScope.setLiveModule(module);
    Layouts.MODULE.getFields(lexicalScope.getParent().getLiveModule()).addLexicalDependent(module);

    final InternalMethod definition =
        definitionMethod.executeMethod(frame).withDeclaringModule(module);
    return callModuleDefinitionNode.call(
        frame,
        definition.getCallTarget(),
        RubyArguments.pack(
            definition, null, null, module, null, DeclarationContext.MODULE, new Object[] {}));
  }
Exemplo n.º 25
0
  @Override
  public Object execute(VirtualFrame frame) {
    try {
      assert assertArgumentsShouldBeVisible(frame);

      final Object result = child.execute(frame);

      assert shouldObjectBeVisible(result)
          : "result@" + getEncapsulatingSourceSection().getShortDescription();

      return result;
    } catch (StackOverflowError error) {
      // TODO: we might want to do sth smarter here to avoid consuming frames when we are almost out
      // of it.
      CompilerDirectives.transferToInterpreter();
      throw new RaiseException(translate(error));
    } catch (TruffleFatalException | ThreadExitException exception) {
      throw exception;
    } catch (ControlFlowException exception) {
      controlProfile.enter();
      throw exception;
    } catch (RaiseException exception) {
      rethrowProfile.enter();
      throw exception;
    } catch (MainExitException exception) {
      CompilerDirectives.transferToInterpreter();
      throw exception;
    } catch (ArithmeticException exception) {
      CompilerDirectives.transferToInterpreter();
      throw new RaiseException(translate(exception));
    } catch (UnsupportedSpecializationException exception) {
      CompilerDirectives.transferToInterpreter();
      throw new RaiseException(translate(exception));
    } catch (org.jruby.exceptions.RaiseException e) {
      CompilerDirectives.transferToInterpreter();
      throw new RaiseException(getContext().toTruffle(e.getException(), this));
    } catch (Throwable exception) {
      CompilerDirectives.transferToInterpreter();
      throw new RaiseException(translate(exception));
    }
  }
Exemplo n.º 26
0
 @Override
 public void executeVoid(VirtualFrame frame) {
   for (RubyNode child : keyValues) {
     child.executeVoid(frame);
   }
 }
  public static boolean assignableTo(RubyClass thisClass, RubyModule otherClass) {
    RubyNode.notDesignedForCompilation();

    return includesModule(thisClass, otherClass);
  }
Exemplo n.º 28
0
  @Override
  public boolean equals(Object other) {
    RubyNode.notDesignedForCompilation();

    return other instanceof RubyNilClass || other instanceof NilPlaceholder;
  }
Exemplo n.º 29
0
  public void shutdown() {
    RubyNode.notDesignedForCompilation();

    messageQueue.add(new FiberExitMessage());
  }
Exemplo n.º 30
0
  public static boolean isNil(Object block) {
    RubyNode.notDesignedForCompilation();

    return block == NilPlaceholder.INSTANCE || block instanceof RubyNilClass;
  }