title: How do you call functions using function pointers in Lua with SWIG binding?
body: <p>I have C code which looks something like the following:</p>
  
  <pre><code>typedef int (*SIMPLEFUNC)(int);
  
  SIMPLEFUNC foo = NULL;
  
  void setup_foo(void) {
      foo = ...;
  }
  </code></pre>
  
  <p>I wrap this with SWIG with a bare-bones .i file, then can use it in Lua. I am able to call <code>setup_foo()</code> from Lua. I can see the variable <code>foo</code>. But I am unable to call <code>foo</code> as a function from Lua. It appears as userdata.</p>
  
  <pre><code>local mylib = require('mylib')
  mylib.setup_foo()
  print(mylib.foo)
  print(mylib.foo(3))
  </code></pre>
  
  <p>I get output like this:</p>
  
  <pre><code>setup_foo() has been called
  userdata: 0x1b441c8
  PANIC: [string "bootstrap0"]:86: attempt to call a userdata value (field 'foo')
  stack traceback:
      [string "bootstrap0"]:86: in main chunk
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>Is there a way to call function pointers such as <code>foo</code> from my Lua code when using SWIG?</p>
  
  <p>Is there some SWIG configuration that might help?</p>
  
  <p>If not, what is the best way to modify my C code to make it work?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>At a higher level, I solved this problem by switching to Selene instead of SWIG. Selene allows rebinding functions in the Lua state at runtime easily.
  <a href="https://github.com/jeremyong/Selene" rel="nofollow">https://github.com/jeremyong/Selene</a></p>