<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>cexcept.h</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -2,9 +2,9 @@
 #ifndef __LUARPC_CONFIG_H__
 #define __LUARPC_CONFIG_H__
 
-/* #define LUARPC_ENABLE_SOCKET */
+#define LUARPC_ENABLE_SOCKET
 /* #define LUARPC_ENABLE_FIFO  -- not implemented!!! */
-#define LUARPC_ENABLE_SERIAL
+/* #define LUARPC_ENABLE_SERIAL */
 
 /* signed and unsigned 8, 16 and 32 bit types */
 </diff>
      <filename>config.h</filename>
    </modified>
    <modified>
      <diff>@@ -19,6 +19,7 @@
 #include &quot;config.h&quot;
 #include &quot;luarpc_rpc.h&quot;
 
+struct exception_context the_exception_context[1];
 
 static void errorMessage (const char *msg, va_list ap)
 {
@@ -66,31 +67,6 @@ static const char * errorString (int n)
   }
 }
 
-static jmp_buf exception_stack[MAX_NESTED_TRYS];
-volatile static int exception_num_trys = 0;
-volatile static int exception_errnum = 0;
-
-
-/* you can call this when you have just entered or are about to leave a
- * Lua-RPC function from lua itself - this function resets the exception
- * stack, which is not used at all outside Lua-RPC.
- */
-
-static void exception_init( )
-{
-  exception_num_trys = 0;
-  exception_errnum = 0;
-}
-
-/* throw an exception. this will jump to the most recent CATCH block. */
-
-void exception_throw( int n )
-{
-  MYASSERT( exception_num_trys &gt; 0 );
-  exception_errnum = n;
-  exception_num_trys--;
-  longjmp( exception_stack[ exception_num_trys ], 1 );
-}
 
 /****************************************************************************/
 /* transport layer generics */
@@ -201,7 +177,6 @@ static void transport_write_double (Transport *tpt, double x)
 
 void my_lua_error (lua_State *L, const char *errmsg)
 {
-  exception_init();
   lua_pushstring(L,errmsg);
   lua_error (L);
 }
@@ -234,7 +209,7 @@ static int ismetatable_type (lua_State *L, int ud, const char *tname)
 /* read and write lua variables to a transport.
  * these functions do little error handling of their own, but they call transport
  * functions which may throw exceptions, so calls to these functions must be
- * wrapped in a TRY block.
+ * wrapped in a Try block.
  */
 
 enum {
@@ -419,7 +394,7 @@ static int read_variable (Transport *tpt, lua_State *L)
     return 0;
 
   default:
-    THROW (ERR_PROTOCOL); /* unknown type in request */
+    Throw ERR_PROTOCOL; /* unknown type in request */
   }
   return 1;
 }
@@ -503,21 +478,23 @@ static int handle_index (lua_State *L)
 
 static int helper_function (lua_State *L)
 {
+	int e;
+	int freturn = 0;
   Helper *h;
   Transport *tpt;
   MYASSERT (lua_gettop (L) &gt;= 1);
   MYASSERT (lua_isuserdata (L,1) &amp;&amp; ismetatable_type(L, 1, &quot;rpc.helper&quot;));
-  exception_init();
   
   /* get helper object and its transport */
   h = (Helper*) lua_touserdata (L,1);
   tpt = &amp;h-&gt;handle-&gt;tpt;
 
-  TRY
+  Try
 	{
     int i,len,n;
     u32 nret,ret_code;
 
+
     /* first read out any pending return values for old async calls */
     for (; h-&gt;handle-&gt;read_reply_count &gt; 0; h-&gt;handle-&gt;read_reply_count--) {
       ret_code = transport_read_u8 (tpt);   /* return code */
@@ -540,9 +517,8 @@ static int helper_function (lua_State *L)
   			transport_read_string( tpt, err_string, len );
   			err_string[ len ] = 0;
 
-  			ENDTRY;
   			deal_with_error( L, h-&gt;handle, err_string );
-  			return 0;
+  			freturn = 0;
       }
     }
 
@@ -563,8 +539,7 @@ static int helper_function (lua_State *L)
     if ( h-&gt;handle-&gt;async )
 		{
       h-&gt;handle-&gt;read_reply_count++;
-      ENDTRY;
-      return 0;
+      freturn = 0;
     }
 
     /* read return code */
@@ -578,8 +553,7 @@ static int helper_function (lua_State *L)
 			for ( i = 0; i &lt; ( (int ) nret ); i ++ )
 				read_variable( tpt, L );
 			
-      ENDTRY;
-      return nret;
+      freturn = ( int )nret;
     }
     else
 		{
@@ -590,22 +564,22 @@ static int helper_function (lua_State *L)
       transport_read_string( tpt, err_string, len );
       err_string[ len ] = 0;
 
-      ENDTRY;
       deal_with_error( L, h-&gt;handle, err_string );
-      return 0;
+      freturn = 0;
     }
   }
