Procházet zdrojové kódy

hooks for line change and function calls; first version.

Roberto Ierusalimschy před 29 roky
rodič
revize
b2afc410fa
2 změnil soubory, kde provedl 88 přidání a 10 odebrání
  1. 7 3
      luadebug.h
  2. 81 7
      opcode.c

+ 7 - 3
luadebug.h

@@ -2,7 +2,7 @@
 ** LUA - Linguagem para Usuarios de Aplicacao
 ** LUA - Linguagem para Usuarios de Aplicacao
 ** Grupo de Tecnologia em Computacao Grafica
 ** Grupo de Tecnologia em Computacao Grafica
 ** TeCGraf - PUC-Rio
 ** TeCGraf - PUC-Rio
-** $Id: luadebug.h,v 1.1 1995/10/17 14:12:45 roberto Exp roberto $
+** $Id: luadebug.h,v 1.2 1995/10/26 14:21:56 roberto Exp $
 */
 */
 
 
 
 
@@ -11,10 +11,14 @@
 
 
 #include "lua.h"
 #include "lua.h"
 
 
+typedef void (*lua_LHFunction) (int line);
+typedef void (*lua_CHFunction) (lua_Object func, char *file, int line);
+
 lua_Object lua_stackedfunction(int level);
 lua_Object lua_stackedfunction(int level);
 void lua_funcinfo (lua_Object func, char **filename, int *linedefined);
 void lua_funcinfo (lua_Object func, char **filename, int *linedefined);
 int lua_currentline (lua_Object func);
 int lua_currentline (lua_Object func);
-char *getobjname (lua_Object o, char **name);
-
+char *lua_getobjname (lua_Object o, char **name);
+lua_LHFunction lua_setlinehook (lua_LHFunction hook);
+lua_CHFunction lua_setcallhook (lua_CHFunction hook);
 
 
 #endif
 #endif

+ 81 - 7
opcode.c

@@ -3,7 +3,7 @@
 ** TecCGraf - PUC-Rio
 ** TecCGraf - PUC-Rio
 */
 */
 
 
-char *rcs_opcode="$Id: opcode.c,v 3.50 1995/11/16 20:46:24 roberto Exp roberto $";
+char *rcs_opcode="$Id: opcode.c,v 3.50 1995/11/16 20:46:24 roberto Exp $";
 
 
 #include <setjmp.h>
 #include <setjmp.h>
 #include <stdlib.h>
 #include <stdlib.h>
@@ -53,6 +53,11 @@ static int CnResults = 0; /* when Lua calls C, has the number of parameters; */
 static  jmp_buf *errorJmp = NULL; /* current error recover point */
 static  jmp_buf *errorJmp = NULL; /* current error recover point */
 
 
 
 
+/* Hooks */
+static lua_LHFunction line_hook = NULL;
+static lua_CHFunction call_hook = NULL;
+
+
 static StkId lua_execute (Byte *pc, StkId base);
 static StkId lua_execute (Byte *pc, StkId base);
 static void do_call (StkId base, int nResults);
 static void do_call (StkId base, int nResults);
 
 
@@ -64,6 +69,23 @@ Object *luaI_Address (lua_Object o)
 }
 }
 
 
 
 
+/*
+** Functions to change hook functions.
+*/
+lua_LHFunction lua_setlinehook (lua_LHFunction hook)
+{
+  lua_LHFunction temp = line_hook;
+  line_hook = hook;
+  return temp;
+}
+
+lua_CHFunction lua_setcallhook (lua_CHFunction hook)
+{
+  lua_CHFunction temp = call_hook;
+  call_hook = hook;
+  return temp;
+}
+
 
 
 /*
 /*
 ** Init stack
 ** Init stack
@@ -192,6 +214,48 @@ static void open_stack (int nelems)
 }
 }
 
 
 
 
+/*
+** call Line hook
+*/
+static void lineHook (int line)
+{
+  StkId oldBase = CBase;
+  int oldCnResults = CnResults;
+  StkId old_top = CBase = top-stack;
+  CnResults = 0;
+  (*line_hook)(line);
+  top = stack+old_top;
+  CnResults = oldCnResults;
+  CBase = oldBase;
+}
+
+
+/*
+** Call hook
+** The function being called is in [stack+base-1]
+*/
+static void callHook (StkId base, lua_Type type, int isreturn)
+{
+  StkId oldBase = CBase;
+  int oldCnResults = CnResults;
+  StkId old_top = CBase = top-stack;
+  CnResults = 0;
+  if (isreturn)
+    (*call_hook)(LUA_NOOBJECT, "(return)", 0);
+  else
+  {
+    Object *f = stack+base-1;
+    if (type == LUA_T_MARK)
+      (*call_hook)(Ref(f), f->value.tf->fileName, f->value.tf->lineDefined);
+    else
+      (*call_hook)(Ref(f), "(C)", -1);
+  }
+  top = stack+old_top;
+  CnResults = oldCnResults;
+  CBase = oldBase;
+}
+
+
 /*
 /*
 ** Call a C function. CBase will point to the top of the stack,
 ** Call a C function. CBase will point to the top of the stack,
 ** and CnResults is the number of parameters. Returns an index
 ** and CnResults is the number of parameters. Returns an index
@@ -204,8 +268,15 @@ static StkId callC (lua_CFunction func, StkId base)
   StkId firstResult;
   StkId firstResult;
   CnResults = (top-stack) - base;
   CnResults = (top-stack) - base;
   /* incorporate parameters on the stack */
   /* incorporate parameters on the stack */
-  CBase = base+CnResults;
-  (*func)();
+  CBase = base+CnResults;  /* == top-stack */
+  if (call_hook)
+  {
+    callHook (base, LUA_T_CMARK, 0);
+    (*func)();
+    callHook (base, LUA_T_CMARK, 1);
+  }
+  else
+    (*func)();
   firstResult = CBase;
   firstResult = CBase;
   CBase = oldBase;
   CBase = oldBase;
   CnResults = oldCnResults;
   CnResults = oldCnResults;
@@ -784,6 +855,8 @@ static void comparison (lua_Type tag_less, lua_Type tag_equal,
 */
 */
 static StkId lua_execute (Byte *pc, StkId base)
 static StkId lua_execute (Byte *pc, StkId base)
 {
 {
+  if (call_hook)
+    callHook (base, LUA_T_MARK, 0);
  while (1)
  while (1)
  {
  {
   OpCode opcode;
   OpCode opcode;
@@ -1144,10 +1217,10 @@ static StkId lua_execute (Byte *pc, StkId base)
    break;
    break;
 
 
    case RETCODE0:
    case RETCODE0:
-     return base;
-
    case RETCODE:
    case RETCODE:
-     return base+*pc;
+     if (call_hook)
+       callHook (base, LUA_T_MARK, 1);
+     return (base + ((opcode==RETCODE0) ? 0 : *pc));
 
 
    case SETLINE:
    case SETLINE:
    {
    {
@@ -1161,6 +1234,8 @@ static StkId lua_execute (Byte *pc, StkId base)
       (stack+base-1)->tag = LUA_T_LINE;
       (stack+base-1)->tag = LUA_T_LINE;
     }
     }
     (stack+base-1)->value.i = code.w;
     (stack+base-1)->value.i = code.w;
+    if (line_hook)
+      lineHook (code.w);
     break;
     break;
    }
    }
 
 
@@ -1170,4 +1245,3 @@ static StkId lua_execute (Byte *pc, StkId base)
  }
  }
 }
 }
 
 
-