// FIXME: We should have a better way of using the proper method based // on version as a general solution... private RubyString makeShared(Ruby runtime, RubyString str, int begin, int length) { if (runtime.is1_9()) { return str.makeShared19(runtime, begin, length); } else { return str.makeShared(runtime, begin, length); } }
public static RubyClass createIntegerClass(Ruby runtime) { RubyClass integer = runtime.defineClass( "Integer", runtime.getNumeric(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR); runtime.setInteger(integer); integer.index = ClassIndex.INTEGER; integer.setReifiedClass(RubyInteger.class); integer.kindOf = new RubyModule.KindOf() { public boolean isKindOf(IRubyObject obj, RubyModule type) { return obj instanceof RubyInteger; } }; integer.getSingletonClass().undefineMethod("new"); if (!runtime.is1_9()) { integer.includeModule(runtime.getPrecision()); } integer.defineAnnotatedMethods(RubyInteger.class); return integer; }
public void exceptionRaised(RaiseException exception) { assert isCurrent(); RubyException rubyException = exception.getException(); Ruby runtime = rubyException.getRuntime(); if (runtime.getSystemExit().isInstance(rubyException)) { runtime .getThreadService() .getMainThread() .raise(new IRubyObject[] {rubyException}, Block.NULL_BLOCK); } else if (abortOnException(runtime)) { RubyException systemExit; if (!runtime.is1_9()) { runtime.printError(rubyException); systemExit = RubySystemExit.newInstance(runtime, 1); systemExit.message = rubyException.message; systemExit.set_backtrace(rubyException.backtrace()); } else { systemExit = rubyException; } runtime .getThreadService() .getMainThread() .raise(new IRubyObject[] {systemExit}, Block.NULL_BLOCK); return; } else if (runtime.getDebug().isTrue()) { runtime.printError(exception.getException()); } exitingException = exception; }
@JRubyMethod(name = "inspect") @Override public IRubyObject inspect() { if (str == null) return anyToString(); Ruby runtime = getRuntime(); RubyString result = runtime.newString(); result.cat((byte) '#').cat((byte) '<'); result.append(getMetaClass().getRealClass().to_s()); NameEntry[] names = new NameEntry[regs == null ? 1 : regs.numRegs]; if (pattern.numberOfNames() > 0) { for (Iterator<NameEntry> i = pattern.namedBackrefIterator(); i.hasNext(); ) { NameEntry e = i.next(); for (int num : e.getBackRefs()) names[num] = e; } } for (int i = 0; i < names.length; i++) { result.cat((byte) ' '); if (i > 0) { NameEntry e = names[i]; if (e != null) { result.cat(e.name, e.nameP, e.nameEnd - e.nameP); } else { result.cat((byte) ('0' + i)); } result.cat((byte) ':'); } IRubyObject v = RubyRegexp.nth_match(i, this); if (v.isNil()) { result.cat("nil".getBytes()); } else { result.append(((RubyString) v).inspectCommon(runtime.is1_9())); } } return result.cat((byte) '>'); }
public static void createGlobals(ThreadContext context, Ruby runtime) { runtime.defineGlobalConstant("TOPLEVEL_BINDING", runtime.newBinding()); runtime.defineGlobalConstant("TRUE", runtime.getTrue()); runtime.defineGlobalConstant("FALSE", runtime.getFalse()); runtime.defineGlobalConstant("NIL", runtime.getNil()); // define ARGV and $* for this runtime RubyArray argvArray = runtime.newArray(); String[] argv = runtime.getInstanceConfig().getArgv(); for (int i = 0; i < argv.length; i++) { argvArray.append(RubyString.newStringShared(runtime, argv[i].getBytes())); } runtime.defineGlobalConstant("ARGV", argvArray); runtime.getGlobalVariables().defineReadonly("$*", new ValueAccessor(argvArray)); IAccessor d = new ValueAccessor(runtime.newString(runtime.getInstanceConfig().displayedFileName())); runtime.getGlobalVariables().define("$PROGRAM_NAME", d); runtime.getGlobalVariables().define("$0", d); // Version information: IRubyObject version = null; IRubyObject patchlevel = null; IRubyObject release = runtime.newString(Constants.COMPILE_DATE).freeze(context); IRubyObject platform = runtime.newString(Constants.PLATFORM).freeze(context); IRubyObject engine = runtime.newString(Constants.ENGINE).freeze(context); switch (runtime.getInstanceConfig().getCompatVersion()) { case RUBY1_8: version = runtime.newString(Constants.RUBY_VERSION).freeze(context); patchlevel = runtime.newFixnum(Constants.RUBY_PATCHLEVEL).freeze(context); break; case RUBY1_9: version = runtime.newString(Constants.RUBY1_9_VERSION).freeze(context); patchlevel = runtime.newFixnum(Constants.RUBY1_9_PATCHLEVEL).freeze(context); break; } runtime.defineGlobalConstant("RUBY_VERSION", version); runtime.defineGlobalConstant("RUBY_PATCHLEVEL", patchlevel); runtime.defineGlobalConstant("RUBY_RELEASE_DATE", release); runtime.defineGlobalConstant("RUBY_PLATFORM", platform); runtime.defineGlobalConstant("RUBY_ENGINE", engine); IRubyObject description = runtime.newString(runtime.getInstanceConfig().getVersionString()).freeze(context); runtime.defineGlobalConstant("RUBY_DESCRIPTION", description); IRubyObject copyright = runtime.newString(runtime.getInstanceConfig().getCopyrightString()).freeze(context); runtime.defineGlobalConstant("RUBY_COPYRIGHT", copyright); runtime.defineGlobalConstant("VERSION", version); runtime.defineGlobalConstant("RELEASE_DATE", release); runtime.defineGlobalConstant("PLATFORM", platform); IRubyObject jrubyVersion = runtime.newString(Constants.VERSION).freeze(context); IRubyObject jrubyRevision = runtime.newString(Constants.REVISION).freeze(context); runtime.defineGlobalConstant("JRUBY_VERSION", jrubyVersion); runtime.defineGlobalConstant("JRUBY_REVISION", jrubyRevision); if (runtime.is1_9()) { // needs to be a fixnum, but our revision is a sha1 hash from git runtime.defineGlobalConstant("RUBY_REVISION", runtime.newFixnum(Constants.RUBY1_9_REVISION)); } GlobalVariable kcodeGV = new KCodeGlobalVariable(runtime, "$KCODE", runtime.newString("NONE")); runtime.defineVariable(kcodeGV); runtime.defineVariable(new GlobalVariable.Copy(runtime, "$-K", kcodeGV)); IRubyObject defaultRS = runtime.newString(runtime.getInstanceConfig().getRecordSeparator()).freeze(context); GlobalVariable rs = new StringGlobalVariable(runtime, "$/", defaultRS); runtime.defineVariable(rs); runtime.setRecordSeparatorVar(rs); runtime.getGlobalVariables().setDefaultSeparator(defaultRS); runtime.defineVariable(new StringGlobalVariable(runtime, "$\\", runtime.getNil())); runtime.defineVariable(new StringGlobalVariable(runtime, "$,", runtime.getNil())); runtime.defineVariable(new LineNumberGlobalVariable(runtime, "$.")); runtime.defineVariable(new LastlineGlobalVariable(runtime, "$_")); runtime.defineVariable(new LastExitStatusVariable(runtime, "$?")); runtime.defineVariable(new ErrorInfoGlobalVariable(runtime, "$!", runtime.getNil())); runtime.defineVariable(new NonEffectiveGlobalVariable(runtime, "$=", runtime.getFalse())); if (runtime.getInstanceConfig().getInputFieldSeparator() == null) { runtime.defineVariable(new GlobalVariable(runtime, "$;", runtime.getNil())); } else { runtime.defineVariable( new GlobalVariable( runtime, "$;", RubyRegexp.newRegexp( runtime, runtime.getInstanceConfig().getInputFieldSeparator(), 0))); } Boolean verbose = runtime.getInstanceConfig().getVerbose(); IRubyObject verboseValue = null; if (verbose == null) { verboseValue = runtime.getNil(); } else if (verbose == Boolean.TRUE) { verboseValue = runtime.getTrue(); } else { verboseValue = runtime.getFalse(); } runtime.defineVariable(new VerboseGlobalVariable(runtime, "$VERBOSE", verboseValue)); IRubyObject debug = runtime.newBoolean(runtime.getInstanceConfig().isDebug()); runtime.defineVariable(new DebugGlobalVariable(runtime, "$DEBUG", debug)); runtime.defineVariable(new DebugGlobalVariable(runtime, "$-d", debug)); runtime.defineVariable(new SafeGlobalVariable(runtime, "$SAFE")); runtime.defineVariable(new BacktraceGlobalVariable(runtime, "$@")); IRubyObject stdin = new RubyIO(runtime, STDIO.IN); IRubyObject stdout = new RubyIO(runtime, STDIO.OUT); IRubyObject stderr = new RubyIO(runtime, STDIO.ERR); runtime.defineVariable(new InputGlobalVariable(runtime, "$stdin", stdin)); runtime.defineVariable(new OutputGlobalVariable(runtime, "$stdout", stdout)); runtime.getGlobalVariables().alias("$>", "$stdout"); runtime.getGlobalVariables().alias("$defout", "$stdout"); runtime.defineVariable(new OutputGlobalVariable(runtime, "$stderr", stderr)); runtime.getGlobalVariables().alias("$deferr", "$stderr"); runtime.defineGlobalConstant("STDIN", stdin); runtime.defineGlobalConstant("STDOUT", stdout); runtime.defineGlobalConstant("STDERR", stderr); runtime.defineVariable(new LoadedFeatures(runtime, "$\"")); runtime.defineVariable(new LoadedFeatures(runtime, "$LOADED_FEATURES")); runtime.defineVariable(new LoadPath(runtime, "$:")); runtime.defineVariable(new LoadPath(runtime, "$-I")); runtime.defineVariable(new LoadPath(runtime, "$LOAD_PATH")); runtime.defineVariable(new MatchMatchGlobalVariable(runtime, "$&")); runtime.defineVariable(new PreMatchGlobalVariable(runtime, "$`")); runtime.defineVariable(new PostMatchGlobalVariable(runtime, "$'")); runtime.defineVariable(new LastMatchGlobalVariable(runtime, "$+")); runtime.defineVariable(new BackRefGlobalVariable(runtime, "$~")); // On platforms without a c-library accessable through JNA, getpid will return hashCode // as $$ used to. Using $$ to kill processes could take down many runtimes, but by basing // $$ on getpid() where available, we have the same semantics as MRI. runtime.getGlobalVariables().defineReadonly("$$", new PidAccessor(runtime)); // after defn of $stderr as the call may produce warnings defineGlobalEnvConstants(runtime); // Fixme: Do we need the check or does Main.java not call this...they should consolidate if (runtime.getGlobalVariables().get("$*").isNil()) { runtime.getGlobalVariables().defineReadonly("$*", new ValueAccessor(runtime.newArray())); } runtime .getGlobalVariables() .defineReadonly( "$-p", new ValueAccessor(runtime.newBoolean(runtime.getInstanceConfig().isAssumePrinting()))); runtime .getGlobalVariables() .defineReadonly( "$-a", new ValueAccessor(runtime.newBoolean(runtime.getInstanceConfig().isSplit()))); runtime .getGlobalVariables() .defineReadonly( "$-l", new ValueAccessor(runtime.newBoolean(runtime.getInstanceConfig().isProcessLineEnds()))); // ARGF, $< object RubyArgsFile.initArgsFile(runtime); }
private static RubyTime createTime(IRubyObject recv, IRubyObject[] args, boolean gmt) { Ruby runtime = recv.getRuntime(); int len = ARG_SIZE; Boolean isDst = null; DateTimeZone dtz; if (gmt) { dtz = DateTimeZone.UTC; } else if (args.length == 10 && args[9] instanceof RubyString) { dtz = getTimeZone(runtime, ((RubyString) args[9]).toString()); } else { dtz = getLocalTimeZone(runtime); } if (args.length == 10) { if (args[8] instanceof RubyBoolean) { isDst = ((RubyBoolean) args[8]).isTrue(); } args = new IRubyObject[] { args[5], args[4], args[3], args[2], args[1], args[0], runtime.getNil() }; } else { // MRI accepts additional wday argument which appears to be ignored. len = args.length; if (len < ARG_SIZE) { IRubyObject[] newArgs = new IRubyObject[ARG_SIZE]; System.arraycopy(args, 0, newArgs, 0, args.length); for (int i = len; i < ARG_SIZE; i++) { newArgs[i] = runtime.getNil(); } args = newArgs; len = ARG_SIZE; } } if (args[0] instanceof RubyString) { args[0] = RubyNumeric.str2inum(runtime, (RubyString) args[0], 10, false); } int year = (int) RubyNumeric.num2long(args[0]); int month = 1; if (len > 1) { if (!args[1].isNil()) { IRubyObject tmp = args[1].checkStringType(); if (!tmp.isNil()) { String monthString = tmp.toString().toLowerCase(); Integer monthInt = MONTHS_MAP.get(monthString); if (monthInt != null) { month = monthInt; } else { try { month = Integer.parseInt(monthString); } catch (NumberFormatException nfExcptn) { throw runtime.newArgumentError("Argument out of range."); } } } else { month = (int) RubyNumeric.num2long(args[1]); } } if (1 > month || month > 12) { throw runtime.newArgumentError("Argument out of range: for month: " + month); } } int[] int_args = {1, 0, 0, 0, 0, 0}; for (int i = 0; int_args.length >= i + 2; i++) { if (!args[i + 2].isNil()) { if (!(args[i + 2] instanceof RubyNumeric)) { args[i + 2] = args[i + 2].callMethod(runtime.getCurrentContext(), "to_i"); } long value = RubyNumeric.num2long(args[i + 2]); if (time_min[i] > value || value > time_max[i]) { throw runtime.newArgumentError("argument out of range."); } int_args[i] = (int) value; } } if (!runtime.is1_9()) { if (0 <= year && year < 39) { year += 2000; } else if (69 <= year && year < 139) { year += 1900; } } DateTime dt; // set up with min values and then add to allow rolling over try { dt = new DateTime(year, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC); dt = dt.plusMonths(month - 1) .plusDays(int_args[0] - 1) .plusHours(int_args[1]) .plusMinutes(int_args[2]) .plusSeconds(int_args[3]); if (runtime.is1_9() && !args[5].isNil()) { double millis = RubyFloat.num2dbl(args[5]); int int_millis = (int) (millis * 1000) % 1000; dt = dt.plusMillis(int_millis); } dt = dt.withZoneRetainFields(dtz); // we might need to perform a DST correction if (isDst != null) { // the instant at which we will ask dtz what the difference between DST and // standard time is long offsetCalculationInstant = dt.getMillis(); // if we might be moving this time from !DST -> DST, the offset is assumed // to be the same as it was just before we last moved from DST -> !DST if (dtz.isStandardOffset(dt.getMillis())) { offsetCalculationInstant = dtz.previousTransition(offsetCalculationInstant); } int offset = dtz.getStandardOffset(offsetCalculationInstant) - dtz.getOffset(offsetCalculationInstant); if (!isDst && !dtz.isStandardOffset(dt.getMillis())) { dt = dt.minusMillis(offset); } if (isDst && dtz.isStandardOffset(dt.getMillis())) { dt = dt.plusMillis(offset); } } } catch (org.joda.time.IllegalFieldValueException e) { throw runtime.newArgumentError("time out of range"); } RubyTime time = new RubyTime(runtime, (RubyClass) recv, dt); // Ignores usec if 8 args (for compatibility with parsedate) or if not supplied. if (args.length != 8 && !args[6].isNil()) { int usec = int_args[4] % 1000; int msec = int_args[4] / 1000; if (int_args[4] < 0) { msec -= 1; usec += 1000; } time.dt = dt.withMillis(dt.getMillis() + msec); time.setUSec(usec); } time.callInit(IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); return time; }