Question: I have a block of code that looks like this:

```
lua_newtable(L);

lua_pushstring(L, "gid");
lua_pushinteger(L, info[i].codepoint);
lua_settable(L, -3);
lua_pushstring(L, "cl");
lua_pushinteger(L, info[i].cluster);
lua_settable(L, -3);
lua_pushstring(L, "ax");
lua_pushnumber(L, pos[i].x_advance);
lua_settable(L, -3);
lua_pushstring(L, "ay");
lua_pushnumber(L, pos[i].y_advance);
lua_settable(L, -3);
lua_pushstring(L, "dx");
lua_pushnumber(L, pos[i].x_offset);
lua_settable(L, -3);
lua_pushstring(L, "dy");
lua_pushnumber(L, pos[i].y_offset);
lua_settable(L, -3);
lua_pushstring(L, "w");
lua_pushinteger(L, extents.width);
lua_settable(L, -3);
lua_pushstring(L, "h");
lua_pushinteger(L, extents.height);
lua_settable(L, -3);
lua_pushstring(L, "yb");
lua_pushinteger(L, extents.y_bearing);
lua_settable(L, -3);
lua_pushstring(L, "xb");
lua_pushinteger(L, extents.x_bearing);
lua_settable(L, -3);

```

What I am doing is just setting a few fields inside a table. The fields are either strings or numbers. Note how repetitive the code is.

Is there a way to make this a bit cleaner, maybe using C macros?

Answer: 
You can reduce the length by 1/3 by replacing

```
lua_pushstring(L, "cl");
lua_pushinteger(L, info[i].cluster);
lua_settable(L, -3);

```

by

```
lua_pushinteger(L, info[i].cluster);
lua_setfield(L, -2, "cl");

```

and so on.
You can use the C preprocessor to apply a C macro to a list of tuples:

```
#define INFO_FIELDS( _ ) \
  _( codepoint, "gid", integer ) \
  _( cluster, "cl", integer )

#define POS_FIELDS( _ ) \
  _( x_advance, "ax", number ) \
  _( y_advance, "ay", number ) \
  _( x_offset, "dx", number ) \
  _( y_offset, "dy", number )

#define EXTENT_FIELDS( _ ) \
  _( width, "w", integer ) \
  _( height, "h", integer ) \
  _( y_bearing, "yb", integer ) \
  _( x_bearing, "xb", integer ) \

#define GEN( _m, _n, _t ) \
  (lua_push##_t( L, info[i]._m ), lua_setfield( L, -2, _n ));
INFO_FIELDS( GEN )
#undef GEN

#define GEN( _m, _n, _t ) \
  (lua_push##_t( L, pos[i]._m ), lua_setfield( L, -2, _n ));
POS_FIELDS( GEN )
#undef GEN

#define GEN( _m, _n, _t ) \
  (lua_push##_t( L, extents._m ), lua_setfield( L, -2, _n ));
EXTENT_FIELDS( GEN )
#undef GEN

```

I'm not sure it pays off in your case, but it shines if you can use the lists multiple times (e.g. to read values from the stack and assign them to struct fields, or to define the C structs in the first place).