Menu

#1095 [lua] SWIGLUA_REF + default parameters -> bad type

open
lua (25)
5
2024-04-02
2010-06-16
Anonymous
No

SWIG 1.3 with Lua/fnptr.i generates bad type checking when dealing with a use case:
SWIGLUA_REF references in a function that has default parameters.

By default, when SWIG encounters a SWIGLUA_REF parameter, it ignores type-checking on that parameter.
In the dispatcher method for functions that are overloaded/have default parameters, it performs explicit type checking of
the form:
lua_isuserdata(L,argv[7])==0 || SWIG_ConvertPtr(L,argv[7], (void **) &ptr, SWIGTYPE_p_SWIGLUA_REF, 0)
This will kill any usage of SWIGLUA_REF in functions with default parameters.

Discussion

  • Olly Betts

    Olly Betts - 2010-09-27
    • summary: SWIG [lua]: SWIGLUA_REF + default parameters -> bad type --> [lua] SWIGLUA_REF + default parameters -> bad type
    • assigned_to: nobody --> mgossage
    • labels: --> lua
     
  • Olly Betts

    Olly Betts - 2022-01-30

    Still reproducible with git master it seems:

    diff --git a/Examples/lua/funcptr3/Makefile b/Examples/lua/funcptr3/Makefile
    index fb363cbe..99702b4b 100644
    --- a/Examples/lua/funcptr3/Makefile
    +++ b/Examples/lua/funcptr3/Makefile
    @@ -5,6 +5,7 @@ SRCS       = example.c
     TARGET     = example
     INTERFACE  = example.i
     LIBS       =
    +SWIGOPTS   = -c++
    
     check: build
            $(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' lua_run
    @@ -12,7 +13,7 @@ check: build
     build:
            $(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
            SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
    
    -       TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' lua
    +       TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' lua_cpp
    
     static:
            $(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
    diff --git a/Examples/lua/funcptr3/example.i b/Examples/lua/funcptr3/example.i
    index 319b2274..611e4c85 100644
    --- a/Examples/lua/funcptr3/example.i
    +++ b/Examples/lua/funcptr3/example.i
    @@ -66,4 +66,6 @@ void call_the_func(int a)
            }
     }      
    
    +int set_the_func(SWIGLUA_REF x, int y = 0) { the_func = x; return y; }
    +
     %}
    diff --git a/Examples/lua/funcptr3/runme.lua b/Examples/lua/funcptr3/runme.lua
    index 3cd58520..37ec0590 100644
    --- a/Examples/lua/funcptr3/runme.lua
    +++ b/Examples/lua/funcptr3/runme.lua
    @@ -49,6 +49,6 @@ example.call_the_func(5)
    
     function bar(i) print("bar",i) end
     print("setting to bar")
    -example.the_func=bar
    +example.set_the_func(bar)
     print("call_the_func(5)")
     example.call_the_func(5)
    
     
  • Olly Betts

    Olly Betts - 2024-04-02

    This patch fixes the example here, but seems a bit of a hack as it just allows anything to be passed for this parameter, but with the lowest typecheck precedence. Ideally we should actually check for what's accepted her, though the in typemap allows anything so maybe that's actually correct:

    diff --git a/Lib/lua/lua_fnptr.i b/Lib/lua/lua_fnptr.i
    index b4c663c5..8e67d8fb 100644
    --- a/Lib/lua/lua_fnptr.i
    +++ b/Lib/lua/lua_fnptr.i
    @@ -115,6 +115,9 @@ void swiglua_ref_get(SWIGLUA_REF* pref){
    
     %}
    
    +%typemap(typecheck, precedence=SWIG_TYPECHECK_SWIGOBJECT) SWIGLUA_REF
    +%{  $1 = 1; %}
    +
     %typemap(in) SWIGLUA_REF
     %{  swiglua_ref_set(&$1,L,$input); %}
    

    Also really needs test coverage.

     

Log in to post a comment.

MongoDB Logo MongoDB