qse/ase/awk/jni.c

1332 lines
31 KiB
C
Raw Normal View History

2006-08-02 15:03:49 +00:00
/*
2006-12-19 14:20:30 +00:00
* $Id: jni.c,v 1.43 2006-12-19 14:20:30 bacon Exp $
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>
#include <ase/awk/awk_i.h>
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-29 02:39:10 +00:00
#ifdef _MSC_VER
#define snprintf _snprintf
#endif
2006-11-21 15:06:51 +00:00
#ifndef ASE_CHAR_IS_WCHAR
#error this module supports ASE_CHAR_IS_WCHAR only
#endif
2006-11-27 04:33:22 +00:00
#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
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
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
};
2006-10-24 04:10:12 +00:00
static void* __awk_malloc (ase_size_t n, void* custom_data)
2006-10-12 14:36:25 +00:00
{
return malloc (n);
}
2006-10-24 04:10:12 +00:00
static void* __awk_realloc (void* ptr, ase_size_t n, void* custom_data)
2006-10-12 14:36:25 +00:00
{
return realloc (ptr, n);
}
static void __awk_free (void* ptr, void* custom_data)
{
free (ptr);
}
2006-08-06 08:16:03 +00:00
2006-11-21 15:06:51 +00:00
static ase_real_t __awk_pow (ase_real_t x, ase_real_t y)
{
return pow (x, y);
}
2006-11-19 15:33:40 +00:00
static int __awk_sprintf (
ase_char_t* buf, ase_size_t len, const ase_char_t* fmt, ...)
{
int n;
va_list ap;
va_start (ap, fmt);
#if defined(_WIN32)
n = _vsntprintf (buf, len, fmt, ap);
if (n < 0 || (ase_size_t)n >= len)
{
if (len > 0) buf[len-1] = ASE_T('\0');
n = -1;
}
#elif defined(__MSDOS__)
/* TODO: check buffer overflow */
n = vsprintf (buf, fmt, ap);
#else
n = xp_vsprintf (buf, len, fmt, ap);
#endif
va_end (ap);
return n;
}
static void __awk_aprintf (const ase_char_t* fmt, ...)
{
va_list ap;
#ifdef _WIN32
int n;
ase_char_t buf[1024];
#endif
va_start (ap, fmt);
#if defined(_WIN32)
2006-11-29 02:39:10 +00:00
n = _vsntprintf (buf, ASE_COUNTOF(buf), fmt, ap);
if (n < 0) buf[ASE_COUNTOF(buf)-1] = ASE_T('\0');
2006-11-19 15:33:40 +00:00
#if defined(_MSC_VER) && (_MSC_VER<1400)
MessageBox (NULL, buf,
ASE_T("Assertion Failure"), MB_OK|MB_ICONERROR);
#else
MessageBox (NULL, buf,
ASE_T("\uB2DD\uAE30\uB9AC \uC870\uB610"), MB_OK|MB_ICONERROR);
#endif
#elif defined(__MSDOS__)
vprintf (fmt, ap);
#else
xp_vprintf (fmt, ap);
#endif
va_end (ap);
}
static void __awk_dprintf (const ase_char_t* fmt, ...)
{
va_list ap;
va_start (ap, fmt);
#if defined(_WIN32)
_vftprintf (stderr, fmt, ap);
#elif defined(__MSDOS__)
vfprintf (stderr, fmt, ap);
#else
xp_vfprintf (stderr, fmt, ap);
#endif
va_end (ap);
}
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;
2006-11-27 04:33:22 +00:00
jfieldID fid_handle;
2006-08-06 08:16:03 +00:00
jthrowable except;
2006-10-24 04:10:12 +00:00
ase_awk_t* awk;
2006-12-13 14:13:07 +00:00
ase_awk_sysfns_t sysfns;
2006-12-15 14:58:37 +00:00
int opt, errnum;
2006-08-06 08:16:03 +00:00
2006-12-13 14:13:07 +00:00
memset (&sysfns, 0, sizeof(sysfns));
sysfns.malloc = __awk_malloc;
sysfns.realloc = __awk_realloc;
sysfns.free = __awk_free;
sysfns.is_upper = iswupper;
sysfns.is_lower = iswlower;
sysfns.is_alpha = iswalpha;
sysfns.is_digit = iswdigit;
sysfns.is_xdigit = iswxdigit;
sysfns.is_alnum = iswalnum;
sysfns.is_space = iswspace;
sysfns.is_print = iswprint;
sysfns.is_graph = iswgraph;
sysfns.is_cntrl = iswcntrl;
sysfns.is_punct = iswpunct;
sysfns.to_upper = towupper;
sysfns.to_lower = towlower;
sysfns.memcpy = memcpy;
sysfns.memset = memset;
sysfns.pow = __awk_pow;
sysfns.sprintf = __awk_sprintf;
sysfns.aprintf = __awk_aprintf;
sysfns.dprintf = __awk_dprintf;
sysfns.abort = abort;
2006-12-15 14:58:37 +00:00
awk = ase_awk_open (&sysfns, &errnum);
2006-08-06 08:16:03 +00:00
if (awk == NULL)
{
2006-11-27 04:33:22 +00:00
except = (*env)->FindClass (env, CLASS_EXCEPTION);
if (except == NULL) return;
2006-08-06 08:16:03 +00:00
(*env)->ThrowNew (env, except, "cannot create awk");
2006-11-27 04:33:22 +00:00
(*env)->DeleteLocalRef (env, except);
2006-08-06 08:16:03 +00:00
return;
}
2006-11-28 15:09:53 +00:00
class = (*env)->GetObjectClass(env, obj);
2006-11-27 04:33:22 +00:00
fid_handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
2006-11-28 15:09:53 +00:00
(*env)->DeleteLocalRef (env, class);
if (fid_handle == NULL)
2006-11-27 04:33:22 +00:00
{
2006-11-28 15:09:53 +00:00
except = (*env)->FindClass (env, CLASS_EXCEPTION);
if (except == NULL) return;
(*env)->ThrowNew (env, except, "cannot find the handle field");
(*env)->DeleteLocalRef (env, except);
2006-11-27 04:33:22 +00:00
return;
}
2006-08-06 08:16:03 +00:00
2006-11-27 04:33:22 +00:00
(*env)->SetLongField (env, obj, fid_handle, (jlong)awk);
2006-08-06 08:16:03 +00:00
2006-12-04 06:04:07 +00:00
opt = ASE_AWK_EXPLICIT | ASE_AWK_UNIQUEAFN | ASE_AWK_SHADING |
ASE_AWK_IMPLICIT | ASE_AWK_SHIFT | ASE_AWK_IDIV |
2006-11-23 03:31:58 +00:00
ASE_AWK_EXTIO | ASE_AWK_BLOCKLESS | ASE_AWK_HASHSIGN |
2006-11-23 14:27:52 +00:00
ASE_AWK_NEXTOFILE;
2006-10-24 04:10:12 +00:00
ase_awk_setopt (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;
2006-11-27 04:33:22 +00:00
jfieldID fid_handle;
2006-08-06 08:16:03 +00:00
class = (*env)->GetObjectClass(env, obj);
2006-11-27 04:33:22 +00:00
fid_handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
2006-11-28 15:09:53 +00:00
(*env)->DeleteLocalRef (env, class);
2006-11-27 04:33:22 +00:00
if (fid_handle == NULL)
{
/* something wrong. should not happen */
2006-11-28 15:09:53 +00:00
/* TODO: should it throw an exception??? */
2006-11-27 04:33:22 +00:00
return;
}
2006-08-06 08:16:03 +00:00
2006-11-27 04:33:22 +00:00
ase_awk_close ((ase_awk_t*)(*env)->GetLongField (env, obj, fid_handle));
(*env)->SetLongField (env, obj, fid_handle, (jlong)0);
2006-08-02 15:03:49 +00:00
}
2006-11-26 15:55:44 +00:00
static jint __java_get_max_depth (JNIEnv* env, jobject obj, const char* name)
{
2006-11-28 15:09:53 +00:00
jclass class;
2006-11-26 15:55:44 +00:00
jmethodID mid;
jthrowable thrown;
jint ret;
class = (*env)->GetObjectClass(env, obj);
mid = (*env)->GetMethodID (env, class, name, "()I");
2006-11-28 15:09:53 +00:00
(*env)->DeleteLocalRef (env, class);
if (mid == NULL) return -1;
2006-11-26 15:55:44 +00:00
ret = (*env)->CallIntMethod (env, obj, mid);
thrown = (*env)->ExceptionOccurred (env);
if (thrown)
{
(*env)->ExceptionClear (env);
ret = -1;
}
return ret;
}
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;
2006-11-28 15:09:53 +00:00
jfieldID fid_handle;
2006-08-06 08:16:03 +00:00
jthrowable except;
2006-11-26 15:55:44 +00:00
jint depth;
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);
2006-11-28 15:09:53 +00:00
fid_handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
(*env)->DeleteLocalRef (env, class);
if (fid_handle == NULL)
{
except = (*env)->FindClass (env, CLASS_EXCEPTION);
if (except == NULL) return;
(*env)->ThrowNew (env, except, "cannot find the handle field");
(*env)->DeleteLocalRef (env, except);
return;
}
2006-08-06 08:16:03 +00:00
2006-11-28 15:09:53 +00:00
awk = (ase_awk_t*) (*env)->GetLongField (env, obj, fid_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-11-26 15:55:44 +00:00
depth = __java_get_max_depth (env, obj, "getMaxParseDepth");
if (depth < 0) depth = 0;
ase_awk_setmaxparsedepth (awk,
ASE_AWK_DEPTH_BLOCK | ASE_AWK_DEPTH_EXPR, depth);
2006-10-24 04:10:12 +00:00
if (ase_awk_parse (awk, &srcios) == -1)
2006-08-06 08:16:03 +00:00
{
2006-11-28 15:09:53 +00:00
char msg[MSG_SIZE];
2006-11-26 15:55:44 +00:00
int n;
2006-11-27 04:33:22 +00:00
except = (*env)->FindClass (env, CLASS_EXCEPTION);
if (except == NULL) return;
2006-11-26 15:55:44 +00:00
2006-12-16 14:48:31 +00:00
n = snprintf (msg, sizeof(msg),
"parse error at line %d: %S",
2006-12-19 14:20:30 +00:00
ase_awk_geterrlin(awk),
ase_awk_geterrmsg(awk));
2006-11-26 15:55:44 +00:00
if (n < 0 || n >= sizeof(msg)) msg[sizeof(msg)-1] = '\0';
(*env)->ThrowNew (env, except, msg);
2006-11-27 04:33:22 +00:00
(*env)->DeleteLocalRef (env, except);
2006-08-06 08:16:03 +00:00
return;
}
}
2006-10-24 04:10:12 +00:00
JNIEXPORT void JNICALL Java_ase_awk_Awk_run (JNIEnv* env, jobject obj)
2006-08-06 08:16:03 +00:00
{
2006-10-13 10:18:39 +00:00
jclass class;
2006-11-27 04:33:22 +00:00
jfieldID fid_handle;
2006-10-13 10:18:39 +00:00
jthrowable except;
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;
2006-10-13 10:18:39 +00:00
class = (*env)->GetObjectClass (env, obj);
2006-11-27 04:33:22 +00:00
fid_handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J");
2006-11-28 15:09:53 +00:00
(*env)->DeleteLocalRef (env, class);
2006-11-27 04:33:22 +00:00
if (fid_handle == 0)
{
2006-11-28 15:09:53 +00:00
except = (*env)->FindClass (env, CLASS_EXCEPTION);
if (except == NULL) return;
(*env)->ThrowNew (env, except, "cannot find the handle field");
(*env)->DeleteLocalRef (env, except);
2006-11-27 04:33:22 +00:00
return;
}
2006-10-13 10:18:39 +00:00
2006-11-27 04:33:22 +00:00
awk = (ase_awk_t*) (*env)->GetLongField (env, obj, fid_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");
ASE_AWK_ASSERT (awk, run_data.string_class != NULL);
ASE_AWK_ASSERT (awk, run_data.integer_class != NULL);
2006-11-29 14:52:36 +00:00
ASE_AWK_ASSERT (awk, run_data.short_class != NULL);
ASE_AWK_ASSERT (awk, run_data.long_class != NULL);
ASE_AWK_ASSERT (awk, run_data.float_class != NULL);
2006-11-29 11:41:15 +00:00
ASE_AWK_ASSERT (awk, run_data.double_class != NULL);
ASE_AWK_ASSERT (awk, run_data.object_class != NULL);
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");
ASE_AWK_ASSERT (awk, run_data.integer_init != NULL);
ASE_AWK_ASSERT (awk, run_data.long_init != NULL);
ASE_AWK_ASSERT (awk, run_data.short_init != NULL);
ASE_AWK_ASSERT (awk, run_data.float_init != NULL);
ASE_AWK_ASSERT (awk, run_data.double_init != NULL);
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");
ASE_AWK_ASSERTX (awk, run_data.integer_value != NULL,
"The Integer class must has the method - int intValue()");
ASE_AWK_ASSERTX (awk, run_data.long_value != NULL,
"The Long class must has the method - long longValue()");
ASE_AWK_ASSERTX (awk, run_data.short_value != NULL,
"The Short class must has the method - short shortValue()");
ASE_AWK_ASSERTX (awk, run_data.float_value != NULL,
"The Float class must has the method - float floatValue()");
ASE_AWK_ASSERTX (awk, run_data.double_value != NULL,
"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;
2006-11-28 15:09:53 +00:00
// TODO:
2006-11-26 15:55:44 +00:00
//depth = __java_get_max_depth (env, obj, "getMaxRunDepth");
2006-11-28 15:09:53 +00:00
// setMaxRunDepth...
2006-11-26 15:55:44 +00:00
2006-11-28 04:30:57 +00:00
if (ase_awk_run (awk,
ASE_NULL, &runios, ASE_NULL, ASE_NULL, &run_data) == -1)
2006-10-13 10:18:39 +00:00
{
2006-11-28 15:09:53 +00:00
char msg[MSG_SIZE];
2006-11-21 15:06:51 +00:00
int n;
2006-11-27 04:33:22 +00:00
except = (*env)->FindClass (env, CLASS_EXCEPTION);
if (except == NULL) return; /* exception thrown */
2006-11-21 15:06:51 +00:00
2006-11-28 15:09:53 +00:00
n = snprintf (msg, sizeof(msg), "%S",
2006-11-21 15:06:51 +00:00
ase_awk_geterrstr(ase_awk_geterrnum(awk)));
if (n < 0 || n >= sizeof(msg)) msg[sizeof(msg)-1] = '\0';
2006-11-28 15:09:53 +00:00
2006-11-21 15:06:51 +00:00
(*env)->ThrowNew (env, except, msg);
2006-11-27 04:33:22 +00:00
(*env)->DeleteLocalRef (env, except);
2006-11-29 11:41:15 +00:00
(*env)->DeleteLocalRef (env, run_data.integer_class);
2006-11-29 14:52:36 +00:00
(*env)->DeleteLocalRef (env, run_data.long_class);
(*env)->DeleteLocalRef (env, run_data.float_class);
2006-11-29 11:41:15 +00:00
(*env)->DeleteLocalRef (env, run_data.double_class);
(*env)->DeleteLocalRef (env, run_data.string_class);
(*env)->DeleteLocalRef (env, run_data.object_class);
2006-10-13 10:18:39 +00:00
return;
}
2006-11-27 04:33:22 +00:00
2006-11-29 11:41:15 +00:00
(*env)->DeleteLocalRef (env, run_data.integer_class);
2006-11-29 14:52:36 +00:00
(*env)->DeleteLocalRef (env, run_data.long_class);
(*env)->DeleteLocalRef (env, run_data.short_class);
(*env)->DeleteLocalRef (env, run_data.float_class);
2006-11-29 11:41:15 +00:00
(*env)->DeleteLocalRef (env, run_data.double_class);
(*env)->DeleteLocalRef (env, run_data.string_class);
(*env)->DeleteLocalRef (env, run_data.object_class);
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;
jmethodID mid;
jthrowable thrown;
jint ret;
class = (*env)->GetObjectClass(env, obj);
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);
2006-11-21 15:06:51 +00:00
if (mid == NULL)
{
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);
thrown = (*env)->ExceptionOccurred (env);
if (thrown)
2006-08-06 08:16:03 +00:00
{
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;
jmethodID mid;
jthrowable thrown;
jint ret;
class = (*env)->GetObjectClass(env, obj);
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);
2006-11-21 15:06:51 +00:00
if (mid == NULL)
{
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);
thrown = (*env)->ExceptionOccurred (env);
if (thrown)
2006-08-06 08:16:03 +00:00
{
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;
jmethodID mid;
jcharArray array;
jchar* tmp;
2006-10-13 10:18:39 +00:00
jint ret, i;
jthrowable thrown;
2006-08-06 08:16:03 +00:00
class = (*env)->GetObjectClass(env, obj);
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);
2006-11-21 15:06:51 +00:00
if (mid == NULL)
{
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)
{
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);
thrown = (*env)->ExceptionOccurred (env);
if (thrown)
{
(*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;
jmethodID mid;
jcharArray array;
jchar* tmp;
2006-12-12 05:44:42 +00:00
jint ret;
2006-10-13 10:18:39 +00:00
jthrowable thrown;
2006-12-12 05:44:42 +00:00
ase_size_t i;
2006-08-06 15:03:42 +00:00
class = (*env)->GetObjectClass(env, obj);
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);
2006-11-21 15:06:51 +00:00
if (mid == NULL)
{
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)
{
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);
thrown = (*env)->ExceptionOccurred (env);
if (thrown)
{
2006-11-25 15:51:57 +00:00
(*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;
jmethodID mid;
jthrowable thrown;
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;
2006-11-27 04:33:22 +00:00
extio_class = (*env)->FindClass (env, CLASS_EXTIO);
2006-11-29 14:52:36 +00:00
if (extio_class == NULL) return -1;
2006-10-13 14:05:24 +00:00
2006-11-21 15:06:51 +00:00
/* get the constructor */
extio_cons = (*env)->GetMethodID (
2006-11-22 15:12:04 +00:00
env, extio_class, "<init>", "(Ljava/lang/String;IIJ)V");
2006-11-21 15:06:51 +00:00
if (extio_cons == NULL)
{
(*env)->DeleteLocalRef (env, extio_class);
return -1;
}
2006-10-22 07:05:34 +00:00
2006-11-21 15:06:51 +00:00
/* get the method - meth */
2006-11-28 15:09:53 +00:00
class = (*env)->GetObjectClass(env, obj);
2006-11-21 15:06:51 +00:00
mid = (*env)->GetMethodID (env, class, meth, "(Lase/awk/Extio;)I");
2006-11-28 15:09:53 +00:00
(*env)->DeleteLocalRef (env, class);
2006-11-21 15:06:51 +00:00
if (mid == NULL)
{
(*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 */
extio_name = (*env)->NewString (
env, extio->name, ase_awk_strlen(extio->name));
if (extio_name == NULL)
{
(*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,
2006-11-22 15:12:04 +00:00
extio_name, extio->type & 0xFF, extio->mode, 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)
{
(*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);
2006-10-13 10:18:39 +00:00
thrown = (*env)->ExceptionOccurred (env);
if (thrown)
{
2006-11-21 15:06:51 +00:00
(*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);
2006-11-29 11:41:15 +00:00
/* TODO: close it...
if (extio->handle == NULL)
{
close it again...
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;
jmethodID mid;
jthrowable thrown;
jint ret;
class = (*env)->GetObjectClass(env, obj);
2006-11-28 15:09:53 +00:00
mid = (*env)->GetMethodID (env, class, meth, "(Lase/awk/Extio;)I");
(*env)->DeleteLocalRef (env, class);
2006-11-21 15:06:51 +00:00
if (mid == NULL)
2006-10-13 14:05:24 +00:00
{
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);
2006-10-13 10:18:39 +00:00
thrown = (*env)->ExceptionOccurred (env);
if (thrown)
{
2006-11-21 15:06:51 +00:00
(*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;
jmethodID mid;
jcharArray array;
jchar* tmp;
jint ret, i;
jthrowable thrown;
class = (*env)->GetObjectClass(env, obj);
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);
2006-11-21 15:06:51 +00:00
if (mid == NULL)
{
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)
{
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);
2006-10-13 10:18:39 +00:00
thrown = (*env)->ExceptionOccurred (env);
if (thrown)
{
(*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;
jmethodID mid;
jcharArray array;
jchar* tmp;
jint ret;
2006-12-12 05:44:42 +00:00
ase_size_t i;
2006-10-13 10:18:39 +00:00
jthrowable thrown;
class = (*env)->GetObjectClass(env, obj);
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);
2006-11-21 15:06:51 +00:00
if (mid == NULL)
{
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)
{
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);
2006-10-13 10:18:39 +00:00
thrown = (*env)->ExceptionOccurred (env);
if (thrown)
{
(*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;
jmethodID mid;
jthrowable thrown;
jint ret;
class = (*env)->GetObjectClass(env, obj);
2006-11-28 15:09:53 +00:00
mid = (*env)->GetMethodID (env, class, meth, "(Lase/awk/Extio;)I");
(*env)->DeleteLocalRef (env, class);
2006-11-24 15:07:18 +00:00
if (mid == NULL)
{
return -1;
}
ret = (*env)->CallIntMethod (env, obj, mid, extio->handle);
thrown = (*env)->ExceptionOccurred (env);
if (thrown)
{
(*env)->ExceptionDescribe (env);
(*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;
jmethodID mid;
jthrowable thrown;
jint ret;
class = (*env)->GetObjectClass(env, obj);
2006-11-28 15:09:53 +00:00
mid = (*env)->GetMethodID (env, class, meth, "(Lase/awk/Extio;)I");
(*env)->DeleteLocalRef (env, class);
2006-11-22 15:12:04 +00:00
if (mid == NULL)
{
return -1;
}
ret = (*env)->CallIntMethod (env, obj, mid, extio->handle);
thrown = (*env)->ExceptionOccurred (env);
if (thrown)
{
(*env)->ExceptionDescribe (env);
(*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 04:30:57 +00:00
jthrowable thrown;
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;
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);
2006-11-28 04:30:57 +00:00
env = run_data->env;
obj = run_data->obj;
2006-11-28 15:09:53 +00:00
name = (*env)->NewString (env, fnm, fnl);
if (name == NULL)
2006-11-28 04:30:57 +00:00
{
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
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);
2006-11-28 04:30:57 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
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.
* so clear it to prevent it from being thrown */
if ((*env)->ExceptionOccurred (env))
(*env)->ExceptionClear (env);
ase_awk_setrunerrnum (run, ASE_AWK_EBFNIMPL);
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
{
2006-11-28 15:09:53 +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)
{
arg = (*env)->NewString (env,
((ase_awk_val_str_t*)v)->buf,
((ase_awk_val_str_t*)v)->len);
}
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
{
2006-11-29 11:41:15 +00:00
(*env)->DeleteLocalRef (env, args);
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);
2006-11-28 15:09:53 +00:00
thrown = (*env)->ExceptionOccurred (env);
if (thrown)
{
2006-11-29 11:41:15 +00:00
(*env)->ExceptionDescribe (env);
2006-11-28 15:09:53 +00:00
(*env)->ExceptionClear (env);
(*env)->DeleteLocalRef (env, args);
2006-12-02 16:26:29 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_EBFNFAIL);
// TODO:
//ase_awk_setrunerror (run, ASE_AWK_EBFNFAIL, "EXCEPTION:....");
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);
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
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);
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
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);
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
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);
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
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);
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
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);
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
return -1;
}
2006-11-27 04:33:22 +00:00
2006-11-29 14:52:36 +00:00
v = ase_awk_makestrval (run, ptr, len);
if (v == NULL)
{
(*env)->ReleaseStringChars (env, ret, ptr);
(*env)->DeleteLocalRef (env, ret);
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
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);
ase_awk_setrunerrnum (run, ASE_AWK_EBFNIMPL);
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
2006-11-29 14:52:36 +00:00
JNIEXPORT jint JNICALL Java_ase_awk_Awk_addbfn (
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);
2006-12-02 16:26:29 +00:00
if (handle == NULL) return -1;
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
2006-11-29 14:52:36 +00:00
ptr = (*env)->GetStringChars (env, name, JNI_FALSE);
2006-11-29 02:39:10 +00:00
len = (*env)->GetStringLength (env, name);
2006-11-29 14:52:36 +00:00
n = (ase_awk_addbfn (awk, ptr, len, 0,
min_args, max_args, ASE_NULL, __handle_bfn) == NULL)? -1: 0;
(*env)->ReleaseStringChars (env, name, ptr);
2006-11-29 02:39:10 +00:00
2006-11-29 14:52:36 +00:00
return n;
}
2006-11-29 02:39:10 +00:00
2006-11-29 14:52:36 +00:00
JNIEXPORT jint JNICALL Java_ase_awk_Awk_delbfn (
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);
2006-11-29 14:52:36 +00:00
if (handle == NULL) return -1; /* should never happen */
awk = (ase_awk_t*) (*env)->GetLongField (env, obj, handle);
len = (*env)->GetStringLength (env, name);
ptr = (*env)->GetStringChars (env, name, JNI_FALSE);
n = ase_awk_delbfn (awk, ptr, len);
(*env)->ReleaseStringChars (env, name, ptr);
return n;
2006-11-29 02:39:10 +00:00
}
JNIEXPORT jint JNICALL Java_ase_awk_Awk_setfilename (
JNIEnv* env, jobject obj, jlong run_id, jstring name)
{
ase_awk_run_t* run = (ase_awk_run_t*)run_id;
2006-12-02 16:26:29 +00:00
const jchar* ptr;
jsize len;
jint n;
2006-11-29 02:39:10 +00:00
2006-12-02 16:26:29 +00:00
ptr = (*env)->GetStringChars (env, name, JNI_FALSE);
2006-11-29 02:39:10 +00:00
len = (*env)->GetStringLength (env, name);
2006-12-02 16:26:29 +00:00
n = ase_awk_setfilename (run, ptr, len);
(*env)->ReleaseStringChars (env, name, ptr);
2006-11-29 02:39:10 +00:00
return n;
}
JNIEXPORT jint JNICALL Java_ase_awk_Awk_setofilename (
JNIEnv* env, jobject obj, jlong run_id, jstring name)
{
ase_awk_run_t* run = (ase_awk_run_t*)run_id;
2006-12-02 16:26:29 +00:00
const jchar* ptr;
jsize len;
jint n;
2006-11-29 02:39:10 +00:00
2006-12-02 16:26:29 +00:00
ptr = (*env)->GetStringChars (env, name, JNI_FALSE);
2006-11-29 02:39:10 +00:00
len = (*env)->GetStringLength (env, name);
2006-12-02 16:26:29 +00:00
n = ase_awk_setofilename (run, ptr, len);
(*env)->ReleaseStringChars (env, name, ptr);
2006-11-29 02:39:10 +00:00
return n;
}
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;
ptr = (*env)->GetStringChars (env, str, JNI_FALSE);
len = (*env)->GetStringLength (env, str);
n = ase_awk_strtonum ((ase_awk_run_t*)runid, ptr, len, &lv, &rv);
(*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);
}
/*
{
jthrowable except;
except = (*env)->FindClass (env, CLASS_EXCEPTION);
if (except == NULL) return;
(*env)->ThrowNew (env, except, "cannot create awk");
(*env)->DeleteLocalRef (env, except);
}
*/
return ret;
}