The SOM Interface Definition Language

From EDM2
Revision as of 00:15, 2 March 2018 by Ak120 (Talk | contribs)

Jump to: navigation, search

By Yuri Prokushev

In a previous article we briefly looked at SOM. Now we'll try to define our class interface.

Interface Definition Language (IDL) is core of System Object Model. All classes has definition of its interface via IDL. With help of SOM Compiler IDL file can be translated to various formats, including various language bindings. For example, to produce C header you can run

sc -s"h" somcls.idl

to produce DEF file you can run

sc -s"def" somcls.idl

SOM Compiler uses emitter to produce corresponding language binding. You can create new bindings emitter using Emitter Framework.

First of all, think about your class. What it must do? Define them in terms of object. Propose attributes and methods of class.

Ok. Imagine, we need class to have access to Java objects. Let's write class interface in terms of Interface Definition Language.

#include <somobj.idl>

interface JavaObject : SOMObject
{
  implementation
  {
    somDefaultInit: override;
    // Init Java Virtual Machine (if no current) and create Java object
    somDefaultDestruct: override;
    // Destruct Java object and close Java Virtual Machine (if needed)
  }
}

As you can see, no many problems. Syntax of IDL too closest to C-like languages. First thing you need is to include definitions of parent classes. In our case it is SOMObject definition. Generic Java object doesn't need to have any methods. Only thing JavaObject will do its check for existence of Java Virtual Machine and execution of it if required. On object destruction checks is Java Virtual Machine still required will be done and it will be destroyed if not required. Also same constructor and destructor will call corresponding constructor and destructor of Java object.

Classic IDL file contains definitions like

interface <class> : <parent_class>
{
  attribute <type> <name>

  <type> <method>(<parameters>)
}

As I known OMG IDL doesn't support methods override. SOM IDL has such feature (and incompatibility with OMG CORBA). This is done via keyword implementation. To solve problems with other IDL compilers such part must be wrapped to #ifdef structure:

#include <somobj.idl>

interface JavaObject : SOMObject
{

  #ifdef __SOMIDL__
  implementation
  {
    somDefaultInit: override;
    // Init Java Virtual Machine (if no current) and create Java object
    somDefaultDestruct: override;
    // Destruct Java object and close Java Virtual Machine (if needed)
  }
  #endif
}

Such approach well known in C-world, but also have some problems. For example, IDL of Document Object Model (DOM) (Yes, W3C DOM uses same IDL as SOM and CORBA) has attribute implementation. As result, SOM Compiler has some problems with IDL compilation.

Ok. Let's stop talking about syntax things (you always can read documentation) and try to create IDL from Java class.

As a first step we'll create SOM class interface for java.lang.Object. It is can be done with help of javah tool.

javah -jni java.lang.Object

As result you'll have such file:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class java_lang_Object */

#ifndef _Included_java_lang_Object
#define _Included_java_lang_Object
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     java_lang_Object
 * Method:    hashCode
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_java_lang_Object_hashCode
  (JNIEnv *, jobject);

/*
 * Class:     java_lang_Object
 * Method:    notify
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_java_lang_Object_notify
  (JNIEnv *, jobject);

/*
 * Class:     java_lang_Object
 * Method:    notifyAll
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_java_lang_Object_notifyAll
  (JNIEnv *, jobject);

/*
 * Class:     java_lang_Object
 * Method:    registerNatives
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_java_lang_Object_registerNatives
  (JNIEnv *, jclass);

/*
 * Class:     java_lang_Object
 * Method:    wait
 * Signature: (J)V
 */
JNIEXPORT void JNICALL Java_java_lang_Object_wait
  (JNIEnv *, jobject, jlong);

/*
 * Class:     java_lang_Object
 * Method:    getClass
 * Signature: ()Ljava/lang/Class;
 */
JNIEXPORT jclass JNICALL Java_java_lang_Object_getClass
  (JNIEnv *, jobject);

/*
 * Class:     java_lang_Object
 * Method:    clone
 * Signature: ()Ljava/lang/Object;
 */
JNIEXPORT jobject JNICALL Java_java_lang_Object_clone
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

So, this header can be used to generate actual class interface using this script:

/* REXX - our best dog */

do while lines('java_lang_Object.h')
  s=linein('java_lang_Object.h');
  parse value s with x 'Header for class' name '*/'
  if name\= then
  do
    classname=strip(name)
    say '#include <JavaObject.idl>'
    say 
    say 'interface '||classname||' : JavaObject'
    say '{'
  end
  parse value s with x 'Method:' name
  if name\= then
  do
    /* skip 3 lines */
    s=linein('java_lang_Object.h');
    s=linein('java_lang_Object.h');
    s=linein('java_lang_Object.h');
    s2=linein('java_lang_Object.h');
    interpret("parse value s with 'JNIEXPORT' type 'JNICALL Java_"||classname||"_' name")
    name=strip(name)
    s2=strip(s2)
    parse value s2 with start 'JNIEnv *, jobject' end
    s2=start||end
    parse value s2 with x ', ' y
    if x='(' then s2='('||y
    say '  '||type||name||' '||s2
  end
end
say '}'

As result, you'll have following:

#include <JavaObject.idl>

interface java_lang_Object : JavaObject
{
   jint hashCode ();
   void notify ();
   void notifyAll ();
   void registerNatives (JNIEnv *, jclass);
   void wait (jlong);
   jclass getClass ();
   jobject clone ();
} 

Using such approach you can easily make SOM wrappers for all Java classes. Using Java_JNI_API you can create Java classes and using SOM wrappers you integrate Java code to SOM-based applications. Because SOM API more generic then Java API you can use any available language bindings for development. Also, you can start extend Java classes by native code with help of SOM engine.