an initial skeleton for building a Java binding to the C runtime

This commit is contained in:
kr.angelov
2013-05-28 12:59:19 +00:00
parent af8cec11f9
commit bd859fcf28
3 changed files with 63 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
all:
libtool --mode=compile gcc -I /usr/lib/jvm/java-6-openjdk/include -std=c99 -c jpgf.c
libtool --mode=link gcc -g -O -o libjpgf.la jpgf.lo -rpath /usr/lib -lpgf -lgu
libtool --mode=install cp libjpgf.la /usr/lib/libjpgf.la
headers:
javah org.grammaticalframework.PGF

31
src/runtime/java/jpgf.c Normal file
View File

@@ -0,0 +1,31 @@
#include <pgf/pgf.h>
#include <gu/mem.h>
#include "org_grammaticalframework_PGF.h"
JNIEXPORT jobject JNICALL
Java_org_grammaticalframework_PGF_readPGF(JNIEnv *env, jclass cls, jstring s)
{
const char *fpath = (*env)->GetStringUTFChars(env, s, 0);
GuPool* pool = gu_new_pool();
GuPool* tmp_pool = gu_local_pool();
// Create an exception frame that catches all errors.
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
// Read the PGF grammar.
PgfPGF* pgf = pgf_read(fpath, pool, err);
if (!gu_ok(err)) {
gu_pool_free(pool);
gu_pool_free(tmp_pool);
return NULL;
}
gu_pool_free(tmp_pool);
(*env)->ReleaseStringUTFChars(env, s, fpath);
jmethodID constrId = (*env)->GetMethodID(env, cls, "<init>", "(II)V");
return (*env)->NewObject(env, cls, constrId, (int) pool, (int) pgf);
}

View File

@@ -0,0 +1,25 @@
package org.grammaticalframework;
public class PGF {
static native PGF readPGF(String path);
private int pool;
private int gr;
private PGF(int pool, int gr) {
this.pool = pool;
this.gr = gr;
}
static {
System.loadLibrary("jpgf");
}
public void test() {
System.out.println("pool="+pool+", gr="+gr);
}
public static void main(String[] args) {
readPGF("/home/krasimir/www.grammaticalframework.org/treebanks/PennTreebank/ParseEngAbs.pgf").test();
}
}