Example #1
0
 @ExplodeLoop
 @Override
 public void executeVoid(VirtualFrame frame) {
   for (RubyNode value : values) {
     value.executeVoid(frame);
   }
 }
  @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;
    }
  }
Example #3
0
 @Override
 public void executeVoid(VirtualFrame frame) {
   try {
     tryPart.executeVoid(frame);
   } finally {
     ensurePart.executeVoid(frame);
   }
 }
Example #4
0
 @Override
 public Object execute(VirtualFrame frame) {
   try {
     return tryPart.execute(frame);
   } finally {
     ensurePart.executeVoid(frame);
   }
 }
Example #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);
  }
Example #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);
  }
Example #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;
  }
Example #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;
  }
Example #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;
  }
Example #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;
    }
  }
Example #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();
  }
Example #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();
  }
Example #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());
  }
Example #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());
  }
Example #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);
   }
 }
  @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));
    }
  }
Example #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;
    }
  }
 @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);
   }
 }
Example #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);
  }
Example #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;
        }
      }
    }
  }
Example #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;
      }
    }
  }
Example #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;
  }
Example #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));
  }
Example #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[] {}));
  }
  @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));
    }
  }
Example #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);
  }
Example #28
0
  @Override
  public boolean equals(Object other) {
    RubyNode.notDesignedForCompilation();

    return other instanceof RubyNilClass || other instanceof NilPlaceholder;
  }
Example #29
0
  public void shutdown() {
    RubyNode.notDesignedForCompilation();

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

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