What is wrong with my SWIG typemap?

Multi tool use
Multi tool use


What is wrong with my SWIG typemap?



I created a SWIG typemap for my function.


%typemap(in) (int width, int height, lua_State *L)
{
if (!lua_isnumber(L, -1) || !lua_isnumber(L, -2))
SWIG_exception(SWIG_RuntimeError, "argument mismatch: number expected");
$1 = lua_tonumber(L, -1);
$2 = lua_tonumber(L, -2);
$3 = L;
}



But it doesn't work if I try to call the function in Lua.



I call this function like the following in Lua.



createWindow(500,300)


createWindow(500,300)



I just want to pass width, height from Lua to this function.


width


height



How can I fix the typemap to make it work?





Actually for a function taking primitive types you don't need a typemap at all.
– Henri Menke
Jul 3 at 10:09





@HenriMenke Thanks. So I shouldn't pass lua_State* to C++ function? What should I do if I want to use lua_State* only inside the specific function and not typemap? Say, I have void createWindow(int width, int height)
– Zack Lee
Jul 3 at 10:09


lua_State*


lua_State*


void createWindow(int width, int height)





@HenriMenke Could you please show me how to do this not using the typemaps? I'm a newbie. please help me.
– Zack Lee
Jul 3 at 10:20





See my answer below.
– Henri Menke
Jul 3 at 10:26




1 Answer
1



Multi-argument typemaps won't do the trick here because they are designed to map a single Lua argument to multiple C++ arguments. You can help yourself with a default argument here. This is done using %typemap(default). We tell SWIG to default any lua_State *L argument to the instance used by SWIG.


%typemap(default)


lua_State *L


%module window

%{
#include <iostream>

void createWindow(int width, int height, lua_State *L) {
std::cout << "Creating window of size " << width << "x" << height << 'n'
<< "Number of arguments: " << lua_gettop(L) << 'n';
}
%}

%typemap(default) (lua_State *L) {
$1 = L;
}

void createWindow(int width, int height, lua_State *L);


local window = require("window")
window.createWindow(500,300)


$ lua5.2 test.lua
Creating window of size 500x300
Number of arguments: 2





This is exactly what I wanted to know. Thank you so much Sir.
– Zack Lee
Jul 3 at 10:33






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

E5gaubXCQEsosN,Gi6nL AW8hCkd0oCLkxZq9pDfhutyBCJqjpLuF6VXI,r9EfGZLa
FixCqj AEHZ0PNQAVESiDXMKCyeIpvSnF4eq43o,ob KnXvFH,AnaUVJv 8vvwEmGBR,5M

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications