Skip to content

Commit

Permalink
refactor(build): Move repeated Lua module code into common library
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed Nov 1, 2023
1 parent d18ff56 commit 1719c47
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 169 deletions.
24 changes: 24 additions & 0 deletions justenough/compat-5.2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <lua.h>
#include <lualib.h>
#include "compat-5.2.h"

#if !defined LUA_VERSION_NUM || LUA_VERSION_NUM == 501
/*
** Adapted from Lua 5.2.0
*/
void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup) {
luaL_checkstack(L, nup + 1, "too many upvalues");
for (; l->name != NULL; l++) { /* fill the table with given functions */
int i;
lua_pushstring(L, l->name);
for (i = 0; i < nup; i++) /* copy upvalues to the top */
lua_pushvalue(L, -(nup + 1));
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
lua_settable(
L,
-(nup +
3)); /* table must be below the upvalues, the name and the closure */
}
lua_pop(L, nup); /* remove upvalues */
}
#endif
6 changes: 6 additions & 0 deletions justenough/compat-5.2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#if LUA_VERSION_NUM==501
/* Lua 5.1 */
#define lua_rawlen lua_objlen lua_strlen
#endif

void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);
24 changes: 2 additions & 22 deletions justenough/fontmetrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <lualib.h>

#include "hb-utils.h"
#include "compat-5.2.h"


int fm_get_typographic_extents (lua_State *L) {
Expand Down Expand Up @@ -45,35 +46,14 @@ int fm_glyphwidth (lua_State* L) {
return 1;
}

#if !defined LUA_VERSION_NUM
/* Lua 5.0 */
#define luaL_Reg luaL_reg
#endif

#if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501
/*
** Adapted from Lua 5.2.0
*/
void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
luaL_checkstack(L, nup+1, "too many upvalues");
for (; l->name != NULL; l++) { /* fill the table with given functions */
int i;
lua_pushstring(L, l->name);
for (i = 0; i < nup; i++) /* copy upvalues to the top */
lua_pushvalue(L, -(nup+1));
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
lua_settable(L, -(nup + 3));
}
lua_pop(L, nup); /* remove upvalues */
}
#endif

static const struct luaL_Reg lib_table [] = {
{"get_typographic_extents", fm_get_typographic_extents},
{"glyphwidth", fm_glyphwidth},
{NULL, NULL}
};


