示例#1
0
    @ExplodeLoop
    @Override
    public RubyArray executeArray(VirtualFrame frame) {
      CompilerDirectives.transferToInterpreter();

      final Object[] executedValues = new Object[values.length];

      for (int n = 0; n < values.length; n++) {
        executedValues[n] = values[n].execute(frame);
      }

      final RubyArray array =
          RubyArray.fromObjects(getContext().getCoreLibrary().getArrayClass(), executedValues);
      final Object store = array.getStore();

      if (store == null) {
        replace(new EmptyArrayLiteralNode(getContext(), getSourceSection(), values));
      }
      if (store instanceof int[]) {
        replace(new IntegerFixnumArrayLiteralNode(getContext(), getSourceSection(), values));
      } else if (store instanceof long[]) {
        replace(new LongFixnumArrayLiteralNode(getContext(), getSourceSection(), values));
      } else if (store instanceof double[]) {
        replace(new FloatArrayLiteralNode(getContext(), getSourceSection(), values));
      } else {
        replace(new ObjectArrayLiteralNode(getContext(), getSourceSection(), values));
      }

      return array;
    }
  @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));
    }
  }
示例#3
0
文件: HashNodes.java 项目: phs/jruby
    @Specialization
    public RubyArray values(RubyHash hash) {
      final RubyArray array = new RubyArray(getContext().getCoreLibrary().getArrayClass());

      for (Object value : hash.storage.values()) {
        array.push(value);
      }

      return array;
    }
示例#4
0
文件: HashNodes.java 项目: phs/jruby
    @Specialization
    public RubyArray keys(RubyHash hash) {
      final RubyArray array = new RubyArray(getContext().getCoreLibrary().getArrayClass());

      for (Object key : hash.storage.keySet()) {
        array.push(key);
      }

      return array;
    }
示例#5
0
文件: HashNodes.java 项目: phs/jruby
    @Specialization
    public RubyArray map(VirtualFrame frame, RubyHash hash, RubyProc block) {
      final RubyArray result = new RubyArray(getContext().getCoreLibrary().getArrayClass());

      for (Map.Entry<Object, Object> entry : hash.storage.entrySet()) {
        result.push(yield(frame, block, entry.getKey(), entry.getValue()));
      }

      return result;
    }
示例#6
0
 @Specialization
 public RubyString javaClassOf(RubyArray array) {
   notDesignedForCompilation();
   return getContext()
       .makeString(
           "RubyArray("
               + (array.getStore() == null ? "null" : array.getStore().getClass())
               + "*"
               + array.getSize()
               + ")");
 }
示例#7
0
  @Override
  public void init() {
    if (RubyContext.PRINT_RUNTIME) {
      runtime
          .getInstanceConfig()
          .getError()
          .println("jruby: using " + Truffle.getRuntime().getName());
    }

    // Bring in core method nodes

    CoreMethodNodeManager.addStandardMethods(truffleContext.getCoreLibrary().getObjectClass());

    // Give the core library manager a chance to tweak some of those methods

    truffleContext.getCoreLibrary().initializeAfterMethodsAdded();

    // Set program arguments

    for (IRubyObject arg :
        ((org.jruby.RubyArray) runtime.getObject().getConstant("ARGV")).toJavaArray()) {
      assert arg != null;

      truffleContext.getCoreLibrary().getArgv().slowPush(truffleContext.makeString(arg.toString()));
    }

    // Set the load path

    final RubyArray loadPath =
        (RubyArray)
            truffleContext.getCoreLibrary().getGlobalVariablesObject().getInstanceVariable("$:");

    for (IRubyObject path :
        ((org.jruby.RubyArray) runtime.getLoadService().getLoadPath()).toJavaArray()) {
      loadPath.slowPush(truffleContext.makeString(path.toString()));
    }

    // Hook

    if (truffleContext.getHooks() != null) {
      truffleContext.getHooks().afterInit(truffleContext);
    }
  }
示例#8
0
  protected RubyArray makeGeneric(VirtualFrame frame, Object[] alreadyExecuted) {
    CompilerAsserts.neverPartOfCompilation();

    replace(new ObjectArrayLiteralNode(getContext(), getSourceSection(), values));

    final Object[] executedValues = new Object[values.length];

    for (int n = 0; n < values.length; n++) {
      if (n < alreadyExecuted.length) {
        executedValues[n] = alreadyExecuted[n];
      } else {
        executedValues[n] = values[n].execute(frame);
      }
    }

    return RubyArray.fromObjects(getContext().getCoreLibrary().getArrayClass(), executedValues);
  }
示例#9
0
文件: HashNodes.java 项目: phs/jruby
    @Specialization
    public RubyHash construct(Object[] args) {
      final RubyHash hash = new RubyHash(getContext().getCoreLibrary().getHashClass());

      if (args.length == 1) {
        final RubyArray array = (RubyArray) args[0];

        for (int n = 0; n < array.size(); n++) {
          final RubyArray keyValue = (RubyArray) array.get(n);
          hash.put(keyValue.get(0), keyValue.get(1));
        }
      } else {
        if (args.length % 2 != 0) {
          // TODO(CS): figure out what error to throw here
          throw new UnsupportedOperationException();
        }

        for (int n = 0; n < args.length; n += 2) {
          hash.put(args[n], args[n + 1]);
        }
      }

      return hash;
    }