RingJavafor Ring

How it works

RingJava architecture: your Ring program, javalib (Ring), ring_java (C), JNI, ringjava.jar (Java), the Java class library
The layers of RingJava

The C extension (extensions/ringjava/ring_java.c) loads the JVM library of the JDK at run time (LoadLibrary / dlopen of jvm.dll / libjvm.so, so one build works with any JDK 17+), starts it with JNI_CreateJavaVM (options: the bridge jar on the class path, -Xrs, UTF-8 text), binds ringjava.Bridge.invoke(byte[]) and registers the native method Bridge.ringCallback(byte[]) that Java uses to run Ring code. A call is one request (a byte array) and one answer. The functions: java_do(op,list) (the fast path: it packs the list, calls, and converts the answer to a Ring value), java_invoke(request) (a request that was packed in Ring), java_guard / java_unguard / java_flush (see below), java_pump and java_callbackrequest / java_callbackresult (callbacks).

The bridge (Bridge.java) keeps the handle table, finds the class (Class.forName through a class loader that jClasspath extends), chooses the method or constructor (all public overloads with the number of values, the cheapest conversion wins, varargs last), converts values both ways, makes java.lang.reflect.Proxy objects for callbacks, and turns exceptions into answers.

javalib (Ring) has the JavaObject class, the functions of the Core API and the generated classes.

Wire format#

A request is a sequence of items tag + decimal length + ":" + data: s text, n whole number, d double (8 bytes), z null, o object (handle), l list (items inside), c callback, x typed value (long:5). The first item is the operation (new, call, scall, get, set, sget, sset, tolist, newarray, box, release, classpath...). The answer is one item; e is an error (class|message|stack).

Handles and memory#

Every object that comes back gets a handle. The Ring object keeps a managed C pointer (the guard) with the number: when Ring frees the object, the handle goes in a queue, and the queue (64 handles) is sent to the bridge as one release request at the next call. The free function itself never calls Java (Ring can free objects at any moment).

Choosing the overload#

For every public method with the name (the exact spelling first, then ignoring case) and the number of values, each value gets a cost for the type of the parameter (an exact match is 0, a wider type or Object costs more, an enum by name, a Ring list for an array...). The cheapest total wins; a varargs method adds a penalty and is used only when no method matches the number of values. A public method that a hidden class declares (the ArrayList iterator) is found in the public super type that declares it.

Speed#

A call crosses the bridge twice (Ring to Java and back), and some of the work is Ring's own. What the package does to keep it small:

The cost of new X depends on the number of classes that are loaded and on the depth of the class: using only the packages that the program needs keeps it low. tools/javatools/benchmark.ring prints the time of each kind of call.

One object, several interfaces#

A Ring class has one parent, but a Java object can be several interfaces at once. For an ordinary class this is no problem: the generated Ring class has every public method of the class, those from its interfaces too. The case that needs more is an object whose class has no Ring class (a lambda, a proxy, a class that is not public) and that implements two or more unrelated interfaces. The bridge sends the names of its accessible interfaces and super classes (a type that another one includes is left out, and a marker interface such as Serializable is ignored), and Ring makes one combined class for that list: it derives from the first class and mergemethods() adds the methods of the others, as in RingDotNet. The class is made once for each list, and the methods of all the interfaces work on the object.

GUI mode#

With jGui() (java_gui(1)) a call of Ring is sent with Bridge.invokeGui: it runs on the event thread and the answer is a ticket. The C code then loops: it runs the callbacks that the event thread queued (jv_serve) and asks Bridge.await(ticket) until the answer is there. A callback of a listener without an answer (the proxy knows the return type is void) is queued without waiting (a request that starts with !), so the event thread is free for modal dialogs; a callback with an answer waits, and the Ring calls inside it run at once on the Ring thread.

RingJava - use Java classes and methods from Ring programs.