-  CATCH
+  Catch (e)
 	{
-    if ( ERRCODE == ERR_CLOSED )
+    if ( e == ERR_CLOSED )
       my_lua_error( L, &quot;can't refer to a remote function after the handle has been closed&quot; );
     else
 		{
-      deal_with_error( L, h-&gt;handle, errorString( ERRCODE ) );
+      deal_with_error( L, h-&gt;handle, errorString( e ) );
       transport_close( tpt );
     }
     return 0;
   }
+	return freturn;
 }
 
 /****************************************************************************/
@@ -645,10 +619,10 @@ static void server_handle_destroy( ServerHandle *h )
 
 static int rpc_connect( lua_State *L )
 {
+	int e;
   Handle *handle = 0;
   
-  exception_init( );
-  TRY
+  Try
 	{
     char header[ 5 ];
     handle = handle_create (L );
@@ -661,16 +635,13 @@ static int rpc_connect( lua_State *L )
     header[3] = 'C';
     header[4] = RPC_PROTOCOL_VERSION;
     transport_write_string( &amp;handle-&gt;tpt, header, sizeof( header ) );
-    
-    ENDTRY;
-    return 1;
   }
-  CATCH
+  Catch (e)
 	{			
-    deal_with_error( L, 0, errorString( ERRCODE ) );
+    deal_with_error( L, 0, errorString( e ) );
     lua_pushnil( L );
-    return 1;
   }
+	return 1;
 }
 
 
@@ -825,28 +796,26 @@ static void read_function_call( Transport *tpt, lua_State *L )
 
 static ServerHandle *rpc_listen_helper( lua_State *L )
 {
+	int e;
   ServerHandle *handle = 0;
-  exception_init( );
 
-  TRY
+  Try
 	{
     /* make server handle */
     handle = server_handle_create( L );
 
     /* make listening transport */
     transport_open_listener( L, handle );
-
-    ENDTRY;
-    return handle;
   }
-  CATCH
+  Catch (e)
 	{
     if( handle )
 			server_handle_destroy( handle );
 		
-    deal_with_error( L, 0, errorString( ERRCODE ) );
+    deal_with_error( L, 0, errorString( e ) );
     return 0;
   }
+  return handle;
 }
 
 
