Created
December 12, 2015 08:00
-
-
Save rovf/d446dc8565ea82b4a710 to your computer and use it in GitHub Desktop.
Examplecode for Geany issue #795 demonstrating the error in Geany 1.25 on Windows. The Japanese characters in line 79 are displayed as Null by Geany.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package vp; | |
import org.jruby.javasupport.JavaEmbedUtils; | |
import org.jruby.runtime.builtin.IRubyObject; | |
import org.jruby.runtime.ThreadContext; | |
import org.jruby.internal.runtime.ThreadService; | |
import org.jruby.Ruby; | |
import org.jruby.RubyObject; | |
import org.jruby.RubyString; | |
import org.jruby.embed.ScriptingContainer; | |
import org.jruby.embed.LocalContextScope; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.util.*; | |
import java.lang.*; | |
import hostenv.HE; | |
import static java.lang.System.out; | |
public class VP { | |
private ScriptingContainer container; | |
// We can safely cache the Runtime object, iff the container is "singleton" or | |
// "concurrent". | |
private Ruby runtime; | |
public VP() { | |
container = new ScriptingContainer(LocalContextScope.CONCURRENT); | |
runtime = container.getProvider().getRuntime(); | |
} | |
public void run() { | |
// Return a Ruby object and invoke some methods on it | |
RubyObject asahi = (RubyObject)container.runScriptlet(org.jruby.embed.PathType.RELATIVE,"rbsrc/bridge.rb"); | |
// RubyObject asahi = (RubyObject)container.runScriptlet(org.jruby.embed.PathType.CLASSPATH,"bridge.rb"); | |
out.println("scriptlet executed"); | |
// | |
// Invoking the methods using JavaEmbedUtils | |
// | |
try { | |
// | |
// The method say_something prints on stdout | |
// | |
JavaEmbedUtils.invokeMethod(runtime,asahi,"say_something",new Object[0],Class.forName("java.lang.Boolean")); | |
// | |
// The method dupstr returns twice the input string | |
// | |
Object[] str_arg = new Object[1]; | |
str_arg[0] = new String("Almrausch"); | |
String dup = (String)JavaEmbedUtils.invokeMethod(runtime,asahi,"dupstr",str_arg,Class.forName("java.lang.String")); | |
out.println("dupstr returns "+dup); | |
} catch(Exception e) { | |
out.println("Exception when using JavaEmbedUtils.invokeMethod"); | |
e.printStackTrace(); | |
} | |
// | |
// Invoking the same methods using IRubyObject.callMethod() | |
// | |
ThreadContext currentContext = asahi.getRuntime().getCurrentContext(); | |
// alternative way to get the thread context: | |
// ThreadContext currentContext = runtime.getThreadService().getCurrentContext(); | |
asahi.callMethod(currentContext,"say_something"); | |
String dup0 = asahi.callMethod(currentContext,"dupstr",JavaEmbedUtils.javaToRuby(runtime,"Dirndlrock")).convertToString().asJavaString(); | |
out.println("dupstr returns now "+dup0); | |
// | |
// Invoking the same methods using ScriptingContainer.callMethod() | |
// | |
container.callMethod(asahi,"say_something",Boolean.class); | |
Object dupstr_args[] = new Object[1]; | |
dupstr_args[0] = "Lederhosen"; | |
String dup1 = container.callMethod(asahi,"dupstr",dupstr_args,String.class).toString(); | |
out.println("dupstr returns finally "+dup1); | |
// Access the global variable created by bridge.rb. Also demonstrates | |
// handling of Unicode characters. | |
RubyObject kirin = (RubyObject)container.get("$kirin"); | |
String ucstr_result = container.callMethod(kirin,"ucstr").toString(); | |
try { | |
FileWriter fw = new FileWriter("ucout.txt"); | |
fw.write(ucstr_result); | |
fw.write("さん、今日は\n"); | |
fw.close(); | |
} catch(IOException e) { | |
out.println("Can not create or write file for storing Kirin output!"); | |
out.println(e.getMessage()); | |
} | |
// Fetching a Java String, which was created on the Ruby side | |
String jstring = (String)(container.callMethod(asahi,"aJavaString")); | |
out.println(jstring); | |
// HE is a Java class. We instantiate two HE objects, one from | |
// within Java, and one in Ruby | |
HE jHE = new HE(47); | |
HE rHE = (HE)(container.callMethod(asahi,"aHEobject",11)); | |
out.println(jHE.get_i().toString() + rHE.get_i().toString()); | |
out.println("container var map:"); | |
Map m = container.getVarMap(); | |
Set<String> keys = container.getVarMap().keySet(); | |
for (String key : keys) { | |
out.println(key + ", " + m.get(key)); | |
} | |
out.println("end run"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment