From 122f676c9b1ab34ab42d207af224a223e238a097 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Tue, 13 Sep 2011 23:40:41 -0500 Subject: [PATCH] Fix a test failure and move native/cext enabled flags to per-runtime (per-config). --- src/org/jruby/Ruby.java | 2 +- src/org/jruby/RubyGlobal.java | 2 +- src/org/jruby/RubyInstanceConfig.java | 60 +++++++++++++++++++++ src/org/jruby/ext/ffi/FFIService.java | 3 +- src/org/jruby/ext/socket/SocketLibrary.java | 3 +- src/org/jruby/runtime/load/LoadService.java | 2 +- test/org/jruby/test/TestRuby.java | 41 +++++--------- 7 files changed, 77 insertions(+), 36 deletions(-) diff --git a/src/org/jruby/Ruby.java b/src/org/jruby/Ruby.java index bcd12fb5285..cd1d2a1c516 100644 --- a/src/org/jruby/Ruby.java +++ b/src/org/jruby/Ruby.java @@ -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( diff --git a/src/org/jruby/RubyGlobal.java b/src/org/jruby/RubyGlobal.java index aabe6c7b9a7..eba999aa47c 100644 --- a/src/org/jruby/RubyGlobal.java +++ b/src/org/jruby/RubyGlobal.java @@ -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); diff --git a/src/org/jruby/RubyInstanceConfig.java b/src/org/jruby/RubyInstanceConfig.java index 9baca792529..482b8e5ffe6 100644 --- a/src/org/jruby/RubyInstanceConfig.java +++ b/src/org/jruby/RubyInstanceConfig.java @@ -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); @@ -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; + } } diff --git a/src/org/jruby/ext/ffi/FFIService.java b/src/org/jruby/ext/ffi/FFIService.java index b2adaaf2dbc..97056a1fcdf 100644 --- a/src/org/jruby/ext/ffi/FFIService.java +++ b/src/org/jruby/ext/ffi/FFIService.java @@ -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()) { diff --git a/src/org/jruby/ext/socket/SocketLibrary.java b/src/org/jruby/ext/socket/SocketLibrary.java index 28d8b36625a..64f1450de0d 100644 --- a/src/org/jruby/ext/socket/SocketLibrary.java +++ b/src/org/jruby/ext/socket/SocketLibrary.java @@ -2,7 +2,6 @@ import java.io.IOException; import org.jruby.Ruby; -import org.jruby.RubyInstanceConfig; import org.jruby.runtime.load.Library; /** @@ -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); } diff --git a/src/org/jruby/runtime/load/LoadService.java b/src/org/jruby/runtime/load/LoadService.java index 15fa3d3aa56..c8d1eaae283 100644 --- a/src/org/jruby/runtime/load/LoadService.java +++ b/src/org/jruby/runtime/load/LoadService.java @@ -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() + "'"); diff --git a/test/org/jruby/test/TestRuby.java b/test/org/jruby/test/TestRuby.java index 943b25af977..f6b31f0e52f 100644 --- a/test/org/jruby/test/TestRuby.java +++ b/test/org/jruby/test/TestRuby.java @@ -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 {