@@ -905,29 +874,29 @@ static int rpc_peek (lua_State *L)
 
 static void rpc_dispatch_helper( lua_State *L, ServerHandle *handle )
 {
-  exception_init( );
-  TRY 
+	int e;
+	
+  Try 
 	{
     /* if accepting transport is open, read function calls */
     if ( transport_is_open( &amp;handle-&gt;atpt ) )
 		{
-      TRY
+      Try
 			{
 				/* If transport is readable, read a function call */
 				if ( transport_readable( &amp;handle-&gt;atpt ) &amp;&amp; transport_is_open( &amp;handle-&gt;atpt ) )
 				{
   				read_function_call( &amp;handle-&gt;atpt, L );
 				}
-  			ENDTRY;
       }
-      CATCH
+      Catch (e)
 			{
 			  /* if the client has closed the connection, close our side
 			   * gracefully too.
 			   */
   			transport_close( &amp;handle-&gt;atpt );
-  			if( ERRCODE != ERR_EOF &amp;&amp; ERRCODE != ERR_PROTOCOL )
-					THROW( ERRCODE );
+  			if( e != ERR_EOF &amp;&amp; e != ERR_PROTOCOL )
+					Throw e;
       }
     }
     else
@@ -948,19 +917,16 @@ static void rpc_dispatch_helper( lua_State *L, ServerHandle *handle )
       {
         /* bad remote function call header, close the connection */
         transport_close( &amp;handle-&gt;atpt );
-        ENDTRY;
-        return;
       }
     }
-    ENDTRY;
   }
-  CATCH
+  Catch (e)
 	{
 		/* If exception is non-fatal, don't die.  Otherwise deal with error. */
-		if( ERRCODE != ERR_NODATA ) 
+		if( e != ERR_NODATA ) 
 		{
 			server_handle_shutdown( handle );
-			deal_with_error( L, 0, errorString( ERRCODE ) );
+			deal_with_error( L, 0, errorString( e ) );
 		}
   }
 }</diff>
      <filename>luarpc.c</filename>
    </modified>
    <modified>
      <diff>@@ -1,3 +1,5 @@
+#include &quot;cexcept.h&quot;
+
 /****************************************************************************/
 /* handle the differences between winsock and unix */
 
@@ -67,47 +69,8 @@ enum {
 	ERR_BADFNAME = MAXINT - 104
 };
 
-
-/****************************************************************************/
-/* exception handling using setjmp()/longjmp().
- *
- * do this:
- *
- *  TRY {
- *    some_stuff();
- *    THROW (error_code);
- *    // if THROW is not called, you must call ENDTRY before the end of
- *    // the TRY block (this includes before any `return' is called).
- *    ENDTRY;
- *  }
- *  CATCH {
- *    // *all* errors caught here, not just specific ones
- *    there_is_an_error (ERRCODE);
- *    if (dont_handle_here()) THROW (ERRCODE);
- *  }
- */
-
-/* the exception stack. the top of the stack is the environment to longjmp()
- * to if there is a THROW.
- */
-
-#define MAX_NESTED_TRYS 4
-
-#define THROW(errnum) exception_throw (errnum)
-
-#define ERRCODE (exception_errnum)
-
-#define TRY \
-  MYASSERT( exception_num_trys &lt; MAX_NESTED_TRYS ); \
-  exception_num_trys++; \
-  if( setjmp( exception_stack[ exception_num_trys-1 ] ) == 0 )
-
-#define ENDTRY { \
-  MYASSERT (exception_num_trys &gt; 0); \
-  exception_num_trys--; \
-}
-
-#define CATCH else
+define_exception_type(int);
+extern struct exception_context the_exception_context[1];
 
 #define NUM_FUNCNAME_CHARS 4
 
@@ -159,13 +122,11 @@ typedef struct _ServerHandle ServerHandle;
 #define SYNC_FLAG (0x7e)
 
 #define TRANSPORT_VERIFY_OPEN \
-	if (tpt-&gt;fd == INVALID_TRANSPORT) THROW (ERR_CLOSED);
+	if (tpt-&gt;fd == INVALID_TRANSPORT) Throw ERR_CLOSED;
 
 /* Arg &amp; Error Checking Provided to Transport Mechanisms */
 int check_num_args (lua_State *L, int desired_n);
 void deal_with_error (lua_State *L, Handle *h, const char *error_string);