int luaopen_fontmetrics (lua_State *L) {
lua_newtable(L);
luaL_setfuncs(L, lib_table, 0);
Expand Down
3 changes: 1 addition & 2 deletions justenough/hb-utils.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "hb-utils.h"

#include <string.h>
#include <stdlib.h>
#include <hb-ot.h>

#include "hb-utils.h"
#include "silewin32.h"

static hb_variation_t* scan_variation_string(const char* cp1, unsigned int* ret) {
Expand Down
24 changes: 1 addition & 23 deletions justenough/justenoughfontconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <lualib.h>

#include "silewin32.h"
#include "compat-5.2.h"

int je_face_from_options(lua_State* L) {
FcChar8 * font_path, * fullname, * familyname;
Expand Down Expand Up @@ -156,29 +157,6 @@ int je_face_from_options(lua_State* L) {
return 1;
}

#if !defined LUA_VERSION_NUM
/* Lua 5.0 */
#define luaL_Reg luaL_reg
#endif

#if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501
/*
** Adapted from Lua 5.2.0
*/
void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
luaL_checkstack(L, nup+1, "too many upvalues");
for (; l->name != NULL; l++) { /* fill the table with given functions */
int i;
lua_pushstring(L, l->name);
for (i = 0; i < nup; i++) /* copy upvalues to the top */
lua_pushvalue(L, -(nup+1));
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
lua_settable(L, -(nup + 3));
}
lua_pop(L, nup); /* remove upvalues */
}
#endif

static const struct luaL_Reg lib_table [] = {
{"_face", je_face_from_options},
{NULL, NULL}
Expand Down
26 changes: 1 addition & 25 deletions justenough/justenoughharfbuzz.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
#include <lauxlib.h>
#include <lualib.h>

#include "silewin32.h"

#include "hb-utils.h"
#include "silewin32.h"

/* The following function stolen from XeTeX_ext.c */
static hb_tag_t
Expand Down Expand Up @@ -448,29 +447,6 @@ int je_hb_get_table (lua_State *L) {
return 1;
}

#if !defined LUA_VERSION_NUM
/* Lua 5.0 */
#define luaL_Reg luaL_reg
#endif

#if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501
/*
** Adapted from Lua 5.2.0
*/
void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
luaL_checkstack(L, nup+1, "too many upvalues");
for (; l->name != NULL; l++) { /* fill the table with given functions */
int i;
lua_pushstring(L, l->name);
for (i = 0; i < nup; i++) /* copy upvalues to the top */
lua_pushvalue(L, -(nup+1));
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
lua_settable(L, -(nup + 3));
}
lua_pop(L, nup); /* remove upvalues */
}
#endif

static const struct luaL_Reg lib_table [] = {
{"_shape", je_hb_shape},
{"get_glyph_dimensions", je_hb_get_glyph_dimensions},
Expand Down
26 changes: 3 additions & 23 deletions justenough/justenoughicu.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
#include <string.h>
#include <stdlib.h>
#include <assert.h>

#include <unicode/ustring.h>
#include <unicode/ustdio.h>
#include <unicode/unum.h>
#include <unicode/ubrk.h>
#include <unicode/ubidi.h>
#include <unicode/ucol.h>
#include <unicode/utf16.h>

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#include "compat-5.2.h"
#include "silewin32.h"

typedef int32_t (*conversion_function_t)(UChar *dest, int32_t destCapacity, const UChar *src, int32_t srcLength, const char *locale, UErrorCode *pErrorCode);
Expand Down Expand Up @@ -495,29 +498,6 @@ int je_icu_version(lua_State *L) {
return 1;
}

#if !defined LUA_VERSION_NUM
/* Lua 5.0 */
#define luaL_Reg luaL_reg
#endif

#if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501 && !LUAJIT
/*
** Adapted from Lua 5.2.0
*/
void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
luaL_checkstack(L, nup+1, "too many upvalues");
for (; l->name != NULL; l++) { /* fill the table with given functions */
int i;
lua_pushstring(L, l->name);
for (i = 0; i < nup; i++) /* copy upvalues to the top */
lua_pushvalue(L, -(nup+1));
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
lua_settable(L, -(nup + 3));
}
lua_pop(L, nup); /* remove upvalues */
}
#endif

static const struct luaL_Reg lib_table [] = {
{"breakpoints", je_icu_breakpoints},
{"case", je_icu_case},
Expand Down
27 changes: 1 addition & 26 deletions justenough/justenoughlibtexpdf.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

#include <libtexpdf.h>

#if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501
#define lua_rawlen lua_strlen
#endif
#include "compat-5.2.h"

pdf_doc *p = NULL;
double height = 0.0;
Expand Down Expand Up @@ -543,29 +541,6 @@ int je_pdf_version(lua_State *L) {
return 1;
}

#if !defined LUA_VERSION_NUM
/* Lua 5.0 */
#define luaL_Reg luaL_reg
#endif

#if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501
/*
** Adapted from Lua 5.2.0
*/
void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
luaL_checkstack(L, nup+1, "too many upvalues");
for (; l->name != NULL; l++) { /* fill the table with given functions */
int i;
lua_pushstring(L, l->name);
for (i = 0; i < nup; i++) /* copy upvalues to the top */
lua_pushvalue(L, -(nup+1));
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
lua_settable(L, -(nup + 3));
}
lua_pop(L, nup); /* remove upvalues */
}
#endif

static const struct luaL_Reg lib_table [] = {
{"init", je_pdf_init},
{"beginpage", je_pdf_beginpage},
Expand Down
24 changes: 1 addition & 23 deletions justenough/macfonts.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,7 @@
#include <hb.h>
#include <hb-ot.h>


#if !defined LUA_VERSION_NUM
/* Lua 5.0 */
#define luaL_Reg luaL_reg
#endif

#if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501
/*
** Adapted from Lua 5.2.0
*/
static void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
luaL_checkstack(L, nup+1, "too many upvalues");
for (; l->name != NULL; l++) { /* fill the table with given functions */
int i;
lua_pushstring(L, l->name);
for (i = 0; i < nup; i++) /* copy upvalues to the top */
lua_pushvalue(L, -(nup+1));
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
lua_settable(L, -(nup + 3));
}
lua_pop(L, nup); /* remove upvalues */
}
#endif
#include "compat-5.2.h"

#define MAX_NAME_LEN 512

Expand Down
30 changes: 5 additions & 25 deletions justenough/svg.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#include <stdio.h>
#include <string.h>
#include <math.h>
#define NANOSVG_IMPLEMENTATION // Expands implementation
#include "nanosvg.h"

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#define NANOSVG_IMPLEMENTATION // Expands implementation
#include "nanosvg.h"

#include "compat-5.2.h"

static char* safe_append(char* output, int* output_l, int* max_output, char* s2) {
int append_len = strlen(s2) + 1; // strlen doesn't count \0
if (*output_l + append_len > *max_output) {
Expand Down Expand Up @@ -98,29 +101,6 @@ int svg_to_ps(lua_State *L) {
return 3;
}

#if !defined LUA_VERSION_NUM
/* Lua 5.0 */
#define luaL_Reg luaL_reg
#endif

#if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501
/*
** Adapted from Lua 5.2.0
*/
void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
luaL_checkstack(L, nup+1, "too many upvalues");
for (; l->name != NULL; l++) { /* fill the table with given functions */
int i;
lua_pushstring(L, l->name);
for (i = 0; i < nup; i++) /* copy upvalues to the top */
lua_pushvalue(L, -(nup+1));
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
lua_settable(L, -(nup + 3));
}
lua_pop(L, nup); /* remove upvalues */
}
#endif

static const struct luaL_Reg lib_table [] = {
{"svg_to_ps", svg_to_ps},
{NULL, NULL}
Expand Down

0 comments on commit 1719c47

Please sign in to comment.