diff --git a/ase/awk/Argument.java b/ase/awk/Argument.java index 7e86bbc3..4dcf5056 100644 --- a/ase/awk/Argument.java +++ b/ase/awk/Argument.java @@ -1,36 +1,38 @@ /* - * $Id: Argument.java,v 1.2 2007/10/17 14:38:28 bacon Exp $ + * $Id: Argument.java,v 1.3 2007/10/18 14:51:04 bacon Exp $ */ package ase.awk; public class Argument { - protected Context ctx; - protected long value; + protected long runid; + protected long valid; - Argument (Context ctx) + Argument (long runid, long valid) { - this.ctx = ctx; + this.runid = runid; + this.valid = valid; } long getIntValue () { - return getintval (ctx.getId(), value); + return getintval (this.runid, this.valid); } double getRealValue () { - return 0.0; + return getrealval (this.runid, this.valid); } - String getStringValue () + String getStringValue () throws Exception { - return null; + return getstrval (this.runid, this.valid); } Argument getIndexed (String idx) { + // TODO:.. return null; } @@ -41,5 +43,5 @@ public class Argument protected native long getintval (long runid, long valid); protected native double getrealval (long runid, long valid); - protected native String getstrval (long runid, long valid); + protected native String getstrval (long runid, long valid) throws Exception; } diff --git a/ase/awk/Awk.java b/ase/awk/Awk.java index d52af7d0..618f238e 100644 --- a/ase/awk/Awk.java +++ b/ase/awk/Awk.java @@ -1,5 +1,5 @@ /* - * $Id: Awk.java,v 1.23 2007/10/17 14:38:28 bacon Exp $ + * $Id: Awk.java,v 1.25 2007/10/18 14:51:04 bacon Exp $ * * {License} */ @@ -62,49 +62,75 @@ public abstract class Awk /* == just in case == */ protected void finalize () throws Throwable { - if (this.awkid != 0) close (); + close (); super.finalize (); } - /* == native methods == */ - private native void open () throws Exception; - public native void close (); - public native void parse () throws Exception; - public native void run (String main, String[] args) throws Exception; - public native void stop (); - private native int getmaxdepth (int id); - private native void setmaxdepth (int id, int depth); - private native int getoption (); - private native void setoption (int opt); - private native boolean getdebug (); - private native void setdebug (boolean debug); - private native void setword (String ow, String nw); - private native void addfunc (String name, int min_args, int max_args) throws Exception; - private native void delfunc (String name) throws Exception; - native void setfilename (long runid, String name) throws Exception; - native void setofilename (long runid, String name) throws Exception; - private native Object strtonum (long runid, String str) throws Exception; - private native String valtostr (long runid, Object obj) throws Exception; - protected native String strftime (String fmt, long sec); - protected native String strfgmtime (String fmt, long sec); - protected native int system (String cmd); + public void close () + { + if (this.awkid != 0) + { + close (this.awkid); + this.awkid = 0; + } + } + + public void parse () throws Exception + { + parse (this.awkid); + } + + public void run (String main, String[] args) throws Exception + { + run (this.awkid, main, args); + } - /* == simpler run methods == */ public void run (String main) throws Exception { - run (main, null); + run (this.awkid, main, null); } public void run (String[] args) throws Exception { - run (null, args); + run (this.awkid, null, args); } public void run () throws Exception { - run (null, null); + run (this.awkid, null, null); } + public void stop () throws Exception + { + stop (this.awkid); + } + + /* == native methods == */ + private native void open () throws Exception; + protected native void close (long awkid); + protected native void parse (long awkid) throws Exception; + protected native void run (long awkid, String main, String[] args) throws Exception; + protected native void stop (long awkid) throws Exception; + protected native int getmaxdepth (long awkid, int id) throws Exception; + protected native void setmaxdepth (long awkid, int id, int depth) throws Exception; + protected native int getoption (long awkid) throws Exception; + protected native void setoption (long awkid, int opt) throws Exception; + protected native boolean getdebug (long awkid) throws Exception; + protected native void setdebug (long awkid, boolean debug) throws Exception; + protected native void setword (long awkid, String ow, String nw) throws Exception; + + + protected native void addfunc (String name, int min_args, int max_args) throws Exception; + protected native void delfunc (String name) throws Exception; + native void setfilename (long runid, String name) throws Exception; + native void setofilename (long runid, String name) throws Exception; + protected native Object strtonum (long runid, String str) throws Exception; + protected native String valtostr (long runid, Object obj) throws Exception; + protected native String strftime (String fmt, long sec); + protected native String strfgmtime (String fmt, long sec); + protected native int system (String cmd); + + /* == intrinsic functions == */ public void addFunction (String name, int min_args, int max_args) throws Exception { @@ -236,51 +262,52 @@ public abstract class Awk } /* == depth limiting == */ - public int getMaxDepth (int id) + public int getMaxDepth (int id) throws Exception { - return getmaxdepth (id); + return getmaxdepth (this.awkid, id); } - public void setMaxDepth (int ids, int depth) + public void setMaxDepth (int ids, int depth) throws Exception { - setmaxdepth (ids, depth); + setmaxdepth (this.awkid, ids, depth); } /* == option == */ - public int getOption () + public int getOption () throws Exception { - return getoption (); + return getoption (this.awkid); } - public void setOption (int opt) + public void setOption (int opt) throws Exception { - setoption (opt); + setoption (this.awkid, opt); } /* == debug == */ - public boolean getDebug () + public boolean getDebug () throws Exception { - return getdebug (); + return getdebug (this.awkid); } - public void setDebug (boolean debug) + public void setDebug (boolean debug) throws Exception { - setdebug (debug); + setdebug (this.awkid, debug); } - public void setWord (String ow, String nw) + /* == word replacement == */ + public void setWord (String ow, String nw) throws Exception { - setword (ow, nw); + setword (this.awkid, ow, nw); } - public void unsetWord (String ow) + public void unsetWord (String ow) throws Exception { - setword (ow, null); + setword (this.awkid, ow, null); } - public void unsetAllWords () + public void unsetAllWords () throws Exception { - setword (null, null); + setword (this.awkid, null, null); } /* == source code management == */ diff --git a/ase/awk/StdAwk.java b/ase/awk/StdAwk.java index 522d9702..923de567 100644 --- a/ase/awk/StdAwk.java +++ b/ase/awk/StdAwk.java @@ -1,5 +1,5 @@ /* - * $Id: StdAwk.java,v 1.15 2007/10/15 16:10:10 bacon Exp $ + * $Id: StdAwk.java,v 1.16 2007/10/18 14:51:04 bacon Exp $ * * {License} */ @@ -333,8 +333,12 @@ public abstract class StdAwk extends Awk /* == arithmetic built-in functions */ public Object sin (Context ctx, String name, Object[] args) throws Exception { + /* double x = builtinFunctionArgumentToDouble (ctx, args[0]); return new Double (Math.sin(x)); + */ + Argument x = (Argument)args[0]; + return new Double (Math.sin(x.getIntValue())); } public Object cos (Context ctx, String name, Object[] args) throws Exception diff --git a/ase/awk/jni.c b/ase/awk/jni.c index cd08dbd3..a6e8f9f9 100644 --- a/ase/awk/jni.c +++ b/ase/awk/jni.c @@ -1,5 +1,5 @@ /* - * $Id: jni.c,v 1.27 2007/10/17 14:38:28 bacon Exp $ + * $Id: jni.c,v 1.29 2007/10/18 14:51:04 bacon Exp $ * * {License} */ @@ -39,6 +39,7 @@ #define CLASS_EXCEPTION "ase/awk/Exception" #define CLASS_EXTIO "ase/awk/Extio" #define CLASS_CONTEXT "ase/awk/Context" +#define CLASS_ARGUMENT "ase/awk/Argument" #define FIELD_AWKID "awkid" #define FIELD_RUNID "runid" @@ -102,6 +103,7 @@ struct run_data_t jclass double_class; jclass object_class; jclass context_class; + jclass argument_class; jmethodID integer_init; jmethodID long_init; @@ -109,6 +111,7 @@ struct run_data_t jmethodID float_init; jmethodID double_init; jmethodID context_init; + jmethodID argument_init; jmethodID integer_value; jmethodID long_value; @@ -488,45 +491,33 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_open (JNIEnv* env, jobject obj) #endif } -JNIEXPORT void JNICALL Java_ase_awk_Awk_close (JNIEnv* env, jobject obj) +JNIEXPORT void JNICALL Java_ase_awk_Awk_close (JNIEnv* env, jobject obj, jlong awkid) { - jclass class; - jfieldID awkid; ase_awk_t* awk; + awk_data_t* tmp; + awk = (ase_awk_t*)awkid; + /* don't like to throw an exception for close. + * i find doing so very irritating, especially when it + * should be called in an exception handler */ + if (awk == NULL) return; + #if defined(_WIN32) && defined(_DEBUG) OutputDebugStringW (L"<<>>\n"); #endif - class = (*env)->GetObjectClass(env, obj); - awkid = (*env)->GetFieldID (env, class, FIELD_AWKID, "J"); - (*env)->DeleteLocalRef (env, class); - if (awkid == NULL) - { - /* internal error. no awkid field - * NoSuchFieldError, ExceptionInitializerError, - * OutOfMemoryError might have occurred */ - return; - } - - awk = (ase_awk_t*) (*env)->GetLongField (env, obj, awkid); - if (awk != NULL) - { - /* the awkid is not NULL. close it */ - awk_data_t* tmp = (awk_data_t*)ase_awk_getcustomdata (awk); + tmp = (awk_data_t*)ase_awk_getcustomdata (awk); #if defined(_WIN32) && defined(__DMC__) - HANDLE heap = tmp->heap; + HANDLE heap = tmp->heap; #endif - ase_awk_close (awk); - (*env)->SetLongField (env, obj, awkid, (jlong)0); + ase_awk_close (awk); #if defined(_WIN32) && defined(__DMC__) - awk_free (heap, tmp); - HeapDestroy (heap); + awk_free (heap, tmp); + HeapDestroy (heap); #else - awk_free (NULL, tmp); + awk_free (NULL, tmp); #endif - } #if defined(_WIN32) && defined(_DEBUG) OutputDebugStringW (L"<<>>\n"); @@ -534,30 +525,38 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_close (JNIEnv* env, jobject obj) _CrtDumpMemoryLeaks (); #endif #endif - } -JNIEXPORT void JNICALL Java_ase_awk_Awk_parse (JNIEnv* env, jobject obj) -{ - jclass class; - jfieldID handle; +#define EXCEPTION_ON_NULL_AWK(awk) \ + if (awk == NULL) \ + { \ + throw_exception ( \ + env, \ + ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOPER), \ + ASE_AWK_ENOPER, \ + 0); \ + return; \ + } +#define EXCEPTION_ON_NULL_AWK_RETURNING(awk,ret) \ + if (awk == NULL) \ + { \ + throw_exception ( \ + env, \ + ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOPER), \ + ASE_AWK_ENOPER, \ + 0); \ + return ret; \ + } + +JNIEXPORT void JNICALL Java_ase_awk_Awk_parse (JNIEnv* env, jobject obj, jlong awkid) +{ ase_awk_t* awk; ase_awk_srcios_t srcios; srcio_data_t srcio_data; - class = (*env)->GetObjectClass (env, obj); - handle = (*env)->GetFieldID (env, class, FIELD_AWKID, "J"); - (*env)->DeleteLocalRef (env, class); - if (handle == NULL) - { - /* internal error. no handle field - * NoSuchFieldError, ExceptionInitializerError, - * OutOfMemoryError might have occurred */ - return; - } - - awk = (ase_awk_t*) (*env)->GetLongField (env, obj, handle); + awk = (ase_awk_t*) awkid; + EXCEPTION_ON_NULL_AWK (awk); srcio_data.env = env; srcio_data.obj = obj; @@ -587,6 +586,7 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_parse (JNIEnv* env, jobject obj) (*env)->DeleteLocalRef (env, run_data.string_class); \ (*env)->DeleteLocalRef (env, run_data.object_class); \ (*env)->DeleteLocalRef (env, run_data.context_class); \ + (*env)->DeleteLocalRef (env, run_data.argument_class); \ if (run_data.context_object != NULL) \ (*env)->DeleteLocalRef (env, run_data.context_object); \ } while (0) @@ -651,35 +651,21 @@ static void on_run_end (ase_awk_run_t* run, int errnum, void* custom) (*env)->SetLongField (env, run_data->context_object, runid, (jlong)0); } -JNIEXPORT void JNICALL Java_ase_awk_Awk_run (JNIEnv* env, jobject obj, jstring mfn, jobjectArray args) +JNIEXPORT void JNICALL Java_ase_awk_Awk_run (JNIEnv* env, jobject obj, jlong awkid, jstring mfn, jobjectArray args) { - jclass class; - jfieldID handle; - ase_awk_t* awk; ase_awk_runcbs_t runcbs; ase_awk_runios_t runios; runio_data_t runio_data; run_data_t run_data; ase_char_t* mmm; + ase_awk_runarg_t* runarg = NULL; ase_size_t len, i; const jchar* ptr; - ase_awk_runarg_t* runarg = NULL; - - class = (*env)->GetObjectClass (env, obj); - handle = (*env)->GetFieldID (env, class, FIELD_AWKID, "J"); - (*env)->DeleteLocalRef (env, class); - if (handle == 0) - { - /* internal error. no handle field - * NoSuchFieldError, ExceptionInitializerError, - * OutOfMemoryError might have occurred */ - return; - } - - awk = (ase_awk_t*) (*env)->GetLongField (env, obj, handle); + awk = (ase_awk_t*) awkid; + EXCEPTION_ON_NULL_AWK (awk); run_data.env = env; run_data.obj = obj; @@ -693,6 +679,7 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_run (JNIEnv* env, jobject obj, jstring m run_data.double_class = (*env)->FindClass (env, "java/lang/Double"); run_data.object_class = (*env)->FindClass (env, "java/lang/Object"); run_data.context_class = (*env)->FindClass (env, CLASS_CONTEXT); + run_data.argument_class = (*env)->FindClass (env, CLASS_ARGUMENT); ASE_ASSERT (run_data.string_class != NULL); ASE_ASSERT (run_data.integer_class != NULL); @@ -702,6 +689,7 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_run (JNIEnv* env, jobject obj, jstring m ASE_ASSERT (run_data.double_class != NULL); ASE_ASSERT (run_data.object_class != NULL); ASE_ASSERT (run_data.context_class != NULL); + ASE_ASSERT (run_data.argument_class != NULL); run_data.integer_init = (*env)->GetMethodID ( env, run_data.integer_class, "", "(I)V"); @@ -715,6 +703,8 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_run (JNIEnv* env, jobject obj, jstring m env, run_data.double_class, "", "(D)V"); run_data.context_init = (*env)->GetMethodID ( env, run_data.context_class, "", "(Lase/awk/Awk;)V"); + run_data.argument_init = (*env)->GetMethodID ( + env, run_data.argument_class, "", "(JJ)V"); ASE_ASSERT (run_data.integer_init != NULL); ASE_ASSERT (run_data.long_init != NULL); @@ -722,6 +712,7 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_run (JNIEnv* env, jobject obj, jstring m ASE_ASSERT (run_data.float_init != NULL); ASE_ASSERT (run_data.double_init != NULL); ASE_ASSERT (run_data.context_init != NULL); + ASE_ASSERT (run_data.argument_init != NULL); run_data.integer_value = (*env)->GetMethodID ( env, run_data.integer_class, "intValue", "()I"); @@ -955,25 +946,11 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_run (JNIEnv* env, jobject obj, jstring m DELETE_CLASS_REFS (env, run_data); } -JNIEXPORT void JNICALL Java_ase_awk_Awk_stop (JNIEnv* env, jobject obj) +JNIEXPORT void JNICALL Java_ase_awk_Awk_stop (JNIEnv* env, jobject obj, jlong awkid) { - jclass class; - jfieldID handle; - ase_awk_t* awk; - - class = (*env)->GetObjectClass(env, obj); - handle = (*env)->GetFieldID (env, class, FIELD_AWKID, "J"); - (*env)->DeleteLocalRef (env, class); - if (handle == NULL) - { - /* internal error. no handle field - * NoSuchFieldError, ExceptionInitializerError, - * OutOfMemoryError might have occurred */ - return; - } - - awk = (ase_awk_t*) (*env)->GetLongField (env, obj, handle); - if (awk != NULL) ase_awk_stopall (awk); + ase_awk_t* awk = (ase_awk_t*)awkid; + EXCEPTION_ON_NULL_AWK (awk); + ase_awk_stopall (awk); } static ase_ssize_t java_open_source (JNIEnv* env, jobject obj, int mode) @@ -1718,6 +1695,11 @@ static int handle_bfn ( { v = ase_awk_getarg (run, i); + arg = (*env)->NewObject (env, + run_data->argument_class, + run_data->argument_init, (jlong)run, (jlong)v); + +#if 0 arg = NULL; if (v->type == ASE_AWK_VAL_INT) @@ -1768,8 +1750,9 @@ static int handle_bfn ( env, (jchar*)((ase_awk_val_str_t*)v)->buf, len); } } +#endif - if (v->type != ASE_AWK_VAL_NIL && arg == NULL) + if (/*v->type != ASE_AWK_VAL_NIL && */arg == NULL) { if ((*env)->ExceptionOccurred (env)) { @@ -2099,155 +2082,60 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_delfunc ( } } -JNIEXPORT jint JNICALL Java_ase_awk_Awk_getmaxdepth ( - JNIEnv* env, jobject obj, jint id) +JNIEXPORT jint JNICALL Java_ase_awk_Awk_getmaxdepth (JNIEnv* env, jobject obj, jlong awkid, jint id) { - jclass class; - jfieldID handle; - ase_awk_t* awk; - - class = (*env)->GetObjectClass(env, obj); - handle = (*env)->GetFieldID (env, class, FIELD_AWKID, "J"); - (*env)->DeleteLocalRef (env, class); - if (handle == NULL) return 0; /* should never happen */ - - awk = (ase_awk_t*) (*env)->GetLongField (env, obj, handle); + ase_awk_t* awk = (ase_awk_t*)awkid; + EXCEPTION_ON_NULL_AWK_RETURNING (awk, 0); return (jint)ase_awk_getmaxdepth (awk, id); } -JNIEXPORT void JNICALL Java_ase_awk_Awk_setmaxdepth ( - JNIEnv* env, jobject obj, jint ids, jint depth) +JNIEXPORT void JNICALL Java_ase_awk_Awk_setmaxdepth (JNIEnv* env, jobject obj, jlong awkid, jint ids, jint depth) { - jclass class; - jfieldID handle; - ase_awk_t* awk; - - class = (*env)->GetObjectClass(env, obj); - handle = (*env)->GetFieldID (env, class, FIELD_AWKID, "J"); - (*env)->DeleteLocalRef (env, class); - if (handle == NULL) - { - /* internal error. no handle field - * NoSuchFieldError, ExceptionInitializerError, - * OutOfMemoryError might have occurred */ - return; - } - - awk = (ase_awk_t*) (*env)->GetLongField (env, obj, handle); + ase_awk_t* awk = (ase_awk_t*)awkid; + EXCEPTION_ON_NULL_AWK (awk); ase_awk_setmaxdepth (awk, ids, depth); } -JNIEXPORT jint JNICALL Java_ase_awk_Awk_getoption ( - JNIEnv* env, jobject obj) +JNIEXPORT jint JNICALL Java_ase_awk_Awk_getoption (JNIEnv* env, jobject obj, jlong awkid) { - jclass class; - jfieldID handle; - ase_awk_t* awk; - - class = (*env)->GetObjectClass(env, obj); - handle = (*env)->GetFieldID (env, class, FIELD_AWKID, "J"); - (*env)->DeleteLocalRef (env, class); - if (handle == NULL) - { - /* internal error. no handle field - * NoSuchFieldError, ExceptionInitializerError, - * OutOfMemoryError might have occurred */ - return 0; - } - - awk = (ase_awk_t*) (*env)->GetLongField (env, obj, handle); + ase_awk_t* awk = (ase_awk_t*)awkid; + EXCEPTION_ON_NULL_AWK_RETURNING (awk, 0); return ase_awk_getoption (awk); } -JNIEXPORT void JNICALL Java_ase_awk_Awk_setoption ( - JNIEnv* env, jobject obj, jint options) +JNIEXPORT void JNICALL Java_ase_awk_Awk_setoption (JNIEnv* env, jobject obj, jlong awkid, jint options) { - jclass class; - jfieldID handle; - ase_awk_t* awk; - - class = (*env)->GetObjectClass(env, obj); - handle = (*env)->GetFieldID (env, class, FIELD_AWKID, "J"); - (*env)->DeleteLocalRef (env, class); - if (handle == NULL) - { - /* internal error. no handle field - * NoSuchFieldError, ExceptionInitializerError, - * OutOfMemoryError might have occurred */ - return; - } - - awk = (ase_awk_t*) (*env)->GetLongField (env, obj, handle); + ase_awk_t* awk = (ase_awk_t*)awkid; + EXCEPTION_ON_NULL_AWK (awk); ase_awk_setoption (awk, (int)options); } -JNIEXPORT jboolean JNICALL Java_ase_awk_Awk_getdebug ( - JNIEnv* env, jobject obj) +JNIEXPORT jboolean JNICALL Java_ase_awk_Awk_getdebug (JNIEnv* env, jobject obj, jlong awkid) { - jclass class; - jfieldID handle; - ase_awk_t* awk; - - class = (*env)->GetObjectClass(env, obj); - handle = (*env)->GetFieldID (env, class, FIELD_AWKID, "J"); - (*env)->DeleteLocalRef (env, class); - if (handle == NULL) - { - /* internal error. no handle field - * NoSuchFieldError, ExceptionInitializerError, - * OutOfMemoryError might have occurred */ - return JNI_FALSE; - } - - awk = (ase_awk_t*) (*env)->GetLongField (env, obj, handle); + ase_awk_t* awk = (ase_awk_t*)awkid; + EXCEPTION_ON_NULL_AWK_RETURNING (awk, JNI_FALSE); return ((awk_data_t*)ase_awk_getcustomdata(awk))->debug? JNI_TRUE: JNI_FALSE; } JNIEXPORT void JNICALL Java_ase_awk_Awk_setdebug ( - JNIEnv* env, jobject obj, jboolean debug) + JNIEnv* env, jobject obj, jlong awkid, jboolean debug) { - jclass class; - jfieldID handle; - ase_awk_t* awk; - - class = (*env)->GetObjectClass(env, obj); - handle = (*env)->GetFieldID (env, class, FIELD_AWKID, "J"); - (*env)->DeleteLocalRef (env, class); - if (handle == NULL) - { - /* internal error. no handle field - * NoSuchFieldError, ExceptionInitializerError, - * OutOfMemoryError might have occurred */ - return; - } - - awk = (ase_awk_t*) (*env)->GetLongField (env, obj, handle); + ase_awk_t* awk = (ase_awk_t*)awkid; + EXCEPTION_ON_NULL_AWK (awk); ((awk_data_t*)ase_awk_getcustomdata(awk))->debug = debug; } JNIEXPORT void JNICALL Java_ase_awk_Awk_setword ( - JNIEnv* env, jobject obj, jstring ow, jstring nw) + JNIEnv* env, jobject obj, jlong awkid, jstring ow, jstring nw) { - jclass class; - jfieldID handle; ase_awk_t* awk; const jchar* op = NULL, * np = NULL; jsize ol = 0, nl = 0; ase_char_t* ox, * nx; jint r; - class = (*env)->GetObjectClass(env, obj); - handle = (*env)->GetFieldID (env, class, FIELD_AWKID, "J"); - (*env)->DeleteLocalRef (env, class); - if (handle == NULL) - { - /* internal error. no handle field - * NoSuchFieldError, ExceptionInitializerError, - * OutOfMemoryError might have occurred */ - return; - } - - awk = (ase_awk_t*) (*env)->GetLongField (env, obj, handle); + awk = (ase_awk_t*) awkid; + EXCEPTION_ON_NULL_AWK (awk); if (ow != NULL) { @@ -2941,20 +2829,51 @@ JNIEXPORT jdouble JNICALL Java_ase_awk_Argument_getrealval (JNIEnv* env, jobject JNIEXPORT jstring JNICALL Java_ase_awk_Argument_getstrval (JNIEnv* env, jobject obj, long runid, long valid) { - int n; + ase_awk_run_t* run = (ase_awk_run_t*)runid; + ase_awk_val_t* val = (ase_awk_val_t*)valid; + ase_awk_t* awk = ase_awk_getrunawk (run); + ase_char_t* str; ase_size_t len; - jstring ret; + jstring ret = NULL; str = ase_awk_valtostr ( - (ase_awk_run_t*)runid, (ase_awk_val_t*)valid, - ASE_AWK_VALTOSTR_CLEAR, ASE_NULL,&len); - if (str == ASE_NULL) return ASE_NULL; + run, val, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &len); + if (str == ASE_NULL) goto no_mem; - // TODO: convert string properly.... - ret = (*env)->NewString (env, (jchar*)str, len); - ase_awk_free (ase_awk_getrunawk((ase_awk_run_t*)runid), str); + if (len > 0 && ASE_SIZEOF(jchar) != ASE_SIZEOF(ase_char_t)) + { + jchar* tmp; + ase_size_t i; + + tmp = (jchar*) ase_awk_malloc (awk, ASE_SIZEOF(jchar)*len); + if (tmp == NULL) + { + ase_awk_free (awk, str); + goto no_mem; + } + for (i = 0; i < len; i++) tmp[i] = (jchar)str[i]; + ret = (*env)->NewString (env, tmp, len); + ase_awk_free (awk, tmp); + } + else + { + ret = (*env)->NewString (env, (jchar*)str, len); + } + + ase_awk_free (awk, str); + + if (ret == NULL) + { + if (is_debug(awk)) (*env)->ExceptionDescribe (env); + (*env)->ExceptionClear (env); +no_mem: + throw_exception ( + env, + ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM), + ASE_AWK_ENOMEM, + 0); + } - // TODO: clear exception if any... return ret; } diff --git a/ase/awk/jni.h b/ase/awk/jni.h index a448cd5b..3da09bc6 100644 --- a/ase/awk/jni.h +++ b/ase/awk/jni.h @@ -1,5 +1,5 @@ /* - * $Id: jni.h,v 1.7 2007/10/17 14:38:28 bacon Exp $ + * $Id: jni.h,v 1.8 2007/10/18 11:14:48 bacon Exp $ * * {License} */ @@ -18,11 +18,11 @@ extern "C" { #endif JNIEXPORT void JNICALL Java_ase_awk_Awk_open (JNIEnv* env, jobject obj); -JNIEXPORT void JNICALL Java_ase_awk_Awk_close (JNIEnv* env, jobject obj); -JNIEXPORT void JNICALL Java_ase_awk_Awk_parse (JNIEnv* env, jobject obj); +JNIEXPORT void JNICALL Java_ase_awk_Awk_close (JNIEnv* env, jobject obj, jlong awkid); +JNIEXPORT void JNICALL Java_ase_awk_Awk_parse (JNIEnv* env, jobject obj, jlong awkid); JNIEXPORT void JNICALL Java_ase_awk_Awk_run ( - JNIEnv* env, jobject obj, jstring mfn, jobjectArray args); -JNIEXPORT void JNICALL Java_ase_awk_Awk_stop (JNIEnv* env, jobject obj); + JNIEnv* env, jobject obj, jlong awkid, jstring mfn, jobjectArray args); +JNIEXPORT void JNICALL Java_ase_awk_Awk_stop (JNIEnv* env, jobject obj, jlong awkid); JNIEXPORT void JNICALL Java_ase_awk_Awk_addfunc ( JNIEnv* env, jobject obj, jstring name, jint min_args, jint max_args); @@ -30,22 +30,22 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_delfunc ( JNIEnv* env, jobject obj, jstring name); JNIEXPORT jint JNICALL Java_ase_awk_Awk_getmaxdepth ( - JNIEnv* env, jobject obj, jint id); + JNIEnv* env, jobject obj, jlong awkid, jint id); JNIEXPORT void JNICALL Java_ase_awk_Awk_setmaxdepth ( - JNIEnv* env, jobject obj, jint ids, jint depth); + JNIEnv* env, jobject obj, jlong awkid, jint ids, jint depth); JNIEXPORT jint JNICALL Java_ase_awk_Awk_getoption ( - JNIEnv* env, jobject obj); + JNIEnv* env, jobject obj, jlong awkid); JNIEXPORT void JNICALL Java_ase_awk_Awk_setoption ( - JNIEnv* env, jobject obj, jint options); + JNIEnv* env, jobject obj, jlong awkid, jint options); JNIEXPORT jboolean JNICALL Java_ase_awk_Awk_getdebug ( - JNIEnv* env, jobject obj); + JNIEnv* env, jobject obj, jlong awkid); JNIEXPORT void JNICALL Java_ase_awk_Awk_setdebug ( - JNIEnv* env, jobject obj, jboolean debug); + JNIEnv* env, jobject obj, jlong awkid, jboolean debug); JNIEXPORT void JNICALL Java_ase_awk_Awk_setword ( - JNIEnv* env, jobject obj, jstring ow, jstring nw); + JNIEnv* env, jobject obj, jlong awkid, jstring ow, jstring nw); JNIEXPORT void JNICALL Java_ase_awk_Awk_setfilename ( JNIEnv* env, jobject obj, jlong runid, jstring name); diff --git a/ase/test/awk/AseAwk.java b/ase/test/awk/AseAwk.java index 0d9b298b..a101a1f9 100644 --- a/ase/test/awk/AseAwk.java +++ b/ase/test/awk/AseAwk.java @@ -1,5 +1,5 @@ /* - * $Id: AseAwk.java,v 1.12 2007/10/15 16:10:10 bacon Exp $ + * $Id: AseAwk.java,v 1.13 2007/10/18 14:51:04 bacon Exp $ */ import java.awt.*; @@ -223,7 +223,8 @@ public class AseAwk extends StdAwk String ow = arg.substring (0, idx); String nw = arg.substring (idx+1); - awk.setWord (ow, nw); + try { awk.setWord (ow, nw); } + catch (Exception e) {/* don't care */} mode = 0; } } diff --git a/ase/test/awk/AseAwkPanel.java b/ase/test/awk/AseAwkPanel.java index 0e9f4db9..d88f7408 100644 --- a/ase/test/awk/AseAwkPanel.java +++ b/ase/test/awk/AseAwkPanel.java @@ -1,5 +1,5 @@ /* - * $Id: AseAwkPanel.java,v 1.4 2007/10/15 16:10:10 bacon Exp $ + * $Id: AseAwkPanel.java,v 1.5 2007/10/18 14:51:04 bacon Exp $ */ import java.awt.*; @@ -399,10 +399,16 @@ public class AseAwkPanel extends Panel awk.parse (); awk.run (); + } catch (ase.awk.Exception e) { - showMessage ("An exception occurred - " + e.getMessage()); + int line = e.getLine(); + if (line <= 0) + showMessage ("An exception occurred - " + e.getMessage()); + else + showMessage ("An exception occurred - " + e.getMessage() + " at line " + line); + return; } finally