Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 411ca6a526a5
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 9a784f0f9a7e
Choose a head ref
  • 4 commits
  • 2 files changed
  • 1 contributor

Commits on Aug 6, 2015

  1. for better ENV compatibility we should not respond to to_h on <= 1.9.3

    (only available since Ruby 2.0)
    kares committed Aug 6, 2015
    Copy the full SHA
    6cf8d01 View commit details
  2. minor cleanup at RubyGlobal

    kares committed Aug 6, 2015
    Copy the full SHA
    41f0a88 View commit details
  3. Copy the full SHA
    2a12ce0 View commit details
  4. Copy the full SHA
    9a784f0 View commit details
Showing with 54 additions and 47 deletions.
  1. +52 −46 core/src/main/java/org/jruby/RubyGlobal.java
  2. +2 −1 core/src/main/java/org/jruby/RubyHash.java
98 changes: 52 additions & 46 deletions core/src/main/java/org/jruby/RubyGlobal.java
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@
* Copyright (C) 2006 Miguel Covarrubias <mlcovarrubias@gmail.com>
* Copyright (C) 2006 Michael Studman <codehaus@michaelstudman.com>
* Copyright (C) 2008 Joseph LaFata <joe@quibb.org>
*
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
@@ -36,6 +36,8 @@
***** END LICENSE BLOCK *****/
package org.jruby;

import java.util.Collections;
import java.util.Map;
import jnr.posix.POSIX;
import org.jcodings.Encoding;
import org.jruby.anno.JRubyMethod;
@@ -63,11 +65,8 @@

import static org.jruby.internal.runtime.GlobalVariable.Scope.*;

import java.util.HashMap;
import java.util.Map;

/** This class initializes global variables and constants.
*
*
* @author jpetersen
*/
public class RubyGlobal {
@@ -92,7 +91,7 @@ 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());
@@ -139,7 +138,7 @@ public static void createGlobals(ThreadContext context, Ruby runtime) {

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);
@@ -155,13 +154,13 @@ public static void createGlobals(ThreadContext context, Ruby runtime) {
// needs to be a fixnum, but our revision is a sha1 hash from git
runtime.defineGlobalConstant("RUBY_REVISION", runtime.newFixnum(Constants.RUBY1_9_REVISION));
}

if (runtime.is1_9()) {
RubyInstanceConfig.Verbosity verbosity = runtime.getInstanceConfig().getVerbosity();
runtime.defineVariable(new WarningGlobalVariable(runtime, "$-W", verbosity), GLOBAL);
}

