Recovered from cvs revision 2007-10-13 05:08:00

This commit is contained in:
hyung-hwan 2007-10-13 15:28:00 +00:00
parent 8c505c6d5d
commit 6eb72e1918
6 changed files with 194 additions and 69 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: Awk.java,v 1.16 2007/10/10 07:03:56 bacon Exp $
* $Id: Awk.java,v 1.18 2007/10/12 16:13:34 bacon Exp $
*
* {License}
*/
@ -7,9 +7,14 @@
package ase.awk;
import java.io.*;
import java.util.HashMap;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
public abstract class Awk
{
private HashMap functionTable;
// mode for open_source & close_source
public static final int SOURCE_READ = 1;
public static final int SOURCE_WRITE = 2;
@ -53,6 +58,7 @@ public abstract class Awk
public Awk () throws Exception
{
this.handle = 0;
this.functionTable = new HashMap ();
open ();
}
@ -68,6 +74,7 @@ public abstract class Awk
public native void close ();
public native void parse () throws Exception;
public native void run (String main, String[] args) throws Exception;
public native void stop ();
private native int getmaxdepth (int id);
private native void setmaxdepth (int id, int depth);
@ -115,15 +122,34 @@ public abstract class Awk
}
/* == builtin functions == */
public void addFunction (
String name, int min_args, int max_args) throws Exception
public void addFunction (String name, int min_args, int max_args) throws Exception
{
addfunc (name, min_args, max_args);
addFunction (name, min_args, max_args, "bfn_" + name);
}
public void addFunction (String name, int min_args, int max_args, String method) throws Exception
{
if (functionTable.containsKey (name))
{
throw new Exception (
"cannot add existing function '" + name + "'",
Exception.EXIST);
}
functionTable.put (name, method);
try { addfunc (name, min_args, max_args); }
catch (Exception e)
{
functionTable.remove (name);
throw e;
}
}
public void deleteFunction (String name) throws Exception
{
delfunc (name);
delfunc (name);
functionTable.remove (name);
}
protected long builtinFunctionArgumentToLong (
@ -276,6 +302,23 @@ public abstract class Awk
setword (null, null);
}
/* == intrinsic function handling == */
protected Object handleFunction (
long run, String name, Object args[]) throws java.lang.Exception
{
String mn = (String)functionTable.get(name);
// name should always be found in this table.
// otherwise, there is something wrong with this program.
Class c = this.getClass ();
Class[] a = { Context.class, String.class, Object[].class };
// TODO: remove new Context ....
Method m = c.getMethod (mn, a);
return m.invoke (this,
new Object[] { new Context(run), name, args}) ;
}
/* == source code management == */
protected abstract int openSource (int mode);
protected abstract int closeSource (int mode);

31
ase/awk/Context.java Normal file
View File

@ -0,0 +1,31 @@
/*
* $Id: Context.java,v 1.2 2007/10/12 16:13:34 bacon Exp $
*/
package ase.awk;
public class Context
{
private long run;
private Object custom;
Context (long run)
{
this.run = run;
}
public long getId ()
{
return this.run;
}
public void setCustom (Object custom)
{
this.custom = custom;
}
public Object getCustom ()
{
return this.custom;
}
}

View File

