public static void createContinuation(Ruby runtime) {
    RubyClass cContinuation =
        runtime.defineClass(
            "Continuation", runtime.getObject(), runtime.getObject().getAllocator());

    cContinuation.setClassIndex(ClassIndex.CONTINUATION);
    cContinuation.setReifiedClass(RubyContinuation.class);

    cContinuation.defineAnnotatedMethods(RubyContinuation.class);
    cContinuation.getSingletonClass().undefineMethod("new");

    runtime.setContinuation(cContinuation);
  }
Example #2
0
  public static RubyClass createThreadClass(Ruby runtime) {
    // FIXME: In order for Thread to play well with the standard 'new' behavior,
    // it must provide an allocator that can create empty object instances which
    // initialize then fills with appropriate data.
    RubyClass threadClass =
        runtime.defineClass(
            "Thread", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
    runtime.setThread(threadClass);

    threadClass.index = ClassIndex.THREAD;
    threadClass.setReifiedClass(RubyThread.class);

    threadClass.defineAnnotatedMethods(RubyThread.class);

    RubyThread rubyThread = new RubyThread(runtime, threadClass);
    // TODO: need to isolate the "current" thread from class creation
    rubyThread.threadImpl = new NativeThread(rubyThread, Thread.currentThread());
    runtime.getThreadService().setMainThread(Thread.currentThread(), rubyThread);

    // set to default thread group
    runtime.getDefaultThreadGroup().addDirectly(rubyThread);

    threadClass.setMarshal(ObjectMarshal.NOT_MARSHALABLE_MARSHAL);

    return threadClass;
  }
Example #3
0
  public static void initARGV(Ruby runtime) {
    // define ARGV and $* for this runtime
    RubyArray argvArray = runtime.newArray();
    String[] argv = runtime.getInstanceConfig().getArgv();

    for (String arg : argv) {
      argvArray.append(RubyString.newInternalFromJavaExternal(runtime, arg));
    }

    if (runtime.getObject().getConstantNoConstMissing("ARGV") != null) {
      ((RubyArray) runtime.getObject().getConstant("ARGV")).replace(argvArray);
    } else {
      runtime.getObject().setConstantQuiet("ARGV", argvArray);
      runtime.getGlobalVariables().define("$*", new ValueAccessor(argvArray), GLOBAL);
    }
  }
Example #4
0
 public static RubyClass createRandomClass(Ruby runtime) {
   RubyClass randomClass = runtime.defineClass("Random", runtime.getObject(), RANDOM_ALLOCATOR);
   randomClass.defineAnnotatedMethods(RubyRandom.class);
   RubyRandom defaultRand = new RubyRandom(runtime, randomClass);
   defaultRand.random = new RandomType(randomSeed(runtime));
   randomClass.setConstant("DEFAULT", defaultRand);
   runtime.setDefaultRand(defaultRand.random);
   runtime.setRandomClass(randomClass);
   return randomClass;
 }
Example #5
0
  private RubyModule buildClassFromDescriptor(ThreadContext context) {
    Ruby runtime = context.runtime;

    ObjectAllocator allocator =
        new ObjectAllocator() {
          @Override
          public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
            return new RubyMessage(runtime, klazz, descriptor);
          }
        };

    // rb_define_class_id
    RubyClass klass = RubyClass.newClass(runtime, runtime.getObject());
    klass.setAllocator(allocator);
    klass.makeMetaClass(runtime.getObject().getMetaClass());
    klass.inherit(runtime.getObject());
    klass.instance_variable_set(runtime.newString(Utils.DESCRIPTOR_INSTANCE_VAR), this);
    klass.defineAnnotatedMethods(RubyMessage.class);
    return klass;
  }
Example #6
0
  public static RubyClass createProcClass(Ruby runtime) {
    RubyClass procClass = runtime.defineClass("Proc", runtime.getObject(), PROC_ALLOCATOR);
    runtime.setProc(procClass);

    procClass.index = ClassIndex.PROC;
    procClass.setReifiedClass(RubyProc.class);

    procClass.defineAnnotatedMethods(RubyProc.class);

    return procClass;
  }
