Note that there are some explanatory texts on larger screens.

plurals
  1. POWrapping a C library for Lua: how do I create nested tables of functions?
    primarykey
    data
    text
    <p>The code related to this question is here: <a href="https://github.com/jchester/lua-polarssl/tree/master/src">https://github.com/jchester/lua-polarssl/tree/master/src</a></p> <p>Currently I'm trying to wrap one part of the PolarSSL library (http://polarssl.org) to give me access to SHA-512 HMACs (luacrypto does not provide this).</p> <p>The API I'm aiming for is of the form:</p> <pre><code>a_sha512_hash = polarssl.hash.sha512('text') </code></pre> <p>or more fully</p> <pre><code>local polarssl = require 'polarssl' local hash = polarssl.hash a_sha512_hash = hash.sha512('test') </code></pre> <p>If you refer to polarssl.c in the link above, you'll see I've written functions that wrap PolarSSL code. Then I'm trying to build the function tables thus:</p> <pre><code>LUA_API int luaopen_polarssl( lua_State *L ) { static const struct luaL_Reg core[] = { { NULL, NULL } }; static const struct luaL_Reg hash_functions[] = { { "sha512", hash_sha512 }, { "sha384", hash_sha384 }, { NULL, NULL } }; static const struct luaL_Reg hmac_functions[] = { { "sha512", hmac_sha512 }, { "sha384", hmac_sha384 }, { NULL, NULL } }; luaL_register( L, CORE_MOD_NAME, core ); luaL_register( L, HASH_MOD_NAME, hash_functions ); luaL_register( L, HMAC_MOD_NAME, hmac_functions ); return 1; } </code></pre> <p>Where CORE_MOD_NAME = 'polarssl', HASH_MOD_NAME = 'polarssl.hash', HMAC_MOD_NAME = 'polarssl.hmac'.</p> <p>When I run a test script similar to the Lua code at the top of this question, I get this:</p> <pre><code>lua: test.lua:23: attempt to index global 'polarssl' (a nil value) stack traceback: test.lua:23: in main chunk [C]: ? </code></pre> <p>I've tried looking for examples of how to achieve this module.submodule approach (eg <a href="https://github.com/jwise/naim/blob/lua-experimental/lua/moon.c#L180">naim</a> vs <a href="https://github.com/sam-github/luasocket/blob/master/src/luasocket.c#L47">luasockets</a>), but everyone seems to have a different way of achieving it. I'm completely lost.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload