Exemple #1
0
  public static IRubyObject lexicalSearchConst(
      ThreadContext context,
      StaticScope scope,
      MutableCallSite site,
      String constName,
      boolean noPrivateConsts)
      throws Throwable {
    Ruby runtime = context.runtime;

    IRubyObject constant = scope.getConstantInner(constName);

    if (constant == null) {
      constant = UndefinedValue.UNDEFINED;
    }

    SwitchPoint switchPoint = (SwitchPoint) runtime.getConstantInvalidator(constName).getData();

    // bind constant until invalidated
    MethodHandle target = Binder.from(site.type()).drop(0, 2).constant(constant);
    MethodHandle fallback =
        Binder.from(site.type())
            .append(site, constName)
            .append(noPrivateConsts)
            .invokeStatic(LOOKUP, Bootstrap.class, "lexicalSearchConst");

    site.setTarget(switchPoint.guardWithTest(target, fallback));

    return constant;
  }
Exemple #2
0
 public static CallSite contextValueString(
     Lookup lookup, String name, MethodType type, String str) {
   MutableCallSite site = new MutableCallSite(type);
   site.setTarget(
       Binder.from(type).append(site, str).invokeStaticQuiet(lookup, Bootstrap.class, name));
   return site;
 }
Exemple #3
0
  public static CallSite searchConst(
      Lookup lookup, String name, MethodType type, int noPrivateConsts) {
    MutableCallSite site = new MutableCallSite(type);
    String[] bits = name.split(":");
    String constName = bits[1];

    MethodHandle handle =
        Binder.from(lookup, type)
            .append(site, constName.intern())
            .append(noPrivateConsts == 0 ? false : true)
            .invokeStaticQuiet(LOOKUP, Bootstrap.class, bits[0]);

    site.setTarget(handle);

    return site;
  }
Exemple #4
0
  public static IRubyObject inheritanceSearchConst(
      ThreadContext context,
      IRubyObject cmVal,
      MutableCallSite site,
      String constName,
      boolean noPrivateConsts)
      throws Throwable {
    Ruby runtime = context.runtime;
    RubyModule module;

    if (cmVal instanceof RubyModule) {
      module = (RubyModule) cmVal;
    } else {
      throw runtime.newTypeError(cmVal + " is not a type/class");
    }

    IRubyObject constant =
        noPrivateConsts
            ? module.getConstantFromNoConstMissing(constName, false)
            : module.getConstantNoConstMissing(constName);

    if (constant == null) {
      constant = UndefinedValue.UNDEFINED;
    }

    SwitchPoint switchPoint = (SwitchPoint) runtime.getConstantInvalidator(constName).getData();

    // bind constant until invalidated
    MethodHandle target = Binder.from(site.type()).drop(0, 2).constant(constant);
    MethodHandle fallback =
        Binder.from(site.type())
            .append(site, constName)
            .append(noPrivateConsts)
            .invokeStatic(LOOKUP, Bootstrap.class, "inheritanceSearchConst");

    // test that module is same as before
    MethodHandle test =
        Binder.from(site.type().changeReturnType(boolean.class))
            .drop(0, 1)
            .insert(1, module.id)
            .invokeStaticQuiet(LOOKUP, Bootstrap.class, "testArg0ModuleMatch");
    target = guardWithTest(test, target, fallback);
    site.setTarget(switchPoint.guardWithTest(target, fallback));

    return constant;
  }
Exemple #5
0
  public static IRubyObject searchConst(
      ThreadContext context,
      StaticScope staticScope,
      MutableCallSite site,
      String constName,
      boolean noPrivateConsts)
      throws Throwable {

    // Lexical lookup
    Ruby runtime = context.getRuntime();
    RubyModule object = runtime.getObject();
    IRubyObject constant =
        (staticScope == null)
            ? object.getConstant(constName)
            : staticScope.getConstantInner(constName);

    // Inheritance lookup
    RubyModule module = null;
    if (constant == null) {
      // SSS FIXME: Is this null check case correct?
      module = staticScope == null ? object : staticScope.getModule();
      constant =
          noPrivateConsts
              ? module.getConstantFromNoConstMissing(constName, false)
              : module.getConstantNoConstMissing(constName);
    }

    // Call const_missing or cache
    if (constant == null) {
      return module.callMethod(context, "const_missing", context.runtime.fastNewSymbol(constName));
    }

    SwitchPoint switchPoint = (SwitchPoint) runtime.getConstantInvalidator(constName).getData();

    // bind constant until invalidated
    MethodHandle target = Binder.from(site.type()).drop(0, 2).constant(constant);
    MethodHandle fallback =
        Binder.from(site.type())
            .append(site, constName)
            .append(noPrivateConsts)
            .invokeStatic(LOOKUP, Bootstrap.class, "searchConst");

    site.setTarget(switchPoint.guardWithTest(target, fallback));

    return constant;
  }
