change the api and extend lua user data len

This commit is contained in:
2026-04-23 21:19:33 +02:00
parent 865ac5b979
commit d79c238924
7 changed files with 212 additions and 164 deletions
+70 -23
View File
@@ -5,7 +5,6 @@ static int in_set_defaults = 0;
extern lua_State *L;
extern RDSEncoder* enc;
extern uint8_t unload_refs[33];
static int writing_program = 0;
int lua_get_userdata(lua_State *localL) {
@@ -54,8 +53,6 @@ int lua_set_rds_program_defaults(lua_State *localL) {
return 0;
}
in_set_defaults = 1;
for (int i = 1; i < *unload_refs; i++) luaL_unref(L, LUA_REGISTRYINDEX, unload_refs[i]);
unload_refs[0] = 1;
set_rds_defaults(enc, writing_program);
lua_call_tfunction_nolock("on_init");
lua_call_tfunction_nolock("on_state");
@@ -65,8 +62,6 @@ int lua_set_rds_program_defaults(lua_State *localL) {
int lua_reset_rds(lua_State *localL) {
(void)localL;
for (int i = 1; i < *unload_refs; i++) luaL_unref(L, LUA_REGISTRYINDEX, unload_refs[i]);
unload_refs[0] = 1;
encoder_saveToFile(enc);
encoder_loadFromFile(enc);
@@ -280,19 +275,58 @@ int lua_toggle_rt_ab(lua_State *localL) {
int lua_set_rds_eon(lua_State *localL) {
int eon = luaL_checkinteger(localL, 1);
if(eon >= EONs) return luaL_error(localL, "eon index exceeded");
if (!lua_isboolean(localL, 2)) return luaL_error(localL, "boolean expected, got %s", luaL_typename(localL, 2));
if (!lua_isboolean(localL, 4)) return luaL_error(localL, "boolean expected, got %s", luaL_typename(localL, 4));
if (!lua_isboolean(localL, 5)) return luaL_error(localL, "boolean expected, got %s", luaL_typename(localL, 5));
luaL_checktype(localL, 8, LUA_TTABLE);
enc->data[writing_program].eon[eon].enabled = lua_toboolean(localL, 2);
enc->data[writing_program].eon[eon].pi = luaL_checkinteger(localL, 3);
enc->data[writing_program].eon[eon].tp = lua_toboolean(localL, 4);
enc->data[writing_program].eon[eon].ta = lua_toboolean(localL, 5);
enc->data[writing_program].eon[eon].pty = luaL_checkinteger(localL, 6);
_strncpy(enc->data[writing_program].eon[eon].ps, luaL_checklstring(localL, 7, NULL), 8);
int n = lua_rawlen(localL, 8);
if(!lua_istable(localL, 2)) return luaL_error(localL, "table expected, got %s", luaL_typename(localL, 2));
lua_getfield(localL, 2, "enabled");
if(!lua_isnil(localL, -1)) {
luaL_checktype(localL, -1, LUA_TBOOLEAN);
enc->data[writing_program].eon[eon].enabled = lua_toboolean(localL, -1);
} lua_pop(localL, 1);
lua_getfield(localL, 2, "tp");
if(!lua_isnil(localL, -1)) {
luaL_checktype(localL, -1, LUA_TBOOLEAN);
enc->data[writing_program].eon[eon].tp = lua_toboolean(localL, -1);
} lua_pop(localL, 1);
lua_getfield(localL, 2, "ta");
if(!lua_isnil(localL, -1)) {
luaL_checktype(localL, -1, LUA_TBOOLEAN);
enc->data[writing_program].eon[eon].ta = lua_toboolean(localL, -1);
} lua_pop(localL, 1);
lua_getfield(localL, 2, "pi");
if(!lua_isnil(localL, -1)) {
lua_Integer pi = luaL_checkinteger(localL, -1);
lua_pop(localL, 1);
enc->data[writing_program].eon[eon].pi = pi;
} else lua_pop(localL, 1);
lua_getfield(localL, 2, "pty");
if(!lua_isnil(localL, -1)) {
lua_Integer pty = luaL_checkinteger(localL, -1);
lua_pop(localL, 1);
enc->data[writing_program].eon[eon].pty = pty;
} else lua_pop(localL, 1);
lua_getfield(localL, 2, "data");
if(!lua_isnil(localL, -1)) {
lua_Integer data = luaL_checkinteger(localL, -1);
lua_pop(localL, 1);
enc->data[writing_program].eon[eon].data = data;
} else lua_pop(localL, 1);
lua_getfield(localL, 2, "ps");
if(!lua_isnil(localL, -1)) _strncpy(enc->data[writing_program].eon[eon].ps, luaL_checklstring(localL, -1, NULL), 8);
lua_pop(localL, 1);
lua_getfield(localL, 2, "afs");
luaL_checktype(localL, -1, LUA_TTABLE);
int n = lua_rawlen(localL, -1);
if (n == 0) {
lua_pop(localL, 1);
memset(&(enc->data[writing_program].eon[eon].af), 0, sizeof(RDSAFs));
return 0;
}
@@ -302,29 +336,42 @@ int lua_set_rds_eon(lua_State *localL) {
memset(&new_af, 0, sizeof(RDSAFs));
for (int i = 1; i <= n; i++) {
lua_rawgeti(localL, 8, i);
if (lua_isnumber(localL, -1)) add_rds_af(&new_af, lua_tonumber(localL, -1));
else return luaL_error(localL, "number expected, got %s", luaL_typename(localL, -1));
lua_rawgeti(localL, -1, i);
if (!lua_isnumber(localL, -1)) {
const char *type = luaL_typename(localL, -1);
lua_pop(localL, 1);
return luaL_error(localL, "number expected, got %s", type);
}
add_rds_af(&new_af, lua_tonumber(localL, -1));
lua_pop(localL, 1);
}
memcpy(&(enc->data[writing_program].eon[eon].af), &new_af, sizeof(new_af));
enc->data[writing_program].eon[eon].data = luaL_checkinteger(localL, 9);
lua_pop(localL, 1);
return 0;
}
int lua_get_rds_eon(lua_State *localL) {
int eon = luaL_checkinteger(localL, 1);
if(eon >= EONs) return luaL_error(localL, "eon index exceeded");
lua_newtable(localL);
lua_pushboolean(localL, enc->data[writing_program].eon[eon].enabled);
lua_setfield(L, -2, "enabled");
lua_pushinteger(localL, enc->data[writing_program].eon[eon].pi);
lua_setfield(L, -2, "pi");
lua_pushboolean(localL, enc->data[writing_program].eon[eon].tp);
lua_setfield(L, -2, "tp");
lua_pushboolean(localL, enc->data[writing_program].eon[eon].ta);
lua_setfield(L, -2, "ta");
lua_pushinteger(localL, enc->data[writing_program].eon[eon].pty);
lua_setfield(L, -2, "pty");
lua_pushlstring(localL, enc->data[writing_program].eon[eon].ps, 8);
lua_createtable(localL, 0, 0); // don't have decoding for AF, so just return empty table
lua_setfield(L, -2, "ps");
lua_newtable(localL); // don't have decoding for AF, so just return empty table
lua_setfield(L, -2, "afs");
lua_pushinteger(localL, enc->data[writing_program].eon[eon].data);
return 8;
lua_setfield(L, -2, "data");
return 1;
}
int lua_set_rds_udg(lua_State *localL) {
+4 -78
View File
@@ -5,7 +5,6 @@
RDSEncoder* enc = NULL;
lua_State *L = NULL;
static pthread_mutex_t lua_mutex;
uint8_t unload_refs[33] = {LUA_REFNIL};
#define lua_registertotable(L,n,f) (lua_pushcfunction(L, (f)), lua_setfield(L, -2, (n)))
void init_lua(RDSEncoder* _enc) {
@@ -54,8 +53,8 @@ void init_lua(RDSEncoder* _enc) {
lua_registertotable(L, "set_program_defaults", lua_set_rds_program_defaults);
lua_pushinteger(L, PROGRAMS);
lua_setfield(L, -2, "max_programs");
lua_registertotable(L, "set_program", lua_set_rds_program);
lua_registertotable(L, "get_program", lua_get_rds_program);
lua_registertotable(L, "set_output_program", lua_set_rds_program);
lua_registertotable(L, "get_output_program", lua_get_rds_program);
lua_registertotable(L, "set_writing_program", lua_set_rds_writing_program);
lua_registertotable(L, "get_writing_program", lua_get_rds_writing_program);
lua_setglobal(L, "dp");
@@ -148,7 +147,6 @@ void init_lua(RDSEncoder* _enc) {
lua_setglobal(L, "rds");
if (luaL_loadfile(L, "/etc/rds95.lua") != LUA_OK) {
fprintf(stderr, "Lua error loading file: %s\n", lua_tostring(L, -1));
lua_pop(L, 1);
@@ -274,39 +272,6 @@ int lua_rds2_group(RDSGroup* group, int stream) {
return 1;
}
void lua_group_ref(RDSGroup* group, int ref) {
pthread_mutex_lock(&lua_mutex);
lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
if (lua_isfunction(L, -1)) {
if (lua_pcall(L, 0, 3, 0) == LUA_OK) {
if (!lua_isinteger(L, -1)) {
pthread_mutex_unlock(&lua_mutex);
lua_pop(L, 1);
return;
}
if (!lua_isinteger(L, -2)) {
pthread_mutex_unlock(&lua_mutex);
lua_pop(L, 1);
return;
}
if (!lua_isinteger(L, -3)) {
pthread_mutex_unlock(&lua_mutex);
lua_pop(L, 1);
return;
}
group->d = luaL_checkinteger(L, -1);
group->c = luaL_checkinteger(L, -2);
group->b = luaL_checkinteger(L, -3);
lua_pop(L, 3);
} else {
fprintf(stderr, "Lua error: %s at ref %d\n", lua_tostring(L, -1), ref);
lua_pop(L, 1);
}
} else lua_pop(L, 1);
pthread_mutex_unlock(&lua_mutex);
}
void lua_call_function_nolock(const char* function) {
lua_getglobal(L, function);
@@ -327,40 +292,6 @@ void lua_call_function(const char* function) {
pthread_mutex_unlock(&lua_mutex);
}
void lua_call_table_nolock(const char *table_name) {
lua_getglobal(L, table_name);
if (!lua_istable(L, -1)) {
lua_pop(L, 1);
return;
}
lua_Integer len = lua_rawlen(L, -1);
for (lua_Integer i = 1; i <= len; i++) {
lua_rawgeti(L, -1, i);
if (lua_isfunction(L, -1)) {
if (lua_pcall(L, 0, 0, 0) != LUA_OK) {
fprintf(stderr,
"Lua error: %s at '%s[%lld]'\n",
lua_tostring(L, -1),
table_name,
(long long)i);
lua_pop(L, 1);
}
} else lua_pop(L, 1);
}
lua_pop(L, 1); // pop table
}
void lua_call_table(const char* function) {
int need_lock = (pthread_mutex_trylock(&lua_mutex) == 0);
if (!need_lock) {
fprintf(stderr, "Warning: lua_mutex already locked when table calling %s\n", function);
return;
}
lua_call_table_nolock(function);
pthread_mutex_unlock(&lua_mutex);
}
void lua_call_tfunction_nolock(const char* name) {
lua_getglobal(L, "hooks");
if (!lua_istable(L, -1)) {
@@ -380,11 +311,8 @@ void lua_call_tfunction_nolock(const char* name) {
lua_rawgeti(L, -1, i);
if (lua_isfunction(L, -1)) {
if (lua_pcall(L, 0, 0, 0) != LUA_OK) {
fprintf(stderr,
"Lua error: %s at '%s[%lld]'\n",
lua_tostring(L, -1),
name,
(long long)i);
fprintf(stderr, "Lua error: %s at '%s[%lld]'\n",
lua_tostring(L, -1), name, (long long)i);
lua_pop(L, 1);
}
} else lua_pop(L, 1);
@@ -404,8 +332,6 @@ void lua_call_tfunction(const char* name) {
void destroy_lua() {
if (L) {
for (int i = 1; i < *unload_refs; i++) luaL_unref(L, LUA_REGISTRYINDEX, unload_refs[i]);
*unload_refs = 1;
lua_close(L);
L = NULL;
}
-3
View File
@@ -11,9 +11,6 @@ int lua_group(RDSGroup* group, const char grp);
int lua_rds2_group(RDSGroup* group, int stream);
void lua_call_function_nolock(const char* function);
void lua_call_function(const char* function);
void lua_call_table_nolock(const char *table_name);
void lua_call_table(const char* function);
void lua_call_tfunction_nolock(const char* name);
void lua_call_tfunction(const char* name);
void lua_group_ref(RDSGroup* group, int ref);
void destroy_lua();
+1 -1
View File
@@ -1,6 +1,6 @@
#pragma once
#include "common.h"
#define LUA_USER_DATA 1152
#define LUA_USER_DATA 1280
/* The RDS error-detection code generator polynomial is
* x^10 + x^8 + x^7 + x^5 + x^4 + x^3 + x^0