qse/ase/awk/jni.c

2347 lines
52 KiB
C
Raw Normal View History

2006-08-02 15:03:49 +00:00
/*
2007-03-19 03:33:54 +00:00
* $Id: jni.c,v 1.75 2007-03-19 03:33:53 bacon Exp $
2007-02-03 10:47:41 +00:00
*
* {License}
2006-08-02 15:03:49 +00:00
*/
2006-12-12 05:44:42 +00:00
#include <stdio.h>
#include <stddef.h>
2006-10-12 14:36:25 +00:00
#include <stdlib.h>
#include <string.h>
#include <wctype.h>
#include <wchar.h>
2006-11-19 15:33:40 +00:00
#include <stdarg.h>
2006-11-21 15:06:51 +00:00
#include <math.h>
2006-12-12 05:44:42 +00:00
#include <ase/awk/jni.h>
2007-01-23 14:23:18 +00:00
#include <ase/awk/awk.h>
#include <ase/awk/val.h>
2006-11-19 15:33:40 +00:00
2007-02-23 08:28:39 +00:00
#include <ase/utl/stdio.h>
#include <ase/utl/ctype.h>
2007-01-19 03:23:47 +00:00
2006-11-19 15:33:40 +00:00
#ifdef _WIN32
#include <windows.h>
#include <tchar.h>
#endif
2006-08-02 15:03:49 +00:00
2006-11-21 15:06:51 +00:00
#ifndef ASE_CHAR_IS_WCHAR
#error this module supports ASE_CHAR_IS_WCHAR only
#endif
2007-01-23 14:23:18 +00:00
#define CLASS_OUTOFMEMORYERROR "java/lang/OutOfMemoryError"
#define CLASS_EXCEPTION "ase/awk/Exception"
#define CLASS_EXTIO "ase/awk/Extio"
#define FIELD_HANDLE "handle"
2006-08-06 08:16:03 +00:00
2006-11-28 15:09:53 +00:00
#define MSG_SIZE 256
2006-10-13 10:18:39 +00:00
enum
{
SOURCE_READ = 1,
SOURCE_WRITE = 2
};
2006-10-24 04:10:12 +00:00
static ase_ssize_t __read_source (
int cmd, void* arg, ase_char_t* data, ase_size_t count);
static ase_ssize_t __write_source (
int cmd, void* arg, ase_char_t* data, ase_size_t count);
2006-11-21 15:06:51 +00:00
static ase_ssize_t __process_extio (
2006-10-24 04:10:12 +00:00
int cmd, void* arg, ase_char_t* data, ase_size_t count);
2006-08-06 08:16:03 +00:00
2007-01-25 14:14:56 +00:00
typedef struct awk_data_t awk_data_t;
2006-08-06 08:16:03 +00:00
typedef struct srcio_data_t srcio_data_t;
2006-10-13 10:18:39 +00:00
typedef struct runio_data_t runio_data_t;
2006-11-28 04:30:57 +00:00
typedef struct run_data_t run_data_t;
2006-08-06 08:16:03 +00:00
2007-01-25 14:14:56 +00:00
struct awk_data_t
{
int debug;
};
2006-08-06 08:16:03 +00:00
struct srcio_data_t
{
JNIEnv* env;
jobject obj;
};
2006-10-13 10:18:39 +00:00
struct runio_data_t
{
JNIEnv* env;
jobject obj;
};
2006-11-28 04:30:57 +00:00
struct run_data_t
{
JNIEnv* env;
jobject obj;
2006-11-29 11:41:15 +00:00
jclass string_class;
jclass integer_class;
2006-11-29 14:52:36 +00:00
jclass long_class;
jclass short_class;
jclass float_class;
2006-11-29 11:41:15 +00:00
jclass double_class;
jclass object_class;
2006-11-29 14:52:36 +00:00
jmethodID integer_init;
jmethodID long_init;
jmethodID short_init;
jmethodID float_init;
jmethodID double_init;
jmethodID integer_value;
jmethodID long_value;
jmethodID short_value;
jmethodID float_value;
jmethodID double_value;
2006-11-28 04:30:57 +00:00
};
2007-02-24 14:32:44 +00:00
static void* awk_malloc (void* custom, ase_size_t n)
2006-10-12 14:36:25 +00:00
{
return malloc (n);
}
2007-02-24 14:32:44 +00:00
static void* awk_realloc (void* custom, void* ptr, ase_size_t n)
2006-10-12 14:36:25 +00:00
{
return realloc (ptr, n);
}
2007-02-24 14:32:44 +00:00
static void awk_free (void* custom, void* ptr)
2006-10-12 14:36:25 +00:00
{
free (ptr);
}
2006-08-06 08:16:03 +00:00
2007-03-10 15:42:00 +00:00
/* custom character class functions */
static ase_bool_t awk_isupper (void* custom, ase_cint_t c)
{
return ase_isupper (c);
}
static ase_bool_t awk_islower (void* custom, ase_cint_t c)
{
return ase_islower (c);
}
static ase_bool_t awk_isalpha (void* custom, ase_cint_t c)
{
return ase_isalpha (c);
}
static ase_bool_t awk_isdigit (void* custom, ase_cint_t c)
{
return ase_isdigit (c);
}
static ase_bool_t awk_isxdigit (void* custom, ase_cint_t c)
{
return ase_isxdigit (c);
}
static ase_bool_t awk_isalnum (void* custom, ase_cint_t c)
{
return ase_isalnum (c);
}
static ase_bool_t awk_isspace (void* custom, ase_cint_t c)
{
return ase_isspace (c);
}
static ase_bool_t awk_isprint (void* custom, ase_cint_t c)
{
return ase_isprint (c);
}
static ase_bool_t awk_isgraph (void* custom, ase_cint_t c)
{
return ase_isgraph (c);
}
static ase_bool_t awk_iscntrl (void* custom, ase_cint_t c)
{
return ase_iscntrl (c);
}
static ase_bool_t awk_ispunct (void* custom, ase_cint_t c)
{
return ase_ispunct (c);
}
static ase_cint_t awk_toupper (void* custom, ase_cint_t c)
{
return ase_toupper (c);
}
static ase_cint_t awk_tolower (void* custom, ase_cint_t c)
{
return ase_tolower (c);
}
2007-02-24 14:32:44 +00:00
static ase_real_t awk_pow (void* custom, ase_real_t x, ase_real_t y)
2006-11-21 15:06:51 +00:00
{
return pow (x, y);
}
2007-02-24 14:32:44 +00:00
static int awk_sprintf (
void* custom, ase_char_t* buf, ase_size_t size,
const ase_char_t* fmt, ...)
{
int n;
va_list ap;
va_start (ap, fmt);
n = ase_vsprintf (buf, size, fmt, ap);
va_end (ap);
return n;
}
2007-03-06 14:58:00 +00:00
static void awk_dprintf (void* custom, const ase_char_t* fmt, ...)
2007-02-24 14:32:44 +00:00
{
va_list ap;
va_start (ap, fmt);
2007-03-06 14:58:00 +00:00
ase_vfprintf (stderr, fmt, ap);
2007-02-24 14:32:44 +00:00
va_end (ap);
}
2007-03-06 14:58:00 +00:00
#ifndef NDEBUG
void ase_assert_abort (void)
{
abort ();
}
2007-03-10 15:42:00 +00:00
void ase_assert_printf (const ase_char_t* fmt, ...)
2006-11-19 15:33:40 +00:00
{
va_list ap;
va_start (ap, fmt);
2007-03-06 14:58:00 +00:00
ase_vfprintf (stdout, fmt, ap);
2006-11-19 15:33:40 +00:00
va_end (ap);
}
2007-03-06 14:58:00 +00:00
#endif
2006-11-19 15:33:40 +00:00
2007-01-23 14:23:18 +00:00
static void throw_exception (
JNIEnv* env, const ase_char_t* msg, jint code, jint line)
{
jclass except_class;
jmethodID except_cons;
jstring except_msg;
jthrowable except_obj;
2007-01-31 08:23:59 +00:00
ase_size_t len;
2007-01-23 14:23:18 +00:00
except_class = (*env)->FindClass (env, CLASS_EXCEPTION);
if (except_class == NULL)
{
/* the exception to be thrown by FindClass is not cleared.
* 1. this should not happend as the ase.awk.Exception
* class should always be there.
* 2. if it happens, this exception may abort the entire
* program as the exception is not likely to be handled
* explicitly by the java program. */
return;
}
except_cons = (*env)->GetMethodID (
env, except_class, "<init>", "(Ljava/lang/String;II)V");
if (except_cons == NULL)
{
/* the potential exception to be thrown by the GetMethodID
* method is not cleared here for the same reason as the
* FindClass method above */
(*env)->DeleteLocalRef (env, except_class);
return;
}
2007-02-23 08:17:51 +00:00
len = ase_strlen(msg);
2007-01-31 08:23:59 +00:00
if (len > 0 && ASE_SIZEOF(jchar) != ASE_SIZEOF(ase_char_t))
2007-01-23 14:23:18 +00:00
{
2007-01-31 08:23:59 +00:00
ase_size_t i;
2007-01-23 14:23:18 +00:00
jchar* tmp = (jchar*) malloc (ASE_SIZEOF(jchar)*len);
if (tmp == NULL)
{
(*env)->DeleteLocalRef (env, except_class);
2007-01-24 14:21:30 +00:00
except_class = (*env)->FindClass (
env, CLASS_OUTOFMEMORYERROR);
2007-01-23 14:23:18 +00:00
if (except_class == NULL) return;
2007-01-26 15:27:01 +00:00
(*env)->ThrowNew (env, except_class, "out of memory");
2007-01-23 14:23:18 +00:00
(*env)->DeleteLocalRef (env, except_class);
return;
}
for (i = 0; i < len; i++) tmp[i] = (jchar)msg[i];
except_msg = (*env)->NewString (env, tmp, len);
free (tmp);
}
else
{
2007-01-31 08:23:59 +00:00
except_msg = (*env)->NewString (env, (jchar*)msg, len);
2007-01-23 14:23:18 +00:00
}
if (except_msg == NULL)
{
(*env)->DeleteLocalRef (env, except_class);
return;
}
except_obj = (*env)->NewObject (
env, except_class, except_cons,
except_msg, code, line);
(*env)->DeleteLocalRef (env, except_msg);
(*env)->DeleteLocalRef (env, except_class);
if (except_obj == NULL) return;
(*env)->Throw (env, except_obj);
(*env)->DeleteLocalRef (env, except_obj);
}
2007-01-25 14:14:56 +00:00
static jboolean is_debug (ase_awk_t* awk)
{
awk_data_t* awk_data = (awk_data_t*)ase_awk_getcustomdata (awk);
return awk_data->debug? JNI_TRUE: JNI_FALSE;
}
2006-10-24 04:10:12 +00:00
JNIEXPORT void JNICALL Java_ase_awk_Awk_open (JNIEnv* env, jobject obj)
2006-08-06 08:16:03 +00:00
{
jclass class;
2007-01-23 14:23:18 +00:00
jfieldID handle;
2006-10-24 04:10:12 +00:00
ase_awk_t* awk;
2007-02-01 08:38:24 +00:00
ase_awk_prmfns_t prmfns;
2007-01-25 14:14:56 +00:00
awk_data_t* awk_data;
2006-12-15 14:58:37 +00:00
int opt, errnum;
2006-08-06 08:16:03 +00:00
2007-02-01 08:38:24 +00:00
memset (&prmfns, 0, sizeof(prmfns));
2007-02-23 08:28:39 +00:00
prmfns.mmgr.malloc = awk_malloc;
prmfns.mmgr.realloc = awk_realloc;
prmfns.mmgr.free = awk_free;
prmfns.mmgr.custom_data = NULL;
2007-03-10 15:42:00 +00:00
prmfns.ccls.is_upper = awk_isupper;
prmfns.ccls.is_lower = awk_islower;
prmfns.ccls.is_alpha = awk_isalpha;
prmfns.ccls.is_digit = awk_isdigit;
prmfns.ccls.is_xdigit = awk_isxdigit;
prmfns.ccls.is_alnum = awk_isalnum;
prmfns.ccls.is_space = awk_isspace;
prmfns.ccls.is_print = awk_isprint;
prmfns.ccls.is_graph = awk_isgraph;
prmfns.ccls.is_cntrl = awk_iscntrl;
prmfns.ccls.is_punct = awk_ispunct;
prmfns.ccls.to_upper = awk_toupper;
prmfns.ccls.to_lower = awk_tolower;
2007-02-23 08:28:39 +00:00
prmfns.ccls.custom_data = NULL;
prmfns.misc.pow = awk_pow;
2007-02-24 14:32:44 +00:00
prmfns.misc.sprintf = awk_sprintf;
2007-02-23 08:28:39 +00:00
prmfns.misc.dprintf = awk_dprintf;
prmfns.misc.custom_data = NULL;
2006-12-13 14:13:07 +00:00
2007-01-28 11:28:27 +00:00
awk_data = (awk_data_t*) malloc (sizeof(awk_data_t));
2007-01-25 14:14:56 +00:00
if (awk_data == NULL)
{
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
2007-01-28 11:28:27 +00:00
ASE_AWK_ENOMEM,
2007-01-25 14:14:56 +00:00
0);
return;
}
memset (awk_data, 0, sizeof(awk_data_t));
2007-02-01 08:38:24 +00:00
awk = ase_awk_open (&prmfns, awk_data, &errnum);
2006-08-06 08:16:03 +00:00
if (awk == NULL)
{
2007-01-23 14:23:18 +00:00
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, errnum),
2007-01-23 14:23:18 +00:00
errnum,
0);
2006-08-06 08:16:03 +00:00
return;
}
2006-11-28 15:09:53 +00:00
class = (*env)->GetObjectClass(env, obj);
2007-01-23 14:23:18 +00:00
handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
2006-11-28 15:09:53 +00:00
(*env)->DeleteLocalRef (env, class);
2007-01-23 14:23:18 +00:00
if (handle == NULL)
2006-11-27 04:33:22 +00:00
{
2007-01-24 14:21:30 +00:00
/* internal error. no handle field
* NoSuchFieldError, ExceptionInitializerError,
* OutOfMemoryError might occur */
2007-01-25 14:14:56 +00:00
ase_awk_close (awk);
free (awk_data);
2006-11-27 04:33:22 +00:00
return;
}
2006-08-06 08:16:03 +00:00
2007-01-23 14:23:18 +00:00
(*env)->SetLongField (env, obj, handle, (jlong)awk);
2006-08-06 08:16:03 +00:00
2007-01-03 09:51:53 +00:00
opt = ASE_AWK_EXPLICIT | ASE_AWK_UNIQUEFN | ASE_AWK_SHADING |
2006-12-04 06:04:07 +00:00
ASE_AWK_IMPLICIT | ASE_AWK_SHIFT | ASE_AWK_IDIV |
2007-01-05 13:39:38 +00:00
ASE_AWK_EXTIO | ASE_AWK_BLOCKLESS | ASE_AWK_NEXTOFILE;
2007-01-07 07:30:40 +00:00
ase_awk_setoption (awk, opt);
2006-08-06 08:16:03 +00:00
}
2006-10-24 04:10:12 +00:00
JNIEXPORT void JNICALL Java_ase_awk_Awk_close (JNIEnv* env, jobject obj)
2006-08-02 15:03:49 +00:00
{
2006-08-06 08:16:03 +00:00
jclass class;
2007-01-23 14:23:18 +00:00
jfieldID handle;
ase_awk_t* awk;
2006-08-06 08:16:03 +00:00
class = (*env)->GetObjectClass(env, obj);
2007-01-23 14:23:18 +00:00
handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
2006-11-28 15:09:53 +00:00
(*env)->DeleteLocalRef (env, class);
2007-01-23 14:23:18 +00:00
if (handle == NULL)
{
2007-01-24 14:21:30 +00:00
/* internal error. no handle field
* NoSuchFieldError, ExceptionInitializerError,
* OutOfMemoryError might occur */
2006-11-27 04:33:22 +00:00
return;
}
2006-08-06 08:16:03 +00:00
2007-01-23 14:23:18 +00:00
awk = (ase_awk_t*) (*env)->GetLongField (env, obj, handle);
if (awk != NULL)
2006-11-26 15:55:44 +00:00
{
2007-01-23 14:23:18 +00:00
/* the handle is not NULL. close it */
2007-01-25 14:14:56 +00:00
void* tmp = ase_awk_getcustomdata (awk);
2007-01-23 14:23:18 +00:00
ase_awk_close (awk);
(*env)->SetLongField (env, obj, handle, (jlong)0);
2007-01-25 14:14:56 +00:00
free (tmp);
2006-11-26 15:55:44 +00:00
}
}
2006-10-24 04:10:12 +00:00
JNIEXPORT void JNICALL Java_ase_awk_Awk_parse (JNIEnv* env, jobject obj)
2006-08-02 15:03:49 +00:00
{
2006-08-06 08:16:03 +00:00
jclass class;
2007-01-23 14:23:18 +00:00
jfieldID handle;
2006-08-06 08:16:03 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_t* awk;
ase_awk_srcios_t srcios;
2006-08-06 08:16:03 +00:00
srcio_data_t srcio_data;
class = (*env)->GetObjectClass (env, obj);
2007-01-23 14:23:18 +00:00
handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
2006-11-28 15:09:53 +00:00
(*env)->DeleteLocalRef (env, class);
2007-01-23 14:23:18 +00:00
if (handle == NULL)
2006-11-28 15:09:53 +00:00
{
2007-01-24 14:21:30 +00:00
/* internal error. no handle field
* NoSuchFieldError, ExceptionInitializerError,
* OutOfMemoryError might occur */
2006-11-28 15:09:53 +00:00
return;
}
2006-08-06 08:16:03 +00:00
2007-01-23 14:23:18 +00:00
awk = (ase_awk_t*) (*env)->GetLongField (env, obj, handle);
2006-08-06 08:16:03 +00:00
srcio_data.env = env;
srcio_data.obj = obj;
srcios.in = __read_source;
2006-08-06 15:03:42 +00:00
srcios.out = __write_source;
2006-08-06 08:16:03 +00:00
srcios.custom_data = &srcio_data;
2006-10-24 04:10:12 +00:00
if (ase_awk_parse (awk, &srcios) == -1)
2006-08-06 08:16:03 +00:00
{
2007-01-23 14:23:18 +00:00
throw_exception (
env,
ase_awk_geterrmsg(awk),
ase_awk_geterrnum(awk),
ase_awk_geterrlin(awk));
2006-11-26 15:55:44 +00:00
2006-08-06 08:16:03 +00:00
return;
}
}
2007-01-31 09:31:29 +00:00
#define DELETE_CLASS_REFS(env, run_data) \
do { \
(*env)->DeleteLocalRef (env, run_data.integer_class); \
(*env)->DeleteLocalRef (env, run_data.long_class); \
(*env)->DeleteLocalRef (env, run_data.float_class); \
(*env)->DeleteLocalRef (env, run_data.double_class); \
(*env)->DeleteLocalRef (env, run_data.string_class); \
(*env)->DeleteLocalRef (env, run_data.object_class); \
} while (0)
2007-02-01 07:24:00 +00:00
static ase_char_t* java_strxdup (jchar* str, jint len)
{
if (len > 0 && ASE_SIZEOF(jchar) != ASE_SIZEOF(ase_char_t))
{
ase_char_t* tmp;
ase_size_t i;
tmp = (ase_char_t*) malloc ((len+1) * ASE_SIZEOF(ase_char_t));
if (tmp == ASE_NULL) return ASE_NULL;
for (i = 0; i < (ase_size_t)len; i++) tmp[i] = (ase_char_t)str[i];
tmp[i] = ASE_T('\0');
return tmp;
}
else
{
ase_char_t* tmp;
tmp = (ase_char_t*) malloc ((len+1) * ASE_SIZEOF(ase_char_t));
if (tmp == ASE_NULL) return ASE_NULL;
2007-02-23 08:54:03 +00:00
ase_strncpy (tmp, (ase_char_t*)str, (ase_size_t)len);
2007-02-01 07:24:00 +00:00
return tmp;
}
}
JNIEXPORT void JNICALL Java_ase_awk_Awk_run (JNIEnv* env, jobject obj, jstring mfn, jobjectArray args)
2006-08-06 08:16:03 +00:00
{
2006-10-13 10:18:39 +00:00
jclass class;
2007-01-23 14:23:18 +00:00
jfieldID handle;
2006-10-13 10:18:39 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_t* awk;
ase_awk_runios_t runios;
2006-10-13 10:18:39 +00:00
runio_data_t runio_data;
2006-11-28 04:30:57 +00:00
run_data_t run_data;
2007-01-31 09:31:29 +00:00
ase_char_t* mmm;
2006-10-13 10:18:39 +00:00
2007-02-01 07:24:00 +00:00
ase_size_t len, i;
2007-01-31 09:31:29 +00:00
jchar* ptr;
2007-01-23 14:23:18 +00:00
2007-02-01 07:24:00 +00:00
ase_awk_runarg_t* runarg = NULL;
2006-10-13 10:18:39 +00:00
class = (*env)->GetObjectClass (env, obj);
2007-01-23 14:23:18 +00:00
handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
2006-11-28 15:09:53 +00:00
(*env)->DeleteLocalRef (env, class);
2007-01-23 14:23:18 +00:00
if (handle == 0)
2006-11-27 04:33:22 +00:00
{
2007-01-24 14:21:30 +00:00
/* internal error. no handle field
* NoSuchFieldError, ExceptionInitializerError,
* OutOfMemoryError might occur */
2006-11-27 04:33:22 +00:00
return;
}
2006-10-13 10:18:39 +00:00
2007-01-23 14:23:18 +00:00
awk = (ase_awk_t*) (*env)->GetLongField (env, obj, handle);
2006-10-13 10:18:39 +00:00
2006-11-28 04:30:57 +00:00
run_data.env = env;
run_data.obj = obj;
2006-11-29 11:41:15 +00:00
/* would global reference be necessary? */
run_data.string_class = (*env)->FindClass (env, "java/lang/String");
run_data.integer_class = (*env)->FindClass (env, "java/lang/Integer");
2006-11-29 14:52:36 +00:00
run_data.long_class = (*env)->FindClass (env, "java/lang/Long");
run_data.short_class = (*env)->FindClass (env, "java/lang/Short");
run_data.float_class = (*env)->FindClass (env, "java/lang/Float");
2006-11-29 11:41:15 +00:00
run_data.double_class = (*env)->FindClass (env, "java/lang/Double");
run_data.object_class = (*env)->FindClass (env, "java/lang/Object");
2007-03-06 14:58:00 +00:00
ASE_ASSERT (run_data.string_class != NULL);
ASE_ASSERT (run_data.integer_class != NULL);
ASE_ASSERT (run_data.short_class != NULL);
ASE_ASSERT (run_data.long_class != NULL);
ASE_ASSERT (run_data.float_class != NULL);
ASE_ASSERT (run_data.double_class != NULL);
ASE_ASSERT (run_data.object_class != NULL);
2006-11-29 11:41:15 +00:00
2006-11-29 14:52:36 +00:00
run_data.integer_init = (*env)->GetMethodID (
env, run_data.integer_class, "<init>", "(I)V");
run_data.long_init = (*env)->GetMethodID (
env, run_data.long_class, "<init>", "(J)V");
run_data.short_init = (*env)->GetMethodID (
env, run_data.short_class, "<init>", "(S)V");
run_data.float_init = (*env)->GetMethodID (
env, run_data.float_class, "<init>", "(F)V");
run_data.double_init = (*env)->GetMethodID (
env, run_data.double_class, "<init>", "(D)V");
2007-03-06 14:58:00 +00:00
ASE_ASSERT (run_data.integer_init != NULL);
ASE_ASSERT (run_data.long_init != NULL);
ASE_ASSERT (run_data.short_init != NULL);
ASE_ASSERT (run_data.float_init != NULL);
ASE_ASSERT (run_data.double_init != NULL);
2006-11-29 14:52:36 +00:00
run_data.integer_value = (*env)->GetMethodID (
env, run_data.integer_class, "intValue", "()I");
run_data.long_value = (*env)->GetMethodID (
env, run_data.long_class, "longValue", "()J");
run_data.short_value = (*env)->GetMethodID (
env, run_data.short_class, "shortValue", "()S");
run_data.float_value = (*env)->GetMethodID (
env, run_data.float_class, "floatValue", "()F");
run_data.double_value = (*env)->GetMethodID (
env, run_data.double_class, "doubleValue", "()D");
2007-03-06 14:58:00 +00:00
ASE_ASSERTX (run_data.integer_value != NULL,
2006-11-29 14:52:36 +00:00
"The Integer class must has the method - int intValue()");
2007-03-06 14:58:00 +00:00
ASE_ASSERTX (run_data.long_value != NULL,
2006-11-29 14:52:36 +00:00
"The Long class must has the method - long longValue()");
2007-03-06 14:58:00 +00:00
ASE_ASSERTX (run_data.short_value != NULL,
2006-11-29 14:52:36 +00:00
"The Short class must has the method - short shortValue()");
2007-03-06 14:58:00 +00:00
ASE_ASSERTX (run_data.float_value != NULL,
2006-11-29 14:52:36 +00:00
"The Float class must has the method - float floatValue()");
2007-03-06 14:58:00 +00:00
ASE_ASSERTX (run_data.double_value != NULL,
2006-11-29 14:52:36 +00:00
"The Double class must has the method - double doubleValue()");
2006-10-13 10:18:39 +00:00
runio_data.env = env;
runio_data.obj = obj;
2006-11-24 15:07:18 +00:00
runios.pipe = __process_extio;
2006-10-24 04:10:12 +00:00
runios.coproc = ASE_NULL;
2006-11-21 15:06:51 +00:00
runios.file = __process_extio;
runios.console = __process_extio;
2006-10-13 10:18:39 +00:00
runios.custom_data = &runio_data;
2007-01-31 09:31:29 +00:00
if (mfn == NULL)
{
mmm = NULL;
ptr = NULL;
}
else
{
2007-02-01 07:24:00 +00:00
/* process the main entry point */
2007-01-31 09:31:29 +00:00
len = (*env)->GetStringLength (env, mfn);
2007-02-23 08:54:03 +00:00
if (len > 0)
2007-01-31 09:31:29 +00:00
{
ase_size_t i;
ptr = (*env)->GetStringChars (env, mfn, JNI_FALSE);
if (ptr == NULL)
{
(*env)->ExceptionClear (env);
DELETE_CLASS_REFS (env, run_data);
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
2007-01-31 09:31:29 +00:00
ASE_AWK_ENOMEM,
0);
return;
}
mmm = (ase_char_t*) malloc (ASE_SIZEOF(ase_char_t)*(len+1));
if (mmm == ASE_NULL)
{
(*env)->ReleaseStringChars (env, mfn, ptr);
DELETE_CLASS_REFS (env, run_data);
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
2007-01-31 09:31:29 +00:00
ASE_AWK_ENOMEM,
0);
return;
}
for (i = 0; i < len; i++)
{
mmm[i] = (ase_char_t)ptr[i];
if (mmm[i] == ASE_T('\0'))
{
free (mmm);
(*env)->ReleaseStringChars (env, mfn, ptr);
DELETE_CLASS_REFS (env, run_data);
throw_exception (
env,
ASE_T("main function name not valid"),
ASE_AWK_EINVAL,
0);
return;
}
}
mmm[len] = ASE_T('\0');
}
else
{
2007-02-23 08:54:03 +00:00
mmm = NULL;
2007-01-31 09:31:29 +00:00
ptr = NULL;
}
}
2007-02-01 07:24:00 +00:00
if (args != NULL)
{
/* compose arguments */
len = (*env)->GetArrayLength (env, args);
runarg = malloc (sizeof(ase_awk_runarg_t) * (len+1));
if (runarg == NULL)
{
2007-02-23 08:54:03 +00:00
if (mmm != NULL) free (mmm);
2007-02-01 07:24:00 +00:00
if (ptr != NULL) (*env)->ReleaseStringChars (env, mfn, ptr);
DELETE_CLASS_REFS (env, run_data);
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
2007-02-01 07:24:00 +00:00
ASE_AWK_ENOMEM,
0);
return;
}
for (i = 0; i < len; i++)
{
jchar* tmp;
jstring obj = (jstring)(*env)->GetObjectArrayElement (env, args, i);
runarg[i].len = (*env)->GetStringLength (env, obj);
tmp = (*env)->GetStringChars (env, obj, JNI_FALSE);
if (tmp == NULL)
{
ase_size_t j;
for (j = 0; j < i; j++) free (runarg[j].ptr);
free (runarg);
(*env)->DeleteLocalRef (env, obj);
2007-02-23 08:54:03 +00:00
if (mmm != NULL && mmm) free (mmm);
2007-02-01 07:24:00 +00:00
if (ptr != NULL) (*env)->ReleaseStringChars (env, mfn, ptr);
DELETE_CLASS_REFS (env, run_data);
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
2007-02-01 07:24:00 +00:00
ASE_AWK_ENOMEM,
0);
return;
}
runarg[i].ptr = java_strxdup (tmp, runarg[i].len);
if (runarg[i].ptr == NULL)
{
ase_size_t j;
for (j = 0; j < i; j++) free (runarg[j].ptr);
free (runarg);
(*env)->ReleaseStringChars (env, obj, tmp);
(*env)->DeleteLocalRef (env, obj);
2007-02-23 08:54:03 +00:00
if (mmm != NULL) free (mmm);
2007-02-01 07:24:00 +00:00
if (ptr != NULL) (*env)->ReleaseStringChars (env, mfn, ptr);
DELETE_CLASS_REFS (env, run_data);
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
2007-02-01 07:24:00 +00:00
ASE_AWK_ENOMEM,
0);
return;
}
(*env)->ReleaseStringChars (env, obj, tmp);
(*env)->DeleteLocalRef (env, obj);
}
runarg[i].ptr = NULL;
runarg[i].len = 0;
}
2006-11-28 04:30:57 +00:00
if (ase_awk_run (awk,
2007-02-01 07:24:00 +00:00
mmm, &runios, ASE_NULL, runarg, &run_data) == -1)
2006-10-13 10:18:39 +00:00
{
2007-02-01 07:24:00 +00:00
if (runarg != NULL)
{
for (i = 0; i < len; i++) free (runarg[i].ptr);
free (runarg);
}
2007-02-23 08:54:03 +00:00
if (mmm != NULL) free (mmm);
2007-01-31 09:31:29 +00:00
if (ptr != NULL) (*env)->ReleaseStringChars (env, mfn, ptr);
DELETE_CLASS_REFS (env, run_data);
2007-01-23 14:23:18 +00:00
throw_exception (
env,
ase_awk_geterrmsg(awk),
ase_awk_geterrnum(awk),
ase_awk_geterrlin(awk));
2006-10-13 10:18:39 +00:00
return;
}
2006-11-27 04:33:22 +00:00
2007-02-01 07:24:00 +00:00
if (runarg != NULL)
{
for (i = 0; i < len; i++) free (runarg[i].ptr);
free (runarg);
}
2007-02-23 08:54:03 +00:00
if (mmm != NULL) free (mmm);
2007-01-31 09:31:29 +00:00
if (ptr != NULL) (*env)->ReleaseStringChars (env, mfn, ptr);
DELETE_CLASS_REFS (env, run_data);
2006-08-06 08:16:03 +00:00
}
2006-11-26 15:55:44 +00:00
static ase_ssize_t __java_open_source (JNIEnv* env, jobject obj, int mode)
2006-08-06 08:16:03 +00:00
{
2006-10-13 10:18:39 +00:00
jclass class;
2007-01-25 14:14:56 +00:00
jfieldID handle;
2006-10-13 10:18:39 +00:00
jmethodID mid;
jint ret;
2007-01-25 14:14:56 +00:00
ase_awk_t* awk;
2006-10-13 10:18:39 +00:00
class = (*env)->GetObjectClass(env, obj);
2007-01-25 14:14:56 +00:00
handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
2006-11-26 15:55:44 +00:00
mid = (*env)->GetMethodID (env, class, "openSource", "(I)I");
2006-11-28 15:09:53 +00:00
(*env)->DeleteLocalRef (env, class);
2007-01-25 14:14:56 +00:00
if (handle == NULL)
{
(*env)->ExceptionClear (env);
return -1;
}
awk = (ase_awk_t*)(*env)->GetLongField (env, obj, handle);
2006-11-21 15:06:51 +00:00
if (mid == NULL)
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
2006-11-21 15:06:51 +00:00
return -1;
}
2006-10-12 14:36:25 +00:00
2006-10-13 10:18:39 +00:00
ret = (*env)->CallIntMethod (env, obj, mid, mode);
2007-01-25 14:14:56 +00:00
if ((*env)->ExceptionOccurred (env))
2006-08-06 08:16:03 +00:00
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
2006-10-13 10:18:39 +00:00
(*env)->ExceptionClear (env);
ret = -1;
2006-08-06 08:16:03 +00:00
}
2006-08-06 15:03:42 +00:00
2006-10-13 10:18:39 +00:00
return ret;
2006-08-06 15:03:42 +00:00
}
2006-11-26 15:55:44 +00:00
static ase_ssize_t __java_close_source (JNIEnv* env, jobject obj, int mode)
2006-08-06 15:03:42 +00:00
{
2006-10-13 10:18:39 +00:00
jclass class;
2007-01-25 14:14:56 +00:00
jfieldID handle;
2006-10-13 10:18:39 +00:00
jmethodID mid;
jint ret;
2007-01-25 14:14:56 +00:00
ase_awk_t* awk;
2006-10-13 10:18:39 +00:00
class = (*env)->GetObjectClass(env, obj);
2007-01-25 14:14:56 +00:00
handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
2006-11-26 15:55:44 +00:00
mid = (*env)->GetMethodID (env, class, "closeSource", "(I)I");
2006-11-28 15:09:53 +00:00
(*env)->DeleteLocalRef (env, class);
2007-01-25 14:14:56 +00:00
if (handle == NULL)
{
(*env)->ExceptionClear (env);
return -1;
}
awk = (ase_awk_t*)(*env)->GetLongField (env, obj, handle);
2006-11-21 15:06:51 +00:00
if (mid == NULL)
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
2006-11-21 15:06:51 +00:00
return -1;
}
2006-08-06 15:03:42 +00:00
2006-10-13 10:18:39 +00:00
ret = (*env)->CallIntMethod (env, obj, mid, mode);
2007-01-25 14:14:56 +00:00
if ((*env)->ExceptionOccurred (env))
2006-08-06 08:16:03 +00:00
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
2006-10-13 10:18:39 +00:00
(*env)->ExceptionClear (env);
ret = -1;
2006-08-06 08:16:03 +00:00
}
2006-08-06 15:03:42 +00:00
2006-10-13 10:18:39 +00:00
return ret;
2006-08-06 08:16:03 +00:00
}
2006-11-26 15:55:44 +00:00
static ase_ssize_t __java_read_source (
2006-10-24 04:10:12 +00:00
JNIEnv* env, jobject obj, ase_char_t* buf, ase_size_t size)
2006-08-06 08:16:03 +00:00
{
jclass class;
2007-01-25 14:14:56 +00:00
jfieldID handle;
2006-08-06 08:16:03 +00:00
jmethodID mid;
jcharArray array;
jchar* tmp;
2006-10-13 10:18:39 +00:00
jint ret, i;
2007-01-25 14:14:56 +00:00
ase_awk_t* awk;
2006-08-06 08:16:03 +00:00
class = (*env)->GetObjectClass(env, obj);
2007-01-25 14:14:56 +00:00
handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
2006-11-26 15:55:44 +00:00
mid = (*env)->GetMethodID (env, class, "readSource", "([CI)I");
2006-11-28 15:09:53 +00:00
(*env)->DeleteLocalRef (env, class);
2007-01-25 14:14:56 +00:00
if (handle == NULL)
{
(*env)->ExceptionClear (env);
return -1;
}
awk = (ase_awk_t*)(*env)->GetLongField (env, obj, handle);
2006-11-21 15:06:51 +00:00
if (mid == NULL)
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
2006-11-21 15:06:51 +00:00
return -1;
}
2006-08-06 08:16:03 +00:00
2006-10-13 10:18:39 +00:00
array = (*env)->NewCharArray (env, size);
2006-11-21 15:06:51 +00:00
if (array == NULL)
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
2006-11-21 15:06:51 +00:00
return -1;
}
2006-08-06 08:16:03 +00:00
2006-10-13 10:18:39 +00:00
ret = (*env)->CallIntMethod (env, obj, mid, array, size);
2007-01-25 14:14:56 +00:00
if ((*env)->ExceptionOccurred (env))
2006-10-13 10:18:39 +00:00
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
2006-10-13 10:18:39 +00:00
(*env)->ExceptionClear (env);
ret = -1;
}
2006-08-06 08:16:03 +00:00
tmp = (*env)->GetCharArrayElements (env, array, 0);
2006-10-24 04:10:12 +00:00
for (i = 0; i < ret; i++) buf[i] = (ase_char_t)tmp[i];
2006-08-06 08:16:03 +00:00
(*env)->ReleaseCharArrayElements (env, array, tmp, 0);
2006-10-22 07:05:34 +00:00
(*env)->DeleteLocalRef (env, array);
2006-08-06 08:16:03 +00:00
return i;
2006-08-02 15:03:49 +00:00
}
2006-08-06 15:03:42 +00:00
2006-11-26 15:55:44 +00:00
static ase_ssize_t __java_write_source (
2006-10-24 04:10:12 +00:00
JNIEnv* env, jobject obj, ase_char_t* buf, ase_size_t size)
2006-08-06 15:03:42 +00:00
{
jclass class;
2007-01-25 14:14:56 +00:00
jfieldID handle;
2006-08-06 15:03:42 +00:00
jmethodID mid;
jcharArray array;
jchar* tmp;
2006-12-12 05:44:42 +00:00
jint ret;
ase_size_t i;
2007-01-25 14:14:56 +00:00
ase_awk_t* awk;
2006-08-06 15:03:42 +00:00
class = (*env)->GetObjectClass(env, obj);
2007-01-25 14:14:56 +00:00
handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
2006-11-26 15:55:44 +00:00
mid = (*env)->GetMethodID (env, class, "writeSource", "([CI)I");
2006-11-28 15:09:53 +00:00
(*env)->DeleteLocalRef (env, class);
2007-01-25 14:14:56 +00:00
if (handle == NULL)
2006-11-21 15:06:51 +00:00
{
2007-01-25 14:14:56 +00:00
(*env)->ExceptionClear (env);
return -1;
}
awk = (ase_awk_t*)(*env)->GetLongField (env, obj, handle);
if (mid == NULL)
{
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
2006-11-21 15:06:51 +00:00
return -1;
}
2006-08-06 15:03:42 +00:00
array = (*env)->NewCharArray (env, size);
2006-11-21 15:06:51 +00:00
if (array == NULL)
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
2006-11-21 15:06:51 +00:00
return -1;
}
2006-08-06 15:03:42 +00:00
tmp = (*env)->GetCharArrayElements (env, array, 0);
for (i = 0; i < size; i++) tmp[i] = (jchar)buf[i];
(*env)->ReleaseCharArrayElements (env, array, tmp, 0);
2006-10-13 10:18:39 +00:00
ret = (*env)->CallIntMethod (env, obj, mid, array, size);
2007-01-25 14:14:56 +00:00
if ((*env)->ExceptionOccurred (env))
2006-10-13 10:18:39 +00:00
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
2006-10-13 10:18:39 +00:00
(*env)->ExceptionClear (env);
ret = -1;
}
2006-10-22 07:05:34 +00:00
(*env)->DeleteLocalRef (env, array);
2006-10-13 10:18:39 +00:00
return ret;
}
2006-11-26 15:55:44 +00:00
static ase_ssize_t __java_open_extio (
2006-10-24 04:10:12 +00:00
JNIEnv* env, jobject obj, char* meth, ase_awk_extio_t* extio)
2006-10-13 10:18:39 +00:00
{
jclass class;
2007-01-25 14:14:56 +00:00
jfieldID handle;
2006-10-13 10:18:39 +00:00
jmethodID mid;
2006-11-21 15:06:51 +00:00
jclass extio_class;
jmethodID extio_cons;
jobject extio_object;
jstring extio_name;
2006-10-13 10:18:39 +00:00
jint ret;
2007-01-25 14:14:56 +00:00
ase_awk_t* awk;
2007-01-31 08:23:59 +00:00
ase_size_t len;
2006-10-13 10:18:39 +00:00
2007-01-25 14:14:56 +00:00
/* get the method - meth */
class = (*env)->GetObjectClass(env, obj);
handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
mid = (*env)->GetMethodID (env, class, meth, "(Lase/awk/Extio;)I");
(*env)->DeleteLocalRef (env, class);
2006-10-13 14:05:24 +00:00
2007-01-25 14:14:56 +00:00
if (handle == NULL)
2006-11-21 15:06:51 +00:00
{
2007-01-25 14:14:56 +00:00
(*env)->ExceptionClear (env);
2006-11-21 15:06:51 +00:00
return -1;
}
2007-01-25 14:14:56 +00:00
awk = (ase_awk_t*)(*env)->GetLongField (env, obj, handle);
2006-10-22 07:05:34 +00:00
2006-11-21 15:06:51 +00:00
if (mid == NULL)
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
return -1;
}
/* look for extio class */
extio_class = (*env)->FindClass (env, CLASS_EXTIO);
if (extio_class == NULL)
{
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
return -1;
}
/* get the constructor */
extio_cons = (*env)->GetMethodID (
env, extio_class, "<init>", "(Ljava/lang/String;IIJ)V");
if (extio_cons == NULL)
{
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
2006-11-21 15:06:51 +00:00
(*env)->DeleteLocalRef (env, extio_class);
return -1;
}
2006-10-22 07:05:34 +00:00
2006-11-21 15:06:51 +00:00
/* construct the name */
2007-02-23 08:17:51 +00:00
len = ase_strlen(extio->name);
2007-01-31 08:23:59 +00:00
if (len > 0 && ASE_SIZEOF(jchar) != ASE_SIZEOF(ase_char_t))
2007-01-21 13:21:14 +00:00
{
2007-01-31 08:23:59 +00:00
ase_size_t i;
2007-01-23 14:23:18 +00:00
jchar* tmp = (jchar*) malloc (ASE_SIZEOF(jchar)*len);
2007-01-21 13:21:14 +00:00
if (tmp == NULL)
{
(*env)->DeleteLocalRef (env, extio_class);
return -1;
}
for (i = 0; i < len; i++) tmp[i] = (jchar)extio->name[i];
extio_name = (*env)->NewString (env, tmp, len);
free (tmp);
}
else
{
2007-01-31 08:23:59 +00:00
extio_name = (*env)->NewString (env, (jchar*)extio->name, len);
2007-01-21 13:21:14 +00:00
}
2006-11-21 15:06:51 +00:00
if (extio_name == NULL)
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
2006-11-21 15:06:51 +00:00
(*env)->DeleteLocalRef (env, extio_class);
return -1;
}
2006-10-22 07:05:34 +00:00
2006-11-21 15:06:51 +00:00
/* construct the extio object */
extio_object = (*env)->NewObject (
env, extio_class, extio_cons,
2007-01-31 08:23:59 +00:00
extio_name, extio->type & 0xFF, extio->mode, (jlong)extio->run);
2006-11-28 15:09:53 +00:00
(*env)->DeleteLocalRef (env, extio_class);
2006-11-21 15:06:51 +00:00
if (extio_object == NULL)
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
2006-11-21 15:06:51 +00:00
(*env)->DeleteLocalRef (env, extio_name);
return -1;
2006-10-13 14:05:24 +00:00
}
2006-10-13 10:18:39 +00:00
2006-11-28 15:09:53 +00:00
(*env)->DeleteLocalRef (env, extio_name);
2006-11-21 15:06:51 +00:00
/* execute the method */
ret = (*env)->CallIntMethod (env, obj, mid, extio_object);
2007-01-23 14:23:18 +00:00
if ((*env)->ExceptionOccurred(env))
2006-10-13 10:18:39 +00:00
{
2007-01-23 14:23:18 +00:00
/* clear the exception */
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
2006-10-13 10:18:39 +00:00
(*env)->ExceptionClear (env);
2006-11-28 15:09:53 +00:00
ret = -1;
2006-11-21 15:06:51 +00:00
}
2006-12-09 17:36:27 +00:00
if (ret >= 0)
2006-11-21 15:06:51 +00:00
{
/* ret == -1 failed to open the stream
* ret == 0 opened the stream and reached its end
* ret == 1 opened the stream. */
extio->handle = (*env)->NewGlobalRef (env, extio_object);
2007-01-25 14:14:56 +00:00
/*
2006-11-29 11:41:15 +00:00
if (extio->handle == NULL)
{
2007-01-25 14:14:56 +00:00
// TODO: close the stream ...
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
2006-11-29 11:41:15 +00:00
ret = -1;
}
*/
2006-10-13 10:18:39 +00:00
}
2006-11-21 15:06:51 +00:00
(*env)->DeleteLocalRef (env, extio_object);
2006-10-13 10:18:39 +00:00
return ret;
}
2006-11-26 15:55:44 +00:00
static ase_ssize_t __java_close_extio (
2006-10-24 04:10:12 +00:00
JNIEnv* env, jobject obj, char* meth, ase_awk_extio_t* extio)
2006-10-13 10:18:39 +00:00
{
jclass class;
2007-01-25 14:14:56 +00:00
jfieldID handle;
2006-10-13 10:18:39 +00:00
jmethodID mid;
jint ret;
2007-01-25 14:14:56 +00:00
ase_awk_t* awk;
2006-10-13 10:18:39 +00:00
class = (*env)->GetObjectClass(env, obj);
2007-01-25 14:14:56 +00:00
handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
2006-11-28 15:09:53 +00:00
mid = (*env)->GetMethodID (env, class, meth, "(Lase/awk/Extio;)I");
(*env)->DeleteLocalRef (env, class);
2007-01-25 14:14:56 +00:00
if (handle == NULL)
{
(*env)->ExceptionClear (env);
return -1;
}
awk = (ase_awk_t*)(*env)->GetLongField (env, obj, handle);
2006-11-21 15:06:51 +00:00
if (mid == NULL)
2006-10-13 14:05:24 +00:00
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
2006-11-21 15:06:51 +00:00
return -1;
2006-10-13 14:05:24 +00:00
}
2006-10-13 10:18:39 +00:00
2006-11-21 15:06:51 +00:00
ret = (*env)->CallIntMethod (env, obj, mid, extio->handle);
2007-01-23 14:23:18 +00:00
if ((*env)->ExceptionOccurred (env))
2006-10-13 10:18:39 +00:00
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
2006-10-13 10:18:39 +00:00
(*env)->ExceptionClear (env);
ret = -1;
}
2006-12-09 17:36:27 +00:00
if (ret >= 0)
2006-11-21 15:06:51 +00:00
{
/* ret == -1 failed to close the stream
* ret == 0 closed the stream */
(*env)->DeleteGlobalRef (env, extio->handle);
extio->handle = NULL;
}
2006-10-13 10:18:39 +00:00
return ret;
}
2006-11-26 15:55:44 +00:00
static ase_ssize_t __java_read_extio (
2006-11-21 15:06:51 +00:00
JNIEnv* env, jobject obj, char* meth,
ase_awk_extio_t* extio, ase_char_t* buf, ase_size_t size)
2006-10-13 10:18:39 +00:00
{
jclass class;
2007-01-25 14:14:56 +00:00
jfieldID handle;
2006-10-13 10:18:39 +00:00
jmethodID mid;
jcharArray array;
jchar* tmp;
jint ret, i;
2007-01-25 14:14:56 +00:00
ase_awk_t* awk;
2006-10-13 10:18:39 +00:00
class = (*env)->GetObjectClass(env, obj);
2007-01-25 14:14:56 +00:00
handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
2006-11-21 15:06:51 +00:00
mid = (*env)->GetMethodID (env, class, meth, "(Lase/awk/Extio;[CI)I");
2006-11-28 15:09:53 +00:00
(*env)->DeleteLocalRef (env, class);
2007-01-25 14:14:56 +00:00
if (handle == NULL)
{
(*env)->ExceptionClear (env);
return -1;
}
awk = (ase_awk_t*)(*env)->GetLongField (env, obj, handle);
2006-11-21 15:06:51 +00:00
if (mid == NULL)
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
2006-11-21 15:06:51 +00:00
return -1;
}
2006-10-13 10:18:39 +00:00
array = (*env)->NewCharArray (env, size);
2006-11-21 15:06:51 +00:00
if (array == NULL)
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
2006-11-21 15:06:51 +00:00
return -1;
}
2006-10-13 10:18:39 +00:00
2006-11-21 15:06:51 +00:00
ret = (*env)->CallIntMethod (env, obj, mid, extio->handle, array, size);
2007-01-25 14:14:56 +00:00
if ((*env)->ExceptionOccurred (env))
2006-10-13 10:18:39 +00:00
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
2006-10-13 10:18:39 +00:00
(*env)->ExceptionClear (env);
ret = -1;
}
2006-11-21 15:06:51 +00:00
if (ret > 0)
{
tmp = (*env)->GetCharArrayElements (env, array, 0);
for (i = 0; i < ret; i++) buf[i] = (ase_char_t)tmp[i];
(*env)->ReleaseCharArrayElements (env, array, tmp, 0);
}
2006-10-13 10:18:39 +00:00
2006-10-22 07:05:34 +00:00
(*env)->DeleteLocalRef (env, array);
2006-10-13 10:18:39 +00:00
return ret;
}
2006-11-26 15:55:44 +00:00
static ase_ssize_t __java_write_extio (
2006-11-21 15:06:51 +00:00
JNIEnv* env, jobject obj, char* meth,
ase_awk_extio_t* extio, ase_char_t* data, ase_size_t size)
2006-10-13 10:18:39 +00:00
{
jclass class;
2007-01-25 14:14:56 +00:00
jfieldID handle;
2006-10-13 10:18:39 +00:00
jmethodID mid;
jcharArray array;
jchar* tmp;
jint ret;
2006-12-12 05:44:42 +00:00
ase_size_t i;
2007-01-25 14:14:56 +00:00
ase_awk_t* awk;
2006-10-13 10:18:39 +00:00
class = (*env)->GetObjectClass(env, obj);
2007-01-25 14:14:56 +00:00
handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
2006-11-21 15:06:51 +00:00
mid = (*env)->GetMethodID (env, class, meth, "(Lase/awk/Extio;[CI)I");
2006-11-28 15:09:53 +00:00
(*env)->DeleteLocalRef (env, class);
2007-01-25 14:14:56 +00:00
if (handle == NULL)
{
(*env)->ExceptionClear (env);
return -1;
}
awk = (ase_awk_t*)(*env)->GetLongField (env, obj, handle);
2006-11-21 15:06:51 +00:00
if (mid == NULL)
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
2006-11-21 15:06:51 +00:00
return -1;
}
2006-10-13 10:18:39 +00:00
array = (*env)->NewCharArray (env, size);
2006-11-21 15:06:51 +00:00
if (array == NULL)
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
2006-11-21 15:06:51 +00:00
return -1;
}
2006-10-13 10:18:39 +00:00
tmp = (*env)->GetCharArrayElements (env, array, 0);
for (i = 0; i < size; i++) tmp[i] = (jchar)data[i];
(*env)->ReleaseCharArrayElements (env, array, tmp, 0);
2006-11-21 15:06:51 +00:00
ret = (*env)->CallIntMethod (env, obj, mid, extio->handle, array, size);
2007-01-25 14:14:56 +00:00
if ((*env)->ExceptionOccurred (env))
2006-10-13 10:18:39 +00:00
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
2006-10-13 10:18:39 +00:00
(*env)->ExceptionClear (env);
ret = -1;
}
2006-10-22 07:05:34 +00:00
(*env)->DeleteLocalRef (env, array);
2006-10-13 10:18:39 +00:00
return ret;
}
2006-11-24 15:07:18 +00:00
2006-11-26 15:55:44 +00:00
static ase_ssize_t __java_flush_extio (
2006-11-24 15:07:18 +00:00
JNIEnv* env, jobject obj, char* meth, ase_awk_extio_t* extio)
{
jclass class;
2007-01-25 14:14:56 +00:00
jfieldID handle;
2006-11-24 15:07:18 +00:00
jmethodID mid;
jint ret;
2007-01-25 14:14:56 +00:00
ase_awk_t* awk;
2006-11-24 15:07:18 +00:00
class = (*env)->GetObjectClass(env, obj);
2007-01-25 14:14:56 +00:00
handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
2006-11-28 15:09:53 +00:00
mid = (*env)->GetMethodID (env, class, meth, "(Lase/awk/Extio;)I");
(*env)->DeleteLocalRef (env, class);
2007-01-25 14:14:56 +00:00
if (handle == NULL)
{
(*env)->ExceptionClear (env);
return -1;
}
awk = (ase_awk_t*)(*env)->GetLongField (env, obj, handle);
2006-11-24 15:07:18 +00:00
if (mid == NULL)
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
2006-11-24 15:07:18 +00:00
return -1;
}
ret = (*env)->CallIntMethod (env, obj, mid, extio->handle);
2007-01-24 14:21:30 +00:00
if ((*env)->ExceptionOccurred (env))
2006-11-24 15:07:18 +00:00
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
2006-11-24 15:07:18 +00:00
(*env)->ExceptionClear (env);
ret = -1;
}
return ret;
}
2006-11-26 15:55:44 +00:00
static ase_ssize_t __java_next_extio (
2006-11-22 15:12:04 +00:00
JNIEnv* env, jobject obj, char* meth, ase_awk_extio_t* extio)
{
jclass class;
2007-01-25 14:14:56 +00:00
jfieldID handle;
2006-11-22 15:12:04 +00:00
jmethodID mid;
jint ret;
2007-01-25 14:14:56 +00:00
ase_awk_t* awk;
2006-11-22 15:12:04 +00:00
class = (*env)->GetObjectClass(env, obj);
2007-01-25 14:14:56 +00:00
handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
2006-11-28 15:09:53 +00:00
mid = (*env)->GetMethodID (env, class, meth, "(Lase/awk/Extio;)I");
(*env)->DeleteLocalRef (env, class);
2007-01-25 14:14:56 +00:00
if (handle == NULL)
{
(*env)->ExceptionClear (env);
return -1;
}
awk = (ase_awk_t*)(*env)->GetLongField (env, obj, handle);
2006-11-22 15:12:04 +00:00
if (mid == NULL)
{
2007-01-25 14:14:56 +00:00
(*env)->ExceptionClear (env);
2006-11-22 15:12:04 +00:00
return -1;
}
ret = (*env)->CallIntMethod (env, obj, mid, extio->handle);
2007-01-24 14:21:30 +00:00
if ((*env)->ExceptionOccurred (env))
2006-11-22 15:12:04 +00:00
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
2006-11-22 15:12:04 +00:00
(*env)->ExceptionClear (env);
ret = -1;
}
return ret;
}
2006-10-24 04:10:12 +00:00
static ase_ssize_t __read_source (
int cmd, void* arg, ase_char_t* data, ase_size_t count)
2006-10-13 10:18:39 +00:00
{
srcio_data_t* srcio_data = (srcio_data_t*)arg;
2006-10-24 04:10:12 +00:00
if (cmd == ASE_AWK_IO_OPEN)
2006-10-13 10:18:39 +00:00
{
2006-11-26 15:55:44 +00:00
return __java_open_source (
2006-10-13 10:18:39 +00:00
srcio_data->env, srcio_data->obj, SOURCE_READ);
}
2006-10-24 04:10:12 +00:00
else if (cmd == ASE_AWK_IO_CLOSE)
2006-10-13 10:18:39 +00:00
{
2006-11-26 15:55:44 +00:00
return __java_close_source (
2006-10-13 10:18:39 +00:00
srcio_data->env, srcio_data->obj, SOURCE_READ);
}
2006-10-24 04:10:12 +00:00
else if (cmd == ASE_AWK_IO_READ)
2006-10-13 10:18:39 +00:00
{
2006-11-26 15:55:44 +00:00
return __java_read_source (
2006-10-13 10:18:39 +00:00
srcio_data->env, srcio_data->obj, data, count);
}
return -1;
}
2006-10-24 04:10:12 +00:00
static ase_ssize_t __write_source (
int cmd, void* arg, ase_char_t* data, ase_size_t count)
2006-10-13 10:18:39 +00:00
{
srcio_data_t* srcio_data = (srcio_data_t*)arg;
2006-10-24 04:10:12 +00:00
if (cmd == ASE_AWK_IO_OPEN)
2006-10-13 10:18:39 +00:00
{
2006-11-26 15:55:44 +00:00
return __java_open_source (
2006-10-13 10:18:39 +00:00
srcio_data->env, srcio_data->obj, SOURCE_WRITE);
}
2006-10-24 04:10:12 +00:00
else if (cmd == ASE_AWK_IO_CLOSE)
2006-10-13 10:18:39 +00:00
{
2006-11-26 15:55:44 +00:00
return __java_close_source (
2006-10-13 10:18:39 +00:00
srcio_data->env, srcio_data->obj, SOURCE_WRITE);
}
2006-10-24 04:10:12 +00:00
else if (cmd == ASE_AWK_IO_WRITE)
2006-10-13 10:18:39 +00:00
{
2006-11-26 15:55:44 +00:00
return __java_write_source (
2006-10-13 10:18:39 +00:00
srcio_data->env, srcio_data->obj, data, count);
}
return -1;
}
2006-11-21 15:06:51 +00:00
static ase_ssize_t __process_extio (
2006-10-24 04:10:12 +00:00
int cmd, void* arg, ase_char_t* data, ase_size_t size)
2006-10-13 10:18:39 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_extio_t* epa = (ase_awk_extio_t*)arg;
2006-10-13 10:18:39 +00:00
runio_data_t* runio_data = (runio_data_t*)epa->custom_data;
2006-10-24 04:10:12 +00:00
if (cmd == ASE_AWK_IO_OPEN)
2006-10-13 10:18:39 +00:00
{
2006-11-26 15:55:44 +00:00
return __java_open_extio (
2006-10-13 14:05:24 +00:00
runio_data->env, runio_data->obj,
2006-11-26 15:55:44 +00:00
"openExtio", epa);
2006-10-13 10:18:39 +00:00
}
2006-10-24 04:10:12 +00:00
else if (cmd == ASE_AWK_IO_CLOSE)
2006-10-13 10:18:39 +00:00
{
2006-11-26 15:55:44 +00:00
return __java_close_extio (
2006-10-13 14:05:24 +00:00
runio_data->env, runio_data->obj,
2006-11-26 15:55:44 +00:00
"closeExtio", epa);
2006-10-13 10:18:39 +00:00
}
2006-10-24 04:10:12 +00:00
else if (cmd == ASE_AWK_IO_READ)
2006-10-13 10:18:39 +00:00
{
2006-11-26 15:55:44 +00:00
return __java_read_extio (
2006-11-21 15:06:51 +00:00
runio_data->env, runio_data->obj,
2006-11-26 15:55:44 +00:00
"readExtio", epa, data, size);
2006-10-13 10:18:39 +00:00
}
2006-10-24 04:10:12 +00:00
else if (cmd == ASE_AWK_IO_WRITE)
2006-10-13 10:18:39 +00:00
{
2006-11-26 15:55:44 +00:00
return __java_write_extio (
2006-11-21 15:06:51 +00:00
runio_data->env, runio_data->obj,
2006-11-26 15:55:44 +00:00
"writeExtio", epa, data, size);
2006-10-13 10:18:39 +00:00
}
2006-11-24 15:07:18 +00:00
else if (cmd == ASE_AWK_IO_FLUSH)
{
2006-11-26 15:55:44 +00:00
return __java_flush_extio (
2006-11-24 15:07:18 +00:00
runio_data->env, runio_data->obj,
2006-11-26 15:55:44 +00:00
"flushExtio", epa);
2006-11-24 15:07:18 +00:00
}
2006-11-22 15:12:04 +00:00
else if (cmd == ASE_AWK_IO_NEXT)
{
2006-11-26 15:55:44 +00:00
return __java_next_extio (
2006-11-22 15:12:04 +00:00
runio_data->env, runio_data->obj,
2006-11-26 15:55:44 +00:00
"nextExtio", epa);
2006-11-22 15:12:04 +00:00
}
2006-10-13 14:05:24 +00:00
return -1;
}
2006-11-22 15:12:04 +00:00
2006-11-28 04:30:57 +00:00
static int __handle_bfn (
ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl)
2006-11-27 04:33:22 +00:00
{
jclass class;
2006-11-29 14:52:36 +00:00
jmethodID method;
2006-11-28 15:09:53 +00:00
jstring name;
const char* name_utf;
2006-11-28 04:30:57 +00:00
run_data_t* run_data;
JNIEnv* env;
jobject obj;
2006-11-29 11:41:15 +00:00
jint i, nargs;
2006-11-28 15:09:53 +00:00
jobjectArray args;
2006-11-29 11:41:15 +00:00
jobject arg, ret;
2006-11-28 15:09:53 +00:00
ase_awk_val_t* v;
2007-01-25 14:14:56 +00:00
ase_awk_t* awk;
2006-11-27 04:33:22 +00:00
2006-11-28 04:30:57 +00:00
run_data = ase_awk_getruncustomdata (run);
2006-11-28 15:09:53 +00:00
nargs = ase_awk_getnargs (run);
2007-01-25 14:14:56 +00:00
awk = ase_awk_getrunawk (run);
2006-11-28 04:30:57 +00:00
env = run_data->env;
obj = run_data->obj;
2007-01-31 08:23:59 +00:00
if (fnl > 0 && ASE_SIZEOF(jchar) != ASE_SIZEOF(ase_char_t))
2007-01-21 13:21:14 +00:00
{
ase_size_t i;
2007-01-25 14:14:56 +00:00
jchar* tmp = (jchar*) malloc (ASE_SIZEOF(jchar)*fnl);
2007-01-21 13:21:14 +00:00
if (tmp == NULL)
{
2007-03-10 15:32:54 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
2007-01-21 13:21:14 +00:00
return -1;
}
for (i = 0; i < fnl; i++) tmp[i] = (jchar)fnm[i];
name = (*env)->NewString (env, tmp, fnl);
free (tmp);
}
2007-01-31 08:23:59 +00:00
else
{
name = (*env)->NewString (env, (jchar*)fnm, fnl);
}
2007-01-21 13:21:14 +00:00
2006-11-28 15:09:53 +00:00
if (name == NULL)
2006-11-28 04:30:57 +00:00
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
2007-01-24 14:21:30 +00:00
(*env)->ExceptionClear (env);
2007-03-10 15:32:54 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
2006-11-28 04:30:57 +00:00
return -1;
}
2006-11-28 15:09:53 +00:00
name_utf = (*env)->GetStringUTFChars (env, name, JNI_FALSE);
if (name_utf == NULL)
2006-11-28 04:30:57 +00:00
{
2006-11-28 15:09:53 +00:00
(*env)->DeleteLocalRef (env, name);
2007-03-10 15:32:54 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
2006-11-28 04:30:57 +00:00
return -1;
}
2006-11-27 04:33:22 +00:00
2006-11-28 15:09:53 +00:00
class = (*env)->GetObjectClass(env, obj);
method = (*env)->GetMethodID (
env, class, name_utf,
2006-12-02 16:26:29 +00:00
"(J[Ljava/lang/Object;)Ljava/lang/Object;");
2006-11-28 15:09:53 +00:00
(*env)->DeleteLocalRef (env, class);
(*env)->ReleaseStringUTFChars (env, name, name_utf);
(*env)->DeleteLocalRef (env, name);
if (method == NULL)
2006-11-28 04:30:57 +00:00
{
2006-11-29 14:52:36 +00:00
/* if the method is not found, the exception is thrown.
2007-01-25 14:14:56 +00:00
* clear it to prevent it from being thrown */
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
2007-01-24 14:21:30 +00:00
(*env)->ExceptionClear (env);
2007-01-02 12:25:18 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_EBFNUSER);
2006-11-28 04:30:57 +00:00
return -1;
}
2006-11-29 11:41:15 +00:00
args = (*env)->NewObjectArray (
env, nargs, run_data->object_class, NULL);
2006-11-28 15:09:53 +00:00
if (args == NULL)
2006-11-27 04:33:22 +00:00
{
2007-01-25 14:14:56 +00:00
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
2007-03-10 15:32:54 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
2006-11-27 04:33:22 +00:00
return -1;
}
2006-11-28 15:09:53 +00:00
for (i = 0; i < nargs; i++)
{
v = ase_awk_getarg (run, i);
2006-11-29 11:41:15 +00:00
arg = NULL;
if (v->type == ASE_AWK_VAL_INT)
2006-11-28 15:09:53 +00:00
{
2006-11-29 14:52:36 +00:00
jlong jv;
2006-11-29 11:41:15 +00:00
jv = ((ase_awk_val_int_t*)v)->val;
arg = (*env)->NewObject (env,
2006-11-29 14:52:36 +00:00
run_data->long_class,
run_data->long_init, jv);
2006-11-28 15:09:53 +00:00
}
else if (v->type == ASE_AWK_VAL_REAL)
{
2006-11-29 11:41:15 +00:00
jdouble jv;
jv = ((ase_awk_val_real_t*)v)->val;
arg = (*env)->NewObject (env,
2006-11-29 14:52:36 +00:00
run_data->double_class,
run_data->double_init, jv);
2006-11-28 15:09:53 +00:00
}
else if (v->type == ASE_AWK_VAL_STR)
{
2007-01-31 08:23:59 +00:00
ase_size_t len = ((ase_awk_val_str_t*)v)->len;
2007-01-21 13:21:14 +00:00
if (ASE_SIZEOF(jchar) != ASE_SIZEOF(ase_char_t))
{
ase_size_t i;
2007-01-31 08:23:59 +00:00
jchar* tmp = (jchar*) malloc (ASE_SIZEOF(jchar)*len);
2007-01-21 13:21:14 +00:00
if (tmp == NULL)
{
(*env)->DeleteLocalRef (env, args);
2007-03-10 15:32:54 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
2007-01-21 13:21:14 +00:00
return -1;
}
2007-01-31 08:23:59 +00:00
for (i = 0; i < len; i++)
2007-01-21 13:21:14 +00:00
tmp[i] = (jchar)((ase_awk_val_str_t*)v)->buf[i];
2007-01-31 08:23:59 +00:00
arg = (*env)->NewString (env, tmp, len);
2007-01-21 13:21:14 +00:00
free (tmp);
}
else
{
2007-01-31 08:23:59 +00:00
arg = (*env)->NewString (
env, (jchar*)((ase_awk_val_str_t*)v)->buf, len);
2007-01-21 13:21:14 +00:00
}
2006-11-28 15:09:53 +00:00
}
2006-11-29 11:41:15 +00:00
if (v->type != ASE_AWK_VAL_NIL && arg == NULL)
2006-11-29 02:39:10 +00:00
{
2007-01-23 14:23:18 +00:00
if ((*env)->ExceptionOccurred (env))
2007-01-25 14:14:56 +00:00
{
if (is_debug(awk))
(*env)->ExceptionDescribe (env);
2007-01-23 14:23:18 +00:00
(*env)->ExceptionClear (env);
2007-01-25 14:14:56 +00:00
}
2006-11-29 11:41:15 +00:00
(*env)->DeleteLocalRef (env, args);
2007-03-10 15:32:54 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
2006-11-29 02:39:10 +00:00
return -1;
}
2006-11-28 15:09:53 +00:00
(*env)->SetObjectArrayElement (env, args, i, arg);
2006-11-29 11:41:15 +00:00
if (arg != NULL) (*env)->DeleteLocalRef (env, arg);
2006-11-28 15:09:53 +00:00
}
2006-11-27 04:33:22 +00:00
2006-12-02 16:26:29 +00:00
ret = (*env)->CallObjectMethod (env, obj, method, (jlong)run, args);
2007-01-23 14:23:18 +00:00
if ((*env)->ExceptionOccurred (env))
2006-11-28 15:09:53 +00:00
{
2007-01-25 14:14:56 +00:00
if (is_debug(ase_awk_getrunawk(run)))
(*env)->ExceptionDescribe (env);
2007-01-24 14:21:30 +00:00
2006-11-28 15:09:53 +00:00
(*env)->ExceptionClear (env);
(*env)->DeleteLocalRef (env, args);
2007-03-19 03:33:54 +00:00
2007-01-02 12:25:18 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_EBFNIMPL);
2006-11-28 15:09:53 +00:00
return -1;
}
2006-11-29 11:41:15 +00:00
2006-11-29 14:52:36 +00:00
(*env)->DeleteLocalRef (env, args);
if (ret == NULL)
2006-11-29 11:41:15 +00:00
{
2006-11-29 14:52:36 +00:00
ase_awk_setretval (run, ase_awk_val_nil);
2006-11-29 11:41:15 +00:00
}
else if ((*env)->IsInstanceOf (env, ret, run_data->integer_class))
{
2006-11-29 14:52:36 +00:00
jint jv = (*env)->CallIntMethod (
env, ret, run_data->integer_value);
2006-11-27 04:33:22 +00:00
2006-11-29 14:52:36 +00:00
v = ase_awk_makeintval (run, jv);
if (v == NULL)
{
(*env)->DeleteLocalRef (env, ret);
2007-03-10 15:32:54 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
2006-11-29 14:52:36 +00:00
return -1;
}
2006-11-27 04:33:22 +00:00
2006-11-29 14:52:36 +00:00
(*env)->DeleteLocalRef (env, ret);
ase_awk_setretval (run, v);
}
else if ((*env)->IsInstanceOf (env, ret, run_data->long_class))
{
jlong jv = (*env)->CallLongMethod (
env, ret, run_data->long_value);
2006-11-27 04:33:22 +00:00
2006-11-29 14:52:36 +00:00
v = ase_awk_makeintval (run, jv);
if (v == NULL)
{
(*env)->DeleteLocalRef (env, ret);
2007-03-10 15:32:54 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
2006-11-29 14:52:36 +00:00
return -1;
}
2006-11-27 04:33:22 +00:00
2006-11-29 14:52:36 +00:00
(*env)->DeleteLocalRef (env, ret);
ase_awk_setretval (run, v);
2006-11-27 04:33:22 +00:00
}
2006-11-29 14:52:36 +00:00
else if ((*env)->IsInstanceOf (env, ret, run_data->short_class))
{
jshort jv = (*env)->CallShortMethod (
env, ret, run_data->short_value);
2006-11-27 04:33:22 +00:00
2006-11-29 14:52:36 +00:00
v = ase_awk_makeintval (run, jv);
if (v == NULL)
{
(*env)->DeleteLocalRef (env, ret);
2007-03-10 15:32:54 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
2006-11-29 14:52:36 +00:00
return -1;
}
2006-11-27 04:33:22 +00:00
2006-11-29 14:52:36 +00:00
(*env)->DeleteLocalRef (env, ret);
ase_awk_setretval (run, v);
}
else if ((*env)->IsInstanceOf (env, ret, run_data->float_class))
{
jfloat jv = (*env)->CallFloatMethod (
env, ret, run_data->float_value);
v = ase_awk_makerealval (run, jv);
if (v == NULL)
{
(*env)->DeleteLocalRef (env, ret);
2007-03-10 15:32:54 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
2006-11-29 14:52:36 +00:00
return -1;
}
2006-11-27 04:33:22 +00:00
2006-11-29 14:52:36 +00:00
(*env)->DeleteLocalRef (env, ret);
ase_awk_setretval (run, v);
}
else if ((*env)->IsInstanceOf (env, ret, run_data->double_class))
2006-11-27 04:33:22 +00:00
{
2006-11-29 14:52:36 +00:00
jdouble jv = (*env)->CallDoubleMethod (
env, ret, run_data->double_value);
v = ase_awk_makerealval (run, jv);
if (v == NULL)
{
(*env)->DeleteLocalRef (env, ret);
2007-03-10 15:32:54 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
2006-11-29 14:52:36 +00:00
return -1;
}
2006-11-28 15:09:53 +00:00
2006-11-29 14:52:36 +00:00
(*env)->DeleteLocalRef (env, ret);
ase_awk_setretval (run, v);
}
else if ((*env)->IsInstanceOf (env, ret, run_data->string_class))
{
jsize len;
const jchar* ptr;
2006-11-27 04:33:22 +00:00
2006-11-29 14:52:36 +00:00
len = (*env)->GetStringLength (env, ret);
ptr = (*env)->GetStringChars (env, ret, JNI_FALSE);
if (ptr == NULL)
{
(*env)->DeleteLocalRef (env, ret);
2007-03-10 15:32:54 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
2006-11-29 14:52:36 +00:00
return -1;
}
2006-11-27 04:33:22 +00:00
2007-01-31 08:23:59 +00:00
if (len > 0 && ASE_SIZEOF(jchar) != ASE_SIZEOF(ase_char_t))
2007-01-21 13:21:14 +00:00
{
ase_size_t i;
ase_char_t* tmp = (ase_char_t*)
malloc (ASE_SIZEOF(ase_char_t)*len);
if (tmp == ASE_NULL)
{
(*env)->ReleaseStringChars (env, ret, ptr);
(*env)->DeleteLocalRef (env, ret);
2007-03-10 15:32:54 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
2007-01-21 13:21:14 +00:00
return -1;
}
for (i = 0; i < len; i++) tmp[i] = (ase_char_t)ptr[i];
v = ase_awk_makestrval (run, tmp, len);
free (tmp);
}
else
{
v = ase_awk_makestrval (run, (ase_char_t*)ptr, len);
}
2006-11-29 14:52:36 +00:00
if (v == NULL)
{
(*env)->ReleaseStringChars (env, ret, ptr);
(*env)->DeleteLocalRef (env, ret);
2007-03-10 15:32:54 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
2006-11-29 14:52:36 +00:00
return -1;
}
2006-11-28 15:09:53 +00:00
2006-11-29 14:52:36 +00:00
(*env)->ReleaseStringChars (env, ret, ptr);
(*env)->DeleteLocalRef (env, ret);
ase_awk_setretval (run, v);
}
else
{
(*env)->DeleteLocalRef (env, ret);
2007-01-02 12:25:18 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_EBFNUSER);
2006-11-29 14:52:36 +00:00
return -1;
2006-11-27 04:33:22 +00:00
}
2006-11-29 14:52:36 +00:00
return 0;
2006-11-27 04:33:22 +00:00
}
2006-11-29 02:39:10 +00:00
2007-01-23 14:23:18 +00:00
JNIEXPORT void JNICALL Java_ase_awk_Awk_addbfn (
2006-11-29 14:52:36 +00:00
JNIEnv* env, jobject obj, jstring name, jint min_args, jint max_args)
2006-11-29 02:39:10 +00:00
{
jclass class;
2006-11-29 14:52:36 +00:00
jfieldID handle;
jint n;
2006-11-29 02:39:10 +00:00
ase_awk_t* awk;
2006-11-29 14:52:36 +00:00
const jchar* ptr;
jsize len;
2006-11-29 02:39:10 +00:00
class = (*env)->GetObjectClass(env, obj);
2006-11-29 14:52:36 +00:00
handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
(*env)->DeleteLocalRef (env, class);
2007-01-23 14:23:18 +00:00
if (handle == NULL)
{
2007-01-24 14:21:30 +00:00
/* internal error. no handle field
* NoSuchFieldError, ExceptionInitializerError,
* OutOfMemoryError might occur */
2007-01-23 14:23:18 +00:00
return;
}
2006-11-29 02:39:10 +00:00
2006-11-29 14:52:36 +00:00
awk = (ase_awk_t*) (*env)->GetLongField (env, obj, handle);
2006-11-29 02:39:10 +00:00
len = (*env)->GetStringLength (env, name);
2007-01-23 14:23:18 +00:00
ptr = (*env)->GetStringChars (env, name, JNI_FALSE);
if (ptr == NULL)
{
2007-01-24 14:21:30 +00:00
(*env)->ExceptionClear (env);
2007-01-25 14:14:56 +00:00
2007-01-24 14:21:30 +00:00
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
2007-01-24 14:21:30 +00:00
ASE_AWK_ENOMEM,
0);
2007-01-23 14:23:18 +00:00
return;
}
2006-11-29 02:39:10 +00:00
2007-01-31 08:23:59 +00:00
if (len > 0 && ASE_SIZEOF(jchar) != ASE_SIZEOF(ase_char_t))
2007-01-21 13:21:14 +00:00
{
ase_size_t i;
ase_char_t* tmp = (ase_char_t*)
malloc (ASE_SIZEOF(ase_char_t)*len);
if (tmp == ASE_NULL)
{
(*env)->ReleaseStringChars (env, name, ptr);
2007-01-24 14:21:30 +00:00
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
2007-01-24 14:21:30 +00:00
ASE_AWK_ENOMEM,
0);
2007-01-23 14:23:18 +00:00
return;
2007-01-21 13:21:14 +00:00
}
for (i = 0; i < len; i++) tmp[i] = (ase_char_t)ptr[i];
n = (ase_awk_addbfn (awk, tmp, len, 0,
min_args, max_args, ASE_NULL, __handle_bfn) == NULL)? -1: 0;
free (tmp);
}
else
{
n = (ase_awk_addbfn (awk, (ase_char_t*)ptr, len, 0,
min_args, max_args, ASE_NULL, __handle_bfn) == NULL)? -1: 0;
}
2006-11-29 14:52:36 +00:00
(*env)->ReleaseStringChars (env, name, ptr);
2006-11-29 02:39:10 +00:00
2007-01-23 14:23:18 +00:00
if (n == -1)
{
throw_exception (
env,
ase_awk_geterrmsg(awk),
ase_awk_geterrnum(awk),
ase_awk_geterrlin(awk));
}
2006-11-29 14:52:36 +00:00
}
2006-11-29 02:39:10 +00:00
2007-01-23 14:23:18 +00:00
JNIEXPORT void JNICALL Java_ase_awk_Awk_delbfn (
2006-11-29 14:52:36 +00:00
JNIEnv* env, jobject obj, jstring name)
{
jclass class;
jfieldID handle;
jint n;
2006-11-29 02:39:10 +00:00
2006-11-29 14:52:36 +00:00
ase_awk_t* awk;
const jchar* ptr;
jsize len;
2006-11-29 02:39:10 +00:00
2006-11-29 14:52:36 +00:00
class = (*env)->GetObjectClass(env, obj);
handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
2006-11-29 02:39:10 +00:00
(*env)->DeleteLocalRef (env, class);
2007-01-23 14:23:18 +00:00
if (handle == NULL)
{
2007-01-24 14:21:30 +00:00
/* internal error. no handle field
* NoSuchFieldError, ExceptionInitializerError,
* OutOfMemoryError might occur */
2007-01-23 14:23:18 +00:00
return;
}
2006-11-29 14:52:36 +00:00
awk = (ase_awk_t*) (*env)->GetLongField (env, obj, handle);
len = (*env)->GetStringLength (env, name);
ptr = (*env)->GetStringChars (env, name, JNI_FALSE);
2007-01-23 14:23:18 +00:00
if (ptr == NULL)
{
2007-01-24 14:21:30 +00:00
(*env)->ExceptionClear (env);
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
2007-01-24 14:21:30 +00:00
ASE_AWK_ENOMEM,
0);
2007-01-23 14:23:18 +00:00
return;
}
2007-01-21 13:21:14 +00:00
2007-01-31 08:23:59 +00:00
if (len > 0 && ASE_SIZEOF(jchar) != ASE_SIZEOF(ase_char_t))
2007-01-21 13:21:14 +00:00
{
ase_size_t i;
ase_char_t* tmp = (ase_char_t*)
malloc (ASE_SIZEOF(ase_char_t)*len);
if (tmp == ASE_NULL)
{
(*env)->ReleaseStringChars (env, name, ptr);
2007-01-24 14:21:30 +00:00
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
2007-01-24 14:21:30 +00:00
ASE_AWK_ENOMEM,
0);
2007-01-23 14:23:18 +00:00
return;
2007-01-21 13:21:14 +00:00
}
for (i = 0; i < len; i++) tmp[i] = (ase_char_t)ptr[i];
n = ase_awk_delbfn (awk, tmp, len);
free (tmp);
}
else
{
n = ase_awk_delbfn (awk, (ase_char_t*)ptr, len);
}
2006-11-29 14:52:36 +00:00
(*env)->ReleaseStringChars (env, name, ptr);
2007-01-23 14:23:18 +00:00
if (n == -1)
{
throw_exception (
env,
ase_awk_geterrmsg(awk),
ase_awk_geterrnum(awk),
ase_awk_geterrlin(awk));
}
}
JNIEXPORT jint JNICALL Java_ase_awk_Awk_getmaxdepth (
JNIEnv* env, jobject obj, jint id)
{
jclass class;
jfieldID handle;
ase_awk_t* awk;
class = (*env)->GetObjectClass(env, obj);
handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
(*env)->DeleteLocalRef (env, class);
if (handle == NULL) return 0; /* should never happen */
awk = (ase_awk_t*) (*env)->GetLongField (env, obj, handle);
return (jint)ase_awk_getmaxdepth (awk, id);
}
JNIEXPORT void JNICALL Java_ase_awk_Awk_setmaxdepth (
JNIEnv* env, jobject obj, jint ids, jint depth)
{
jclass class;
jfieldID handle;
ase_awk_t* awk;
class = (*env)->GetObjectClass(env, obj);
handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
(*env)->DeleteLocalRef (env, class);
2007-01-24 14:21:30 +00:00
if (handle == NULL)
{
/* internal error. no handle field
* NoSuchFieldError, ExceptionInitializerError,
* OutOfMemoryError might occur */
return;
}
2007-01-23 14:23:18 +00:00
awk = (ase_awk_t*) (*env)->GetLongField (env, obj, handle);
ase_awk_setmaxdepth (awk, ids, depth);
2006-11-29 02:39:10 +00:00
}
2007-01-25 14:14:56 +00:00
JNIEXPORT jint JNICALL Java_ase_awk_Awk_getoption (
JNIEnv* env, jobject obj)
{
jclass class;
jfieldID handle;
ase_awk_t* awk;
class = (*env)->GetObjectClass(env, obj);
handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
(*env)->DeleteLocalRef (env, class);
if (handle == NULL)
{
/* internal error. no handle field
* NoSuchFieldError, ExceptionInitializerError,
* OutOfMemoryError might occur */
return 0;
}
awk = (ase_awk_t*) (*env)->GetLongField (env, obj, handle);
return ase_awk_getoption (awk);
}
JNIEXPORT void JNICALL Java_ase_awk_Awk_setoption (
JNIEnv* env, jobject obj, jint options)
{
jclass class;
jfieldID handle;
ase_awk_t* awk;
class = (*env)->GetObjectClass(env, obj);
handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
(*env)->DeleteLocalRef (env, class);
if (handle == NULL)
{
/* internal error. no handle field
* NoSuchFieldError, ExceptionInitializerError,
* OutOfMemoryError might occur */
return;
}
awk = (ase_awk_t*) (*env)->GetLongField (env, obj, handle);
ase_awk_setoption (awk, (int)options);
}
JNIEXPORT jboolean JNICALL Java_ase_awk_Awk_getdebug (
JNIEnv* env, jobject obj)
{
jclass class;
jfieldID handle;
ase_awk_t* awk;
class = (*env)->GetObjectClass(env, obj);
handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
(*env)->DeleteLocalRef (env, class);
if (handle == NULL)
{
/* internal error. no handle field
* NoSuchFieldError, ExceptionInitializerError,
* OutOfMemoryError might occur */
return JNI_FALSE;
}
awk = (ase_awk_t*) (*env)->GetLongField (env, obj, handle);
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)
{
jclass class;
jfieldID handle;
ase_awk_t* awk;
class = (*env)->GetObjectClass(env, obj);
handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
(*env)->DeleteLocalRef (env, class);
if (handle == NULL)
{
/* internal error. no handle field
* NoSuchFieldError, ExceptionInitializerError,
* OutOfMemoryError might occur */
return;
}
awk = (ase_awk_t*) (*env)->GetLongField (env, obj, handle);
((awk_data_t*)ase_awk_getcustomdata(awk))->debug = debug;
}
2007-01-24 11:54:16 +00:00
JNIEXPORT void JNICALL Java_ase_awk_Awk_setfilename (
2007-01-21 13:21:14 +00:00
JNIEnv* env, jobject obj, jlong runid, jstring name)
2006-11-29 02:39:10 +00:00
{
2007-01-21 13:21:14 +00:00
ase_awk_run_t* run = (ase_awk_run_t*)runid;
2006-12-02 16:26:29 +00:00
const jchar* ptr;
jsize len;
jint n;
2006-11-29 02:39:10 +00:00
len = (*env)->GetStringLength (env, name);
2007-01-23 14:23:18 +00:00
ptr = (*env)->GetStringChars (env, name, JNI_FALSE);
2007-01-24 11:54:16 +00:00
if (ptr == NULL)
{
2007-01-24 14:21:30 +00:00
(*env)->ExceptionClear (env);
2007-01-24 11:54:16 +00:00
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
2007-01-24 14:21:30 +00:00
ASE_AWK_ENOMEM,
2007-01-24 11:54:16 +00:00
0);
return;
}
2007-01-21 13:21:14 +00:00
2007-01-31 08:23:59 +00:00
if (len > 0 && ASE_SIZEOF(jchar) != ASE_SIZEOF(ase_char_t))
2007-01-21 13:21:14 +00:00
{
ase_size_t i;
ase_char_t* tmp = (ase_char_t*)
malloc (ASE_SIZEOF(ase_char_t)*len);
if (tmp == ASE_NULL)
{
(*env)->ReleaseStringChars (env, name, ptr);
2007-01-24 11:54:16 +00:00
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
2007-01-24 11:54:16 +00:00
ASE_AWK_ENOMEM,
0);
return;
2007-01-21 13:21:14 +00:00
}
for (i = 0; i < len; i++) tmp[i] = (ase_char_t)ptr[i];
n = ase_awk_setfilename (run, tmp, len);
free (tmp);
}
else
{
n = ase_awk_setfilename (run, (ase_char_t*)ptr, len);
}
2006-12-02 16:26:29 +00:00
(*env)->ReleaseStringChars (env, name, ptr);
2007-01-24 11:54:16 +00:00
if (n == -1)
{
throw_exception (
env,
ase_awk_getrunerrmsg(run),
ase_awk_getrunerrnum(run),
ase_awk_getrunerrlin(run));
}
2006-11-29 02:39:10 +00:00
}
2007-01-24 11:54:16 +00:00
JNIEXPORT void JNICALL Java_ase_awk_Awk_setofilename (
2007-01-21 13:21:14 +00:00
JNIEnv* env, jobject obj, jlong runid, jstring name)
2006-11-29 02:39:10 +00:00
{
2007-01-21 13:21:14 +00:00
ase_awk_run_t* run = (ase_awk_run_t*)runid;
2006-12-02 16:26:29 +00:00
const jchar* ptr;
jsize len;
jint n;
2006-11-29 02:39:10 +00:00
len = (*env)->GetStringLength (env, name);
2007-01-23 14:23:18 +00:00
ptr = (*env)->GetStringChars (env, name, JNI_FALSE);
2007-01-24 11:54:16 +00:00
if (ptr == NULL)
{
2007-01-24 14:21:30 +00:00
(*env)->ExceptionClear (env);
2007-01-24 11:54:16 +00:00
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
2007-01-24 11:54:16 +00:00
ASE_AWK_ENOMEM,
0);
return;
}
2007-01-21 13:21:14 +00:00
2007-01-31 08:23:59 +00:00
if (len > 0 && ASE_SIZEOF(jchar) != ASE_SIZEOF(ase_char_t))
2007-01-21 13:21:14 +00:00
{
ase_size_t i;
ase_char_t* tmp = (ase_char_t*)
2007-01-31 08:23:59 +00:00
malloc (ASE_SIZEOF(ase_char_t)*len);
2007-01-21 13:21:14 +00:00
if (tmp == ASE_NULL)
{
(*env)->ReleaseStringChars (env, name, ptr);
2007-01-24 11:54:16 +00:00
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
2007-01-24 11:54:16 +00:00
ASE_AWK_ENOMEM,
0);
return;
2007-01-21 13:21:14 +00:00
}
for (i = 0; i < len; i++) tmp[i] = (ase_char_t)ptr[i];
n = ase_awk_setofilename (run, tmp, len);
free (tmp);
}
else
{
n = ase_awk_setofilename (run, (ase_char_t*)ptr, len);
}
2006-12-02 16:26:29 +00:00
(*env)->ReleaseStringChars (env, name, ptr);
2007-01-24 11:54:16 +00:00
if (n == -1)
{
throw_exception (
env,
ase_awk_getrunerrmsg(run),
ase_awk_getrunerrnum(run),
ase_awk_getrunerrlin(run));
}
2006-11-29 02:39:10 +00:00
}
2006-12-02 16:26:29 +00:00
JNIEXPORT jobject JNICALL Java_ase_awk_Awk_strtonum (
JNIEnv* env, jobject obj, jlong runid, jstring str)
{
const jchar* ptr;
jsize len;
jint n;
ase_long_t lv;
ase_real_t rv;
jobject ret;
run_data_t* run_data;
len = (*env)->GetStringLength (env, str);
2007-01-23 14:23:18 +00:00
ptr = (*env)->GetStringChars (env, str, JNI_FALSE);
2007-01-24 14:21:30 +00:00
if (ptr == NULL)
{
(*env)->ExceptionClear (env);
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
2007-01-24 14:21:30 +00:00
ASE_AWK_ENOMEM,
0);
return NULL;
}
2007-01-23 14:23:18 +00:00
2007-01-31 08:23:59 +00:00
if (len > 0 && ASE_SIZEOF(jchar) != ASE_SIZEOF(ase_char_t))
2007-01-21 13:21:14 +00:00
{
ase_size_t i;
ase_char_t* tmp = (ase_char_t*)
malloc (ASE_SIZEOF(ase_char_t)*len);
if (tmp == ASE_NULL)
{
(*env)->ReleaseStringChars (env, str, ptr);
2007-01-24 14:21:30 +00:00
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
2007-01-24 14:21:30 +00:00
ASE_AWK_ENOMEM,
0);
return NULL;
2007-01-21 13:21:14 +00:00
}
for (i = 0; i < len; i++) tmp[i] = (ase_char_t)ptr[i];
2007-01-26 15:27:01 +00:00
n = ase_awk_strtonum (
(ase_awk_run_t*)runid, tmp, len, &lv, &rv);
2007-01-21 13:21:14 +00:00
free (tmp);
}
else
{
2007-01-26 15:27:01 +00:00
n = ase_awk_strtonum (
(ase_awk_run_t*)runid,
(ase_char_t*)ptr, len, &lv, &rv);
2007-01-21 13:21:14 +00:00
}
2006-12-02 16:26:29 +00:00
(*env)->ReleaseStringChars (env, str, ptr);
run_data = ase_awk_getruncustomdata ((ase_awk_run_t*)runid);
if (n == 0)
{
ret = (*env)->NewObject (env,
run_data->long_class,
run_data->long_init, (jlong)lv);
}
else
{
ret = (*env)->NewObject (env,
run_data->double_class,
run_data->double_init, (jdouble)rv);
}
return ret;
}
2007-01-25 14:14:56 +00:00
JNIEXPORT jstring JNICALL Java_ase_awk_Awk_valtostr (
2007-01-28 11:12:30 +00:00
JNIEnv* env, jobject obj, jlong runid, jobject val)
2007-01-25 14:14:56 +00:00
{
2007-01-28 11:12:30 +00:00
ase_awk_run_t* run = (ase_awk_run_t*)runid;
run_data_t* run_data;
jstring ret;
ase_awk_val_t* v;
ase_char_t* str;
ase_size_t len;
ase_awk_t* awk;
awk = ase_awk_getrunawk (run);
if (val == NULL)
{
ret = (*env)->NewString (env, NULL, 0);
if (ret == NULL)
{
(*env)->ExceptionClear (env);
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
2007-01-28 11:12:30 +00:00
ASE_AWK_ENOMEM,
0);
}
return ret;
}
run_data = ase_awk_getruncustomdata (run);
if ((*env)->IsInstanceOf (env, val, run_data->string_class))
{
const jchar* ptr;
len = (*env)->GetStringLength (env, val);
ptr = (*env)->GetStringChars (env, val, JNI_FALSE);
if (ptr == NULL)
{
(*env)->ExceptionClear (env);
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
2007-01-28 11:12:30 +00:00
ASE_AWK_ENOMEM,
0);
return NULL;
}
ret = (*env)->NewString (env, ptr, len);
(*env)->ReleaseStringChars (env, ret, ptr);
return ret;
}
if ((*env)->IsInstanceOf (env, val, run_data->integer_class))
{
jint jv;
jv = (*env)->CallIntMethod (
env, val, run_data->integer_value);
v = ase_awk_makeintval (run, jv);
}
else if ((*env)->IsInstanceOf (env, val, run_data->long_class))
{
jlong jv = (*env)->CallLongMethod (
env, val, run_data->long_value);
v = ase_awk_makeintval (run, jv);
}
else if ((*env)->IsInstanceOf (env, val, run_data->short_class))
{
jshort jv = (*env)->CallShortMethod (
env, val, run_data->short_value);
v = ase_awk_makeintval (run, jv);
}
else if ((*env)->IsInstanceOf (env, val, run_data->float_class))
{
jfloat jv = (*env)->CallFloatMethod (
env, val, run_data->float_value);
v = ase_awk_makerealval (run, jv);
}
else if ((*env)->IsInstanceOf (env, val, run_data->double_class))
{
jdouble jv = (*env)->CallDoubleMethod (
env, val, run_data->double_value);
v = ase_awk_makerealval (run, jv);
}
else
{
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_EVALTYPE),
2007-01-28 11:12:30 +00:00
ASE_AWK_EVALTYPE,
0);
return NULL;
}
if (v == NULL)
{
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
2007-01-28 11:12:30 +00:00
ASE_AWK_ENOMEM,
0);
return NULL;
}
ase_awk_refupval (run, v);
str = ase_awk_valtostr (run, v, ASE_AWK_VALTOSTR_CLEAR, NULL, &len);
ase_awk_refdownval (run, v);
if (str == NULL)
{
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
2007-01-28 11:12:30 +00:00
ASE_AWK_ENOMEM,
0);
return NULL;
}
2007-01-31 08:23:59 +00:00
if (len > 0 && ASE_SIZEOF(jchar) != ASE_SIZEOF(ase_char_t))
2007-01-28 11:12:30 +00:00
{
ase_size_t i;
jchar* tmp = (jchar*) malloc (ASE_SIZEOF(jchar)*len);
if (tmp == NULL)
{
ase_awk_free (awk, str);
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
2007-01-28 11:12:30 +00:00
ASE_AWK_ENOMEM,
0);
return NULL;
}
for (i = 0; i < len; i++) tmp[i] = (jchar)str[i];
ret = (*env)->NewString (env, tmp, len);
free (tmp);
}
else
{
2007-01-30 10:55:27 +00:00
ret = (*env)->NewString (env, (jchar*)str, len);
2007-01-28 11:12:30 +00:00
}
ase_awk_free (awk, str);
if (ret == NULL)
{
(*env)->ExceptionClear (env);
throw_exception (
env,
2007-03-10 15:42:00 +00:00
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
2007-01-28 11:12:30 +00:00
ASE_AWK_ENOMEM,
0);
}
return ret;
2007-01-25 14:14:56 +00:00
}