# HG changeset patch # User Paul Aurich # Date 1290312251 28800 # Node ID 87625285de20832fd37d912ab0797210c0922f49 # Parent 36ed99e1ce1ee5119eed18850b2f13f2f666f080 ssl.core: Add __tostring metamethod diff -r 36ed99e1ce1e -r 87625285de20 src/ssl.c --- 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); diff -r 36ed99e1ce1e -r 87625285de20 src/x509.c --- 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;