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 710439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:37:14+00:00 2026-05-14T04:37:14+00:00

I have the follwing C function. How should I wrap it so it can

  • 0

I have the follwing C function. How should I wrap it so it can be called from a Lua script?

typedef struct tagT{
    int a ; 
    int b ;
} type_t;

int lib_a_f_4(type_t *t)
{
     return t->a * t->b ;
}

I know how to wrapr it if the function parameter type were int or char *. Should I use table type for a C structure?

EDIT: I am using SWIG for the wraping , according to this doc, It seems that I should automatically have this function new_type_t(2,3) , but it is not the case.

If you wrap a C structure, it is also
mapped to a Lua userdata. By adding a
metatable to the userdata, this
provides a very natural interface. For
example,

struct Point{ int x,y; };

is used as follows:

p=example.new_Point()
p.x=3
p.y=5
print(p.x,p.y) 3 5

Similar access is provided for unions
and the data members of C++ classes. C
structures are created using a
function new_Point(), but for C++
classes are created using just the
name Point().

  • 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-14T04:37:14+00:00Added an answer on May 14, 2026 at 4:37 am

    I put this together in a hurry. It compiled; then I did a few last-minute edits. I hope it’s close to the right thing. Go through the Lua manual and look at all the unfamiliar functions.

    #include <lua.h>
    #include <lauxlib.h>
    
    const char *metaname = "mine.type_t"; // associated with userdata of type type_t*
    
    typedef struct tagT{
        int a ; 
        int b ;
    }type_t;
    
    
    int lib_a_f_4(type_t *t)
    {
         return t->a * t->b ;
    }
    
    static int lua_lib_a_f_4(lua_State *L) {
      type_t *t = luaL_checkudata(L, 1, metaname);  // check argument type
      lua_pushnumber(L, (lua_Number)lib_a_f_4(t));
      return 1;
    }
    
    static int lua_new_t(lua_State *L) { // get Lua to allocate an initialize a type_t*
      int a = luaL_checkint(L, 1);
      int b = luaL_checkint(L, 2);
      type_t *t = lua_newuserdata(L, sizeof(*t));
      luaL_getmetatable(L, metaname);
      lua_setmetatable(L, -2);
      t->a = a;
      t->b = b;
      return 1;
    }
    
    static const struct luaL_reg functions[] = {
      { "lib_a_f_4", lua_lib_a_f_4 },
      { "new_t", lua_new_t },
      { NULL, NULL }
    };
    
    int mylib_open(lua_State *L) {
      luaL_register(L, "mylib", functions);
      luaL_newmetatable(L, metaname);
      lua_pop(L, 1);
      return 1;
    }
    
    //compile and use it in lua
    root@pierr-desktop:/opt/task/dt/lua/try1# gcc -shared -o mylib.so -I/usr/include/lua5.1/ -llua *.c -ldl
    root@pierr-desktop:/opt/task/dt/lua/try1# lua
    Lua 5.1.3  Copyright (C) 1994-2008 Lua.org, PUC-Rio
    > require("mylib")
    > t=mylib.new_t(2,3)
    > mylib.lib_a_f_4(t)
    > print(mylib.lib_a_f_4(t))
    6
    > 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.