Skip to content

Commit

Permalink
Fix a test failure and move native/cext enabled flags to per-runtime …
Browse files Browse the repository at this point in the history
…(per-config).
  • Loading branch information
headius committed Sep 14, 2011
1 parent 27ba592 commit 122f676
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/org/jruby/Ruby.java
Expand Up @@ -1072,7 +1072,7 @@ private void init() {

// Construct key services
loadService = config.createLoadService(this);
posix = POSIXFactory.getPOSIX(new JRubyPOSIXHandler(this), RubyInstanceConfig.NATIVE_ENABLED);
posix = POSIXFactory.getPOSIX(new JRubyPOSIXHandler(this), config.isNativeEnabled());
javaSupport = new JavaSupport(this);

executor = new ThreadPoolExecutor(
Expand Down
2 changes: 1 addition & 1 deletion src/org/jruby/RubyGlobal.java
Expand Up @@ -386,7 +386,7 @@ private static void defineGlobalEnvConstants(Ruby runtime) {
CaseInsensitiveStringOnlyRubyHash env = new CaseInsensitiveStringOnlyRubyHash(runtime,
environmentVariableMap,
runtime.getNil(),
RubyInstanceConfig.NATIVE_ENABLED &&
runtime.getInstanceConfig().isNativeEnabled() &&
runtime.getInstanceConfig().isUpdateNativeENVEnabled() );
env.getSingletonClass().defineAnnotatedMethods(CaseInsensitiveStringOnlyRubyHash.class);
runtime.defineGlobalConstant("ENV", env);
Expand Down
60 changes: 60 additions & 0 deletions src/org/jruby/RubyInstanceConfig.java
Expand Up @@ -300,11 +300,34 @@ public enum ProfilingMode {
public static final String COMPILE_EXCLUDE
= SafePropertyAccessor.getProperty("jruby.jit.exclude");

/**
* Indicates the global default for whether native code is enabled. Default
* is true or the value of the property "jruby.native.enabled", if set.
*
* This value is used to default new runtime configurations.
*/
public static final boolean NATIVE_ENABLED = SafePropertyAccessor.getBoolean("jruby.native.enabled", true);
@Deprecated
public static final boolean nativeEnabled = NATIVE_ENABLED;

/**
* Indicates the global default for whether C extensions are enabled. Default
* is the value of RubyInstanceConfig.NATIVE_ENABLED or the value of the
* property "jruby.cext.enabled", if set.
*
* This value is used to default new runtime configurations.
*/
public final static boolean CEXT_ENABLED = SafePropertyAccessor.getBoolean("jruby.cext.enabled", NATIVE_ENABLED);

/**
* Whether native code is enabled for this configuration.
*/
private boolean _nativeEnabled = NATIVE_ENABLED;

/**
* Whether C extensions are enabled for this configuration.
*/
private boolean _cextEnabled = CEXT_ENABLED;

public static final boolean REIFY_RUBY_CLASSES
= SafePropertyAccessor.getBoolean("jruby.reify.classes", false);
Expand Down Expand Up @@ -1875,4 +1898,41 @@ public static boolean hasLoadedNativeExtensions() {
public static void setLoadedNativeExtensions(boolean loadedNativeExtensions) {
RubyInstanceConfig.loadedNativeExtensions = loadedNativeExtensions;
}

/**
* Set whether native code is enabled for this config. Disabling it also
* disables C extensions (@see RubyInstanceConfig#setCextEnabled).
*
* @param b new value indicating whether native code is enabled
*/
public void setNativeEnabled(boolean b) {
_nativeEnabled = false;
}

/**
* Get whether native code is enabled for this config.
*
* @return true if native code is enabled; false otherwise.
*/
public boolean isNativeEnabled() {
return _nativeEnabled;
}

/**
* Set whether C extensions are enabled for this config.
*
* @param b new value indicating whether native code is enabled
*/
public void setCextEnabled(boolean b) {
_cextEnabled = b;
}

/**
* Get whether C extensions are enabled for this config.
*
* @return true if C extensions are enabled; false otherwise.
*/
public boolean isCextEnabled() {
return _cextEnabled;
}
}
3 changes: 1 addition & 2 deletions src/org/jruby/ext/ffi/FFIService.java
Expand Up @@ -30,13 +30,12 @@

import java.io.IOException;
import org.jruby.Ruby;
import org.jruby.RubyInstanceConfig;
import org.jruby.RubyModule;
import org.jruby.runtime.load.Library;

public class FFIService implements Library {
public void load(final Ruby runtime, boolean wrap) throws IOException {
if (!RubyInstanceConfig.NATIVE_ENABLED) {
if (!runtime.getInstanceConfig().isNativeEnabled()) {
throw runtime.newLoadError("Native API access is disabled");
}
if (!Platform.getPlatform().isSupported()) {
Expand Down
3 changes: 1 addition & 2 deletions src/org/jruby/ext/socket/SocketLibrary.java
Expand Up @@ -2,7 +2,6 @@

import java.io.IOException;
import org.jruby.Ruby;
import org.jruby.RubyInstanceConfig;
import org.jruby.runtime.load.Library;

/**
Expand All @@ -15,7 +14,7 @@ public void load(final Ruby runtime, boolean wrap) throws IOException {
RubyBasicSocket.createBasicSocket(runtime);
RubySocket.createSocket(runtime);

if (RubyInstanceConfig.NATIVE_ENABLED && RubyUNIXSocket.tryUnixDomainSocket()) {
if (runtime.getInstanceConfig().isNativeEnabled() && RubyUNIXSocket.tryUnixDomainSocket()) {
RubyUNIXSocket.createUNIXSocket(runtime);
RubyUNIXServer.createUNIXServer(runtime);
}
Expand Down
2 changes: 1 addition & 1 deletion src/org/jruby/runtime/load/LoadService.java
Expand Up @@ -890,7 +890,7 @@ protected Library createLibrary(SearchState state, LoadServiceResource resource)
}
String file = state.loadName;
if (file.endsWith(".so") || file.endsWith(".dll") || file.endsWith(".bundle")) {
if (RubyInstanceConfig.CEXT_ENABLED) {
if (runtime.getInstanceConfig().isCextEnabled()) {
return new CExtension(resource);
} else {
throw runtime.newLoadError("C extensions are disabled, can't load `" + resource.getName() + "'");
Expand Down
41 changes: 12 additions & 29 deletions test/org/jruby/test/TestRuby.java
Expand Up @@ -104,38 +104,21 @@ public void testNativeENVSettingWhenItIsDisabledOnTheRuntime() throws Exception
}

public void testNativeENVSettingWhenNativeIsDisabledGlobally() throws Exception {
try {
setNativeEnabled(false);
runtime = Ruby.newInstance();
runtime.evalScriptlet("ENV['gravy'] = 'with sausage'");
assertNull(runtime.getPosix().getenv("gravy"));
} finally {
setNativeEnabled(true);
}
RubyInstanceConfig cfg = new RubyInstanceConfig();
cfg.setNativeEnabled(false);
runtime = Ruby.newInstance(cfg);
runtime.evalScriptlet("ENV['gravy'] = 'with sausage'");
assertNull(runtime.getPosix().getenv("gravy"));
System.out.println(runtime.getPosix().getenv("gravy"));
}

public void testNativeENVSettingWhenNativeIsDisabledGloballyButExplicitlyEnabledOnTheRuntime() throws Exception {
try {
setNativeEnabled(false);
RubyInstanceConfig cfg = new RubyInstanceConfig();
cfg.setUpdateNativeENVEnabled(true);
runtime = Ruby.newInstance(cfg);
runtime.evalScriptlet("ENV['sausage'] = 'biscuits'");
assertNull(runtime.getPosix().getenv("sausage"));
} finally {
setNativeEnabled(true);
}
}

private void setNativeEnabled(boolean nativeEnabled) throws Exception {
Field nativeEnabledField = RubyInstanceConfig.class.getDeclaredField("nativeEnabled");
nativeEnabledField.setAccessible(true);

Field modifiers = Field.class.getDeclaredField("modifiers");
modifiers.setAccessible(true);
modifiers.setInt(nativeEnabledField, nativeEnabledField.getModifiers() & ~Modifier.FINAL);

nativeEnabledField.set(RubyInstanceConfig.class, nativeEnabled);
RubyInstanceConfig cfg = new RubyInstanceConfig();
cfg.setNativeEnabled(false);
cfg.setUpdateNativeENVEnabled(true);
runtime = Ruby.newInstance(cfg);
runtime.evalScriptlet("ENV['sausage'] = 'biscuits'");
assertNull(runtime.getPosix().getenv("sausage"));
}

public void testPrintErrorWithNilBacktrace() throws Exception {
Expand Down

0 comments on commit 122f676

Please sign in to comment.