-void exception_throw (int n);
-
 
 /* TRANSPORT API */
 </diff>
      <filename>luarpc_rpc.h</filename>
    </modified>
    <modified>
      <diff>@@ -34,7 +34,7 @@ void transport_open( Transport *tpt, const char *path )
 	tpt-&gt;fd = open(path , O_RDWR | O_NOCTTY );
 	
 	if( tpt-&gt;fd == INVALID_TRANSPORT)
-		THROW ( errno );
+		Throw errno;
 		
 	tcgetattr( tpt-&gt;fd, &amp;options);
 	
@@ -93,10 +93,10 @@ void transport_read_buffer (Transport *tpt, const u8 *buffer, int length)
     n = read ( tpt-&gt;fd, ( void * )buffer, length );
    	
 		if( n == 0 )
-			THROW( ERR_NODATA );
+			Throw ERR_NODATA;
 		
     if( n &lt; 0 )
-			THROW( errno );
+			Throw errno;
    
 		buffer += n;
     length -= n;
@@ -111,7 +111,7 @@ void transport_write_buffer (Transport *tpt, const u8 *buffer, int length)
 	n = write( tpt-&gt;fd, buffer,length );
 
   if ( n != length )
-		THROW ( errno );
+		Throw errno;
 }
 
 /* Check if data is available on connection without reading:
@@ -135,7 +135,7 @@ int transport_readable (Transport *tpt)
 	ret = select( tpt-&gt;fd+1, &amp;rdfs, NULL, NULL, &amp;tv );
 	
 	if ( ret &lt; 0 )
-		THROW( errno );
+		Throw errno;
 		
   return ( ret &gt; 0 );
 }</diff>
      <filename>luarpc_serial.c</filename>
    </modified>
    <modified>
      <diff>@@ -40,6 +40,7 @@
 
 #ifdef LUARPC_ENABLE_SOCKET
 
+
 /****************************************************************************/
 /* handle the differences between winsock and unix */
 
@@ -169,7 +170,7 @@ static int get_port_number (lua_State *L, int i)
 /****************************************************************************/
 /* socket reading and writing functions.
  * the socket functions throw exceptions if there are errors, so you must call
- * them from within a TRY block.
+ * them from within a Try block.
  */
 
 
@@ -193,7 +194,7 @@ void transport_open (Transport *tpt)
 {
   tpt-&gt;fd = socket (PF_INET,SOCK_STREAM,IPPROTO_TCP);
   if (tpt-&gt;fd == INVALID_TRANSPORT) 
-		THROW (sock_errno);
+		Throw sock_errno;
 }
 
 /* close a socket */
@@ -215,7 +216,7 @@ static void transport_connect (Transport *tpt, u32 ip_address, u16 ip_port)
   myname.sin_port = htons (ip_port);
   myname.sin_addr.s_addr = htonl (ip_address);
   if (connect (tpt-&gt;fd, (struct sockaddr *) &amp;myname, sizeof (myname)) != 0)
-    THROW (sock_errno);
+    Throw sock_errno;
 }
 
 
@@ -229,7 +230,7 @@ static void transport_bind (Transport *tpt, u32 ip_address, u16 ip_port)
   myname.sin_port = htons (ip_port);
   myname.sin_addr.s_addr = htonl (ip_address);
   if (bind (tpt-&gt;fd, (struct sockaddr *) &amp;myname, sizeof (myname)) != 0)
-    THROW (sock_errno);
+    Throw sock_errno;
 }
 
 
@@ -240,7 +241,8 @@ static void transport_bind (Transport *tpt, u32 ip_address, u16 ip_port)
 static void transport_listen (Transport *tpt, int maxcon)
 {
   TRANSPORT_VERIFY_OPEN;
-  if (listen (tpt-&gt;fd,maxcon) != 0) THROW (sock_errno);
+  if (listen (tpt-&gt;fd,maxcon) != 0)
+ 		Throw sock_errno;
 }
 
 