final GlobalVariable kcodeGV;
final GlobalVariable kcodeGV;
if (runtime.is1_9()) {
kcodeGV = new NonEffectiveGlobalVariable(runtime, "$KCODE", runtime.getNil());
} else {
@@ -190,7 +189,7 @@ public static void createGlobals(ThreadContext context, Ruby runtime) {
} 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) {
@@ -203,7 +202,7 @@ public static void createGlobals(ThreadContext context, Ruby runtime) {
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);
@@ -235,26 +234,26 @@ public static void createGlobals(ThreadContext context, Ruby runtime) {
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
// 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

// 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);
@@ -297,21 +296,24 @@ public static void createGlobals(ThreadContext context, Ruby runtime) {
}

private static void defineGlobalEnvConstants(Ruby runtime) {
Map environmentVariableMap = null;
OSEnvironment environment = new OSEnvironment();
Map environmentVariableMap;
final OSEnvironment environment = new OSEnvironment();
environmentVariableMap = environment.getEnvironmentVariableMap(runtime);

if (environmentVariableMap == null) {
// if the environment variables can't be obtained, define an empty ENV
environmentVariableMap = new HashMap();
environmentVariableMap = Collections.EMPTY_MAP;
}

CaseInsensitiveStringOnlyRubyHash env = new CaseInsensitiveStringOnlyRubyHash(runtime,
environmentVariableMap,
environmentVariableMap,
runtime.getNil(),
runtime.getInstanceConfig().isNativeEnabled() &&
runtime.getInstanceConfig().isNativeEnabled() &&
runtime.getInstanceConfig().isUpdateNativeENVEnabled() );
env.getSingletonClass().defineAnnotatedMethods(CaseInsensitiveStringOnlyRubyHash.class);
if ( ! runtime.is2_0() ) { // MRI 1.9.3 does not have ENV#to_h
env.getSingletonClass().undefineMethod("to_h");
}
runtime.defineGlobalConstant("ENV", env);
runtime.setENV(env);

@@ -344,9 +346,13 @@ public IRubyObject op_aset(ThreadContext context, IRubyObject key, IRubyObject v
return case_aware_op_aset(context, key, value, false);
}

@JRubyMethod
@JRubyMethod(name = "to_s")
public IRubyObject to_s(ThreadContext context) {
return context.runtime.newString("ENV");
}

@Override
public IRubyObject to_s(){
public IRubyObject to_s() {
return getRuntime().newString("ENV");
}

@@ -368,7 +374,7 @@ public static class StringOnlyRubyHash extends RubyHash {
// class and not in the caseinsensitive hash. In order to not refactor
// both of these maps we will pass in a flag to specify whether we want
// the op_aset to also update the real ENV map via setenv/unsetenv.
private boolean updateRealENV;
private final boolean updateRealENV;

public StringOnlyRubyHash(Ruby runtime, Map valueMap, IRubyObject defaultValue, boolean updateRealENV) {
super(runtime, valueMap, defaultValue);
@@ -410,15 +416,16 @@ protected IRubyObject case_aware_op_aref(ThreadContext context, IRubyObject key,
return super.op_aref(context, key);
}

protected IRubyObject case_aware_op_aset(ThreadContext context, IRubyObject key, IRubyObject value, boolean caseSensitive) {
protected IRubyObject case_aware_op_aset(ThreadContext context,
IRubyObject key, final IRubyObject value, boolean caseSensitive) {
if (!key.respondsTo("to_str")) {
throw getRuntime().newTypeError("can't convert " + key.getMetaClass() + " into String");
}
if (!value.respondsTo("to_str") && !value.isNil()) {
throw getRuntime().newTypeError("can't convert " + value.getMetaClass() + " into String");
}

if (! caseSensitive) {
if (!caseSensitive) {
key = getCorrectKey(key, context);
}

@@ -427,14 +434,14 @@ protected IRubyObject case_aware_op_aset(ThreadContext context, IRubyObject key,
}

IRubyObject keyAsStr = normalizeEnvString(Helpers.invoke(context, key, "to_str"));
IRubyObject valueAsStr = value.isNil() ? getRuntime().getNil() :
IRubyObject valueAsStr = value.isNil() ? context.nil :
normalizeEnvString(Helpers.invoke(context, value, "to_str"));

if (updateRealENV) {
POSIX posix = getRuntime().getPosix();
POSIX posix = context.runtime.getPosix();
String keyAsJava = keyAsStr.asJavaString();
// libc (un)setenv is not reentrant, so we need to synchronize across the entire JVM (JRUBY-5933)
if (valueAsStr == getRuntime().getNil()) {
if (valueAsStr == context.nil) {
synchronized (Object.class) { posix.unsetenv(keyAsJava); }
} else {
synchronized (Object.class) { posix.setenv(keyAsJava, valueAsStr.asJavaString(), 1); }
@@ -470,9 +477,8 @@ private IRubyObject normalizeEnvString(IRubyObject str) {
RubyString newStr = getRuntime().newString(new ByteList(str.toString().getBytes(), enc));
newStr.setFrozen(true);
return newStr;
} else {
return str;
}
return str;
}
}

@@ -514,13 +520,13 @@ private static class LastExitStatusVariable extends GlobalVariable {
public LastExitStatusVariable(Ruby runtime, String name) {
super(runtime, name, runtime.getNil());
}

@Override
public IRubyObject get() {
IRubyObject lastExitStatus = runtime.getCurrentContext().getLastExitStatus();
return lastExitStatus == null ? runtime.getNil() : lastExitStatus;
}

@Override
public IRubyObject set(IRubyObject lastExitStatus) {
throw runtime.newNameError("$? is a read-only variable", "$?");
@@ -531,7 +537,7 @@ private static class MatchMatchGlobalVariable extends GlobalVariable {
public MatchMatchGlobalVariable(Ruby runtime, String name) {
super(runtime, name, runtime.getNil());
}

@Override
public IRubyObject get() {
return RubyRegexp.last_match(runtime.getCurrentContext().getBackRef());
@@ -542,7 +548,7 @@ private static class PreMatchGlobalVariable extends GlobalVariable {
public PreMatchGlobalVariable(Ruby runtime, String name) {
super(runtime, name, runtime.getNil());
}

@Override
public IRubyObject get() {
return RubyRegexp.match_pre(runtime.getCurrentContext().getBackRef());
@@ -553,7 +559,7 @@ private static class PostMatchGlobalVariable extends GlobalVariable {
public PostMatchGlobalVariable(Ruby runtime, String name) {
super(runtime, name, runtime.getNil());
}

@Override
public IRubyObject get() {
return RubyRegexp.match_post(runtime.getCurrentContext().getBackRef());
@@ -564,7 +570,7 @@ private static class LastMatchGlobalVariable extends GlobalVariable {
public LastMatchGlobalVariable(Ruby runtime, String name) {
super(runtime, name, runtime.getNil());
}

@Override
public IRubyObject get() {
return RubyRegexp.match_last(runtime.getCurrentContext().getBackRef());
@@ -575,7 +581,7 @@ private static class BackRefGlobalVariable extends GlobalVariable {
public BackRefGlobalVariable(Ruby runtime, String name) {
super(runtime, name, runtime.getNil());
}

@Override
public IRubyObject get() {
return Helpers.getBackref(runtime, runtime.getCurrentContext());
@@ -622,7 +628,7 @@ public IRubyObject set(IRubyObject value) {
!(JavaUtil.isJavaObject(value) && JavaUtil.unwrapJavaObject(value) instanceof Throwable)) {
throw runtime.newTypeError("assigning non-exception to $!");
}

return runtime.getCurrentContext().setErrorInfo(value);
}

@@ -686,7 +692,7 @@ public VerboseGlobalVariable(Ruby runtime, String name, IRubyObject initialValue
super(runtime, name, initialValue);
set(initialValue);
}

@Override
public IRubyObject get() {
return runtime.getVerbose();
@@ -703,7 +709,7 @@ public IRubyObject set(IRubyObject newValue) {
return newValue;
}
}

private static class WarningGlobalVariable extends ReadonlyGlobalVariable {
public WarningGlobalVariable(Ruby runtime, String name, RubyInstanceConfig.Verbosity verbosity) {
super(runtime, name,
@@ -791,7 +797,7 @@ public IRubyObject set(IRubyObject value) {
if (value == get()) {
return value;
}

return super.set(value);
}
}
@@ -808,7 +814,7 @@ public IRubyObject set(IRubyObject value) {
}
if (value instanceof RubyIO) {
RubyIO io = (RubyIO)value;

// HACK: in order to have stdout/err act like ttys and flush always,
// we set anything assigned to stdout/stderr to sync
try {
@@ -840,7 +846,7 @@ private static class LoadPath extends ReadonlyGlobalVariable {
public LoadPath(Ruby runtime, String name) {
super(runtime, name, null);
}

/**
* @see org.jruby.runtime.GlobalVariable#get()
*/
@@ -854,7 +860,7 @@ private static class LoadedFeatures extends ReadonlyGlobalVariable {
public LoadedFeatures(Ruby runtime, String name) {
super(runtime, name, null);
}

/**
* @see org.jruby.runtime.GlobalVariable#get()
*/
3 changes: 2 additions & 1 deletion core/src/main/java/org/jruby/RubyHash.java
Original file line number Diff line number Diff line change
@@ -948,7 +948,8 @@ public RubyHash to_hash() {

@JRubyMethod
public RubyHash to_h(ThreadContext context) {
return getType() == getRuntime().getHash() ? this : newHash(getRuntime()).replace(context, this);
final Ruby runtime = context.runtime;
return getType() == runtime.getHash() ? this : newHash(runtime).replace(context, this);
}

@Override