Menu

#1080 Lua: C++ typename check fails for equally named classes

None
closed-fixed
lua (25)
5
2022-03-20
2010-03-17
sebastian
No

Suggest you have two SWIG modules, "A" and "B". Also suggest you use
different namespaces in both modules with equally named classes, say:

library A:

namespace A
{
class classname
{
//! constructor
classname(const std::string & ident);
...
};
}

and library B:

namespace B
{
class classname
{
//! constructor
classname(const int & id);
...
};
}

Then both libraries will be wrapped by SWIG into 2 different modules
and linked together. In Lua, you expect to be able to call:

a = A.classname("hello") -- call constructor of A::classname
b = B.classname( 3 ) -- call constructor of B::classname

SWIG actually merges equally class names from different namespaces into
one. That is, if the load_module routine of module A was called before
B, then it expects the syntax of A::classname, if B.classname( ... ) is
called.

I am using latest SWIG 1.3.40

Best regards
Sebastian

Discussion

  • Olly Betts

    Olly Betts - 2022-03-20
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,4 +1,3 @@
    -
     Suggest you have two SWIG modules, "A" and "B". Also suggest you use 
     different namespaces in both modules with equally named classes, say:
    
    • status: open --> closed-fixed
    • Group: -->
     
  • Olly Betts

    Olly Betts - 2022-03-20

    Seems to have been fixed since as the testcase works with SWIG 4.0.2.

    The testcode above was incomplete, so here's what I tested with:

    A.i

    %module A
    %include <std_string.i>
    %inline %{
    #include <string>
    namespace A
    {
    class classname
    {
    public:
    classname(const std::string &ident) {}
    };
    }
    %}
    

    B.i

    %module B
    %inline %{
    namespace B
    {
    class classname
    {
    public:
    classname(const int &id) {}
    };
    }
    %}
    

    runme.lua

    require "A"
    require "B"
    a = A.classname("hello")
    b = B.classname(2)
    

    runme

    #!/bin/sh -ex
    swig -lua -c++ A.i
    swig -lua -c++ B.i
    g++ -fPIC -shared -I/usr/include/lua5.2 A_wrap.cxx -o A.so
    g++ -fPIC -shared -I/usr/include/lua5.2 B_wrap.cxx -o B.so
    lua5.2 runme.lua
    

    If you can still reproduce, please show what you need to change in my working example.

     

Log in to post a comment.