@ -1,5 +1,5 @@
/*
* $Id: StdAwk.java,v 1.11 2007/08/26 14:33:38 bacon Exp $
* $Id: StdAwk.java,v 1.13 2007/10/12 16:13:34 bacon Exp $
*
* {License}
*/
@ -20,7 +20,7 @@ public abstract class StdAwk extends Awk
seed = System.currentTimeMillis();
random = new java.util.Random (seed);
addFunction ("sin", 1, 1);
addFunction ("sin", 1, 1, "sin");
addFunction ("cos", 1, 1);
addFunction ("tan", 1, 1);
addFunction ("atan", 1, 1);
@ -28,7 +28,7 @@ public abstract class StdAwk extends Awk
addFunction ("log", 1, 1);
addFunction ("exp", 1, 1);
addFunction ("sqrt", 1, 1);
addFunction ("int", 1, 1);
addFunction ("int", 1, 1, "bfnint");
addFunction ("srand", 0, 1);
addFunction ("rand", 0, 0);
@ -330,104 +330,103 @@ public abstract class StdAwk extends Awk
return -1;
}
/* == arithmetic built-in functions */
public Object bfn_sin (long runid, Object[] args) throws Exception
public Object sin (Context ctx, String name, Object[] args) throws Exception
{
double x = builtinFunctionArgumentToDouble (runid, args[0]);
double x = builtinFunctionArgumentToDouble (ctx.getId(), args[0]);
return new Double (Math.sin(x));
}
public Object bfn_cos (long runid, Object[] args) throws Exception
public Object cos (Context ctx, String name, Object[] args) throws Exception
{
double x = builtinFunctionArgumentToDouble (runid, args[0]);
double x = builtinFunctionArgumentToDouble (ctx.getId(), args[0]);
return new Double (Math.cos(x));
}
public Object bfn_tan (long runid, Object[] args) throws Exception
public Object tan (Context ctx, String name, Object[] args) throws Exception
{
double x = builtinFunctionArgumentToDouble (runid, args[0]);
double x = builtinFunctionArgumentToDouble (ctx.getId(), args[0]);
return new Double (Math.tan(x));
}
public Object bfn_atan (long runid, Object[] args) throws Exception
public Object atan (Context ctx, String name, Object[] args) throws Exception
{
double x = builtinFunctionArgumentToDouble (runid, args[0]);
double x = builtinFunctionArgumentToDouble (ctx.getId(), args[0]);
return new Double (Math.atan(x));
}
public Object bfn_atan2 (long runid, Object[] args) throws Exception
public Object atan2 (Context ctx, String name, Object[] args) throws Exception
{
double y = builtinFunctionArgumentToDouble (runid, args[0]);
double x = builtinFunctionArgumentToDouble (runid, args[1]);
double y = builtinFunctionArgumentToDouble (ctx.getId(), args[0]);
double x = builtinFunctionArgumentToDouble (ctx.getId(), args[1]);
return new Double (Math.atan2(y,x));
}
public Object bfn_log (long runid, Object[] args) throws Exception
public Object log (Context ctx, String name, Object[] args) throws Exception
{
double x = builtinFunctionArgumentToDouble (runid, args[0]);
double x = builtinFunctionArgumentToDouble (ctx.getId(), args[0]);
return new Double (Math.log(x));
}
public Object bfn_exp (long runid, Object[] args) throws Exception
public Object exp (Context ctx, String name, Object[] args) throws Exception
{
double x = builtinFunctionArgumentToDouble (runid, args[0]);
double x = builtinFunctionArgumentToDouble (ctx.getId(), args[0]);
return new Double (Math.exp(x));
}
public Object bfn_sqrt (long runid, Object[] args) throws Exception
public Object sqrt (Context ctx, String name, Object[] args) throws Exception
{
double x = builtinFunctionArgumentToDouble (runid, args[0]);
double x = builtinFunctionArgumentToDouble (ctx.getId(), args[0]);
return new Double (Math.sqrt(x));
}
public Object bfn_int (long runid, Object[] args) throws Exception
public Object bfnint (Context ctx, String name, Object[] args) throws Exception
{
long x = builtinFunctionArgumentToLong (runid, args[0]);
long x = builtinFunctionArgumentToLong (ctx.getId(), args[0]);
return new Long (x);
}
public Object bfn_rand (long runid, Object[] args)
public Object rand (Context ctx, String name, Object[] args)
{
return new Double (random.nextDouble ());
}
public Object bfn_srand (long runid, Object[] args) throws Exception
public Object srand (Context ctx, String name, Object[] args) throws Exception
{
long prev_seed = seed;
seed = (args == null || args.length == 0)?
System.currentTimeMillis ():
builtinFunctionArgumentToLong (runid, args[0]);
builtinFunctionArgumentToLong (ctx.getId(), args[0]);
random.setSeed (seed);
return new Long (prev_seed);
}
public Object bfn_systime (long runid, Object[] args)
public Object systime (Context ctx, String name, Object[] args)
{
long msec = System.currentTimeMillis ();
return new Long (msec / 1000);
}
public Object bfn_strftime (long runid, Object[] args) throws Exception
public Object strftime (Context ctx, String name, Object[] args) throws Exception
{
String fmt = (args.length<1)? "%c": builtinFunctionArgumentToString (runid, args[0]);
long t = (args.length<2)? (System.currentTimeMillis()/1000): builtinFunctionArgumentToLong (runid, args[1]);
String fmt = (args.length<1)? "%c": builtinFunctionArgumentToString (ctx.getId(), args[0]);
long t = (args.length<2)? (System.currentTimeMillis()/1000): builtinFunctionArgumentToLong (ctx.getId(), args[1]);
return strftime (fmt, t);
}
public Object bfn_strfgmtime (long runid, Object[] args) throws Exception
public Object strfgmtime (Context ctx, String name, Object[] args) throws Exception
{
String fmt = (args.length<1)? "%c": builtinFunctionArgumentToString (runid, args[0]);
long t = (args.length<2)? (System.currentTimeMillis()/1000): builtinFunctionArgumentToLong (runid, args[1]);
String fmt = (args.length<1)? "%c": builtinFunctionArgumentToString (ctx.getId(), args[0]);
long t = (args.length<2)? (System.currentTimeMillis()/1000): builtinFunctionArgumentToLong (ctx.getId(), args[1]);
return strfgmtime (fmt, t);
}
/* miscellaneous built-in functions */
public Object bfn_system (long runid, Object[] args) throws Exception
public Object system (Context ctx, String name, Object[] args) throws Exception
{
String str = builtinFunctionArgumentToString (runid, args[0]);
String str = builtinFunctionArgumentToString (ctx.getId(), args[0]);
return system (str);
}

