1998-01-16 15:19:09 +03:00
|
|
|
/*
|
|
|
|
* tcltklib.c
|
|
|
|
* Aug. 27, 1997 Y. Shigehiro
|
|
|
|
* Oct. 24, 1997 Y. Matsumoto
|
|
|
|
*/
|
|
|
|
|
2000-07-06 11:21:26 +04:00
|
|
|
#include "ruby.h"
|
|
|
|
#include "rubysig.h"
|
|
|
|
#undef EXTERN /* avoid conflict with tcl.h of tcl8.2 or before */
|
1998-01-16 15:19:09 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <tcl.h>
|
|
|
|
#include <tk.h>
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
#ifdef __MACOS__
|
|
|
|
# include <tkMac.h>
|
|
|
|
# include <Quickdraw.h>
|
|
|
|
#endif
|
1998-01-16 15:19:09 +03:00
|
|
|
|
1999-08-13 09:37:52 +04:00
|
|
|
/* for ruby_debug */
|
1999-01-20 07:59:39 +03:00
|
|
|
|
1999-08-13 09:37:52 +04:00
|
|
|
#define DUMP1(ARG1) if (ruby_debug) { fprintf(stderr, "tcltklib: %s\n", ARG1);}
|
|
|
|
#define DUMP2(ARG1, ARG2) if (ruby_debug) { fprintf(stderr, "tcltklib: ");\
|
1998-01-16 15:19:09 +03:00
|
|
|
fprintf(stderr, ARG1, ARG2); fprintf(stderr, "\n"); }
|
|
|
|
/*
|
|
|
|
#define DUMP1(ARG1)
|
|
|
|
#define DUMP2(ARG1, ARG2)
|
|
|
|
*/
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
/* for callback break & continue */
|
1999-08-13 09:37:52 +04:00
|
|
|
static VALUE eTkCallbackBreak;
|
|
|
|
static VALUE eTkCallbackContinue;
|
|
|
|
|
|
|
|
static VALUE ip_invoke_real _((int, VALUE*, VALUE));
|
1999-01-20 07:59:39 +03:00
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
/* from tkAppInit.c */
|
|
|
|
|
2000-05-13 20:13:31 +04:00
|
|
|
#if !defined __MINGW32__
|
1998-01-16 15:19:09 +03:00
|
|
|
/*
|
|
|
|
* The following variable is a special hack that is needed in order for
|
|
|
|
* Sun shared libraries to be used for Tcl.
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern int matherr();
|
|
|
|
int *tclDummyMathPtr = (int *) matherr;
|
2000-05-13 20:13:31 +04:00
|
|
|
#endif
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
/*---- module TclTkLib ----*/
|
|
|
|
|
1999-08-13 09:37:52 +04:00
|
|
|
struct invoke_queue {
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE obj;
|
|
|
|
int done;
|
|
|
|
VALUE result;
|
|
|
|
VALUE thread;
|
|
|
|
struct invoke_queue *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct invoke_queue *iqueue;
|
|
|
|
static VALUE main_thread;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
/* Tk_ThreadTimer */
|
1999-08-13 09:37:52 +04:00
|
|
|
static Tcl_TimerToken timer_token;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
/* timer callback */
|
1999-08-13 09:37:52 +04:00
|
|
|
static void
|
|
|
|
_timer_for_tcl(clientData)
|
|
|
|
ClientData clientData;
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
1999-08-13 09:37:52 +04:00
|
|
|
struct invoke_queue *q, *tmp;
|
|
|
|
VALUE thread;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
1999-08-13 09:37:52 +04:00
|
|
|
Tk_DeleteTimerHandler(timer_token);
|
2000-02-01 06:12:21 +03:00
|
|
|
timer_token = Tk_CreateTimerHandler(100, _timer_for_tcl, (ClientData)0);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
1999-08-13 09:37:52 +04:00
|
|
|
CHECK_INTS;
|
|
|
|
q = iqueue;
|
|
|
|
while (q) {
|
|
|
|
tmp = q;
|
|
|
|
q = q->next;
|
|
|
|
if (!tmp->done) {
|
|
|
|
tmp->done = 1;
|
|
|
|
tmp->result = ip_invoke_real(tmp->argc, tmp->argv, tmp->obj);
|
|
|
|
thread = tmp->thread;
|
|
|
|
tmp = tmp->next;
|
|
|
|
rb_thread_run(thread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rb_thread_schedule();
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
/* execute Tk_MainLoop */
|
|
|
|
static VALUE
|
1999-08-13 09:37:52 +04:00
|
|
|
lib_mainloop(self)
|
|
|
|
VALUE self;
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
2000-02-01 06:12:21 +03:00
|
|
|
timer_token = Tk_CreateTimerHandler(100, _timer_for_tcl, (ClientData)0);
|
1998-01-16 15:19:09 +03:00
|
|
|
DUMP1("start Tk_Mainloop");
|
1999-08-13 09:37:52 +04:00
|
|
|
Tk_MainLoop();
|
1998-01-16 15:19:09 +03:00
|
|
|
DUMP1("stop Tk_Mainloop");
|
1999-08-13 09:37:52 +04:00
|
|
|
Tk_DeleteTimerHandler(timer_token);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---- class TclTkIp ----*/
|
|
|
|
struct tcltkip {
|
|
|
|
Tcl_Interp *ip; /* the interpreter */
|
|
|
|
int return_value; /* return value */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Tcl command `ruby' */
|
|
|
|
static VALUE
|
1999-08-13 09:37:52 +04:00
|
|
|
ip_eval_rescue(failed, einfo)
|
|
|
|
VALUE *failed;
|
|
|
|
VALUE einfo;
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
|
|
|
*failed = einfo;
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1999-01-20 07:59:39 +03:00
|
|
|
#if TCL_MAJOR_VERSION >= 8
|
1999-08-13 09:37:52 +04:00
|
|
|
ip_ruby(clientData, interp, argc, argv)
|
|
|
|
ClientData clientData;
|
|
|
|
Tcl_Interp *interp;
|
|
|
|
int argc;
|
|
|
|
Tcl_Obj *CONST argv[];
|
1999-01-20 07:59:39 +03:00
|
|
|
#else
|
1999-08-13 09:37:52 +04:00
|
|
|
ip_ruby(clientData, interp, argc, argv)
|
|
|
|
ClientData clientData;
|
|
|
|
Tcl_Interp *interp;
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
1999-01-20 07:59:39 +03:00
|
|
|
#endif
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
|
|
|
VALUE res;
|
|
|
|
int old_trapflg;
|
|
|
|
VALUE failed = 0;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *arg;
|
|
|
|
int dummy;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
/* ruby command has 1 arg. */
|
|
|
|
if (argc != 2) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eArgError, "wrong # of arguments (%d for 1)", argc);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
/* get C string from Tcl object */
|
|
|
|
#if TCL_MAJOR_VERSION >= 8
|
|
|
|
arg = Tcl_GetStringFromObj(argv[1], &dummy);
|
|
|
|
#else
|
|
|
|
arg = argv[1];
|
|
|
|
#endif
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
/* evaluate the argument string by ruby */
|
1999-01-20 07:59:39 +03:00
|
|
|
DUMP2("rb_eval_string(%s)", arg);
|
|
|
|
old_trapflg = rb_trap_immediate;
|
|
|
|
rb_trap_immediate = 0;
|
|
|
|
res = rb_rescue(rb_eval_string, (VALUE)arg, ip_eval_rescue, (VALUE)&failed);
|
|
|
|
rb_trap_immediate = old_trapflg;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Tcl_ResetResult(interp);
|
1998-01-16 15:19:09 +03:00
|
|
|
if (failed) {
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE eclass = CLASS_OF(failed);
|
|
|
|
Tcl_AppendResult(interp, STR2CSTR(failed), (char*)NULL);
|
|
|
|
if (eclass == eTkCallbackBreak) {
|
1999-08-13 09:37:52 +04:00
|
|
|
return TCL_BREAK;
|
1999-01-20 07:59:39 +03:00
|
|
|
} else if (eclass == eTkCallbackContinue) {
|
1999-08-13 09:37:52 +04:00
|
|
|
return TCL_CONTINUE;
|
1999-01-20 07:59:39 +03:00
|
|
|
} else {
|
1999-08-13 09:37:52 +04:00
|
|
|
return TCL_ERROR;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* result must be string or nil */
|
|
|
|
if (NIL_P(res)) {
|
|
|
|
DUMP1("(rb_eval_string result) nil");
|
|
|
|
return TCL_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy result to the tcl interpreter */
|
1999-01-20 07:59:39 +03:00
|
|
|
DUMP2("(rb_eval_string result) %s", STR2CSTR(res));
|
1998-01-16 15:19:09 +03:00
|
|
|
DUMP1("Tcl_AppendResult");
|
1999-01-20 07:59:39 +03:00
|
|
|
Tcl_AppendResult(interp, STR2CSTR(res), (char *)NULL);
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
return TCL_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* destroy interpreter */
|
|
|
|
static void
|
1999-08-13 09:37:52 +04:00
|
|
|
ip_free(ptr)
|
|
|
|
struct tcltkip *ptr;
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
|
|
|
DUMP1("Tcl_DeleteInterp");
|
|
|
|
Tcl_DeleteInterp(ptr->ip);
|
1999-01-20 07:59:39 +03:00
|
|
|
free(ptr);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create and initialize interpreter */
|
|
|
|
static VALUE
|
1999-08-13 09:37:52 +04:00
|
|
|
ip_new(self)
|
|
|
|
VALUE self;
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
|
|
|
struct tcltkip *ptr; /* tcltkip data struct */
|
|
|
|
VALUE obj; /* newly created object */
|
|
|
|
|
|
|
|
/* create object */
|
|
|
|
obj = Data_Make_Struct(self, struct tcltkip, 0, ip_free, ptr);
|
|
|
|
ptr->return_value = 0;
|
|
|
|
|
|
|
|
/* from Tk_Main() */
|
|
|
|
DUMP1("Tcl_CreateInterp");
|
|
|
|
ptr->ip = Tcl_CreateInterp();
|
|
|
|
|
|
|
|
/* from Tcl_AppInit() */
|
|
|
|
DUMP1("Tcl_Init");
|
|
|
|
if (Tcl_Init(ptr->ip) == TCL_ERROR) {
|
1999-08-13 09:37:52 +04:00
|
|
|
rb_raise(rb_eRuntimeError, "%s", ptr->ip->result);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
DUMP1("Tk_Init");
|
|
|
|
if (Tk_Init(ptr->ip) == TCL_ERROR) {
|
1999-08-13 09:37:52 +04:00
|
|
|
rb_raise(rb_eRuntimeError, "%s", ptr->ip->result);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
DUMP1("Tcl_StaticPackage(\"Tk\")");
|
|
|
|
Tcl_StaticPackage(ptr->ip, "Tk", Tk_Init,
|
|
|
|
(Tcl_PackageInitProc *) NULL);
|
|
|
|
|
|
|
|
/* add ruby command to the interpreter */
|
1999-01-20 07:59:39 +03:00
|
|
|
#if TCL_MAJOR_VERSION >= 8
|
|
|
|
DUMP1("Tcl_CreateObjCommand(\"ruby\")");
|
2000-07-06 11:21:26 +04:00
|
|
|
Tcl_CreateObjCommand(ptr->ip, "ruby", ip_ruby, (ClientData)NULL,
|
1999-01-20 07:59:39 +03:00
|
|
|
(Tcl_CmdDeleteProc *)NULL);
|
|
|
|
#else
|
1998-01-16 15:19:09 +03:00
|
|
|
DUMP1("Tcl_CreateCommand(\"ruby\")");
|
2000-07-06 11:21:26 +04:00
|
|
|
Tcl_CreateCommand(ptr->ip, "ruby", ip_ruby, (ClientData)NULL,
|
1998-01-16 15:19:09 +03:00
|
|
|
(Tcl_CmdDeleteProc *)NULL);
|
1999-01-20 07:59:39 +03:00
|
|
|
#endif
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* eval string in tcl by Tcl_Eval() */
|
|
|
|
static VALUE
|
1999-08-13 09:37:52 +04:00
|
|
|
ip_eval(self, str)
|
|
|
|
VALUE self;
|
|
|
|
VALUE str;
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
char *s;
|
1998-01-16 15:19:09 +03:00
|
|
|
char *buf; /* Tcl_Eval requires re-writable string region */
|
|
|
|
struct tcltkip *ptr; /* tcltkip data struct */
|
|
|
|
|
|
|
|
/* get the data struct */
|
|
|
|
Data_Get_Struct(self, struct tcltkip, ptr);
|
|
|
|
|
|
|
|
/* call Tcl_Eval() */
|
1999-01-20 07:59:39 +03:00
|
|
|
s = STR2CSTR(str);
|
|
|
|
buf = ALLOCA_N(char, strlen(s)+1);
|
|
|
|
strcpy(buf, s);
|
1998-01-16 15:19:09 +03:00
|
|
|
DUMP2("Tcl_Eval(%s)", buf);
|
|
|
|
ptr->return_value = Tcl_Eval(ptr->ip, buf);
|
|
|
|
if (ptr->return_value == TCL_ERROR) {
|
1999-08-13 09:37:52 +04:00
|
|
|
rb_raise(rb_eRuntimeError, "%s", ptr->ip->result);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
DUMP2("(TCL_Eval result) %d", ptr->return_value);
|
|
|
|
|
|
|
|
/* pass back the result (as string) */
|
1999-01-20 07:59:39 +03:00
|
|
|
return(rb_str_new2(ptr->ip->result));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 09:37:52 +04:00
|
|
|
ip_toUTF8(self, str, encodename)
|
|
|
|
VALUE self;
|
|
|
|
VALUE str;
|
|
|
|
VALUE encodename;
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
1999-08-13 09:37:52 +04:00
|
|
|
#ifdef TCL_UTF_MAX
|
|
|
|
Tcl_Interp *interp;
|
|
|
|
Tcl_Encoding encoding;
|
|
|
|
Tcl_DString dstr;
|
|
|
|
struct tcltkip *ptr;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
Data_Get_Struct(self,struct tcltkip, ptr);
|
|
|
|
interp = ptr->ip;
|
|
|
|
|
|
|
|
encoding = Tcl_GetEncoding(interp,STR2CSTR(encodename));
|
|
|
|
buf = ALLOCA_N(char,strlen(STR2CSTR(str))+1);
|
|
|
|
strcpy(buf,STR2CSTR(str));
|
|
|
|
|
|
|
|
Tcl_DStringInit(&dstr);
|
|
|
|
Tcl_DStringFree(&dstr);
|
|
|
|
Tcl_ExternalToUtfDString(encoding,buf,strlen(buf),&dstr);
|
|
|
|
str = rb_str_new2(Tcl_DStringValue(&dstr));
|
|
|
|
|
|
|
|
Tcl_FreeEncoding(encoding);
|
|
|
|
Tcl_DStringFree(&dstr);
|
1999-01-20 07:59:39 +03:00
|
|
|
#endif
|
1999-08-13 09:37:52 +04:00
|
|
|
return str;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 09:37:52 +04:00
|
|
|
ip_fromUTF8(self, str, encodename)
|
|
|
|
VALUE self;
|
|
|
|
VALUE str;
|
|
|
|
VALUE encodename;
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
1999-08-13 09:37:52 +04:00
|
|
|
#ifdef TCL_UTF_MAX
|
|
|
|
Tcl_Interp *interp;
|
|
|
|
Tcl_Encoding encoding;
|
|
|
|
Tcl_DString dstr;
|
|
|
|
struct tcltkip *ptr;
|
|
|
|
char *buf;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
1999-08-13 09:37:52 +04:00
|
|
|
Data_Get_Struct(self,struct tcltkip, ptr);
|
|
|
|
interp = ptr->ip;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
1999-08-13 09:37:52 +04:00
|
|
|
encoding = Tcl_GetEncoding(interp,STR2CSTR(encodename));
|
|
|
|
buf = ALLOCA_N(char,strlen(STR2CSTR(str))+1);
|
|
|
|
strcpy(buf,STR2CSTR(str));
|
1999-01-20 07:59:39 +03:00
|
|
|
|
1999-08-13 09:37:52 +04:00
|
|
|
Tcl_DStringInit(&dstr);
|
|
|
|
Tcl_DStringFree(&dstr);
|
|
|
|
Tcl_UtfToExternalDString(encoding,buf,strlen(buf),&dstr);
|
|
|
|
str = rb_str_new2(Tcl_DStringValue(&dstr));
|
1999-01-20 07:59:39 +03:00
|
|
|
|
1999-08-13 09:37:52 +04:00
|
|
|
Tcl_FreeEncoding(encoding);
|
|
|
|
Tcl_DStringFree(&dstr);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
#endif
|
1999-08-13 09:37:52 +04:00
|
|
|
return str;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 09:37:52 +04:00
|
|
|
ip_invoke_real(argc, argv, obj)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE obj;
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
|
|
|
struct tcltkip *ptr; /* tcltkip data struct */
|
|
|
|
int i;
|
|
|
|
Tcl_CmdInfo info;
|
|
|
|
char *cmd;
|
|
|
|
char **av = (char **)NULL;
|
|
|
|
#if TCL_MAJOR_VERSION >= 8
|
|
|
|
Tcl_Obj **ov = (Tcl_Obj **)NULL;
|
|
|
|
Tcl_Obj *resultPtr;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* get the data struct */
|
|
|
|
Data_Get_Struct(obj, struct tcltkip, ptr);
|
|
|
|
|
|
|
|
/* get the command name string */
|
|
|
|
cmd = STR2CSTR(argv[0]);
|
|
|
|
|
|
|
|
/* map from the command name to a C procedure */
|
|
|
|
if (!Tcl_GetCommandInfo(ptr->ip, cmd, &info)) {
|
|
|
|
rb_raise(rb_eNameError, "invalid command name `%s'", cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* memory allocation for arguments of this command */
|
|
|
|
#if TCL_MAJOR_VERSION >= 8
|
1999-08-13 09:37:52 +04:00
|
|
|
if (info.isNativeObjectProc) {
|
|
|
|
/* object interface */
|
|
|
|
ov = (Tcl_Obj **)ALLOCA_N(Tcl_Obj *, argc+1);
|
|
|
|
for (i = 0; i < argc; ++i) {
|
|
|
|
char *s = STR2CSTR(argv[i]);
|
|
|
|
ov[i] = Tcl_NewStringObj(s, strlen(s));
|
|
|
|
Tcl_IncrRefCount(ov[i]);
|
|
|
|
}
|
|
|
|
ov[argc] = (Tcl_Obj *)NULL;
|
|
|
|
}
|
|
|
|
else
|
1999-01-20 07:59:39 +03:00
|
|
|
#endif
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
/* string interface */
|
1999-08-13 09:37:52 +04:00
|
|
|
av = (char **)ALLOCA_N(char *, argc+1);
|
|
|
|
for (i = 0; i < argc; ++i) {
|
|
|
|
char *s = STR2CSTR(argv[i]);
|
|
|
|
|
|
|
|
av[i] = ALLOCA_N(char, strlen(s)+1);
|
|
|
|
strcpy(av[i], s);
|
|
|
|
}
|
|
|
|
av[argc] = (char *)NULL;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Tcl_ResetResult(ptr->ip);
|
|
|
|
|
|
|
|
/* Invoke the C procedure */
|
|
|
|
#if TCL_MAJOR_VERSION >= 8
|
1999-08-13 09:37:52 +04:00
|
|
|
if (info.isNativeObjectProc) {
|
|
|
|
int dummy;
|
|
|
|
ptr->return_value = (*info.objProc)(info.objClientData,
|
|
|
|
ptr->ip, argc, ov);
|
|
|
|
|
|
|
|
/* get the string value from the result object */
|
|
|
|
resultPtr = Tcl_GetObjResult(ptr->ip);
|
|
|
|
Tcl_SetResult(ptr->ip, Tcl_GetStringFromObj(resultPtr, &dummy),
|
|
|
|
TCL_VOLATILE);
|
|
|
|
|
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
Tcl_DecrRefCount(ov[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
1999-01-20 07:59:39 +03:00
|
|
|
#endif
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
|
|
|
ptr->return_value = (*info.proc)(info.clientData,
|
|
|
|
ptr->ip, argc, av);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ptr->return_value == TCL_ERROR) {
|
1999-08-13 09:37:52 +04:00
|
|
|
rb_raise(rb_eRuntimeError, "%s", ptr->ip->result);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* pass back the result (as string) */
|
1999-08-13 09:37:52 +04:00
|
|
|
return rb_str_new2(ptr->ip->result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ip_invoke(argc, argv, obj)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
struct invoke_queue *tmp, *p;
|
|
|
|
VALUE result = rb_thread_current();
|
|
|
|
|
|
|
|
if (result == main_thread) {
|
|
|
|
return ip_invoke_real(argc, argv, obj);
|
|
|
|
}
|
|
|
|
tmp = ALLOC(struct invoke_queue);
|
|
|
|
tmp->obj = obj;
|
|
|
|
tmp->argc = argc;
|
|
|
|
tmp->argv = ALLOC_N(VALUE, argc);
|
|
|
|
MEMCPY(tmp->argv, argv, VALUE, argc);
|
|
|
|
tmp->thread = result;
|
|
|
|
tmp->done = 0;
|
|
|
|
|
|
|
|
tmp->next = iqueue;
|
|
|
|
iqueue = tmp;
|
|
|
|
|
|
|
|
rb_thread_stop();
|
|
|
|
result = tmp->result;
|
|
|
|
if (iqueue == tmp) {
|
|
|
|
iqueue = tmp->next;
|
|
|
|
free(tmp->argv);
|
|
|
|
free(tmp);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = iqueue;
|
|
|
|
while (p->next) {
|
|
|
|
if (p->next == tmp) {
|
|
|
|
p->next = tmp->next;
|
|
|
|
free(tmp->argv);
|
|
|
|
free(tmp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
return result;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get return code from Tcl_Eval() */
|
|
|
|
static VALUE
|
1999-08-13 09:37:52 +04:00
|
|
|
ip_retval(self)
|
|
|
|
VALUE self;
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
|
|
|
struct tcltkip *ptr; /* tcltkip data struct */
|
|
|
|
|
|
|
|
/* get the data strcut */
|
|
|
|
Data_Get_Struct(self, struct tcltkip, ptr);
|
|
|
|
|
|
|
|
return (INT2FIX(ptr->return_value));
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
#ifdef __MACOS__
|
|
|
|
static void
|
|
|
|
_macinit()
|
|
|
|
{
|
1999-08-13 09:37:52 +04:00
|
|
|
tcl_macQdPtr = &qd; /* setup QuickDraw globals */
|
|
|
|
Tcl_MacSetEventProc(TkMacConvertEvent); /* setup event handler */
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
/*---- initialization ----*/
|
1999-08-13 09:37:52 +04:00
|
|
|
void
|
|
|
|
Init_tcltklib()
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
|
|
|
VALUE lib = rb_define_module("TclTkLib");
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE ip = rb_define_class("TclTkIp", rb_cObject);
|
|
|
|
|
|
|
|
eTkCallbackBreak = rb_define_class("TkCallbackBreak", rb_eStandardError);
|
|
|
|
eTkCallbackContinue = rb_define_class("TkCallbackContinue",rb_eStandardError);
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
rb_define_module_function(lib, "mainloop", lib_mainloop, 0);
|
|
|
|
|
|
|
|
rb_define_singleton_method(ip, "new", ip_new, 0);
|
|
|
|
rb_define_method(ip, "_eval", ip_eval, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(ip, "_toUTF8",ip_toUTF8,2);
|
|
|
|
rb_define_method(ip, "_fromUTF8",ip_fromUTF8,2);
|
|
|
|
rb_define_method(ip, "_invoke", ip_invoke, -1);
|
1998-01-16 15:19:09 +03:00
|
|
|
rb_define_method(ip, "_return_value", ip_retval, 0);
|
|
|
|
rb_define_method(ip, "mainloop", lib_mainloop, 0);
|
|
|
|
|
1999-08-13 09:37:52 +04:00
|
|
|
main_thread = rb_thread_current();
|
1999-01-20 07:59:39 +03:00
|
|
|
#ifdef __MACOS__
|
|
|
|
_macinit();
|
|
|
|
#endif
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
/*---- initialize tcl/tk libraries ----*/
|
|
|
|
/* from Tk_Main() */
|
|
|
|
DUMP1("Tcl_FindExecutable");
|
|
|
|
Tcl_FindExecutable(RSTRING(rb_argv0)->ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* eof */
|