Example #7
0
  public static RubyClass createProcClass(Ruby runtime) {
    RubyClass procClass =
        runtime.defineClass("Proc", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
    runtime.setProc(procClass);

    procClass.setClassIndex(ClassIndex.PROC);
    procClass.setReifiedClass(RubyProc.class);

    procClass.defineAnnotatedMethods(RubyProc.class);

    return procClass;
  }
Example #8
0
  public static RubyClass createStringIOClass(final Ruby runtime) {
    RubyClass stringIOClass =
        runtime.defineClass("StringIO", runtime.fastGetClass("Data"), STRINGIO_ALLOCATOR);

    stringIOClass.defineAnnotatedMethods(RubyStringIO.class);
    stringIOClass.includeModule(runtime.getEnumerable());

    if (runtime.getObject().isConstantDefined("Java")) {
      stringIOClass.defineAnnotatedMethods(IOJavaAddons.AnyIO.class);
    }

    return stringIOClass;
  }
Example #9
0
  public static RubyClass createTimeClass(Ruby runtime) {
    RubyClass timeClass = runtime.defineClass("Time", runtime.getObject(), TIME_ALLOCATOR);

    timeClass.index = ClassIndex.TIME;
    timeClass.setReifiedClass(RubyTime.class);

    runtime.setTime(timeClass);

    timeClass.includeModule(runtime.getComparable());

    timeClass.defineAnnotatedMethods(RubyTime.class);

    return timeClass;
  }
Example #10
0
  public static RubyClass createNumericClass(Ruby runtime) {
    RubyClass numeric = runtime.defineClass("Numeric", runtime.getObject(), NUMERIC_ALLOCATOR);
    runtime.setNumeric(numeric);

    numeric.setClassIndex(ClassIndex.NUMERIC);
    numeric.setReifiedClass(RubyNumeric.class);

    numeric.kindOf = new RubyModule.JavaClassKindOf(RubyNumeric.class);

    numeric.includeModule(runtime.getComparable());
    numeric.defineAnnotatedMethods(RubyNumeric.class);

    return numeric;
  }
Example #11
0
  public static RubyClass createFalseClass(Ruby runtime) {
    RubyClass falseClass =
        runtime.defineClass(
            "FalseClass", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
    runtime.setFalseClass(falseClass);
    falseClass.setClassIndex(ClassIndex.FALSE);
    falseClass.setReifiedClass(RubyBoolean.class);

    falseClass.defineAnnotatedMethods(False.class);
    falseClass.defineAnnotatedMethods(RubyBoolean.class);

    falseClass.getMetaClass().undefineMethod("new");

    return falseClass;
  }
Example #12
0
  public static RubyClass createMatchDataClass(Ruby runtime) {
    RubyClass matchDataClass =
        runtime.defineClass("MatchData", runtime.getObject(), MATCH_DATA_ALLOCATOR);
    runtime.setMatchData(matchDataClass);

    matchDataClass.setClassIndex(ClassIndex.MATCHDATA);
    matchDataClass.setReifiedClass(RubyMatchData.class);

    runtime.defineGlobalConstant("MatchingData", matchDataClass);
    matchDataClass.kindOf = new RubyModule.JavaClassKindOf(RubyMatchData.class);

    matchDataClass.getMetaClass().undefineMethod("new");
    matchDataClass.defineAnnotatedMethods(RubyMatchData.class);
    return matchDataClass;
  }
Example #13
0
 public static void createRubyDescriptor(Ruby runtime) {
   RubyModule protobuf = runtime.getClassFromPath("Google::Protobuf");
   RubyClass cDescriptor =
       protobuf.defineClassUnder(
           "Descriptor",
           runtime.getObject(),
           new ObjectAllocator() {
             @Override
             public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
               return new RubyDescriptor(runtime, klazz);
             }
           });
   cDescriptor.includeModule(runtime.getEnumerable());
   cDescriptor.defineAnnotatedMethods(RubyDescriptor.class);
 }
Example #14
0
  public static RubyClass createTrueClass(Ruby runtime) {
    RubyClass trueClass =
        runtime.defineClass(
            "TrueClass", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
    runtime.setTrueClass(trueClass);
    trueClass.setClassIndex(ClassIndex.TRUE);
    trueClass.setReifiedClass(RubyBoolean.class);

    trueClass.defineAnnotatedMethods(True.class);
    trueClass.defineAnnotatedMethods(RubyBoolean.class);

    trueClass.getMetaClass().undefineMethod("new");

    return trueClass;
  }
Example #15
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;
  }
Example #16
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);
    }
  }
