Exemplo n.º 1
0
  static MethodHandle buildJittedHandle(InvokeSite site, DynamicMethod method, boolean blockGiven) {
    MethodHandle mh = null;
    SmartBinder binder;
    CompiledIRMethod compiledIRMethod = null;

    if (method instanceof CompiledIRMethod) {
      compiledIRMethod = (CompiledIRMethod) method;
    } else if (method instanceof InterpretedIRMethod) {
      DynamicMethod actualMethod = ((InterpretedIRMethod) method).getActualMethod();
      if (actualMethod instanceof CompiledIRMethod) {
        compiledIRMethod = (CompiledIRMethod) actualMethod;
      }
    }

    if (compiledIRMethod != null) {
      // attempt IR direct binding
      // TODO: this will have to expand when we start specializing arities

      binder = SmartBinder.from(site.signature).permute("context", "self", "arg.*", "block");

      if (site.arity == -1) {
        // already [], nothing to do
        mh = (MethodHandle) compiledIRMethod.getHandle();
      } else if (site.arity == 0) {
        MethodHandle specific;
        if ((specific = compiledIRMethod.getHandleFor(site.arity)) != null) {
          mh = specific;
        } else {
          mh = (MethodHandle) compiledIRMethod.getHandle();
          binder = binder.insert(2, "args", IRubyObject.NULL_ARRAY);
        }
      } else {
        MethodHandle specific;
        if ((specific = compiledIRMethod.getHandleFor(site.arity)) != null) {
          mh = specific;
        } else {
          mh = (MethodHandle) compiledIRMethod.getHandle();
          binder = binder.collect("args", "arg.*");
        }
      }

      if (!blockGiven) {
        binder = binder.append("block", Block.class, Block.NULL_BLOCK);
      }

      binder =
          binder
              .insert(1, "scope", StaticScope.class, compiledIRMethod.getStaticScope())
              .append("class", RubyModule.class, compiledIRMethod.getImplementationClass());

      mh = binder.invoke(mh).handle();
    }

    return mh;
  }
Exemplo n.º 2
0
  static MethodHandle buildGenericHandle(
      InvokeSite site, DynamicMethod method, RubyClass dispatchClass) {
    SmartBinder binder;

    binder =
        SmartBinder.from(site.signature)
            .permute("context", "self", "arg.*", "block")
            .insert(
                2,
                new String[] {"rubyClass", "name"},
                new Class[] {RubyModule.class, String.class},
                dispatchClass,
                site.name())
            .insert(0, "method", DynamicMethod.class, method);

    if (site.arity > 3) {
      binder = binder.collect("args", "arg.*");
    }

    return binder.invokeVirtualQuiet(LOOKUP, "call").handle();
  }
Exemplo n.º 3
0
  public Binder prepareBinder() {
    SmartBinder binder = SmartBinder.from(signature);

    // prepare arg[]
    if (arity == -1) {
      // do nothing, already have IRubyObject[] in args
    } else if (arity == 0) {
      binder = binder.insert(argOffset, "args", IRubyObject.NULL_ARRAY);
    } else {
      binder = binder.collect("args", "arg[0-9]+");
    }

    // add block if needed
    if (signature.lastArgType() != Block.class) {
      binder = binder.append("block", Block.NULL_BLOCK);
    }

    // bind to site
    binder = binder.insert(0, "site", this);

    return binder.binder();
  }