-
Notifications
You must be signed in to change notification settings - Fork 915
/
JRubyTruffleImpl.java
71 lines (58 loc) · 2.5 KB
/
JRubyTruffleImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Copyright (c) 2014, 2016 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle;
import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.api.vm.PolyglotEngine;
import org.jruby.JRubyTruffleInterface;
import org.jruby.Ruby;
import org.jruby.truffle.interop.JRubyContextWrapper;
import org.jruby.truffle.language.control.ExitException;
import org.jruby.truffle.platform.graal.Graal;
import org.jruby.util.cli.Options;
import java.io.IOException;
public class JRubyTruffleImpl implements JRubyTruffleInterface {
private final PolyglotEngine engine;
private final RubyContext context;
// Created by reflection from Ruby#loadTruffle
public JRubyTruffleImpl(Ruby runtime) {
engine = PolyglotEngine.newBuilder()
.globalSymbol(JRubyTruffleInterface.RUNTIME_SYMBOL, new JRubyContextWrapper(runtime))
.build();
try {
context = (RubyContext) engine.eval(Source.fromText("Truffle::Boot.context", "context")
.withMimeType(RubyLanguage.MIME_TYPE)).get();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public Object execute(org.jruby.ast.RootNode rootNode) {
if (!Graal.isGraal() && Options.TRUFFLE_GRAAL_WARNING_UNLESS.load()) {
System.err.println("WARNING: This JVM does not have the Graal compiler. " +
"JRuby+Truffle's performance without it will be limited. " +
"See https://github.com/jruby/jruby/wiki/Truffle-FAQ#how-do-i-get-jrubytruffle");
}
context.getJRubyInterop().setOriginalInputFile(rootNode.getPosition().getFile());
try {
return engine.eval(Source.fromText("Truffle::Boot.run_jruby_root", "run_jruby_root")
.withMimeType(RubyLanguage.MIME_TYPE)).get();
} catch (IOException e) {
if (e.getCause() instanceof ExitException) {
final ExitException exit = (ExitException) e.getCause();
throw new org.jruby.exceptions.MainExitException(exit.getCode());
}
throw new RuntimeException(e);
}
}
@Override
public void dispose() {
engine.dispose();
}
}