ssl.core: Add __tostring metamethod

Sat, 20 Nov 2010 20:04:11 -0800

author
Paul Aurich <paul@darkrain42.org>
date
Sat, 20 Nov 2010 20:04:11 -0800
changeset 31
87625285de20
parent 30
36ed99e1ce1e
child 32
c47594a84f04

ssl.core: Add __tostring metamethod

src/ssl.c file | annotate | diff | comparison | revisions
src/x509.c file | annotate | diff | comparison | revisions
--- a/src/ssl.c	Sat Nov 20 20:04:11 2010 -0800
+++ b/src/ssl.c	Sat Nov 20 20:04:11 2010 -0800
@@ -60,6 +60,16 @@
 }
 
 /**
+ * Object information -- tostring metamethod
+ */
+static int meth_tostring(lua_State *L)
+{
+  p_ssl ssl = (p_ssl)lua_touserdata(L, 1);
+  lua_pushfstring(L, "SSL connection: %p", ssl);
+  return 1;
+}
+
+/**
  * Perform the TLS/SSL handshake
  */
 static int handshake(p_ssl ssl)
@@ -455,7 +465,7 @@
 /**
  * SSL metamethods 
  */
-static luaL_Reg meta[] = {
+static luaL_Reg methods[] = {
   {"close",             meth_close},
   {"getfd",             meth_getfd},
   {"dirty",             meth_dirty},
@@ -483,6 +493,15 @@
 };
 
 /**
+ * Context metamethods.
+ */
+static luaL_Reg meta[] = {
+  {"__gc",       meth_destroy},
+  {"__tostring", meth_tostring},
+  {NULL, NULL}
+};
+
+/**
  * Initialize modules
  */
 LUASEC_API int luaopen_ssl_core(lua_State *L)
@@ -499,11 +518,11 @@
 
   /* Register the functions and tables */
   luaL_newmetatable(L, "SSL:Connection");
+  luaL_register(L, NULL, meta);
+
   lua_newtable(L);
-  luaL_register(L, NULL, meta);
+  luaL_register(L, NULL, methods);
   lua_setfield(L, -2, "__index");
-  lua_pushcfunction(L, meth_destroy);
-  lua_setfield(L, -2, "__gc");
 
   luaL_register(L, "ssl.core", funcs);
   lua_pushnumber(L, SOCKET_INVALID);
--- a/src/x509.c	Sat Nov 20 20:04:11 2010 -0800
+++ b/src/x509.c	Sat Nov 20 20:04:11 2010 -0800
@@ -264,6 +264,13 @@
   return 0;
 }
 
+int meth_tostring(lua_State *L)
+{
+  X509 *cert = luasec_to_x509(L, 1);
+  lua_pushfstring(L, "X509 certificate: %p", cert);
+  return 1;
+}
+
 int cert_from_pem(lua_State* L)
 {
   X509 *cert;
@@ -283,7 +290,7 @@
 /**
  * Certificate metamethods
  */
-static luaL_Reg meta[] = {
+static luaL_Reg methods[] = {
   {"subject",       meth_subject},
   {"issuer",        meth_issuer},
   {"extensions",    meth_extensions},
@@ -301,15 +308,24 @@
   {NULL,            NULL}
 };
 
+/**
+ * Context metamethods.
+ */
+static luaL_Reg meta[] = {
+  {"__gc",       meth_destroy},
+  {"__tostring", meth_tostring},
+  {NULL, NULL}
+};
+
 LUASEC_API int luaopen_ssl_x509(lua_State *L)
 {
   /* Register the functions and tables */
   luaL_newmetatable(L, "SSL:Certificate");
+  luaL_register(L, NULL, meta);
+
   lua_newtable(L);
-  luaL_register(L, NULL, meta);
+  luaL_register(L, NULL, methods);
   lua_setfield(L, -2, "__index");
-  lua_pushcfunction(L, meth_destroy);
-  lua_setfield(L, -2, "__gc");
 
   luaL_register(L, "ssl.x509", funcs);
   return 1;

mercurial