@SuppressWarnings("unchecked") @JRubyMethod(name = "names") public IRubyObject names(ThreadContext context) { Ruby runtime = context.runtime; EncodingService service = runtime.getEncodingService(); Entry entry = service.findEncodingOrAliasEntry(name); RubyArray result = runtime.newArray(); HashEntryIterator i; i = service.getEncodings().entryIterator(); while (i.hasNext()) { CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry> e = ((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry>) i.next()); if (e.value == entry) { result.append( RubyString.newUsAsciiStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context)); } } i = service.getAliases().entryIterator(); while (i.hasNext()) { CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry> e = ((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry>) i.next()); if (e.value == entry) { result.append( RubyString.newUsAsciiStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context)); } } result.append(runtime.newString(EXTERNAL)); result.append(runtime.newString(LOCALE)); return result; }
@SuppressWarnings("unchecked") @JRubyMethod(name = "aliases", meta = true) public static IRubyObject aliases(ThreadContext context, IRubyObject recv) { Ruby runtime = context.runtime; EncodingService service = runtime.getEncodingService(); IRubyObject list[] = service.getEncodingList(); HashEntryIterator i = service.getAliases().entryIterator(); RubyHash result = RubyHash.newHash(runtime); while (i.hasNext()) { CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry> e = ((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry>) i.next()); IRubyObject alias = RubyString.newUsAsciiStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context); IRubyObject name = RubyString.newUsAsciiStringShared(runtime, ((RubyEncoding) list[e.value.getIndex()]).name) .freeze(context); result.fastASet(alias, name); } result.fastASet( runtime.newString(EXTERNAL), runtime.newString(new ByteList(runtime.getDefaultExternalEncoding().getName()))); result.fastASet( runtime.newString(LOCALE), runtime.newString(new ByteList(service.getLocaleEncoding().getName()))); return result; }
@SuppressWarnings("unchecked") @JRubyMethod(name = "name_list", meta = true) public static IRubyObject name_list(ThreadContext context, IRubyObject recv) { Ruby runtime = context.runtime; EncodingService service = runtime.getEncodingService(); RubyArray result = runtime.newArray(service.getEncodings().size() + service.getAliases().size()); HashEntryIterator i; i = service.getEncodings().entryIterator(); while (i.hasNext()) { CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry> e = ((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry>) i.next()); result.append( RubyString.newUsAsciiStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context)); } i = service.getAliases().entryIterator(); while (i.hasNext()) { CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry> e = ((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry>) i.next()); result.append( RubyString.newUsAsciiStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context)); } result.append(runtime.newString(EXTERNAL)); result.append(runtime.newString(LOCALE)); return result; }
@JRubyMethod public IRubyObject source_location(ThreadContext context) { Ruby runtime = context.runtime; if (file != null) return runtime.newArray(runtime.newString(file), runtime.newFixnum(line + 1 /*zero-based*/)); if (block != null) { Binding binding = block.getBinding(); return runtime.newArray( runtime.newString(binding.getFile()), runtime.newFixnum(binding.getLine() + 1 /*zero-based*/)); } return runtime.getNil(); }
@JRubyMethod(name = "source_location", compat = CompatVersion.RUBY1_9) public IRubyObject source_location(ThreadContext context) { Ruby runtime = context.getRuntime(); if (sourcePosition != null) { return runtime.newArray( runtime.newString(sourcePosition.getFile()), runtime.newFixnum(sourcePosition.getLine() + 1 /*zero-based*/)); } else if (block != null) { Binding binding = block.getBinding(); return runtime.newArray( runtime.newString(binding.getFile()), runtime.newFixnum(binding.getLine() + 1 /*zero-based*/)); } return runtime.getNil(); }
/** flo_to_s */ @JRubyMethod(name = "to_s") @Override public IRubyObject to_s() { Ruby runtime = getRuntime(); if (Double.isInfinite(value)) return RubyString.newString(runtime, value < 0 ? "-Infinity" : "Infinity"); if (Double.isNaN(value)) return RubyString.newString(runtime, "NaN"); ByteList buf = new ByteList(); // Under 1.9, use full-precision float formatting (JRUBY-4846). // Double-precision can represent around 16 decimal digits; // we use 20 to ensure full representation. Sprintf.sprintf(buf, Locale.US, "%#.20g", this); int e = buf.indexOf('e'); if (e == -1) e = buf.getRealSize(); ASCIIEncoding ascii = ASCIIEncoding.INSTANCE; if (!ascii.isDigit(buf.get(e - 1))) { buf.setRealSize(0); Sprintf.sprintf(buf, Locale.US, "%#.14e", this); e = buf.indexOf('e'); if (e == -1) e = buf.getRealSize(); } int p = e; while (buf.get(p - 1) == '0' && ascii.isDigit(buf.get(p - 2))) p--; System.arraycopy(buf.getUnsafeBytes(), e, buf.getUnsafeBytes(), p, buf.getRealSize() - e); buf.setRealSize(p + buf.getRealSize() - e); buf.setEncoding(USASCIIEncoding.INSTANCE); return runtime.newString(buf); }
@JRubyMethod(name = "open", required = 1, frame = true, meta = true) public static IRubyObject open( final ThreadContext context, IRubyObject recv, IRubyObject filename, Block block) throws IOException { Ruby runtime = recv.getRuntime(); IRubyObject io = RuntimeHelpers.invoke( context, runtime.getFile(), "open", filename, runtime.newString("rb")); RubyGzipReader gzio = newInstance(recv, new IRubyObject[] {io}, block); return RubyGzipFile.wrapBlock(context, gzio, block); }
@JRubyMethod(name = "zone") public RubyString zone() { Ruby runtime = getRuntime(); String envTZ = getEnvTimeZone(runtime).toString(); // see declaration of SHORT_TZNAME if (SHORT_STD_TZNAME.containsKey(envTZ) && !dt.getZone().toTimeZone().inDaylightTime(dt.toDate())) { return runtime.newString(SHORT_STD_TZNAME.get(envTZ)); } if (SHORT_DL_TZNAME.containsKey(envTZ) && dt.getZone().toTimeZone().inDaylightTime(dt.toDate())) { return runtime.newString(SHORT_DL_TZNAME.get(envTZ)); } String zone = dt.getZone().getShortName(dt.getMillis()); Matcher offsetMatcher = TIME_OFFSET_PATTERN.matcher(zone); if (offsetMatcher.matches()) { boolean minus_p = offsetMatcher.group(1).toString().equals("-"); int hourOffset = Integer.valueOf(offsetMatcher.group(2)); if (zone.equals("+00:00")) { zone = "GMT"; } else { // try non-localized time zone name zone = dt.getZone().getNameKey(dt.getMillis()); if (zone == null) { char sign = minus_p ? '+' : '-'; zone = "GMT" + sign + hourOffset; } } } return runtime.newString(zone); }
@JRubyMethod(name = "print", rest = true) public IRubyObject print(ThreadContext context, IRubyObject[] args) { Ruby runtime = context.getRuntime(); if (args.length != 0) { for (int i = 0, j = args.length; i < j; i++) { append(context, args[i]); } } else { IRubyObject arg = runtime.getGlobalVariables().get("$_"); append(context, arg.isNil() ? runtime.newString("nil") : arg); } IRubyObject sep = runtime.getGlobalVariables().get("$\\"); if (!sep.isNil()) { append(context, sep); } return getRuntime().getNil(); }
@JRubyMethod(name = "open", required = 1, optional = 2, frame = true, meta = true) public static IRubyObject open( final ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) throws IOException { Ruby runtime = recv.getRuntime(); IRubyObject level = runtime.getNil(); IRubyObject strategy = runtime.getNil(); if (args.length > 1) { level = args[1]; if (args.length > 2) strategy = args[2]; } IRubyObject io = RuntimeHelpers.invoke( context, runtime.getFile(), "open", args[0], runtime.newString("wb")); RubyGzipWriter gzio = newGzipWriter(recv, new IRubyObject[] {io, level, strategy}, block); return RubyGzipFile.wrapBlock(context, gzio, block); }
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; }
@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); }
/** 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; }
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; }
public static void createGlobals(ThreadContext context, Ruby runtime) { GlobalVariables globals = runtime.getGlobalVariables(); runtime.defineGlobalConstant("TOPLEVEL_BINDING", runtime.newBinding()); runtime.defineGlobalConstant("TRUE", runtime.getTrue()); runtime.defineGlobalConstant("FALSE", runtime.getFalse()); runtime.defineGlobalConstant("NIL", runtime.getNil()); initARGV(runtime); IAccessor d = new ValueAccessor(runtime.newString(runtime.getInstanceConfig().displayedFileName())); globals.define("$PROGRAM_NAME", d, GLOBAL); globals.define("$0", d, GLOBAL); // 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); version = runtime.newString(Constants.RUBY_VERSION).freeze(context); patchlevel = runtime.newFixnum(Constants.RUBY_PATCHLEVEL); runtime.defineGlobalConstant("RUBY_VERSION", version); runtime.defineGlobalConstant("RUBY_PATCHLEVEL", patchlevel); runtime.defineGlobalConstant("RUBY_RELEASE_DATE", release); runtime.defineGlobalConstant("RUBY_PLATFORM", platform); IRubyObject description = runtime.newString(OutputStrings.getVersionString()).freeze(context); runtime.defineGlobalConstant("RUBY_DESCRIPTION", description); IRubyObject copyright = runtime.newString(OutputStrings.getCopyrightString()).freeze(context); runtime.defineGlobalConstant("RUBY_COPYRIGHT", copyright); 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); // needs to be a fixnum, but our revision is a sha1 hash from git runtime.defineGlobalConstant("RUBY_REVISION", runtime.newFixnum(Constants.RUBY_REVISION)); runtime.defineGlobalConstant("RUBY_ENGINE", engine); runtime.defineGlobalConstant("RUBY_ENGINE_VERSION", jrubyVersion); RubyInstanceConfig.Verbosity verbosity = runtime.getInstanceConfig().getVerbosity(); runtime.defineVariable(new WarningGlobalVariable(runtime, "$-W", verbosity), GLOBAL); final GlobalVariable kcodeGV; kcodeGV = new NonEffectiveGlobalVariable(runtime, "$KCODE", runtime.getNil()); runtime.defineVariable(kcodeGV, GLOBAL); runtime.defineVariable(new GlobalVariable.Copy(runtime, "$-K", kcodeGV), GLOBAL); IRubyObject defaultRS = runtime.newString(runtime.getInstanceConfig().getRecordSeparator()).freeze(context); GlobalVariable rs = new StringGlobalVariable(runtime, "$/", defaultRS); runtime.defineVariable(rs, GLOBAL); runtime.setRecordSeparatorVar(rs); globals.setDefaultSeparator(defaultRS); runtime.defineVariable(new StringGlobalVariable(runtime, "$\\", runtime.getNil()), GLOBAL); runtime.defineVariable(new StringGlobalVariable(runtime, "$,", runtime.getNil()), GLOBAL); runtime.defineVariable(new LineNumberGlobalVariable(runtime, "$."), GLOBAL); runtime.defineVariable(new LastlineGlobalVariable(runtime, "$_"), FRAME); runtime.defineVariable(new LastExitStatusVariable(runtime, "$?"), THREAD); runtime.defineVariable(new ErrorInfoGlobalVariable(runtime, "$!", runtime.getNil()), THREAD); runtime.defineVariable( new NonEffectiveGlobalVariable(runtime, "$=", runtime.getFalse()), GLOBAL); if (runtime.getInstanceConfig().getInputFieldSeparator() == null) { runtime.defineVariable(new GlobalVariable(runtime, "$;", runtime.getNil()), GLOBAL); } else { runtime.defineVariable( new GlobalVariable( runtime, "$;", RubyRegexp.newRegexp( runtime, runtime.getInstanceConfig().getInputFieldSeparator(), new RegexpOptions())), GLOBAL); } RubyInstanceConfig.Verbosity verbose = runtime.getInstanceConfig().getVerbosity(); IRubyObject verboseValue = null; if (verbose == RubyInstanceConfig.Verbosity.NIL) { verboseValue = runtime.getNil(); } else if (verbose == RubyInstanceConfig.Verbosity.TRUE) { verboseValue = runtime.getTrue(); } else { verboseValue = runtime.getFalse(); } runtime.defineVariable(new VerboseGlobalVariable(runtime, "$VERBOSE", verboseValue), GLOBAL); runtime.defineVariable(new VerboseGlobalVariable(runtime, "$-v", verboseValue), GLOBAL); runtime.defineVariable(new VerboseGlobalVariable(runtime, "$-w", verboseValue), GLOBAL); IRubyObject debug = runtime.newBoolean(runtime.getInstanceConfig().isDebug()); runtime.defineVariable(new DebugGlobalVariable(runtime, "$DEBUG", debug), GLOBAL); runtime.defineVariable(new DebugGlobalVariable(runtime, "$-d", debug), GLOBAL); runtime.defineVariable(new SafeGlobalVariable(runtime, "$SAFE"), THREAD); runtime.defineVariable(new BacktraceGlobalVariable(runtime, "$@"), THREAD); IRubyObject stdin = RubyIO.prepStdio( runtime, runtime.getIn(), prepareStdioChannel(runtime, STDIO.IN, runtime.getIn()), OpenFile.READABLE, runtime.getIO(), "<STDIN>"); IRubyObject stdout = RubyIO.prepStdio( runtime, runtime.getOut(), prepareStdioChannel(runtime, STDIO.OUT, runtime.getOut()), OpenFile.WRITABLE, runtime.getIO(), "<STDOUT>"); IRubyObject stderr = RubyIO.prepStdio( runtime, runtime.getErr(), prepareStdioChannel(runtime, STDIO.ERR, runtime.getErr()), OpenFile.WRITABLE | OpenFile.SYNC, runtime.getIO(), "<STDERR>"); runtime.defineVariable(new InputGlobalVariable(runtime, "$stdin", stdin), GLOBAL); runtime.defineVariable(new OutputGlobalVariable(runtime, "$stdout", stdout), GLOBAL); globals.alias("$>", "$stdout"); runtime.defineVariable(new OutputGlobalVariable(runtime, "$stderr", stderr), GLOBAL); runtime.defineGlobalConstant("STDIN", stdin); runtime.defineGlobalConstant("STDOUT", stdout); runtime.defineGlobalConstant("STDERR", stderr); runtime.defineVariable(new LoadedFeatures(runtime, "$\""), GLOBAL); runtime.defineVariable(new LoadedFeatures(runtime, "$LOADED_FEATURES"), GLOBAL); runtime.defineVariable(new LoadPath(runtime, "$:"), GLOBAL); runtime.defineVariable(new LoadPath(runtime, "$-I"), GLOBAL); runtime.defineVariable(new LoadPath(runtime, "$LOAD_PATH"), GLOBAL); runtime.defineVariable(new MatchMatchGlobalVariable(runtime, "$&"), FRAME); runtime.defineVariable(new PreMatchGlobalVariable(runtime, "$`"), FRAME); runtime.defineVariable(new PostMatchGlobalVariable(runtime, "$'"), FRAME); runtime.defineVariable(new LastMatchGlobalVariable(runtime, "$+"), FRAME); runtime.defineVariable(new BackRefGlobalVariable(runtime, "$~"), FRAME); // 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. globals.defineReadonly("$$", new PidAccessor(runtime), GLOBAL); // 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 (globals.get("$*").isNil()) { globals.defineReadonly("$*", new ValueAccessor(runtime.newArray()), GLOBAL); } globals.defineReadonly( "$-p", new ValueAccessor(runtime.newBoolean(runtime.getInstanceConfig().isAssumePrinting())), GLOBAL); globals.defineReadonly( "$-a", new ValueAccessor(runtime.newBoolean(runtime.getInstanceConfig().isSplit())), GLOBAL); globals.defineReadonly( "$-l", new ValueAccessor(runtime.newBoolean(runtime.getInstanceConfig().isProcessLineEnds())), GLOBAL); // ARGF, $< object RubyArgsFile.initArgsFile(runtime); globals.alias("$-0", "$/"); // Define aliases originally in the "English.rb" stdlib globals.alias("$ERROR_INFO", "$!"); globals.alias("$ERROR_POSITION", "$@"); globals.alias("$FS", "$;"); globals.alias("$FIELD_SEPARATOR", "$;"); globals.alias("$OFS", "$,"); globals.alias("$OUTPUT_FIELD_SEPARATOR", "$,"); globals.alias("$RS", "$/"); globals.alias("$INPUT_RECORD_SEPARATOR", "$/"); globals.alias("$ORS", "$\\"); globals.alias("$OUTPUT_RECORD_SEPARATOR", "$\\"); globals.alias("$NR", "$."); globals.alias("$INPUT_LINE_NUMBER", "$."); globals.alias("$LAST_READ_LINE", "$_"); globals.alias("$DEFAULT_OUTPUT", "$>"); globals.alias("$DEFAULT_INPUT", "$<"); globals.alias("$PID", "$$"); globals.alias("$PROCESS_ID", "$$"); globals.alias("$CHILD_STATUS", "$?"); globals.alias("$LAST_MATCH_INFO", "$~"); globals.alias("$IGNORECASE", "$="); globals.alias("$ARGV", "$*"); globals.alias("$MATCH", "$&"); globals.alias("$PREMATCH", "$`"); globals.alias("$POSTMATCH", "$'"); globals.alias("$LAST_PAREN_MATCH", "$+"); }