Skip to content

Commit ec41098

Browse files
committed
[GR-9875] Define TruffleRuby.graalvm_home
PullRequest: truffleruby/2061
2 parents 55796eb + 609d8a8 commit ec41098

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

doc/user/truffleruby-additions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ standard `RUBY_ENGINE_VERSION` constant.
3333
TruffleRuby provides these non-standard methods and classes that provide
3434
additional functionality in the `TruffleRuby` module.
3535

36+
`TruffleRuby.graalvm_home` returns the GraalVM home or `nil` if running outside of GraalVM (e.g., TruffleRuby standalone).
37+
3638
`TruffleRuby.jit?` reports if the GraalVM Compiler is available and will be
3739
used.
3840

lib/truffle/rbconfig.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ module RbConfig
5959
libs = ''
6060

6161
prefix = ruby_home
62-
graalvm_home = Truffle::System.get_java_property 'org.graalvm.home'
62+
graalvm_home = TruffleRuby.graalvm_home
6363
extra_bindirs = if graalvm_home
6464
jre_bin = "#{graalvm_home}/jre/bin"
6565
["#{graalvm_home}/bin", *(jre_bin if File.directory?(jre_bin))]

spec/truffle/launcher_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def should_print_full_java_command(options, env: {})
328328
out.should include("--jvm")
329329
end
330330

331-
if Truffle::System.get_java_property 'org.graalvm.home'
331+
if TruffleRuby.graalvm_home
332332
# These options are only shown in GraalVM, as they are not available in a standalone distribution
333333
out.should include("--polyglot")
334334
end

spec/truffle/truffleruby/no_other_methods_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
it "contains no other public methods" do
1414
(TruffleRuby.methods - Module.new.methods).sort.should == %i{
15-
full_memory_barrier graal? jit? native? revision sulong? cexts? synchronized
15+
full_memory_barrier graal? graalvm_home jit? native? revision sulong? cexts? synchronized
1616
}.sort
1717
end
1818

src/main/java/org/truffleruby/extra/TruffleRubyNodes.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@
99
*/
1010
package org.truffleruby.extra;
1111

12+
import com.oracle.truffle.api.dsl.Cached;
13+
import org.jcodings.specific.UTF8Encoding;
1214
import org.truffleruby.RubyContext;
1315
import org.truffleruby.RubyLanguage;
1416
import org.truffleruby.builtins.CoreMethod;
1517
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
1618
import org.truffleruby.builtins.CoreMethodNode;
1719
import org.truffleruby.builtins.CoreModule;
20+
import org.truffleruby.core.rope.CodeRange;
21+
import org.truffleruby.core.string.StringNodes;
1822
import org.truffleruby.extra.ffi.Pointer;
1923

2024
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -25,6 +29,27 @@
2529
@CoreModule("TruffleRuby")
2630
public abstract class TruffleRubyNodes {
2731

32+
@CoreMethod(names = "graalvm_home", onSingleton = true)
33+
public abstract static class GraalvmHomeNode extends CoreMethodArrayArgumentsNode {
34+
35+
@Specialization
36+
protected Object graalvmHome(
37+
@Cached StringNodes.MakeStringNode makeStringNode) {
38+
String value = getProperty("org.graalvm.home");
39+
if (value == null) {
40+
return nil;
41+
} else {
42+
return makeStringNode.executeMake(value, UTF8Encoding.INSTANCE, CodeRange.CR_UNKNOWN);
43+
}
44+
}
45+
46+
@TruffleBoundary
47+
private static String getProperty(String key) {
48+
return System.getProperty(key);
49+
}
50+
51+
}
52+
2853
@CoreMethod(names = "jit?", onSingleton = true)
2954
public abstract static class GraalNode extends CoreMethodArrayArgumentsNode {
3055

0 commit comments

Comments
 (0)