Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7599027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:33:29+00:00 2026-05-30T22:33:29+00:00

The code related to this question is here: https://github.com/jchester/lua-polarssl/tree/master/src Currently I’m trying to wrap

  • 0

The code related to this question is here: https://github.com/jchester/lua-polarssl/tree/master/src

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).

The API I’m aiming for is of the form:

a_sha512_hash = polarssl.hash.sha512('text')

or more fully

local polarssl = require 'polarssl'
local hash = polarssl.hash

a_sha512_hash = hash.sha512('test')

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:

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;
}

Where CORE_MOD_NAME = ‘polarssl’, HASH_MOD_NAME = ‘polarssl.hash’, HMAC_MOD_NAME = ‘polarssl.hmac’.

When I run a test script similar to the Lua code at the top of this question, I get this:

lua: test.lua:23: attempt to index global 'polarssl' (a nil value)
stack traceback:
    test.lua:23: in main chunk
    [C]: ?

I’ve tried looking for examples of how to achieve this module.submodule approach (eg naim vs luasockets), but everyone seems to have a different way of achieving it. I’m completely lost.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:33:30+00:00Added an answer on May 30, 2026 at 10:33 pm

    everyone seems to have a different way of achieving it.

    That’s Lua; everyone does it their own way. It’s Lua’s greatest strength and greatest weakness: the language provides mechanisms, not policies.

    The first thing you need to do is stop using luaL_register. Yes, I know it’s convenient. But you want something special, and luaL_register isn’t going to help you get it.

    What you want is to create a table that contains a table that contains one or more functions. So… do that.

    Create a table.

    lua_newtable(L);
    

    That was easy. The function pushes a table on to the stack, so our stack now has a table on top of it. This is the table we will return.

    Now, we need to create a new table to go inside the old one.

    lua_newtable(L);
    

    Again, easy. Next, we want to put the function we want to go into that table on the stack.

    lua_pushcfunction(L, hash_sha512);
    

    So the stack has three things: the destination table, the “hash” table (we’ll get to “naming” it in a second), and the function we want to put into the “hash” table.

    So put the function into the hash table.

    lua_setfield(L, -2, "sha512");
    

    This takes whatever is on top of the stack and sets it into the field named “sha512” on the table at the -2 index on the stack. That’s where our “hash” table is. After this function completes, it removes the top item from the stack. This leaves the “hash” table at the top.

    We can repeat the process for the second function:

    lua_pushcfunction(L, hash_sha384);
    lua_setfield(L, -2, "sha384");
    

    Now, we want to put the “hash” table into the table we want to return. That’s done easily enough with another lua_setfield call:

    lua_setfield(L, -2, "hash");
    

    Remember: this function takes whatever is on top of the stack. At this point, the table we want to return (which will be our module’s table) is on the stack.

    We can repeat this process for the “hmac” table:

    lua_newtable(L); //Create "hmac" table
    lua_pushcfunction(L, hmac_sha512);
    lua_setfield(L, -2, "sha512");
    lua_pushcfunction(L, hmac_sha384);  
    lua_setfield(L, -2, "sha384");
    lua_setfield(L, -2, "hmac"); //Put the "hmac" table into our module table
    

    The module’s table now has two entries in it: “hash” and “hmac”. Both are tables with two functions in them.

    We can stick it into the global table with this:

    lua_pushvalue(L, -1);
    lua_setfield(L, LUA_GLOBALSINDEX, "polarssl");
    

    Not every module maker wants to do that. Some prefer to force people to use the local polarssl = require "polarssl" syntax, to avoid polluting the global namespace. It’s up to you.

    But what you must do either way is to return this table. Return 1 from your luaopen function, to let Lua know that there is one return value. The lua_pushvalue call above exists for the sole purpose of copying the table (remember: tables are referenced, so it’s like copying a pointer). That way, when you use lua_setfield, the copy gets removed from the stack, while the original remains to be used as a return value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is related to this one: https://stackoverflow.com/questions/7906356/how-to-solve-a-a-reference-tracked-object-changed-reference-during-deserializat Since the above post has been
This question is related to a previous question of mine That's my current code
I ran the code to create the generically related objects from this demo: http://www.djangoproject.com/documentation/models/generic_relations/
This question is very much related to: A) How to separate CUDA code into
This question is related to the question posted here: Why isn't my custom WCF
I asked and answered a question here that is related to this issue and
This question is related to This question on Aardvark This question on here The
This is related to a chapter from beautiful code . And in that chapter
This issue is somewhat related: Problem with Code Generated by XSD.EXE: Sequence of Elements
I apologize for making my first question not the hard-hitting code-related question I was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.