From 7cfac026e0f00449272f3fee232c560ac244dc35 Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jon.w.wilkes@gmail.com>
Date: Thu, 27 Jul 2017 22:08:51 -0400
Subject: [PATCH] allow class_addcreator to register an additional creator with
 the namespace prefix if one was used

This will allow legacy externals like iemmatrix and zexy to properly load
aliases the first time when the user instantiates an object with
[namespace_prefix/alias_name]

This becomes relevant when there are kludge classes that essentially just
"#include" the original C file of a class and add an alias_setup function
which just calls the original setup routine. (For example, see iemmatrix.)
However, such an approach still calls class_new with the original class name.
Thus "namespace_prefix/classname" gets added to the pd_objectcreator
methodspace, but "namespace_prefix/alias_name"-- which is what we want--
does not. This results in a series of 1000 recursive calls into
pd_objectmaker's new_anything method. (I.e., sys_load_lib finds the
"namespace_prefix/classname" is already loaded, new_anything_sends a
typedmess to pd_objectmaker's new_anything method, which calls
sys_load_lib, etc.)

To prevent this, we just make sure to add "namespace_prefix/alias" by
adding the relevant lines to class_addcreator. This should really be
happening anyway-- since internal classes don't have a class_loadsym
they aren't affected, and externals with aliases won't work with
namespace prefix without this.
---
 pd/src/m_class.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/pd/src/m_class.c b/pd/src/m_class.c
index f5926e98e..54cfa965d 100644
--- a/pd/src/m_class.c
+++ b/pd/src/m_class.c
@@ -318,6 +318,18 @@ void class_addcreator(t_newmethod newmethod, t_symbol *s,
     va_end(ap);
     class_addmethod(pd_objectmaker, (t_method)newmethod, s,
         vec[0], vec[1], vec[2], vec[3], vec[4], vec[5]);
+    if (class_loadsym)
+    {
+            /* if we're loading an extern it might have been invoked by a
+            longer file name; in this case, make this an admissible name
+            too. */
+        char *loadstring = class_loadsym->s_name,
+            l1 = strlen(s->s_name), l2 = strlen(loadstring);
+        if (l2 > l1 && !strcmp(s->s_name, loadstring + (l2 - l1)))
+            class_addmethod(pd_objectmaker, (t_method)newmethod,
+                class_loadsym,
+                vec[0], vec[1], vec[2], vec[3], vec[4], vec[5]);
+    }
 }
 
 void class_addmethod(t_class *c, t_method fn, t_symbol *sel,
-- 
GitLab