Skip to content

Commit

Permalink
refactor JRuby::CONFIG into being the RubyInstanceConfig object
Browse files Browse the repository at this point in the history
also introduced security_restricted helpers + adjusted jruby.rb for doc
  • Loading branch information
kares committed Aug 29, 2017
1 parent ed222fd commit bad6bd3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 29 deletions.
55 changes: 31 additions & 24 deletions core/src/main/java/org/jruby/ext/jruby/JRubyLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import org.jcodings.specific.ASCIIEncoding;
import org.jruby.Ruby;
import org.jruby.RubyBoolean;
import org.jruby.RubyClass;
import org.jruby.RubyModule;
import org.jruby.RubyString;
Expand Down Expand Up @@ -70,24 +71,30 @@ public void load(Ruby runtime, boolean wrap) {
// load Ruby parts of the 'jruby' library
runtime.getLoadService().loadFromClassLoader(runtime.getJRubyClassLoader(), "jruby/jruby.rb", false);

// define JRuby module
RubyModule jrubyModule = runtime.getOrCreateModule("JRuby");
RubyModule JRuby = runtime.getOrCreateModule("JRuby");

jrubyModule.defineAnnotatedMethods(JRubyLibrary.class);
jrubyModule.defineAnnotatedMethods(JRubyUtilLibrary.class);
JRuby.defineAnnotatedMethods(JRubyLibrary.class);
JRuby.defineAnnotatedMethods(JRubyUtilLibrary.class);

RubyClass threadLocalClass = jrubyModule.defineClassUnder("ThreadLocal", runtime.getObject(), JRubyThreadLocal.ALLOCATOR);
threadLocalClass.defineAnnotatedMethods(JRubyExecutionContextLocal.class);
JRuby.defineClassUnder("ThreadLocal", runtime.getObject(), JRubyThreadLocal.ALLOCATOR)
.defineAnnotatedMethods(JRubyExecutionContextLocal.class);

RubyClass fiberLocalClass = jrubyModule.defineClassUnder("FiberLocal", runtime.getObject(), JRubyFiberLocal.ALLOCATOR);
fiberLocalClass.defineAnnotatedMethods(JRubyExecutionContextLocal.class);
JRuby.defineClassUnder("FiberLocal", runtime.getObject(), JRubyFiberLocal.ALLOCATOR)
.defineAnnotatedMethods(JRubyExecutionContextLocal.class);

RubyModule config = jrubyModule.defineModuleUnder("CONFIG");
config.getSingletonClass().defineAnnotatedMethods(JRubyConfig.class);
/**
* JRuby::CONFIG ~ shortcut for JRuby.runtime.instance_config
* @since 9.2
*/
IRubyObject config = Java.getInstance(runtime, runtime.getInstanceConfig());
config.getMetaClass().defineAlias("rubygems_disabled?", "isDisableGems");
config.getMetaClass().defineAlias("did_you_mean_disabled?", "isDisableDidYouMean");
JRuby.setConstant("CONFIG", config);
}

@Deprecated // old JRuby.defineModuleUnder("CONFIG")
public static class JRubyConfig {
@JRubyMethod(name = "rubygems_disabled?")
// @JRubyMethod(name = "rubygems_disabled?")
public static IRubyObject rubygems_disabled_p(ThreadContext context, IRubyObject self) {
return context.runtime.newBoolean(context.runtime.getInstanceConfig().isDisableGems());
}
Expand Down Expand Up @@ -117,19 +124,6 @@ public static IRubyObject runtime(ThreadContext context, IRubyObject recv) {
return Java.wrapJavaObject(context.runtime, context.runtime); // context.nil.getRuntime()
}

/**
* JRuby.config a shortcut for JRuby.runtime.instance_config
* @param context
* @param recv
* @return a wrapped RubyInstanceConfig
* @since 9.2
*/
// TODO needs to get fine tuned considering there's a CONFIG constant already
// @JRubyMethod(module = true)
public static IRubyObject config(ThreadContext context, IRubyObject recv) {
return Java.wrapJavaObject(context.runtime, context.runtime.getInstanceConfig());
}

/**
* Unwrap the given Java-integration-wrapped object, returning the unwrapped
* object. If the wrapped object is not a Ruby object, an error will raise.
Expand Down Expand Up @@ -180,6 +174,19 @@ public static IRubyObject set_context_class_loader(ThreadContext context, IRubyO
return Java.wrapJavaObject(context.runtime, loader); // reference0
}

@JRubyMethod(name = "security_restricted?", module = true)
public static RubyBoolean is_security_restricted(IRubyObject recv) {
final Ruby runtime = recv.getRuntime();
return RubyBoolean.newBoolean(runtime, Ruby.isSecurityRestricted());
}

// NOTE: its probably too late to set this when jruby library is booted (due the java library) ?
@JRubyMethod(name = "security_restricted=", module = true)
public static IRubyObject set_security_restricted(IRubyObject recv, IRubyObject arg) {
Ruby.setSecurityRestricted(arg.isTrue());
return is_security_restricted(recv);
}

/**
* Provide the "identity" hash code that System.identityHashCode would produce.
*
Expand Down
13 changes: 8 additions & 5 deletions core/src/main/ruby/jruby/jruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ def dereference(obj); end if false
# @note implemented in *org.jruby.ext.jruby.JRubyLibrary*
def runtime; end if false

# Get the current runtime's config.
# Changes to the configuration won't be reflected in the runtime, meant to be read-only.
# Provide the "identity" hash for an object (that `System.identityHashCode` would produce).
# @note implemented in *org.jruby.ext.jruby.JRubyLibrary*
# def config; end if false
def identity_hash(obj); end if false

# Run the provided (required) block with the "global runtime" set to the current runtime,
# for libraries that expect to operate against the global runtime.
# @note implemented in *org.jruby.ext.jruby.JRubyLibrary*
# @note Mostly meant for dealing with legacy JRuby extensions.
def with_current_runtime_as_global; end if false

# Change the current threads context classloader.
Expand All @@ -35,10 +34,14 @@ def parse(content, filename = '', extra_position_info = false, lineno = 0); end
def compile_ir(content, filename = '', extra_position_info = false); end if false

# Parse and compile the given block or provided content.
# @return JRuby::CompiledScript instance
# @return [JRuby::CompiledScript]
# @note implemented in *org.jruby.ext.jruby.JRubyLibrary*
def compile(content, filename = '', extra_position_info = false); end if false

# The current runtime's config (a `org.jruby.RubyInstanceConfig` instance).
# Changes to the configuration won't be reflected in the runtime, meant to be read-only.
CONFIG = runtime.instance_config if false

end

# NOTE: This is not a public API and is subject to change at our whim.
Expand Down
5 changes: 5 additions & 0 deletions test/jruby/test_jruby_internals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ def teardown
Dir.rmdir @dir
end

def test_CONFIG; require 'jruby'
assert JRuby::CONFIG
assert_equal false, JRuby::CONFIG.rubygems_disabled?
end

def test_smart_split_paths
require 'jruby/path_helper'
assert_equal %w(foo bar blah),
Expand Down

0 comments on commit bad6bd3

Please sign in to comment.