Skip to content

Commit

Permalink
At Ben's request, added a get_args method.
Browse files Browse the repository at this point in the history
self:get_args() returns an object's arguments in a Lua table. float
atoms are returned as numbers, other types of atoms as a string using
their print representation in Pd syntax (including escapes of special
symbols according to Pd's quoting rules).

Note that there are some special kinds of Pd atoms such as pointers
which don't have a serializable string representation. But as long as
all arguments are float and symbol atoms, the resulting table can be
passed to self:set_table() to re-create the same object arguments.
  • Loading branch information
agraef committed Sep 24, 2024
1 parent f712456 commit e3927bd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,10 @@ function pd.Class:postinitialize() end

function pd.Class:finalize() end

function pd.Class:get_args()
return pd._get_args(self._object)
end

function pd.Class:set_args(args)
pd._set_args(self._object, args)
end
Expand Down
43 changes: 43 additions & 0 deletions pdlua.c
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,46 @@ static void pdlua_dsp(t_pdlua *x, t_signal **sp){
freebytes(sigvec, sigvecsize * sizeof(t_int));
}

static int pdlua_get_arguments(lua_State *L)
{
// Check if the first argument is a valid user data pointer
char msg[MAXPDSTRING];
if (lua_islightuserdata(L, 1))
{
// Retrieve the userdata pointer
t_pdlua *o = lua_touserdata(L, 1);
if (!o) {
pd_error(NULL, "%s: set_args: null object", src_info(L, msg));
return 0;
}

// Retrieve the binbuf
t_binbuf* b = o->pd.te_binbuf;

if (!b) return 0;
lua_newtable(L);
char buf[MAXPDSTRING];
const t_atom *ap;
int indx = binbuf_getnatom(b), i = 0;
for (ap = binbuf_getvec(b); indx--; ap++, i++) {
if (i == 0) continue; // skip 1st atom = object name
lua_pushnumber(L, i);
if (ap->a_type == A_FLOAT)
lua_pushnumber(L, ap->a_w.w_float);
else {
atom_string(ap, buf, MAXPDSTRING);
lua_pushstring(L, buf);
}
lua_settable(L, -3);
}
return 1;
} else {
pd_error(NULL, "%s: set_args: missing object", src_info(L, msg));
}

return 0;
}

static int pdlua_set_arguments(lua_State *L)
{
// Check if the first argument is a valid user data pointer
Expand Down Expand Up @@ -2629,6 +2669,9 @@ static void pdlua_init(lua_State *L)
lua_pushstring(L, "post");
lua_pushcfunction(L, pdlua_post);
lua_settable(L, -3);
lua_pushstring(L, "_get_args");
lua_pushcfunction(L, pdlua_get_arguments);
lua_settable(L, -3);
lua_pushstring(L, "_set_args");
lua_pushcfunction(L, pdlua_set_arguments);
lua_settable(L, -3);
Expand Down

0 comments on commit e3927bd

Please sign in to comment.