Пример #1
0
  private boolean moduleFunctionFlag(VirtualFrame frame) {
    final FrameSlot moduleFunctionFlagSlot =
        frame.getFrameDescriptor().findFrameSlot(RubyModule.MODULE_FUNCTION_FLAG_FRAME_SLOT_ID);

    if (moduleFunctionFlagSlot == null) {
      return false;
    } else {
      Object moduleFunctionObject;

      try {
        moduleFunctionObject = frame.getObject(moduleFunctionFlagSlot);
      } catch (FrameSlotTypeException e) {
        throw new RuntimeException(e);
      }

      return (moduleFunctionObject instanceof Boolean) && (boolean) moduleFunctionObject;
    }
  }
Пример #2
0
  @Override
  public Object execute(VirtualFrame frame) {
    frame.setObject(FormatFrameDescriptor.SOURCE_SLOT, frame.getArguments()[0]);
    frame.setInt(FormatFrameDescriptor.SOURCE_LENGTH_SLOT, (int) frame.getArguments()[1]);
    frame.setInt(FormatFrameDescriptor.SOURCE_POSITION_SLOT, 0);
    frame.setObject(FormatFrameDescriptor.OUTPUT_SLOT, new byte[expectedLength]);
    frame.setInt(FormatFrameDescriptor.OUTPUT_POSITION_SLOT, 0);
    frame.setInt(FormatFrameDescriptor.STRING_LENGTH_SLOT, 0);
    frame.setInt(FormatFrameDescriptor.STRING_CODE_RANGE_SLOT, CodeRange.CR_UNKNOWN.toInt());
    frame.setBoolean(FormatFrameDescriptor.TAINT_SLOT, false);

    child.execute(frame);

    final int outputLength;

    try {
      outputLength = frame.getInt(FormatFrameDescriptor.OUTPUT_POSITION_SLOT);
    } catch (FrameSlotTypeException e) {
      throw new IllegalStateException(e);
    }

    if (outputLength > expectedLength) {
      CompilerDirectives.transferToInterpreterAndInvalidate();

      /*
       * Don't over-compensate and allocate 2x or something like that for next time, as we have to copy the
       * byte[] at the end if it's too big to make it fit its contents. In the ideal case the byte[] is exactly
       * the right size. If we have to keep making it bigger in the slow-path, we can live with that.
       */

      expectedLength = outputLength;
    }

    final byte[] output;

    try {
      output = (byte[]) frame.getObject(FormatFrameDescriptor.OUTPUT_SLOT);
    } catch (FrameSlotTypeException e) {
      throw new IllegalStateException(e);
    }

    final boolean taint;

    try {
      taint = frame.getBoolean(FormatFrameDescriptor.TAINT_SLOT);
    } catch (FrameSlotTypeException e) {
      throw new IllegalStateException(e);
    }

    final int stringLength;

    if (encoding == FormatEncoding.UTF_8) {
      try {
        stringLength = frame.getInt(FormatFrameDescriptor.STRING_LENGTH_SLOT);
      } catch (FrameSlotTypeException e) {
        throw new IllegalStateException(e);
      }
    } else {
      stringLength = outputLength;
    }

    final CodeRange stringCodeRange;

    try {
      stringCodeRange =
          CodeRange.fromInt(frame.getInt(FormatFrameDescriptor.STRING_CODE_RANGE_SLOT));
    } catch (FrameSlotTypeException e) {
      throw new IllegalStateException(e);
    }

    return new BytesResult(output, outputLength, stringLength, stringCodeRange, taint, encoding);
  }