Example #17
0
  public static RubyClass createEncodingClass(Ruby runtime) {
    RubyClass encodingc =
        runtime.defineClass(
            "Encoding", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
    runtime.setEncoding(encodingc);
    encodingc.index = ClassIndex.ENCODING;
    encodingc.setReifiedClass(RubyEncoding.class);
    encodingc.kindOf =
        new RubyModule.KindOf() {
          @Override
          public boolean isKindOf(IRubyObject obj, RubyModule type) {
            return obj instanceof RubyEncoding;
          }
        };

    encodingc.getSingletonClass().undefineMethod("allocate");
    encodingc.defineAnnotatedMethods(RubyEncoding.class);

    return encodingc;
  }
Example #18
0
  public static RubyClass createMatchDataClass(Ruby runtime) {
    RubyClass matchDataClass =
        runtime.defineClass("MatchData", runtime.getObject(), MATCH_DATA_ALLOCATOR);
    runtime.setMatchData(matchDataClass);

    matchDataClass.index = ClassIndex.MATCHDATA;
    matchDataClass.setReifiedClass(RubyMatchData.class);

    runtime.defineGlobalConstant("MatchingData", matchDataClass);
    matchDataClass.kindOf =
        new RubyModule.KindOf() {
          @Override
          public boolean isKindOf(IRubyObject obj, RubyModule type) {
            return obj instanceof RubyMatchData;
          }
        };

    matchDataClass.getMetaClass().undefineMethod("new");
    matchDataClass.defineAnnotatedMethods(RubyMatchData.class);
    return matchDataClass;
  }
Example #19
0
  public static RubyClass createRangeClass(Ruby runtime) {
    RubyClass result = runtime.defineClass("Range", runtime.getObject(), RANGE_ALLOCATOR);
    runtime.setRange(result);
    result.kindOf =
        new RubyModule.KindOf() {
          public boolean isKindOf(IRubyObject obj, RubyModule type) {
            return obj instanceof RubyRange;
          }
        };

    result.setMarshal(RANGE_MARSHAL);
    result.includeModule(runtime.getEnumerable());

    // We override Enumerable#member? since ranges in 1.8.1 are continuous.
    //        result.defineMethod("member?", callbackFactory.getMethod("include_p",
    // RubyKernel.IRUBY_OBJECT));
    //        result.defineMethod("===", callbackFactory.getMethod("include_p",
    // RubyKernel.IRUBY_OBJECT));

    result.defineAnnotatedMethods(RubyRange.class);
    return result;
  }
Example #20
0
  public static IRubyObject each_object(
      ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
    RubyModule rubyClass;
    if (args.length == 0) {
      rubyClass = recv.getRuntime().getObject();
    } else {
      if (!(args[0] instanceof RubyModule))
        throw recv.getRuntime().newTypeError("class or module required");
      rubyClass = (RubyModule) args[0];
    }
    Ruby runtime = recv.getRuntime();
    int count = 0;
    if (rubyClass != runtime.getClassClass()) {
      if (!runtime.isObjectSpaceEnabled()) {
        throw runtime.newRuntimeError(
            "ObjectSpace is disabled; each_object will only work with Class, pass -X+O to enable");
      }
      Iterator iter = recv.getRuntime().getObjectSpace().iterator(rubyClass);

      IRubyObject obj = null;
      while ((obj = (IRubyObject) iter.next()) != null) {
        count++;
        block.yield(context, obj);
      }
    } else {
      Iterator iter = runtime.getObject().subclasses(true).iterator();

      while (iter.hasNext()) {
        IRubyObject obj = (IRubyObject) iter.next();
        if (obj instanceof RubyClass && ((RubyClass) obj).isIncluded()) {
          continue;
        }
        count++;
        block.yield(context, obj);
      }
    }
    return recv.getRuntime().newFixnum(count);
  }
Example #21
0
 private static IRubyObject getEnvTimeZone(Ruby runtime) {
   RubyString tzVar = runtime.newString(TZ_STRING);
   RubyHash h = ((RubyHash) runtime.getObject().getConstant("ENV"));
   IRubyObject tz = h.op_aref(runtime.getCurrentContext(), tzVar);
   return tz;
 }
Example #22
0
  /** Create the Zlib module and add it to the Ruby runtime. */
  public static RubyModule createZlibModule(Ruby runtime) {
    RubyModule result = runtime.defineModule("Zlib");

    RubyClass gzfile =
        result.defineClassUnder("GzipFile", runtime.getObject(), RubyGzipFile.GZIPFILE_ALLOCATOR);
    gzfile.defineAnnotatedMethods(RubyGzipFile.class);

    RubyClass gzreader =
        result.defineClassUnder("GzipReader", gzfile, RubyGzipReader.GZIPREADER_ALLOCATOR);
    gzreader.includeModule(runtime.getEnumerable());
    gzreader.defineAnnotatedMethods(RubyGzipReader.class);

    RubyClass standardError = runtime.getStandardError();
    RubyClass zlibError =
        result.defineClassUnder("Error", standardError, standardError.getAllocator());
    gzreader.defineClassUnder("Error", zlibError, zlibError.getAllocator());

    RubyClass gzwriter =
        result.defineClassUnder("GzipWriter", gzfile, RubyGzipWriter.GZIPWRITER_ALLOCATOR);
    gzwriter.defineAnnotatedMethods(RubyGzipWriter.class);

    result.defineConstant("ZLIB_VERSION", runtime.newString("1.2.1"));
    result.defineConstant("VERSION", runtime.newString("0.6.0"));

    result.defineConstant("BINARY", runtime.newFixnum(0));
    result.defineConstant("ASCII", runtime.newFixnum(1));
    result.defineConstant("UNKNOWN", runtime.newFixnum(2));

    result.defineConstant("DEF_MEM_LEVEL", runtime.newFixnum(8));
    result.defineConstant("MAX_MEM_LEVEL", runtime.newFixnum(9));

    result.defineConstant("OS_UNIX", runtime.newFixnum(3));
    result.defineConstant("OS_UNKNOWN", runtime.newFixnum(255));
    result.defineConstant("OS_CODE", runtime.newFixnum(11));
    result.defineConstant("OS_ZSYSTEM", runtime.newFixnum(8));
    result.defineConstant("OS_VMCMS", runtime.newFixnum(4));
    result.defineConstant("OS_VMS", runtime.newFixnum(2));
    result.defineConstant("OS_RISCOS", runtime.newFixnum(13));
    result.defineConstant("OS_MACOS", runtime.newFixnum(7));
    result.defineConstant("OS_OS2", runtime.newFixnum(6));
    result.defineConstant("OS_AMIGA", runtime.newFixnum(1));
    result.defineConstant("OS_QDOS", runtime.newFixnum(12));
    result.defineConstant("OS_WIN32", runtime.newFixnum(11));
    result.defineConstant("OS_ATARI", runtime.newFixnum(5));
    result.defineConstant("OS_MSDOS", runtime.newFixnum(0));
    result.defineConstant("OS_CPM", runtime.newFixnum(9));
    result.defineConstant("OS_TOPS20", runtime.newFixnum(10));

    result.defineConstant("DEFAULT_STRATEGY", runtime.newFixnum(0));
    result.defineConstant("FILTERED", runtime.newFixnum(1));
    result.defineConstant("HUFFMAN_ONLY", runtime.newFixnum(2));

    result.defineConstant("NO_FLUSH", runtime.newFixnum(0));
    result.defineConstant("SYNC_FLUSH", runtime.newFixnum(2));
    result.defineConstant("FULL_FLUSH", runtime.newFixnum(3));
    result.defineConstant("FINISH", runtime.newFixnum(4));

    result.defineConstant("NO_COMPRESSION", runtime.newFixnum(0));
    result.defineConstant("BEST_SPEED", runtime.newFixnum(1));
    result.defineConstant("DEFAULT_COMPRESSION", runtime.newFixnum(-1));
    result.defineConstant("BEST_COMPRESSION", runtime.newFixnum(9));

    result.defineConstant("MAX_WBITS", runtime.newFixnum(15));

    result.defineAnnotatedMethods(RubyZlib.class);

    result.defineClassUnder("StreamEnd", zlibError, zlibError.getAllocator());
    result.defineClassUnder("StreamError", zlibError, zlibError.getAllocator());
    result.defineClassUnder("BufError", zlibError, zlibError.getAllocator());
    result.defineClassUnder("NeedDict", zlibError, zlibError.getAllocator());
    result.defineClassUnder("MemError", zlibError, zlibError.getAllocator());
    result.defineClassUnder("VersionError", zlibError, zlibError.getAllocator());
    result.defineClassUnder("DataError", zlibError, zlibError.getAllocator());

    RubyClass gzError = gzfile.defineClassUnder("Error", zlibError, zlibError.getAllocator());
    gzfile.defineClassUnder("CRCError", gzError, gzError.getAllocator());
    gzfile.defineClassUnder("NoFooter", gzError, gzError.getAllocator());
    gzfile.defineClassUnder("LengthError", gzError, gzError.getAllocator());

    // ZStream actually *isn't* allocatable
    RubyClass zstream =
        result.defineClassUnder(
            "ZStream", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
    zstream.defineAnnotatedMethods(ZStream.class);
    zstream.undefineMethod("new");

    RubyClass infl = result.defineClassUnder("Inflate", zstream, Inflate.INFLATE_ALLOCATOR);
    infl.defineAnnotatedMethods(Inflate.class);

    RubyClass defl = result.defineClassUnder("Deflate", zstream, Deflate.DEFLATE_ALLOCATOR);
    defl.defineAnnotatedMethods(Deflate.class);

    runtime
        .getKernel()
        .callMethod(runtime.getCurrentContext(), "require", runtime.newString("stringio"));

    return result;
  }