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

  • Home
  • SEARCH
  • 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 623625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:06:47+00:00 2026-05-13T19:06:47+00:00

Consider the following example (a simple 2d vector lib). Here there is a single

  • 0

Consider the following example (a simple 2d vector lib). Here there is a single constructor function which returns an object table with methods. My issue with this approach is that it is creating new tables with every constructor. Is there a way to use a single instance of the table but change only the _data field which identifies the point being worked on by the methods? Is this a better approach?

#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

const char* test = 
"p1 = point(410, 680);"
"p2 = point(320, 120);"
"print('dot='..p1:dot(p2));"
"print('cross='..p1:cross(p2));";

typedef struct point_t {
    lua_Number x, y;
} point_t;

point_t* p_new(lua_Number x, lua_Number y) {
    point_t* p = malloc(sizeof(point_t));
    p->x = x;
    p->y = y;
    return p;
}

void lua_settabledata(lua_State *L , char * key , void * value) {
    lua_pushstring(L, key);
    lua_pushlightuserdata(L, value);
    lua_settable(L, -3);
}

void lua_settablefunction(lua_State *L, char * key , lua_CFunction value) {
    lua_pushstring(L, key);
    lua_pushcfunction(L, value);
    lua_settable(L, -3);
}

point_t* lua_topoint(lua_State *L, int index) {
    point_t* p;
    lua_pushstring(L, "_data");
    lua_gettable(L, index);
    p = lua_touserdata(L, -1);
    lua_pop(L, 1);
    assert(p);
    return p;
}

int l_dot(lua_State *L) {
    point_t* p1 = lua_topoint(L, 1);
    point_t* p2 = lua_topoint(L, 2);
    lua_pushnumber(L, p1->x*p2->x + p1->y*p2->y);
    return 1;
}

int l_cross(lua_State *L) {
    point_t* p1 = lua_topoint(L, 1);
    point_t* p2 = lua_topoint(L, 2);
    lua_pushnumber(L, p1->x*p2->y - p1->y*p2->x);
    return 1;
}

int l_setx(lua_State *L) {
    point_t* p = lua_topoint(L, 1);
    p->x = lua_tonumber(L, 2);
    return 0;
}

int l_sety(lua_State *L) {
    point_t* p = lua_topoint(L, 1);
    p->y = lua_tonumber(L, 2);
    return 0;
}

int l_getx(lua_State *L) {
    point_t* p = lua_topoint(L, 1);
    lua_pushnumber(L, p->x);
    return 1;
}

int l_gety(lua_State *L) {
    point_t* p = lua_topoint(L, 1);
    lua_pushnumber(L, p->y);
    return 1;
}

int l_point(lua_State* L) {
    lua_Number x = lua_tonumber(L, 1);
    lua_Number y = lua_tonumber(L, 2);
    lua_newtable(L);
    lua_settabledata(L , "_data", p_new(x, y));
    lua_settablefunction(L , "dot", l_dot);
    lua_settablefunction(L , "cross", l_cross);
    lua_settablefunction(L , "setx", l_setx);
    lua_settablefunction(L , "sety", l_sety);
    lua_settablefunction(L , "getx", l_getx);
    lua_settablefunction(L , "gety", l_gety);
    return 1;
}

int main() {
    lua_State* L = lua_open();
    luaL_openlibs(L);
    lua_register(L, "point", l_point);
    if (luaL_loadstring(L, test) || lua_pcall(L, 0, 0, 0))
       printf("error: %s", lua_tostring(L, -1));
    getchar();
    return 0;
}
  • 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-13T19:06:47+00:00Added an answer on May 13, 2026 at 7:06 pm

    I’ve just skimmed your code, but it looks like each of your objects is a table containing a light userdatum with the instance data, as well as a bunch of C functions wrapped into Lua closures. Yes, this is very inefficient. A first improvement would be to not make a separate closure for each instance’s use of the same C function. But we can do much better than even that.

    You can’t do what it sounds like you’re imagining: you can’t have a single table shared between objects but with one field different for each object. If one field is different then you’ve got a different table.

    But you can divide up the shared data and the instance data in ways that are more efficient.

    There’s no need to create a table to hold your instance data. Just make your allocated structure into a (full, not light) userdatum. This is a bit smaller than a table. (I think 40 bytes vs 64 bytes on x86_64; plus the size of your allocated structure.)

    You’ll put all your methods into a single method table, and associate that one table with all of the userdata you return. Shared data can be associated with objects in a variety of ways. Since you want the methods to be accessible in Lua, you’re going to want the method table to be assigned to M.__index, where M is the metatable of each instance object. All the objects can be assigned a single shared M, and M can contain the methods itself if you like (then M.__index will just be M).

    Also in the metatable you’ll want to put a __gc method, to free your allocated struct when the object is garbage-collected.

    Have a look at the chapter of Progamming in Lua on userdata. Also take a look at the lv3 package at lhf’s site. (He’s one of the Lua authors.)

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

Sidebar

Ask A Question

Stats

  • Questions 301k
  • Answers 301k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This should work. Uri uri = Uri.fromFile("/blah/myimage.jpg"); Intent intent =… May 13, 2026 at 8:08 pm
  • Editorial Team
    Editorial Team added an answer The C++ standard defines what an array is and its… May 13, 2026 at 8:08 pm
  • Editorial Team
    Editorial Team added an answer You can try Mirrorrr: http://code.google.com/p/mirrorrr/ Or Masher Nations, Itube Appengine,… May 13, 2026 at 8:08 pm

Related Questions

In Oracle, the number of rows returned in an arbitrary query can be limited
How can I access the last element of keys in a hash without having
I'm building a concert calendar that's very heavy on javascript (using jQuery). I'm having
I have two interrupt service routines (ISR) which basically do the exact same thing
I am facing a class resolution issue while trying to make my architecture flexible.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.