Exemple #6
0
  public static Ruby runtime(ThreadContext context, MutableCallSite site) {
    MethodHandle constant = (MethodHandle) context.runtime.constant();
    if (constant == null)
      constant = (MethodHandle) OptoFactory.newConstantWrapper(Ruby.class, context.runtime);

    site.setTarget(constant);

    return context.runtime;
  }
Exemple #7
0
  public static IRubyObject nil(ThreadContext context, MutableCallSite site) {
    MethodHandle constant = (MethodHandle) ((RubyNil) context.nil).constant();
    if (constant == null)
      constant = (MethodHandle) OptoFactory.newConstantWrapper(IRubyObject.class, context.nil);

    site.setTarget(constant);

    return context.nil;
  }
Exemple #8
0
  public static CallSite string(
      Lookup lookup, String name, MethodType type, String value, String encodingName) {
    Encoding encoding;
    EncodingDB.Entry entry = EncodingDB.getEncodings().get(encodingName.getBytes());
    if (entry == null) entry = EncodingDB.getAliases().get(encodingName.getBytes());
    if (entry == null) throw new RuntimeException("could not find encoding: " + encodingName);
    encoding = entry.getEncoding();
    ByteList byteList = new ByteList(value.getBytes(RubyEncoding.ISO), encoding);
    MutableCallSite site = new MutableCallSite(type);
    Binder binder =
        Binder.from(RubyString.class, ThreadContext.class)
            .insert(0, arrayOf(MutableCallSite.class, ByteList.class), site, byteList);
    if (name.equals("frozen")) {
      site.setTarget(binder.invokeStaticQuiet(lookup, Bootstrap.class, "frozenString"));
    } else {
      site.setTarget(binder.invokeStaticQuiet(lookup, Bootstrap.class, "string"));
    }

    return site;
  }
Exemple #9
0
  public static RubyEncoding encoding(ThreadContext context, MutableCallSite site, String name) {
    RubyEncoding rubyEncoding = IRRuntimeHelpers.retrieveEncoding(context, name);

    MethodHandle constant = (MethodHandle) rubyEncoding.constant();
    if (constant == null)
      constant = (MethodHandle) OptoFactory.newConstantWrapper(RubyEncoding.class, rubyEncoding);

    site.setTarget(constant);

    return rubyEncoding;
  }
Exemple #10
0
  public static IRubyObject False(ThreadContext context, MutableCallSite site) {
    MethodHandle constant = (MethodHandle) context.runtime.getFalse().constant();
    if (constant == null)
      constant =
          (MethodHandle)
              OptoFactory.newConstantWrapper(IRubyObject.class, context.runtime.getFalse());

    site.setTarget(constant);

    return context.runtime.getFalse();
  }
Exemple #11
0
  public static RubyString frozenString(MutableCallSite site, ByteList value, ThreadContext context)
      throws Throwable {
    RubyString frozen =
        context.runtime.freezeAndDedupString(RubyString.newStringShared(context.runtime, value));
    MethodHandle handle =
        Binder.from(RubyString.class, ThreadContext.class).dropAll().constant(frozen);

    site.setTarget(handle);

    return frozen;
  }
Exemple #12
0
  public static RubyString string(MutableCallSite site, ByteList value, ThreadContext context)
      throws Throwable {
    MethodHandle handle =
        SmartBinder.from(STRING_SIGNATURE)
            .invoke(NEW_STRING_SHARED_HANDLE.apply("byteList", value))
            .handle();

    site.setTarget(handle);

    return RubyString.newStringShared(context.runtime, value);
  }