@@ -254,7 +256,8 @@ void transport_accept (Transport *tpt, Transport *atpt)
   TRANSPORT_VERIFY_OPEN;
   namesize = sizeof (clientname);
   atpt-&gt;fd = accept (tpt-&gt;fd, (struct sockaddr*) &amp;clientname, &amp;namesize);
-  if (atpt-&gt;fd == INVALID_TRANSPORT) THROW (sock_errno);
+  if (atpt-&gt;fd == INVALID_TRANSPORT) 
+		Throw sock_errno;
 }
 
 
@@ -265,8 +268,10 @@ void transport_read_buffer (Transport *tpt, const u8 *buffer, int length)
   TRANSPORT_VERIFY_OPEN;
   while (length &gt; 0) {
     int n = read (tpt-&gt;fd,(void*) buffer,length);
-    if (n == 0) THROW (ERR_EOF);
-    if (n &lt; 0) THROW (sock_errno);
+    if (n == 0) 
+			Throw ERR_EOF;
+    if (n &lt; 0) 
+			Throw sock_errno;
     buffer += n;
     length -= n;
   }
@@ -279,7 +284,8 @@ void transport_write_buffer (Transport *tpt, const u8 *buffer, int length)
   int n;
   TRANSPORT_VERIFY_OPEN;
   n = write (tpt-&gt;fd,buffer,length);
-  if (n != length) THROW (sock_errno);
+  if (n != length) 
+		Throw sock_errno;
 }
 
 int transport_open_connection(lua_State *L, Handle *handle)</diff>
      <filename>luarpc_socket.c</filename>
    </modified>
    <modified>
      <diff>@@ -17,8 +17,8 @@ function doStuff()
 	rpc.on_error (error_handler);
 	io.write (&quot;error set\n&quot;)
 
-	-- local slave,err = rpc.connect (&quot;localhost&quot;,12345);
-	local slave,err = rpc.connect (&quot;/dev/ttys0&quot;);
+	local slave,err = rpc.connect (&quot;localhost&quot;,12345);
+	-- local slave,err = rpc.connect (&quot;/dev/ttys0&quot;);
 	-- local slave,err = rpc.connect (&quot;/dev/pts/4&quot;);
 	if not slave then
 		io.write (&quot;error: &quot; .. err .. &quot;\n&quot;);</diff>
      <filename>test-client.lua</filename>
    </modified>
    <modified>
      <diff>@@ -32,9 +32,9 @@ testvar = 23
 
 io.write (&quot;server started\n&quot;)
 
-rpc.server (&quot;/dev/ptys0&quot;); -- use for serial mode
+-- rpc.server (&quot;/dev/ptys0&quot;); -- use for serial mode
 -- rpc.server (&quot;/dev/ptmx&quot;); -- use for serial mode
--- rpc.server (12345); -- use for socket mode
+rpc.server (12345); -- use for socket mode
 
 -- an alternative way
 </diff>
      <filename>test-server.lua</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>c2510ae7b1bc675c50fc09c08e037a6e5159a6f8</id>
    </parent>
  </parents>
  <author>
    <name>James Snyder</name>
    <email>jbsnyder@fanplastic.org</email>
  </author>
  <url>http://github.com/jsnyder/luarpc/commit/16e64342a4e62147dacb31025dc6d89dc492ea34</url>
  <id>16e64342a4e62147dacb31025dc6d89dc492ea34</id>
  <committed-date>2009-05-29T13:30:14-07:00</committed-date>
  <authored-date>2009-05-29T13:30:14-07:00</authored-date>
  <message>Swap out exception implementation for more common cexcept.h.

One downside is not being able to return from within a Try block, but there is
some additional flexibility in terms of information carried by exceptions that
seems more useful.</message>
  <tree>0f3b0a22cd9a72ebbdcb84f70e047f0c62fa163d</tree>
  <committer>
    <name>James Snyder</name>
    <email>jbsnyder@fanplastic.org</email>
  </committer>
</commit>