View File

@ -1,5 +1,5 @@
/*
* $Id: jni.c,v 1.17 2007/10/10 07:03:56 bacon Exp $
* $Id: jni.c,v 1.19 2007/10/12 16:13:34 bacon Exp $
*
* {License}
*/
@ -25,6 +25,11 @@
#include <tchar.h>
#endif
#if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#endif
#ifndef ASE_CHAR_IS_WCHAR
#error this module supports ASE_CHAR_IS_WCHAR only
#endif
@ -49,11 +54,11 @@ enum
SOURCE_WRITE = 2
};
static ase_ssize_t __read_source (
static ase_ssize_t read_source (
int cmd, void* arg, ase_char_t* data, ase_size_t count);
static ase_ssize_t __write_source (
static ase_ssize_t write_source (
int cmd, void* arg, ase_char_t* data, ase_size_t count);
static ase_ssize_t __process_extio (
static ase_ssize_t process_extio (
int cmd, void* arg, ase_char_t* data, ase_size_t count);
typedef struct awk_data_t awk_data_t;
@ -320,6 +325,10 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_open (JNIEnv* env, jobject obj)
awk_data_t* awk_data;
int opt;
#if defined(_WIN32) && defined(_DEBUG) && defined(_MSC_VER)
OutputDebugStringW (L"<<<OPENING AWK>>>\n");
_CrtSetDbgFlag (_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF);
#endif
memset (&prmfns, 0, sizeof(prmfns));
prmfns.mmgr.malloc = awk_malloc;
@ -424,6 +433,12 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_close (JNIEnv* env, jobject obj)
(*env)->SetLongField (env, obj, handle, (jlong)0);
free (tmp);
}
#if defined(_WIN32) && defined(_DEBUG) && defined(_MSC_VER)
OutputDebugStringW (L"<<<CLOSING AWK>>>\n");
_CrtDumpMemoryLeaks ();
#endif
}
JNIEXPORT void JNICALL Java_ase_awk_Awk_parse (JNIEnv* env, jobject obj)
@ -451,8 +466,8 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_parse (JNIEnv* env, jobject obj)
srcio_data.env = env;
srcio_data.obj = obj;
srcios.in = __read_source;
srcios.out = __write_source;
srcios.in = read_source;
srcios.out = write_source;
srcios.custom_data = &srcio_data;
if (ase_awk_parse (awk, &srcios) == -1)
@ -595,10 +610,10 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_run (JNIEnv* env, jobject obj, jstring m
runio_data.env = env;
runio_data.obj = obj;
runios.pipe = __process_extio;
runios.pipe = process_extio;
runios.coproc = ASE_NULL;
runios.file = __process_extio;
runios.console = __process_extio;
runios.file = process_extio;
runios.console = process_extio;
runios.custom_data = &runio_data;
if (mfn == NULL)
@ -782,6 +797,27 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_run (JNIEnv* env, jobject obj, jstring m
DELETE_CLASS_REFS (env, run_data);
}
JNIEXPORT void JNICALL Java_ase_awk_Awk_stop (JNIEnv* env, jobject obj)
{
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);
if (awk != NULL) ase_awk_stopall (awk);
}
static ase_ssize_t __java_open_source (JNIEnv* env, jobject obj, int mode)
{
jclass class;
@ -1325,7 +1361,7 @@ static ase_ssize_t __java_next_extio (
return ret;
}
static ase_ssize_t __read_source (
static ase_ssize_t read_source (
int cmd, void* arg, ase_char_t* data, ase_size_t count)
{
srcio_data_t* srcio_data = (srcio_data_t*)arg;
@ -1349,7 +1385,7 @@ static ase_ssize_t __read_source (
return -1;
}
static ase_ssize_t __write_source (
static ase_ssize_t write_source (
int cmd, void* arg, ase_char_t* data, ase_size_t count)
{
srcio_data_t* srcio_data = (srcio_data_t*)arg;
@ -1373,13 +1409,12 @@ static ase_ssize_t __write_source (
return -1;
}
static ase_ssize_t __process_extio (
static ase_ssize_t process_extio (
int cmd, void* arg, ase_char_t* data, ase_size_t size)
{
ase_awk_extio_t* epa = (ase_awk_extio_t*)arg;
runio_data_t* runio_data = (runio_data_t*)epa->custom_data;
switch (cmd)
{
@ -1419,7 +1454,7 @@ static ase_ssize_t __process_extio (
return -1;
}
static int __handle_bfn (
static int handle_bfn (
ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl)
{
jclass class;
@ -1442,7 +1477,7 @@ static int __handle_bfn (
env = run_data->env;
obj = run_data->obj;
/*if (fnl > 0 && ASE_SIZEOF(jchar) != ASE_SIZEOF(ase_char_t))*/
if (fnl > 0 && ASE_SIZEOF(jchar) != ASE_SIZEOF(ase_char_t))
{
ase_size_t i;
jchar* tmp = (jchar*) malloc (ASE_SIZEOF(jchar)*(fnl+4));
@ -1452,18 +1487,21 @@ static int __handle_bfn (
return -1;
}
/*
tmp[0] = (jchar)'b';
tmp[1] = (jchar)'f';
tmp[2] = (jchar)'n';
tmp[3] = (jchar)'_';
for (i = 0; i < fnl; i++) tmp[i+4] = (jchar)fnm[i];
*/
for (i = 0; i < fnl; i++) tmp[i] = (jchar)fnm[i];
name = (*env)->NewString (env, tmp, fnl+4);
free (tmp);
}
/*else
else
{
name = (*env)->NewString (env, (jchar*)fnm, fnl);
}*/
}
if (name == NULL)
{
@ -1472,6 +1510,8 @@ static int __handle_bfn (
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
return -1;
}
/*
name_utf = (*env)->GetStringUTFChars (env, name, JNI_FALSE);
if (name_utf == NULL)
{
@ -1484,13 +1524,21 @@ static int __handle_bfn (
method = (*env)->GetMethodID (
env, class, name_utf,
"(J[Ljava/lang/Object;)Ljava/lang/Object;");
*/
class = (*env)->GetObjectClass(env, obj);
method = (*env)->GetMethodID (
env, class, "handleFunction",
"(JLjava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;");
//"(J[Ljava/lang/Object;)Ljava/lang/Object;");
(*env)->DeleteLocalRef (env, class);
(*env)->ReleaseStringUTFChars (env, name, name_utf);
(*env)->DeleteLocalRef (env, name);
/*(*env)->ReleaseStringUTFChars (env, name, name_utf);*/
//(*env)->DeleteLocalRef (env, name);
if (method == NULL)
{
/* if the method is not found, the exception is thrown.
* clear it to prevent it from being thrown */
(*env)->DeleteLocalRef (env, name);
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
ase_awk_setrunerrnum (run, ASE_AWK_EBFNUSER);
@ -1501,6 +1549,7 @@ static int __handle_bfn (
env, nargs, run_data->object_class, NULL);
if (args == NULL)
{
(*env)->DeleteLocalRef (env, name);
if (is_debug(awk)) (*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
@ -1543,6 +1592,7 @@ static int __handle_bfn (
if (tmp == NULL)
{
(*env)->DeleteLocalRef (env, args);
(*env)->DeleteLocalRef (env, name);
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
return -1;
}
@ -1570,6 +1620,7 @@ static int __handle_bfn (
(*env)->ExceptionClear (env);
}
(*env)->DeleteLocalRef (env, args);
(*env)->DeleteLocalRef (env, name);
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
return -1;
}
@ -1578,7 +1629,7 @@ static int __handle_bfn (
if (arg != NULL) (*env)->DeleteLocalRef (env, arg);
}
ret = (*env)->CallObjectMethod (env, obj, method, (jlong)run, args);
ret = (*env)->CallObjectMethod (env, obj, method, (jlong)run, name, args);
if ((*env)->ExceptionOccurred (env))
{
if (is_debug(ase_awk_getrunawk(run)))
@ -1586,12 +1637,14 @@ static int __handle_bfn (
(*env)->ExceptionClear (env);
(*env)->DeleteLocalRef (env, args);
(*env)->DeleteLocalRef (env, name);
ase_awk_setrunerrnum (run, ASE_AWK_EBFNIMPL);
return -1;
}
(*env)->DeleteLocalRef (env, args);
(*env)->DeleteLocalRef (env, name);
if (ret == NULL)
{
@ -1789,13 +1842,13 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_addfunc (
for (x = 0; x < len; x++) tmp[x] = (ase_char_t)ptr[x];
n = (ase_awk_addfunc (awk, tmp, len, 0,
min_args, max_args, ASE_NULL, __handle_bfn) == NULL)? -1: 0;
min_args, max_args, ASE_NULL, handle_bfn) == NULL)? -1: 0;
free (tmp);
}
else
{
n = (ase_awk_addfunc (awk, (ase_char_t*)ptr, len, 0,
min_args, max_args, ASE_NULL, __handle_bfn) == NULL)? -1: 0;
min_args, max_args, ASE_NULL, handle_bfn) == NULL)? -1: 0;
}

View File

@ -5,20 +5,20 @@ EXPORTS
Java_ase_awk_Awk_close
Java_ase_awk_Awk_parse
Java_ase_awk_Awk_run
Java_ase_awk_Awk_stop
Java_ase_awk_Awk_getmaxdepth
Java_ase_awk_Awk_setmaxdepth
Java_ase_awk_Awk_getoption
Java_ase_awk_Awk_setoption
Java_ase_awk_Awk_getdebug
Java_ase_awk_Awk_setdebug
Java_ase_awk_Awk_addfunc
Java_ase_awk_Awk_delfunc
Java_ase_awk_Awk_setfilename
Java_ase_awk_Awk_setofilename
Java_ase_awk_Awk_strtonum
Java_ase_awk_Awk_valtostr
Java_ase_awk_Awk_strftime
Java_ase_awk_Awk_strfgmtime
Java_ase_awk_Awk_system

View File

@ -1,5 +1,5 @@
/*
* $Id: AseAwk.java,v 1.9 2007/06/24 11:14:58 bacon Exp $
* $Id: AseAwk.java,v 1.10 2007/10/12 16:13:34 bacon Exp $
*/
import java.awt.*;
@ -300,8 +300,7 @@ public class AseAwk extends StdAwk
addFunction ("sleep", 1, 1);
}
public Object bfn_sleep (
long runid, Object[] args) throws ase.awk.Exception
public Object sleep (Context ctx, String name, Object[] args) throws ase.awk.Exception
{
long x = builtinFunctionArgumentToLong (runid, args[0]);
try { Thread.sleep (x * 1000); }